<!-- ----------------------------------------------------------------------------------
//
// Description:	Contains various formating functions.
//
//--------------------------------------------------------------------------------- -->

//-------------------------------------------------------------------------------------
// Function:	FormatCurrency
//
// Synopsis:	Formats a value to U.S. currency.
//
// Arguments:	sValue	Value to be formated.
//
// Returns:		U.S. currency.
//
// Notes:		none
//
//-------------------------------------------------------------------------------------
function FormatCurrency(sValue)
{
	// Make sure input is a string:
	sValue += "";

	// Keep original copy of input string:
	var sOriginalValue = sValue;

	// Replace any existing format characters.
	sValue = sValue.replace(/$/g, "");
	sValue = sValue.replace(/,/g, "");
	
	// Get float value:
	var fAmount = parseFloat(sValue);
	var bNeg = (fAmount < 0) ? true : false;
	
	fAmount = Math.abs(fAmount);

	// Return unmodified input if we weren't able to convert it:
	if (isNaN(fAmount)) return sOriginalValue;

	// Express amount in pennies, rounded to the nearest penny:
	fAmount = Math.round(100 * fAmount);

	// Prepare to add a US currency prefix:
	var sPrefix = "$";

	// Convert amount to string and pad with leading zeros if necessary:
	var sCurrency;
	if (fAmount < 10)
        sCurrency = "00" + fAmount;
	else if (fAmount < 100)
        sCurrency = "0" + fAmount;
	else
        sCurrency = "" + fAmount;

	// Insert prefix:
	sCurrency = sPrefix + sCurrency;

	// Insert decimal point before last two digits:
	sCurrency = sCurrency.substring(0, sCurrency.length - 2) + "." + sCurrency.substring(sCurrency.length - 2, sCurrency.length);

	// Insert commas.
	iCommas = parseInt((sCurrency.length - 5) / 3);
	for (i=1;i<=iCommas;i++) {
		// Calcuate position of comma.
		p = ((i - 1) * 4) + 6;
		sCurrency = sCurrency.substring(0, sCurrency.length - p) + "," + sCurrency.substring(sCurrency.length - p, sCurrency.length);
	}

	if (bNeg == true) sCurrency = "-" + sCurrency;

	// Return formatted currency.
	return sCurrency;
}

//-------------------------------------------------------------------------------------
// Formats a SQL date.
//-------------------------------------------------------------------------------------
function FormatDate(sDate, bAddDayOfWeek, bLongTime) {

    if (sDate == null) {
        sDate = "";
    }
    else {
        sDate = (bLongTime) ? sDate : sDate.substr(0,19);
        sDate = sDate.replace("T", " ");
        
        if (bAddDayOfWeek) sDate += " " + moMain.VBScript_GetDayOfWeek(sDate);
    }
    
    return sDate;
}

//-------------------------------------------------------------------------------------
// Function:	FormatMeasure
//
// Synopsis:	Formats a OLAP measure to have commas and desired digits after decimal.
//
// Arguments:	sMeasure Desired format.
//					sValue	Value to be formated.
//
// Returns:		Number formatted with comma and number of digits after decimal.
//
// Notes:		none
//
//-------------------------------------------------------------------------------------
function FormatMeasure(sMeasureFormat, sValue) {

	switch(sMeasureFormat) {
		case "COMMA":
			sValue = FormatNumber(sValue, 0);
			break;
		case "COMMA1":
			sValue = FormatNumber(sValue, 1);
			break;
		case "COMMA2":
			sValue = FormatNumber(sValue, 2);
			break;
		case "DOLLAR":
			sValue = "$" + FormatNumber(sValue, 0);
			break;
		case "DOLLAR2":
			sValue = "$" + FormatNumber(sValue, 2);
			break;
		default:
			sValue = FormatNumber(sValue, 0);
			break;	
	}
	
	return sValue;
}

//-------------------------------------------------------------------------------------
// Function:	FormatNumber
//
// Synopsis:	Formats a number to have commas and desired digits after decimal.
//
// Arguments:	sValue						Value to be formated.
//					iNumDigitsAfterDecimal	Number of digits to have after decimal.			
//
// Returns:		Number formatted with comma and number of digits after decimal.
//
// Notes:		none
//
//-------------------------------------------------------------------------------------
function FormatNumber(sValue, iNumDigitsAfterDecimal)
{
	// Keep original copy of input string:
	var sOriginalValue = sValue;

	// Make sure input is a string:
	sValue += "";	

	// Replace any existing format characters.
	sValue = sValue.replace(/,/g, "");

	// Get float value:
	var fAmount = parseFloat(sValue);

	// Return input if we weren't able to convert it.
	if (isNaN(fAmount)) return sOriginalValue;

	var bNeg = (fAmount < 0) ? true : false;
	if (bNeg) fAmount = Math.abs(fAmount);
	
	// Insert decimal point.
	sValue = fAmount.toFixed(iNumDigitsAfterDecimal);
	
	if (iNumDigitsAfterDecimal == 0) sValue += ".";

	// Insert commas.
	var iCommas = parseInt((sValue.length - (iNumDigitsAfterDecimal + 2)) / 3);
	for (i=1;i<=iCommas;i++) {
		// Calcuate position of comma.
		p = ((i - 1) * 4) + 4 + iNumDigitsAfterDecimal;
		sValue = sValue.substring(0, sValue.length - p) + "," + sValue.substring(sValue.length - p, sValue.length);
	}
	
	if (bNeg == true) {
		if (sValue.substring(0, 1) != "-") sValue = "-" + sValue;
	}	

	// Parse off decimal.
	if (iNumDigitsAfterDecimal == 0) sValue = sValue.substring(0, sValue.length - 1)

	// Return formatted number.
	return sValue;
}
