
// simple swap image function based on image name and new image source
function swapImage(imgName,newImg){
 	if (document.images){
		eval('document.' + imgName + '.src = "' + newImg + '"');
	}
}

// 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;
}

// generates a basic UUID based on time and a random number
function genUUID(){
	var uuid = new Date().getTime(); // milliseconds since 01/01/70
	uuid = uuid * Math.random() + ""; // multiply by a random number and convert to a string
	uuid = uuid.replace(".", ""); // remove decimal point
	return uuid;
}