/*
	getElementsByClassName function
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

//toggle table row colors
function toggleRows() {
	var oddRows = getElementsByClassName("odd");
	var evenRows = getElementsByClassName("even");
	
	if (oddRows!=null && evenRows!=null) {
		for (i=0; i<oddRows.length; i++) oddRows[i].className = "even";
		for (i=0; i<evenRows.length; i++) evenRows[i].className = "odd";
	}
}

//Unhide elements
function showElements(className) {
	var elements = getElementsByClassName(className);
	
	if (elements!=null) {
		for (i=0; i<elements.length; i++) {
			elements[i].style.visibility = "";
			elements[i].style.width = "auto";
			elements[i].style.height = "auto";
		}
	}
}

function hideElements(className) {
	var elements = getElementsByClassName(className);
	
	if (elements!=null) {
		for (i=0; i<elements.length; i++) {
			elements[i].style.visibility = "hidden";
			elements[i].style.width = "0px";
			elements[i].style.height = "0px";
		}
	}
}

//Toggle page loading animation (used for first time load)
function toggleAnim(id) {
	if (id==null || id=="") return; 
	
	var obj = document.getElementById(id);
	if (obj==null) return;
	
	var txt = obj.innerHTML;
	
	var anim_txt = "<img id='refresh' src='/people/images/spinner.gif' style='position: absolute; top: 25%; left: 50%; width: 50px; height:50px; z-index:9999999;' />";
	
	if (txt==null || txt.length<0) obj.innerHTML = anim_txt;
	else if (txt==anim_txt) obj.innerHTML = "";
}

//save current page into anchor part of URL so that it can be used for browser history
function saveHist(pg, pgnum) {
	if (pg!=null) {
		var z = pg.lastIndexOf("/");
		
		if (z>0) {
			var script = pg.substring(z+1); //get the script URL & query string params
			z = script.indexOf("?");
			if (z>0 && pgnum!=null) {
				script = script.substring(0, z);
				script = script + "?selectedPageCounter=" + pgnum;
			}
			z = script.indexOf("#");
			if (z>0) script = script.substring(0, z);
			
			var current = location.href;
			
			z = current.indexOf("#");
			if (z>0) current = current.substring(0, z);
			
			var target = current + "#" + script;
			location.href = target;
		}
	}	
}

function getHist() {
	var pg = location.href;
	
	var z = pg.lastIndexOf("/");
	var y = pg.lastIndexOf("#");
	
	if (z>0 && y>z) pg = pg.substring(y+1); //get the script URL & query string params
	
	return pg;
}

//save page history
function setPageHist(pg) {
	if (pg==null || pg=="") return;
	
	var url = "/people/pages/content/page_num.jsp";
	
	var xmlhttp;
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
	else if (window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
	else return;
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.status==200) {
				var response = xmlhttp.responseText;
				saveHist(pg, response);
			}
		}
	}

	var mytime = "?ms="+new Date().getTime();
	url = url + mytime;	
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

/*
	fetchNoContent: call a script silently in backgound
		url: page to check
		target: page to redirect to if required, otherwise null. optional field
*/
function fetchNoContent(url, target) {
	if (url==null || url=="") url = location.href;
	
	var xmlhttp;
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
	else if (window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
	else return;
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) { //no action required
			if (target!=null) {
				var response = xmlhttp.responseText;
				if (response.indexOf("<html")<1 && response.indexOf("</html>")<1) window.open(target, "_parent", "");
			}
		}
	}
	
	if (url.indexOf("?")>0) url = url + "&";
	else url = url + "?";
	
	var mytime = "ms="+new Date().getTime();
	url = url + mytime;	
		
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

/*
	displayHTML: fetch content & display in a <div> or <span>
		url: page to fetch
		id: element id to display content
		anim_id: element id to display animation, null if animation not required. optional field
		save_pg: if this is used, page history will be used. this is for sections with paginations
*/
function displayHTML(url, id, anim_id, save_pg) {
	if (id==null || id=="") return; 
	
	var obj = document.getElementById(id);
	if (obj==null) return;
	
	var anim_obj = null;
	if (anim_id!=null) anim_obj = document.getElementById(anim_id);
	
	if (url==null || url=="") {
		if (save_pg!=null) url = getHist();
		else url = location.href;
		
		if (anim_obj!=null && anim_id!=id)
			obj.innerHTML = "<img id='refresh' src='/people/images/spinner.gif' style='position: absolute; top: 40%; left: 50%; width: 50px; height:50px; z-index:9999999;' />";
	}
	
	var xmlhttp;
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
	else if (window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
	else return;
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (xmlhttp.status==200) {
				var response = xmlhttp.responseText;
			
				//check that it is not a HTML document, e.g. error page
				if (response.indexOf("<html")<1 && response.indexOf("</html>")<1) {
					response = response.trim();
					obj.innerHTML = response;
					if (save_pg!=null) setPageHist(url);
				}
			}
			
			if (anim_obj!=null && anim_id!=id) anim_obj.innerHTML = ""; //clear animation
		}
	}
	
	if (url.indexOf("?")>0) url = url + "&";
	else url = url + "?";

	var mytime = "ms="+new Date().getTime();
	url = url + mytime;	
	
	//display animtion
	if (anim_obj!=null) anim_obj.innerHTML = "<img src='/people/images/spinner.gif' style='position: absolute; top: 40%; left: 50%; width: 50px; height:50px; z-index:999999;' />";
	
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

/*
	fetchReload: fetch a page, but load content from another page when complete
	
	url: page to fetch
	target: page to display (also using AJAX)
	id: element id to display content
	anim_id: element id to display animation, null if animation not required. optional field
*/
function fetchReload(url, target, id, anim_id, save_pg) {
	if (url==null || url=="") return;
	
	var anim_obj = null;
	if (anim_id!=null) anim_obj = document.getElementById(anim_id);
	
	var xmlhttp;
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
	else if (window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
	else return;
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) displayHTML(target, id, anim_id, save_pg);
		else {
			if (anim_obj!=null && anim_id!=id) anim_obj.innerHTML = ""; //clear animation
		}
	}
	
	if (url.indexOf("?")>0) url = url + "&";
	else url = url + "?";

	var mytime = "ms="+new Date().getTime();
	url = url + mytime;	
	
	//display animtion
	if (anim_obj!=null) anim_obj.innerHTML = "<img src='/people/images/spinner.gif' style='position: absolute; top: 40%; left: 50%; width: 50px; height:50px; z-index:999999;' />";
		
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

/*
	loadContentWithAltRows: load content which contains table with alternate row coloring
		id: element id to display content
		count: used for toggle row color. use "1" to change color (by switching odd & even rows). optional field
		url: page to fetch content. optional field
		anim_id: element id to display animation, null if animation not required. optional field
		
	- alternate_rows.js is assumed to included on the page calling this function
	- if anim_id is not used, animation is only shown when content loads for the first time
*/
function loadContentWithAltRows(id, count, url, anim_id, save_pg) {
	if (id==null || id=="") return;
	
	var obj = document.getElementById(id);
	if (obj==null) return;
	
	if (count==null) count = 0;
	if (count<0) count = 0;
	count = count%2;
	
	var xmlhttp;
	if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
	else if (window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
	else return;
	
	if (url==null || url=="") {
		if (save_pg!=null) url = getHist();
		else url = location.href;
	}
	
	var anim_obj = null;
	if (anim_id!=null) anim_obj = document.getElementById(anim_id);
		
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			if (anim_obj==null) toggleAnim(id);
			
			if (xmlhttp.status==200) {
				var response = xmlhttp.responseText;
				
				//check that it is not a HTML document, e.g. error page
				if (response.indexOf("<html")<1 && response.indexOf("</html>")<1)  {
					obj.innerHTML = response;
					alternate_init(); //reload alternate row coloring, from alternate_rows.js
					if (count==1) toggleRows();
					if (save_pg!=null) setPageHist(url);
				}
			}
			
			if (anim_obj!=null) anim_obj.innerHTML = "";
		}
	}
	
	if (url.indexOf("?")>0) url = url + "&";
	else url = url + "?";
	
	var mytime = "ms="+new Date().getTime();
	url = url + mytime;
	
	if (anim_obj==null) toggleAnim(id);
	else anim_obj.innerHTML = "<img id='refresh' src='/people/images/spinner.gif' style='position: absolute; top: 40%; left: 50%; width: 50px; height:50px; z-index:999999;' />";
		
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
