//------------------------------------------------------
// Page:					Controls.js
// Author:				John Bradnam
// Created:				070812 JLB
// Copyright:			Republicorp P/L
// Purpose:				Control support scripts
// History:
//------------------------------------------------------

//------------------------------------------------------
// Control library initialisation
//------------------------------------------------------

var aryPreLoad = new Array();

function ControlsOnLoad() {
	GlassButtonOnLoad();
	GlassTabOnLoad();
	BeautyBarOnLoad();
} //ControlsOnLoad

//------------------------------------------------------
// Glass Button
//------------------------------------------------------

function GlassButtonOnLoad() {
	var objPre = null;
	var aryImg = document.getElementsByTagName("IMG");
	for (var intIndex = 1; intIndex < aryImg.length; intIndex++) {
		var objImg = aryImg[intIndex];
		var strImg = objImg.getAttribute("down");
		if (strImg != null) {
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
			strImg = objImg.getAttribute("over");
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
			strImg = objImg.getAttribute("enabled");
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
			strImg = objImg.getAttribute("off");
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
		}
	}
} //GlassButtonOnLoad

function GlassButtonOver(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("over"), "right top");
} //GlassButtonOver

function GlassButtonOut(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("enabled"), "left top");
} //GlassButtonOut

function GlassButtonDown(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("down"), "left bottom");
} //GlassButtonDown

function GlassButtonUp(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("enabled"), "left top");
} //GlassButtonUp

function GlassButtonEnable(strId, blnEnable) {
	var objBtn = document.getElementById(strId);
	if (objBtn != null) {
		if (blnEnable) {
			GlassButtonSet(objBtn, objBtn.getAttribute("enabled"), "left top");
			objBtn.setAttribute("state", "on");
			objBtn.style.cursor = "pointer";
			objBtn.disabled = false;
		}
		else {
			GlassButtonSet(objBtn, objBtn.getAttribute("off"), "right bottom");
			objBtn.setAttribute("state", "off");
			objBtn.style.cursor = "default";
			objBtn.disabled = true;
		}
	}
} //GlassButtonEnable

function GlassButtonSet(objBtn, strSrc, strPos) {
	if (objBtn.getAttribute("enabled") == null)
		objBtn.style.backgroundPosition = strPos;
	else if (objBtn.getAttribute("filter") != "on")
		objBtn.src = strSrc;
	else
		objBtn.filters['DXImageTransform.Microsoft.AlphaImageLoader'].src = strSrc;
} //GlassButtonSet

//------------------------------------------------------
// Glass Tab
//------------------------------------------------------

function GlassTabOnLoad() {
	var objPre = null;
	var aryImg = document.getElementsByTagName("IMG");
	for (var intIndex = 1; intIndex < aryImg.length; intIndex++) {
		var objImg = aryImg[intIndex];
		var strImg = objImg.getAttribute("selected");
		if (strImg != null) {
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
			strImg = objImg.getAttribute("over");
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
			strImg = objImg.getAttribute("enabled");
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
			strImg = objImg.getAttribute("off");
			objPre = document.createElement("IMG");
			objPre.src = strImg;
			aryPreLoad.push(objPre);
		}
	}
} //GlassTabOnLoad

function GlassTabOver(objTab) {
	if (objTab.getAttribute("active") == "False")
		GlassTabSet(objTab, objTab.getAttribute("over"), "right top");
} //GlassTabOver

function GlassTabOut(objTab) {
	if (objTab.getAttribute("active") == "False")
		GlassTabSet(objTab, objTab.getAttribute("enabled"), "left top");
	else
		GlassTabSet(objTab, objTab.getAttribute("selected"), "left bottom");
} //GlassTabOut

function GlassTabClick(objSelectedTab, blnExecute) {
	var blnChanged = false;
	if (typeof(objSelectedTab) != "object")
		objSelectedTab = document.getElementById(objSelectedTab);
	if (typeof(blnExecute) == "undefined")
		blnExecute = true;
	var objTabbar = objSelectedTab.parentNode;
	var objTab = GetFirstChild(objTabbar);
	while (objTab != null) {
		if (objTab == objSelectedTab) {
			blnChanged = (objTab.getAttribute("active") == "False");
			GlassTabSet(objTab, objTab.getAttribute("selected"), "right top");
			objTab.setAttribute("active", "True");
			objTab.style.cursor = "default";
		}
		else if ((objTab.nodeType == 1) && (objTab.tagName == "IMG")) {
			GlassTabSet(objTab, objTab.getAttribute("enabled"), "left top");
			objTab.setAttribute("active", "False");
			objTab.style.cursor = "pointer";
		}
		objTab = GetNextSibling(objTab);
	} //while
	if ((blnExecute) && (blnChanged))
		GlassTabExecute(objSelectedTab);
} //GlassTabClick

function GlassTabExecute(objSelectedTab) {
	var strAction = objSelectedTab.getAttribute("action");
	if (strAction != null)
		eval(strAction);
} //GlassTabExecute

function GlassTabSet(objTab, strSrc, strPos) {
	if (objTab.getAttribute("enabled") == null)
		objTab.style.backgroundPosition = strPos;
	else if (objTab.getAttribute("filter") != "on")
		objTab.src = strSrc;
	else
		objTab.filters['DXImageTransform.Microsoft.AlphaImageLoader'].src = strSrc;
} //GlassTabSet

//------------------------------------------------------
// Beauty Bar
//------------------------------------------------------

var intBeautyMoveBy = 25;
var blnBeautyInAnimate = false;
var	blnBeautyInResize = false;
var intBeautyBodyPadding = 15;
var intBeautyHeadingPadding = 7;
var intBeautyBorderWidth = 10;
var blnBeautyBorderHeight = 0;

function BeautyBarOnLoad() {
	blnBeautyBorderHeight = ((BrowserDetect.browser == "IE") && (BrowserDetect.major == "6")) ? 2 : 0;
} //BeautyBarOnLoad

function BeautyBarSetTabCount() {
	var objBar = document.getElementById("beautybar");
	if (objBar != null) {
		var objHdr = GetFirstChild(objBar);
		var objBdy = GetNextSibling(objHdr);
		var objFtr = GetNextSibling(objBdy);
		var intTabs = 0;
		var objTab = GetFirstChild(objBdy);
		while (objTab != null) {
			if ((objTab.nodeType == 1) && (objTab.tagName == "IMG") && (objTab.style.display != "none"))
				intTabs++;
			objTab = GetNextSibling(objTab);
		} //while
		objBdy.setAttribute("tabs", intTabs);
	}
} //BeautyBarSetTabCount

function BeautyBarResize() {
	var objBar = document.getElementById("beautybar");
	if ((objBar != null) && (!blnBeautyInAnimate) && (!blnBeautyInResize)) {
		blnBeautyInResize = true;
		var intHeight = 768;
		if ((document.documentElement) && (document.documentElement.clientHeight))
			intHeight = document.documentElement.clientHeight;
		else if (document.body)
		  intHeight = document.body.clientHeight;
		intBeautyMoveBy = (intHeight > 603) ? Math.floor((intHeight - 603) / 6) + 25 : 25;
		try	{
			switch (BrowserDetect.browser) {
				case "Chrome": intBeautyMoveBy = intBeautyMoveBy >> 2; break;
				case "Safari": intBeautyMoveBy = intBeautyMoveBy >> 1; break;
				case "Firefox": intBeautyMoveBy = intBeautyMoveBy << 1; break;
			}
		}
		catch (e) {
		}
		var objHdr = GetFirstChild(objBar);
		var objBdy = GetNextSibling(objHdr);
		var objFtr = GetNextSibling(objBdy);
/*
		var objContainer = document.getElementById("beautybarcontainer");
		var intTableHeight = objContainer.offsetHeight;
		var objDebug = document.getElementById("id_debug");
		var strDebug = "Offset Height: " + intTableHeight + "px; Doc Height: " + intHeight + "px<br/>";
		//intHeight = intTableHeight;
		objContainer.style.height = (intHeight - intBeautyBorderWidth) + "px";
		var intHdr = BeautyBarGetHeight(objHdr);
		var intFtr = BeautyBarGetHeight(objFtr);
		var intBdy = intHeight - intHdr - intFtr;
		strDebug += "Height: " + intHeight + "px; Header: " + intHdr + "px; Footer: " + intFtr + "px;<br/>";
		strDebug += "Body (true): " + intBdy + "px; Body (calc): " + (intBdy - intBeautyBodyPadding) + "px;";
		document.getElementById("id_debug").innerHTML = strDebug;
*/		
		intHeight = intHeight - BeautyBarGetHeight(objHdr) - BeautyBarGetHeight(objFtr) - intBeautyBodyPadding;
		if (intHeight >= 0) {
//			alert(objBdy.className + " height = " + intHeight + "px");
			objBdy.style.height = intHeight + "px";
			var objTab = GetFirstChild(objBdy);
			while (objTab != null) {
				if ((objTab.nodeType == 1) && (objTab.tagName == "IMG")) {
					objPage = GetNextSibling(objTab);
					objPage.removeAttribute("max");
					if (objPage.getAttribute("state") == "1")
						BeautyBarInitPage(objPage, intHeight);
				}
				objTab = GetNextSibling(objTab);
			} //while
		}
		blnBeautyInResize = false;
	}
} //BeautyBarResize

function BeautyBarGetHeight(objNode)
{
	var intHeight = 0;
	var intTop = 0;
	var intBase = -1;
	var intLast = 0;
	var objChild = GetFirstChild(objNode);
	while (objChild != null) {
		if ((objChild.nodeType == 1) && (objChild.style.display != "none")) {
			intTop = objChild.offsetTop;
			intLast = (objChild.naturalHeight != null) ? objChild.naturalHeight : objChild.offsetHeight;
			intBase = (intBase < 0) ? intTop : intBase;
			intHeight += intLast;
		}
		objChild = GetNextSibling(objChild);
	} //while
	if ((intBase == 0) && (intTop != 0))
		intHeight = intTop + intLast;
	return intHeight;
} //BeautyBarGetHeight

function BeautyBarSelect(strSelectedTab) {
	if (!blnBeautyInAnimate) {
		blnBeautyInResize = true;
		var objTab = (typeof(strSelectedTab) == "object") ? strSelectedTab : document.getElementById(strSelectedTab);
		var objPage = GetNextSibling(objTab);
		if (objPage.getAttribute("state") == "0") {
			if (objPage.getAttribute("max") == null)
				BeautyBarInitPage(objPage);
			var strCloId = "";
			var objDiv = GetFirstChild(objTab.parentNode);
			while (objDiv != null) {
				if ((objDiv.nodeType == 1) && (objDiv != objTab) && (objDiv.className.substr(0,9) == "beautytab")) {
					objPage = GetNextSibling(objDiv);
					if (objPage.getAttribute("state") == "1")
						strCloId = objDiv.id;
				}
				objDiv = GetNextSibling(objDiv);
			} //while
			blnBeautyInAnimate = true;
			setTimeout('BeautyBarAnimate("' + objTab.id + '","' + strCloId + '")', 1);
		}
		blnBeautyInResize = false;
	}
} //BeautyBarSelect

function BeautyBarInitPage(objPage, intPagesHeight)
{
	var strChange = "";

	var objPages = objPage.parentNode;
	var objTab = GetPreviousSibling(objPage);
	var intTabs = parseInt(objPages.getAttribute("tabs"));
	if (typeof(intPagesHeight) == "undefined")
		intPagesHeight = objPages.offsetHeight;
	var intHeight = intPagesHeight - intTabs * objTab.offsetHeight - blnBeautyBorderHeight;
/*
	if ((objPage.getAttribute("state") == "1") && (objPage.offsetHeight != intHeight)) {
		var s="";
		s += "Tab: " + objTab.id + "\n";
		s += "Page.offsetHeight: " + objPage.offsetHeight + "px --> " + intHeight + "px\n";
		s += "Pages.className: " + objPages.className + "\n";
		s += "Pages.offsetHeight = " + intPagesHeight + "px\n";
		s += "Pages.scrollHeight = " + objPages.scrollHeight + "px\n";
		alert(s);
	}
*/
	if (intHeight <= 0)
		objPage.setAttribute("max", 0);
	else {
		objPage.style.overflow = "hidden";
		objPage.style.visibility = "visible";
		objPage.setAttribute("max", intHeight);
		if (objPage.getAttribute("state") != "1")
			objPage.style.height = "0px";
		else if (objPage.offsetHeight != intHeight) {
			strChange += "objPage.height: " + objPage.offsetHeight + " -> " + intHeight + ";";
			objPage.style.height = intHeight + "px";
		}
		objHdr = GetFirstChild(objPage);
		objBdy = GetNextSibling(objHdr);
		objFtr = GetNextSibling(objBdy);
		var intHeaderHeight = BeautyBarGetHeight(objHdr) + intBeautyHeadingPadding;
		if (objHdr.offsetHeight != intHeaderHeight) {
			strChange += "objHdr.height: " + objHdr.offsetHeight + " -> " + intHeaderHeight + ";";
			objHdr.style.height = intHeaderHeight + "px";
		}
		var intFooterHeight = BeautyBarGetHeight(objFtr);
		if (objFtr.offsetHeight != intFooterHeight) {
			strChange += "objFtr.height: " + objFtr.offsetHeight + " -> " + intFooterHeight + ";";
			objFtr.style.height = intFooterHeight + "px";
		}
/*
		var objDebug = document.getElementById("id_debug");
		var strDebug = objDebug.innerHTML + "<br/>";
		var intPagesHeight = objPages.offsetHeight;
		var intTabsHeight = intTabs * objTab.offsetHeight;
		strDebug += "Pages: " + intPagesHeight + "px; Tabs: " + intTabsHeight + "px; Panel: " + (intPagesHeight - intTabsHeight) + "px;<br/>";
		strDebug += "Height: " + intHeight + "px; Header: " + objHdr.offsetHeight + "px; Footer: " + objFtr.offsetHeight + "px;";
		document.getElementById("id_debug").innerHTML = strDebug;
*/		
		var intScr = intHeight - objHdr.offsetHeight - objFtr.offsetHeight - intBeautyHeadingPadding;
		if (intScr < 0)
			intScr = 0;
		if (objBdy.offsetHeight != intScr) {
			strChange += "objBdy.height: " + objBdy.offsetHeight + " -> " + intScr + ";";
			objBdy.style.height = intScr + "px";
		}
//		if (strChange != "")
//			window.status = strChange;
	}
	return intHeight;
} //BeautyBarInitPage

function BeautyBarAnimate(strOpnId, strCloId)
{
	strCloId = BeautyBarMovePage(strCloId);
	strOpnId = BeautyBarMovePage(strOpnId);
	if ((strOpnId != "") || (strCloId != ""))
		setTimeout('BeautyBarAnimate("' + strOpnId + '","' + strCloId + '")', 1);
} //BeautyBarAnimate

function BeautyBarMovePage(strTabId)
{
	if (strTabId != "") {
		var objTab = document.getElementById(strTabId);
		var objPage = GetNextSibling(objTab);
		var intHeight = objPage.offsetHeight;
/*
		var objDebug = document.getElementById("id_debug");
		var strDebug = objDebug.innerHTML + "<br/>";
		var intMax = (objPage.getAttribute("max") != null) ? parseInt(objPage.getAttribute("max")) : 0;
		strDebug += "Tab: " + objTab.id + "; PageHeight: " + intHeight + "px; Max: " + intMax + "px; Move: " + intBeautyMoveBy + "px; Dir: " + objPage.getAttribute("state") + ";";
		document.getElementById("id_debug").innerHTML = strDebug;
*/
/*
		var intMax = (objPage.getAttribute("max") != null) ? parseInt(objPage.getAttribute("max")) : 0;
		var strDebug = "Tab: " + objTab.id + "; PageHeight: " + intHeight + "px; Max: " + intMax + "px; Move: " + intBeautyMoveBy + "px; Dir: " + objPage.getAttribute("state") + ";";
		window.status = strDebug;
*/
		if (objPage.getAttribute("state") == "0") {
			intHeight += intBeautyMoveBy;
			var intMax = (objPage.getAttribute("max") != null) ? parseInt(objPage.getAttribute("max")) : 0;
			if (intHeight < intMax)
				objPage.style.height = intHeight + "px";
			else {
/*
				window.status = "Completed";
*/
				objPage.style.height = intMax + "px";
				objPage.setAttribute("state","1");
				strTabId = "";
				GlassTabSet(objTab, objTab.getAttribute("selected"), "right top");
				blnBeautyInAnimate = false;
				var strAction = objPage.getAttribute("action");
				if (strAction != null)
					eval(strAction);
			}
		} 
		else {
			intHeight -= intBeautyMoveBy;
			if (intHeight > 0)
				objPage.style.height = intHeight + "px";
			else  {
				objPage.style.height = "0px";
				objPage.setAttribute("state","0");
				strTabId = "";
				GlassTabSet(objTab, objTab.getAttribute("enabled"), "left top");
				blnBeautyInAnimate = false;
			}
		}
	}
	return strTabId;
} //BeautyBarMovePage

//------------------------------------------------------
// WatermarkTextBox
//------------------------------------------------------

function WatermarkSetEmptyText(objEdt, strText) {
	if (typeof(objEdt) == "string")
		objEdt = document.getElementById(objEdt);
	if (objEdt != null) {
		var strEmpty = objEdt.getAttribute("empty");
		if (objEdt.value == strEmpty)
			objEdt.value = strText;
		objEdt.setAttribute("empty", strText);
	}
} //WatermarkSetEmptyText

function WatermarkGetValue(objEdt) {
	var strValue = "";
	var strEmpty = objEdt.getAttribute("empty");
	if (objEdt.value != strEmpty)
		strValue = objEdt.value;
	return strValue;
} //WatermarkGetValue

function WatermarkSetValue(objEdt, strValue) {
	objEdt.className = objEdt.className.replace(new RegExp("watermark\\b"), "").replace(new RegExp(" $"), "");
	objEdt.value = strValue;
	if (strValue == "") {
		objEdt.value = objEdt.getAttribute("empty");
		objEdt.className = objEdt.className + " watermark";
	}
} //WatermarkSetValue

function WatermarkFocus(objEdt, objEvent)
{
	var strEmpty = objEdt.getAttribute("empty");
	if (objEdt.value == strEmpty)
		objEdt.value = "";
	objEdt.className = objEdt.className.replace(new RegExp("watermark\\b"), "").replace(new RegExp(" $"), "");
} //WatermarkFocus

function WatermarkBlur(objEdt, objEvent)
{
	if (objEdt.value == "") {
		objEdt.value = objEdt.getAttribute("empty");
		objEdt.className = objEdt.className.replace(new RegExp("watermark\\b"), "").replace(new RegExp(" $"), "");
		objEdt.className = objEdt.className + " watermark";
	}
} //WatermarkBlur

function WatermarkKeyDown(objEdt, objEvent)
{
	if (objEvent == null)
		objEvent = event;
	if (objEvent.keyCode == 13) {
		var strOnEnter = objEdt.getAttribute("enter");
		if (strOnEnter != null) {
			var intPage = WatermarkGetValue(objEdt);
			//objEdt.value = "";
			var blnFocus = "true";
			eval("blnFocus = " + strOnEnter + "(intPage);");
			if (blnFocus)
				WatermarkFocus(objEdt, objEvent);
			objEvent.returnValue = false;
		}
	}
} //WatermarkKeyDown

//------------------------------------------------------
// Table Cursor
//------------------------------------------------------

function TableCursorOver(tagRow)
{
	if (typeof(tagRow.cells) == "unknown") {
		for (var intCell = 0; intCell < tagRow.children.length; intCell++) {
			tagRow.children[intCell].className = tagRow.children[intCell].className.replace(new RegExp("tablecursor\\b"), "").replace(new RegExp(" $"), "");
			tagRow.children[intCell].className = tagRow.children[intCell].className + " tablecursor";
		}
	}
	else {
		for (var intCell = 0; intCell < tagRow.cells.length; intCell++) {
			tagRow.cells[intCell].className = tagRow.cells[intCell].className.replace(new RegExp("tablecursor\\b"), "").replace(new RegExp(" $"), "");
			tagRow.cells[intCell].className = tagRow.cells[intCell].className + " tablecursor";
		}
	}
} //TableCursorOver

function TableCursorOut(tagRow)
{
	if (typeof(tagRow.cells) == "unknown") {
		for (var intCell = 0; intCell < tagRow.children.length; intCell++) {
			tagRow.children[intCell].className = tagRow.children[intCell].className.replace(new RegExp("tablecursor\\b"), "").replace(new RegExp(" $"), "");
		}
	}
	else {
		for (var intCell = 0; intCell < tagRow.cells.length; intCell++) {
			tagRow.cells[intCell].className = tagRow.cells[intCell].className.replace(new RegExp("tablecursor\\b"), "").replace(new RegExp(" $"), "");
		}
	}
} //TableCursorOut

//------------------------------------------------------
// Error
//------------------------------------------------------

function ErrorShowMessage(strId, intTimeOut, strMessage)
{
	var objErr = document.getElementById(strId);
	if (typeof(strMessage) != "undefined")
		objErr.innerHTML = strMessage;
	objErr.style.display = "block";
	window.setTimeout("ErrorHideMessage()", intTimeOut);
} //ErrorShowMessage

function ErrorHideMessage() {
	var aryErr = document.getElementsByTagName("CITE");
	for (var intIndex = 0; intIndex < aryErr.length; intIndex++)
		aryErr[intIndex].style.display = "none";
} //ErrorHideMessage

//------------------------------------------------------
// cross browser support
//------------------------------------------------------

function GetFirstChild(objObject)
{
	objObject = objObject.firstChild;
	if ((objObject != null) && (objObject.nodeType != 1))
		objObject = GetNextSibling(objObject);
	return objObject;
} //GetFirstChild
		
function GetNextSibling(objObject)
{
	objObject = objObject.nextSibling;
	while ((objObject != null) && (objObject.nodeType != 1))
		objObject = objObject.nextSibling;
	return objObject;
} //GetNextSibling
		
function GetPreviousSibling(objObject)
{
	objObject = objObject.previousSibling;
	while ((objObject != null) && (objObject.nodeType != 1))
		objObject = objObject.previousSibling;
	return objObject;
} //GetPreviousSibling
	
