/*---------------------------------------------------------------------------------*\
	jQuery Form Validation 06/2010
	Loops through required fields, and returns errors for empty values.
	
							Form Validation Usage
	FIELDS
	- Default/confirmed field containers have a class of "fieldBox."
	- Field containers with an error have a class of "pinkBox."
	- Fields that are required must have the class "required" attached.
	- The container element must have an ID of "c_{ID of field}," as well as
	  the class "fieldBox" with no other classes attached.
		* If the input ID is "email," the container with the ID of "c_email"
		  is required to modify the element class for.
	- There must be an element with an ID of "e_{ID of field}"
		* If the input ID is "email," an element with the ID of "e_email" 
		  is required to display the error message.
	- The "alt" property should be set to the desired display name.
		* <input name="email" id="email" alt="Email Address" class="required" />
		  If the field is left empty "Email Address is required" will display.
	
	CODE
	- Within the "$(document).ready..." parameters, the following must exist:
		
		var formID='#{ID OF THE FORM TO VALIDATE}';
		$(formID).submit(function(){
			var errors='';
			errors = validateForm(formID);
				// {Any other pre-return code}
				
			return (errors==0 ? true : false);
		});
	- formID must be the exact ID of the form to be validated
		* A form with the ID of "registration" would need formID='#registration';
	- Any other pre-return code for error validation within this function 
	  must increment the "errors" variable.
		
\*----------------------------------------------------------------------------------*/
<!--

var errorMessages='';

function validateForm(formID){
	var errors=0,id=0; errorMessages='';

	$(formID+' input').each(function(){	
		if($(this).hasClass('required')){
			var id=$(this).attr('id');
			if(id!='' && $(this).val()==''){++errors;isError(id,1);}else{isNotError(id);}
		}
	});
	$(formID+' select').each(function(){	
		if($(this).hasClass('required')){
			var id=$(this).attr('id');
			if(id!='' && $(this).val()==''){++errors;isError(id,1);}else{isNotError(id);}
		}
	});
	$(formID+' textarea').each(function(){
		if($(this).hasClass('required')){
			var id=$(this).attr('id');
			if(id!='' && $(this).val()==''){++errors;isError(id,1);}else{isNotError(id);}
		}
	});

	$(formID+' .error.focus:first').focus();
	
	return errors;
}
function validateLogin(formID){
	var errors=0,id=0; errorMessages='';
	$('input').each(function(){
		var id=$(this).attr('id');
		isNotError(id);
	});
	$(formID+' input').each(function(){
		if($(this).hasClass('required')){
			var id=$(this).attr('id');
			if(id!='' && $(this).val()==''){++errors;isError(id,1);}else{isNotError(id);}
		}
	});
	
	$(formID+' .focus:first').focus();
	
	if(errors != 0){
		var err_box='<div id="errorBoxSM"><div class="txt"><p class="header">We\'re Sorry</p>';
		err_box+='<div id="resultMessage">'+errorMessages+'</div>';
		err_box+='</div></div><div class="clear"></div>';
		
		if($('#resultMessage').length == 0){
			$('#resultBoxSM').append(err_box);
		}else{
			$('#resultMessage').html(errorMessages);
		}
		
	}
	
	return errors;
}
function validateEmail(id_email){
	var error=0,val=$('#'+id_email).val(),
	emailFilter= /^[^@]+@[^@.]+\.[^@]*\w\w$/,illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
	if(val==''){error=1;isError(id_email,1);}
	else if(!emailFilter.test(val) || val.match(illegalChars)){error=1;isError(id_email,2);}
	else{isNotError(id_email);}
	return error;
}
function validatePhone(id_phone){
	var error=0;
	phone = $('#'+id_phone).val().replace(/[\(\)\.\-\?\*\#\ ]/g, '');
	if(phone==''){error=1;isError(id_phone,1);}
	else if(phone.search(/\d{3}\d{3}\d{4}/) < 0){error=1;isError(id_phone,2);}
	else{isNotError(id_phone);}
	return error;
}
function isError(id,type){
	$('#'+id).removeClass('focus');
	$('#'+id).addClass('error');
	$('#'+id).addClass('focus');
	insertError(id,type);
	return false;
}
function isNotError(id){
	if(id!=''){
		var lenE = $('#e_'+id).length;
		if(lenE > 0){
			$('#e_'+id).html('');
		}
		$('#'+id).removeClass('error');
	}
	return false;
}
function insertError(id,type){
	var errorText='',altText='',lenE=0;
	
	altText=$('#'+id).attr('alt');
	if(altText!='' && altText!='undefined' && altText!=null){
		msg=$('#'+id).attr('alt')+(type!=3?' is '+(type==1?'required':'invalid')+'.':'');
	}else{
		msg=(type==1?'Required':'Invalid');	
	}
	errorMessages+='<p>- '+msg+'</p>';
	
	lenE = $('#e_'+id).length;
	if(lenE > 0){
		errorText='<img src="/images/red_arrow.png" alt="&lt;" /> '+msg;
		$('#e_'+id).html(errorText);
	}
}
-->
