function ramorderValidate(theForm) {
var reason = "";
// Required Fields
  reason += validateEmail(theForm.email);
	reason += validateEmail2(theForm.email2);
	reason += verifyEmail(theForm.email,theForm.email2);
  reason += validateFirstname(theForm.fname);
  reason += validateLastname(theForm.lname);
	reason += validateCompany(theForm.company);
  reason += validatePhone(theForm.phone);
	reason += validateLocation(theForm.orderloc);
	reason += validateHandling(theForm.orderspec);
	reason += validateShipDate(theForm.shipdate1,theForm.orderspec)
	reason += validateAccount_Opt(theForm.account);
	reason += validatePO_Opt(theForm.ponum);
	reason += validateStreet(theForm.street);
  reason += validateCity(theForm.city);
	reason += validateZip(theForm.zip);
	// ----- Order Lines 1 thru 7
	reason += validateOrderLine1(theForm);  
	reason += validateOrderLine2(theForm);
	reason += validateOrderLine3(theForm);  
	reason += validateOrderLine4(theForm);
	reason += validateOrderLine5(theForm);
	reason += validateOrderLine6(theForm);
	reason += validateOrderLine7(theForm);
	// ------------------------------------
	reason += validateAgree(theForm.agree);
	reason += validateCaptcha(theForm.security_code);
  // Optional Fields
	reason += validateMessage_Opt(theForm.message);
	// reason += validateReason_Opt(theForm.orderobj);
	    
 if (reason != "") {
    alert("Some fields are missing or incorrect:\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateFirstname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a First Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The First Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateLastname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Last Name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Last Name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The Password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The Password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The Password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an Email Address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid Email Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Email Address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateEmail2(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an Email Address Verification.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid Email Address Verification.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Email Address Verification contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function verifyEmail(fld1,fld2) {
    var error="";
    var tfld1 = trim(fld1.value);    // value of field with whitespace trimmed off
		var tfld2 = trim(fld2.value);    // value of field with whitespace trimmed off
    if ((tfld1 != "") && (tfld1 != tfld2)) {
		fld1.style.background = 'Yellow';
		fld2.style.background = 'Yellow';
    error = "Email Addresses not the same.\n";  
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a Phone Number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Phone Number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The Phone Number is the wrong length. Make sure you included an Area Code.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePhone_Opt(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Phone Number contains illegal characters.\n";
        fld.style.background = 'Yellow';
		}
		if (fld.value == "") {
        fld.style.background = 'White';
    } else if (!(stripped.length == 10)) {
        error = "The Phone Number is the wrong length. Make sure you included an Area Code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}
function validateCounty(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't select a Residence County.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateStreet(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Street Address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The Street Address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCity(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a City.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The City contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateZip(fld) {  // 5 Character Numeric
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
		if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Zip Code.\n";
   } else if (IsNumeric(tfld) == false) 
      {
       fld.style.background = 'Yellow';
       error = "You didn't enter a Numeric Zip Code.\n";
	 } else if (fld.value.length != 5) {
        error = "The Zip Code must be 5 Characters. \n";
        fld.style.background = 'Yellow';	 
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCompany(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Company.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Company contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateAgree(fld) {
    var error="";
  
    if (fld.checked == false) {
        fld.style.background = 'Yellow';
        error = "Please Accept Our Internet Order Policy!\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
	 
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
	 
function validateCaptcha(fld) {  // 6 Character
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
		if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a Captcha Code.\n";
    } else if (fld.value.length != 6) {
        error = "The Captcha Code must be 6 Characters. \n";
        fld.style.background = 'Yellow';	 
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateLocation(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't select a Order Location.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateHandling(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't select Order Handling.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
	
function validateShipDate(fld,fld2) {  // Cross-check for Special Handling
var error="";
//------ Validate order Day of Week Requirements
var eTypDay=fld2.value;   // Day implied by orderspec:  Sat=6; Sun=0;
//--Entered date Format MM/DD/YYYY
var enterdate = fld.value;
var convdate = Date.parse(enterdate);
var cDateMS = new Date();
var eDate = new Date(convdate);
var eDay = eDate.getDay();

if (fld.value == "") {
   fld.style.background = 'Yellow';
   error = "You didn't enter a Shipping Date.\n";
 } else if (convdate <= cDateMS) {
  fld.style.background = 'Yellow';
	error = "Date entered must be later than current date \n"			
 } else if (eTypDay=="Sat" && eDay!=6) {
  fld.style.background = 'Yellow';
	error= "Date entered is not a Saturday? \n"
 } else if (eTypDay!="Sat" && eDay==6) {
  fld.style.background = 'Yellow';
	error= "Date entered is Saturday, Special Handling Not Requested?\n"
 } else if (eTypDay=="Sun" && eDay!=0) {
  fld.style.background = 'Yellow';
	error= "Date entered is not a Sunday?"
 } else if (eTypDay!="Sun" && eDay==0) {
	fld.style.background = 'Yellow';
	error =  "Date entered is Sunday, Special Handling Not Requested?\n"
 } else {
   fld.style.background = 'White';
    }
    return error;
}	

function validateAccount_Opt(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;     // restrict tainted characters
    
    if (fld.value == "") {
		    fld.style.background = 'White';
		} else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Account contains illegal characters \\,;: \n";
		} else if (fld.value.length > 20) {
        fld.style.background = 'Yellow'; 
        error = "The Account is greater than 20 characters. \n";
    }
	  return error;
}	
	
function validatePO_Opt(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;     // restrict tainted characters
    
    if (fld.value == "") {
		    fld.style.background = 'White';
		} else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The PO contains illegal characters \\,;: \n";
		} else if (fld.value.length > 30) {
        fld.style.background = 'Yellow'; 
        error = "The PO is greater than 30 characters. \n";
    }
	  return error;
}	


function validateMessage(fld) {
    var error = "";
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;  // restrict tainted characters
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't include a Message.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Message contains illegal characters \\,;: \n";
		} else if (fld.value.length > 240) {
        fld.style.background = 'Yellow'; 
        error = "The Message is greater than 240 characters. \n";
    } else {	
   
        fld.style.background = 'White';
    } 
    return error;
}

function validateMessage_Opt(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;     // restrict tainted characters
    
    if (fld.value == "") {
		    fld.style.background = 'White';
		} else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Message contains illegal characters \\,;: \n";
		} else if (fld.value.length > 240) {
        fld.style.background = 'Yellow'; 
        error = "The Message is greater than 240 characters. \n";
    }
	  return error;
}

function validateOrderLine1(form) {
// ---------------------------------------------------
// ----- Order Line Item Validation  Line-1
	var error="";	
  if((form.ordqty1.value == "") || (form.orditem1.value == "")){
	form.ordqty1.style.background = 'Yellow';
	form.orditem1.style.background = 'Yellow';
	error= "(1) Order Quantity or Item Missing \n";
	}else{
	form.ordqty1.style.background = 'White';
	form.orditem1.style.background = 'White';
	}
	return error;
}
//---- END Order Line Item validation (1)

function validateOrderLine2(form) {
// ---- Order Line Item Validation Line-2
// -------------- confirm paired qty and item; and valid min/max qty for (2) 
  var error="";
  if(((form.ordqty2.value != "")&&(form.orditem2.value == "")) || ((form.ordqty2.value == "")&&(form.orditem2.value != ""))) {
	form.ordqty2.style.background = 'Yellow';
	form.orditem2.style.background = 'Yellow';
	error= "(2) Order Quantity or Item Missing \n";
	}else{
	form.ordqty2.style.background = 'White';
	form.orditem2.style.background = 'White';
	}
	return error;
	}

// --------END Order Line Item Validation Line-2

function validateOrderLine3(form) {
// ---- Order Line Item Validation Line-3
// -------------- confirm paired qty and item; and valid min/max qty for (3) 
  var error="";
  if(((form.ordqty3.value != "")&&(form.orditem3.value == "")) || ((form.ordqty3.value == "")&&(form.orditem3.value != ""))) {
	form.ordqty3.style.background = 'Yellow';
	form.orditem3.style.background = 'Yellow';
	error= "(3) Order Quantity or Item Missing \n";
	}else{
	form.ordqty3.style.background = 'White';
	form.orditem3.style.background = 'White';
	}
	return error;
	}

// --------END Order Line Item Validation Line-3

function validateOrderLine4(form) {
// ---- Order Line Item Validation Line-4
// -------------- confirm paired qty and item; and valid min/max qty for (4) 
  var error="";
  if(((form.ordqty4.value != "")&&(form.orditem4.value == "")) || ((form.ordqty4.value == "")&&(form.orditem4.value != ""))) {
	form.ordqty4.style.background = 'Yellow';
	form.orditem4.style.background = 'Yellow';
	error= "(4) Order Quantity or Item Missing \n";
	}else{
	form.ordqty4.style.background = 'White';
	form.orditem4.style.background = 'White';
	}
	return error;
	}

// --------END Order Line Item Validation Line-4

function validateOrderLine5(form) {
// ---- Order Line Item Validation Line-5
// -------------- confirm paired qty and item; and valid min/max qty for (5) 
  var error="";
  if(((form.ordqty5.value != "")&&(form.orditem5.value == "")) || ((form.ordqty5.value == "")&&(form.orditem5.value != ""))) {
	form.ordqty5.style.background = 'Yellow';
	form.orditem5.style.background = 'Yellow';
	error= "(5) Order Quantity or Item Missing \n";
	}else{
	form.ordqty5.style.background = 'White';
	form.orditem5.style.background = 'White';
	}
	return error;
	}

// --------END Order Line Item Validation Line-5

function validateOrderLine6(form) {
// ---- Order Line Item Validation Line-6
// -------------- confirm paired qty and item; and valid min/max qty for (6) 
  var error="";
  if(((form.ordqty6.value != "")&&(form.orditem6.value == "")) || ((form.ordqty6.value == "")&&(form.orditem6.value != ""))) {
	form.ordqty6.style.background = 'Yellow';
	form.orditem6.style.background = 'Yellow';
	error= "(6) Order Quantity or Item Missing \n";
	}else{
	form.ordqty6.style.background = 'White';
	form.orditem6.style.background = 'White';
	}
	return error;
	}

// --------END Order Line Item Validation Line-6

function validateOrderLine7(form) {
// ---- Order Line Item Validation Line-7
// -------------- confirm paired qty and item; and valid min/max qty for (7) 
  var error="";
  if(((form.ordqty7.value != "")&&(form.orditem7.value == "")) || ((form.ordqty7.value == "")&&(form.orditem7.value != ""))) {
	form.ordqty7.style.background = 'Yellow';
	form.orditem7.style.background = 'Yellow';
	error= "(7) Order Quantity or Item Missing \n";
	}else{
	form.ordqty7.style.background = 'White';
	form.orditem7.style.background = 'White';
	}
	return error;
	}

// --------END Order Line Item Validation Line-7
