function registerEvent(oObj, sEvent, oFunc, bCapture) {
    bCapture = (typeof(bCapture) == "undefined") ? false : bCapture;
    sEvent = sEvent.replace(/^on/i, "");

    if (oObj.addEventListener) { oObj.addEventListener(sEvent, oFunc, bCapture); }
    else if (oObj.attachEvent) { oObj.attachEvent("on"+sEvent, oFunc, bCapture); }
}


registerEvent(window, "load", loadTools, false);

var oShade;
var aSelects;

function loadTools() {
	getBrowserAbilities();
	aSelects	= document.getElementsByTagName("select");
}


/*-----------------------------------------------------------------------*/
function findPos(obj) {
	// http://www.quirksmode.org/
	var curleft = curtop = 0;
	if (obj && obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft - obj.scrollLeft;
			curtop += obj.offsetTop - obj.scrollTop;
		}
	}
	
	oBody = document.getElementsByTagName("body")[0];
	curleft = curleft - oBody.offsetLeft;
	curtop	= curtop - oBody.offsetTop;
	
	return [curleft,curtop];
}

function findClickPos(e) {
	// http://www.quirksmode.org/
	var pos = Array();
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		pos[0] = e.pageX;
		pos[1] = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		pos[0] = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		pos[1] = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return pos;
}

function findScrollPos() {
	// http://www.quirksmode.org/
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	result = new Array(x, y);
	return result;
}

function findWindowProps() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	result = new Array(x, y);
	return result;
}

function findPageScroll() {
	var s;
	// scrolling offset calculation via www.quirksmode.org
	if (self.pageYOffset){
		s = self.pageYOffset;
	}else if (document.documentElement && document.documentElement.scrollTop) { 
		s = document.documentElement.scrollTop; 
	}else if (document.body) { 
		s = document.body.scrollTop; 
	}
	return s;
}


/*-----------------------------------------------------------------------*/
function centerObject(obj, hOffset, vOffset) {
	centerObjectHorizontal(obj, hOffset);
	centerObjectVertical(obj, vOffset);
}

function centerObjectHorizontal(obj, offset) {
	if (!offset) offset = 0;
	
	aPos = findPos(obj);
	oBody	= document.getElementsByTagName('body')[0];
	
	aWinProps = findWindowProps();
	winW = aWinProps[0];
	boxW = (obj.scrollWidth > 0) ? obj.scrollWidth : parseInt(YAHOO.util.Dom.getStyle(obj, "width"));
	bodyL = (winW-oBody.scrollWidth)/2;
	var left = parseInt( (winW-boxW)/2 + offset - bodyL );

	if (left > 0) { obj.style.left = left+'px'; } 
	else { obj.style.left = '0px'; }
}

function centerObjectVertical(obj, offset) {
	if (!offset) offset = 0;
	
	aPos 	= findPos(obj);
	aScroll	= findScrollPos();
	aWin	= findWindowProps();
	oBody	= document.getElementsByTagName('body')[0];
	
	winH = aWin[1];
	boxH = obj.scrollHeight;
	var top = parseInt( ((winH-boxH)/2) + aScroll[1] + offset );
	
	if (top > 0) { obj.style.top = top+'px'; } 
	else { obj.style.top = '10px'; }

}

function placeObjectVertical(obj, offset) {
	if (!offset) offset = 0;
	
	aPos 	= findPos(obj);
	aScroll	= findScrollPos();
	aWin	= findWindowProps();
	oBody	= document.getElementsByTagName('body')[0];
	
	winH = aWin[1];
	boxH = obj.scrollHeight;
	var top = aScroll[1] + offset;
	
	if (top > 0) { obj.style.top = top+'px'; } 
	else { obj.style.top = '10px'; }
}


/*-----------------------------------------------------------------------*/
var aSelects = new Array();
var shimTimer = null;
var oShim = null;
var oShimObj = null;
var iShimLevel = 200;

function ShimOn(thisObj) {
	// IFrame hack for IE6 & windowed controls
	oShim	= document.getElementById("shim");
	oShimObj = thisObj;
	
	adjustShim(thisObj, oShim);
	shimTimer = setInterval(adjustShim, 500);

}

function adjustShim() {
	
	if (oShim) {
		if (iShimLevel == 200) { 
			if (oShimObj.style.zIndex < iShimLevel) { oShimObj.style.zIndex = iShimLevel; }
			iShimLevel++;
		}
		aPos	= findPos(oShimObj);
	
		oShim.style.position	= "absolute";
		oShim.style.left		= aPos[0] + "px";
		oShim.style.top			= aPos[1] + "px";
		oShim.style.width		= oShimObj.offsetWidth + "px";
		oShim.style.height		= oShimObj.offsetHeight + "px";
		oShim.style.display		= "block";
		oShim.style.zIndex		= 1;

	}

}

function ShimOff() {
	oShim	= document.getElementById("shim");
	if (oShim) { oShim.style.display = "none"; }
	clearInterval(shimTimer);
}


/*-----------------------------------------------------------------------*/
function getElementsByClassName(thisClass, tag, obj) {
	if (typeof(tag) == "undefined") { tag = "*"; }
	if (typeof(obj) == "undefined" || !obj.getElementsByTagName) { obj = document; }
	//var aMatches = YAHOO.util.Dom.getElementsByClassName(thisClass, tag, obj);
	
	var aMatches = [];
	var aEls = obj.getElementsByTagName(tag);
	for (var i=0; i<aEls.length; i++) {
		if (aEls[i].className && aEls[i].className.indexOf(thisClass) > -1) {
			//aMatches.push(aEls[i]);
			aMatches[aMatches.length] = aEls[i];
		}
	}
	
	return aMatches;
}

function inputText(obj, str) {
	if (str != null && obj) {
		if (aBrowser && aBrowser['innerText']) {
			obj.innerText = str;
			return (obj.innerText == str);
		} else if (obj.tagName.toLowerCase() == "input" || obj.tagName.toLowerCase() == "textarea") {
			obj.value = str;
			return (obj.value == str);
		} else if (obj.textContent) {
			obj.textContent = str;
			return (obj.textContent == str);
		} else {
			obj.innerHTML = str;
			return (obj.innerHTML = str);
		}
	}
}

function getText(obj) {
	if (obj && obj.tagName) {
		thisTag = obj.tagName.toLowerCase();
		switch(thisTag) {
		case "select":
			if (obj.selectedIndex < 0) { obj.selectedIndex = 0; }
		case "input":
		case "textarea":
			return obj.value;
			break;
		default:
			if (aBrowser && aBrowser['innerText']) {
				return obj.innerText;
			} else {
				return obj.textContent;
			}
		}
	}
}

function breakLargeString(sIn) {
	sOut = sIn;	
	
	if (typeof(sIn) == "string" && sIn.length > 15) {
		sOut = sIn.replace(/(\w)(\W)(\w)/gi, "$1 $2 $3");
	}
	
	return sOut;
}

/*-----------------------------------------------------------------------*/
function formatMoney(str) {
	mny = stripMoney(str);
	if (mny > -1) {
		mny += "";
		mny2 = "";
		digit = 0;
		for (var i=mny.length; i>0; i--) {
			digit++;
			delta = (digit)/3;
			mny2 = mny.charAt(i-1) + mny2;
			if (delta - Math.floor(delta) == 0 && digit < mny.length) { mny2 = "," + mny2; }
		}
		return "$" + mny2;
	} else {
		return str;
	}
}

function abbrMoney(str) {
	mny = stripMoney(str);
	if (mny > -1) {
		mny += "";
		mny2 = "";
		digit = 0;
		if (mny >= 1000000) {
			mny2 = (Math.round((mny/1000000)*100)/100)
			return "$" + mny2 + "B";
		}
		else if (mny >= 1000) {
			mny2 = (Math.round((mny/1000)*100)/100)
			return "$" + mny2 + "M";
		}
		else if (mny > 0) {
			return "$" + mny + "K";	
		}
		else {
			return "$" + mny;	
		}
	} else {
		return str;	
	}
}

function stripMoney(str) {
	if (typeof(str) == "string") {
		pattern = /\D/g;
		mny = str.replace(pattern, "");
		return Number(mny);
	} else {
		return parseInt(str);
	}
}

/*-----------------------------------------------------------------------*/
function formatDate(sDate) {
	if (typeof(sDate) == "object") {
		targ	= (sDate.target) ? sDate.target : sDate.srcElement;
		sDate	= targ.value;
	}
	
	sTemp	= sDate.match(/(\d{1,2})[-\/]?(\d{1,2})[-\/]?(\d{4})/i, sDate);

	if (!sTemp) {
		return "MM/DD/YYYY";
	} else {
		newDate = sTemp[1]+"/"+sTemp[2]+"/"+sTemp[3];
		return newDate;
	}
}

/*-----------------------------------------------------------------------*/
aBrowser = new Array();
function getBrowserAbilities() {
	aBrowser['innerText']	= (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
	
	var reg = /MSIE [56]/;
	aBrowser['windowedControls'] = (reg.test(navigator.userAgent) && navigator.platform == "Win32") ? true : false;
}

/*-----------------------------------------------------------------------*/
function checkInputButton(e, noEdit) {
	if (!e) var e = window.event;
	var targ = (e.target) ? e.target : e.srcElement;
	var thisKey = (e.keyCode) ? e.keyCode : e.which;
	
	switch (thisKey) {
	case 8:		//Delete
	case 46:	//Backspace
		if (noEdit) { return false; break;}
	case 35:	//End
	case 37:	//Left
	case 38:	//Up
	case 39:	//Right
	case 40:	//Down
		return true;
		break;
	default:
		return false;
	}

}

/*-----------------------------------------------------------------------*/
function toggleShade(bHideSelects) {
    bHideSelects = (typeof(bHideSelects) != "undefined") ? bHideSelects : true;
	if (!oShade) { 
		oShade = document.getElementById("bgShade");
		if (!oShade) {
			oShade = document.body.appendChild(document.createElement("div"));
			oShade = bringToFront(oShade);
			oShade.id = "bgShade";
		}
		oShade.innerHTML = "&nbsp;";
		oShade.style.position = "absolute";
		oShade.style.top = "0px";
		oShade.style.left = "0px";
		oShade.style.backgroundColor = "#000000";
		oShade.style.opacity = 0.6;
		oShade.style.filter = "alpha(opacity=60)";
		oShade.style.zIndex = 100;
	}
	
	if (window.addEventListener) { window.addEventListener("resize", resizeShade, false); }
	else if (window.attachEvent) { window.attachEvent("onresize", resizeShade, false); }
	
	if (oShade.style.display != "block") {
		resizeShade();
		oShade.style.display	= "block";
		if (bHideSelects == true) { hideSelects(); }
	} else {
		oShade.style.display	= "none";
		if (bHideSelects == true) { showSelects(); }
	}
}

function resizeShade() {
	var aWin = findWindowProps();
	var iWidth = (document.body.scrollWidth > aWin[0]) ? document.body.scrollWidth : aWin[0];
	var iHeight = (document.body.scrollHeight > aWin[1]) ? document.body.scrollHeight : aWin[1];
	oShade.style.width = iWidth + "px";
	oShade.style.height = iHeight + "px";
}

function hideSelects() {
	if (aBrowser['windowedControls']) {
		for (var i=0; i<aSelects.length; i++) {
		    if (aSelects[i].style.zIndex < 2) {
			    aSelects[i].style.visibility = "hidden";
			}
		}
	}
}

function showSelects() {
	if (aBrowser['windowedControls']) {
		for (var i=0; i<aSelects.length; i++) {
			aSelects[i].style.visibility = "visible";
		}
	}

}

/*-----------------------------------------------------------------------*/
function togglePopup(oEl, nextFunc, vertOffset) {
	nextFunc	= (typeof(nextFunc) == "function") ? nextFunc : false;
	vertOffset	= (typeof(vertOffset) == "undefined") ? 100 : vertOffset;
	
	toggleShade();
	if (oEl.style.display != "block") {
		oEl.style.display = "block";
		oEl = bringToFront(oEl);
		centerObjectHorizontal(oEl);
		placeObjectVertical(oEl, vertOffset);
	} else {
		oEl.style.display = "none";
		oEl = false;
	}
	
	if (nextFunc) { nextFunc();	}
	
	return oEl;
}

function bringToFront(oEl) {
    var aForms = document.getElementsByTagName("form");
	var oBox = (typeof(aForms[0]) != "undefined") ? aForms[0] : document.getElementsByTagName("body")[0];
	
	if (oEl.parentNode.tagName.toLowerCase() != "form") {
	    oTempEl = (oEl.adoptNode) ? oEl.parentNode.adoptNode(oEl) : oEl.cloneNode(true);
	    oEl.parentNode.removeChild(oEl);
	    oEl = oBox.appendChild(oTempEl);
	}
	oEl.style.position	= "absolute";
	oEl.style.zIndex	= 1000;

	return oEl;
}

/*-----------------------------------------------------------------------*/
function fixPNG(thisImg) {
	var reg = /MSIE [56]/;
	var supported = reg.test(navigator.userAgent) && navigator.platform == "Win32";

	if (supported) {
		if (thisImg.src.indexOf(".png") > -1) {
				thisHeight	= thisImg.height;
				thisWidth	= thisImg.width;

				src			= thisImg.src;
				thisImg.src = "../Images/1px.gif";
				thisImg.alt	= src;
				thisImg.runtimeStyle.filter		= "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
													src + "',sizingMethod='scale')";
				
				thisImg.runtimeStyle.width		= thisWidth + "px";
				thisImg.runtimeStyle.height		= thisHeight + "px";

		}
	}
}

/*-----------------------------------------------------------------------*/
function getQueryVar(sVar) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == sVar) { return pair[1]; }
    }
    return null;
} 

/*-----------------------------------------------------------------------*/
function mysqlTimeStampToDate(timestamp) {
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
  }

/*-----------------------------------------------------------------------*/

