//==============================================================
// General Functions
//==============================================================

//
// winopen(url, title, vars)
// Pops up a new window using the passed in url.  The title var
// is the window title to use (no spaces allowed in IE).  To open
// multiple pop-ups into the same window, use the same title.
// vars = window settings such as height, width, scrollbars, etc.
//
function winopen( url, title, vars ) {
	newWindow = window.open(url, title, vars);
	newWindow.focus();
}

//
// SmartScrollSave()
// Stores the current scroll position into hidden form fields and
// sets itself up to automatically run every 10ms.
//
function SmartScrollSave() {
	var arrayPageScroll = getPageScroll();
	var xScroll = getObject('SmartScrollPositionX');
	var yScroll = getObject('SmartScrollPositionY');
	if ( xScroll && yScroll ) {
		xScroll.value = arrayPageScroll[0];
		yScroll.value = arrayPageScroll[1];
		setTimeout('SmartScrollSave()', 10);
	}
}



//==============================================================
// Internet Explorer Specific Functions
//==============================================================

//
// CheckEnterKey(buttonToClick)
// Will automatically click the specified button when the
// enter key is pressed inside a form field.
//
function CheckEnterKey( buttonToClick ) {
	if (document.body && document.body.style && document.all) {
		if ( event.keyCode == 13 ) {
			event.cancelBubble = true;
			event.returnValue = false;
			if ( document.all[ buttonToClick ] ) {
				document.all[ buttonToClick ].focus();
				document.all[ buttonToClick ].click();
			}
		}
	}
}
//
// StopEnterKey()
// Prevents enter key from submitting a form.
//
function StopEnterKey() {
	if (document.body && document.body.style && document.all) {
		if ( event.keyCode == 13 ) {
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
}



//==============================================================
// 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');
	
	if (objLoadingScreen && objLoadingImage) {
		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';
}



//==============================================================
// Helper Functions
//==============================================================

//
// getObject(obj)
// Returns the page object with the specified name.
//
function getObject( obj ) {
	var theObj;
	if (typeof obj == "string") {
		if ( document.forms[0] && document.forms[0].elements[obj] ) {
			theObj = document.forms[0].elements[obj];
		} else if ( document.getElementById ) {
			// W3C DOM
			theObj = document.getElementById(obj);
			if ( !theObj ) { theObj = document.getElementsByTagName(obj); }
		} else if ( document.all ) {
			// IE DOM
			theObj = document.all[obj];
		} else if ( document.layers ) {
			// NS4 DOM
			theObj = document.layers[obj];
		}
	} else {
		// pass through object reference
		theObj = obj;
	}
	return theObj;
}

//
// getObjectStyle(obj)
// Returns an object style to be modified.
//
function getObjectStyle( obj ) {
	var theObj;
	if ( document.getElementByID || document.all ) {
		theObj = getObject(obj).style;
	} else {
		theObj = getObject(obj);
	}
	return theObj;
}

//
// 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 = 0;

	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(0,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;
}

// methods to retrieve the timezone offset from the client
function GetClientUTC(controlname)
{        
    var now = new Date();    
    var offset = now.getTimezoneOffset()/60 ;    
    document.getElementById(controlname).value = offset;
}

function ToggleDetails(objID, show) {
		obj = document.getElementById(objID);
               
		if (obj) {
			if (show) {
				obj.style.display = 'block';
				
			} else {
				obj.style.display = 'none';
			}
		}
	}



//==============================================================
// OLDER FUNCTIONS TO BE GONE OVER
//==============================================================

//--------------------------------------------------------------
// 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";
	}
}
//--------------------------------------------------------------