$(document).ready(function() {

	var isValid = new Array(false,false,false);

	

	// Hide submit button
	$("#contact-form").find("input[type=submit]").attr("disabled","true");
	
 	// Append error message containers
	$("#contact-form").find(".form-errorpanel").append("<div class='message' id='msg_1'>Please enter your name</div>");
	$("#contact-form").find(".form-errorpanel").find("div[id=msg_1]").hide();
	
	$("#contact-form").find(".form-errorpanel").append("<div class='message' id='msg_2'>Please enter a valid email address</div>");
	$("#contact-form").find(".form-errorpanel").find("div[id=msg_2]").hide();
	
	$("#contact-form").find(".form-errorpanel").append("<div class='message' id='msg_3'>Please enter a message</div>");
	$("#contact-form").find(".form-errorpanel").find("div[id=msg_3]").hide();
	
	
	$("#contact-form").find("input[name=name]").blur(function() {
    	
		checkNameField($(this).attr("value"));
	});
	
	$("#contact-form").find("input[name=email]").blur(function() {
    	
		checkEmailField($(this).attr("value"));
		
	});
	
	$("#contact-form").find("textarea[name=comment]").blur(function() {
    	
		checkMessageField($(this).attr("value"));
		
	});
  	
	
	
	function checkAllFields(){
	
		var val = true;
	 
		for(var n=0;n<isValid.length;n++){
			if (isValid[n] == false) {
				val = false;
			}
		}
	
		if(val){
			$("#contact-form").find("input[type=submit]").removeAttr("disabled");		
		}
	
		else{
			$("#contact-form").find("input[type=submit]").attr("disabled","true");
		}
	
	}
	
	
	
	function checkNameField(strg){
	
	
	
		if (strg.length < 1) {
			$("#contact-form").find(".form-errorpanel").find("div[id=msg_1]").show();
			isValid[0] = false;
		}
	
		else{
			$("#contact-form").find(".form-errorpanel").find("div[id=msg_1]").hide();
			isValid[0] = true;
		}
	
		checkAllFields();
	
	}
	
	
	function checkEmailField(strg){
	
		checkForChar(strg,"@");
	
		if (strg.length < 3 || checkForChar(strg,"@")==false || checkForChar(strg,".")==false) {
			$("#contact-form").find(".form-errorpanel").find("div[id=msg_2]").show();
			isValid[1] = false;
			}
		
		else{
			$("#contact-form").find(".form-errorpanel").find("div[id=msg_2]").hide();
			isValid[1] = true;
			}
		
		checkAllFields();
		
	}
	
	
	function checkForChar(strg,c){

		var flag = false;
		
		if(strg.indexOf(c)>=0) flag=true;
		
		return flag;
	}
		

	
	function checkMessageField(strg){
	
		if (strg.length < 3) {
			$("#contact-form").find(".form-errorpanel").find("div[id=msg_3]").show();
			isValid[2] = false;
		}
			
		else{
			$("#contact-form").find(".form-errorpanel").find("div[id=msg_3]").hide();
			isValid[2] = true;
		}
	
		checkAllFields();
		
	}
	
	
		
});

