//----------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Number_Format
//  Description:	Formats a number value to a given numeric format
//  Arguments:		input   	- the number value to be formatted
//					format		- the format to be used
//  Return Value:	A formatted numeric value
//  Comments:		(none)
//----------------------------------------------------------------
function Number_Format(input, format) {
	var formatArray = new Array;
	var userdecimal = "";
	var serverdecimal = "";
	var sReturn = "";
	var precision = 0;
	var thousandseparator = "";
	
	//If one of the input arguments is blank, return the input value
	if ((input == "") || (format == "")) {
		return input;
	} else if (input + "" == "undefined") {
		return input;	
	}	

	//Assign the format value and decimal separator to be used
	formatArray = format.split("|");
	if (formatArray.length > 1) 
	{
		format = formatArray[0];
		serverdecimal = formatArray[1];
		if(formatArray.length > 2)
			thousandseparator = formatArray[2];
	}
	
	//Identify the format's decimal character
	for (i=0; i < format.length; i++) {
		if ((format.charAt(i) != "-") && 
			(format.charAt(i) != "+") && 
			(isNaN(format.charAt(i)))) {
				userdecimal = format.charAt(i);
				precision = format.substring(i+1);
		}
	}
	//Replace the current decimal character with the format's decimal character and return the result (Tack on empty string to convert to string)
	sTemp = Replace(input, serverdecimal, userdecimal) + "";
	var sDigit;
	var pos = 0;
	var sDecimal = "";
	var sSign = "";
	var sTail = "";
	if(sTemp.charAt(0) == "-")
	{
		sSign = "-"
		sTemp = sTemp.substring(1);
	}
	
	pos = sTemp.indexOf(userdecimal);
	if (pos >=0)
	{
		sDigit = sTemp.substring(0, pos);
		sDecimal = sTemp.substring(pos);
	}
	else
		sDigit = sTemp;
	if(thousandseparator!="")
	{
		while(sDigit.length > 3)
		{
			pos = sDigit.length - 3		
			sTail = thousandseparator + sDigit.substring(pos) + sTail;		
			sDigit = sDigit.substring(0, pos);		
		}
		sDigit = sDigit + sTail;
	}
	if(precision == 0) //remove any decimal point if precision is 0
		sReturn = sSign + sDigit;
	else
		sReturn = sSign + sDigit + sDecimal;
	return sReturn;
}


//----------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Number_onInput
//  Description:	Examines a character entered into a number field and determines
//					whether or not to allow the character to be appended to the 
//					existing numeric value
//  Arguments:		field - the name of the HTML form field containing the number 
//  Return Value:	Boolean - True if the entered character is allowed
//							  False if the entered character is invalid
//  Comments:		This function assumes that the form field in question has a
//					populated format attribute
//----------------------------------------------------------------
function Number_onKeyPress(field) {
	var inputChar = 0;
	var sInput = "";
	var format = "";
	var formatArray = new Array;
	var separator = "";
	var decimal = "";
	var precision = "";
	var sign = "";
	var numdigits = "";
	var digitsLeft = 0;
	var digitsRight = 0;
	var digitsArray = new Array;
	var valueDigitsLeft = 0;
	var valueDigitsRight = 0;
	
	//Identify the character that was entered by the user
	inputChar = window.event.keyCode;
	if(IsMac())//Allow Tab for MAC
	{
	if (inputChar == 9) {
		return true;
	}
		
	}
	//Allow the backspace character
	if (inputChar == 8) {
		return true;
	}

	//Read in the field value and format strings
	sInput = field.value; //JDH DefectID: 241303
	format = field.getAttribute("NumberFormat"); //278706
	
	//Assign the format value and decimal separator to be used
	formatArray = format.split("|");
	if (formatArray.length > 1) {
		format = formatArray[0];
		separator = formatArray[1];
	}

	//Identify the format's decimal character
	for (i=0; i < format.length; i++) {
		if ((format.charAt(i) != "-") && 
			(format.charAt(i) != "+") && 
			(isNaN(format.charAt(i)))) {
				decimal = format.charAt(i);
		}
	}

	// Figure out the number of digits to the left and to the right of the decimal
	var numFormat = format;
	if (numFormat.charAt(0) == "-" || numFormat.charAt(0) == "+")
		numFormat = numFormat.substring(1);
		
	if (decimal != "") {
		var digitsArray = numFormat.split(decimal);
		digitsLeft = parseInt(digitsArray[0]);
		digitsRight = parseInt(digitsArray[1]);
	} else {
		digitsLeft = parseInt(numFormat);
		digitsRight = 0;
	}		
	
	//Determine the format's precision, sign and total number of digits allowed
	if (decimal != "") {
		formatArray = format.split(decimal);
		if (formatArray.length > 1) {
			precision = formatArray[1];
			format = formatArray[0];
		}
		if (format.charAt(0) == "-" || format.charAt(0) == "+") {
			sign = format.charAt(0);
			numdigits = format.substring(1, format.length);
		}
		else {
			numdigits = format;
		}
	}

	//Only the negative sign, the decimal character and number values are allowed to be input 
	if ((String.fromCharCode(inputChar) != decimal) && (String.fromCharCode(inputChar) != "-") && (inputChar < 48 || inputChar > 57)) {
		return false;
	}

	//The decimal character is not allowed for number values with no precision
	if ((String.fromCharCode(inputChar) == decimal) && (precision == "0")) {
		return false;
	}

	// Only allow one negative sign
	if (String.fromCharCode(inputChar) == "-") {
		if (sInput.indexOf("-") >= 0) {
			return false;	
		}		
	}	
	
	// Only allow one positive sign
	if (String.fromCharCode(inputChar) == "+") {
		if (sInput.indexOf("+") >= 0) {
			return false;	
		}		
	}	
	
	//The negative sign is only allowed at the beginning of the numeric value
	if (sign == "-" && String.fromCharCode(inputChar) == sign && !IsMac()) {
		if (document.selection.type.toLowerCase() == "text") {
			// If they first selected some text, then don't allow the negative sign.
			return false;
		} else {
			var caretPos = document.selection.createRange().duplicate();
			
     		// Create textrange for the text of the textbox
     		var textAreaRange = field.createTextRange();	
     		// Make sure they are trying to put the negative sign as the first character.
     		if (textAreaRange.boundingLeft != caretPos.boundingLeft) {
				return false;	
			}				
		}		
	}

	//A negative sign is not allowed in positive values
	if ((sign == "+") && (String.fromCharCode(inputChar) == "-")) {
		return false;
	}

	//Sequential decimal characters are not allowed, but a single leading decimal is
	if ((String.fromCharCode(inputChar) == decimal) && (sInput.lastIndexOf(decimal) == sInput.length-1) && (sInput.length != 0)) {
		return false;
	}

	// Check and see if we can add more numbers (ONLY DO THIS FOR IE AND FOR ENTERING NUMBERS)
	if (sInput != "" && inputChar >= 48 && inputChar <= 57) {
		var fieldValue = sInput;
		if (fieldValue.charAt(0) == "-" || fieldValue.charAt(0) == "+")
			fieldValue = fieldValue.substring(1);
		
		var decimalLocation = fieldValue.indexOf(decimal);
		if (decimalLocation >= 0) {
			digitsArray = fieldValue.split(decimal);
			valueDigitsLeft = digitsArray[0].length;
			if (digitsArray.length > 1) {
				valueDigitsRight = digitsArray[1].length;
			} else {
				valueDigitsRight = 0;
			}		
		} else {	
			valueDigitsLeft = fieldValue.length
			valueDigitsRight = 0;
		}
	
		// Check document.selection to make sure they haven't selected any text that they're attempting to overwrite
		if (!IsMac())
		{
			if (document.selection.type.toLowerCase() == "text") {
				// They have selected some text, allow the input
			} else {
				// Nothing selected	
				var caretPos = document.selection.createRange().duplicate();
				
	     		// Create textrange for the text of the textbox
	     		var decimalRange = field.createTextRange();
	     		
				if (decimalLocation >= 0) {
					decimalRange.findText(decimal);

					if (decimalRange.boundingLeft >= caretPos.boundingLeft) {
						if (valueDigitsLeft >= digitsLeft) {
							return false;	
						}	
					} else {
						if (valueDigitsRight >= digitsRight) {
							return false;	
						}
					}		
				} else {
					if (valueDigitsLeft >= digitsLeft) {
						return false;	
					}	
				}		
			}
		}
		else //JDH DefectID: 241303
		{		
			if (decimalLocation >= 0) {
				if (field.selectionStart <= decimalLocation)
				{
				  if (valueDigitsLeft >= digitsLeft)
					  return false;	
				}
				else
				{
				  if (valueDigitsRight >= digitsRight)
					  return false;		
				}
			}
			else
			{
			  if (valueDigitsLeft >= digitsLeft)
				return false;	
			}
		}
		
	}
	//If no errors are found, return true to allow the input character
	return true;

}


//----------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Number_Unformat
//  Description:	Formats an already formatted number into a new numeric format
//  Arguments:		sInput	 - the number value to be reformatted
//					format	 - the old format that was used
//					unformat - the new format to be used
//  Return Value:	A reformatted numeric string
//	Comments:		(none)
//----------------------------------------------------------------
function Number_Unformat(sInput, format) {
	var formatArray = new Array;
	var userdecimal = "";
	var serverdecimal = "";
	var sReturn = "";
	var precision = 0;
	var thousandseparator = "";
	var sTemp = ""
	var strTempSeparator = "@";
	
	//If one of the input arguments is blank, return the input value
	if ((sInput == "") || (format == "")) {
		return sInput;
	}
	
	//Assign the format value and decimal separator to be used
	formatArray = format.split("|");
	if (formatArray.length > 1) 
	{
		format = formatArray[0];
		serverdecimal = formatArray[1];
		if(formatArray.length > 2)
			thousandseparator = formatArray[2];
	}
	//Identify the format's decimal character
	for (i=0; i < format.length; i++) {
		if ((format.charAt(i) != "-") && 
			(format.charAt(i) != "+") && 
			(isNaN(format.charAt(i)))) {
				userdecimal = format.charAt(i);				
		}
	}
	sTemp = sInput;
	//Replace the user decimal character with the server decimal character and return the result
	if(userdecimal!= serverdecimal)
		sTemp = Replace(sTemp, userdecimal, strTempSeparator);
	
	if(thousandseparator!="")
		sTemp = Replace(sTemp, thousandseparator, "");
	sTemp = Replace(sTemp, ",", "");
	sTemp = Replace(sTemp, " ", "");
	
	if(userdecimal!= serverdecimal)
		sTemp = Replace(sTemp, strTempSeparator, serverdecimal);
		
	return sTemp;
}


//----------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Number_Validate
//  Description:	Verifies that a given HTML form field contains a valid number
//  Arguments:		field - the name of the HTML form field containing the number 
//  Return Value:	True if the field contains an empty string
//					False if the field contains an invalid numeric value
//					A numeric variable if the field contains a valid number
//  Comments:		This function assumes that the form field in question has a
//					populated format attribute
//----------------------------------------------------------------

//DO NOT DELETE USED FOR INBOUND FORM FIELDS
function Number_Validate(field) {
	var sInput = "";
	var format = "";
	var formatArray = new Array;
	var separator = "";
	var decimal = "";
	var precision = "";
	var sign = "";
	var numdigits = "";
	var signed = false;
	var numberArray = new Array;
	var digitsLeft = 0;
	var digitsRight = 0;

	//If the input value is blank, return true
	sInput = field.value;
	if (sInput == "")
		return true;
	
	//Read in the field value and format strings
	if (field.getAttribute("NumberFormat")) {	
		format = field.getAttribute("NumberFormat");
	} else {
		format = field.getAttribute("format");
	}	
	
	//Assign the format value and decimal separator to be used
	formatArray = format.split("|");
	if (formatArray.length > 1) {
		format = formatArray[0];
		separator = formatArray[1];
	}
	
	//Identify the format's decimal character
	for (i=0; i < format.length; i++) {
		if ((format.charAt(i) != "-") && 
			(format.charAt(i) != "+") && 
			(isNaN(format.charAt(i)))) {
				decimal = format.charAt(i);
		}
	}
	
	//Determine the format's precision, sign and total number of digits allowed
	if (decimal != "") {
		formatArray = format.split(decimal);
		if (formatArray.length > 1) {
			precision = formatArray[1];
			format = formatArray[0];
		}
		if (format.charAt(0) == "-" || format.charAt(0) == "+") {
			sign = format.charAt(0);
			numdigits = format.substring(1, format.length);
		}
		else {
			numdigits = format;
		}
	}
	var sTemp = Number_Unformat(sInput, format);
	if(isNaN(sTemp)) // not a number
	{
		popMessage(JSResourceID512 + " " + sign + numdigits + decimal + precision, field);
		return false;
	}
	//The number may not violate the format's sign restriction
	if ((decimal != "") && ((sInput.charAt(0) == "-") && (sign == "+"))) 
	{
		popMessage(JSResourceID512 + " " + sign + numdigits + decimal + precision, field);
		return false;
	}
	if ((decimal != "") && ((sInput.charAt(0) != "-") && (sign == "-"))) 
	{
		popMessage(JSResourceID512 + " " + sign + numdigits + decimal + precision, field);
		return false;
	}

	//Determine the number of input digits on each side of the decimal character
	numberArray = sInput.split(decimal);
	if (numberArray.length > 1) 
	{
		digitsLeft = numberArray[0].length;
		digitsRight = numberArray[1].length;
	}
	else 
	{
		digitsLeft = numberArray[0].length;
		digitsRight = 0;
	}

	//Compensate for a possible leading sign character
	if ((sInput.charAt(0) == "-") || (sInput.charAt(0) == "+")) 
	{
		digitsLeft = eval(digitsLeft - 1);
	}

	//The number may not have more digits than are specified in the format
	if ((digitsLeft > numdigits) || (digitsRight > precision)) {
		popMessage(JSResourceID512 + " " + sign + numdigits + decimal + precision, field);
		return false;
	}
	return true;
}