
function validName(theName) {
	if (theName.length < 3) {
		return false;
	}
	return true;
}
	
function validAddr(theAddr) {
	if (theAddr.length < 3) {
		return false;
	}
	return true;
} 
function validEmail(email) {
		invalidChars = " /:,;"
		if (email == "") {
			return false;
		}
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i);
			if (email.indexOf(badChar,0) != -1) {
				return false;
			}
		}
		atPos = email.indexOf("@",1);
		if (atPos == -1) {
			return false;
		}
		if (email.indexOf("@",atPos+1) != -1) {
			return false;
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {
			return false;
		}
		if (periodPos+3 > email.length)	{
			return false;
		}
		return true;
} 
function validZip(theZip) {
	if (theZip == "") {
      	return false;	
    }
	if (theZip.length != 5 && theZip.length != 10) {
        return false;
    }
	for (i=0; i<=4; i++) {
        ch = theZip.charAt(i);
        if (ch != "0" && ch != "1" && ch != "2" &&
            ch != "3" && ch != "4" && ch != "5" && ch != "6" &&
            ch != "7" && ch != "8" && ch != "9") {
 				return false;
		}
	}
	for ((i=theZip.length-1);i>5;i--) {
	    ch = theZip.charAt(i);
        if (ch != "0" && ch != "1" && ch != "2" && ch != "3" &&
            ch != "4" && ch != "5" && ch != "6" && ch != "7" &&
            ch != "8" && ch != "9") {
			return false;
		}
   }
   if ((theZip.indexOf("-") != 5) && theZip.length == 10) {
 		return false;
   }
   return true;
}  	
function validPhone(ph) {
		if (ph == "") {
			return false;	
	    }
		var Nbrs = 0;
		for (i=0; i<ph.length; i++) {
			ch = ph.charAt(i);
			 if (ch != "0" && ch != "1" && ch != "2" && ch != "3" &&
			     ch != "4" && ch != "5" && ch != "6" && ch != "7" &&
	             ch != "8" && ch != "9" && ch != ")" && ch != "(" && ch != "-" && ch != " ") {
				return false;
			}
			else {
				if (ch != "(" && ch != ")" && ch != "-" && ch !=" ") {
					Nbrs++;
				}
			}
		}
		if (Nbrs < 7) {
			return false;
		}			
		return true;
} 
function validNumber(nbr) {		
	if (nbr <= "0") {
		return false;
	}
	for (i=0; i<nbr.length; i++) {
			ch = nbr.charAt(i);
			 if (ch != "0" && ch != "1" && ch != "2" && ch != "3" &&
			     ch != "4" && ch != "5" && ch != "6" && ch != "7" &&
	             ch != "8" && ch != "9") {
				 return false;
			}		
	 }
	 return true;
} 
function validMM(mm) {
		mm=parseInt(mm);
		if (mm < 00 || mm > 13) {
 			  return false;
  		 }
		 return true;
} 
function validDD(mm, dd, yy) {	

		//mm=parseInt(mm);
		//dd=parseInt(dd);		
		//yy=parseInt(yy);
		if (dd < 00 || dd > 31)  {
			return false;
		}

		if (dd > 30 &&
			mm != 01 &&
			mm !=03 &&  
			mm != 05 &&  
			mm != 07 &&  
			mm != 08 &&  
			mm != 10 &&  
			mm != 12) {
				 return false;			
		}

		if (mm == 02 && dd > 29) {
			return false;			
		}
		
		// leap year:
		if (mm == 02 && dd == 29  && yy !=04 &&	yy !=08 && yy !=12) {
		   return false;
		}			
		
		return true;
} 
function validYY(yy) {
	yy=parseInt(yy);	
		if (yy < 01 || yy > 20 ) {
		   return false;
	   }			
	return true;
} 
function validStartEndDates(sMM, sDD, sYY, eMM, eDD, eYY) {
	sYYYY="20" + sYY;
	sDate=sYYYY + sMM + sDD;
	
	eYYYY="20" + eYY;
	eDate=eYYYY + eMM + eDD;
		
	 
	if (eDate < sDate) {
	   return false;
    }
	return true;
} 
function submitIt(theForm) {
		var currYMD = theForm.currYMD.value;
	 	if (theForm.LoadFrom.value.length < 2) {
			alert("Please enter where the load is starting from");
			theForm.LoadFrom.focus();
			theForm.LoadFrom.select();
			return false;
		} 
		if (!(validPhone(theForm.Phone.value))) {
			alert("Please enter your phone number");
			theForm.Phone.focus();
			theForm.Phone.select();
			return false;
		} 
		if (!(validName(theForm.CompanyName.value))) {
			alert("Please enter the name of the company");
			theForm.CompanyName.focus();
			theForm.CompanyName.select();
			return false;
		} 
		if (!(validName(theForm.YourName.value))) {
			alert("Please enter your name");
			theForm.YourName.focus();
			theForm.YourName.select();
			return false;
		} 
		if (theForm.LoadTo.value.length < 2) {
			alert("Please enter where the load is going to");
			theForm.LoadTo.focus();
			theForm.LoadTo.select();
			return false;
		} 
		if (!(validName(theForm.OwnerName.value))) {
			alert("Please enter the name of the company owner");
			theForm.OwnerName.focus();
			theForm.OwnerName.select();
			return false;
		} 
		if (!(validPhone(theForm.Fax.value))) {
			alert("Please enter your fax number");
			theForm.Fax.focus();
			theForm.Fax.select();
			return false;
		} 
		if (!(validName(theForm.DriverName.value))) {
			alert("Please enter the name of the driver");
			theForm.DriverName.focus();
			theForm.DriverName.select();
			return false;
		} 
		if (!(validPhone(theForm.DriverPhone.value))) {
			alert("Please enter the cell phone number of the driver");
			theForm.DriverPhone.focus();
			theForm.DriverPhone.select();
			return false;
		} 
		theSMM=theForm.PickUpDateMM.value;
		theSDD=theForm.PickUpDateDD.value;
		theSYY=theForm.PickUpDateYY.value;
		
		theDMM=theForm.DropOffDateMM.value;
		theDDD=theForm.DropOffDateDD.value;
		theDYY=theForm.DropOffDateYY.value;
		
		if (theSMM.length < 2)	theSMM= "0" + theSMM;
		if (theSDD.length < 2)	theSDD="0"  + theSDD;
		if (theSYY.length < 2)	theSYY= "0"  + theSYY;
		
		if (theDMM.length < 2)	theDMM= "0" + theDMM;
		if (theDDD.length < 2)	theDDD="0"  + theDDD;
		if (theDYY.length < 2)	theDYY= "0"  + theDYY;
			
		if (!validNumber(theSMM) || !validNumber(theSDD) || !validNumber(theSYY) ) {
			alert("Please enter a valid date.\rLike 12 20 10");
					theForm.PickUpDateMM.focus();
					theForm.PickUpDateMM.select();
					return false;
		}			
		if (!validMM(theSMM)) {
			alert("Please enter a Month between 01 and 12.");
					theForm.PickUpDateMM.focus();
					theForm.PickUpDateMM.select();
					return false;
		}		
		if (!validDD(theSMM, theSDD, theSYY)) {
			alert("Please enter a valid day in the month.");
					theForm.PickUpDateDD.focus();
					theForm.PickUpDateDD.select();
					return false;
		}
					
		 
		yyyymmdd='20' + theSYY + theSMM + theSDD;
		if (yyyymmdd <  currYMD) {
			alert("Please enter a future date for the pickup date");
				theForm.PickUpDateMM.focus();
				theForm.PickUpDateMM.select();
				return false;
		}
		 
		
		if (!validNumber(theDMM) || !validNumber(theDDD) || !validNumber(theDYY) ) {
			alert("Please enter a valid date.\rLike 01 20 04");
					theForm.DropOffDateMM.focus();
					theForm.DropOffDateMM.select();
					return false;
		}
			
		if (!validMM(theDMM)) {
			alert("Please enter a Month between 01 and 12.");
					theForm.DropOffDateMM.focus();
					theForm.DropOffDateMM.select();
					return false;
		}
		
		
		if (!validDD(theDMM, theDDD, theDYY)) {
			alert("Please enter a valid day in this month.");
					theForm.DropOffDateDD.focus();
					theForm.DropOffDateDD.select();
					return false;
		}
							 
		yyyymmdd='20' + theDYY + theDMM + theDDD;
		if (yyyymmdd <  currYMD) {
			alert("Please enter a future date");
				theForm.DropOffDateMM.focus();
				theForm.DropOffDateMM.select();
				return false;
		}
		 
		
		if (!validStartEndDates(theSMM, theSDD, theSYY, theDMM, theDDD, theDYY)) {
		alert("The Pickup Date must come before the Drop off date");
				theForm.PickUpDateMM.focus();
				theForm.PickUpDateMM.select();
				return false;
	}
		 
		if (!(theForm.terms.checked )) {
			alert("You must read and accept the Terms ");
			theForm.terms.focus();
			theForm.terms.select();
			return false;
		}
		
		if (!(validName(theForm.Signature.value))) {
			alert("Please enter your signature");
			theForm.Signature.focus();
			theForm.Signature.select();
			return false;
		}
		
		if (!(validName(theForm.Email.value))) {
			alert("Please enter your email address where\r we can reply to you");
			theForm.Email.focus();
			theForm.Email.select();
			return false;
		}
		
		
		return true;
}
