/*#####################################################################
##																	 ##
##	 .d888  888                    .d8888b.   .d8888b.   .d8888b.    ##
##	 d88P"  888                   d88P  Y88b d88P  Y88b d88P  Y88b   ##
##	 888    888                        .d88P 888        888    888   ##
##	 888888 888  .d88b.  888  888     8888"  888d888b.  888    888   ##
##	 888    888 d8P  Y8b `Y8bd8P'      "Y8b. 888P "Y88b 888    888   ##
##	 888    888 88888888   X88K   888    888 888    888 888    888   ##
##	 888    888 Y8b.     .d8""8b. Y88b  d88P Y88b  d88P Y88b  d88P   ##
##	 888    888  "Y8888  888  888  "Y8888P"   "Y8888P"   "Y8888P"    ##
##																	 ##
##	 formHandler 0.8												 ##
##		Release: 3.25.2009											 ##
##		Author: Lance Corder										 ##
##																	 ##
##   Dependencies:													 ##
##   	jQuery >=1.2.3 (http://jquery.com)							 ##
##   	jQuery sprintf plugin >=1.0.2 (http://tinyurl.com/d6gs6h)	 ##
##   																 ##
#######################################################################
#####################################################################*/

//#####################################################################
//##  Globals														 ##
//#####################################################################
var __global_formError = 'You left the "%(rel)s" field empty. Please make sure you provide the necessary information.';
var __global_formattingError = '"%(val)s" is not a properly formatted %(typ)s.';
var __global_requiredTitle = 'required';
var __global_ghostTitle = 'ghost';
var __global_nolabelTitle = 'nolabel';
var __global_formatTitle = 'format';
/* NO NEED TO EDIT PAST THIS LINE */

//#####################################################################
//#####################################################################
//##  DOM															 ##
//#####################################################################
$(document).ready( function() { $('label').placeLabels(); });
//##############################################################################
//##############################################################################
//##  Form Handler Functions												  ##
//##############################################################################
$.fn.placeLabels = function() {
  this.each(function(index,text) {
        var label = $(this);
        var id = this.htmlFor || label.attr('for');
        if (id) {
          var searchIn = id.toString();
          var idTrimmed = searchIn.replace(/[^a-zA-Z]+/g,'');
        
	      //attach proper labels to fields [text/textarea/checkbox/select]
          var field = $("#"+id+"[class*='"+__global_requiredTitle+"']");
          var output = label.text();
          if (field) { field.attr("rel",output); }
        
          //attach proper labels to fields [radio]
          var radioFields = $('input[type*="radio"].'+idTrimmed).get();
          if (radioFields.length) {
            $.each(radioFields, function(){
              var field = $("#"+$(this).attr('id')+"[class*='"+__global_requiredTitle+"']");
	          if (field) { field.attr("rel",output); }
            });
          }
	    
		  //check for hidden labels
		  var hide = $("#"+id+"[class*='"+__global_nolabelTitle+"']");
		  if (document.getElementById(hide.attr('id'))) { label.hide().text(); }
		
		  //check for ghosted elements
          var control = $("#"+id+"[class*='"+__global_ghostTitle+"']");
		  control.focus(function () { if (this.value === this.getAttribute("rel")) { this.value=''; } })
			     .blur(function () { if ((this.value === '') && (this.getAttribute("rel") == label.text())) { this.value=output; }
          });
        }
    });
}; //end placeLabels
$.fn.findParents = function() { 
    this.each(function() {
      parentInfo=new Object();
      parentInfo.parentForm = $(this).parents()
	  					  	  		 .filter('form')
                          	  		 .map(function () { return this.id; })
                          	  		 .get();
      //get parent div for output target
      var parentDivList = $("div:has(form[id*='"+parentInfo.parentForm+"'])");
      var parentDivItem = parentDivList[(parentDivList.length - 1)];
      parentInfo.parentDiv = parentDivItem.id;
	  //get form action
	  parentInfo.parentFormAction = $('#'+parentInfo.parentForm).attr('action');
    });
    return parentInfo;
}; //end findParents
$.fn.checkRequired = function(parentInfo) { 
    var halt=0;
    this.each(function() {
      
      var contactFields = $('.'+__global_requiredTitle).get();
      $.each(contactFields, function(){
        var loopForm = $(this).parents().filter('form').map(function () { return this.id; }).get();
        //look at only the correct form
        if (loopForm.toString() == parentInfo.parentForm.toString()) {
          var fieldType=$(this).attr('type');
          switch (fieldType) {
            case 'radio':
              var fieldName = $(this).attr('name');
              fieldName = fieldName.replace(/[^a-zA-Z]+/g,'');
			  fieldValue = $("input:radio."+fieldName+":checked").val();
              if (!fieldValue) {
                var error = $.sprintf(__global_formError,{rel: $(this).attr("rel")});
                halt++; alert(error); $(this).focus(); return false;
              }
              break;
            default:
              //setup formatting
              var formatting = [{ term: /email/gi, filter: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, err: "email address" },
			  					{ term: /phone/gi, filter: /\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/i, err: "phone number" }];
              //check formatting
              var format = $("#"+$(this).attr('id')+"[class*='"+__global_formatTitle+"']");
              if (format.attr('id')) {
				var formatName = format.attr('name').toString();
				var formatVal = format.val().toString();
				$.each(formatting, function(elm, obj) {
				  var isTerm = (formatName.search(obj.term) > -1)?true:false;
                  if ((isTerm) && (formatVal)) {
                    if (formatVal.search(obj.filter) == -1) {
                      var error = $.sprintf(__global_formattingError,{val: formatVal, typ: obj.err});
                	  halt++; alert(error); $(this).focus(); return false;
                    }
                  }
				});
              }
              //check values
              if (($(this).val()=="") || ($(this).val()=="notset")) {
                var error = $.sprintf(__global_formError,{rel: $(this).attr("rel")});
                halt++; alert(error); $(this).focus(); return false;
              }
          }
        }
      });
      return halt;
    });
    if (halt) { return false; }
    else { return true; }
}; //end checkRequired
//##############################################################################
//##############################################################################
//##  jQuery Firebug Console Tie											  ##
//##############################################################################
jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
//##############################################################################
//##############################################################################
