// JavaScript Document


// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
   if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
   }
   
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
   if (string.search) {
      if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
   }
   return true;
}

// Remove all spaces from a string
function removeSpaces(string) {
   var newString = '';
   for (var i = 0; i < string.length; i++) {
      if (string.charAt(i) != ' ') newString += string.charAt(i);
   }
   return newString;
}

// Check that a US zip code is valid
function isValidZipcode(zipcode) {
   zipcode = removeSpaces(zipcode);
   if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
   if ((zipcode.length == 5 || zipcode.length == 9) && !isNumeric(zipcode)) return false;
   if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
   return true;
}

// Check that a US or Canadian phone number is valid
function isValidUSPhoneNumber(areaCode, prefixNumber, suffixNumber) {
   if (arguments.length == 1) {
      var phoneNumber = arguments[0];
      phoneNumber = phoneNumber.replace(/\D+/g, '');
      var length = phoneNumber.length;
      if (phoneNumber.length >= 7) {
         var areaCode = phoneNumber.substring(0, length-7);
         var prefixNumber = phoneNumber.substring(length-7, length-4);
         var suffixNumber = phoneNumber.substring(length-4);
      }
      else return false;
   }
   else if (arguments.length == 3) {
      var areaCode = arguments[0];
      var prefixNumber = arguments[1];
      var suffixNumber = arguments[2];
   }
   else return true;

   if (areaCode.length != 3 || !isNumeric(areaCode) || prefixNumber.length != 3 || !isNumeric(prefixNumber) || suffixNumber.length != 4 || !isNumeric(suffixNumber)) return false;
   return true;
}

// Returns a checksum digit for a number using mod 10
function getMod10(number) {
   
   // convert number to a string and check that it contains only digits
   // return -1 for illegal input
   number = '' + number;
   number = removeSpaces(number);
   if (!isNumeric(number)) return -1;
   
   // calculate checksum using mod10
   var checksum = 0;
   for (var i = number.length - 1; i >= 0; i--) {
      var isOdd = ((number.length - i) % 2 != 0) ? true : false;
      digit = number.charAt(i);
      
      if (isOdd) checksum += parseInt(digit);
      else {
         var evenDigit = parseInt(digit) * 2;
         if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);
         else checksum += evenDigit;
      }
   }
   return (checksum % 10);
}


// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)
function isValidCreditCard(number) {
   number = '' + number;
   
   if (number.length > 16 || number.length < 13 ) return false;
   else if (getMod10(number) != 0) return false;
   else if (arguments[1]) {
      var type = arguments[1];
      var first2digits = number.substring(0, 2);
      var first4digits = number.substring(0, 4);
      
      if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&
         (number.length == 16 || number.length == 13 )) return true;
      else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&
         (first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;
      else if (type.toLowerCase() == 'american express' && number.length == 15 && 
         (first2digits == '34' || first2digits == '37')) return true;
      else if (type.toLowerCase() == 'diners club' && number.length == 14 && 
         (first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;
      else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;
      else if (type.toLowerCase() == 'enroute' && number.length == 15 && 
         (first4digits == '2014' || first4digits == '2149')) return true;
      else if (type.toLowerCase() == 'jcb' && number.length == 16 &&
         (first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;
      
    // if the above card types are all the ones that the site accepts, change the line below to 'else return false'
    else return true;
   }
   else return true;
}



function ContactValid()
	{
	var Valid = true;
	var Errors = "Please submit the following information:\n";
	if (document.forms.Form.name.value == ""){
		Valid = false;
		Errors += "\nYour Full Name\n";
	}
	if (document.forms.Form.phone.value == ""){
		Valid = false;
		Errors += "\nA Phone number\n";
	}else{
		if (!isValidUSPhoneNumber(document.forms.Form.phone.value)){
			Valid = false;
			Errors += "\nA valid US Phone number\n";
		}
	}
	
	if (document.forms.Form.email.value == ""){
		Valid = false;
		Errors += "\nAn E-Mail Address\n";
	}else{
		if (!isValidEmail(document.forms.Form.email.value)){
			Valid = false;
			Errors += "\nA Valid E-Mail Address\n";
		}
	}
		
	if (document.forms.Form.company.value == ""){
		Valid = false;
		Errors += "\nYour Company Name\n";
	}
	
	if (document.forms.Form.addr.value == ""){
		Valid = false;
		Errors += "\nYour Address\n";
	}
	
	if (document.forms.Form.city.value == ""){
		Valid = false;
		Errors += "\nYour City \n";
	}
	
	if (document.forms.Form.state.value == ""){
		Valid = false;
		Errors += "\nYour State\n";
	}
	
	if (document.forms.Form.zip.value == ""){
		Valid = false;
		Errors += "\nYour Zip Code\n";
	}
	
	if (Valid){
		return true;
	}else{
		alert(Errors);
		return false;
	}
	
	
}


	function RateValid(){
	var Valid = true;
	var Errors = "Please submit the following information:\n";
	if (document.forms.Form.name.value == ""){
		Valid = false;
		Errors += "\nYour Full Name\n";
	}
	if (document.forms.Form.phone.value == ""){
		Valid = false;
		Errors += "\nA Phone number\n";
	}else{
		if (!isValidUSPhoneNumber(document.forms.Form.phone.value)){
			Valid = false;
			Errors += "\nA valid US Phone number\n";
		}
	}
	if (document.forms.Form.email.value == ""){
		Valid = false;
		Errors += "\nAn E-Mail Address\n";
	}else{
		if (!isValidEmail(document.forms.Form.email.value)){
			Valid = false;
			Errors += "\nA Valid E-Mail Address\n";
		}
	}
		
	if (document.forms.Form.company.value == ""){
		Valid = false;
		Errors += "\nYour Company Name\n";
	}
	
	if (document.forms.Form.inputDate.value == ""){
		Valid = false;
		Errors += "\nYour Estimated Delivery Date\n";
	}
	
	if (document.forms.Form.origin.value == ""){
		Valid = false;
		Errors += "\nYour Origination \n";
	}
	
	if (document.forms.Form.destin.value == ""){
		Valid = false;
		Errors += "\nYour Destination\n";
	}
	
	if (document.forms.Form.vehicletype.value == "-- Select Type --"){
		Valid = false;
		Errors += "\nYour Vehicle Type\n";
	}
	
	if (document.forms.Form.nop.value == ""){
		Valid = false;
		Errors += "\nYour Number of Pallets\n";
	}
	
	if (document.forms.Form.lbs.value == ""){
		Valid = false;
		Errors += "\nYour Weight\n";
	}
	
	if (Valid){
		return true;
	}else{
		alert(Errors);
		return false;
	}
	
	}
	
	
	function QuoteValid(){
	var Valid = true;
	var Errors = "Please submit the following information:\n";
	
	if (document.forms.Form.ozip.value == ""){
		Valid = false;
		Errors += "\nYour Origin ZIP\n";
	}
	
	if (document.forms.Form.dzip.value == ""){
		Valid = false;
		Errors += "\nYour Destination ZIP\n";
	}
	
	if (document.forms.Form.nop.value == ""){
		Valid = false;
		Errors += "\nYour # of Pallets \n";
	}
	
	if (document.forms.Form.lbs.value == ""){
		Valid = false;
		Errors += "\nYour Weight\n";
	}
	
	if (document.forms.Form.name.value == ""){
		Valid = false;
		Errors += "\nYour Full Name\n";
	}
	if (document.forms.Form.email.value == ""){
		Valid = false;
		Errors += "\nAn E-Mail Address\n";
	}else{
		if (!isValidEmail(document.forms.Form.email.value)){
			Valid = false;
			Errors += "\nA Valid E-Mail Address\n";
		}
	}
	
	if (Valid){
		return true;
	}else{
		alert(Errors);
		return false;
	}
	}