﻿/************************************************************
*
*  TL Utilities.js
*
*  Utility functions and objects
************************************************************/

/**
	getElement(elem) finds the given element and returns it.
	elem can be either the id of the desired element, or the element itself
	
	Returns: the element found, or null if no element is found.
 */
function getElement(elem)
{
	if (typeof elem == "string")
	{
		elem = document.getElementById(elem)
	}
	
	return elem;
	
}

/**
	QueryString	holds information stored in the query string portion of the URL.
	The items are not integer indexed, they must be referred to by name.
*/
function QueryString()
{
	var _items = window.location.search.substring(1).split("&");
	this.Items = new Collection();
	for (var i = 0, len = _items.length; i<len; i++)
	{
		var Name = _items[i].split("=")[0];
		var Value = _items[i].split("=")[1];
		
		this.Items[Name] = Value;
	}
}

/**
	When assigned to an ancor element's click event, opens up the href in a popup window
*/
function PopWindowUp(e)
{
	var tgt = e.srcElement || e.target;
	window.open(tgt.href, "help", "directories=no,location=no,menubar=no,toolbar=no,width=780px,height=400px,scrollbars=yes");
	
	cancelEventDefault(e);
}

/**
 *	A simple object to which you can add arbitrary properties and it will tell
 *	you how many properties have been added to it.
 */
function Collection()
{
	this.count = function() {
		var i = 0;
		for (var item in this) 
		{
			i = i + 1
		}
		return i - 1;//don't count the 'count' property
	};
}

/*
	inserts a "$" at the beginning, rounds to two decimal places,
	and from the left, every 3 characters inserts a comma
	
	Psuedo code for comma insertion:
	FormattedNum = return value
	preComma = sNum % 3 = chars from begining before 1st comma
	
	1] if preComma > 0, take sNum.substring(0, preComma) and add that to FomrattedNum
	2] sNum = sNum.substr(preComma), storing the remaining digits in sNum
	3] Then, add a comma.
	4] While sNum.length > 0, repeat steps 5 & 6
	5] Append the next three digits to FormattedNum
	6] sNum = sNum.substr(3), storing the remaining digits in sNum
	7] if sNum.length > 0, append a comma to FormattedNum
*/
function FormatCurrency(sNum) {

    //Remove the Previous Formatting by this function
    sNum = sNum.replace(/[$]/g, "");
    sNum = sNum.replace(/,/g, "");
    sNum = sNum.replace(/[(]/, "-");
    sNum = sNum.replace(/[(]/g, "");
    sNum = sNum.replace(/[)]/g, "");
    
	if (isNaN(sNum))
	{
		return sNum
	}
	else
	{
	    sNum = parseFloat(sNum)
	}
	
	sNum = sNum.toFixed(2)
	//end mod by kevin
	
	sNum = sNum.toString()
	var negative = ""
	if (sNum.indexOf("-") >= 0)
	{
		negative = "-";
		sNum = sNum.substr(1);
	}
	var FormattedNum = ""; //return value
	
	var bDecimal = new Boolean(sNum.indexOf(".") > 0)
	var DecimalPart
	if (bDecimal == false) 
	{
		DecimalPart = ".00"
	}
	else 
	{
		DecimalPart = sNum.substr(sNum.indexOf("."))
		sNum = sNum.substring(0, sNum.indexOf("."))
	}
	if (DecimalPart.length < 2){DecimalPart = DecimalPart + "0"}
	
	var len = sNum.length;
	var preComma = 0; //chars from begining before 1st comma
	if (len > 2)
	{
		preComma = len % 3
	}
	
	var litComma = ","
	
	
	
	if (preComma > 0) 
	{
	/*1*/	FormattedNum = FormattedNum + sNum.substring(0, preComma)
	/*2*/	sNum = sNum.substr(preComma)
	/*3*/	FormattedNum = FormattedNum + litComma;
	}
	
	/*4*/while (sNum.length > 0)
	{
	/*5*/	FormattedNum = FormattedNum + sNum.substring(0, 3)
	/*6*/	sNum = sNum.substr(3);
	/*7*/	FormattedNum = (sNum.length > 0) ? FormattedNum + litComma : FormattedNum;
	}
	
	//
	//	Updated by sam: 4/13/06.
	//	Changing the negative format to ($123.45) rather than -$123.45
	//
	if (negative.length > 0)
	{
		FormattedNum = "($" + FormattedNum + DecimalPart + ")";
	}
	else
	{
		FormattedNum = "$" + FormattedNum + DecimalPart;
	}
	
	
	return FormattedNum
}


/**
	A simple implementation of Doug Crockford's purge method.
	http://www.crockford.com/javascript/memory/leak.html
	
	For IE 6, if an object has arbitrary methods defined on it,
	setting the object to null will not dispose of those methods,
	resulting in a memory leak.
	To complete the implementation, we will recursively call
	dispose on any attribute of type "object"
**/
function dispose(o)
{
	for (att in o)
	{
		if (typeof att == "function")
		{
			o[att] = null;
		}
//		else if (typeof att == "object")
//		{
//			dispose(att);
//		}
	}
	
}