// JavaScript Document
var picBG = undefined;
var picBox = undefined;
var alertBG = undefined;
var alertBox = undefined;
var global_curErrBox = undefined;
//var strWhichone = "<%=whichone%>";

var ecOpen = "<EXECCMD><!--";
var ecClose = "--></EXECCMD>";


function global_getElementAbsoluteY(el)
{
	var rtn = 0;
	while (el != null)
	{
		rtn += el.offsetTop;
		el = el.offsetParent;
	}
	return rtn;
}
function global_getElementAbsoluteX(el)
{
	var rtn = 0;
	while (el != null)
	{
		rtn += el.offsetLeft;
		el = el.offsetParent;
	}
	return rtn;
}
function global_closeFormErrBox()
{
	if (global_curErrBox != undefined)
		document.body.removeChild(global_curErrBox);

	global_curErrBox = undefined;
}
function global_showFormErrBox(errEl, message)
{
	global_closeFormErrBox();

	if (errEl == undefined)
		alert(message);
	else
	{
		var eDiv = document.createElement("div");
		eDiv.className = "FormErrBox";

		var x = global_getElementAbsoluteX(errEl) + errEl.offsetWidth;
		var y = global_getElementAbsoluteY(errEl) + (errEl.offsetHeight/2);
		var w = errEl.offsetWidth;
		var h = errEl.offsetHeight;

		var eMsg = document.createElement("span");
		eMsg.style.zIndex = 210;
		eMsg.innerText = message;
		eMsg.innerHTML = message;  //+ "&nbsp;<img src='https://cdn.tomwaterhouse.com/site/assets/images/errbox_cross.gif' style='cursor:pointer' onclick='global_closeFormErrBox();' alt='OK' />";

		eDiv.appendChild(eMsg);
		document.body.appendChild(eDiv);

		eDiv.style.left = x + "px";
		eDiv.style.top = (y - (eDiv.offsetHeight / 2)) + "px";
		eDiv.style.zIndex = 202;
		//eDiv.style.height = "40px";

//		alert(x + "#" + y + "#" + w + "#" + h + "#" + eDiv.offsetHeight);
//1305#768#450#22#28
		global_curErrBox = eDiv;
	}
}
function global_UpdateUpcoming(str) {
	var el = document.getElementById("upcoming");
	if (el != null)
		el.innerHTML = str;
}

function global_AddOnLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
function AddOnLoadEvent(func) {
	global_AddOnLoadEvent(func);
}

function cleanReturnedScripts(s) {
	ns = s.indexOf(ecOpen);
	rtn = "";
	while (ns >= 0) {
		rtn += s.substring(0, ns);
		s = s.substring(ns + ecOpen.length);
		ne = s.indexOf(ecClose);

		if (ne >= 0) {
			cmd = s.substring(0, ne);
			s = s.substring(cmd.length + ecClose.length);
		}
		ns = s.indexOf(ecOpen);
	}
	rtn += s;

	return rtn;
}

function runReturnedScripts(s) {
	ns = s.indexOf(ecOpen);
	rtn = "";
	while (ns >= 0) {
		rtn += s.substring(0, ns);
		s = s.substring(ns + ecOpen.length);
		ne = s.indexOf(ecClose);
		
		if (ne >= 0) {
			cmd = s.substring(0, ne);
			//alert(cmd);
			try {
				eval(cmd);
			}
			catch (err) {
				//DO NOTHING
			}
			s = s.substring(cmd.length + ecClose.length);
		}
		ns = s.indexOf(ecOpen);
	}
	rtn += s;
	
	return rtn;
}

function ClosePopupWindow()
{
	global_ClosePopupWindow();
}

function global_ClosePopupWindow()
{
	if (picBG != undefined)
	{
		document.body.removeChild(picBG);
		picBG = undefined;
	}
	if (picBox != undefined) {
		document.body.removeChild(picBox);
		picBox = undefined;
	}
}

function global_CloseBankingPopupWindow(whichone)
{
	if (picBG != undefined)
	{
		document.body.removeChild(picBG);
		picBG = undefined;
	}
	
	if (picBox != undefined)
	{
		document.body.removeChild(picBox);
		picBox = undefined;
	}

	//alert(strWhichone);

	if (whichone == "CardDeposit")
	{
		parent.RefreshCC_Deposits();
	}
}

function ShowPopupWindow(message, w, h)
{
	global_ShowPopupWindow(message);
}

function global_ShowPopupWindow(message) 
{
	message = runReturnedScripts(message);
	message = cleanReturnedScripts(message);

	picBG = AddshowPopupBG("showPopupBG");
	picBox = AddshowPopupBox(message, "showPopupBox", w, h);

	if (self.innerHeight)
	{ //FF
		centerX = this.innerWidth;
		centerY = this.innerHeight;
		scrollX = 0;
		scrollY = window.scrollY;
	} else if (document.documentElement && document.documentElement.clientHeight)
	{ //IE 6+
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	} else if (document.body)
	{
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}
	document.body.appendChild(picBG);
	document.body.appendChild(picBox);

	w = picBox.offsetWidth;
	if (w > 800)
	{
		picBox.style.width = "800px"; //centerX / 2;
	}
	picBox.style.left = (scrollX + ((centerX / 2) - (picBox.offsetWidth / 2))) + "px";
	picBox.style.top = (scrollY + ((centerY / 2) - (picBox.offsetHeight / 2))) + "px";
}

function AddshowPopupBox(message, theClass, w, h) {
	var centerX = 200;
	var centerY = 200;
	var scrollX = 0;
	var scrollY = 0;
	if (self.innerHeight) { //FF
		centerX = this.innerWidth;
		centerY = this.innerHeight;
		scrollX = 0;
		scrollY = window.scrollY;
	} else if (document.documentElement && document.documentElement.clientHeight) { //IE 6+
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	} else if (document.body) {
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}

	var eDiv = document.createElement("div");
	//eDiv.style.width = w + "px";
	eDiv.style.left = (scrollX + ((centerX / 2) - (w/2))) + "px";
	eDiv.style.top = (scrollY + ((centerY / 2) - (h/2))) + "px";
	eDiv.style.zIndex = 102;
	eDiv.className = theClass;

	var eMsg = document.createElement("span");
	eMsg.style.zIndex = 110;
	eMsg.innerText = message;
	eMsg.innerHTML = message;
	eDiv.appendChild(eMsg);

	return eDiv;

}

function AddshowPopupBG(theClass) {
	h = document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight;
	w = document.body.scrollWidth > document.body.offsetWidth ? document.body.scrollWidth : document.body.offsetWidth;

	var eDiv = document.createElement("div");
	eDiv.style.width = w + "px";
	eDiv.style.height = h + "px";
	eDiv.style.left = 0;
	eDiv.style.top = 0;
	eDiv.style.zIndex = 100;
	eDiv.className = theClass;
	return eDiv;
}

function isStrongPassword() {

	var control = document.application.thePassword;
	var myString = control.value;


	var Stringlen = myString.length;
	var ValidateDigits = /[^0-9]/g;
	var ValidateSpChar = /[a-zA-Z0-9]/g;
	var ValidateChar = /[^a-zA-Z]/g;

	var digitString = myString.replace(ValidateDigits, "");
	var specialString = myString.replace(ValidateSpChar, "");
	var charString = myString.replace(ValidateChar, "");

	if (Stringlen == 0) {
		return true;
	}
	if (Stringlen < 6) {
		alert("Passwords must be at least 6 characters");
		control.value = "";
		control.focus();
		return false;
	}
	if (digitString.length < 1) {
		alert("Passwords must include at least 1 numeric character");
		control.value = "";
		control.focus();
		return false;
	}
	return;
}

function global_updateBalance(bal) {
	var el = document.getElementById("displayCurBal");
	if(el != null)
		el.innerText = bal;
}


//-------------------------------------
// ALERT SCRIPTS
//-------------------------------------
var alertIntervalID;
var alertIntervalMSecs = 20000;

function ShowAlertWindow(message, w, h)
{
	global_ShowAlertWindow(message, w, h);
}
function global_ShowAlertWindow(message, w, h) {
	alertBG = AddshowPopupBG("showAlertBG");
	alertBox = AddshowPopupBox(message, "showAlertBox", w, h);

	//<!--CURBAL#"&bal&"#CURBAL-->
	ns = message.indexOf("<!--CURBAL#") + 11;
	ne = message.indexOf("#CURBAL-->");
	if (ne > ns) {
		bal = message.substring(ns, ne);
		global_updateBalance(bal);
	}	
	document.body.appendChild(alertBG);
	document.body.appendChild(alertBox);


	if (self.innerHeight)
	{ //FF
		centerX = this.innerWidth;
		centerY = this.innerHeight;
		scrollX = 0;
		scrollY = window.scrollY;
	} else if (document.documentElement && document.documentElement.clientHeight)
	{ //IE 6+
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	} else if (document.body)
	{
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}
	w = alertBox.offsetWidth;
	if (w > 800)
	{
		alertBox.style.width = "800px"; //centerX / 2;
	}
	alertBox.style.left = (scrollX + ((centerX / 2) - (alertBox.offsetWidth / 2))) + "px";

	h = alertBox.offsetHeight;
	if (h > 500)
	{
		alertBox.style.height = "500px"; //centerX / 2;
		alertBox.style.overflow = 'auto';
	}

	theY = (scrollY + ((centerY / 2) - (alertBox.offsetHeight / 2)));
	if (theY < 20) theY = 20;
	alertBox.style.top = theY + "px";
}

function CloseAlertWindow() {
	if (alertBG != undefined) {
		document.body.removeChild(alertBG);
		alertBG = undefined;
	}
	if (alertBox != undefined) {
		document.body.removeChild(alertBox);
		alertBox = undefined;
	}
}

function global_CloseAlert() 
{
	CloseAlertWindow();
	alertIntervalID = setInterval("global_CheckAlerts()", alertIntervalMSecs);
	//alert(whichone);
}

function global_CheckAlerts() {
	alert_http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		alert_http_request = new XMLHttpRequest();
		if (alert_http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			alert_http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			alert_http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				alert_http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}
	if (!alert_http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	alert_http_request.onreadystatechange = global_ShowAlerts;
	alert_http_request.open('GET', '/process/alert.asp?random=' + Math.floor(Math.random() * 101), true);
	alert_http_request.setRequestHeader("Connection", "close");
	alert_http_request.send('');
}

function global_ShowAlerts() {
	if (alert_http_request.readyState == 4) {
		if (alert_http_request.status == 200) {
			alert_result = alert_http_request.responseText;
			//remove debug messages
			alert_result = alert_result.replace(/#DEBUG [^#]*#<br\/>/gi, "")
			//alert("#" + alert_result + "#");
			if (alert_result.length > 0 && alert_result != "NO DATA")
			{
				msg = alert_result;
				clearInterval(alertIntervalID);
				global_ShowAlertWindow(msg, 750, 250);
				sifrconf_DoReplaces();
			}
		}
	}
}

function globalDoPrint(ids, title, contentElementID) {
	var DocumentContainer = document.getElementById(contentElementID);
	var WindowObject = window.open('', "TWH_PrintPage","width=100,height=100,top=100,left=100,toolbars=no,scrollbars=no,status=no,resizable=no");
	WindowObject.document.writeln("<h1>Tom Waterhouse</h1><h2>" + title + "</h2>" + DocumentContainer.innerHTML);
	WindowObject.document.close();
	WindowObject.focus();
	WindowObject.print();
	WindowObject.close();

}

function global_checkformat(fld, pat, errmsg) {
	//alert(fld.value);
	val = fld.value;
	if (val.length > 0) {
		if (!pat.test(val)) {
			alert(errmsg);
			fld.focus();
			return (false);
		}
	}
	return true;
}


function global_hoverImages() 
{
	var allImgs = document.getElementsByTagName("img");

	for (i = 0; i < allImgs.length; i++) {
		var hasClassName = new RegExp("(?:^|\\s)hasHover(?:$|\\s)");
		var elementClass = allImgs[i].className;

		if (elementClass && elementClass.indexOf("hasHover") != -1 && hasClassName.test(elementClass)) {
			if (allImgs[i].src.indexOf(".jpg") != -1) {
				if (allImgs[i].src.indexOf("_hover.") != -1) {
					//allImgs[i].onmouseover = function() { this.src = this.src.replace("_hover.jpg", ".jpg"); };
					//allImgs[i].onmouseout = function() { this.src = this.src.replace(".jpg", "_hover.jpg"); };
				}
				else {
					allImgs[i].onmouseover = function() { this.src = this.src.replace(".jpg", "_hover.jpg"); };
					allImgs[i].onmouseout = function() { this.src = this.src.replace("_hover.jpg", ".jpg"); };
				}
			}
			else if (allImgs[i].src.indexOf(".gif") != -1) {
				if (allImgs[i].src.indexOf("_hover.") != -1) {
					//allImgs[i].onmouseover = function() { this.src = this.src.replace("_hover.jpg", ".gif"); };
					//allImgs[i].onmouseout = function() { this.src = this.src.replace(".jpg", "_hover.gif"); };
				}
				else {
					allImgs[i].onmouseover = function() { this.src = this.src.replace(".gif", "_hover.gif"); };
					allImgs[i].onmouseout = function() { this.src = this.src.replace("_hover.gif", ".gif"); };
				} 
			}
		}
	}

}
