//@BEGINVERSIONINFO

//@APPVERSION: 50.4014.0.2

//@FILENAME: sfcheckerrors.js 
//MOD WMS 5-30-03  to allow add cart to be clicked without entering "1"
//MOD WMS 6-24-03  changed shipping fields test for Netscape 4
//MOD WMS 072403-8 shipping field does not require email (or email or addressline 2) -- changed message 
//MOD WMS 080103-2 Remove phone requirement in billing
//MOD WMS 080703-1 Put back phone requirement in billing -- remove phone requirement in shipping
//MOD WMS 080703-2 Changed error message to specify billing info
//MOD WMS 091403-6 Removed Middle initial check in shipping  and removed shipping field from list of fields that trigger a "fill in all fields" error 
//MOD WMS 010304-1 Code to check min/max qty
//MOD WMS 010404-1 Removed dev code
//MOD WMS 011904-3 Added better email validation
//MOD WMS 021504-1 Checks that radio buttonis selected
//MOD WMS 040304-1 Radio button test for 2 or more radio buttons
//MOD WMS 080604-1 Allowed all forms of 2 char ending
//MOD WMS 081804-1 Only test for radio objects
//MOD WMS 121504-1 Simple email check 

//@DESCRIPTION: Checkes sfErrors

//@STARTCOPYRIGHT
//The contents of this file is protected under the United States
//copyright laws as an unpublished work, and is confidential and proprietary to
//LaGarde, Incorporated.  Its use or disclosure in whole or in part without the
//expressed written permission of LaGarde, Incorporated is expressly prohibited.

//(c) Copyright 2000 by LaGarde, Incorporated.  All rights reserved.
//@ENDCOPYRIGHT

//@ENDVERSIONINFO

function specialCase(e, form) {
	if ((e.name == "CardName")||(e.name == "CardNumber")||(e.name == "CardExpiryMonth")||(e.name == "CardExpiryYear")) {
		if (((form.CardName.value.length <= 0)||(form.CardNumber.value.length <= 0)||(form.CardExpiryMonth.value.length <= 0)||(form.CardExpiryYear.value.length <= 0))
		 && ((form.CardName.value.length > 0)||(form.CardNumber.value.length > 0)||(form.CardExpiryMonth.value.length > 0)||(form.CardExpiryYear.value.length > 0))) {
			return "Please enter all Credit Card Information.";
		}
		if ((form.CardName.value.length > 0)&&(form.CardNumber.value.length > 0)&&(form.CardExpiryMonth.value.length > 0)&&(form.CardExpiryYear.value.length > 0)) {
			if (!isCardDateValid(form.CardExpiryYear.value, form.CardExpiryMonth.value)) {
				return "The Credit Card has Expired.";
			}
			if (isCardNumValid(form.CardNumber.value)) {
				return "The Credit Card Number is an invalid format.";
			}
		}
	}
	if ((e.name == "CheckNumber")||(e.name == "BankName")||(e.name == "RoutingNumber")||(e.name == "CheckingAccountNumber")) {
		if (((form.CheckNumber.value.length <= 0)||(form.BankName.value.length <= 0)||(form.RoutingNumber.value.length <= 0)||(form.CheckingAccountNumber.value.length <= 0))
		 && ((form.CheckNumber.value.length > 0)||(form.BankName.value.length > 0)||(form.RoutingNumber.value.length > 0)||(form.CheckingAccountNumber.value.length > 0))) {
			return "Please enter all eCheck Information.";
		}		
	}
	if ((e.name == "POName")||(e.name == "PONumber")) {
		
		if (((form.POName.value.length <= 0)||(form.PONumber.value.length <= 0))
		 && ((form.POName.value.length > 0)||(form.PONumber.value.length > 0))) {
			return "Please enter all Purchase Order Information.";
		}
	}
	if ((form.CardName.value.length <= 0)&&(form.CardNumber.value.length <= 0)&&(form.CardExpiryMonth.value.length <= 0)&&(form.CardExpiryYear.value.length <= 0)
	 && (form.CheckNumber.value.length <= 0)&&(form.BankName.value.length <= 0)&&(form.RoutingNumber.value.length <= 0)&&(form.CheckingAccountNumber.value.length <= 0)
	 && (form.POName.value.length <= 0)&&(form.PONumber.value.length <= 0)) {
		return "Please enter payment method Information.";	
	}
	return "";
}
function stripChar(sValue, sChar) {
	var i, tempChar, buildString;
	buildString = ""
	for (var i=0; i<sValue.length; i++) {
		tempChar = sValue.charAt(i);
		if (tempChar != sChar) {
			buildString = buildString + tempChar;
		}
	}
	return buildString;
}

function isCardDateValid(year, month) {
	var dateCheck, now;
	if (year.length == 2) {
		if (parseInt(year) < 50) {
			year = "20" + year;
		}
	}
	now = new Date();
	dateCheck = new Date(year, month);
	if (now > dateCheck) {
		return false;
	}
	else {
		return true;
	}
}

function isCardNumValid(num) {
	var num1, num2, tempNum;
	if (!isNumber(num)) {
		return true;
	}
	num1 = ""
	if (!(num.length%2==0)) {
		for(var j=0; j < num.length; j++) {
			if ((j+1)%2==0){
				tempNum = 2 * num.charAt(j);
			}
			else {
				tempNum = 1 * num.charAt(j);
			}
			num1 = num1 + tempNum.toString();
		}
	}
	else{
		for(var j=0; j < num.length; j++){
			if ((j+1)%2==0){
				tempNum = 1 * num.charAt(j);
			}
			else{
				tempNum = 2 * num.charAt(j);
			}
			num1 = num1 + tempNum.toString();
		}
	}
	num2 = 0;
	for (var j = 0; j < num1.length; j++) {
		num2 = num2 + parseInt(num1.charAt(j));
	}
	if (num2%10==0) {
		return false;
	}
	else {
		return true;
	}
}

function isNumber(value) {
	for (var i=0; i < value.length; i++) {
		a = parseInt(value.charAt(i));
		if (isNaN(a)) {
			return false;			
			break;
		}
	}
	return true;
}

function sfCheck(form) {
	var e, title, empty_fields, char_check, invalid_card, month, year, invalid_date, eMail, invalid_eMail 
	var iQuantity, quantity_check, checkSpecial, tempError, special_Error, msg, upperLine, lowerLine
	var num, invalid_phoneNumber, passwd_mismatch
	msg = "";
	empty_fields = "";
	char_check = "";
	special_Error = "";
	tempError = "";
	num = form.length
	for (var i = 0; i < form.length; i++) {
		e = form.elements[i]
		
		/////////////////////////////////////////////
		// WMS 010304-1
		// Code to check min max qty 
		// product code must have QRxxx-yyy 
		// where xxx is min 1 and yyy is max or "M"
		/////////////////////////////////////////////
		
		// Only do test for radio buttons and when PRODUCT_ID is on the form and using radio buttons or checkbox  (not drop down box)		
		if (e.name=='PRODUCT_ID') {				
			if ((form.PRODUCT_ID.length >= 2) && (form.PRODUCT_ID[0].type=='radio')) {  						
				myOption = -1;
				for (i=0; i<form.PRODUCT_ID.length; i++){										
					if (form.PRODUCT_ID[i].checked){						
						myOption = i;
					}
				}
				
			    if (myOption == -1) {
					alert("Please select one of the products.");
			    return false;
				}				
		
				var code = form.PRODUCT_ID[myOption].value;
				var loc = code.lastIndexOf("QR");
				if (loc > 3){
					var loc_dash = code.lastIndexOf("-");
					var min_start = loc+2;
					var min = parseInt(code.substring(min_start,loc_dash));
					var max = parseInt(code.substring(loc_dash+1));
					if (form.QUANTITY.value == 0) {
						alert('Please fill in the quantity field for this product.')
						return false;				
					}
					if (form.QUANTITY.value < min) {
						alert('Please re-enter the quantity. The minimum order for this price is ' + min)
						return false;
					}	
					if (max != 'M') {
						if (form.QUANTITY.value > max) {
							alert('Please re-enter the quantity. The maximum order for this price is ' + max)
							return false;
						}						
					}	
				}	 
			}
		}	
		/////////////////////////////////////////////

		
		//MOD WMS 5-30-03  to allow add cart to be clicked without entering "1"
		if (e.name == "QUANTITY" )
			{
			if (e.value == ""){e.value = 1}
			}		
		
		if ((e.title == null)||(e.title == "")) {
			title = e.name;
		}
		else {
			title = e.title;
		}
		if (((e.type == "text") || (e.type == "textarea")||(e.type == "password")) && !e.special && !e.disabled) {
			if (e.value.length <= 0 && !e.optional && (e.name.indexOf("Ship") == -1)) {
				empty_fields += "\n            " + title;
				continue;
			}
			if (e.number) {
				num = e.value;
				num = stripChar(num, ".");
				num = stripChar(num, ",");
				if (!isNumber(num)) {
					char_check += "\n             " + title;
				}
			}
			if (e.creditCardNumber) {
				e.value = stripChar(e.value, " "); 
				e.value = stripChar(e.value, "-"); 
				invalid_card = isCardNumValid(e.value);
			}
			
			if ((e.creditCardExpMonth)||(e.creditCardExpYear)) {
				if (e.creditCardExpMonth) {
					month = e.value;
					month = stripChar(month, " ")
					if (!isNumber(month)) {
						invalid_date = true;
						month = null;
					}
				}
				if (e.creditCardExpYear) {
					year = e.value;
					year = stripChar(year, " ")
					if (!isNumber(year)) {
						invalid_date = true;
						year = null;
					}
				}
				if ((month != null) && (year != null)) {
					if(!isCardDateValid(year, month)) {
						invalid_date = true;
					}	
				}
			}
			
			// MOD WMS 121504-1			
			if (e.eMail) {
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}
			
            if (e.name == "txtEmail") {
				eMail = e.value;
				// MOD WMS 011904-3
				if (!emailCheck(eMail)){
					invalid_eMail = true;
				}
				else {
					invalid_eMail = false;
				}
			}	
			if (e.name == "txtFriend") {
				eMail = e.value;
				// MOD WMS 011904-3
				if (!emailCheck(eMail)){
					invalid_eMail = true;
				}
				else {
					invalid_eMail = false;
				}
			}	

			if (e.phoneNumber) {
				num = e.value;
				num = stripChar(num, " ");
				num = stripChar(num, "-");
				num = stripChar(num, "+");
				if (num.length < 10) {
					invalid_phoneNumber = true;
				}	
			}
		}
		if (e.quantityBox) {
			iQuantity = e.value;
			if (!isNumber(iQuantity)) {
				quantity_check = true;
			}
			if (parseInt(iQuantity) < 0) {
				quantity_check = true;
			}
			if ((iQuantity) < 1) {
				quantity_check = true;
			}

		}
		if (e.password) {
			if (form.Password.value != form.Password2.value) {
					passwd_mismatch = true;
			}
		}
		if (e.special) {
			checkSpecial = specialCase(e, form);
			if (tempError != checkSpecial) {
				special_Error = special_Error + checkSpecial
			}
			tempError = checkSpecial;
		}
		if (e.type == "select-one" && !e.optional) {
			if (e.value == "") {
				empty_fields += "\n            " + title;
				continue;
			}
		}
	}
	
	if (!empty_fields && !char_check && !special_Error && !invalid_card && !invalid_date && !invalid_eMail && !quantity_check && !invalid_phoneNumber && !passwd_mismatch) {return true}
	
	msg = "The form was not submited due to the following error(s).\n";
	
	upperLine = "\n_________________________________________________________\n\n";
	lowerLine = "_________________________________________________________\n";
	
	if (empty_fields) {
		msg += upperLine;
		msg += "The following Billing Information field(s) must be filled in:\n";
		msg += lowerLine;
		msg += empty_fields;
	}
	if (char_check) {
		msg += upperLine;
		msg += "The following field(s) need a numeric value:\n";
		msg += lowerLine;
		msg += char_check;
	}
	if (quantity_check) {
		msg += upperLine;
		msg += "Please Enter a Positive Integer.\n"
		msg += lowerLine;
	}
	if (invalid_card) {
		msg += upperLine;
		msg += "The Credit Card Number is an invalid format.\n";
		msg += lowerLine;
	}
	if (invalid_date) {
		msg += upperLine;
		msg += "The Credit Card has Expired.\n";
		msg += lowerLine;
	}
	if (invalid_eMail) {
		msg += upperLine;
		msg += "The Email Address is in an invalid format.\n";
		msg += lowerLine;
	}
	if (invalid_phoneNumber) {
		msg += upperLine;
		msg += "Please enter a valid Phone Number with area code.\n";
		msg += lowerLine;
	}
	if (special_Error) {
		msg += upperLine;
		msg += special_Error + "\n";
		msg += lowerLine;
	}
	if (passwd_mismatch) {
		msg += upperLine;
		msg += "Your passwords did not match. Please enter them again.\n";
		msg += lowerLine;
	}		
	alert(msg);
	return false;
}	

function sfCheckPlus(frm) {
	
	
	/*MOD 062403, MOD 091403-6 */
	if (window.document.form1.ShipFirstName.value != "" || 
	     window.document.form1.ShipLastName.value != "" || 
	      window.document.form1.ShipCompany.value != "" || 
	     window.document.form1.ShipAddress1.value != "" || 
	     window.document.form1.ShipAddress2.value != "" || 
	         window.document.form1.ShipCity.value != "" || 
         ((window.document.form1.ShipState.value != "") && (window.document.form1.ShipState.value != null))|| 
         window.document.form1.ShipZip.value != "" || 
         window.document.form1.ShipPhone.value != "" || 
         window.document.form1.ShipEmail.value != "" || 
         window.document.form1.ShipFax.value != "")			  
	{
	/*MOD 072403-8, MOD 080703-1 */
	if (window.document.form1.ShipFirstName.value == "" || window.document.form1.ShipLastName.value == "" || window.document.form1.ShipAddress1.value == "" || window.document.form1.ShipCity.value == "" || window.document.form1.ShipState.value == "" || window.document.form1.ShipZip.value == "" || window.document.form1.ShipCountry.value == "")			  
	{
	//window.alert("Please either fill in all shipping info or no shipping info.")
	window.alert("If you fill in the shipping info, you must fill in the First Name, Last Name, First Street Address field, City, State, Zip, and Country fields.")
	return false
	}
	else
	{return sfCheck(frm)}
	}		  

}
function POCheck(poname,poNum)
{
  if(poname == "" || poNum == "")
   {
    alert("Please Enter the required purchase order information"); 
      
    return false;
   } 
   else
    {
     return true;
    }
}


//used in icb 
function emailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var firstChars=validChars
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom="(" + firstChars + validChars + "*" + ")"
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) {
	 alert("Email address seems incorrect (check @ and .'s)")
	 return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	 alert("The username doesn't seem to be valid.")
	 return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	   for (var i=1;i<=4;i++) {
	     if (IPArray[i]>255) {
	         alert("Destination IP address is invalid!")
	 	return false
	     }
	 }
	 return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	 alert("The domain name doesn't seem to be valid.")
	 return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	 domArr[domArr.length-1].length>3) {
	alert("The address must end in a three-letter domain, or two letter country.")
	return false
	}

	/* removed to allow abc@somedomain.cc
	if (domArr[domArr.length-1].length==2 && len<3) {
	var errStr="This address ends in two characters, which is a country"
	errStr+=" code.  Country codes must be preceded by "
	errStr+="a hostname and category (like com, co, pub, pu, etc.)"
	alert(errStr)
	return false
	}*/

	if (domArr[domArr.length-1].length==3 && len<2) {
	var errStr="This address is missing a hostname!"
	alert(errStr)
	return false
	}

	return true;
}