﻿var intNetscape = -1;
var blnUseSectionSelectedStyle = false;
var blnUseMenuSelectedStyle = false;

function determineBrowser() {
	if (navigator.appName.toLowerCase() == "netscape")
		intNetscape = 1;
	else
		intNetscape = 0;
}

function GetAttribute(objObject, strName) {
	if (objObject.getAttribute("data-" + strName) != null)
		return objObject.getAttribute("data-" + strName);
	else
		return objObject.getAttribute(strName);
}

function popup(intX, intY, intWidth, intHeight, blnCenter, blnToolbar, blnResizable, blnScrollbars, strUrl) {
	var intWinX, intWinY;
	var strOptions = "";
	
	if (intNetscape == -1) determineBrowser();

	intWinX = (intNetscape == 0)? window.top.screenLeft:window.top.screenX;
	intWinY = (intNetscape == 0)? window.top.screenTop:window.top.screenY;
	
	if (blnCenter) {
		intWinX += ((window.top.document.body.clientWidth / 2) - (intWidth / 2));
		intWinY += ((window.top.document.body.clientHeight / 2) - (intHeight / 2));
	}
	else {
		intWinX += intX;
		intWinY += intY;
	}
	
	strOptions = "left=" + intWinX + ",top=" + intWinY + ",width=" + intWidth + ",height=" + intHeight;
	if (blnToolbar) 
		strOptions += ",fullscreen=0,location=1,menubar=1,status=1,toolbar=1,titlebar=1";
	else
		strOptions += ",fullscreen=0,location=0,menubar=0,status=0,toolbar=0,titlebar=0";
	strOptions += ",resizable=" + ((blnResizable)? "1":"0");
	strOptions += ",scrollbars=" + ((blnScrollbars)? "1":"0");
	
	window.open(unescape(strUrl), "", strOptions);
}

function checkFrame() {
	if (window.top == window) {
		var intPageID = -1;
		var intIndex;
		var blnFrames = true;
		var arrArguments;
		var arrValue;

		// Try to find the pageid		
		intIndex = document.URL.indexOf("?");
		if (intIndex > 0) {
			arrArguments = document.URL.substring(intIndex + 1, document.URL.length).split("&");
			for (intIndex = 0; intIndex < arrArguments.length; intIndex++) {
				if (arrArguments[intIndex].indexOf("PageID=") != -1) {
					arrValue = arrArguments[intIndex].split("=");
					if (arrValue.length == 2) intPageID = arrValue[1];
				}
				if (arrArguments[intIndex].indexOf("NoFrames=") != -1) blnFrames = false;

			}
		}
		
		if (blnFrames) {
			if (intPageID > 0)
				window.top.location.replace("/Default.aspx?PageID=" + intPageID);
			else
				window.top.location.replace("/Default.aspx");
		}
	}
	
	// Flash workaround for 'click to activate'
	if(window.ActiveXObject) { 
		var allObjects = document.body.getElementsByTagName("Object");
		for (i=0; i < allObjects.length; i++) {
			if (allObjects[i].classid == "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000") {
				allObjects[i].outerHTML = allObjects[i].outerHTML;
			}
		}
		allObjects = document.body.getElementsByTagName("embed");
		for (i=0; i < allObjects.length; i++) {
			allObjects[i].outerHTML = allObjects[i].outerHTML;
		}
	}

	CheckFormTextArea();
}

function CheckFormTextArea() {
	var arrText = document.getElementsByTagName("TEXTAREA");
	var i;

	for (i = 0; i < arrText.length; i++) {
		if (GetAttribute(arrText[i], "maxlength")) {
			arrText[i].onkeydown = arrText[i].onkeyup = arrText[i].onchange = new Function("return CheckFormTextAreaMaxLength(event, this);");
		}
	}
}

function CheckFormTextAreaMaxLength(objEvent, objText) {
	var intMaxLength = GetAttribute(objText,'maxlength');
	var intCode;

	if (!objEvent) objEvent = event;
	intCode = (typeof objEvent.charCode != "undefined") ? objEvent.charCode : objEvent.keyCode;

	if (intCode >= 47) {
		if (objText.value.length >= intMaxLength) {
			if (objText.value.length > intMaxLength) objText.value = objText.value.substr(0, intMaxLength);
			return false;
		}
	}
	else
		if (objText.value.length > intMaxLength) objText.value = objText.value.substr(0, intMaxLength);
	return true;
}

function checkPageFrame(intPageID) {
	if (window.top == window) {
		if (intPageID > 0)
			window.top.location.replace("/Default.aspx?PageID=" + intPageID);
		else
			window.top.location.replace("/Default.aspx");
	}
}

function clickLink(objLink) {
	if (intNetscape == -1) determineBrowser();
	
	if (objLink.href.indexOf("javascript:") == -1) {
		if (intNetscape == 0) {
			objLink.click();
		}
		else {
			window.open(objLink.href, objLink.target);
		}
	}
	else {
		eval(objLink.href.substring(11));
	}
}

/*
 * Login 
 */
function LoginCheckKey(objEvent, intPageID) {
	if (objEvent.keyCode == 13) {
		LoginSubmit(intPageID);
		objEvent.returnValue = false;
	}
	else {
		objEvent.returnValue = true;
	}
}  
 
function LoginSubmit(intPageID) {
	var objForm;
	
	objForm = document.getElementById("formlogin" + intPageID);
	if (objForm != null) objForm.submit();

	return false;
}

/*
 * Section functions
 */
function SectionCombinedOver(objSender, objItem) {
	var objTD;
	
	objTD = document.getElementById("GT" + objItem.get_item().get_value())
	if (objTD != null) objTD.className = (blnUseSectionSelectedStyle) ? "section_text_selected" : "section_text_active";
}

function SectionCombinedOut(objSender, objItem) {
	var objTD;
	
	objTD = document.getElementById("GT" +  objItem.get_item().get_value())
	if (objTD != null) objTD.className = "section_text_idle";
}

/*
 * Menu functions
 */
function MenuSetSelectedItem(objItem) {
	var objParentItem;
	
	MenuClearSelectedItem(objItem.get_parentMenu());
	if (objItem == null) return;

	objItem.IsSelected = true;
	objParentItem = objItem.get_parentItem();
	while (objParentItem != null) {
		objParentItem.IsChildSelected = true;
		objParentItem = objParentItem.get_parentItem();
	}

	objItem.ParentMenu.SelectedItem = objItem;
}

function MenuClearSelectedItem(objMenu) {
	var objParentItem;
	var objSelectedItem = objMenu.SelectedItem;

	if (objSelectedItem == null) return;

	objSelectedItem.IsSelected = false;
	objParentItem = objSelectedItem.get_parentItem();
	while (objParentItem != null) {
		objParentItem.IsChildSelected = false;
		objParentItem = objParentItem.get_parentItem();
	}
	
	objMenu.SelectedItem = null;
}

function MenuItemSelect(objSender, objItem) {
	MenuSetSelectedItem(objItem.get_item());
	objItem.get_item().get_parentMenu().render();
	return true;
}

/*
 * Body
 */
function InsertSmiley(strCode) {
	document.getElementById("Message").value += strCode;
}
 
function FlexonyFullPrint() {
	if (window.top.print) {
		window.top.print();
	}
} 
function FlexonyPrint() {
	if (window.print) {
		window.print();
	}
}

function FlexonyPDF() {
	FlexonyFullPDF();
}

function FlexonyFullPDF() {
	var objForm;
	var objText;
	
	objForm = document.createElement("form");
	objForm.method = "post";
	objForm.style.display = "none";	
	objForm.action = "../Site/PDF.aspx" + window.location.href.substring(window.location.href.indexOf("?"));
	objText = document.createElement("textarea");
	objText.name = "pagehtml";
	objText.value = document.getElementsByTagName("HTML")[0].innerHTML;
	objForm.appendChild(objText);
	document.body.appendChild(objForm);
	objForm.submit();
}

function FlexonyLogin() {
	window.location.href = "../Site/Login.aspx";
}

function FlexonyLogout() {
	window.location.href = "../Site/Logout.aspx";
}

function FlexonyRegister() {
	window.location.href = "../Site/Register.aspx";
}

function FlexonyPassword() {
	window.location.href = "../Site/Password.aspx";
}

function FlexonyProfile() {
	window.location.href = "../Site/Profile.aspx";
}

var strFlexonyPopupPreviousMargin = "";
var strFlexonyPopupPreviousOverflow = "";
function FlexonyMailAFriend() {
	FlexonyOpenPopupWindow("../Site/MailAFriend.aspx?PageID=" + GivePageID());
}

function FlexonyLoginPopup() {
	FlexonyOpenPopupWindow("../Site/LoginPopup.aspx?PageID=" + GivePageID());
}

function FlexonyOpenPopupWindow(strUrl) {
	var objDiv, objFrame;

	if (typeof document.body.scroll != "undefined") {
		strFlexonyPopupPreviousMargin = document.body.style.marginRight;
		strFlexonyPopupPreviousOverflow = document.body.style.overflow;
		document.body.scroll = "no";
		document.body.style.overflow = "hidden";
		document.body.style.marginRight = "17px";
	}
	else {
		strFlexonyPopupPreviousOverflow = document.body.style.overflow;
		document.body.style.overflow = "hidden";
	}
	GetWindowDimensions();
	objDiv = document.getElementById("FlexonyPopupWindow");
	if (objDiv == null) {
		objFrame = document.createElement("iframe");
		objFrame.allowTransparency = true;
		objFrame.frameBorder = "0"
		objFrame.src = strUrl;
		objFrame.style.width = "100%";
		objFrame.style.height = "100%";

		objDiv = document.createElement("div");
		objDiv.id = "FlexonyPopupWindow";
		objDiv.style.position = "absolute";
		objDiv.style.left = 0;
		objDiv.style.top = 0;
		objDiv.style.width = intTipWindowWidth + "px";
		objDiv.style.height = "100%";
		objDiv.style.display = "none";
		objDiv.style.zIndex = "80000";
		objDiv.appendChild(objFrame);
		document.body.appendChild(objDiv);
	}
	else {
		objDiv.firstChild.src = strUrl;
		objDiv.style.display = "";
		objDiv.focus();
	}
}

function FlexonyClosePopupWindow() {
	var objDiv;

	objDiv = document.getElementById("FlexonyPopupWindow");
	if (objDiv != null) {
		while (objDiv.childNodes[0]) {
			objDiv.removeChild(objDiv.childNodes[0]);
		}
		objDiv.parentNode.removeChild(objDiv);
	}
	if (typeof document.body.scroll != "undefined") {
		document.body.style.marginRight = strFlexonyPopupPreviousOverflow;
		document.body.scroll = "";
	}
	else {
		document.body.style.overflow = strFlexonyPopupPreviousOverflow;
	}
}

var objTipObject = null;
var intTipMousePosX = 0;
var intTipMousePosY = 0;
var intTipWindowWidth = 0;
var intTipWindowHeight = 0;
var intTipFadeTimer = 0;
var intTipHideTimer = 0;

function GetMousePosition(objEvent) {
	if (GetAttribute(objTipObject,"tipmode") == "0") {
		if (objEvent.pageX || objEvent.pageY) {
			intTipMousePosX = objEvent.pageX;
			intTipMousePosY = objEvent.pageY;
		}
		else if (objEvent.clientX || objEvent.clientY) {
			intTipMousePosX = objEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			intTipMousePosY = objEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}
	else {
		var objElm;

		intTipMousePosX = 0;
		intTipMousePosY = 0;
		objElm = (objEvent.srcElement) ? objEvent.srcElement : objEvent.target;
		if (objElm.tagName.toLowerCase() == "area") {
			// find area position, tricky!!
			var arrCoords;
			var objImgs = document.getElementsByTagName("IMG");
			var objImg = null;
			var strMapName;

			strMapName = '#' + objElm.parentNode.name;
			if (objElm.shape.toLowerCase() == "rect") {
				arrCoords = objElm.coords.split(",")
				if (arrCoords.length >= 2) {
					intTipMousePosX += parseInt(arrCoords[0]);
					intTipMousePosY += parseInt(arrCoords[1]);
				}
			}
			for (i = 0; i < objImgs.length; i++) {
				if (objImgs[i].getAttribute("useMap") == strMapName) {
					objImg = objImgs[i];
					break;
				}
			}
			while (objImg != null) {
				intTipMousePosX += objImg.offsetLeft;
				intTipMousePosY += objImg.offsetTop;
				objImg = objImg.offsetParent;
			}
		}
		else {
			if (objElm.offsetParent) {
				while (objElm != null) {
					intTipMousePosX += objElm.offsetLeft;
					intTipMousePosY += objElm.offsetTop;
					objElm = objElm.offsetParent;
				}
			}
		}
	}
}

function GetWindowDimensions() {
	if (typeof (window.innerWidth) == 'number') {
		intTipWindowWidth = window.innerWidth;
		intTipWindowHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		intTipWindowWidth = document.documentElement.clientWidth;
		intTipWindowHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		intTipWindowWidth = document.body.clientWidth;
		intTipWindowHeight = document.body.clientHeight;
	}
}

function SetTipPosition(objDiv) {
	var intWidth = eval(GetAttribute(objTipObject, "tipwidth"));
	var intHeight = eval(GetAttribute(objTipObject, "tipheight"));
	var intOffsetX = eval(GetAttribute(objTipObject, "tipoffsetx"));
	var intOffsetY = eval(GetAttribute(objTipObject, "tipoffsety"));
	var intTipX = 0;
	var intTipY = 0;

	if (GetAttribute(objTipObject, "tipmode") == "0") {
		if ((intTipMousePosX + intOffsetX - document.body.scrollLeft + intWidth) > intTipWindowWidth) {
			intTipX = (intTipMousePosX - ((intTipMousePosX + intOffsetX - document.body.scrollLeft + intWidth) - intTipWindowWidth))
		}
		else {
			intTipX = (intTipMousePosX + intOffsetX);
		}
		if ((intTipX - document.body.scrollLeft) < 0) intTipX = document.body.scrollLeft;

		if ((intTipMousePosY + intOffsetY - document.body.scrollTop + intHeight) > intTipWindowHeight) {
			intTipY = (intTipMousePosY - intOffsetY - intHeight);
		}
		else {
			intTipY = (intTipMousePosY + intOffsetY);
		}
		if ((intTipY - document.body.scrollTop) < 0) intTipY = document.body.scrollTop;
	}
	else {
		intTipX = (intTipMousePosX + intOffsetX);
		intTipY = (intTipMousePosY + intOffsetY);
	}
	objDiv.style.left = intTipX + 'px';
	objDiv.style.top = intTipY + 'px';
}

function ShowTip(objObject, objEvent) {
	var objDiv;

	if (!objEvent) objEvent = window.event;
	objTipObject = objObject;
	objDiv = document.getElementById("flexonytipwindow");
	if (intTipHideTimer != 0) window.clearTimeout(intTipHideTimer);
	if (objDiv == null) {
		GetWindowDimensions();
		GetMousePosition(objEvent);
		objFrame = document.createElement("iframe");
		objFrame.allowTransparency = true;
		objFrame.frameBorder = "0"
		objFrame.scrolling = "no";
		objFrame.marginHeight = "0";
		objFrame.marginWidth = "0";
		objFrame.src = GetAttribute(objTipObject, "tiphref");
		objFrame.style.width = GetAttribute(objTipObject, "tipwidth") + "px";
		objFrame.style.height = GetAttribute(objTipObject, "tipheight") + "px";

		objDiv = document.createElement("div");
		objDiv.id = "flexonytipwindow";
		objDiv.style.position = "absolute";
		objDiv.style.width = objFrame.style.width;
		objDiv.style.height = objFrame.style.height;
		objDiv.style.border = GetAttribute(objTipObject, "tipborder");
		objDiv.style.opacity = 0;
		objDiv.style.filter = "alpha(opacity=0)";
		objDiv.style.display = "block";
		objDiv.style.zIndex = "80000";
		objDiv.onmouseover = new Function("if (intTipHideTimer != 0) window.clearTimeout(intTipHideTimer);");
		objDiv.onmouseout = new Function("HideTip();");
		objDiv.appendChild(objFrame);
		SetTipPosition(objDiv);

		document.body.appendChild(objDiv);
		intTipFadeTimer = window.setTimeout("FadeInTip(100);", 100);
	}
	else {
		objDiv.childNodes[0].src = GetAttribute(objTipObject, "tiphref");
		objDiv.childNodes[0].style.width = GetAttribute(objTipObject, "tipwidth") + "px";
		objDiv.childNodes[0].style.height = GetAttribute(objTipObject, "tipheight") + "px";
		objDiv.style.width = objDiv.childNodes[0].style.width;
		objDiv.style.height = objDiv.childNodes[0].style.height;
		objDiv.style.border = GetAttribute(objTipObject, "tipborder");
		GetMousePosition(objEvent);
		SetTipPosition(objDiv);
		FadeInTip(100);
	}
}

function ShowTipText(objObject, objEvent, strClass, strText) {
	var objDiv;

	if (!objEvent) objEvent = window.event;
	objTipObject = objObject;
	objDiv = document.getElementById("flexonytipwindow");
	if (intTipHideTimer != 0) window.clearTimeout(intTipHideTimer);
	if (objDiv == null) {
		GetWindowDimensions();
		GetMousePosition(objEvent);

		objDiv = document.createElement("div");
		objDiv.id = "flexonytipwindow";
		objDiv.className = strClass;
		objDiv.innerHTML = strText;
		objDiv.style.position = "absolute";
		objDiv.style.opacity = 0;
		objDiv.style.filter = "alpha(opacity=0)";
		objDiv.style.display = "block";
		objDiv.style.zIndex = "80000";
		objDiv.onmouseover = new Function("if (intTipHideTimer != 0) window.clearTimeout(intTipHideTimer);");
		objDiv.onmouseout = new Function("HideTip();");
		SetTipPosition(objDiv);

		document.body.appendChild(objDiv);
		intTipFadeTimer = window.setTimeout("FadeInTip(100);", 100);
	}
	else {
		objDiv.className = strClass;
		objDiv.innerHTML = strText;
		GetMousePosition(objEvent);
		SetTipPosition(objDiv);
		FadeInTip(100);
	}
}

function FadeInTip(intValue) {
	var objDiv;
	objDiv = document.getElementById("flexonytipwindow");
	window.clearTimeout(intTipFadeTimer);
	if (objDiv != null) {
		if (intValue > 0) {
			objDiv.style.opacity = (100 - intValue) / 100;
			objDiv.style.filter = "alpha(opacity=" + (100 - intValue) + ")";
			intTipFadeTimer = window.setTimeout("FadeInTip(" + (intValue - 40) + ");", 50);
		}
		else {
			objDiv.style.opacity = 1;
			objDiv.style.filter = "alpha(opacity=100)";
			intTipFadeTimer = 0
		}
	}
}

function FadeOutTip(intValue) {
	var objDiv;
	objDiv = document.getElementById("flexonytipwindow");
	window.clearTimeout(intTipFadeTimer);
	if (objDiv != null) {
		if (intValue > 0) {
			objDiv.style.opacity = intValue / 100;
			objDiv.style.filter = "alpha(opacity=" + intValue + ")";
			intTipFadeTimer = window.setTimeout("FadeOutTip(" + (intValue - 40) + ");", 50);
		}
		else {
			objDiv.removeChild(objDiv.childNodes[0]);
			objDiv.parentNode.removeChild(objDiv);
			objDiv = null;
			intTipFadeTimer = 0
		}
	}
}

function MoveTip(objEvent) {
	objDiv = document.getElementById("flexonytipwindow");
	if (objDiv != null) {
		if (GetAttribute(objTipObject, "tipmode") == "0") {
			GetMousePosition(objEvent);
			SetTipPosition(objDiv);
		}
	}
}

function HideTip() {
	var objDiv;

	objDiv = document.getElementById("flexonytipwindow");
	if (objDiv != null) {
		if (intTipHideTimer != 0) window.clearTimeout(intTipHideTimer);
		if (GetAttribute(objTipObject, "tipmode") == "0")
			intTipHideTimer = window.setTimeout("FadeOutTip(100);", 1);
		else
			intTipHideTimer = window.setTimeout("FadeOutTip(100);", 200);
	}
}
