$(document).ready(function(){
	clear_form_validation.liveFormValidation();	
});

var clear_form_validation = function() {
	
	var liveFormValidation = function () {
		//input validation		
		$('input.required').each(function(index) {		
			$(this).blur(function() {							  
				if( $(this).attr('value') == '' || $(this).attr('value') == null  ){
					$(this).parent('div.form_element_wrap').addClass('form_alert');
				}else{	
					$(this).parent('div.form_element_wrap').removeClass('form_alert');
					$(this).siblings('.error_msg').css('display', 'none');
				}
			});
		});
		//textarea validation
		$('textarea.required').each(function(index) {		
			$(this).blur(function() {
				if( $(this).attr('value') == '' || $(this).attr('value') == null  ){
					$(this).parent('div.form_element_wrap').addClass('form_alert');
				}else{				
					$(this).parent('div.form_element_wrap').removeClass('form_alert');
					$(this).siblings('.error_msg').css('display', 'none');
				}
			});
		});
		//email validation
		$('input.required_email').each(function(index) {		
			$(this).blur(function() {
				var email = $(this).attr('value');
				if( email == '' ){
					$(this).parent('div.form_element_wrap').addClass('form_alert');
				}else{
					if( validate_email(email) ){
						$(this).parent('div.form_element_wrap').removeClass('form_alert');
						$(this).siblings('.error_msg').css('display', 'none');
					}else{
						$(this).parent('div.form_element_wrap').addClass('form_alert');
					}
				}
			});
		});
	};//END: liveFormValidation()
	
	var validateFormSend = function () {
		var formCorrect = true;	
		//check required text fields
		$('input.required').each(function(index) {
			var required_field = $(this);
			if( required_field.attr('value') == '' || required_field.attr('value') == null ){
				required_field.parent('div.form_element_wrap').addClass('form_alert');
				required_field.siblings('.error_msg').css('display', 'block');
				formCorrect = false;
			}
		});
		$('textarea.required').each(function(index) {
			var required_field = $(this);
			if( required_field.attr('value') == '' || required_field.attr('value') == null ){
				required_field.parent('div.form_element_wrap').addClass('form_alert');
				required_field.siblings('.error_msg').css('display', 'block');
				formCorrect = false;
			}
		});
		//check required_email fields
		$('input.required_email').each(function(index) {
			var email = $(this).attr('value');
			if( email == '' ){
				formCorrect = false;
				$(this).parent('div.form_element_wrap').addClass('form_alert');
				$(this).siblings('.error_msg').css('display', 'block');
			}else{
				if( !validate_email(email) ){
					formCorrect = false;
					$(this).parent('div.form_element_wrap').addClass('form_alert');
					$(this).siblings('.error_msg').css('display', 'block');
				}			
			}		
		});
		
		if(formCorrect){
			return true;
		}else{
			$('#form_feedback').css('display', 'block');
			return false;	
		}
		
	};	
	
	var validate_email = function ($email) {
		//Validates a correctly formatted email address
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test($email)) {
			// failed validation do what you want here 
			return false;
		}else{
			return true;	
		}
	};
	
	return {
		validate_email : validate_email,
		validateFormSend : validateFormSend,
		liveFormValidation : liveFormValidation
	};
	
}();
