
$(document).ready(function(){
	
	/* INPUT FOCUS */
	$('.site-form input,.site-form textarea,.site-form select').focus(function(){ $(this).addClass('focus'); });
	$('.site-form input,.site-form textarea,.site-form select').blur(function(){ $(this).removeClass('focus'); });
	
	/* FORM SUBMITTED TO SERVER */
	$('.site-form abbr.error').each(function(){
	
		label = $(this).parent(); //grab label object
		input = $('#'+label.attr('for')); //grab input object
		if($(input).val() ==''){
			$(input).bind("keydown",function(){
				$(this).parent().removeClass('error');
				$(this).parent().children('label').children('abbr').removeClass('error');
				$(this).parent().children('label').children('span.error').remove();
			}); //remove error message while being corrected		
		}
		if($(this).hasClass('email')){
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);  

			$(input).bind("keydown",function(){
				//remove error message while being corrected
				if(pattern.test( $(this).val() )){
					$(this).parent().removeClass('error');
					$(this).parent().children('label').children('abbr').removeClass('error');
					$(this).parent().children('label').children('span.error').remove();
				}
			});
		} //close if email
		
	}); //close each
	
});



/*VALIDATE FORM*/
function errorInputText(id,str){
	if(!id.hasClass('error-display')){ //prevent multiple error displays on same element
		id.addClass('error-display'); //prevent displaying error twice
		id.parent().addClass('error');
		id.parent().children('label').children('abbr').addClass('error');
		id.parent().children('label').append(' <span class="error"><span></span>'+ str +'</span>');
	}
	if(!window.focused){ id.focus(); window.focused = true; } //focused on first error
	return true;
}

function validateForm(){
	$('abbr,label,.item').removeClass('error');
	$('span.error').remove();
	$('input,select,textarea').removeClass('error-display'); //prevent multiple error displays on same element
	window.focused = false; //remove true
	var errors = false;
	var label = '';
	var input = '';
	
	//cycle required fields
	$('.site-form .required').each(function(){
	
		label = $(this).parent(); //grab label object
		input = $('#'+label.attr('for')); //grab input object
		
		//validate nonempty fields
		if($(input).val() =='' && !$(this).hasClass('email')){
			errors = errorInputText($(input),''); //apply error message
			$(input).bind("keydown",function(){
				$(this).parent().removeClass('error');
				$(this).parent().children('label').children('abbr').removeClass('error');
				$(this).parent().children('label').children('span.error').remove();
			}); //remove error message while being corrected
		}
		
		//validate email address		
		if($(this).hasClass('email')){
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);  
			if(!pattern.test( $(input).val() )){
				errors = errorInputText($(input),''); //apply error message
				$(input).bind("keydown",function(){
					//remove error message while being corrected
					if(pattern.test( $(this).val() )){
						$(this).parent().removeClass('error');
						$(this).parent().children('label').children('abbr').removeClass('error');
						$(this).parent().children('label').children('span.error').remove();
					}
				}); //remove error message while being corrected
			}  
		}
	
	});
	if(errors){	return false; } 
	else {return true; }
}
/*VALIDATE FORM*/
