
// simple swap image function based on image name and new image source
function swapImage(imgName,newImg){
 	if (document.images){
		eval('document.' + imgName + '.src = "' + newImg + '"');
	}
}

function formButtonRollover(pButton){
	// toggles imageName.gif and imageName_over.gif
	if(pButton.src){
		var currentSrc = pButton.src;
		var fileExtension = currentSrc.substring(currentSrc.length-3, currentSrc.length);		
		var fileName = currentSrc.substring(0, currentSrc.length-4);
		var newFileName = "";
		if(currentSrc.substring(currentSrc.length-9, currentSrc.length) ==  "_over." + fileExtension){
			newFileName = currentSrc.substring(0, currentSrc.length-9) + "." + fileExtension;
		}else{
			newFileName = fileName + "_over." + fileExtension;
		}
		pButton.src = newFileName;
	}
}

// Macromedia image preload function
// Example: preloadImages('file.gif', 'http://www.x.com/y.gif');
function preloadImages(){
	if(document.images){
		if(!document.imageArray) document.imageArray = new Array();
		var i,j = document.imageArray.length, args = preloadImages.arguments;
    
		for(i=0; i<args.length; i++){
			if (args[i].indexOf("#")!=0){
				document.imageArray[j] = new Image;
				document.imageArray[j++].src = args[i];
			}
		}
	}
}

// open a popup window - width and height are given as seperate parameters
// so that a centred position can be determined (if required)
function openWin(winURL,winName,winAttributes,winWidth,winHeight,isCentred){
	if(window.screen && isCentred){ // check for boolean attribute to centre the window
		var winLeft = (screen.width-winWidth)/2; // window left
		var winTop = (screen.height-winHeight)/2; // window top
		winAttributes += winAttributes.length ? "," : "";
		winAttributes += "left=" + winLeft + ",top=" + winTop; // append to attributes string
	}
	winAttributes += winAttributes.length ? "," : "";
	winAttributes += "width=" + winWidth + ",height=" + winHeight; // append width and height to attributes string
	winToOpen = window.open(winURL,winName,winAttributes); // define window
	if(window.focus){ // focus window if supported
		winToOpen.focus();
	}
}

// find an object by referenceing its ID in the DOM
function getObject(id){	
	if (document.getElementById){ // modern browsers
		return document.getElementById(id);
	}else if (document.all){ // IE 4
		return document.all[id];
	}else{ // ALL OTHERS... NO SUPPORT
		return null;
	}
}

// set html or textual content of an element
function setElementText(element,text,isPlainText){
	var oElement = getObject(element); // get element as an object
	if(isPlainText && oElement.innerText){
		oElement.innerText = text; // plain text
	}else if(oElement.innerHTML){
		oElement.innerHTML = text; // html
	}
}

// show/hide toggle for a given element
function showHide(element){
	oElement = getObject(element); // get object
	if(oElement){
		oElement.style.display = (oElement.style.display) ? "" : "none"; // switch
		if(oElement.style.display == ""){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

//hide a given element
function hideElement(element){
	oElement = getObject(element); // get object
	if(oElement){
		oElement.style.display = "none";
	}else{
		return false;
	}
}
//show a given element
function showElement(element){
	oElement = getObject(element); // get object
	if(oElement){
		oElement.style.display = "";
	}else{
		return false;
	}
}

// determine an elements x,y position
function getPos(element,isObject){
	if(isObject){
		oElement = element; // element is already an object
	}else{
		oElement = getObject(element); // get object
	}
	var pos = {}; // define return object
	pos.x = 0;
	pos.y = 0;
	if (document.getElementById || document.all){ // modern DOM
		while (oElement.offsetParent){
			// loop through all offset parents
			pos.x += oElement.offsetLeft // add parent offsetLeft value
			pos.y += oElement.offsetTop // add parent offsetTop value
			oElement = oElement.offsetParent; // set the next object
		}
	}else if (document.layers){ // maintian NS4 support...
		pos.x = oElement.x; // x position
		pos.y = oElement.y; // y position
	}
	return pos;
}

// xml format
function xmlFormat(sString){
	// need to escape < > ' " &
	//sString = sString.replace(/&amp;/g, "&");
	sString = sString.replace(/&/g, "&amp;");
	
	sString = sString.replace(/</g, "&lt;");
	sString = sString.replace(/>/g, "&gt;");
	sString = sString.replace(/'/g, "&apos;");
	sString = sString.replace(/"/g, "&quot;");
	
	return sString;
}

// print page
function printPage(){
	// check for print ability
	if(window.print){
		window.print();
	}
}

// clear a form field value
function clearField(oElement, pCheckString){
	if(oElement.value == pCheckString){
		oElement.value = "";
	}
}

// used by the 'form' tag as a select all for checkbox groups
function checkboxGroupSelectAll(oElement){
	if(oElement.parentNode){
		var checkBoxes = oElement.parentNode.getElementsByTagName("INPUT");
		for(var i=0; i<checkBoxes.length; i++){
			checkBoxes[i].checked = true;
		}
	}
}


// custom select all function to NOT check the last checkbox
function deSelectLast(oElement){
	if(oElement.parentNode){
		var checkBoxes = oElement.parentNode.getElementsByTagName("INPUT");
		checkBoxes[checkBoxes.length - 1].checked = false;
	}
}

// gets elements by class name
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}


// hide all elements by class name
function hideChildByClass(sClassName, sElementType, sWrapperID){
	
	var aAnswers = getElementsByClassName(sClassName, sElementType, getObject(sWrapperID));
	
	for(var i=0; i<aAnswers.length; i++){
		aAnswers[i].firstChild.style.display = "none";
	}	
}


// output a print link if the browser supports it
function writePrintLink(){
	// object detect on print functionality
	if(window.print){
		// can print, so write the html <a href="javaScript:void(0);" class="print">Print</a>
		document.write("<a href=\"javaScript:void(0);\" onclick=\"window.print();\" class=\"print\">Print<\/a>");
	}else{
		//document.write("&nbsp;");
	}
}

// relocate
function relocate(sLocation){
	self.location=sLocation;
}