// JavaScript validation, client side validation
$(document).ready(function(){

	$("#email").click(function(){
	  $("#emailError").css('visibility','hidden');
	});
	$("#name").click(function(){
	  $("#nameError").css('visibility','hidden');
	});
	$("#contactEmail").click(function(){
	  $("#emailError").css('visibility','hidden');
	});
	$("#subject").click(function(){
	  $("#subjectError").css('visibility','hidden');
	});
	$("#message").click(function(){
	  $("#messageError").css('visibility','hidden');
	});


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

	$("#name").click(function(){
	  $("#name2Error").animate({ opacity: "hide" }, "slow");
	});
	$("#subject").click(function(){
	  $("#subject2Error").animate({ opacity: "hide" }, "slow");
	});
	$("#email").click(function(){
	  $("#email2Error").animate({ opacity: "hide" }, "slow");
	});
	$("#message").click(function(){
	  $("#questionError").animate({ opacity: "hide" }, "slow");
	});


});
function validate(fld) { 				//function validates form onsubmit
	var isError=new Boolean(false);				//note: the reason there are no function calls is because onsubmit() returns any 
	str=fld.email.value;						//returns to any functions calls therefore making validation useless. 
	var at="@";
	var dot=".";		
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);			
	// Declaring required variables
	
	var i;
    var theString = "";
	if((fld.name.value=="Name:") || (fld.name.value== null))				//null string validation on name
	{
		$("#nameError").css('visibility', 'visible');

		isError=true;
	}
   if((fld.subject.value=="Subject:") || (fld.subject.value== null))				//null string validation on name
	{
		$("#subjectError").css('visibility', 'visible');
		isError=true;
	}
	if((fld.message.value=="Message:") || (fld.message.value== null) || (fld.message.value==""))				//null string validation on name
	{
		$("#messageError").css('visibility', 'visible');
		isError=true;
	}
	if((fld.email.value=="Email:") || (fld.email.value== null) )		//validation for email 
	{
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}
	else if (str.indexOf(at)==-1){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}

	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}

	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}

	else if (str.indexOf(at,(lat+1))!=-1){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}

	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}

	else if (str.indexOf(dot,(lat+2))==-1){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}
		
	else if (str.indexOf(" ")!=-1){
		$("#emailError").css('visibility', 'visible');
		isError=true;
	}
	if(isError==true)												//if error exist return false to onsubmit()
	{
		return false;
	}	
	else															//else start the progress bar and submit
	{
		return true;
	}

}
function validateTopic(fld) { 				//function validates form onsubmit
	var isError=new Boolean(false);				//note: the reason there are no function calls is because onsubmit() returns any 
	str=fld.email.value;						//returns to any functions calls therefore making validation useless. 
	var at="@";
	var dot=".";		
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);			
	// Declaring required variables
	
	var i;
    	var theString = "";
	if((fld.name.value=="Name:") || (fld.name.value== null))				//null string validation on name
	{
		$("#name2Error").animate({ opacity: "show" }, "slow");

		isError=true;
	}
   if((fld.subject.value=="Subject:") || (fld.subject.value== null))				//null string validation on name
	{
		$("#subject2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}
	if((fld.message.value=="Question:") || (fld.message.value== null) || (fld.message.value==""))				//null string validation on name
	{
		$("#questionError").animate({ opacity: "show" }, "slow");
		isError=true;
	}
	if((fld.email.value=="Email:") || (fld.email.value== null) )		//validation for email 
	{
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}
	else if (str.indexOf(at)==-1){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}

	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}

	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}

	else if (str.indexOf(at,(lat+1))!=-1){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}

	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}

	else if (str.indexOf(dot,(lat+2))==-1){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}
		
	else if (str.indexOf(" ")!=-1){
		$("#email2Error").animate({ opacity: "show" }, "slow");
		isError=true;
	}
	if(isError==true)												//if error exist return false to onsubmit()
	{
		return false;
	}	
	else															//else start the progress bar and submit
	{
		return true;
	}

}
