﻿//------------------------------------------------------
// 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"));
} //GlassButtonOver

function GlassButtonOut(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("enabled"));
} //GlassButtonOut

function GlassButtonDown(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("down"));
} //GlassButtonDown

function GlassButtonUp(objBtn) {
	if (objBtn.getAttribute("state") != "off")
		GlassButtonSet(objBtn, objBtn.getAttribute("enabled"));
} //GlassButtonUp

function GlassButtonEnable(strId, blnEnable) {
	var objBtn = document.getElementById(strId);
	if (objBtn != null) {
		GlassButtonSet(objBtn, (blnEnable) ? objBtn.getAttribute("enabled") : objBtn.getAttribute("off"));
		objBtn.disabled = (!blnEnable);
		objBtn.setAttribute("state", (blnEnable) ? "on" : "off");
		objBtn.style.cursor = (blnEnable) ? "pointer" : "default";
	}
} //GlassButtonEnable

function GlassButtonSet(objBtn, strSrc) {
	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"));
} //GlassTabOver

function GlassTabOut(objTab) {
	if (objTab.getAttribute("active") == "False")
		GlassTabSet(objTab, objTab.getAttribute("enabled"));
	else
		GlassTabSet(objTab, objTab.getAttribute("selected"));
} //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"));
			objTab.setAttribute("active", "True");
			objTab.style.cursor = "default";
		}
		else if ((objTab.nodeType != 3) && (objTab.tagName == "IMG")) {
			GlassTabSet(objTab, objTab.getAttribute("enabled"));
			objTab.setAttribute("active", "False");
			objTab.style.cursor = "pointer";
		}
		objTab = GetNextSibling(objTab);
	} //while
	if ((blnExecute) && (blnChanged)) {
		var strAction = objSelectedTab.getAttribute("action");
		if (strAction != null)
			eval(strAction);
	}
} //GlassTabClick

function GlassTabSet(objTab, strSrc) {
	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;

function BeautyBarOnLoad() {
} //BeautyBarOnLoad

function BeautyBarResize() {
	var objBar = document.getElementById("beautybar");
	if (objBar != null) {
		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;
		var objHdr = GetFirstChild(objBar);
		var objBdy = GetNextSibling(objHdr);
		var objFtr = GetNextSibling(objBdy);
		intHeight = intHeight - BeautyBarGetHeight(objHdr) - BeautyBarGetHeight(objFtr) - 15;
		if (intHeight >= 0) {
			objBdy.style.height = intHeight + "px";
			var objTab = GetFirstChild(objBdy);
			while (objTab != null) {
				if ((objTab.nodeType != 3) && (objTab.tagName == "IMG")) {
					objPage = GetNextSibling(objTab);
					objPage.removeAttribute("max");
					if (objPage.getAttribute("state") == "1")
						BeautyBarInitPage(objPage)
				}
				objTab = GetNextSibling(objTab);
			} //while
		}
	}
} //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 != 3) {
			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) {
		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 != 3) && (objDiv != objTab) && (objDiv.className == "beautytab")) {
					objPage = GetNextSibling(objDiv);
					if (objPage.getAttribute("state") == "1")
						strCloId = objDiv.id;
				}
				objDiv = GetNextSibling(objDiv);
			} //while
			blnBeautyInAnimate = true;
			setTimeout('BeautyBarAnimate("' + objTab.id + '","' + strCloId + '")', 1);
		}
	}
} //BeautyBarSelect

function BeautyBarInitPage(objPage)
{
	var objPages = objPage.parentNode;
	var objTab = GetPreviousSibling(objPage);
	var intTabs = parseInt(objPages.getAttribute("tabs"));
	var intHeight = objPages.offsetHeight - intTabs * objTab.offsetHeight;
	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 = intHeight + "px";
		objHdr = GetFirstChild(objPage);
		objBdy = GetNextSibling(objHdr);
		objFtr = GetNextSibling(objBdy);
		objHdr.style.height = (BeautyBarGetHeight(objHdr) + 7) + "px";
		objFtr.style.height = BeautyBarGetHeight(objFtr) + "px";
		var intScr = intHeight - objHdr.offsetHeight - objFtr.offsetHeight - 7;
		if (intScr < 0)
			intScr = 0;
		objBdy.style.height = intScr + "px";
	}
	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); //SlideBody
		var intHeight = parseInt(objPage.style.height);
		if (objPage.getAttribute("state") == "0")  {
			intHeight += intBeautyMoveBy;
			var intMax = parseInt(objPage.getAttribute("max"));
			if (intHeight < intMax)
				objPage.style.height = intHeight + "px";
			else {
				objPage.style.height = intMax + "px";
				objPage.setAttribute("state","1");
				strTabId = "";
				GlassTabSet(objTab, objTab.getAttribute("selected"));
				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"));
				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

