// JavaScript Document
function form_validator(theForm)
{
	//Validate Function 
	function validate(value,checkOk)
	{
	var checkStr = value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOk.length;  j++)
		if (ch == checkOk.charAt(j))
			break;
			if (j == checkOk.length) {
			 allValid = false;
			 break;
			}
	}
	  if (!allValid) {
		return (false);
	  }
	else { return (true); }
  }

		//Trim script
		function Trim(STRING){
		STRING = LTrim(STRING);
		return RTrim(STRING);
		}

		function RTrim(STRING){
		while(STRING.charAt((STRING.length -1))==" "){
		STRING = STRING.substring(0,STRING.length-1);
		}
		return STRING;
		}

		function LTrim(STRING){
		while(STRING.charAt(0)==" "){
		STRING = STRING.replace(STRING.charAt(0),"");
		}
		return STRING;
		}
	 // emptycheck
	 var flag=true;
	 function emptystring(value)
	 {
	   if (value=="") return (false);
	   else return(true);
	 }
// Full Name
	theForm.fullname.value = LTrim(theForm.fullname.value);
	theForm.fullname.value = RTrim(theForm.fullname.value);
	if (!emptystring(theForm.fullname.value))
		 {
			alert("Please enter the Full Name ");
			theForm.fullname.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ  .";
			if (!(validate(theForm.fullname.value,checkOk)))
				{
				alert("Please Enter Valid Full Name");
				theForm.fullname.value= "";
				theForm.fullname.focus();
				flag=false;
				return (false);
				}
		}
		//email	
	theForm.email.value = LTrim(theForm.email.value);
	theForm.email.value = RTrim(theForm.email.value);
	if (!emptystring(theForm.email.value))
		 {
			alert("Please enter your Email");
			theForm.email.focus();
			flag=false;
			return (false);
		}else {
	var flag  = true
	var Temp     = theForm.email
	var AtSym    = Temp.value.indexOf('@')
	var Period   = Temp.value.lastIndexOf('.')
	var Space    = Temp.value.indexOf(' ')
	var Length   = Temp.value.length - 1   // Array is from 0 to length-1

	if(theForm.email.value !=""){
		if ((AtSym < 1) ||                     // '@' cannot be in first position
		    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
		    (Period == Length ) ||             // Must be atleast one valid char after '.'
		    (Space  != -1))                    // No empty spaces permitted
			   {  
		      flag = false
		      alert("Please enter a valid e-mail address!");
			      Temp.value="";
			      Temp.focus();
		      return flag;
   			}
		}
		}
	
	//Phone Number

	theForm.dtt.value = LTrim(theForm.dtt.value);
	theForm.dtt.value = RTrim(theForm.dtt.value);
	if (!emptystring(theForm.dtt.value))
		 {
			alert("Please enter your Phone Number");
			theForm.dtt.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.dtt.value,checkOk)))
				{
				alert("Please Enter Valid phone number");
				theForm.dtt.value= "";
				theForm.dtt.focus();
				flag=false;
				return (false);
				}
		}

	// Evening Phone Number

	theForm.et.value = LTrim(theForm.et.value);
	theForm.et.value = RTrim(theForm.et.value);
	if (!emptystring(theForm.et.value))
		 {
			alert("Please enter your Phone Number");
			theForm.et.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.et.value,checkOk)))
				{
				alert("Please Enter Valid phone number");
				theForm.et.value= "";
				theForm.et.focus();
				flag=false;
				return (false);
				}
		}
		
		// City
	theForm.city.value = LTrim(theForm.city.value);
	theForm.city.value = RTrim(theForm.city.value);
	if (!emptystring(theForm.city.value))
		 {
			alert("Please enter the City ");
			theForm.city.focus();
			flag=false;
			return (false);
		}

		//State
	theForm.state.value = LTrim(theForm.state.value);
	theForm.state.value = RTrim(theForm.state.value);
	if (!emptystring(theForm.state.value))
		 {
			alert("Please enter the State");
			theForm.state.focus();
			flag=false;
			return (false);
		}

//ZIP

	theForm.zip.value = LTrim(theForm.zip.value);
	theForm.zip.value = RTrim(theForm.zip.value);
	if (!emptystring(theForm.zip.value))
		 {
			alert("Please enter your Zip Code");
			theForm.zip.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.zip.value,checkOk)))
				{
				alert("Please Enter Valid Zip Code");
				theForm.zip.value= "";
				theForm.zip.focus();
				flag=false;
				return (false);
				}
		}
				// City
	theForm.tcity.value = LTrim(theForm.tcity.value);
	theForm.tcity.value = RTrim(theForm.tcity.value);
	if (!emptystring(theForm.tcity.value))
		 {
			alert("Please enter the City ");
			theForm.tcity.focus();
			flag=false;
			return (false);
		}

		//State
	theForm.tstate.value = LTrim(theForm.tstate.value);
	theForm.tstate.value = RTrim(theForm.tstate.value);
	if (!emptystring(theForm.tstate.value))
		 {
			alert("Please enter the State");
			theForm.tstate.focus();
			flag=false;
			return (false);
		}

//ZIP

	theForm.tzip.value = LTrim(theForm.tzip.value);
	theForm.tzip.value = RTrim(theForm.tzip.value);
	if (!emptystring(theForm.tzip.value))
		 {
			alert("Please enter your Zip Code");
			theForm.tzip.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.tzip.value,checkOk)))
				{
				alert("Please Enter Valid Zip Code");
				theForm.tzip.value= "";
				theForm.tzip.focus();
				flag=false;
				return (false);
				}
		}
		//How
	theForm.how.value = LTrim(theForm.how.value);
	theForm.how.value = RTrim(theForm.how.value);
	if (!emptystring(theForm.how.value))
		 {
			alert("Please enter How Did You Hear Of Us");
			theForm.how.focus();
			flag=false;
			return (false);
		}

	//month

	theForm.month.value = LTrim(theForm.month.value);
	theForm.month.value = RTrim(theForm.month.value);
	if (!emptystring(theForm.month.value))
		 {
			alert("Please enter Month");
			theForm.month.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.month.value,checkOk)))
				{
				alert("Please Enter Valid Month");
				theForm.month.value= "";
				theForm.month.focus();
				flag=false;
				return (false);
				}
		}
//day

	theForm.day.value = LTrim(theForm.day.value);
	theForm.day.value = RTrim(theForm.day.value);
	if (!emptystring(theForm.day.value))
		 {
			alert("Please enter day");
			theForm.day.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.day.value,checkOk)))
				{
				alert("Please Enter Valid day");
				theForm.day.value= "";
				theForm.day.focus();
				flag=false;
				return (false);
				}
		}
//year

	theForm.year.value = LTrim(theForm.year.value);
	theForm.year.value = RTrim(theForm.year.value);
	if (!emptystring(theForm.year.value))
		 {
			alert("Please enter year");
			theForm.year.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.year.value,checkOk)))
				{
				alert("Please Enter Valid year");
				theForm.year.value= "";
				theForm.year.focus();
				flag=false;
				return (false);
				}
		}

	
	//Set this return value to true when you want to submit the form
	
	return (flag);
}