function ConfirmCancel (){
  if (confirm("Are you sure you want to clear the form? Click OK to clear it."))
   {
       document.getaquote.reset();
   }
  return false;
}

function ValidateForm(thisform){
    var error;

    with (thisform)
    {
        error = checkText(compname.value);  
        if (error.length > 0)
        {
            error = "Company Name" + error;
            alert(error);
            compname.focus();
            return false;
        }
        error = checkPhone(phone.value);  
        if (error.length > 0)
        {
            alert(error);
            phone.focus();
            return false;
        }
        error = checkText(contname.value);  
        if (error.length > 0)
        {
            error = "Contact Name" + error;
            alert(error);
            contname.focus();
            return false;
        }
        error = checkEmail(email.value);  
        if (error.length > 0)
        {
             alert(error);
             email.focus();
            return false;
        }
        error = checkEmail(retypeemail.value);  
        if (error.length > 0)
        {
             alert(error);
             retypeemail.focus();
            return false;
        }
        if (email.value != retypeemail.value)
        {
             alert("E-mail value does not match Re-typeEmail value");
             retypeemail.focus();
            return false;
        }
        error = checkText(loadorigcity.value);  
        if (error.length > 0)
        {
            error = "Load Origin City" + error;
            alert(error);
            loadorigcity.focus();
            return false;
        }
        error = checkText(loaddestcity.value);  
        if (error.length > 0)
        {
            error = "Load Destination City" + error;
            alert(error);
            loaddestcity.focus();
            return false;
        }
        error = checkState(loadorigstate.value);  
         if (error.length > 0)
        {
            error = "Load Origin State" + error;
            alert(error);
            loadorigstate.focus();
            return false;
        }
        error = checkState(loaddeststate.value);  
        if (error.length > 0)
        {
            error = "Load Destination State" + error;
            alert(error);
            loaddeststate.focus();
            return false;
        }
        error = checkZip(loadorigzipcodea.value, loadorigzipcodeb.value);  
        if (error.length > 0)
        {
            alert(error);
            loadorigzipcodea.focus();
            return false;
        }
        error = checkZip(loaddestzipcodea.value, loaddestzipcodeb.value);  
        if (error.length > 0)
        {
            alert(error);
            loaddestzipcodea.focus();
            return false;
        }
        error = checkText(loadorigpickdate.value);  
        if (error.length > 0)
        {
            error = "Load Origin Deliver Date/Time" + error;
            alert(error);
            loadorigpickdate.focus();
            return false;
        }
        error = checkText(loaddestpickdate.value);  
        if (error.length > 0)
        {
            error = "Load Destination Deliver Date/Time" + error;
            alert(error);
            loaddestpickdate.focus();
            return false;
        }       
       error = checkText(commodship.value);  
        if (error.length > 0)
        {
            error = "Commodity being shipped" + error;
            alert(error);
            commodship.focus();
            return false;
        }       
    } 
    return true;
}

function checkText (strng) {
    var error="";
    if (strng.length == 0) {
        error = " is a required field, please enter one now.\n";
    }
    return error;
}
function checkState(strng) {
    var error="";
    if (strng.search("Choose") == 0) {
        error = " is a required field, please select one now.\n";
    }
    return error;
}
function checkPhone(strng) {
		var error="";
		var valid = "0123456789-()";
		var hyphencount = 0;

		if (strng.length == 0) {
			error = "Please enter your Phone Number";
			return error;
		}
		
		for (var i=0; i < strng.length; i++) {
			temp = "" + strng.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				error = "Invalid characters in your phone number.  Please try again.";
				return error;
			}
		}
 
	return error;
}
function checkZip(strnga, strngb) {
		var error="";
		var zip = strnga;
		var valid = "0123456789-";
		var hyphencount = 0;

		if (strngb.length != 0)
			zip = zip + "-" + strngb;

		if (strnga.length!=5 && zip.length!=10) {
			error = "Please enter your 5 digit or 5 digit+4 zip code.";
			return error;
		}
		
		for (var i=0; i < zip.length; i++) {
			temp = "" + zip.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				error = "Invalid characters in your zip code.  Please try again.";
				return error;
			}
		}
		if ((hyphencount > 1) || ((zip.length==10) && ""+zip.charAt(5)!="-")) {
			error = "The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.";
			return error;
   }
 
	return error;
}
function checkdate (month, day, year) {
    var error="";
 		var valid = "0123456789";
 		var mydate = month+day+year;

    if ((month.length == 0)||(month.length > 2)) {
        error = " month field is invalid, please enter one with format mm.\n";
        document.getaquote.month.focus();
    }
    else if ((month > 12) || (month < 1)) {
        error = " month field is invalid, please enter a number between 1 and 12.\n";
        document.getaquote.month.focus();
    }
   else if ((day.length == 0)||(day.length > 2)) {
        error = " day field is blank, please enter one with format dd.\n";
        document.getaquote.day.focus();
    }
    else if ((day > 31) || (day < 1)) {
        error = " day field is invalid, please enter a number between 1 and 31.\n";
        document.getaquote.day.focus();
    }
    else if ((year.length == 0)||(year.length > 2)) {
        error = " year field is blank, please enter one with format yy.\n";
        document.getaquote.year.focus();
    }
    
 		for (var i=0; i < mydate.length; i++) {
			temp = "" + mydate.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				error = " date has Invalid characters.  Please try again.";
				return error;
			}
		}

    return error;
}

function checkEmail (strng) {
    var error="";
    if (strng == "") {
        error = "You didn't enter an email address.\n";
    }

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
//test email for illegal characters
   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
   if (strng.match(illegalChars)) {
       error = "The email address contains illegal characters.\n";
   }
    return error;    
}
