String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
var IE = document.all?true:false;

$(function() {	
	$(".HoverRow")
		.live("mouseenter", function() {$(this).css("background-color","#FFFFCC");$(".ImgButton",this).css("visibility","visible");})
		.live("mouseleave", function() {$(this).css("background-color","");$(".ImgButton",this).css("visibility","hidden");});
	$("#PopupWin .close, #PopupWin .closeLink").live("click",function() {closePopupWin();});
	
	//ToggleDivs
	$(".ToggleDiv").each(function() {
		if ( !$(this).hasClass("Checked") && !$(this).hasClass("Unchecked") ) $(this).addClass("Unchecked");
		$(this).append("<input type=\"hidden\" name=\""+$(this).attr("id")+"\" value=\""+($(this).hasClass("Checked")?1:0)+"\" class=\"ToggleVal\" />");
	});	
	$(".ToggleDiv").live("click", function() {doToggleDiv(this);});
	
	//Programmatic Classes
	$(".GhostText")
		.each(function() {$.data(this,"DefaultValue",this.value);})
		.focus(function() {if(this.value==$.data(this,"DefaultValue")){this.value="";$(this).removeClass("GhostText");}})
		.blur(function() {if(this.value==""){this.value=$.data(this,"DefaultValue");$(this).addClass("GhostText");}});
	$(".NumericOnly")
		.live("keypress",function(event) {var keyCode=event.keyCode;
			if ((keyCode>31 && keyCode<48) || keyCode>57) event.preventDefault();});
	$(".AutoTab")
		.live("keyup",function(event) {
			if(this.form==null) {alert("Control must be inside a <form> for AutoTab");return;};
			var keyCode=event.keyCode, filter = [0,8,9,16,17,18,37,38,39,40,46];
			if(this.value.length==this.maxLength && !containsElement(filter,keyCode)) {
				for(i=0; i<this.form.length; i++) {if(this.form[i]==this) {this.form[i+1].focus();break;};};
			}
		});
});

function showPopupWin() {
	$("#PopupWin, #ScreenOverlay").show();
	$("#PopupWin").find("button,input,textarea,select").first().focus();
}
function closePopupWin() {
	$("#PopupWin, #ScreenOverlay").fadeOut("fast");
}

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 doToggleDiv(ctl) {
	$(ctl).toggleClass("Checked").toggleClass("Unchecked");
	var togVal = $(ctl).children(".ToggleVal");
	togVal.val(togVal.val()=="1"?"0":"1");
	$(ctl).trigger("toggle", [togVal.val()=="1"]);
}
function setToggleDiv(ctl, bChecked) {
	if(bChecked) {
		$(ctl).addClass("Checked").removeClass("Unchecked");
		$(ctl).children(".ToggleVal").val("1");
	} else {
		$(ctl).removeClass("Checked").addClass("Unchecked");
		$(ctl).children(".ToggleVal").val("0");
	};
	$(ctl).trigger("toggle", [bChecked]);
}

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) {
	//Depreciated: Instead apply class="AutoTab" to the control
	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) {
	//Depreciated: Instead apply class="NumericOnly" to the control
	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
}
