/* requires: jsTranslatedVarMessages.asp on including page */
/*
	CheckCodeNumber
	Validates the Code Number
	Given
		a text box object
		a formate error string
		a length error string
	Return
		0 if code is malformed
		1 iff code is well formed
	Notes
		This new version has more error code messages, which are phrases 
		  built on the calling page.
	Notes -- we shortened the requirements
*/
function CheckCodeNumber(oCodeNumber, sError, sLError)
{
	var strCodeNumber = new String(oCodeNumber.value);
	var intCodeLen = strCodeNumber.length;
	var intDashPos = strCodeNumber.indexOf('-');
	var blnIsError = false;
	var strRetErrorMsg = new String();
	
	//Length should be > 3 and < 12
	if (!( intCodeLen > 3 && intCodeLen < 12))
	{
		blnIsError = true;
		strRetErrorMsg += strNumBadLen; //"The code number is too long or short.\n(4 minumum, 9 maximum)"; //strNumBadLen
	}	
	
	/*
		the requirements have been cut, deferring to the code checking
		 functionality of the BAPI. Get a previous version of this page
		 for the older code.
	*/
	
	if(blnIsError)
	{
		alert(strRetErrorMsg);
		oCodeNumber.focus();
		return 0;
	}
		
	//made it
	return 1;
}


/*
	CheckSerialNumber
		Validates the Serial Number
	Given
		text box object
		error message
	Return
		1 iff serial number is valid
		0 otherwise
	Notes
		exactly 11 characters or exactly 8
		if 9, must start with 'AC' and have exactly 6 additional digits
		if 11, first character one of A, B, C, E, F, G, I, J, M, N, O, P, Q  R, S, T, U and 10 digits
		
		also, the error messages have changed and need to be phrases
*/
function CheckSerialNumber(oSerialNumber,sError)
{
	var blnIsError = false;
	var strRetErrorMsg = new String();
	var serialnumber = new String(oSerialNumber.value).toLowerCase();
	var snlength = serialnumber.length
	var firstchar = serialnumber.substr(0,1)
	var validchars = "abcefgijmnopqrstu"
	
	if(serialnumber.length <=0)
	{
		blnIsError = true;
		strRetErrorMsg += strNumBadSerial; //"Please type a valid serial \nnumber ('U' or 'AC' type)."; //strNumBadSerial
	} else if(serialnumber.substring(0,2) == 'ac') {
		if(serialnumber.length != 8)
		{
			blnIsError = true;
			strRetErrorMsg += strNumBadACSerial; //"'AC' type serial numbers must have 8 \nand only 8 characters ('AC' + 6 digits)"; //strNumBadACSerial
		}else if (isNaN(serialnumber.substring(2))){
			blnIsError = true;
			strRetErrorMsg += strNumBadACSerial; //"'AC' type serial numbers must have 8 \nand only 8 characters ('AC' + 6 digits)"; //strNumBadACSerial
		}
	} else {
		if(serialnumber.length != 11)
		{
			blnIsError = true;
			strRetErrorMsg = strNumBadUSerial; //"'U' type serial numbers must be 11 characters.\n 'U' plus exactly 10 digits."; //strNumBadUSerial
		}

		//1st char must be one of a, b, c, e, f, g, i, j, m, n, o, p, q, r, s, t, or u upper or lower case			
		if(validchars.indexOf(firstchar, 0) < 0) 
		{
			if(blnIsError) strRetErrorMsg += "\n\n";
			blnIsError = true;
			strRetErrorMsg = strNumBadSerialFirst;//"1st character must be one of the following: \na, b, c, e, f, g, i, j, m, n, o, p, q, r, s, t, or u \n (upper or lower case)"; //strNumBadSerialFirst
		}

		//validation rules - 2nd character must be an integer
		var secondchar = serialnumber.substr(1,1)
		if (isNaN(secondchar))
		{
			if(blnIsError) strRetErrorMsg += "\n\n";
			blnIsError = true;
			strRetErrorMsg += strNumBadSerialSecond; //"2nd character must be an integer."; //strNumBadSerialSecond
		}
						
		//then need a two digit year followed by a two digit month
		var year = serialnumber.substr(2,2)
		var month = serialnumber.substr(4,2)
		var otherchars = serialnumber.substr(6,5)
		if ( isNaN(year) || isNaN(month) || month < 1 || month > 12 || isNaN(otherchars) || snlength > 11 ) 
		{
			if(blnIsError) strRetErrorMsg += "\n\n";
			strRetErrorMsg += strNumBadSerialDigits; //"All but the 1st character must be digits.\nThe second two digits represent the year.\nThe 3rd & 4th digits represent the month.\nThe final 7 digits comprise the rest of the serial number."; //strNumBadSerialDigits
			blnIsError = true;
		}
	}

	if(blnIsError)
	{
		alert(strRetErrorMsg);
		oSerialNumber.focus();
		return 0;
	}
	
	return 1;
}