var _oXslNumProc = null;
var _oXmlNumber = null; 

function FormatInteger(vValue, iMinValue, iMaxValue, bGrouping, sFormat)
{
	try
	{	
		var iVal = StringToInt(vValue, sFormat);

		if (isNaN(iVal) || iVal < iMinValue || iVal > iMaxValue)
		{
			throw  "You must enter a whole number between "  + AddFormatting(iMinValue, 0, sFormat) + " and " + AddFormatting(iMaxValue, 0, sFormat) + ".";
		}
						
		return AddFormatting(iVal, 0, sFormat);
	}
	catch(e)
	{		
		if ("string" == typeof(e))
		{
			alert(e);
		}
		else if (e.message)
		{
			alert(e.message);
		}

		return "";
	}
}


function FormatFloat(vValue, iMinValue, iMaxValue, bGrouping, sFormat, iAccuracy)
{
	try
	{	
		var iVal = StringToFloat(vValue, sFormat);		
				
		if ( isNaN(iVal) || (iVal < iMinValue) || (iVal > iMaxValue) ) 
		{
			throw "You must enter a number between " + AddFormatting(iMinValue, iAccuracy, sFormat) + " and " + AddFormatting(iMaxValue, iAccuracy, sFormat) + ".";
		}
	
		return AddFormatting(iVal, iAccuracy, sFormat);
	}
	catch(e)
	{
		if ("string" == typeof(e))
		{
			alert(e);
		}
		else if (e.message)
		{
			alert(e.message);
		}

		return "";
	}
}

function StringToInt(sValue, sFormat)
{
	if (!sFormat)
	{
		sFormat = ORG_NUMBER_FORMAT;
	}
	try
	{
		return parseInt(RemoveFormatting(sValue, sFormat), 10);
	}
	catch (e)
	{
		return NaN;
	}	
}

function StringToFloat(sValue, sFormat)
{
	if (!sFormat)
	{
		sFormat = ORG_NUMBER_FORMAT;
	}	
	try
	{
		return parseFloat(RemoveFormatting(sValue, sFormat));
	}
	catch (e)
	{
		return NaN;
	}		
}

// Formats the number according to the locale settings
function AddFormatting(vValue, iAccuracy, sFormat, bNoTrailingZeros) 
{	
	try
	{
		if (!_oXslNumProc || !_oXmlNumber)
		{
			var oXslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
			oXslDoc.async = false;
			
			oXslDoc.load(fmCommonPath + "UI/number/formatNumber.xsl");
				
			var oXslt = new ActiveXObject("Msxml2.XSLTemplate");
			oXslt.stylesheet = oXslDoc;		
			
			_oXmlNumber = new ActiveXObject("Microsoft.XMLDOM");
			_oXmlNumber.async = false;				
			
			_oXslNumProc = oXslt.createProcessor();
			_oXslNumProc.addParameter("numberFormat", sFormat);				
		}
		
		var sAccuracy = bNoTrailingZeros ? "##########" : "0000000000";
		
		vValue = new String(vValue);
		
		var iIndex = vValue.indexOf('.');
		if (iIndex > -1)
		{
			vValue = vValue.substring(0, iIndex + iAccuracy + 1);
		}
		
		var sPattern = ORG_NUMBER_FORMAT_MASK.replace(/\*/g, sAccuracy.substr(0, iAccuracy));		
				
		_oXslNumProc.addParameter("pattern", sPattern);
		
		_oXmlNumber.loadXML("<number>" + vValue + "</number>");
		_oXslNumProc.input = _oXmlNumber;			
		
		_oXslNumProc.transform();

		return _oXslNumProc.output;
	}
	catch  (e)
	{
		return "";
	}
}

// This function translates a number (in a string form) from any local format to the standard US format
// The result of this function can be used in parseInt/Float JS functions safely
// The function throws an error of the vValue is not a number so make sure to use try catch when calling the function
function RemoveFormatting(vValue, sFormat)
{		
	var sVal = new String(vValue);	
	var sFormatError = "You must enter a valid number."
	
	if (null == sVal || "" == sVal) return sVal;
	
	// If '.' is neither decimal nor grouping separator (like in French locale) it is not allowed in the input 
	if ("." != ORG_NUMBER_GROUP_SEPARATOR && "." != ORG_NUMBER_DECIMAL_SEPARATOR && sVal.indexOf(".") != -1)
	{
		throw sFormatError;
	}

	sVal = sVal.replace(/\s*/g, "");
	sVal = sVal.replace(/\xA0*/g, ""); // replace non-breakable spaces

	sVal = sVal.replace(new RegExp("\\" + ORG_NUMBER_GROUP_SEPARATOR, "g"), "");
	sVal = sVal.replace(new RegExp("\\" + ORG_NUMBER_DECIMAL_SEPARATOR, "g"), ".");	
	
	if ("" == sVal) return sVal;
		
	// Add a new variable and assign sVal.length - 1 to it
	var iIndex = sVal.length - 1;
	if ('(' == sVal.charAt(0) && ')' == sVal.charAt(iIndex))
	{
		sVal = "-" + sVal.substring(1, iIndex);		
	}
	else if ('-' == sVal.charAt(iIndex))
	{
		sVal = "-" + sVal.substring(0, iIndex);		
	}
		
	if (null == sVal.match(/^-?((\d+\.?\d*)|(\.\d+))$/))
	{
		throw  sFormatError;
	}
		
	return sVal;
}
