/*-------------------------------------------------
Title:		SAJ JavaScript functions
Author:		John Reed, jreed@cre8media.com
Updated:	Oct 28 2009
------------------------------------------------- */


// add load event function
function addLoadEvent(func) {var oldonload = window.onload; if (typeof window.onload != 'function') {window.onload = func;} else {window.onload = function() {oldonload(); func();}}}

// prepares links based on XML "rel" attribute
function prepareLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i<anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {anchor.target = "_blank";} if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "close") {anchor.onclick = function() {window.close();}}} }

// function to find Y position of a specific element
function findPosY(obj) {var curtop = 0; if(obj.offsetParent) { while(1) { curtop += obj.offsetTop; if(!obj.offsetParent) {break;} obj = obj.offsetParent; } } else if(obj.y) {curtop += obj.y;} return curtop;}

// set heights of content columns
function setHeights() {
	if(document.getElementById) {

		// the bottom of the wrapper and its y-position
		var bottom = document.getElementById('bottom');
		var bottomTop = findPosY(bottom);

		// the screen height and wrapper height
		var screenHeight = window.innerHeight;
		var wrapHeight = bottomTop;

		if(screenHeight > wrapHeight) {

			// set variables for content columns and footer
			var wrap = document.getElementById('wrap');		
			var content = document.getElementById('content');
			var sidebar = document.getElementById('sidebar');
			var footer = document.getElementById('footer');

			var contentTop = findPosY(content);
			var footerTop = findPosY(footer);

			// the header height
			var headerHeight = contentTop;

			// the footer height
			var footerHeight = bottomTop - footerTop;

			// the target height for the content column
			var contentHeight = screenHeight - footerHeight - headerHeight;
	
			// set the heights
			content.style.height = contentHeight + 'px';
			sidebar.style.height = contentHeight + 'px';
		}
	}
}

function checkForm() {
	if(document.getElementById('search')) {
		var form = document.getElementById('search');
		var q = document.getElementById('keywords');
		form.onsubmit = function() {
			if(q.value.length==0) {alert('You must enter a search term.'); q.focus(); return false;}
			else {return true;}
		};
	}
}

// load events
addLoadEvent(prepareLinks);
addLoadEvent(setHeights);
addLoadEvent(checkForm);