//===============================================================
// Function :	get
//
// Purpose : 	Shorthand replacement for document.getElementById().
//
// Parameters:
//    sID				ID of the element
//		bFailSilently	(optional) if true, will not bring up an
//							alert box informing the user of the error
//
// Returns: 	A handle to the element
//===============================================================
function get(sID, bFailSilently) {
	if (bFailSilently == null)
		bFailSilently = false;
	var oElement = document.getElementById(sID);
	if (null == oElement) {
		if (!bFailSilently)
			alert("Element '" + sID + "' does not exist in this page");
		return null;
	}
	return oElement;
}

//===============================================================
// Function :	setElementRollover
//
// Purpose : 	Sets the rollover, rollout and onclick events for
//					the specified element tags and class names
//
// Parameters:
//		sElementTag			The element tags to affect
//		sClass				The class name of the elements to affect
//		sRolloverClass		The rollover class name of the affected elements
//		bClickFirstLink	(Optional) True if the first link should be clicked
//								in the onclick event. Defaults to false
//
// Returns: None
//===============================================================
function setElementRollover(sElementTag, sClass, sRolloverClass, bClickFirstLink) {
	
	if (bClickFirstLink == null)
		bClickFirstLink = false;
	
	var oElements = document.getElementsByTagName(sElementTag);
	for (var i = 0; i < oElements.length; i++) {
	
		if (oElements[i].className == sClass) {
		
			oElements[i].onmouseover = function() { this.className = sRolloverClass; }
			oElements[i].onmouseout = function() { this.className = sClass; }
			
			if (bClickFirstLink)
				oElements[i].onclick = function() { this.getElementsByTagName("A")[0].click(); }
		}
	}
}

//===============================================================
// Function : setBygTimeout
//
// Purpose : Executes timeout code, with arguments.
//
// Parameters:
//    sCode           Code to be executed. Can contain argument references $1, $2 etc.
//    nMilliseconds   Number of milliseconds to wait.
//    sLanguage       Language of sCode.
//    vArgument1, vArgument2 ... vArgumentN
//        Arguments for sCode.
//
// Returns: None
//===============================================================
var oBygTimeouts = new Array();
function setBygTimeout() {

	var sCode = arguments[0];
	var nMilliseconds = arguments[1];
	var sLanguage = arguments[2];
	var oBygTimeout = new Object;
	oBygTimeout.oArguments = new Array();
	var nBygTimeout = oBygTimeouts.length;
	oBygTimeouts[nBygTimeout] = oBygTimeout;
	for (var nArgument = 3; nArgument < arguments.length; nArgument++) {
		var vArgument = arguments[nArgument];
		var nBygTimeoutArgument = oBygTimeout.oArguments.length;
		oBygTimeout.oArguments[nBygTimeoutArgument] = vArgument;
		var sArgumentFind = "\\$" + (nArgument - 2);
		var sArgumentReplace = "oBygTimeouts[" + nBygTimeout + "].oArguments[" + nBygTimeoutArgument + "]";
		sCode = sCode.replace(new RegExp(sArgumentFind, "gi"), sArgumentReplace);
	}
	oBygTimeout.sCode = sCode;
	setTimeout("executeBygTimeout(" + nBygTimeout + ")", nMilliseconds, sLanguage);
}
function executeBygTimeout(nBygTimeout) {
	var oBygTimeout = oBygTimeouts[nBygTimeout];
	var sCode = oBygTimeout.sCode;
	eval(sCode);
	oBygTimeouts[nBygTimeout] = null;
}


//===============================================================
// Function : insertText
//
// Purpose : Inserts text into the supplied placeholder
//
// Parameters:
//		sPlaceholderID			ID of the element to contain the graphic
//		sText						The Text
//
// Returns: None
//===============================================================
function insertText(sPlaceholderID, sText) {
	get(sPlaceholderID).innerHTML = processCustomTags(sText);
}

//===============================================================
// Function : insertGraphic
//
// Purpose : Inserts an Image or Flash movie into the supplied placeholder
//
// Parameters:
//		sPlaceholderID			ID of the element to contain the graphic
//		sID						ID for element (can be null)
//		sGraphic					Image/Flash source
//		sGraphicAltText		Alternative text (can be null)
//		nGraphicWidth			Width (can be null)
//		nGraphicHeight			Height (can be null)
//		sOnLoadEvent			onload event script (can be null)
//
// Returns: None
//===============================================================
function insertGraphic(sPlaceholderID, sID, sGraphic, sGraphicAltText, nGraphicWidth, nGraphicHeight, sOnLoadEvent) {

	// Catch missing or invalid placeholder IDs
	if (null == sPlaceholderID) {
		alert("You must specify a Placeholder ID for the graphic: " + sGraphic);
		return;
	}
	if (null == get(sPlaceholderID)) {
		alert("The Placeholder ID: " + sPlaceholderID + " does not exist on the page, or has not yet loaded");
		return;
	}
	
	if (null == sID) sID = "";
	if (null == sGraphicAltText) sGraphicAltText = "";
	if (null == sOnLoadEvent) sOnLoadEvent = "";
	
	var sGraphicHtml = "";
	var DOT_SWF = ".swf";
	
	var oFlashFileRegExp = new RegExp(".*\\.swf(\\?.*)?$", "gi");
	
	// If the graphic is a flash movie
	if (oFlashFileRegExp.test(sGraphic)) {

		sGraphicHtml
			= "<embed"
			+ " id='" + sID + "'"
			+ " name='" + sID + "'"
			+ " src='" + sGraphic + "'"
			+ " width='" + nGraphicWidth + "'"
			+ " height='" + nGraphicHeight + "'"
			+ " quality='high'"
			+ " wmode='transparent'"
			+ " pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'"
			+ " type='application/x-shockwave-flash'"
			+ " swliveconnect='true'"
			+ ">"
			+ "</embed>";

	// Otherwise, the graphic is an image
	} else {

		sGraphicHtml 
			= "<img"
			+ " id='" + sID + "'"
			+ " src='" + sGraphic + "'"
			+ " alt='" + sGraphicAltText + "'"
			+ " onload='" + sOnLoadEvent + "'";

		if (nGraphicWidth != null)
			sGraphicHtml += " width='" + nGraphicWidth + "'";
		if (nGraphicHeight != null)
			sGraphicHtml += " height='" + nGraphicHeight + "'";

		sGraphicHtml += " />";
	}
	
	get(sPlaceholderID).innerHTML = sGraphicHtml;
}

//===============================================================
// Function:	onRolloverImage
//
// Purpose:		Handles the rollover state for images
//
// Parameters:
//		oImg		The IMG element
//
// Returns:		Nothing
//===============================================================
function onRolloverImage(oImg) {
	var sImageUrl = oImg.src;
	var sImageExtension = sImageUrl.split(".")[sImageUrl.split(".").length - 1];
	var sImageUrlPrefix = sImageUrl.substring(0, (sImageUrl.length - 6));
	if (sImageUrl.indexOf("_c." + sImageExtension) == -1) {
		oImg.src = sImageUrlPrefix + "_b." + sImageExtension;
	}
}

//===============================================================
// Function:	onRolloutImage
//
// Purpose:		Handles the rollout state for images
//
// Parameters:
//		oImg		The IMG element
//
// Returns:		Nothing
//===============================================================
function onRolloutImage(oImg) {
	var sImageUrl = oImg.src;
	var sImageExtension = sImageUrl.split(".")[sImageUrl.split(".").length - 1];
	var sImageUrlPrefix = sImageUrl.substring(0, (sImageUrl.length - 6));
	if (sImageUrl.indexOf("_c." + sImageExtension) == -1) {
		oImg.src = sImageUrlPrefix + "_a." + sImageExtension;
	}
}

//===============================================================
// Function:	getMd5Hash
//
// Purpose:		Returns a hashed string from the supplied string
//
// Parameters:
//		sString	The string to hash
//
// Returns:		String
//===============================================================
function getMd5Hash(sString) {

	return hex_md5(sString);
}


//===============================================================
// Function :	formatDateString
//
// Purpose :	Returns a formatted date string representing the
//					supplied Date Object in the supplied format
//
// Parameters:
//		oDate				The date to format
//		sDateFormat		(Optional) The date format string
//							Defaults to DD/MM/YYYY
//
// Returns: 	String
//===============================================================
function formatDateString(oDate, sDateFormat) {
	
	if (oDate == null)
		return null;
	
	var oDayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	var oMonthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	
	if (sDateFormat == null)
		sDateFormat = "dd/mm/yyyy";
	else
		sDateFormat = sDateFormat.toLowerCase();
	
	var sValue_wwww = oDayNames[oDate.getDay()];
	var sValue_www  = sValue_wwww.substr(0, 3);
	var sValue_d    = oDate.getDate();
	var sValue_dd   = (sValue_d < 10) ? "0" + sValue_d : sValue_d;
	var sValue_m    = oDate.getMonth() + 1;
	var sValue_mm   = (sValue_m < 10) ? "0" + sValue_m : sValue_m;
	var sValue_mmmm = oMonthNames[oDate.getMonth()];
	var sValue_mmm  = sValue_mmmm.substr(0, 3);
	var sValue_yyyy = oDate.getFullYear() + "";
	var sValue_yy   = sValue_yyyy.substr(2, 2);

	if (sDateFormat.indexOf("wwww") > -1)
		sDateFormat = sDateFormat.replace("wwww", "sValue_wwww");
	else if (sDateFormat.indexOf("www") > -1)
		sDateFormat = sDateFormat.replace("www", "sValue_www");

	if (sDateFormat.indexOf("dd") > -1)
		sDateFormat = sDateFormat.replace("dd", "sValue_dd");
	else if (sDateFormat.indexOf("d") > -1)
		sDateFormat = sDateFormat.replace("d", "sValue_d");

	if (sDateFormat.indexOf("mmmm") > -1)
		sDateFormat = sDateFormat.replace("mmmm", "sValue_mmmm");
	else if (sDateFormat.indexOf("mmm") > -1)
		sDateFormat = sDateFormat.replace("mmm", "sValue_mmm");
	else if (sDateFormat.indexOf("mm") > -1)
		sDateFormat = sDateFormat.replace("mm", "sValue_mm");
	else if (sDateFormat.indexOf("mm") > -1)
		sDateFormat = sDateFormat.replace("m", "sValue_m");

	if (sDateFormat.indexOf("yyyy") > -1)
		sDateFormat = sDateFormat.replace("yyyy", "sValue_yyyy");
	else if (sDateFormat.indexOf("yy") > -1)
		sDateFormat = sDateFormat.replace("yy", "sValue_yy");

	sDateFormat = sDateFormat.replace("sValue_wwww", sValue_wwww);
	sDateFormat = sDateFormat.replace("sValue_ww", sValue_www);
	sDateFormat = sDateFormat.replace("sValue_dd", sValue_dd);
	sDateFormat = sDateFormat.replace("sValue_d", sValue_d);
	sDateFormat = sDateFormat.replace("sValue_mmmm", sValue_mmmm);
	sDateFormat = sDateFormat.replace("sValue_mmm", sValue_mmm);
	sDateFormat = sDateFormat.replace("sValue_mm", sValue_mm);
	sDateFormat = sDateFormat.replace("sValue_m", sValue_m);
	sDateFormat = sDateFormat.replace("sValue_yyyy", sValue_yyyy);
	sDateFormat = sDateFormat.replace("sValue_yy", sValue_yy);

	return sDateFormat;
}
