/*
	JavaScript: blendForms
	Version: 	3.6
	Date		2009-06-03  
	Author:		elementalblend.com (brandon burkett)
	
	Desc:		This class contains three objects: effects, validation, and form.  Effects
				controlls the hover, focus, and colors of the form.  Validation holds
				the validation regex used to tell if a field is required or not.  The form
				class process the validation for the form and display a message.
				
				Use class noValidation on any form you do not wish to have validation checks on
				
	Note:		The validation and effects objects can be edited, but avoid edting the form class.
*/
var effects={focusClass:'inputActive',highlight:function(jqElem)
{$('input:text').removeClass(effects.focusClass);$('input:password').removeClass(effects.focusClass);$('input:file').removeClass(effects.focusClass);$('input:checkbox').removeClass(effects.focusClass);$('input:radio').removeClass(effects.focusClass);$('textarea').removeClass(effects.focusClass);$('select').removeClass(effects.focusClass);$(jqElem).addClass(effects.focusClass);},init:function()
{$('input:text').bind('focus',function(){effects.highlight($(this));});$('input:password').bind('focus',function(){effects.highlight($(this));});$('input:file').bind('focus',function(){effects.highlight($(this));});$('input:radio').bind('click',function(){effects.highlight($(this));});$('input:checkbox').bind('click',function(){effects.highlight($(this));});$('textarea').bind('focus',function(){effects.highlight($(this));});$('select').bind('focus',function(){effects.highlight($(this));});}}
var validation={text:{regex:/.+?/,error:'This information is required.','state':{regex:/^[a-zA-Z][a-zA-Z]$/,error:'Please enter a valid state.  Ex. TX'},'phone':{regex:/^[- ()0-9+\.]+$/,'error':'Please enter a valid phone number.  Ex. 123-456-7899'},'decimal':{regex:/^[.0-9]+$/,error:'Please enter a valid amount.  Ex. 19.95'},'integer':{regex:/^[0-9]+$/,error:'Please enter a number.  Ex. 10001'},'email':{regex:/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/,error:'Please enter a valid email address. Ex. name@domain.com'},'zip':{regex:/^[0-9][0-9][0-9][0-9][0-9]$/,error:'Please enter a valid zip code.  Ex. 78610'},'date':{regex:/^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$/,error:'Please enter a valid date.  Ex. 05-27-2008'},'euroDate':{regex:/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$/,error:'Please enter a valid date.  Ex. 2008-05-27'}},file:{regex:/.+?/,error:'This information is required.'},password:{regex:/.+?/,error:'This information is required.'},textarea:{regex:/.+?/,error:'This information is required.'},radio:{regex:/.+?/,error:'Please select one.'},checkbox:{regex:/.+?/,error:'Please select one.'},select:{regex:/.+?/,error:'Please select one.'}}
var forms={buttonImage:new Image(),buttonImageHeight:0,buttonImageWidth:0,processText:'processing...',data:[],errorTotal:0,errors:[],messageClass:'formWarnings',setData:function()
{$('form').each(function()
{var formId=$(this).attr('id');var formFields=[];if($(this).attr('class').indexOf('noValidation')==-1)
{$('#'+formId+' ol li').each(function()
{var inputType='text';var inputId=$(this).find('input:text').attr('id');var labelFor=inputId;var value='';var isArray=false;var valid=true;if(!inputId)
{inputType='password';inputId=$(this).find('input:password').attr('id');labelFor=inputId;}
if(!inputId)
{inputType='file';inputId=$(this).find('input:file').attr('id');labelFor=inputId;}
if(!inputId)
{inputType='radio';inputId=$(this).find('input:radio').attr('id');labelFor=$(this).find('input:radio').attr('name');isArray=(labelFor)?(labelFor.indexOf('[]')!=-1)?true:false:false;labelFor=(labelFor)?labelFor.replace('[]',''):'';$.each(formFields,function(i,thisField)
{if(valid)
{valid=(thisField.labelFor==labelFor)?false:true;}});}
if(!inputId)
{inputType='checkbox';inputId=$(this).find('input:checkbox').attr('id');labelFor=$(this).find('input:checkbox').attr('name');isArray=(labelFor)?(labelFor.indexOf('[]')!=-1)?true:false:false;labelFor=(labelFor)?labelFor.replace('[]',''):'';$.each(formFields,function(i,thisField)
{if(valid)
{valid=(thisField.labelFor==labelFor)?false:true;}});}
if(!inputId)
{inputType='select';inputId=$(this).find('select').attr('id');labelFor=inputId;if(inputId)
{var name=$(this).find('select').attr('name');isArray=(name.indexOf('[]')!=-1)?true:false;}}
if(!inputId)
{inputType='textarea';inputId=$(this).find('textarea').attr('id');labelFor=inputId;}
if(!inputId)
{inputType='submit';var submit=$(this).find('input:submit');inputId=submit.attr('id');value=submit.val();}
if($('label[for='+labelFor+']').length>0)
{var label=$('label[for='+labelFor+']');}
else
{var label=$('#'+labelFor);}
var emText=label.children('em').html();var required=(emText!='optional'&&inputType!='submit')?true:false;var label=(label.html())?label.html():'';if(valid)
{formFields.push({id:inputId,required:required,type:inputType,value:value,label:label,labelNote:emText,labelFor:labelFor,isArray:isArray});}});var formObject={id:$(this).attr('id'),fields:formFields};forms.data.push(formObject);}});},clearData:function()
{forms.errors=[];forms.errorTotal=0;$('input:text').removeClass(effects.focusClass);$('input:password').removeClass(effects.focusClass);$('input:radio').removeClass(effects.focusClass);$('input:checkbox').removeClass(effects.focusClass);$('textarea').removeClass(effects.focusClass);$('select').removeClass(effects.focusClass);$('label').removeClass(effects.focusClass);$('.formMessageImportant').html('').hide();$.each(forms.data,function(i,form)
{$.each(form.fields,function(i,field)
{if($('label[for='+field.labelFor+']').length>0)
{$('label[for='+field.labelFor+']').html(field.label);}
else
{$('#'+field.labelFor).html(field.label);}});});},process:function(jqObject)
{forms.clearData();var formId=jqObject.attr('id');forms.toggleButton('disable',formId);$.each(forms.data,function(i,form)
{if(form.id==formId)
{$.each(form.fields,function(i,field)
{if(field.required==true)
{if((field.type=='radio'||field.type=='checkbox')&&field.isArray)
{var valid=false;$('#'+formId+' input[name="'+field.labelFor+'[]"]').each(function()
{if(!valid&&$(this).is(':checked'))
{valid=true;}});if(!valid)
{forms.errors.push({msg:field.label+' - '+validation[field.type].error,labelFor:field.labelFor,labelMsg:validation[field.type].error,fieldId:field.id});forms.errorTotal++;}}
else if((field.type=='select')&&field.isArray)
{var valid=false;$('#'+formId+' select[name="'+field.labelFor+'[]"]').each(function()
{if(!valid&&($(this).val()!=null&&$(this).val()!=''))
{valid=true;}});if(!valid)
{forms.errors.push({msg:field.label+' - '+validation[field.type].error,labelFor:field.labelFor,labelMsg:validation[field.type].error,fieldId:field.id});forms.errorTotal++;}}
else
{var validationType=field.id.split('_');var jqField=$('#'+field.id);if(validationType[1])
{if(validation[field.type][validationType[1]])
{if(!$.trim(jqField.val()).match(validation[field.type][validationType[1]].regex))
{forms.errors.push({msg:field.label+' - '+validation[field.type][validationType[1]].error,labelMsg:validation[field.type][validationType[1]].error,labelFor:field.labelFor,fieldId:field.id});forms.errorTotal++;}}
else
{if(!$.trim(jqField.val()).match(validation[field.type].regex))
{forms.errors.push({msg:field.label+' - '+validation[field.type].error,labelMsg:validation[field.type].error,labelFor:field.labelFor,fieldId:field.id});forms.errorTotal++;}}}
else
{if(field.type=='checkbox'||field.type=='radio')
{if(!jqField.is(':checked'))
{forms.errors.push({msg:field.label+' - '+validation[field.type].error,labelMsg:validation[field.type].error,labelFor:field.labelFor,fieldId:field.id});forms.errorTotal++;}}
else
{if(!$.trim(jqField.val()).match(validation[field.type].regex))
{forms.errors.push({msg:field.label+' - '+validation[field.type].error,labelMsg:validation[field.type].error,labelFor:field.labelFor,fieldId:field.id});forms.errorTotal++;}}}}}});}});return forms.displayMessage(formId);},displayMessage:function(formId)
{if(forms.errorTotal>0)
{var formMsg='<p>The following errors were found when trying to submit your information:</p>'+'<ul class="errorList">';$.each(forms.errors,function(i,error)
{formMsg+='<li><em>'+error.msg+'</em></li>';var thisLabel;if($('label[for='+error.labelFor+']').length>0)
{thisLabel=$('label[for='+error.labelFor+']');}
else
{thisLabel=$('#'+error.labelFor);}
thisLabel.html(thisLabel.html()+' <strong class="inputError">'+error.labelMsg+'</strong>');});formMsg+='</ul>';var formMessageContainer=$('#'+formId).find('.'+forms.messageClass);formMessageContainer.html(formMsg).show();formMessageContainer.attr('id',formId+'Errors');alert('There were errors while trying to submit the form.');window.location=window.location.pathname+window.location.search+'#'+formId+'Errors';forms.toggleButton('enable',formId);return false;}
return true;},toggleButton:function(action,formId)
{$.each(forms.data,function(i,form)
{if(form.id==formId)
{$.each(forms.data[i].fields,function(x,field)
{if(field.type=='submit')
{if(action=='disable')
{$('#'+field.id).attr('disabled','disabled').val(forms.processText);}
else if(action=='enable')
{$('#'+field.id).attr('disabled','').val(field.value);}}});}});},rebuildFormData:function()
{forms.data=[];forms.errorTotal=0;forms.errors=[];forms.setData();},init:function()
{forms.setData();$('form').bind('submit',function(){return forms.process($(this))});}}
$(document).ready(function(){forms.init();effects.init();});