var bNS = (window.Event) ? 1 : 0;
var lCount;
var oErrors = new Array();


function ValidRadio( oRad, sMes )
{
	var i;
	var len = oRad.length
	for( i=0; i < len; i++)
		if( oRad[i].checked == true )
			return oRad[i].value;

	if(sMes)
	{
		oErrors[lCount] = sMes;
		lCount++;
	}			
	return -1;
}

function ValidString(sFldVal, sMes)
{
	// Perform test
	sFldVal = StripSpacesFromText(sFldVal)
	if(sFldVal.length == 0)
	{
		if(sMes)
		{
			oErrors[lCount] = sMes;
			lCount++;
		}
		return false;
	}
	return true;
}

function StripSpaces(oText)
{
	oText.value = oText.value.replace(/ /g, "");
}

function StripSpacesFromText(sText)
{
	return sText.replace(/ /g, "");
}

function ValidEmail(sFldVal, sMes)
{
	// VG, 10/5/2006, Avoid invalid characters and restrict local-part of an e-mail address up to 64 char and the domain name up to 255 char.
	// Perform test	
	var oValid = new RegExp("^([A-Za-z0-9][A-Za-z0-9_\.\-]{0,64})[@]([A-Za-z0-9][A-Za-z0-9_\-]{0,200})([\.]([A-Za-z0-9][A-Za-z0-9_\-]{0,200}))*[\.]([A-Za-z0-9][A-Za-z0-9_\-]{0,200})$"); 
	if (!oValid.test(sFldVal))
	{
		if(sMes)
		{
			oErrors[lCount] = sMes;
			lCount++;
		}
		return false;
	}
	var MailCont = sFldVal.split("@")
	if(MailCont[1].length > 255)
	{
		oErrors[lCount] = sMes;
		lCount++;
		return false;
	}
	return true;
}

function ValidLen(sFldVal, lAtLeast, lAtMost, sMes)
{
	// Perform test
	var lLen = sFldVal.length;
	if((lLen < lAtLeast)||(lLen > lAtMost))
	{
		if(sMes)
		{
			oErrors[lCount] = sMes;
			lCount++;
		}
		return false;
	}
	return true;
}

function ValidSelection(oSelect, sMes)
{
	if((oSelect.length == 0)||(oSelect.options[oSelect.selectedIndex].value == 0)||(oSelect.options[oSelect.selectedIndex].value == 'None'))
	{
		if(sMes)
		{
			oErrors[lCount] = sMes;
			lCount++;
		}
		return false;
	}
	return true;
}

function ValidPhone(oNumber, sMes)
{
	var sNum	= StripAlpha(oNumber.value);
	var len		= sNum.length;
	if(len == 10)
	{
		sNum = "(" + sNum.substring(0,3) + ") " + sNum.substring(3,6) + "-" + sNum.substring(6, len);
		oNumber.value = sNum;
	}
	else
	{
		if(sMes)
		{
			oErrors[lCount] = sMes;
			lCount++;
		}
		return false;
	}
}

function StripAlpha(txtField)
{
	var curValue
	var newValue
	var sChar
	var len
	var i
	
	newValue = ""
	curValue = txtField;
	len = curValue.length;
	// If length greater than 0 strip out letters
	if (len > 0)
		for(i=0;i<len;i++)
		{
			sChar = curValue.substring(i, i+1);
			if (sChar >= "0" && sChar <= "9")
				newValue += sChar;		
		}
	// Reassign non-comma amount
	return newValue;
}

function FormatPhoneNumber(oNumber, bNoAlert)
{
	var sNum	= StripAlpha(oNumber.value);
	var len		= sNum.length;
	if(len == 10)
	{
		sNum = "(" + sNum.substring(0,3) + ") " + sNum.substring(3,6) + "-" + sNum.substring(6, len);
		oNumber.value = sNum;
		return true;
	}
	else if(bNoAlert)
		return false;
	else
	{
		alert("Please enter a valid phone number including the area code");
		return false;
	}
}

function ShowErrors()
{
	var sAlert = "";
	if(oErrors != 0)
	{
		for(var i=0; i<oErrors.length; i++)
		{
			if(i==0)
				sAlert = "Please correct the following errors:\n\n";
			sAlert += (i+1) + "." + "  " + oErrors[i] + "\n";
		}
	}
	if(sAlert == "") 
		return false;
	else
	{
		alert(sAlert);
		return true;
	}
}

function SubmitItems(oForm)
{
	oForm.submit();
	oForm.disabled = 'true';
}

function ValidateQty(oQty, bDefault) 
{
	var sVal = oQty.value;
	var reg1 = new RegExp("[A-Za-z]"); //Check for hexadecimal notation in the value
	if(sVal!="")
	{
		if(isNaN(sVal) || sVal < 0 || reg1.test(sVal))
		{
			alert("Invalid quantity...please enter a number greater than 0 only.");
			//"onblur" qty checks cause netscape to get caught in a loop if you do not clear the value
			if(bNS)
				oQty.value = ""
			oQty.focus();
			oQty.select();
			return false;
		}
		else
		{	
			// Always round up
			var oNum = new Number()
			oNum = oQty.value;
			if((oNum % 1) != 0)
			{
				// This will round to nearest integer
				oNum = Math.round(oQty.value)
				// Add one if it rounded down
				if(oNum < oQty.value)
					oNum += 1
			}
			oQty.value = Math.round(oNum);
			return true;
		}
	}
	else
	{
		if(bDefault)
			oQty.value = 1;
		// There used to be an else statement with an alert to "Enter an valid quantity"
		// Not sure why it was used, so if it is, be sure to resolve an 
		// issue with netscape where an infinate alert loop is observered. (Schleicher)
	}
	return false;
}	

function ValidateShelfBinNo (oShelfBinNo)
{
	var sShelfNo = oShelfBinNo.value;
	if (sShelfNo.indexOf("'") > -1 || sShelfNo.indexOf(",") > -1 || sShelfNo.indexOf("`") > -1 || sShelfNo.indexOf("-") > -1)
		return false;
	else
		return true;
}

function ValidateFloat(oFloat, bDefault, bCanBeZero, fMaxValue) 
{
	var sVal = trim(oFloat.value);
	var reg1 = new RegExp("[A-Za-z]"); //Check for hexadecimal notation in the value
	if(sVal!="")
	{		
		if(isNaN(sVal) || reg1.test(sVal) || ((bCanBeZero)?(sVal<0):(sVal<=0)))
		{
			sMsg = "Invalid number...please enter numbers greater than 0 only.";
			alert(sMsg);
			//"onblur" qty checks cause netscape to get caught in a loop if you do not clear the value
			if(bNS)
				oFloat.value = ""
			oFloat.focus();
			oFloat.select();
			return false;
		} else {
			if(!isNaN(fMaxValue)) {
				if(fMaxValue < oFloat.value) {
					sMsg = "Invalid number...please enter numbers less than " + fMaxValue + " only.";
					alert(sMsg);
			 		//"onblur" qty checks cause netscape to get caught in a loop if you do not clear the value
					if(bNS)
						oFloat.value = ""
					oFloat.focus();
					oFloat.select();
					return false;
				} else {
					return true;
				}
			} else {
				return true;
			}
		}
	}
	else
	{
		if(bDefault)
			oFloat.value = 1;
		// There used to be an else statement with an alert to "Enter an valid quantity"
		// Not sure why it was used, so if it is, be sure to resolve an 
		// issue with netscape where an infinate alert loop is observered. (Schleicher)
	}
	return false;
}


function ValidateUnit(oUnit,nMax)
{
	var sVal = StripAlpha(oUnit.value);
	// use the stripped value
	oUnit.value = sVal;
	// Default to case(1) if not numeric number not entered
	if((isNaN(sVal))||(sVal==""))
		oUnit.value = "0";
	else if(( sVal < 0) || (sVal > nMax))
	{
		if(bNS)
		{
			oUnit.value = "0";
			if( nMax < 2 )
				alert("Invalid unit of measure...please enter 0 for cases or 1 for eaches.");
			else
				alert("Invalid unit of measure...please enter 0 for cases, 1 for eaches, or 2 for multiples.");
			oUnit.focus();
			oUnit.select();
			return false;
		}
		else
		{
			if( nMax < 2 )
				alert("Invalid unit of measure...please enter 0 for cases or 1 for eaches.");
			else
				alert("Invalid unit of measure...please enter 0 for cases, 1 for eaches, or 2 for multiples.");
			oUnit.focus();
			oUnit.select();
			return false;
		}
	}
	return true;
}

function ValidateProduct(oProduct,bDoNotSetFocus) 
{
	var sProduct = trim(oProduct.value);
	if (sProduct.length > 0)
	{
		var oValid	= new RegExp("[A-Za-z0-9]{" + sProduct.length + "}");
		if (!oValid.test(sProduct))
		{
			alert("Invalid product...please enter only characters and/or numbers.");
			if (!bNS)
			{
				if(bDoNotSetFocus == null || !bDoNotSetFocus) {
					oProduct.focus();
					oProduct.select();
				}
			}
			return false;
		}
	}
	//No value for product, check if there is a qty associated and clear it if so -- BUG04187 
	else
	{
		//Only default Quantity if there is a Prod ID
		var sProdID  = new String(oProduct.id);
		var lPos	= parseInt(sProdID.substr(4));
		var oPid	= eval("document.getElementById('pos_" + (lPos+1) + "');");
		
		if(oPid){
			oPid.value = '';
		}
		return true;
	}
	return true;
}

function ValidLineItems(oItem, lLow, lHigh, sMes, lDefault)
{
	var sFldVal=oItem.value;
	// Perform test
	if((isNaN(sFldVal))||(sFldVal<lLow)||(sFldVal>lHigh))
	{
		alert(sMes);
		oItem.value = lDefault;	
		oItem.focus();
		oItem.select();
	}
}

function checkdate(sDate, sMes)
{
	//declear vars
	var err			= 0;
	var psj			= 0;
	a				= sDate
	//var	sMes;			
	if (a.length != 10) err=1
	b = a.substring(0, 2)// month
	c = a.substring(2, 3)// '/'
	d = a.substring(3, 5)// day
	e = a.substring(5, 6)// '/'
	f = a.substring(6, 10)// year

	//basic error checking
	if (b<1 || b>12) err = 1
	if (c != '/') err = 1
	if (d<1 || d>31) err = 1
	if (e != '/') err = 1
	if (f<0 || f>2099) err = 1
	
	// more basic error checking
	if(isNaN(b)) err=1
	if(isNaN(d)) err=1
	if(isNaN(f)) err=1
	
	//advanced error checking
	// months with 30 days
	if (b==4 || b==6 || b==9 || b==11)
		if (d==31) err=1

	// february, leap year
	if (b==2)
	{
		// feb
		var g=parseInt(f/4)
		if (isNaN(g))
			err=1

		if (d>29) err=1
		if (d==29 && ((f/4)!=parseInt(f/4))) err=1
	}
	if (err==1)
	{
		if (sMes != "")
		{
			oErrors[lCount] = sMes;
			lCount++;
		}
		else
		{
			sMes = "This is not a valid date."
			oErrors[lCount] = sMes;
			lCount++;
		}
		return false;
	}
	else
	{
		return true;
	}
}
function ValidateIntFloatQty(oQty)
{			// U.S.N 02/14/2008
			var sVal = oQty.value;
			var reg1 = new RegExp("[A-Za-z]"); //Check for hexadecimal notation in the value
			if(sVal!="")
			{
					if(isNaN(sVal) || sVal < 0 || reg1.test(sVal))
					{
						alert("Invalid quantity...please enter a number greater than 0 only.");
						//"onblur" qty checks cause netscape to get caught in a loop if you do not clear the value
						if(bNS)
							oQty.value = ""
						oQty.focus();
						oQty.select();
						return false;
					}
					else
					{	
						return true;
					}
			}
			return false;
}
function ValidateAlphaNum(text)
{
	var reg = /\w/;
	if(text.value.match(reg))
		return true;
	else
		return false;
}

function ValidateNumber(oNumber, message)
{
	var sVal = trim(oNumber.value);
	if(isNaN(sVal) || sVal < 0)	
	{			
		if(message)
		{
			alert(message);		
			oNumber.focus();
			oNumber.select();
		}			
		return false;
	}
	else
	{			
		return true;		
	}
}

function validatePrice (sFieldName, decimalLength)
{		
	if(isNaN(document.getElementById(sFieldName).value) || document.getElementById(sFieldName).value < 0)
	{
		alert("Invalid Price, please enter numberic value only.");
		document.getElementById(sFieldName).value = '';
		document.getElementById(sFieldName).focus();
		return false;
	}	
	
	if(document.getElementById(sFieldName).value.indexOf('.') != -1)
	{
		if(trim(document.getElementById(sFieldName).value).length - trim(document.getElementById(sFieldName).value).indexOf('.') - 1 > decimalLength)
		{
			alert("You can not enter more than " + decimalLength + " digit(s) after decimal place.");
			document.getElementById(sFieldName).value = '';
			document.getElementById(sFieldName).focus();
			return false;
		}
	}
}

function ValidateAlphaNumeric(sAlphaNumericValue, message)
{	
	var numeric = trim(sAlphaNumericValue.value);
	for(var j=0; j<numeric.length; j++)
	{
		var alphaa = numeric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if(!((hh > 47 && hh < 59) || (hh > 64 && hh < 91) || (hh > 96 && hh < 123)))
		{
			if(message)
			{
				alert(message);
			}
			sAlphaNumericValue.focus();
			return false;			
		}
	}
	return true;
}

function ValidateString(sFldVal) 
{
	var orgVal = sFldVal.value;
	var sVal = RemoveScriptTags(trim(orgVal));
	sVal = StripSpecial(sVal);
	if (orgVal.length != sVal.length)
	{	
		return false;				
	}
	return true;				
}
function ValidateTitle(sFldVal) 
{
	var orgVal = sFldVal.value;
	sVal = orgVal.replace(/\[|\#|\^|\_|\+|\-|\=|\<|\>|\?|\;|\"|\{|\}|\\|\]/g, "")
	if (orgVal.length != sVal.length)
	{	
		return false;				
	}
	return true;				
}

function ValidateZip(sFldVal) 
{
	var orgVal = sFldVal.value;
	var sVal = RemoveScriptTags(trim(orgVal));
	sVal = sVal.replace(/\[|\!|\@|\#|\^|\&|\*|\(|\)|\_|\+|\=|\<|\>|\?|\:|\;|\'|\"|\{|\}|\\|\]|\~|\,|\%|\$/g, "")
	if (orgVal.length != sVal.length)
	{	
		return false;				
	}
	return true;				
}

function ValidateStreet(sFldVal) 
{
	var orgVal = sFldVal.value;	
	var sVal = orgVal.replace(/\[|\!|\@|\^|\&|\*|\(|\)|\_|\+|\-|\=|\<|\>|\?|\:|\;|\'|\"|\{|\}|\\|\]|\~|\%|\$/g, "")
	if (orgVal.length != sVal.length)
	{	
		return false;				
	}
	return true;				
}

function ValidatePhone(sFldVal) 
{
	var numeric = trim(sFldVal.value);
	if(numeric.indexOf("_") > -1 || numeric.indexOf("/") > -1)
	{
		return false;
	}
	numeric = RemoveScriptTags(trim(numeric));	
	for(var j=0; j<numeric.length; j++)
	{
		var alphaa = numeric.charAt(j);
		var hh = alphaa.charCodeAt(0);		
		if (!((hh > 47 && hh < 58) || (hh == 45) ||(hh == 40)|| (hh == 41)))
		{
			return false;			
		}
	}
	return true;			
}


function ValidateItem(sAlphaNumericValue,message)
{
	if(ValidateString(sAlphaNumericValue))
	{
		if(!ValidateAlphaNumeric(sAlphaNumericValue))
		{
			if(message)
			{
				alert(message);
			}
			sAlphaNumericValue.focus();
			return false;
		}
	}
	else
	{		
		if(message)
		{
			alert(message);
		}
		sAlphaNumericValue.focus();
		return false;
	}
	return true;
}

function ValidateDescription(sFldVal)
{	
	var orgVal = trim(sFldVal.value);
	sVal = orgVal.replace(/\[|\^|\_|\+|\=|\<|\>|\?|\;|\"|\{|\}|\\|\]/g, "")
	sFldVal.value = sVal;
}

function ValidEmailMarketFlyer(sFldVal, sMes)
{
	if(sFldVal!="")
	{
		var oValid = new RegExp("^([A-Za-z0-9][A-Za-z0-9_\.\-]{0,64})[@]([A-Za-z0-9][A-Za-z0-9_\-]{0,200})([\.]([A-Za-z0-9][A-Za-z0-9_\-]{0,200}))*[\.]([A-Za-z0-9][A-Za-z0-9_\-]{0,200})$"); 
		if (!oValid.test(sFldVal))
		{
			if(sMes)
			{
				oErrors[lCount] = sMes;
				lCount++;
			}
			return false;
		}
		var MailCont = sFldVal.split("@")
		if(MailCont[1].length > 255)
		{
			oErrors[lCount] = sMes;
			lCount++;
			return false;
		}
		return true;
	}
	else
		return true;
}