function validateFormOnSubmit(theForm)
{
	var reason = "";
	theForm.style.background = '';

	reason += validateName(theForm.name);
	reason += validateAddress(theForm.address);
	reason += validateCity(theForm.city);
	reason += validateState(theForm.state);
	reason += validateBirthday(theForm.birthmonth,theForm.birthdate,theForm.birthyear);
	reason += validateZip(theForm.zip);
	reason += validateHomephone(theForm.homephone);
	reason += validateEmail(theForm.email);
	reason += validateRecaptcha(theForm.recaptcha_response_field);
	
	if (reason != "")
	{
/*		alert("The following field(s) need correction(s):\n\n" + reason); */
		return false;
	}
	
	return true;
}

function validateEmpty(fld)
{
	var error = "";
	
	if (fld.value.length == 0)
	{
		fld.style.background = '#f0deef';
		error = "The required field has not been filled in.\n"
	}
	else
	{
		fld.style.background = '#FFFFFF';
	}
	
	return error;
}



function validateBirthday(chkmonth,chkdate,chkyear)
{
	var error = "";
	var illegalChars = /[\(\%\`\~\!\@\#\$\%\^\&\*\)\<\>\,\;\:\\\"\[\]]/ ;
	var today=new Date();
	var currentyear=today.getYear()+1900;
	
	if (chkmonth.value == "no month")
	{
		chkmonth.style.background = '#f0deef';
		chkdate.style.background = '#FFFFFF';
		chkyear.style.background = '#FFFFFF';
		document.getElementById('birthdatespan').innerHTML='&laquo; Please select a month';
		error = "Please select a month.\n"
	}
	else if (chkdate.value == "Day")
	{
		chkdate.style.background = '#f0deef';
		chkmonth.style.background = '#FFFFFF';
		chkyear.style.background = '#FFFFFF';
		document.getElementById('birthdatespan').innerHTML='&laquo; Please enter birth date';
		error = "Please enter birth date.\n"
	}
	else if (chkyear.value == "Year")
	{
		chkyear.style.background = '#f0deef';
		chkmonth.style.background = '#FFFFFF';
		chkdate.style.background = '#FFFFFF';
		document.getElementById('birthdatespan').innerHTML='&laquo; Please enter year of birth';
		error = "Please enter year of birth.\n"
	}


	else if (chkmonth.value==1||chkmonth.value==3||chkmonth.value==5||chkmonth.value==7||chkmonth.value==8||chkmonth.value==10||chkmonth.value==12)
	{
		if(chkdate.value<="0" || chkdate.value>="32")
		{
			chkdate.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkyear.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Birth date';
			error = "Invalid Birth date.\n"
		}


		else if (chkyear.value<="1800" || chkyear.value>currentyear)
		{
			chkyear.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Year of birth';
			error = "Invalid Year of birth.\n"
		}

		else
		{
			chkyear.style.background = '#FFFFFF';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';			
		}
	}


	else if (chkmonth.value==4||chkmonth.value==6||chkmonth.value==9||chkmonth.value==11)
	{
		if(chkdate.value<="0" || chkdate.value>="31")
		{
			chkdate.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkyear.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Birth date';
			error = "Invalid Birth date.\n"
		}


		else if (chkyear.value<="1800" || chkyear.value>currentyear)
		{
			chkyear.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Year of birth';
			error = "Invalid Year of birth.\n"
		}

		else
		{
			chkyear.style.background = '#FFFFFF';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';			
		}
	}

	else if (chkmonth.value==2)
	{
		
	   if(chkyear.value%4==0 || chkyear.value%100==0 || chkyear.value%400==0)
	   {

		if(chkdate.value<="0" || chkdate.value>="30")
		{
			chkdate.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkyear.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Birth date';
			error = "Invalid Birth date.\n"
		}

		else if (chkyear.value<="1800" || chkyear.value>currentyear)
		{
			chkyear.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Year of birth';
			error = "Invalid Year of birth.\n"
		}

		else
		{
			chkyear.style.background = '#FFFFFF';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';			
		}
	  }

	  else
	  {
		if(chkdate.value<="0" || chkdate.value>="29")
		{
			chkdate.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkyear.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Birth date';
			error = "Invalid Birth date.\n"
		}

		else if (chkyear.value<="1800" || chkyear.value>currentyear)
		{
			chkyear.style.background = '#f0deef';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='&laquo; Invalid Year of birth';
			error = "Invalid Year of birth.\n"
		}

		else
		{
			chkyear.style.background = '#FFFFFF';
			chkmonth.style.background = '#FFFFFF';
			chkdate.style.background = '#FFFFFF';
			document.getElementById('birthdatespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';			
		}
          }	
	}

	else
	{
		error = "BIG ERROR.\n"
		chkyear.style.background = '#FF0000';
		chkmonth.style.background = '#FF0000';
		chkdate.style.background = '#FF0000';
		document.getElementById('birthdatespan').innerHTML='BIG ERROR';			
	}
	return error;
}


function validateName(fld)
{
	var error = "";
	var illegalChars = /[\(\%\`\~\!\@\#\$\%\^\&\*\)\<\>\,\;\:\\\"\[\]]/ ;
	
	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		document.getElementById('namespan').innerHTML='&laquo; Please enter your name';
		error = "Please enter your Name.\n"
	}
	else if (illegalChars.test(fld.value))
	{
		fld.style.background = '#f0deef';
		document.getElementById('namespan').innerHTML='&laquo;  The name contains illegal characters';
		error = "The name contains illegal characters.\n"
	}
	else if ((fld.value.length < 3) || (fld.value.length > 30))
	{
		fld.style.background = '#f0deef';
		document.getElementById('namespan').innerHTML='&laquo;  Please enter your full name';
		error = "The name is of wrong length.\n"
	}
	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('namespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}	
	return error;
}




function validateRecaptcha(fld)
{
	var error = "";
	
	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		error = "Please enter the Security text.\n"
		document.getElementById('recaptchaspan').innerHTML='^ Please enter the security text ^';
	}
	else if ((fld.value.length < 1) || (fld.value.length > 50))
	{
		fld.style.background = '#f0deef';
		error = "Please enter the Security text.\n"
		document.getElementById('recaptchaspan').innerHTML='^ Please enter complete security text ^';
	}
	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('recaptchaspan').innerHTML='&nbsp;';
	}	
	return error;
}



function validateAddress(fld)
{
	var error = "";
	
	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		error = "Please enter your Address.\n"
		document.getElementById('addressspan').innerHTML='&laquo;  Please enter your address';
	}
	else if ((fld.value.length < 3) || (fld.value.length > 50))
	{
		fld.style.background = '#f0deef';
		error = "The address is of wrong length.\n"
		document.getElementById('addressspan').innerHTML='&laquo;  The address is of wrong length';
	}

	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('addressspan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}	
	return error;
}


function validateCity(fld)
{
	var error = "";
	var illegalChars = /[\(\%\`\~\!\@\#\$\%\^\&\*\)\<\>\,\;\:\\\"\[\]]/ ;
	
	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		error = "Please enter your City.\n"
		document.getElementById('cityspan').innerHTML='&laquo;  Please enter your city';
	}
	else if (illegalChars.test(fld.value))
	{
		fld.style.background = '#f0deef';
		error = "The city contains illegal characters.\n"
		document.getElementById('cityspan').innerHTML='&laquo;  The city contains illegal characters';
	}
	else if ((fld.value.length < 3) || (fld.value.length > 30))
	{
		fld.style.background = '#f0deef';
		error = "Please avoid abbreviations in City name.\n"
		document.getElementById('cityspan').innerHTML='&laquo;  Please avoid abbreviations';
	}
	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('cityspan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}	
	return error;
}


function validateState(fld)
{
	var error = "";
	
	if (fld.value=="no state")
	{
		fld.style.background = '#f0deef';
		error = "Please select your State.\n"
		document.getElementById('statespan').innerHTML='&laquo;  Please select your state';
	}
	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('statespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}	
	return error;
}


function validateZip(fld)
{
	var error = "";
	var illegalChars = /[\(\%\`\~\!\@\#\$\%\^\&\*\)\<\>\,\;\:\\\"\[\]]/ ;

	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		error = "Please enter your Zip.\n"
		document.getElementById('zipspan').innerHTML='&laquo;  Please enter your zip';
	}
	else if (illegalChars.test(fld.value))
	{
		fld.style.background = '#f0deef';
		error = "The zip contains illegal characters.\n"
		document.getElementById('zipspan').innerHTML='&laquo;  The zip contains illegal characters';
	}
	else if ((fld.value.length < 5) || (fld.value.length > 5))
	{
		fld.style.background = '#f0deef';
		error = "Please enter the 5 digit Zip.\n"
		document.getElementById('zipspan').innerHTML='&laquo;  Please enter the 5 digit zip';
	}
	else if ((fld.value >=  9999) && (fld.value <= 99999))
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('zipspan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}
	else
	{
		fld.style.background = '#f0deef';
		error = "Please enter valid Zip.\n"
	}	
	return error;
}


function validateHomephone(fld)
{
	var error = "";
	var illegalChars = /[\(\%\`\~\!\@\#\$\%\^\&\*\)\<\>\,\;\:\\\"\[\]]/ ;

	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		error = "Please enter your Home Phone number.\n"
		document.getElementById('homephonespan').innerHTML='&laquo;  Please enter your Home Phone number';
	}
	else if (illegalChars.test(fld.value))
	{
		fld.style.background = '#f0deef';
		error = "Home Phone number contains illegal characters.\n"
		document.getElementById('homephonespan').innerHTML='&laquo;  Home Phone number contains illegal characters';
	}
	else if ((fld.value.length < 10) || (fld.value.length > 10))
	{
		fld.style.background = '#f0deef';
		error = "Please enter the 10 digit Home Phone number.\n"
		document.getElementById('homephonespan').innerHTML='&laquo;  Please enter the 10 digit Home Phone number';
	}
	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('homephonespan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}	
	return error;
}



function trim(s)
{
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld)
{
	var error = "";
	var tfld = trim(fld.value);
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars = /[\(\%\`\~\!\#\$\%\^\&\*\)\<\>\,\;\:\\\"\[\]]/ ;
	
	if (fld.value.length == "")
	{
		fld.style.background = '#f0deef';
		error = "Please enter your Email address.\n"
		document.getElementById('emailspan').innerHTML='&laquo;  Please enter your email address';
	}
	else if (fld.value.match(illegalChars))
	{
		fld.style.background = '#f0deef';
		error = "The email address contains illegal characters.\n"
		document.getElementById('emailspan').innerHTML='&laquo;  Email contains illegal characters';
	}
	else if (!emailFilter.test(tfld))
	{
		fld.style.background = '#f0deef';
		error = "Please enter a valid Email address.\n"
		document.getElementById('emailspan').innerHTML='&laquo;  Please enter valid email address';
	}
	else
	{
		fld.style.background = '#FFFFFF';
		document.getElementById('emailspan').innerHTML='<img src=images/correct.gif border=0 alt=correct />';
	}	
	return error;
}
