// JavaScript Document

/******************************************************************************************
 * Chandler Functions & Utilities
 *
 ******************************************************************************************/
var MIN_CONTENT_HEIGHT = 550;
var DBG = 0;
var g_WinHeight = -1;

function init() {
	emailCloak();
}

function initDefaultPage() {
	setBodyCss("default");

	var ch = getContentHeight();
	var wh = getWindowHeight();
	var h;
	
	if(ch > 0 && wh > 0) {
		h = ch < MIN_CONTENT_HEIGHT ? MIN_CONTENT_HEIGHT : ch;
		h = ch < wh ? wh : ch;
	} else {
		h = MIN_CONTENT_HEIGHT;	
	}
	if(DBG>0) {alert("Setting content height: " +h);}
	
	if(typeof(g_bIsEditMode) == "undefined" || g_bIsEditMode == false) {
		//only do resize if we're not editing (due to quirk in IE: infinite resize looping on paste)
		setElementHeight("col-left", h);
		setElementHeight("col-center", h);
		setElementHeight("col-right", h);

		var elFooter = getElement("footer-container");
		if(false && elFooter) {
			elFooter.style.position = "absolute";
			elFooter.style.top = (h-elFooter.clientHeight-2) +"px";
		}
		
		//rerun on a resize event
		window.onresize = initDefaultPage;
	}

	return true;
}

function initTwoColumnPage() {
	setBodyCss("default");

	var ch = getContentHeight();
	var wh = getWindowHeight();
	var h;
	
	if(ch > 0 && wh > 0) {
		h = ch < MIN_CONTENT_HEIGHT ? MIN_CONTENT_HEIGHT : ch;
		h = ch < wh ? wh : ch;
	} else {
		h = MIN_CONTENT_HEIGHT;	
	}
	if(DBG>0) {alert("Setting content height: " +h);}
	
	if(typeof(g_bIsEditMode) == "undefined" || g_bIsEditMode == false) {
		//only do resize if we're not editing (due to quirk in IE: infinite resize looping on paste)
		setElementHeight("col-left", h);
		setElementHeight("col-center", h);
		
		var elFooter = getElement("footer-container");
		if(false && elFooter) {
			elFooter.style.position = "absolute";
			elFooter.style.top = (h-elFooter.clientHeight-2) +"px";
		}
	
		//rerun on a resize event
		window.onresize = initTwoColumnPage;
	}

	return true;
}

/******************************************************************************************
 * JavaScript General Helper Functions & Utilities
 *
 ******************************************************************************************/
var scriptTags = document.getElementsByTagName("script");
var path, siteRoot;
for(var i=0; i<scriptTags.length; i++) {
  if(scriptTags[i].src && scriptTags[i].src.match(/common\.js(\?.*)?$/)) {
	 siteRoot = scriptTags[i].src.replace(/\/shared\/js\/common.js/i, '/');
	 break;
  }
}

//Set up additional method on the location object
//This parses out the query string params for you.
location.getParameter = function(sParam) {
		var sKey = sParam +"=";
		var oParams = this.search.substring(1).split("&");
		for(var i = 0; i<oParams.length; i++) {
			if(oParams[i].indexOf(sKey) == 0) {
				return oParams[i].substring(sKey.length);
			}
		}
		return null;
	};

function addEvent(obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
	} else if (obj.attachEvent) {
		var eProp = type + fn;
		obj["e"+eProp] = fn;
		obj[eProp] = function() { obj["e"+eProp](window.event); };
		obj.attachEvent( "on"+type, obj[eProp] );
	} else {
		obj['on'+type] = fn;
	}
};

function getPosX(e) {
	var posx = 0;
	if (!e) var e = window.event;
	
	if (e.pageX) 	{
		posx = e.pageX;
	} else if (e.clientX) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
	}
	
	return posx;
}

function getPosY(e) {
	var posy = 0;
	if (!e) var e = window.event;
	
	if (e.pageY) 	{
		posy = e.pageY;
	} else if (e.clientY) 	{
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	return posy;
}

function removeEvent( obj, type, fn ) {
	if ( obj.removeEventListener ) {
		obj.removeEventListener( type, fn, false );
	} else if ( obj.detachEvent ) {
		var eProp = type + fn;
		obj.detachEvent( "on"+type, obj[eProp] );
		obj['e'+eProp] = null;
		obj[eProp] = null;
	} else {
		obj['on'+type] = null;
	}
};

function trim(sInput) {
	if(sInput != null && sInput != "") {
		return sInput.replace(/^\s+/,'').replace(/\s+$/,'');
	}
	return "";
}

function openWin(sUrl, iWidth, iHeight, bScrollbars) {
	var newWin = window.open(sUrl,"new_window","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=" +(bScrollbars?"yes":"no") +", resizable=yes, width="+iWidth +", height="+iHeight);
}

function emailCloak() {
	if (document.getElementById) {
		var alltags = document.all? document.all : document.getElementsByTagName("span");
		for (i=0; i < alltags.length; i++) {
		  if (alltags[i].className == "emailCloak") {
			var oldText = alltags[i].firstChild;
			var emailAddress = alltags[i].firstChild.nodeValue;
			var user = emailAddress.substring(0, emailAddress.indexOf("("));
			var domain = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
			var newText = user+"@"+domain;
			var a = document.createElement("a");
			a.href = "mailto:"+newText;
			var address = document.createTextNode(newText);
			a.appendChild(address);
			alltags[i].replaceChild(a,oldText);
		  }
		}
	}
}

/******************************************************************************************
 * DHTML Helper Functions
 *
 ******************************************************************************************/
function getElement(sId) {
	var el = null;
	if(document.getElementById) {
		el = document.getElementById(sId);
	} else if(document.all) {
		el = document.all[sId];
	} else if(document.layers) {
		el = document.layers[sId];
	}
	return el;
}

function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
		
	}	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight=	document.documentElement.clientHeight;
		}	else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	
	return windowHeight;
}

function getWindowWidth() {
	var width = -1;
	
	if (self.innerWidth) {
		width = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		width = document.documentElement.clientWidth;
	} else if (document.body) {
		width = document.body.clientWidth;
	}
	
	return width;
}

function getContentHeight() {
	var retVal = -1;
	var els = document.getElementsByTagName("HTML");
	if(els && els.length > 0) {
		var elHtml = els[0];
		if(elHtml && elHtml.scrollHeight) {
			retVal = elHtml.scrollHeight;
		}
	}
	return retVal;
}

function setElementHeight(sElementId, iHeightInPixels) {
	if(sElementId != null && sElementId != "") {
		var elTarget = getElement(sElementId);
		if(elTarget) {
			elTarget.style.height = iHeightInPixels +"px";
			return true;
		}
	}	
	return false;
}

function getBodyElement() {
	var retVal = null;
	if(document.body && document.body.nodeName == "BODY") {
		retVal = document.body;
	} else {
		var aoElements = document.getElementsByTagName("BODY");
		if(aoElements && aoElements.length > 0) {
			retVal = aoElements[0];
		}
	}
	return retVal;
}

function setBodyCss(sClassName) {
	if(sClassName) {
		var elBody = getBodyElement();
		if(elBody) {
			elBody.className = sClassName;
		}		
	}
	return;
}

/******************************************************************************************
 * ASP.NET Helper Functions
 *
 ******************************************************************************************/

/*********************************************
 * Handles and directs default events for textboxes
 *
 * Example: OnKeyDown='OnKeyDownSetEventTarget(event, targetBtnId)'
 * ..where targetBtnId is the intended default event target
 *********************************************/
function setEnterKeyEventTarget(e, sTargetId) {
	if (sTargetId) {
		var elTarget = getElement(sTargetId);
		// Process only the enter key.
		if (e && e.keyCode == 13) {
			// Cancel the default submit.
			e.returnValue = false;
			e.cancel = true;
			// Submit the form by programmatically clicking the specified target.
			if(elTarget) {
				elTarget.click();
			}
		}
	}
}

/******************************************************************************************
 * Initialization
 *
 ******************************************************************************************/
window.onload = init;


/******************************************************************************************
 * Red-headed step child (makes parser unhappy)
 *
 ******************************************************************************************/
function require(sFilePath) {
	document.write("<script type=\"text/javascript\" src=\"" +siteRoot +"/shared/js/\"" +sfilepath +"\"></script>");
}