//JavaScript methods available to all site screens


/*
	Mootools event - called when DOM is ready
	Add any method calls that need to be called for all pages on startup.
*/
window.addEvent('domready', function() {
	addJavaScriptPopups();
});




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

function formFocus() {
	// Set focus on the 1st visible element in the 1st form on the page
	var firstForm = document.forms[0];
	if (firstForm) {
	
		for (var i = 0; i < firstForm.elements.length; i++) {
		
			if (firstForm.elements[i].type != 'hidden' 
                    && firstForm.elements[i].nodeName != 'FIELDSET' 
					&& firstForm.elements[i].tabIndex <= 0
					&& firstForm.elements[i].style.visibility != 'hidden') {				
				firstForm.elements[i].focus();
				break;
			}
		}
	}
}
// Add as an onload event, taking care not to overwrite any existing ones
addLoadEvent(formFocus);


/*
	Open a popup window and center it one the screen 
*/
function openWin(URL, winName) {
	var windowWidth = 620;
	var windowHeight = 495;

	var centerWidth = (window.screen.width - windowWidth) / 2;
    var centerHeight = (window.screen.height - windowHeight) / 2;

	var win = window.open(URL, winName, 'width='+windowWidth+',height='+windowHeight+',left='+centerWidth+',top='+centerHeight+',status=0,scrollbars=yes,resizable=yes');
}


//submits the first form that is a parent of the specified element ID
function submitParentForm(elementId) {
	node = document.getElementById(elementId);
	
	found = false;
	while(!found) {
		if(node.nodeName == 'FORM') {
			node.submit();
			found = true;
		} else {
			node = node.parentNode;
		}
	}
}


/*
	Convert any links on class 'convertToJSPopup' to open the link in a JavaScript popup window
	sized 620x495, with no status bar.
	
	Links using this method should specify target='_blank' so non-JavaScript browsers will still open
	the link in a new window (but wont have the window resized).
*/
function addJavaScriptPopups() {
	//get array of all a tags of class convertToJSPopup
	linksToConvert = $$('a.convertToJSPopup');
	
	//interate through array and convert all links
	linksToConvert.each(function(item) {
		var href = item.getProperty('href');
		item.setProperty('target', '_self');
		item.setProperty('href', 'javascript:openWin(\'' + href + '\',\'Help\')');
	});
}

