//--------------------------------------------------------------
// Common helper functions to determine browser compatability
function isCSS() { return ( (document.body && document.body.style) ? true : false ); }
function isW3C() { return ( (isCSS && document.getElementById) ? true : false ); }
function isIE()  { return ( (isCSS && document.all) ? true : false ); }
function isNS4() { return ( (document.layers) ? true : false ); }
//--------------------------------------------------------------

//--------------------------------------------------------------
// Convert object name string or object reference into a 
// valid element object reference
function getObject(obj) {
	var theObj;
	if (typeof obj == "string") {
		if (isW3C) {
			theObj = document.getElementById(obj);
			if( !theObj ) { theObj = document.getElementByName(obj); }
		}
		else if (isIE) {
			theObj = document.all[obj];
		}
		else if (isNS4) {
			theObj = document.layers[obj];
		}
	} 
	else {
		// pass through object reference
		theObj = obj;
	}
	return theObj;
}
//--------------------------------------------------------------

//--------------------------------------------------------------
// Convert object name string or object reference into a 
// valid style for modifying style properties (ie. visibility)
function getObjectStyle(obj) {
	var theObj = getObject(obj);
	if (theObj && isCSS) { theObj = theObj.style; }
	return theObj;
}
//--------------------------------------------------------------

//--------------------------------------------------------------
// Set the visibility of an object to visible
function show(obj) {
	var theObj = getObjectStyle(obj);
	if (theObj) {
		theObj.visibility = "visible";
		theObj.display = "block";
	}
}
// Set the visibility of an object to hidden
function hide(obj) {
	var theObj = getObjectStyle(obj);
	if (theObj) {
		theObj.visibility = "hidden";
		theObj.display = "none";
	}
}
//--------------------------------------------------------------

//--------------------------------------------------------------
// Capture Enter keypress and click the appropriate button.
// Only works with MS Internet Explorer.
function CheckEnterKey( buttonToClick ) {
	if (isIE) {
		//if ( document.all && event.keyCode == 13 ) {
		if ( event.keyCode == 13 ) {
			event.cancelBubble = true;
			event.returnValue = false;
			var objButton = getObject( buttonToClick );
			if ( objButton ) {
				objButton.focus();
				objButton.click();
			}
		}
	}
}
// Capture Enter keypress and prevent anything from happening.
function StopEnterKey() {
	if (isIE) {
		if ( event.keyCode == 13 ) {
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
}

//--------------------------------------------------------------
// Open a new window
function winopen(url, title, width, height) {
	window.open(url, title, 'width='+width+',height='+height+',scrollbars=yes,resizable=yes');
}
//--------------------------------------------------------------

//==============================================================
// Loading Screen Functions
//==============================================================

//
// initLoadScreen()
// 
// Function runs on window load, going through link tags looking for rel="loadscreen".
// These links receive onclick events that enable the loadscreen display.
//
function initLoadScreen() {
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "loadscreen")){
			anchor.onclick = function () {ShowWaitMsg(); return false;}
		}
	}
}

//
// ShowWaitMsg()
// Makes the loading screen visible and sets the
// width and height to fill the entire page.
//
function ShowWaitMsg() {
	// prep objects
	var objLoadingScreen = document.getElementById('loadingScreen');
	var objLoadingImage = document.getElementById('loadingImage');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// display the loading screen and set the height to take up whole page
	objLoadingScreen.style.height = (arrayPageScroll[1] + arrayPageSize[1] + 'px');
	objLoadingScreen.style.display = 'block';
	
	// center loading image
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 40 - objLoadingImage.width) / 2) + 'px');
	}
}

//
// HideWaitMsg()
// Hides the loading screen from view.
//
function HideWaitMsg() {
	// get objects
	var objLoadingScreen = document.getElementById('loadingScreen');

	// hide lightbox and overlay
	objLoadingScreen.style.display = 'none';
}


// Add initLoadScreen to onLoad event
//addLoadEvent(HideWaitMsg);



//==============================================================
// Helper Functions
//==============================================================

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func) {	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
    	window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
function getPageSize(){
	
	var xScroll, yScroll;

	if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}