
//IsNumeric -- PADD! Oct-10-2003
//	@sText - text to check
//
function isFloat(sText)
{
	var ValidChars = "-0123456789.,";
	var bVal;		
	if(!hasValidChars(sText, ValidChars))
		return false;
	if(isNaN(parseFloat(sText)))
		return false;
	return true;
}//isNumeric

//formatAmount - PADD! 19-Oct-03
//	@amt
//	This method will format the value passed and return
//	formatted result. It will only put .00 or .0 at the end
//	if needed
//
function formatAmount(amt)
{
	return formatAmountData(amt);
	//the followinf function was not working properly.
	//will not process further since the above code is returning 
	//proper format from another function
	//var strAmt = amt.toString();
	//strAmt = FormatCurrency(strAmt, 1);
	//return strAmt;
}//formatAmount

function GetFileNameWithExtension(fileName)
{
	var fileWithExt = fileName;
	//find which path delimeter is the last in the file name
	var index = fileName.lastIndexOf("\\");
	var index2 = fileName.lastIndexOf("/");
	if(index2 > index)
		index = index2;
	//if a name found, remove it
	if(index > 0)
		fileWithExt = fileWithExt.substring(index+1, fileWithExt.length);
	return fileWithExt;
}

//FormatAmount.js 
//
//	HTC Global Services
//
//	Following contains all the methods to format the amount value using comma , 

function GetDecimalDelimiter(nCountryCode)
{

	var sRet='';

	switch (nCountryCode)
	{
			case 3:   
						sRet = '#';
						break;
			case 2:   
						sRet = ',';
						break;
			default:
						sRet = '.';
						break;
	}
	return sRet;
}

function GetCommaDelimiter(nCountryCode)
{
	var sRet='';
	switch (nCountryCode)
	{
			case 3:   
						sRet = '*';
						break;
			case 2:   
						sRet = ',';
						break;
			default:
						sRet = ',';
						break;
	}
	return sRet;
}

function FormatClean(num)
{
	var sVal='';
	var nVal = num.length;
	var sChar='';
	    
	try
	{
		for(i=0;i<nVal;i++)
		{
			sChar = num.charAt(i);
			nChar = sChar.charCodeAt(0);
			if ((nChar >=48) && (nChar <=57))  { sVal += num.charAt(i);   }
		}
	}
	catch (exception) { AlertError("Format Clean",e); }
	return sVal;
}
	

function FormatCurrency(num,nCountryCode)
{       
		var sVal='';
		var minus='';
		var Decimal='';
		Decimal = GetDecimalDelimiter(nCountryCode);
		if (num.lastIndexOf("-") == 0) { minus='-'; }
		if (num.lastIndexOf(Decimal) < 0) { num = num + '00'; }
		num = FormatClean(num);
		
		sVal = minus + FormatDollar(num,GetCommaDelimiter(nCountryCode)) + GetDecimalDelimiter(nCountryCode) + FormatCents(num); 
		return sVal;
}

function FormatNumber(num,nCountryCode)
{       
		var sVal='';
		var minus='';
		var CommaDelimiter='';

		try 
		{

			CommaDelimiter = GetCommaDelimiter(nCountryCode);

			if (num.lastIndexOf("-") == 0) { minus='-'; }

			num = FormatClean(num);

			num = parseInt(num);

			var samount = new String(num);
		            
			for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
			{
				samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
			}

		}
		catch (exception) { AlertError("Format Number",e); }
		return minus + samount;
}

function FormatCents(amount)
{
	
	var cents = '';

	try
	{
		amount = parseInt(amount);
		var samount = new String(amount);

		if (samount.length == 0) { return '00'; }
		if (samount.length == 1) { return '0' + samount; }
		if (samount.length == 2) { return samount; }

		cents =  samount.substring(samount.length -2,samount.length);
	        
	}
	catch (exception) { AlertError("Format Cents",e); }
	return cents;
}

function FormatDollar(amount,CommaDelimiter)
{
	try 
	{
		
			amount = parseInt(amount);

			var samount = new String(amount);

			if (samount.length < 3) { return 0; }  

			samount =  samount.substring(0,samount.length -2);
		            
			for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
			{
			samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
			}

	}
	catch (exception) { AlertError("Format Comma",e); }
	return samount;
}

function AlertError(MethodName,e)
{
		if (e.description == null) { alert(MethodName + " Exception: " + e.message); }
		else {  alert(MethodName + " Exception: " + e.description); }
}

function formatAmountData(strValue)
{

	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '' + dblValue + '.' + strCents);
}
/*CK 06-25-2007 function does not work
function formatAmountDataTo3Dec(strValue)
{

	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*1000+0.50000000001);
	intCents = dblValue%1000;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/1000).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '' + dblValue + '.' + strCents);
}
*/
function formatDecimal(num, decplaces) { // JavaScript & DHTML Cookbook, by Danny Goodman.
    // convert in case it arrives as a string value
    num = parseFloat(num);
    // make sure it passes conversion
    if (!isNaN(num)) {
        // multiply value by 10 to the decplaces power;
        // round the result to the nearest integer;
        // convert the result to a string
        var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
        // exponent means value is too big or small for this routine
        if (str.indexOf("e") != -1) {
            return "Out of Range";
        }
        // if needed for small values, pad zeros
        // to the left of the number
        while (str.length <= decplaces) {
            str = "0" + str;
        }
        // calculate decimal point position
        var decpoint = str.length - decplaces;
        // assemble final result from: (a) the string up to the position of
        // the decimal point; (b) the decimal point; and (c) the balance
        // of the string. Return finished product.
        return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
    } else {
        return "NaN";
    }
}
