tfx_utility = new Object();


tfx_utility.cancelEvent = function(evt)
{
	evt = tfx_utility.correctEvent(evt);
	
	if (evt)
	{
		evt.preventDefault();
	}
}

/*
	Clears the contents of the specified element.
*/
tfx_utility.clearContents = function(element)
{
	if (element)
	{
		while (element.firstChild)
		{
			element.removeChild(element.firstChild);
		}
	}
}

/*
	Corrects event handler implementation
*/
tfx_utility.correctEvent = function(evt)
{
	if (evt == null && window.event != null)
	{
		evt = window.event;
		evt.preventDefault = function()
		{
			this.cancelBubble = true;
			this.returnValue = false;
		}
	}
	
	return evt;
}


/*
	Creates an event handler callback function
*/
tfx_utility.createCallback = function(obj,method)
{
	return function()
	{
		obj[method](arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
	}
}


/*
	Finds the element with the specified ID
*/
tfx_utility.find = function(id)
{
	if (document.all) return document.all[id];
	else
	{
		if (document.getElementById) return document.getElementById(id);
		else return null;
	}
}

/*
	Gets the inner HTML content of an element.
*/
tfx_utility.getInnerHtml = function(id)
{
	var element = tfx_utility.find(id);
	var text = "";
	
	if (element)
	{
		text = element.innerHTML;
	}
	
	return text.replace(/<.*?>/gi, "");
}


/*
	Opens a popup window
*/
tfx_utility.popupWindow = function(url)
{
	window.open(url);
}


/*
	Sets the inner HTML content of an element.
*/
tfx_utility.setInnerHtml = function(id, html)
{
	var element = tfx_utility.find(id);
	
	if (element)
	{
		if (document.all)
		{
			var content = document.createElement("div");
			
			content.innerHTML = html;
			element.replaceChild(content, element.firstChild);
		}
		else
		{
			element.innerHTML = html;
		}
	}
}



/*
	Here are some date formatting extensions for convenience
*/
Date.DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Date.MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
Date.SUFFIXES = ["st","nd","rd","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","nd","rd","th","th","th","th","th","th","th","st"];

Date.fromXML = function(str)
{
	var regex = /([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/;
	
	if (regex.test(str))
	{
		var year = parseInt(RegExp.$1);
		var month = parseInt(RegExp.$2);
		var day = parseInt(RegExp.$3);
		var hour = parseInt(RegExp.$4);
		var minute = parseInt(RegExp.$5);
		var second = parseInt(RegExp.$6);
		
		return new Date(year, month, day, hour, minute, second, 0);
	}
	
	return new Date();
}

Date.prototype.format = function(mask)
{
	var count = 0;
	var formats = new Object();
	var formatted = (mask != null) ? mask : "DD-MMM-YY";
	var letters = "DMyHdhmst".split("");
	var regexA;
	var regexB = /\[(\d+)\]/;
	var temp = new Array();

	var day = this.getDay();
	var date = this.getDate();
	var month = this.getMonth();
	var year = this.getFullYear().toString();
	var hours = this.getHours();
	var minutes = this.getMinutes();
	var seconds = this.getSeconds();
	
	formats["D"] = date;
	formats["d"] = date + Date.SUFFIXES[date - 1];
	formats["DD"] = (date < 10) ? "0" + date : date;
	formats["DDD"] = Date.DAYS[day].substring(0, 3);
	formats["DDDD"] = Date.DAYS[day];
	formats["M"] = month + 1;
	formats["MM"] = (month + 1 < 10) ? "0" + (month + 1) : month + 1;
	formats["MMM"] = Date.MONTHS[month].substring(0, 3);
	formats["MMMM"] = Date.MONTHS[month];
	formats["y"] = (year.charAt(2) == "0") ? year.charAt(3) : year.substring(2, 4);
	formats["yy"] = year.substring(2, 4);
	formats["yyyy"] = year;
	formats["H"] = hours;
	formats["HH"] = (hours < 10) ? "0" + hours : hours;  
	formats["h"] = (hours > 12 || hours == 0) ? Math.abs(hours - 12) : hours;
	formats["hh"] = (formats["h"] < 10) ? "0" + formats["h"] : formats["h"];
	formats["m"] = minutes;
	formats["mm"] = (minutes < 10) ? "0" + minutes : minutes;
	formats["s"] = seconds;
	formats["ss"] = (seconds < 10) ? "0" + seconds : seconds;
	formats["t"] = (hours < 12) ? "A" : "P";
	formats["tt"] = (hours < 12) ? "AM" : "PM";

	for (var i = 0; i < letters.length; i++)
	{
		regexA = new RegExp("(" + letters[i] + "+)");

		while (regexA.test(formatted))
		{
			temp[count] = RegExp.$1;
			formatted = formatted.replace(RegExp.$1, "[" + count + "]");
			count++;
		}
	}

	while (regexB.test(formatted))
	{
		formatted = formatted.replace(regexB, formats[temp[RegExp.$1]]);
	}

	return formatted;
}

