<!--
/*
-- These are the arrays used to controll the validation
	var requiredFields = "";
	var fieldNames = "";
	var optionalFields = "";
	var oFieldNames = "";
	var emailFields = "";
	var zipFields = "";
	var numericFields = "";
	var phoneFields = "";
	var creditcardFields = "";
	var allFields = "";
	var sizeFields = "";
*/
var charexp = /[a-z0-9]/i
var inputexp = /./
var zipexp = /^\d{5}$|^\d{5}[\-\s]?\d{4}$|^\d{3}[\-\s]\d{4}$|^[A-Z]\d[A-Z]\s\d[A-z]\d$/
var emailexp = /^[a-z0-9][\-a-z_0-9\.]*[a-z_0-9]@[\-a-z_0-9\.]+\.[a-z]{2,7}$/i
var moneyexp = /^\d{0,4}(\.\d{2})?$/
var fieldCheck = true;		// Set validation to always pass by default.
var fieldsNeeded = "";

function isValid(pattern, str) {
	return pattern.test(str)
}

function validateForm(input, formName) {
    var validationPasssed
	fieldCheck = true;
	fieldsNeeded = "There was an error in the following field(s):\n\n";

	if (formName == "checkout_Pymt" && input.PayType.value != "SKIP" && input.rdoPayPO.checked == true) {
		if (!(input.rdoHardCopy1.checked == true || input.rdoHardCopy2.checked == true)){
			fieldsNeeded += "Indicate if you are sending a Hard Copy of the PO.\n";
			fieldCheck = false;
		}
	}
	// First check to see if the fields in the requiredFields array were left blank.
	if (requiredFields[0].toUpperCase() != "N/A") {
		for(var fieldNum=0; fieldNum < requiredFields.length; fieldNum++) {
			if (!hasChar(input.elements[requiredFields[fieldNum]].value)) {
				fieldsNeeded += fieldNames[fieldNum] + " field is missing or invalid.\n";
				fieldCheck = false;
			} else {
				validateFieldSize(input,formName,requiredFields[fieldNum],fieldNames[fieldNum]);
			}
		}
	}
	
	if (optionalFields[0].toUpperCase() != "N/A") {
	   for(fieldNum=0; fieldNum < optionalFields.length; fieldNum++) {
			//alert("Field: " + optionalFields[fieldNum] + "  Value: " + input.elements[optionalFields[fieldNum]].value);
			if (hasInput(input.elements[optionalFields[fieldNum]].value)) {
				validateFieldSize(input,formName,optionalFields[fieldNum],oFieldNames[fieldNum]);
			}
	  }
	}

	//  Runs page specific validation as defined on that page
	//     Allows the fieldCheck and fieldsNeeded to be used 
	ConditionalChecks(input);

    // All required fields have been check pass back if the validation succeeded.
    if (fieldCheck == true) {
       	return true;
	} else {
		fieldsNeeded += "\nYou can not submit this information until all errors have been corrected."
        alert(fieldsNeeded);
        return false;
	}
}	// End validateForms

function validateFieldSize(input,formName,fieldID,fieldName) {
	var i;
  // Check to see if the field are of correct size according to database.
	for(i = 0; i < allFields.length; i++) {
		if (allFields[i] == fieldID) {
		    var s = new String(input.elements[fieldID].value);
		    if((s.length) <= sizeFields[i]) {
				validateField(input,formName,fieldID,fieldName);
			} else  {
				fieldsNeeded += fieldName + " field Size is "+ s.length + ", but has a of maximum size of " + sizeFields[i]+ " \n";
				fieldCheck = false;
			 }
		}//if
	}//for
}//function

function validateField(input,formName,fieldID,fieldName) {
	var i;
	var bPassed = true;
	// Check to see if the field needs an e-mail vailidation check.
	for(i = 0; i < emailFields.length; i++) {
		if (emailFields[i] == fieldID) {
			if (!isValid(emailexp,input.elements[fieldID].value)) {
				fieldsNeeded += fieldName + " field does not contain a valid e-mail address.\n";
				fieldCheck = false;
			}
		}
	}

	// Check to see if the field needs a Zip code vailidation check.
	for(i = 0; i < zipFields.length; i++) {
		if (zipFields[i] == fieldID) {
			if (!isValid(zipexp,input.elements[fieldID].value)) {
				fieldsNeeded += fieldName + " field is not in the proper zip code format (i.e. 74146 or 74146-1234).\n";
				fieldCheck = false;
			}
		}
	}

	// Check to see if the field needs a numeric code vailidation check.
	for(i = 0; i < numericFields.length; i++) {
		if (numericFields[i] == fieldID) {
			bPassed = allNumeric(input.elements[fieldID].value)
			if (bPassed == false) {
				fieldsNeeded += fieldName + " field must contain all numeric values (i.e. 0 - 9).\n";
				fieldCheck = false;
			}
		}
	}

	// Check to see if the field needs a phone number vailidation check.
	for(i = 0; i < phoneFields.length; i++) {
		if (phoneFields[i] == fieldID) {
			// Perform the numeric code vailidation check.
			bPassed = isPhone(input.elements[fieldID].value)
			if (bPassed == "false") {
				fieldsNeeded += fieldName + " field is not a valid phone number (example format: 918-6224522 or 9186224522).\n";
				fieldCheck = false;
			} else {
				eval("input." + fieldID + ".value = bPassed");
			}
		}
	}

	// Check to see if the field needs a credit card vailidation check.
	for(i = 0; i < creditcardFields.length; i++) {
		if (creditcardFields[i] == fieldID) {
			// Perform the numeric code vailidation check.
			bPassed = isCreditCard(input.elements[fieldID].value)
			if (bPassed == false) {
				fieldsNeeded += fieldName + " field does not contain a valid credit card number (example format: 1111222233334444 or 1111-2222-3333-4444).\n";
				fieldCheck = false;
			} else {
				input.elements[fieldID].value = bPassed;
			}
		// Call this function to make sure that the expiration date hasn't already expired.
//			bPassed = checkExpDate(formName);
			bPassed = checkExpDate(input);
			if (bPassed == false) {
				fieldsNeeded += "The expiration date you have selected has already expired.  Please double check your selections.\n";
				fieldCheck = false;
			}
		}
	}
} // End validateField


function allNumeric(str) {
	var pattern = /[^0-9]/
	return !pattern.test(str)
}

function allAlpha(checkString) {
	var sChr;
	var bAlpha = false;		// Set to false in case white space characters are found
	var i;

    // Loop through the string character by character.
    for (i = 0; i < checkString.length; i++) {
        sChr = checkString.substring(i, i+1);

        // ENSURE CHARACTER IS AN ALPHA CHARACTER
        if ((sChr >= "a" && sChr <= "z") || (sChr >= "A" && sChr <= "Z" ) ||
			(sChr >= "0" && sChr <= "9"))
		{
            bAlpha = true;		// A character or number was found.
            break;
		}
	}
    return bAlpha;
}



// This function determines if the string passed in is a valid US ten digit phone number.
//If the string is valid, it returns the string formated (###) ###-####, else returns false.
function isPhone(strPhone) {
	var s = new String(stripNonDigits(strPhone));
	if (s.length != 10) {
		// inappropriate length
		return "false";
	}
	s = "(" + s.substring(0,3)+ ") " +s.substring(3,6)+ "-" +s.substring(6,10)
	return s;
}

// This function determines if the string passed in is a valid credit
// card number.  It accepts only numeric values between 13-16 digits. If the
// string is valid, it returns card number, else false.
function isCreditCard(strCC) {
	var s = new String(stripNonDigits(strCC));
	var iSum = 0;
	var iMulti = 0;
	var iProd = 0;
	var iRet;
	var sChr;

	if (s.length < 13 || s.length > 16) {
		// inappropriate length
		return false;
	}
    // Always set card number to an even length string to make this work correctly.
    iRet = (s.length % 2);
    if (iRet == 1) s = "0" + s;

	// Run valid credit card check.
	for (var i = s.length-1; i > '-1'; i--) {
		sChr = s.charAt(i);
		iRet = ((i + 1) % 2);		// Add one to i to offset for zero based length.
		if (iRet == 1) {
			iProd = parseInt(sChr) * 2;
			// If the digit double is 10 or more then the amount to add to the sum is 1+ digit mod 10
			if (iProd > 9) {
				iSum += (iProd - 9);
			} else 	{
				iSum += iProd;
			}
		} else {
		iSum += parseInt(sChr);
		}
	}
	iRet = (iSum % 10);
	if (iRet != 0) return false;		// Invalid card number.
	return s;
}


// Verify that the expiration date choosen has not expired.
function checkExpDate(input) {
	var dDate;
	var dToday = new Date();
	var iMonth = getOPTValue(input.listmonth);
	var iYear = getOPTValue(input.listyear);

	// Make sure that the values aren't empty.
	if (iMonth == ' ' || iMonth == '') return false;
	if (iYear == ' ' || iYear == '') return false;
	if (iMonth == '12') {
		iMonth = '0';
	    	//parseInt returns incorrect values for 08 and 09 strings for some reason.
		var num = parseInt (iYear);
			if (iYear == "08") num = 8;
			if (iYear == "09") num = 9;
			iYear = parseInt(num) + 1;
			if (parseInt(iYear) < 10) iYear = '0' + iYear;
	}
	iYear = '20' + iYear;
	dDate = new Date(iYear,iMonth,01);
	if (dDate < dToday) {
		return false;
	}
	return true;
}

// This function returns that value of the currently selection
// dropdown list option.  The value return updates the state textbox object.
function getOPTValue(input) {
	var listValue = "";
	// Read the current value of the drop down list object and return its value.
	if (input.selectedIndex != -1) {
	   listValue = input.options[input.selectedIndex].value;
	}
	return listValue;
}

// Rounding Function
function roundOff(value, precision) {
	value = "" + value //convert value to string
	precision = parseInt(precision);

	var whole = "" + Math.round(value * Math.pow(10, precision));
	result = whole / Math.pow(10, precision);

	result = "" + result;
	var decPoint = result.indexOf('.');
	if (decPoint == -1) {
		result += "."
		for (var i = 0; i < precision; i++) {
			result += "0";
		}
	} else {
		whole = result.substr(0,decPoint) + ".";
		var decPart = result.substr(decPoint+1);
		decPoint = precision - decPart.length;
		if (decPoint > 0) {
			for (var i = 0; i < decPoint; i++) {
				result += "0";
			}
		}
	}

/*	// Replaced by Michael White  1/10/2006 to correct problem with not enough trailing zeros
	var whole = "" + Math.round(value * Math.pow(10, precision));

	var decPoint = whole.length - precision;

	if(decPoint != 0) {
		result = whole.substring(0, decPoint);
		result += ".";
		result += whole.substring(decPoint, whole.length);
		if (decPoint < 0) {
			for (var i = 0; i < precision + decPoint; i++) {
				result += "0";
			}
		}
	} else {
		result = "0." + whole;
	}	*/

	return result;
}

// This function sets the default value for a drop down list box based
// on the parameter passed to the function.  The first parameter is the default
// value and the second is the form name.
function setOPTValue(input, listObj) {
	for (var i = 0; i < listObj.length; i++) {
		if (listObj.options[i].value == input) {
			listObj.options[i].selected = true;
			return true;
		}
	}
}

function hasChar(str) {
	return charexp.test(str)
}

function hasInput(str) {
	return inputexp.test(str)
}

function stripNonDigits(str) {
	return str.replace(/[^0-9]/g,"")
}

function isMoney(str) {
	return moneyexp.test(str)
}

function ret_getElementById(tagId)
{
	/*  Returns a reference to a form object */
	var obj;
	if(document.all)
		obj=document.all[tagId];
	else
		obj=document.getElementById(tagId);
	if(obj && obj.length && obj[0].id==tagId)
		obj=obj[0];
	return obj;
}

// This function sets the default value for a drop down list box based
// on the parameter passed to the function.  The first parameter is the default
// value and the second is the form name.
function getState(input, listObj)
{
	for (var i = 0; i < listObj.length; i++)
	{
		if (listObj.options[i].value == input)
		{
			listObj.options[i].selected = true;
			return true;
		}
	}
}
//-->
