
// 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);
}

//
// 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++)
		{
			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]["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;
}

function HighlightField(objField)
{
	objField.style.border = "1px solid #ff0000";
	objField.style.padding = "1px";
	objField.style.margin = "1px 0 1px 0";
	objField.select();
	objField.focus();
}

function IsUsernameType(arg1)
{
	var re = /^[a-z0-9_]{6,12}$/;
	if (re.test(arg1)) return true;
	return false;
}

//
// 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)
{
	// 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["intIfrWidth"] = intIfrWidth;
	objPopupProperties["intIfrHeight"] = intIfrHeight;
	objPopupProperties["strTopBarTitle"] = strTopBarTitle;
	objPopupProperties["intMasterDivOpacity"] = 75; // Overall percentage opacity of greyed out background
	objPopupProperties["hexMasterDivBgColour"] = "#456";
	objPopupProperties["strTopBarinnerHTML"] = '<img src="/local/rok.tv/images/winTopClose.gif" style="float:right;" alt="Close" /><div style="float:right;margin:5px 3px 0 0;cursor:pointer;color:#666;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;
	objTopBarStyle["height"] = 26;
	objTopBarStyle["background"] = "#888 url('/local/rok.tv/images/winTopBg.jpg') repeat-x";
	objTopBarStyle["padding"] = "4px 0 0 7px";
	objTopBarStyle["margin"] = "0";
	objTopBarStyle["color"] = "#666";
	objTopBarStyle["fontWeight"] = "bold";
	objTopBarStyle["fontSize"] = "13px";
	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);

			// 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, 10);
		}

		else if (objCentreDiv["offsetHeight"] < parseInt(_hshTopBarCss["height"]+_hshProperties["intIfrHeight"]))
		{
			var intChange = 20;
			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, 10);
		}

		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; }
}
