$(document).ready(function(){
	
	$('input.required').each(function(index) {		
		$(this).blur(function() {
			if( $(this).attr('value') == '' || $(this).attr('value') == null  ){
				$(this).css('background', '#f7eeee');
			}else{				
				$(this).css('background', '#eff7e8');
			}
		});
	});
	
	$('textarea.required').each(function(index) {		
		$(this).blur(function() {
			if( $(this).attr('value') == '' || $(this).attr('value') == null  ){
				$(this).css('background', '#f7eeee');
			}else{				
				$(this).css('background', '#eff7e8');
			}
		});
	});
	
	$('input.required_email').each(function(index) {		
		$(this).blur(function() {
			var email = $(this).attr('value');
			if( email == '' ){
				$(this).css('background', '#f7eeee');
			}else{
				if( validate_email(email) ){
					$(this).css('background', '#eff7e8');
				}else{
					$(this).css('background', '#f7eeee');
				}
			}
		});
	});

});

function validateFormSend(){
	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.css('background', '#f7eeee');
			required_field.siblings('.error').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.css('background', '#f7eeee');
			required_field.siblings('.error').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).css('background', '#f7eeee');
			$(this).siblings('.error').css('display', 'block');
		}else{
			if( !validate_email(email) ){
				formCorrect = false;
				$(this).css('background', '#f7eeee');
				$(this).siblings('.error').css('display', 'block');
			}			
		}		
	});
	
	if(formCorrect){
		return true;
	}else{
		return false;	
	}
	
}

function validate_email($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;	
	}
}
