
function NewImage(arg)
{
	if (document.images)
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function ChangeImage()
{
	if (document.images && (preloadFlag == true))
	{
		for (var i=0; i<ChangeImage.arguments.length; i+=2)
		{
			document[ChangeImage.arguments[i]].src = ChangeImage.arguments[i+1];
		}
	}
}

function FlashManager(strFile, intWidth, intHeight, strBackgroundColour)
{
	var window_mode = 'transparent' //set this to transparent to make flash movie sit in the document order correctly

	document.write('<object\n');
	document.write('id="Rokgsa"\n');
	document.write('classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\n');
	document.write('codebase="'+location.protocol+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"\n');
	document.write('width="'+intWidth+'"\n');
	document.write('height="'+intHeight+'"\n');
	document.write('align="center">\n');
	document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
	document.write('<param name="movie" value="'+strFile+'" />\n');
	document.write('<param name="quality" value="high" />\n');
	document.write('<param name="bgcolor" value="'+strBackgroundColour+'" />\n');
	document.write('<param name="wmode" value="'+window_mode+'" />\n');
	document.write('<embed\n');
	document.write('src="'+strFile+'"\n');
	document.write('quality="high"\n');
	document.write('bgcolor="'+strBackgroundColour+'"\n');
	document.write('width="'+intWidth+'"\n');
	document.write('height="'+intHeight+'"\n');
	document.write('name="rokgsa"\n');
	document.write('align="left"\n');
	document.write('wmode="'+window_mode+'"\n');
	document.write('allowScriptAccess="sameDomain"\n');
	document.write('type="application/x-shockwave-flash"\n');
	document.write('pluginspage="'+location.protocol+'//www.macromedia.com/go/getflashplayer"\n');
	document.write('/>\n');
	document.write('</object>\n');
}

function ConfirmVerifyDelete(strInfo, strPleaseConfirmText, strOkToDelete)
{
	var strUsr = strPleaseConfirmText;
	strUsr += "\n\n";
	strUsr += strOkToDelete;
	strUsr += ": ";
	strUsr += "\n\n";
	strUsr += strInfo
	return confirm(strUsr);
}

// var objWidth = new Object();
// objWidth["stringValue"] = "mystring";

//
// Creates an editable row
// Implementation:
// Ensure the table is nested inside a form tag and
// ...each td tag has a width setting
// The edit field must have an id of 'updateurl'
// The delete field must have an id of 'deleteurl'
// arg 1: The name/id of the form
// arg 2: The name/id of the database id
// arg 3: The id value of this particularly row
// arg 4: csv list of fields needing text area
// arg 5: the number of rows needed for the text field
//
var blnEdit = false;
function EditRow(strIdFieldName, intIdValue, csvTextAreaList, intTextAreaRows, strRequiredQueryStringItem)
{
	if (blnEdit)
	{
		alert(_TXT1); // Another row already being edited
		return false;
	}
	else
	{
		blnEdit = true;
	}

	// Create array of the fields needing text areas, otherwise defauts to normal text field
	arrTextAreaList = csvTextAreaList.split(",");

	// Get the row
	var objRow = document.getElementById(intIdValue);
	var rowId = null;

	// Get cells from the row
	if (objRow)
	{
		var objCell = objRow.getElementsByTagName("td");
		var strOutput = "";

		for(var i = 0; i < objCell.length; i++)
		{
			var arrTmp = objCell[i].id.split("-");
			var strFieldName = arrTmp[0];

			// Sort out the columns
			if (strFieldName == null || strFieldName == "") continue;
			else if(strFieldName == strIdFieldName) // The hidden non-editable field with the row id
			{
				objCell[i].innerHTML = objCell[i].innerHTML+' <input type="hidden" name="'+strFieldName+'" value="'+objCell[i].innerHTML+'" />';
			}
			else if(strFieldName == "deleteurl") // The delete cell, converted to a cancel button
			{
				objCell[i].innerHTML = '<a href="'+location.pathname+'?'+strRequiredQueryStringItem+'">'+_TXT2+'</a>'; // Cancel
				// objCell[i].innerHTML = '<a href="'+location.pathname+location.search+'">'+_TXT2+'</a>';
			}
			else if(strFieldName == "updateurl") // The edit cell, converted to save button
			{
				objCell[i].innerHTML = '<input type="submit" value="'+_TXT3+'" style="width:'+(objCell[i].width-6)+'px;margin:0;cursor:pointer" />'; // Save
			}
			else if (InArray(strFieldName, arrTextAreaList)) // an editable field. uses text area
			{
				if (intTextAreaRows == undefined) intTextAreaRows = 3;
				objCell[i].innerHTML = '<textarea name="'+strFieldName+'" rows="'+intTextAreaRows+'" style="width:'+(objCell[i].width-8)+'px;">'+objCell[i].innerHTML+'</textarea>';
			}
			else // an editable field
			{
				var objCellChild = objCell[i].getElementsByTagName("Select");
				if (objCellChild.length > 0) objCellChild[0].disabled = false; // Only concerned with first element
				else objCell[i].innerHTML = '<input type="text" name="'+strFieldName+'" value="'+objCell[i].innerHTML+'" style="width:'+(objCell[i].width-8)+'px;" />';
			}
		}

		// For debugging
		// alert(strOutput);
	}
	return false;
}

// Typical usage
// var messages = new makeArray(COMMA SEPARATED LIST AS ARGUMENTS HERE);
function CsvToArray()
{
	arrTmp = new Array();
	for (var i = 0; i < CsvToArray.arguments.length; i++)
		arrTmp[i] = CsvToArray.arguments[i];
	return arrTmp;
}

function InArray(strSource, arrTarget)
{
	if (strSource == "") return false;
	else if (arrTarget.length == 0) return false;

	for (var i = 0; i < arrTarget.length; i++)
	{
		if (arrTarget[i] == undefined) continue;
	  	else if (arrTarget[i] == strSource) return true;
	}
	return false;
}

function DropDownGoTo(strName, selObj)
{
	var strDestination = location.pathname + "?"+strName+"="+selObj.options[selObj.selectedIndex].value;
	location.href = strDestination.substr(1); // Remove forward slash
}

function OpenWin(theURL,winName, wWidth, wHeight, withScrollbars, withResizeable, horzPos, vertPos, boolSetCookie)
{
	winHandle = '';
	vAdjustment = -50;
	if (horzPos == "" && vertPos == "")
	{
		if (screen)
		{
			horzPos = Math.ceil((screen.width - wWidth)/2);
			vertPos = Math.ceil((screen.height - wHeight)/2) + vAdjustment;
		}
		else
		{
			horzPos = 0;
			vertPos = 0;
		}
	}
	winChild = window.open(theURL, winName,'toolbar=no,location=no,scrollbars='+withScrollbars+',resizable='+withResizeable+',width='+wWidth+',height='+wHeight+', left='+horzPos+', top='+vertPos+'');
	setTimeout("winChild.focus()", 500);
	if (boolSetCookie) SetCookie("childWinUrl", theURL, 24);
}

function ShowHideObj(linkObj, objId, strShowText, strHideText)
{
	var objItem = document.getElementById(objId);

	if (objItem.style.display == "none")
	{
		objItem.style.display = "block";
		linkObj.innerHTML = strHideText;
	}
	else
	{
		objItem.style.display = "none";
		linkObj.innerHTML = strShowText;
	}

	return false;
}

function GoTo(strLocation)
{
	location.href = strLocation;
	return false;
}

//
// 1, Used for manage the "on" state on menus
// 2. You can set the variable "strOverrideUrl" in the master_template.html through PHP
// ...or allow it to locate the url itself
// 3. Remember to set class="" on the anchor tags so that it's available to be reset
//
function ManageCss(strObName, strNewClassName, strOverrideUrl)
{
	var objMenu = document.getElementById(strObName);
	if(objMenu != null)
	{
		var strCurrUri = window.location.pathname.toLowerCase().replace("/", "");

		// Check for parent menu to override
		// ...set in head of master_template.html
		if (strOverrideUrl != "")
		{
			if (strOverrideUrl != strCurrUri) strCurrUri = strOverrideUrl;
		}

		var arrA = objMenu.getElementsByTagName("a");
		var strAnchorHref = null;
		for(var i=0; i< arrA.length;i++)
		{
			strAnchorHref = arrA[i].href.toLowerCase().replace("//", "").replace("/", "").replace(window.location.hostname, "").replace(window.location.protocol, "");
			// alert(strAnchorHref +" : " + strCurrUri); // For testing
			if (strAnchorHref == strCurrUri)
			{
				arrA[i].className = strNewClassName;
			}
		}
	}
}
/*
function InArray(strValue, arrArray)
{
	for(i = 0; i < arrArray.length; i++)
	{
		if (strValue == arrArray[i]) return true;
	}
	return false;
}*/

function AddSlashes(str)
{
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function StripSlashes(str)
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

//
// Typical Usage
//
/*
var page = new PageQuery(location.search);
var myValue = page.getValue("a");
if (myValue == "-1") myValue = "history.html";
*/
function PageQuery(q)
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q)
	{
		for(var i=0; i < this.q.split("&").length; i++)
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return -1;
	}
	this.getParameters = function()
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
     this.getLength = function() { return this.keyValuePairs.length; }
}

function CheckLeapYear(datea)
{
	datea = parseInt(datea);

	if(datea%4 == 0)
	{
		if(datea%100 != 0)
		{
			return true;
		}
		else
		{
			if(datea%400 == 0)
				return true;
			else
				return false;
		}
	}
	return false;
}

function CheckUserMsg()
{
	setTimeout('CloseUserMsg()', 3000);
	return false;
}


function CloseUserMsg()
{
	var objUserMsg = document.getElementById("userMsg");
	if (objUserMsg != null)
	{
		if (objUserMsg.innerHTML != "")
		{
			objUserMsg.innerHTML = "";
			objUserMsg.style["display"] = "none";
		}
	}
	return false;
}


Date.prototype.getMonthName = function() {
var m = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
return m[this.getMonth()];
}
Date.prototype.getDayName = function() {
var d = ['Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'];
return d[this.getDay()];
}


function NonAcceptableChars(strString)
//  check for unacceptable characters
{
	// strValidChars copntains list of unacceptable characters
	var strValidChars = " ";
	var strChar;
	var blnResult = true;

	//  test strString consists of unacceptable characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == 0)
	 	{
	 		blnResult = false;
	 	}
  }
	return blnResult;
}

function IsNumeric(strString)
//  check for valid numeric strings
{
	var strValidChars = "0123456789 ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
	 	{
	 		blnResult = false;
	 	}
  }
	return blnResult;
}

function updateLocation(location, value){

	var cururl = document.location.toString();

	if(value == ''){
		return false;
	}

	var url_array=cururl.split("/");


	//recreate the uri
	var newurl = '';

	for (i=0; i<url_array.length-1; i++){

		if (i != url_array.length - 2){
			newurl = newurl + url_array[i] + '/'
		}
		else{
			newurl = newurl + location + '/'
		}

	}

	newurl = newurl + url_array[url_array.length-1];

	//remove funny characters and lowercase
	newurl = newurl.toLowerCase();
	newurl = encodeURI(newurl)

	document.location = newurl;

}

