//
// Core JavaScript include file - this should be included on the front end and backend.
//

debugEnabled = false;

if(debugEnabled) {
	
	var debugInfo = '';
	
	debugInfo += '<div id="jsdebug" style="z-index:999;width:100%;position:absolute;visibility:hidden;left:0px;top:0px;height:0px;">';
	debugInfo += '<div id="debugtext" style="border-bottom:1px solid #000000;padding:10px;line-height:140%;color:#000;"><strong>JavaScript debug...</strong><br /><br /></div>';
	debugInfo += '<div id="debugtoggle" style="border-bottom:1px solid #000000;padding:4px;text-align:right;background-color:#FFFFFF;color:#000;">[ <a href="javascript:toggleDiv(\'debugtext\');">toggle debug</a> ]</div></div>';

	document.write (debugInfo);
}


// Used to add a line to debug window...
function debug(debugString) {
	if(debugEnabled) {
		if(document.getElementById('jsdebug')) {
			
			// Have we displayed the JavaScript debug window yet?
			if(document.getElementById('jsdebug').style.visibility == 'hidden') {
				document.getElementById('jsdebug').style.visibility = 'visible';
				document.getElementById('jsdebug').style.opacity = '.95';
				document.getElementById('jsdebug').height = '170px';
				document.getElementById('debugtext').style.height = '140px';
				document.getElementById('debugtext').style.backgroundColor = '#FFFFCC';
				document.getElementById('debugtext').style.overflow = 'auto';
			}
			
			document.getElementById('debugtext').innerHTML += '> '+debugString+'<br />';
			document.getElementById('debugtext').scrollTop = document.getElementById('debugtext').scrollHeight;
		}
		else alert(debugString);
	}
}


// Used to add the specified element type to the document body...
function createElement(elementID, elementType, className) {

	var ourBody = document.getElementsByTagName("body").item(0);
	var ourElement = document.createElement(elementType);

	ourElement.setAttribute('id', elementID);
	ourElement.className = className ? className : '';
	ourElement.style.display = 'none';

	ourBody.appendChild(ourElement);

	return true;
}


// Used to obtain an array of x,y scroll values (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;
}


// Used to obtain an array with page width, height and window width, height (Code from quirksmode.org)...
function getPageSize() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else 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) 

	debug('getPageSize returning page: '+pageWidth+'x'+pageHeight+', window: '+windowWidth+'x'+windowHeight+'...');
	
	return arrayPageSize;
}

// Used to display popup windows...
function popup(sourceURL, windowName, width, height, scrollable, resizable) {
	var featureString = '';
	var ourWindow = null;
	var left = screen.width ? (screen.width - width) / 2 : 0;
    var top = screen.height ? (screen.height - height) / 2 : 0;
	
	featureString  = 'width='+width+',height='+height+',scrollbars='+(scrollable ? 'yes' : 'no')+',left='+left+',top='+top;
	featureString += ',resizable='+(resizable == true ? 'yes' : 'no')+',toolbar=no,titlebar=no,status=no,menubar=no,location=no,directories=no';
	
	ourWindow = window.open(sourceURL, windowName, featureString);
	ourWindow.opener = self;
	ourWindow.focus();
}

// Used to obtain the source image for the specified element...
function getSourceImage(element, source) { if(document.getElementById(element)) return(document.getElementById(element).src); }

// Used to set the source image for the specified element...
function setSourceImage(element, source) { if(document.getElementById(element)) document.getElementById(element).src = source; }

// Used to set the source innerHTML for the specified element...
function setSourceData(element, data) { if(document.getElementById(element)) document.getElementById(element).innerHTML = data; }

// Used to set the source value for the specified element...
function setSourceValue(element, data) { if(document.getElementById(element)) document.getElementById(element).value = data; }

// Used to output a confirmation dialog...
function showConfirm(text, destination) { var returnState = confirm(text); if (returnState) document.location=destination; }

// Used to toggle the visibility of a div...
function toggleDiv(divname) { if (document.getElementById(divname).style.display == "none") showDiv(divname); else hideDiv(divname); }

// Used to show a div...
function showDiv(divname) { if(document.getElementById(divname)) document.getElementById(divname).style.display="block"; }

// Used to hide a div...
function hideDiv(divname) { if(document.getElementById(divname)) document.getElementById(divname).style.display="none"; }

// Used to enable a div...
function enableElement(id) { document.getElementById(id).disabled=false; }

// Used to disable a div...
function disableElement(id) { document.getElementById(id).disabled=true; }

// Used to hide all of the input select's on the current page...
function hideElements(elementType) { elementNodes = document.getElementsByTagName(elementType); for (loop=0; loop != elementNodes.length; loop++) elementNodes[loop].style.visibility = "hidden"; }

// Used to show all of the input select's on the current page...
function showElements(elementType) { elementNodes = document.getElementsByTagName(elementType); for (loop=0; loop != elementNodes.length; loop++) elementNodes[loop].style.visibility = "visible"; }
