// Variables
var __siteadmin_cmsAdminMode = false;

var COL_DELIMITER = String.fromCharCode(18);
var ROW_DELIMITER = String.fromCharCode(17);
var __siteadmin_cmsVars = null;

var __siteadmin_cmsPageLoaded = false;

window.onload = __siteadmin_cms_Page_OnLoad;

function __siteadmin_cms_Page_OnLoad()
{
	var sLoadHandlers = GetVar('__siteadmin_cms_PageLoad');
	if (sLoadHandlers != null)
		eval(sLoadHandlers);

	if (__siteadmin_cmsAdminMode)
	{
		document.body.onscroll = RefreshMessages;
		RefreshMessages();
	}

	__siteadmin_cmsPageLoaded = true;
}

// Initial focus
function SetInitialFocus(sID)
{
	var oCtl = GetByID(sID);	
	if (oCtl != null && CanReceiveFocus(oCtl))
		oCtl.focus();
}

function CanReceiveFocus(e)
{
	if (e.style.display != 'none' && e.tabIndex > -1 && e.disabled == false && e.style.visible != 'hidden')
	{
		var eParent = e.parentElement;
		while (eParent != null && eParent.tagName != 'BODY')
		{
			if (eParent.style.display == 'none' || eParent.disabled || eParent.style.visible == 'hidden')
				return false;
			eParent = eParent.parentElement;
		}
		return true;
	}
	else
		return false;
}

// Scrolling
function ElementTop(eSrc)
{
	if (eSrc != null)
	{
		var iTop = 0;
		var eParent;
		eParent = eSrc;

		while (eParent.tagName.toUpperCase() != "BODY")
		{
			iTop += eParent.offsetTop;
			eParent = eParent.offsetParent;
			if (eParent == null)
				break;
		}

		return iTop;
	}
	else
		return 1;
}

function ElementLeft(eSrc)
{
	if (eSrc != null)
	{
		var iLeft = 0;
		var eParent;
		eParent = eSrc;

		while (eParent.tagName.toUpperCase() != "BODY")
		{
			iLeft += eParent.offsetLeft;
			eParent = eParent.offsetParent;
			if (eParent == null)
				break;
		}

		return iLeft;
	}
	else
		return 1;
}

function ElementHeight(eSrc)
{
	if (eSrc != null)
	{
		if (eSrc.offsetHeight == null || eSrc.offsetHeight == 0)
		{
			if (eSrc.offsetParent.offsetHeight == null || eSrc.offsetParent.offsetHeight == 0)
			{
				if (eSrc.offsetParent.offsetParent != null)
					return eSrc.offsetParent.offsetParent.offsetHeight; //needed for Konqueror
				else
					return 0;
			}
			else
				return eSrc.offsetParent.offsetHeight;
		}
		else
			return eSrc.offsetHeight;
	}
	else
		return 0;
}

function ElementWidth(eSrc)
{
	if (eSrc != null)
	{
		if (eSrc.offsetWidth == null || eSrc.offsetWidth == 0)
		{
			if (eSrc.offsetParent.offsetWidth == null || eSrc.offsetParent.offsetWidth == 0)
			{
				if (eSrc.offsetParent.offsetParent != null)
					return eSrc.offsetParent.offsetParent.offsetWidth; //needed for Konqueror
				else
					return 0;
			}
			else
				return eSrc.offsetParent.offsetWidth
		}
		else
			return eSrc.offsetWidth;
	}
	else
		return 0;
}

function SaveScrollTop()
{
	document.forms[0].__siteadmin_cms_ScrollTop.value = GetBodyScrollTop();
}

function SetScrollTop(iTop)
{
	if (iTop == null)
	{
		if (document.forms[0].__siteadmin_cms_ScrollToControl.value.length > 0)
		{
			var oCtl = GetByID(document.forms[0].__siteadmin_cms_ScrollToControl.value);
			if (oCtl != null)
			{
				iTop = ElementTop(oCtl);
				document.forms[0].__siteadmin_cms_ScrollToControl.value = '';
			}
		}
	}

	if (iTop == null)
		iTop = document.forms[0].__siteadmin_cms_ScrollTop.value;

	if (iTop != null && iTop > 0)
		document.body.scrollTop = iTop;
}

function GetBodyScrollTop()
{
	if (document.body.scrollTop != null)
		return document.body.scrollTop;
	else
		return 1;
}

function GetBodyScrollLeft()
{
	if (document.body.scrollLeft != null)
		return document.body.scrollLeft;
	else
		return 1;
}

// Client Variables
function GetVars()
{
	if (__siteadmin_cmsVars == null)
	{
		__siteadmin_cmsVars = new Array();
		var oCtl = GetByID('__siteadmin_cms_Vars');
		if (oCtl != null)
		{
			var aryItems = oCtl.value.split(ROW_DELIMITER);
			for (var i = 0; i < aryItems.length; i++)
			{
				var aryItem = aryItems[i].split(COL_DELIMITER);
				
				if (aryItem.length == 2)
					__siteadmin_cmsVars[aryItem[0]] = aryItem[1];
			}
		}
	}
	return __siteadmin_cmsVars;
}

function GetVar(sKey)
{
	return GetVars()[sKey];
}

function SetVar(sKey, sVal)
{			
	if (__siteadmin_cmsVars == null)
		GetVars();

	__siteadmin_cmsVars[sKey] = sVal;

	var oCtl = GetByID('__siteadmin_cms_Vars');
	if (oCtl == null)
	{
		oCtl = CreateElement('INPUT');
		oCtl.type = 'hidden';
		oCtl.id = '__siteadmin_cms_Vars';
		AppendChild(GetByTagName("body")[0], oCtl);
	}

	var sVals = '';
	var sKey;
	for (sKey in __siteadmin_cmsVars)
	{
		sVals += ROW_DELIMITER + sKey + COL_DELIMITER + __siteadmin_cmsVars[sKey];
	}
	oCtl.value = sVals;

	return true;
}

// Custom
function GetByID(sID, oCtl)
{
	if (oCtl == null)
		oCtl = document;
	if (oCtl.getElementById)
		return oCtl.getElementById(sID);
	else
		return oCtl.all(sID);
}

function GetByTagName(sTag, oCtl)
{
	if (oCtl == null)
		oCtl = document;
	if (oCtl.getElementsByTagName)
		return oCtl.getElementsByTagName(sTag);
	else if (oCtl.all.tags)
		return oCtl.all.tags(sTag);
	else
		return null;
}

function CreateElement(sTagName)
{
	if (document.createElement)
		return document.createElement(sTagName);
	else 
		return null;
}

function AppendChild(oParent, oChild) 
{
	if (oParent.appendChild) 
		return oParent.appendChild(oChild);
	else 
		return null;
}

function RemoveChild(oChild) 
{
	if (oChild.parentNode.removeChild) 
		return oChild.parentNode.removeChild(oChild);
	else 
		return null;
}

// Cookies
function SetCookie(sName, sVal, iHours, sPath, sDomain, bSecure) 
{
	var dExpires;
	if (iHours)
	{
		dExpires = new Date();
		dExpires.setTime(dExpires.getTime() + (iHours * 60 * 60 * 1000));
	}
	document.cookie = sName + '=' + escape(sVal) +
			  ((dExpires) ? '; expires=' + dExpires.toGMTString() : '') +
			  ((sPath) ? '; path=' + sPath : '') +
			  ((sDomain) ? '; domain=' + sDomain : '') +
			  ((bSecure) ? '; secure' : '');
	
	if (document.cookie.length > 0)
		return true;
}

function GetCookie(sName) 
{
	var sCookie = ' ' + document.cookie;
	var sSearch = ' ' + sName + '=';
	var sStr = null;
	var iOffset = 0;
	var iEnd = 0;

	if (sCookie.length > 0) 
	{
		iOffset = sCookie.indexOf(sSearch);
		if (iOffset != -1)
		{
			iOffset += sSearch.length;
			iEnd = sCookie.indexOf(';', iOffset)
			if (iEnd == -1)
				iEnd = sCookie.length;
			sStr = unescape(sCookie.substring(iOffset, iEnd));
		}
	}

	return sStr;
}

function DeleteCookie(sName, sPath, sDomain) 
{
	if (GetCookie(sName))
	{
		SetCookie(sName, '', -1, sPath, sDomain);
		return true;
	}

	return false;
}

// Section max/min
function SectionMaxMin(oBtn, sContentID)
{
	var oContent = GetByID(sContentID);

	if (oContent != null)
	{
		var sMaxIcon = oBtn.getAttribute('max_icon');
		var sMinIcon = oBtn.getAttribute('min_icon');

		if (oContent.style.display == 'none')
		{
			oBtn.src = sMinIcon;
			oContent.style.display = '';
			SetVar(oBtn.id + ':exp', 'true');
		}
		else
		{
			oBtn.src = sMaxIcon;
			oContent.style.display = 'none';
			SetVar(oBtn.id + ':exp', 'false');
		}

		//return true;
	}
	
	//return false;
}

function toggle(strName, picName, mdata, mpic) {
 var imgObj = eval('document.'+picName);
 if (document.getElementById(strName).style.display=='')
 {
   document.getElementById(strName).style.display='none';
   imgObj.src="img/expb.gif";
 }
 else
 {
    document.getElementById(strName).style.display='';
	imgObj.src="img/expbd.gif";
	for (i=0; i<mdata.length; i++)
	  if(mdata[i] != strName)
	  {
        document.getElementById(mdata[i]).style.display='none';
		var imgObj2 = eval('document.'+mpic[i]);		
        imgObj2.src="img/expb.gif";		
	  }
 }
}

function lighticon(id) {
	var elem = document.getElementById(id);
	var re = new RegExp("_g.gif", "g");
	elem.src = elem.src.replace(re, ".gif");
}

function darkenicon(id) {
	var elem = document.getElementById(id);
	var re = new RegExp(".gif", "g");
	elem.src = elem.src.replace(re, "_g.gif");
}


function lightregion(id) {
	lighticon(id);
	var elem = document.getElementById("h"+id);
	elem.style.background = "#e6e6dc";
}

function darkenregion(id) {
	darkenicon(id);
	var elem = document.getElementById("h"+id);
	elem.style.background = "";
}

