var IE = document.all?true:false;

function getCtl(ref) {
	if (typeof(ref)=="string") {
		return document.getElementById(ref);}
	else {
		if (typeof(ref)=="object") {
			return ref;}
		else {
			return null;
		}
	}
}

function setValue(ctl, text) {
	ctl = getCtl(ctl);
	if (ctl!=null) {ctl.value = text;}
}

function setInnerHTML(ctl, html) {
	ctl = getCtl(ctl);
	if (ctl!=null) {ctl.innerHTML = html;}
}

// --------------------------------------------------------------Mouse Location tracking
var mouseX=0, mouseY=0;
var pageWidth, pageHeight;
if (IE) {
	pageWidth=document.body.clientWidth;
	pageHeight=document.body.clientHeight;
} else {
	pageWidth=window.innerWidth;
	pageHeight=window.innerHeight;
}

function pageLeftScroll() {if(IE) {return document.body.scrollLeft} else {return window.pageXOffset}}
function pageTopScroll() {if(IE) {return document.body.scrollTop} else {return window.pageYOffset}}

// ----------------------------------------------------------------------Popup panel handling
var HoverControl, HoverDelayTimer;

function HoverShow(ctl) {
	if (HoverControl != getCtl(ctl)) {
		if (HoverControl != null) HoverHide(HoverControl);
		HoverControl = getCtl(ctl);
	ShowHoverControl();
	}
	clearTimeout(HoverDelayTimer);
}

function ShowHoverControl() {
	//If the panel would be longer than the viewable area...	
	HoverControl.style.top = mouseY+16;
	HoverControl.style.left = mouseX+16;
	HoverControl.style.display = "block";
	if (mouseY + 16 + HoverControl.offsetHeight > pageHeight + pageTopScroll()) {
		if (pageHeight - HoverControl.offsetHeight + pageTopScroll()<0) {
			HoverControl.style.top = 0;
		} else {
			HoverControl.style.top = pageHeight - HoverControl.offsetHeight + pageTopScroll();			
		}
	}
	HoverControl.style.left = 100;
}

function HoverHide() {
	if (HoverControl!=null) {HoverControl.style.display="none"; HoverControl = null};
	if (HoverDelayTimer!=null) {clearTimeout(HoverDelayTimer)};
}

function DelayHoverShow(ctl) {
	HoverControl = getCtl(ctl);
	if (HoverDelayTimer!=null) {clearTimeout(HoverDelayTimer)};
	//Only setTimeout if the panel is currently not displayed
	if (HoverControl.style.display=="none" || HoverControl.style.display=="") {
		HoverDelayTimer = setTimeout(ShowHoverControl, 1000)
	};
}

function DelayHoverHide(ctl) { //Mouse left or moved, hide the display and stop the timer
	if (HoverDelayTimer!=null) {clearTimeout(HoverDelayTimer)};
	HoverDelayTimer = setTimeout(doDelayHoverHide,1000);
}

function doDelayHoverHide() {
	HoverHide(HoverControl);
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function moveNext(fromCntl, e) {
	var keyCode = (isNN)?e.which:e.keyCode;
	var filter = (isNN)?[0,8,9]:[0,8,9,16,17,18,37,38,39,40,46];
	if(fromCntl.value.length==fromCntl.maxLength && !containsElement(filter,keyCode)) {
		for(i=0; i<fromCntl.form.length; i++) {
			if(fromCntl.form[i]==fromCntl) {
				fromCntl.form[i+1].focus();
				break;
			}
		}
	}
}
function stopEvent(e) {
	if(isNN) {e.preventDefault()}
	else {window.event.returnValue=null};
}
function numericOnly(cntl, e) {
	var keyCode = (isNN)?e.which:e.keyCode;
	if ((keyCode>31 && keyCode<48) || keyCode>57) stopEvent(e);
}
function emailFilter(cntl, e) {
	var keyCode = (isNN)?e.which:e.keyCode;
	//If the key pressed is not between 48(0) and 57(9)
	//and it's not 46(period), 64(@), 95(_), 45(-)
	//and it's not between 65(A) and 90(Z) or 97(a) and 122(z) then throw it out
	if (!((keyCode>=48 && keyCode<=57) ||
		  (keyCode==46 || keyCode==64 || keyCode==95 || keyCode==45) ||
		  (keyCode>=65 && keyCode<=90) ||
		  (keyCode>=97 && keyCode<=122))) window.event.returnValue=null;
}
function validateEmail(str) {
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
		if(arr[index] == ele) found = true;
		else index++;
	return found;
}

function toggle (n) {
	e = document.getElementById(n);
	if (e.style.display == 'none') {
		e.style.display = '';
	} else {
		e.style.display = 'none';
	}
}

if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;

function getMouseXY(e) {
	if (IE) {
		mouseX = event.clientX;
		mouseY = event.clientY;
	} else {
		if (e) {
			mouseX = e.pageX;
			mouseY = e.pageY;
		} else {
			mouseX = 0;
			mouseY = 0;
		}
	}
	if (mouseX < 0){mouseX = 0};
	if (mouseY < 0){mouseY = 0};
	return true
}

function ArrayContains(theArray, CheckVal) {
	for(var idx=0; idx <= theArray.length; idx++) {
		if(theArray[idx] == CheckVal) return true
	}
	return false
}