
function GetCookie(cookieName)
{
	if (document.cookie != "")
	{
		cookieArray = document.cookie.split("; ");
		for(i = 0; i < cookieArray.length; i++)
		{
			if (cookieArray[i].split("=")[0] == cookieName)
				return cookieArray[i].split("=")[1];
		}
	}
	return "";
}

// Leave off the third argument completely to set browser cookie
function SetCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + "; path = /";
}

function FormatCurrency(strValue, strCurrencySymbol)
{
	strValue = strValue.toString().replace(/\$|\,|£/g, "");
	if (isNaN(strValue)) strValue = 0;
	else if (strValue == "") strValue = 0;
	dblValue = parseFloat(strValue);
	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents < 10) strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
	dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
	dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}

function ValidEmail(str)
{
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function ToggleTooltip(objTooltip)
{
	if(!navigator.userAgent.match(/iPhone/i) && !navigator.userAgent.match(/iPod/i) && !navigator.userAgent.match(/iPad/i)) return false;

	var arrSpan = objTooltip.getElementsByTagName("span");

	if (arrSpan[0])
	{
		if (arrSpan[0]["style"]["display"] == "none")
		{
			arrSpan[0]["style"]["display"]="block";
		} else {
			arrSpan[0]["style"]["display"]="none";
		}
	}
	return false;
}

function ShowMessageBox(objSource, strCloseImgPath)
{
	objSource["style"]["display"] = "block";

	var strImgId = "userMsgCloseBtn";
	if (objSource)
	{
		if (!document.getElementById(strImgId))
		{
			objSource["style"]["display"] = "block";
			var objImg = document.createElement("img");
			objImg["src"] = strCloseImgPath;
			objImg.setAttribute("id", "userMsgCloseBtn");
			objImg.setAttribute("alt", "Close");
			objImg.setAttribute("title", "Close");
			objSource.insertBefore(objImg, objSource.firstChild);
			try // IE
			{
				objSource.onclick = function() { return CloseMessageBox(this) };
			}
			catch (err)
			{
				objSource.setAttribute("onclick", "return CloseMessageBox(this);");
			}
		}
	}
	return false;
}

function CloseMessageBox(objSource)
{
	objSource["style"]["display"] = "none";
	return false;
}

function GenericFormCheck(objFrm, arrFields, hshSpecialFields)
{
	var strErr = "";
	var objField = null;

	if (!objFrm) return false;
	else if (!arrFields.length) return false;

	for(var i = 0; i < arrFields.length; i++)
	{
		// alert(objFrm[arrFields[i]]["name"]+":"+objFrm[arrFields[i]]["value"]);
		if (objFrm[arrFields[i]]["placeholder"] !== undefined && objFrm[arrFields[i]]["value"] == objFrm[arrFields[i]]["placeholder"]) objFrm[arrFields[i]]["value"] = "";

		if (objFrm[arrFields[i]]["value"] == "")
		{
			var j = 0; // Failsafe
			var objTest = objFrm[arrFields[i]];
			do
			{
				if (objTest.previousSibling != null) objTest = objTest.previousSibling;
				else break;
				j++;
			}
			while(objTest.nodeName != "LABEL" && j <= 10);

			if (objTest.nodeName == "LABEL") strErr = objTest.innerHTML;
			else strErr = "";
			objField = objFrm[arrFields[i]];
			break;
		}

		// Special field check
		if (hshSpecialFields !== undefined)
		{
			for (var strKey in hshSpecialFields)
			{
				if (strKey == objFrm[arrFields[i]]["name"])
				{
					switch(hshSpecialFields[strKey])
					{
						case "email":
							if (!ValidEmail(objFrm[arrFields[i]]["value"]))
							{
								strErr = "Your email address doesn't appear to be formed correctly.";
								objField = objFrm[arrFields[i]];
							}
							break;
						case "password":
							if (!IsUsernameType(objFrm[arrFields[i]]["value"]))
							{
								strErr = "Your password needs to be between 6 and 12 lower-case letters or numbers.";
								objField = objFrm[arrFields[i]];
							}
							break;

						default:
					} // End switch
					break;
				}
			}
		}
	}

	if (objField != null)
	{
		if (strErr == "" && objFrm[arrFields[i]]["placeholder"] !== undefined) strErr = objField["placeholder"];
		else if (strErr == "") strErr = "... all fields";
		_HighlightField(objField);
		alert("Please complete:\n\n"+strErr.replace("*", ""));
		// SetPlaceHolderText(objFrm, "#000", "#aaa");
		return false;
	}
	return true;
}

function _HighlightField(objField)
{
	objField.style.border = "1px solid #f00";
	if (objField.type != "select-one") objField.style.padding = "2px";
	// objField.style.margin = "0";
	// objField.select();
	// objField.focus();

	try // IE
	{
		objField.onfocus = function() { return _DeHighlightField(this) };
	}
	catch (err)
	{
		objField.setAttribute("onfocus", "return _DeHighlightField(this);");
	}
}

function _DeHighlightField(objField)
{
	objField.style.border = "1px solid #ccc";
	objField.style.color = "#000";
}

function IsUsernameType(arg1)
{
	var re = /^[a-z0-9_]{6,12}$/;
	return (re.test(arg1))
}

function SetPlaceHolderText(objForm, strNormalColour, strPlaceholderColour)
{
	if (!objForm) return;
	else if (navigator.userAgent.indexOf("MSIE") == -1) return;

	if (strNormalColour === undefined) strNormalColour = "#000";
	if (strPlaceholderColour === undefined) strPlaceholderColour = "#aaa";

	for(var i = 0; i < objForm.length; i++)
	{
		if (objForm["elements"][i]["placeholder"] === undefined) continue;

		// Copy the placeholder to the value
		if (objForm["elements"][i]["value"] == "" || objForm["elements"][i]["value"] == objForm["elements"][i]["placeholder"])
		{
			objForm["elements"][i]["style"]["color"] = strPlaceholderColour;
			objForm["elements"][i]["value"] = objForm["elements"][i]["placeholder"];
		}

		try // IE
		{
			objForm["elements"][i].onfocus = function() { return _PlaceHolderFocus(this, strNormalColour) };
			objForm["elements"][i].onblur = function() { return _PlaceHolderBlur(this, strPlaceholderColour) };
		}
		catch (err)
		{
			objForm["elements"][i].setAttribute("onfocus", "return _PlaceHolderFocus(this, strNormalColour);");
			objForm["elements"][i].setAttribute("onblur", "return _PlaceHolderBlur(this, strPlaceholderColour);");
		}
	}
}

function _PlaceHolderBlur(obj, strPlaceholderColour)
{
	if (obj["value"] == "")
	{
		obj["value"] = obj["placeholder"];
		obj["style"]["color"] = strPlaceholderColour;
	}
	return false;
}

function _PlaceHolderFocus(obj, strNormalColour)
{
	if (obj["value"] != obj["placeholder"]) return false;

	obj["value"] = "";
	obj["style"]["color"] = strNormalColour;
	return false;
}

function ProcessRotations()
{
  var intMainIMgHeight = 0;
  var objDiv1 = document.getElementById("_rotationMains");
  var objDiv2 = document.getElementById("_rotationThumbs");
  if (objDiv1 && objDiv2)
  {
    for(var i = 0; i < arrMedia.length; i++)
    {
	  // Forward slash
	  if (arrMedia[i]["main_tested_path"].substring(0, 1) != "/") arrMedia[i]["main_tested_path"] = "/"+arrMedia[i]["main_tested_path"];
	  if (arrMedia[i]["thumb_tested_path"].substring(0, 1) != "/") arrMedia[i]["thumb_tested_path"] = "/"+arrMedia[i]["thumb_tested_path"];

      if (i == 0)
      {
        strDefaultImgName = arrMedia[i]["main_key"];
        var objImgMain = document.createElement("img");
        objImgMain.setAttribute("src", arrMedia[i]["main_tested_path"]);
        objImgMain.setAttribute("id", arrMedia[i]["main_key"]);
        objImgMain.setAttribute("alt", arrMedia[i]["alt_text"]);
        objImgMain["style"]["width"] = arrMedia[i]["main_width"];
        objImgMain["style"]["height"] = arrMedia[i]["main_height"];
        objDiv1.appendChild(objImgMain);

        intMainIMgHeight = arrMedia[i]["main_height"];
      }
      else
      {
        NewImage(arrMedia[i]["main_tested_path"]);
        if (arrMedia[i]["main_height"] > intMainIMgHeight) intMainIMgHeight = arrMedia[i]["main_height"];
      }
      objDiv1["style"]["height"] = intMainIMgHeight+"px";

      var objImgThumb = document.createElement("img");
      objImgThumb.setAttribute("src", arrMedia[i]["thumb_tested_path"]);
      objImgThumb.setAttribute("id", arrMedia[i]["thumb_key"]);
      objImgThumb.setAttribute("alt", arrMedia[i]["alt_text"]);
      objImgThumb["style"]["width"] = arrMedia[i]["thumb_width"];
      objImgThumb["style"]["height"] = arrMedia[i]["thumb_height"];
      objDiv2.appendChild(objImgThumb);

      try // IE
      {
        objImgThumb.onmouseover = function() { return _SwitchImageOver(this) };
        objImgThumb.onmouseout = function() { return _SwitchImageOut(this) };
      }
      catch (err)
      {
        objImgThumb.setAttribute("onmouseover", "return _SwitchImageOver(this);");
        objImgThumb.setAttribute("onmouseout", "return _SwitchImageOut(this);");
      }
    }
  }
}

function _SwitchImageOver(objThumb)
{
  for(var i = 0; i < arrMedia.length; i++)
  {
    if (arrMedia[i]["thumb_key"] == objThumb["id"])
    {
      var objImgMain = document.getElementById(strDefaultImgName);
      if (objImgMain)
      {
        objImgMain.setAttribute("src", arrMedia[i]["main_tested_path"]);
        objImgMain.setAttribute("alt", arrMedia[i]["alt_text"]);
        objImgMain["style"]["width"] = arrMedia[i]["main_width"];
        objImgMain["style"]["height"] = arrMedia[i]["main_height"];
        break;
      }
    }
  }
}

function _SwitchImageOut(objThumb)
{
  for(var i = 0; i < arrMedia.length; i++)
  {
    if (arrMedia[i]["main_key"] == strDefaultImgName)
    {
      var objImgMain = document.getElementById(strDefaultImgName);
      if (objImgMain)
      {
        objImgMain.setAttribute("src", arrMedia[i]["main_tested_path"]);
        objImgMain.setAttribute("alt", arrMedia[i]["alt_text"]);
        objImgMain["style"]["width"] = arrMedia[i]["main_width"];
        objImgMain["style"]["height"] = arrMedia[i]["main_height"];
        break;
      }
    }
  }
}

// IMAGE ROLLOVERS
function NewImage(arg)
{
	if (document.images)
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function ChangeImage()
{
	if (document.images)
	{
		for (var i = 0; i < ChangeImage.arguments.length; i+=2)
		{
			document[ChangeImage.arguments[i]].src = ChangeImage.arguments[i+1];
		}
	}
	// return false;
}

function Trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//
// to replace target="_blank"
//
function OpenNewWindow(objLink)
{
	var newWinHandle = window.open(objLink["href"], "_blank001");
    newWinHandle.focus();
    // setTimeout("newWinHandle.focus()", 500);
    return false;
}

// OpenWin('page-edit-library.html','w2', 380, Math.ceil(screen.height - 76), 1, 1, parseInt(screen.width - (380 + 12)), 0);
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 ManageCssBasic(strObName, strStyleKey, strStyleValue)
{
	var objTmp = document.getElementById(strObName);
	var strOverrideUrl = "";
	if(objTmp != null)
	{
		var strCurrUri = window.location.pathname.toLowerCase().replace("/", "");

		// Add the search string for this project
		strCurrUri += location.search

		// Check for parent menu to override
		// ...set in head of master_template.html
		if (strOverrideUrl != "" && strOverrideUrl != strCurrUri) strCurrUri = strOverrideUrl;

		// Special case for index page
		if (strCurrUri == "") strCurrUri = "index.html";

		var arrA = objTmp.getElementsByTagName("a");
		var strAnchorHref = null;
		for(var i = 0; i < arrA.length; i++)
		{
			strAnchorHref = arrA[i]["href"].toLowerCase().replace("//", "").replace("/", "").replace(location.hostname, "").replace(location.protocol, "");
			strAnchorHref = strAnchorHref.replace(":10080", "").replace(location.hash, ""); // .replace(location.search, "");
			strAnchorHref = strAnchorHref.replace("#", "");
			if (strCurrUri.indexOf(strAnchorHref) != -1)
			{
				arrA[i]["style"][strStyleKey] = strStyleValue;
				break;
			}
		}
	}
}

//
// 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 blnParseEntireStyle = (navigator.userAgent.indexOf("Firefox") != -1)?false:true;
	var objMenu = document.getElementById(strObName);
	if(objMenu != null)
	{
		var strCurrUri = window.location.pathname.toLowerCase().replace("/", "");

		// Add the search string for this project
		strCurrUri += location.search

		// Check for parent menu to override
		// ...set in head of master_template.html
		if (strOverrideUrl != "")
		{
			if (strOverrideUrl != strCurrUri) strCurrUri = strOverrideUrl;
		}

		// Special case for index page
		if (strCurrUri == "") strCurrUri = "index.html";

		var arrA = objMenu.getElementsByTagName("a");
		var strAnchorHref = null;
		for(var i=0; i< arrA.length;i++)
		{
			if (arrA[i]["innerHTML"].match(/\<img/gi)) continue;

			strAnchorHref = arrA[i]["href"].toLowerCase().replace("//", "").replace("/", "").replace(location.hostname, "").replace(location.protocol, "");
			strAnchorHref = strAnchorHref.replace(":10080", "").replace(location.hash, ""); // .replace(location.search, "");
			strAnchorHref = strAnchorHref.replace("#", "");
			strAnchorHref = strAnchorHref.substring(strAnchorHref.lastIndexOf("/")+1, strAnchorHref.length); // Removing country element

			if (strCurrUri.indexOf(strAnchorHref) != -1)
			{
				arrA[i]["class"] = strNewClassName; // For non-IE
				arrA[i]["className"] = strNewClassName; // For IE
				// arrA[i].setAttribute("class", strNewClassName);
				// alert(document.styleSheets[2].rules.item(3).style.getAttribute('color'));
				// alert(arrA[i]["class"]["border"]);
				// arrA[i].href = "javascript:void(0);";
				var objStyle = GetCssObjectByRuleName(strNewClassName);
				arrA[i]["style"]["color"] = objStyle["color"];
				arrA[i]["style"]["fontWeight"] = objStyle["fontWeight"];
				arrA[i]["style"]["background"] = objStyle["background"];
				arrA[i]["style"]["border"] = objStyle["border"];
				arrA[i]["style"]["borderTop"] = objStyle["borderTop"];
				arrA[i]["style"]["borderBottom"] = objStyle["borderBottom"];
				arrA[i]["style"]["borderLeft"] = objStyle["borderLeft"];
				arrA[i]["style"]["borderRight"] = objStyle["borderRight"];
				arrA[i]["style"]["margin"] = objStyle["margin"];
				arrA[i]["style"]["padding"] = objStyle["padding"];

				// alert(objStyle.getAttribute('color'));

				// This works fine in IE and Safari but not FireFox
				if (blnParseEntireStyle)
				{
					if (objStyle)
					{
						var str = "";
						for (key in objStyle)
						{
							// str +=key+":"+objStyle[key]+";"; // For testing
							if (objStyle[key])
								arrA[i]["style"][key] = objStyle[key];
						}
						// alert(str); // For testing
					}
				}
			}
		}
	}
}

//
// Looks through all style sheet for a particular rule name
//
function GetCssObjectByRuleName(strCssRuleName)
{
	var cssRules;
	var objStyle;
	var blnTestMode = false;

	if (document.all)
	{
		cssRules = "rules";
	}
	else if (document.getElementById)
	{
		cssRules = "cssRules";
	}

	for (var i = 0; i < document.styleSheets.length; i++)
	{
		for (var j = 0; j < document.styleSheets[i][cssRules].length; j++)
		{
			if (document.styleSheets[i][cssRules][j].selectorText.replace(".", "") == strCssRuleName)
			{
				var objStyle = document.styleSheets[i][cssRules][j].style;
				if (!blnTestMode) return objStyle;
			}
		}
	}

	if (blnTestMode) // For testing
	{
		var str = "";
		for (key in objStyle)
		{
			str +=key+":"+objStyle[key]+";";
		}
		alert(str);
	}
	return objStyle;
}

function GoTo(strLocation)
{
	location.href = strLocation;
	return false;
}

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;
}

//
// arg1: a unique id
// arg2: URL to display
// arg3: popup width
// arg4: popup height
// arg5: topbar win title
//
function ShowPopup(strWinId, strUrl, intIfrWidth, intIfrHeight, strTopBarTitle, intLeftPos, intTopPos, blnCloseButton)
{
	// http://www.codeproject.com/KB/aspnet/JsOOP1.aspx
	var objPopup = new ClassPopup();

	// Basic properties
	var objPopupProperties = new Object;
	if (intLeftPos) objPopupProperties["intLeftPos"] = intLeftPos;
	if (intTopPos) objPopupProperties["intTopPos"] = intTopPos;
	objPopupProperties["strWinId"] = strWinId;
	objPopupProperties["strUrl"] = strUrl;
	objPopupProperties["blnCloseButton"] = blnCloseButton;
	objPopupProperties["intIfrWidth"] = intIfrWidth;
	objPopupProperties["intIfrHeight"] = intIfrHeight;
	objPopupProperties["strTopBarTitle"] = strTopBarTitle;
	objPopupProperties["intMasterDivOpacity"] = 80; // Overall percentage opacity of greyed out background
	objPopupProperties["hexMasterDivBgColour"] = "#333";
	objPopupProperties["strTopBarinnerHTML"] = '<img src="/local/common/images/winTopClose.png" style="float:right;margin-right:3px;" alt="Close" /><div style="float:right;margin:5px 3px 0 0;cursor:pointer;color:#000;text-decoration: none;">CLOSE</div>';
	objPopupProperties["hexTopBarAnchorColor"] = "#fff";
	objPopup.SetProperties(objPopupProperties);

	// These are standard css styles
	// Names are appropriate for javascript
	var objTopBarStyle = new Object;
	if (blnCloseButton ==- true) objTopBarStyle["height"] = 26;
	else objTopBarStyle["height"] = 0;
	objTopBarStyle["background"] = "#ddd url('/local/common/images/winTopBg.jpg') repeat-x";
	objTopBarStyle["padding"] = "4px 0 0 7px";
	objTopBarStyle["margin"] = "0";
	objTopBarStyle["color"] = "#000";
	objTopBarStyle["fontWeight"] = "bold";
	objTopBarStyle["fontSize"] = "12px";
	objPopup.SetTopBarCss(objTopBarStyle);

	objPopup.BuildWindow();
	return false;
}

function ClassPopup()
{
	//
	// PRIVATE PROPERTIES only available from within the class
	// ...must have var prefix
	//
	var intCounter = 0;
	var _zIndex = 100;
	var objCentreDiv = null;
	var _hshTopBarCss = new Object;
	var _hshProperties = new Object;

	// Public setter arg needs to be a hash
	this["SetProperties"] = _SetProperties;
	this["SetTopBarCss"] = _SetTopBarCss;
	this["BuildWindow"] = _BuildWindow;
	this["_CreateElement"]  = _CreateElement;
	this["_GetWindowHeight"] = _GetWindowHeight;
	this["_GetWindowWidth"] = _GetWindowWidth;
	this["_intRightMarginWidth"] = 16;
	// this["_Tween"] = _Tween;

	// Constants
	this["intWinWidth"] = GetWindowWidth();
	this["intWinHeight"] = GetWindowHeight();
	var arrScrollXY = GetScrollXY();
	this["intWinScrollX"] = arrScrollXY[0];
	this["intWinScrollY"] = arrScrollXY[1];

	// INITIALISATION
	// - without this appears to causes a problem post
	// - ie8!
	this["_zIndex"] = _zIndex;

	function _BuildWindow()
	{
		// System check
		/*
		for (key in _hshProperties)
		{
			alert(_hshProperties[key]);
		}
		*/
		var strContainerDivId = _hshProperties["strWinId"]+"_contDiv";
		var strCentreDivId = _hshProperties["strWinId"]+"_centDiv";
		var strControlDivId = _hshProperties["strWinId"]+"_ctrDiv";
		var strContainerIfrId = _hshProperties["strWinId"]+"_contIfr";

		// These are used mainly for closing the window
		var objMasterDiv = document.getElementById(strContainerDivId); // test whether it has already been created
		var objCenterDiv = document.getElementById(strCentreDivId); // test whether it has already been created
		var objContainerIfrId = document.getElementById(strContainerIfrId);

		// Doesn't already exist
		if (objMasterDiv == null)
		{
			// Create reference to common "body" across doctypes
			var standardbody = (document.compatMode=="CSS1Compat")?document.documentElement:document.body;
			// standardbody["style"]["overflow"] = "hidden";

			if (navigator.userAgent.indexOf("IE") != -1)
			{
				standardbody["style"]["overflow"] = "hidden";
			}

			else
			{
				standardbody["style"]["overflowY"] = "hidden";
			}

			// Hide the scroll bar
			var intBodyMarginRight = (standardbody["style"]["marginRight"] != "")?parseInt(standardbody["style"]["marginRight"]):0;
			standardbody["style"]["marginRight"] = intBodyMarginRight+this["_intRightMarginWidth"]+"px";

			// Create master div container
			var objMasterDiv = this._CreateElement
			(
				"div",
				strContainerDivId,
				this["intWinScrollX"],
				this["intWinScrollY"],
				this["intWinWidth"],
				this["intWinHeight"],
				_hshProperties["hexMasterDivBgColour"],
				this["_zIndex"]++,
				_hshProperties["intMasterDivOpacity"]
			);
			document.body.appendChild(objMasterDiv);

			// Create central div
			// All other objects are created inside this div
			var intVerticalAdjustment = 30;

			if (_hshProperties["intLeftPos"]) var intLeftPos = parseInt(_hshProperties["intLeftPos"])+"px";
			else
			{
				var intLeftPos = parseInt((this["intWinWidth"]/2) - (_hshProperties["intIfrWidth"]/2))+this["intWinScrollX"]+"px";
			}

			if (_hshProperties["intTopPos"]) var intTopPos = parseInt(_hshProperties["intTopPos"])+"px";
			else
			{
				var intTopPos = parseInt((this["intWinHeight"]/2) - (_hshProperties["intIfrHeight"]/2)
						- (_hshTopBarCss["height"]*2)+_hshTopBarCss["height"]+intVerticalAdjustment)+this["intWinScrollY"]+"px";
			}

			objCentreDiv = this._CreateElement
			(
				"div",
				strCentreDivId,
				intLeftPos,
				intTopPos,
				10, // _hshProperties["intIfrWidth"],
				10, // _hshProperties["intIfrHeight"],
				"#333", // _hshProperties["hexMasterDivBgColour"],
				this["_zIndex"]++,
				100
			);
			objCentreDiv["style"]["overflow"] = "hidden";
			document.body.appendChild(objCentreDiv);

			if (_hshProperties["blnCloseButton"] === true)
			{
				// Create top control area
				var objCtrDiv = this._CreateElement
				(
					"div",
					strControlDivId,
					0,
					0,
					_hshProperties["intIfrWidth"],
					_hshTopBarCss["height"],
					_hshTopBarCss["background"],
					this["_zIndex"]++,
					100
				);
				objCentreDiv.appendChild(objCtrDiv);

				// Create close link
				var objLink = document.createElement("a");
				objLink.setAttribute("href", "javascript:void(0);");
				var strWinId = _hshProperties["strWinId"];
				if (navigator.userAgent.indexOf("MSIE") != -1) objLink.onclick = function() {ShowPopup(strWinId)};
				else objLink.setAttribute("onclick", "return ShowPopup('"+strWinId+"');"); // For other browsers
				objLink["innerHTML"] = _hshProperties["strTopBarinnerHTML"];
				objLink["style"]["color"] = _hshProperties["hexTopBarAnchorColor"];
				objCtrDiv.appendChild(objLink);

				// Create box title
				if (_hshProperties["strTopBarTitle"] != undefined)
				{
					var objDiv = document.createElement("div");
					// objDiv["style"]["width"] = parseInt(_hshProperties["intIfrWidth"]/2)+"px";
					objDiv["style"]["height"] = _hshTopBarCss["height"]+"px";
					objDiv["style"]["textAlign"] = "left";
					objDiv["style"]["margin"] = _hshTopBarCss["margin"];
					objDiv["style"]["padding"] = _hshTopBarCss["padding"];
					objDiv["style"]["color"] = _hshTopBarCss["color"];
					objDiv["style"]["fontWeight"] = _hshTopBarCss["fontWeight"];
					objDiv["style"]["fontSize"] = _hshTopBarCss["fontSize"];

					objDiv["innerHTML"] = _hshProperties["strTopBarTitle"];
					objCtrDiv.appendChild(objDiv);
				}
			}

			var objNewIfr = this._CreateElement
			(
				"iframe",
				strContainerIfrId,
				0,
				_hshTopBarCss["height"],
				_hshProperties["intIfrWidth"],
				_hshProperties["intIfrHeight"],
				"#fff",
				this["_zIndex"]++,
				100
			);
			objNewIfr.setAttribute("name", strContainerIfrId);
			objNewIfr.setAttribute("src", _hshProperties["strUrl"]);
			objNewIfr.setAttribute("scrolling", "auto");
			objNewIfr.setAttribute("frameborder", "0");
			objCentreDiv.appendChild(objNewIfr);
			_Tween();

			return false;
		}

		else // Switch display
		{
			// Get the body object
			// ... for IE6
			var standardbody = (document.compatMode=="CSS1Compat")?document.documentElement:document.body;

			// Reveal the scroll bar
			if (navigator.userAgent.indexOf("IE") != -1)
			{
				standardbody["style"]["overflow"] = "auto";
			}

			else
			{
				standardbody["style"]["overflowY"] = "scroll";
				// standardbody["style"]["overflowX"] = "scroll";
			}

			// Adjust right margin
			var intBodyMarginRight = (standardbody["style"]["marginRight"] != "")?parseInt(standardbody["style"]["marginRight"]):0;
			standardbody["style"]["marginRight"] = intBodyMarginRight - this["_intRightMarginWidth"]+"px";

			// Close and remove
			objContainerIfrId["src"] = ""; // Take out the iframe - causes problem with ie8
			document.body.removeChild(objCenterDiv);
			document.body.removeChild(objMasterDiv);

			return false;
		}
	}

	function _Tween()
	{
		if (objCentreDiv["offsetWidth"] < _hshProperties["intIfrWidth"])
		{
			// To avoid making width too great
			var intCurrWidth = 0;
			if ((objCentreDiv["offsetWidth"]+50) > _hshProperties["intIfrWidth"]) intCurrWidth = _hshProperties["intIfrWidth"];
			else intCurrWidth = parseInt(objCentreDiv["offsetWidth"]+50)

			// Make the width change
			objCentreDiv["style"]["width"] = intCurrWidth+"px";

			setTimeout(_Tween, 30);
		}

		else if (objCentreDiv["offsetHeight"] < parseInt(_hshTopBarCss["height"]+_hshProperties["intIfrHeight"]))
		{
			var intChange = 500;
			var intCurrHeight = 0;

			// Ease out tween
			var intCurrDiff = _hshProperties["intIfrHeight"] - (objCentreDiv["offsetHeight"]+intChange);
			if (intCurrDiff < 100 && intCounter < intChange)
			{
				intChange = intChange - intCounter;
				intCounter++;
			}

			// To avoid making height too great
			if ((objCentreDiv["offsetHeight"]+intChange) > _hshProperties["intIfrHeight"]) intCurrHeight = _hshProperties["intIfrHeight"];
			else intCurrHeight = parseInt(objCentreDiv["offsetHeight"]+intChange)

			// Make the height change
			objCentreDiv["style"]["height"] = intCurrHeight+"px";
			setTimeout(_Tween, 30);
		}

		else // Fall through - a final correction
		{
			objCentreDiv["style"]["height"] = parseInt(_hshTopBarCss["height"]+_hshProperties["intIfrHeight"])+"px";
			objCentreDiv["style"]["width"] = _hshProperties["intIfrWidth"]+"px";
			intCounter = 0;
		}
	}

	function _SetProperties(hshProperties)
	{
		_hshProperties = hshProperties;
	}

	function _SetTopBarCss(hshTopBarCss)
	{
		_hshTopBarCss = hshTopBarCss;
	}

	function _CreateElement(strElementType, strId, intLeft, intTop, intWidth, intHeight, strBackground, intZindex, intBackgroundOpacity)
	{
		var objPointer = document.createElement(strElementType);
		objPointer.setAttribute("id", strId);

		// Set these for all
		// Both following lines may be reset if necessary using the returned object
		objPointer["style"]["position"] = "absolute";
		objPointer["style"]["display"] = "block";

		// Style elements
		if (intLeft > 0) objPointer["style"]["left"] = intLeft+"px";
		else objPointer["style"]["left"] = intLeft;

		if (intTop > 0) objPointer["style"]["top"] = intTop+"px";
		else objPointer["style"]["top"] = intTop;

		objPointer["style"]["width"] = intWidth+"px";
		objPointer["style"]["height"] = intHeight+"px";
		objPointer["style"]["background"] = strBackground;
		objPointer["style"]["zIndex"] = intZindex;

		if (intBackgroundOpacity != 100)
		{
			objPointer["style"]["filter"] = "alpha(opacity="+intBackgroundOpacity+") Shadow(Color=#00FF00, Direction=225)";
			objPointer["style"]["-moz-opacity"] = "."+intBackgroundOpacity;
			objPointer["style"]["opacity"] = "."+intBackgroundOpacity;
			objPointer["style"]["-khtml-opacity"] = "0."+intBackgroundOpacity;
		}
		return objPointer;
	}

	function _GetWindowHeight()
	{
		var intHeight = 0;
		if( typeof( window.innerWidth) == "number")
		{
			intHeight = window.innerHeight;
		}

		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			intHeight = document.documentElement.clientHeight;
		}

		else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			intHeight = document.body.clientHeight;
		}
		return intHeight;
	}

	function _GetWindowWidth()
	{
		var intWidth = 0;
		if( typeof( window.innerWidth) == "number")
		{
			intWidth = window.innerWidth;
		}

		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			intWidth = document.documentElement.clientWidth;
		}

		else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			intWidth = document.body.clientWidth;
		}
		return intWidth;
	}

	//
	// OmniWeb 4.2-, NetFront 3.3- and Clue browser do not provide any way to do this.
	// Safari and OmniWeb 4.5+ have bugs that do not affect this script+, which return -8 if the scrolling is 0,
	// but get all other scrolling offsets correct. However, as they correctly provide window.pageXOffset/pageYOffset,
	// this function will not have any problems.
	//
	function GetScrollXY()
	{
		var scrOfX = 0, scrOfY = 0;

		//Netscape compliant
		if( typeof( window.pageYOffset ) == 'number' )
		{
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		}

		//DOM compliant
		else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
		{
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		}

		//IE6 standards compliant mode
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
		{
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [scrOfX, scrOfY];
	}

	return false;
}

//
// 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; }
}

