// 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);
	 }
// Name
	theForm.name.value = LTrim(theForm.name.value);
	theForm.name.value = RTrim(theForm.name.value);
	if (!emptystring(theForm.name.value))
		 {
			alert("Please enter the Name ");
			theForm.name.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ  .";
			if (!(validate(theForm.name.value,checkOk)))
				{
				alert("Please Enter Valid Name");
				theForm.name.value= "";
				theForm.name.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.phone.value = LTrim(theForm.phone.value);
	theForm.phone.value = RTrim(theForm.phone.value);
	if (!emptystring(theForm.phone.value))
		 {
			alert("Please enter your Phone Number");
			theForm.phone.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.phone.value,checkOk)))
				{
				alert("Please Enter Valid phone number");
				theForm.phone.value= "";
				theForm.phone.focus();
				flag=false;
				return (false);
				}
		}
	
// Address
	theForm.addr.value = LTrim(theForm.addr.value);
	theForm.addr.value = RTrim(theForm.addr.value);
	if (!emptystring(theForm.addr.value))
		 {
			alert("Please enter the Address ");
			theForm.addr.focus();
			flag=false;
			return (false);
		}
	
		
	//comments

	theForm.comments.value = LTrim(theForm.comments.value);
	theForm.comments.value = RTrim(theForm.comments.value);
	if (!emptystring(theForm.comments.value))
		 {
			alert("Please enter your Comments");
			theForm.comments.focus();
			flag=false;
			return (false);
		}
	//Set this return value to true when you want to submit the form
	
	return (flag);
}

//Order Details

function order_form(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);
	 }
	 
// Name
	theForm.name.value = LTrim(theForm.name.value);
	theForm.name.value = RTrim(theForm.name.value);
	if (!emptystring(theForm.name.value))
		 {
			alert("Please enter the Name ");
			theForm.name.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ  .";
			if (!(validate(theForm.name.value,checkOk)))
				{
				alert("Please Enter Valid Name");
				theForm.name.value= "";
				theForm.name.focus();
				flag=false;
				return (false);
				}
		}
	
// Company
	theForm.company.value = LTrim(theForm.company.value);
	theForm.company.value = RTrim(theForm.company.value);
	if (!emptystring(theForm.company.value))
		 {
			alert("Please enter the Company Name ");
			theForm.company.focus();
			flag=false;
			return (false);
		}
		
// Address
	theForm.address.value = LTrim(theForm.address.value);
	theForm.address.value = RTrim(theForm.address.value);
	if (!emptystring(theForm.address.value))
		 {
			alert("Please enter Address ");
			theForm.address.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 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 State");
			theForm.state.focus();
			flag=false;
			return (false);
		}
	
//Country
	theForm.country.value = LTrim(theForm.country.value);
	theForm.country.value = RTrim(theForm.country.value);
	if (!emptystring(theForm.country.value))
		 {
			alert("Please enter Country");
			theForm.country.focus();
			flag=false;
			return (false);
		}
//Zip Code
	theForm.zip.value = LTrim(theForm.zip.value);
	theForm.zip.value = RTrim(theForm.zip.value);
	if (!emptystring(theForm.zip.value))
		 {
			alert("Please enter 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);
				}
		}
		
//Phone Number
	theForm.Telephone.value = LTrim(theForm.Telephone.value);
	theForm.Telephone.value = RTrim(theForm.Telephone.value);
	if (!emptystring(theForm.Telephone.value))
		 {
			alert("Please enter your Phone Number");
			theForm.Telephone.focus();
			flag=false;
			return (false);
		}else {
           var checkOk = "0123456789";
			if (!(validate(theForm.Telephone.value,checkOk)))
				{
				alert("Please Enter Valid phone number");
				theForm.Telephone.value= "";
				theForm.Telephone.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;
   			}
		}
		}
		
		
//Product
	theForm.product.value = LTrim(theForm.product.value);
	theForm.product.value = RTrim(theForm.product.value);
	if (!emptystring(theForm.product.value))
		 {
			alert("Please enter Product Order Information");
			theForm.product.focus();
			flag=false;
			return (false);
		}
		
		
//Browse Validation
	theForm.position.value = LTrim(theForm.position.value);
	theForm.position.value = RTrim(theForm.position.value);
	if(theForm.position.value=="")
	{
	 alert("Please attach Product details");
     flag=false;
	 return false;
    }

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