
// 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;
	}
}

// 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){
	oElement.value = "";
}