var isIE = navigator.userAgent.indexOf("MSIE")+1;		//account for M$ and Opera
var isGecko = navigator.userAgent.indexOf("Gecko")+1;  //account for Mozilla/Netscape 6+/Safari/Konquerer

/***************** Cross-browser event object ******************/
function universalEvent() {
	this.evtSrc = null;
	this.evtSrcId = "";
	this.evtType = null;
	this.mouseX = null;
	this.mouseY = null;
	this.evtRef = null;
	this.key = null;
}

universalEvent.prototype.refresh = function (evtObj) {
	if (isIE) evtObj = event;
	if (isIE) {
		this.evtRef = evtObj;
		this.evtSrc = evtObj.srcElement;
		this.evtSrcId = evtObj.srcElement.id;
		this.evtType = evtObj.type;
		this.mouseX = evtObj.clientX + document.body.scrollLeft;
		this.mouseY = evtObj.clientY + document.body.scrollTop;
		this.key = evtObj.keyCode;
	}
	else if (isGecko) {
		this.evtRef = evtObj;
		this.evtSrc = evtObj.target;
		// next line to account for Safari/Konqueror returning the Text node rather than the Element node as the event source.
		while ( this.evtSrc.nodeType != 1 ) {
		    this.evtSrc = this.evtSrc.parentNode;
		}
		this.evtSrcId = this.evtSrc.id;
		this.evtType = evtObj.type;
		this.mouseX = evtObj.pageX;
		this.mouseY = evtObj.pageY;
		this.key = evtObj.charCode;
	}
}

universalEvent.prototype.cancel = function () {
	if (isIE) this.evtRef.returnValue=false;
	else if (isGecko) {
		if (this.evtRef.cancelable) this.evtRef.preventDefault();
	}
}
/**************************************************************/

//tick and X for interactions
var correct=new Image(25,25);
correct.src="assets/images/correct.gif";
var incorrect=new Image(25,25);
incorrect.src="assets/images/incorrect.gif";

/** this runOnLoad function is used in HDR site 
//to handle multiple onload items, this function queues script and runs it when the event triggers.
var onLoadScript="";
function runOnLoad(script) {
	if (typeof(script) == "object" || typeof(script) == "undefined") { 	//being called by event handler
		if (onLoadScript != "") eval(onLoadScript);
	}
	else onLoadScript += script;
}
window.onload = runOnLoad;
/** the above runOnLoad function is used in HDR site **/

//dynamically add a stylesheet link to the doc.
function insertStyleSheet(url) 
{
//alert ('in....1');
	var current_page		=	location.href;
	//alert (current_page.indexOf("accessible=true"));
	var accessible_version	=	current_page.indexOf("accessible=true");
	var print_version		=	current_page.indexOf("printable=true");
	print_version = 0;
	if ((accessible_version>0) || (print_version>0))
	{
		//alert ('in');
		return true; //if accessible version-return without including the stylesheet
	}
	else
	{
		if (arguments.length < 1) return false;
		var d,h;
		d = document.createElement("link");
		d.setAttribute("rel","stylesheet");
		d.setAttribute("type","text/css");
		d.setAttribute("href",url);
		h = document.getElementsByTagName("head")[0];
		h.appendChild(d);
		return true;
	}
}

//exactly as the function name suggests
function removeChildElements(ele) {
	var z;
	while (ele.childNodes.length > 0) ele.removeChild(ele.firstChild);
}

//add link relative to base href to anchor links with out link specified.
function fixAnchorLinks() {
	var a,i,l;
	l = getRelativeLink();
	a=document.getElementsByTagName("a");
	for (i=0;i<a.length;i++) {
		if (a[i].className == "anchor") {
			a[i].href = l + a[i].href.substr(a[i].href.indexOf("#"));
		}
	}
}

//Returns the link to the current page relative to the base href.
function getRelativeLink() {
	var a,l;
	//testing
	return true;
	if (a=document.getElementsByTagName("base")[0].href.length)
	{
		a=document.getElementsByTagName("base")[0].href.length;
		if ((l=location.href.indexOf("#")) > -1) return location.href.substring(a,l);
		else if ((l=location.href.indexOf("?")) > -1) return location.href.substring(a,l);
		else return location.href.substr(a);
	}
}

/*******************
Vista template hack
********************/
function fixLinks() {
	var a,i,l;
	a=document.getElementsByTagName("a");
	for (i=0;i<a.length;i++) {
		if (l=a[i].getAttribute("href")) {
			if (!(l.indexOf("http://")+1) && !(l.indexOf("mailto:")+1) && !(l.indexOf("ftp://")+1) && !(l.indexOf("javascript:")+1) && a[i].getAttribute("target") != "_blank") a[i].setAttribute("href",l + "?pagetype=vista");
		}
	}
}

if (typeof(isVista) != "undefined") {
	if (isVista) window.onload=fixLinks;
}
/*******************/


//copy child element from one node to another
function copyChildNodes(ele1,ele2) {
	if (!ele1.childNodes) return false;
	while (ele1.childNodes.length > 0) {
		ele2.appendChild(ele1.childNodes[0]);
	}
	return true;
}

// exactly the same functionality as DOMElement.insertBefore. Would have used prototyping if it wasn't for IE  >:-|
function insertAfter(newEle,refEle) {
	if (refEle.nextSibling == null) return refEle.parentNode.appendChild(newEle);
	else return refEle.parentNode.insertBefore(newEle,refEle.nextSibling);
}

// Used for finding the next suitable container element to insert comment div's into.
function findTopInlineNode (ele) {
	var foundOne=false,i,last,eles,j=0;
	if (arguments.length == 1) eles=["div","td","body"];
	else {
		eles = new Array();
		while (++j<arguments.length) eles.push(arguments[j]);
	}	
	do {
		for(i=0;i<eles.length;i++) {
			
			if (ele.nodeName.toLowerCase().indexOf(eles[i]) > -1) foundOne = true;
		}
		if (!foundOne) {
			last = ele;
			ele = ele.parentNode;
		}		
	} while (!foundOne);
	return last;
}
/** the following function is called in header pages on Accessibility version link; **/
function accessQueryString()
{
	var current_page		=	location.href;
	//alert (current_page.indexOf("accessible=true"));
	/**
	if (!(current_page.indexOf(".html")>0)) //this is to catch up the index/home pages
	{
		current_page		=	current_page+"index.html";	
	}
	**/
	var qStringExists		=	current_page.indexOf("?");
	if (qStringExists>0)
	{
		var accessExists	=	current_page.indexOf("accessible=true");
		if (accessExists<0)
		{
			var queryString	=	"&accessible=true";
		}	
	}
	else
	{
		var queryString		=	"?accessible=true";
	}
	var accessUrl			=	current_page+queryString;
	window.location.href	=	accessUrl;
	//return accessUrl;
}