// --- FCFunctions.as --- //
/* 
---------------------------------------------------------------------------------
This file contains the base common utility functions associated with FusionCharts
All the functions contained in this file can be used with any other Flash 5/+ movie for common procedures
The base object in this file is _root.Functions
---------------------------------------------------------------------------------
Sample Usage of leftTrimString function: 
trace(_root.Functions.leftTrimString("   Spaces will be trimmed"," "));
---------------------------------------------------------------------------------
*/
_root.Functions = new Object();
Functions.leftTrimString = function(strValue, strCharToBeTrimmed) {
	// This function left trims a certain character from the specified string
	strString = new String(strValue);
	//If the specified character is present in the string, continue...
	if (strString.indexOf(strCharToBeTrimmed) != -1) {
		//Get the length of the string
		intLength = strString.length;
		//The funda : Get the position of the first charatcer which isn't the character to be trimmed
		//And, then extract the rest of the string from that point onwards till the end
		startCharPos = -1;
		for (i=0; i<=intLength; i++) {
			if ((strString.charAt(i) != strCharToBeTrimmed) && (startCharPos == -1)) {
				startCharPos = i;
			}
		}
		strString = strString.subString(startCharPos);
	}
	return strString;
};
Functions.getCorrectValue = function(primalValue, secondaryValue) {
	//This function is used to check the value of a variable to either primalValue or secondaryValue
	//The primalValue is returned, if it's not null, undefined or empty
	//Else the secondaryValue is returned
	strValueToBeRtn = primalValue;
	if (primalValue == null || primalValue == undefined || primalValue == "") {
		strValueToBeRtn = secondaryValue;
	}
	return strValueToBeRtn;
};
Functions.formatHexColor = function(sourceHexColor) {
	// This function formats a hex color code into the format required by FusionCharts
	// In FusionCharts, the hex color code is represented without any #
	//First remove the leading spaces if any - just a precautionary measure
	strHexColor = _root.Functions.leftTrimString(sourceHexColor, " ");
	//Now remove the leading # from the hex color code if any
	strHexColor = _root.Functions.leftTrimString(sourceHexColor, "#");
	return String(strHexColor);
};
Functions.getDarkColor = function(sourceHexColor, intensityRequired) {
	// This function gets a darker color for the specified object
	// Based on the intensity specified
	//First format the hexcolor to trim the # and leading spaces
	sourceHexColor = _root.Functions.formatHexColor(sourceHexColor);
	//Format the color in RGB notation
	sourceclrRGB = parseInt(sourceHexColor, 16);
	//Now, get the r,g,b values separated out of the specified color
	var r = Math.floor(sourceclrRGB/65536);
	var g = Math.floor((sourceclrRGB-r*65536)/256);
	var b = sourceclrRGB-r*65536-g*256;
	//Now, get the darker color based on the Intesity Specified
	darkColor = (r*intensityRequired) << 16 | (g*intensityRequired) << 8 | (b*intensityRequired);
	return(darkColor.toString(16));
};
Functions.formatNumber = function(nValue) {
	//This function formats the numerical value passed to it into 2 decimal places percentage format
	var shiftedValue = Math.round(Number(nValue)*100);
	//Get string conversion of shifted value (*100)
	var nS = String(shiftedValue);
	// If nS is only two character, concatenate one leading zero.
	nS = (nS.length<3) ? "0"+nS : nS;
	//get string length
	var sL = length(nS);
	//Now, get the numerical part and the decimal part
	var numeralPart = nS.substr(0, sL-2);
	var decimalPart = nS.substr(sL-2, 2);
	//If the numeralPart part doesn't contain 1 digit even..
	numeralPart = (numeralPart.length == 0) ? "0" : numeralPart;
	//Now, if the decimal part doesn't contain 2 digits append extra zeroes at the end
	decimalPart = (decimalPart.length == 0) ? "00" : decimalPart;
	decimalPart = (decimalPart.length == 1) ? decimalPart+"0" : decimalPart;
	//Return the concatenaated and formatted percentage value
	return (numeralPart+"."+decimalPart);
};
Functions.formatCommas = function(num)
{
	//This function adds proper commas to a number
	//For, the sake of ease, convert it to positive
	if (Number(num)<0)
	{
		num = num*-1;
		var negNum = true;
	}

	//First, convert the number to a string
	var strNum = num.toString();
	//Get the position of the decimal
	var dotPos = strNum.indexOf(".");
	//Variables to store the integer and decimal part separately
	var decNum = "";
	var finNum = "";
	// If the number contains a decimal, extract the decimal and the integral part
	if (dotPos != -1) {
		// Get the integer part
		var intNumber = strNum.substr(0, dotPos);
		// Get the decimal part
		decNum = strNum.substr(dotPos+1, strNum.length);
	} else {
		// Just set the integer part
		intNumber = strNum;
	}
	// If it's length is greater than 3, then format it
	if (intNumber.length>3) {
		// Get the length of the number
		var lenNumber = intNumber.length;
		for (var i = 0; i<=lenNumber; i++) {
			//Append proper commans
			if ((i>2) && ((i-1)%3 == 0)) {
				finNum = intNumber.charAt(lenNumber-i)+","+finnum;
			} else {
				finNum = intNumber.charAt(lenNumber-i)+finnum;
			}
		}
	} else {
		//If the number is less than or equal to 3 digits, no formatting is required
		finNum = strNum;
	}
	// Now, append the decimal part back
	if (dotPos != -1 && intNumber.length>3) {
		finNum = finNum+"."+decNum;
	}
	//Now, if neg num
	if (negNum==true){
		finNum= "-"+finNum;
	}	
	return finNum;
}