	///////////////////////////////////////////////////////////////////////////
	//
	// ValidationMethods.js
	//
	// This page contains common business validation code used by the TESSCO
	// web sites.
	//
	// NOTE:  THIS PAGE REQUIRES COMMON.js
	//
	///////////////////////////////////////////////////////////////////////////

	// This function will validate that the SKU is a 5 or 6-digit number.
	// As of 7/18/00, there are only 5 digit skus.  Since the web page only
	// accepts up to 5 characters, the 6 digit check is for future functionality. 
	
	function isValidSKU(sku) {
		
		var work = parseNumbersFromString(sku);
		
		if (work.length == 5 || work.length == 6) {		
				return true;
		}else {
				return false;
		}	
	}
	
	// This function will validate that the new quantity is valid.
	
	function isValidQuantity(qty) {
	
		if (!isNaN(qty)) {
			if (qty > 0) {
				if ( qty.indexOf('.') != -1 ) {
					return false;
				} else {
					return true;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}	
	}	
	
	
//======================================================================
// Name:		Is Valid PIN
//
// Function:	Confirms that the PIN follows TESSCO business rules.
//
// Argument:	PIN to evaluate.
//======================================================================
function isValidPIN(pin) {

	if (isNumeric(pin) && pin.length == 12 ) {
		return true;
	}else {
		return false;
	}

}

	
	function changeQuantity(field) {
		if (field.value.length > 0) {
		// remove leading 0's to avoid interpretation as octal.
			field.value = field.value.replace(/^0/,"");
			if (field.value.length == 0 ) {
				field.value=0;
			};

			if (!isValidQuantity(field.value))	{
				alert("\"" + field.value + "\" is not a valid quantity.");
				field.focus();
				field.value="1";
				field.select();
			}
		}
	}

	function stripCharacter(words,character) 
	{
		if( words.indexOf(' ') != -1 )
	  	{
	  		words = words.split(character).join("");
		}		 
	 	return( words );
    }


	function validateSKU(field) {

		// trim the value of spaces so that it will not persist
		field.value = stripCharacter( field.value, ' ' );
		
		var strValue = field.value;
		
		field.value = cleanBarcode(field.value);

		if ((field.value.length < 5 || field.value.length > 6) &&
		    field.value.length != 0) {

			alert("Please enter a five-digit or six-digit TESSCO part number.");
			field.value = "";
			field.focus();
			return "";

		} else {
			if (field.value.length > 0) {
				//if (!isNumeric(field.value) || field.value.indexOf('.') != -1  ) {
				if ( field.value.indexOf('.') != -1  ) {
					alert("Please enter a five-char or six-char TESSCO part number.");
					field.value = "";
					field.focus();
					return "";
				}
				
			}
		}
		
		if( strValue != field.value.toUpperCase() ){
			field.value = field.value.toUpperCase();
		}
					
		if ( strValue.length == 12 && isNumeric( strValue ) ) {
			var codeStr = strValue.substring(0,6);

			if (codeStr == "646444" || codeStr == "729198") {
				var nameSku		=	field.name;
				var objQty = eval( "document.worksheet.newQty" + nameSku.substring(6, nameSku.length) );
				var objSku = eval( "document.worksheet.newSku" + ( eval(nameSku.substring( 6, nameSku.length )) + 1 ) );
				
				if ( ( eval(nameSku.substring( 6, nameSku.length ))) < 10 ) {
					objQty.value = 1;
					objSku.focus();
				} else {
					objQty.value = 1;
					doUpdate();
				}
			}
		}
	}

	function cleanBarcode(str) {
		var codeStr;
		var skuStr;
		if (str.length == 12 && isNumeric(str)) {
			codeStr = str.substring(0,6);

			if (codeStr == "646444" || codeStr == "729198") {
				skuStr = str.substring(6,11);

				if (codeStr == "646444") {
					skuStr = "4" + skuStr;
				}
				return skuStr;
			}
		}
		return str;
	}

	function changeQuantityNew(field) {
		if (field.value.length > 0)	{
		// remove leading 0's to avoid interpretation as octal.
			field.value = field.value.replace(/^0/,"");
			if (field.value.length == 0 ){
				field.value=0;
			};

			if (!isValidQuantity(field.value))	{
				alert("\"" + field.value + "\" is not a valid quantity.");
				field.focus();
				field.value="";
				field.select();
			}
		}
	}
	
	function checkSkuLength( field ) {
		var strValue = field.value;
		
		if ( strValue.length == 12 && isNumeric( strValue ) ) {
			var codeStr = strValue.substring(0,6);

			if (codeStr == "646444" || codeStr == "729198") {
				var nameSku		=	field.name;
				var objQty = eval( "document.worksheet.newQty" + nameSku.substring(6, nameSku.length) );
				var objSku = eval( "document.worksheet.newSku" + ( eval(nameSku.substring( 6, 7 )) + 1 ) );
				
				if ( ( eval(nameSku.substring( 6, nameSku.length ))) <= 10 ) {
					objSku.focus();
				} 
			}
		}
	}
	
	function validateUPC(field) {

		// trim the value of spaces so that it will not persist
		field.value = stripCharacter( field.value, ' ' );
		
		var strValue = field.value;
	
		if ((field.value.length < 11 || field.value.length > 12) &&
		    field.value.length != 0) {
				
			var msg = 'A standard UPC is 12 digits, or 11 digits if the check digit is not included.\n';
			msg += 'You have entered a non-standard value that is ' + field.value.length + ' characters.\n\n Do you wish to continue anyway?';
			if( !confirm( msg ) ){
				field.focus();
				return "";
			}
	
		} else {
			if (field.value.length == 12) {
				if ( getUpcCheckDigit( field.value ) != field.value.charAt(11)  ) {
					alert("The UPC entered is invalid.");
					//field.value = "";
					field.focus();
					return "";
				}
				
			}
		}
	}
	
	function getUpcCheckDigit(upc_code){
	    odd_total  = 0;
	    even_total = 0;
	 
	    for(i=0; i<11; i++)
	    {
	        if(((i+1)%2) == 0) {
	            /* Sum even digits */
	            even_total += parseInt(upc_code.charAt(i));
	        } else {
	            /* Sum odd digits */
	            odd_total += parseInt(upc_code.charAt(i));
	        }
	    }
		
	    sum = (3 * odd_total) + even_total;
	 
	    /* Get the remainder MOD 10*/
	    check_digit = sum % 10;
	 
	    /* If the result is not zero, subtract the result from ten. */
	    return (check_digit > 0) ? 10 - check_digit : check_digit;
	} 

