/*****************************************
***	get, set, and delete cookie functions
*****************************************/
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if(( !start ) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}
	if(start == -1) {return null;}
	var end = document.cookie.indexOf(";", len);
	if(end == -1) {end = document.cookie.length;}
	return unescape(document.cookie.substring( len, end ));
}

function setCookie(name,value,expires,path,domain,secure) {
	var today = new Date();
	today.setTime(today.getTime());
	if(expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	} else {
		expires = 365 * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name+"="+escape(value) +
		((expires) ? ";expires="+expires_date.toGMTString() : "") + //expires.toGMTString()
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
}

function deleteCookie(name,path,domain) {
	if(getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function parseElementId(id) {
	var finalid = "";
	var pos = id.lastIndexOf("__");
	if (pos != -1 && id.length > pos+1) {
		finalid =  id.substr(pos+2);
	}
	finalid = parseInt(finalid,10);
	if (isNaN(finalid)) {
		return "";
	} else {
		return finalid;
	}
}

/*****************************************
***	get all elements with specific class attribute
*****************************************/
function getElementsByClass(searchClass,node,tag) {
	var classElements = [];
	if ( node === null ) {node = document;}
	if ( tag === null ) {tag = '*';}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/*****************************************
***	Update object's class attribute
*****************************************/
function updateClassName(obj,newclass,classtoreplace) {
	var modifiedclass = obj.className;
	if (classtoreplace !== "") {
		var classEStoreplace = classtoreplace.split(",");
		for(var i=0; i<classEStoreplace.length; i++) {
			if(modifiedclass.indexOf(classEStoreplace[i]) != -1) {
				modifiedclass = modifiedclass.replace(classEStoreplace[i],"");
			}
		}
	}
	if (newclass !== "") {
		var newclassES = newclass.split(",");
		for(var k=0; k<newclassES.length; k++) {
			if (modifiedclass.indexOf(newclassES[k]) == -1) {
				if (modifiedclass.length > 0) {
					modifiedclass = modifiedclass + " " + newclassES[k];
				} else {
					modifiedclass = newclassES[k];
				}
			}
		}
	}
	var modifiedclass2 = "";
	var spacedclass = modifiedclass.split(" ");
	for(var j=0; j<spacedclass.length; j++) {
		if(spacedclass[j].length > 0) {
			modifiedclass2 += spacedclass[j] + " ";
		}
	}
	obj.setAttribute("class", modifiedclass2);
	obj.setAttribute("className", modifiedclass2);
	//alert("final: |"+modifiedclass2+"|");
}

/*****************************************
***	toggle display of an object
***** toggleDisplay() and toggle()
*****************************************/
function toggleDisplay(obj,display) {
	var obj_to_toggle = obj;
	var newstatus = display;
	if(typeof(obj_to_toggle) == "string") {
		obj_to_toggle = document.getElementById(obj);
	}
	if(display == true) {
		obj_to_toggle.style.display = "block";
	} else if(display == false) {
		obj_to_toggle.style.display = "none";
	} else {
		if((obj_to_toggle.style.display == "none") || (obj_to_toggle.style.display === "")) {
			obj_to_toggle.style.display = "block";
			newstatus = true;
		} else {
			obj_to_toggle.style.display = "none";
			newstatus = false;
		}
	}
	return newstatus;
}
function toggle(obj) {
	return toggleDisplay(obj);
}


/*****************************************
***	add js event
*****************************************/
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
		return true;
	}
}

/*****************************************
***	add page load event
*****************************************/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
}

/*****************************************
***	remove all data from object
*****************************************/
function clearData(obj) {
	var obj_to_clear = obj;
	if(typeof(obj_to_clear) == "string") {
		obj_to_clear = document.getElementById(obj);
	}
	if(obj_to_clear !== null) {
		var idx = obj_to_clear.childNodes.length;
		for (var i = idx - 1; i >= 0; i--) {
			//alert(obj_to_clear.childNodes[i].nodeName);
			obj_to_clear.removeChild(obj_to_clear.childNodes[i]);
		}
	}
}

/*****************************************
***	function to get page and link urls
*****************************************/
function getScriptName() {
	return location.pathname.slice(0,location.pathname.lastIndexOf("../index.html")+1);
}

function getLinkPath(linkobj) {
	var href = linkobj.getAttribute("href").replace(location.protocol+"//"+location.host,"");
	return href.slice(0,href.lastIndexOf("../index.html")+1);
}

/*****************************************
***	popup window functions
*****************************************/
function popupHelp(Message) {
	var center = 1;
	var width = 600;
	var height = 400;
                                         
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion,10) >= 4 ) && (center)){
        xposition = (screen.width - 600) / 2;
        yposition = (screen.height - 400) / 2;
    }
    args = "width=" + width + "," +
		"height=" + height + "," +
		"location=1," +
		"menubar=1," +
		"resizable=1," +
		"scrollbars=1," +
		"status=1," +
		"titlebar=1," +
		"toolbar=1," +
		"hotkeys=0," +
		"screenx=" + xposition + "," +  //NN Only
		"screeny=" + yposition + "," +  //NN Only
		"left=" + xposition + "," +     //IE Only
		"top=" + yposition;           //IE Only
    newWindow = window.open(Message,"HelpWindow", args);
}
function popupWindow(pageToLoad, winName, args) {
	if (args === null) {
	    args = "location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1,hotkeys=0";
	}
	newWindow = window.open(pageToLoad, winName, args);
}

/************************************************************
***	shows rollover menu from behind select statement
************************************************************/
function hide_dropdowns(what){
	if(what=="in"){
		var anchors = document.getElementsByTagName("select"); 
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
				anchor.style.position="relative";
				anchor.style.top="0px";
				anchor.style.left="-2000px";
		}
	}else{
		var anchors = document.getElementsByTagName("select"); 
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
				anchor.style.position="relative";
				anchor.style.top="0px";
				anchor.style.left="0px";
		}
	}
}
