/*****************************************
***	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";
}
}

/*****************************************
***	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 toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {el.style.display = 'none';}
	else {el.style.display = '';}
}


/*****************************************
***	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("/")+1);
}

function getLinkPath(linkobj) {
	var href = linkobj.getAttribute("href").replace(location.protocol+"//"+location.host,"");
	return href.slice(0,href.lastIndexOf("/")+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);
}

function viewNewsStory(x) {
	var url = '/ClassLibrary/Page/News/view.cfm?id='+x;
	if (window.XMLHttpRequest) {req = new XMLHttpRequest();} 
	else if (window.ActiveXObject) { 
		try {req = new ActiveXObject("Msxml2.XMLHTTP");} 
		catch (e) { 
			try {req = new ActiveXObject("Microsoft.XMLHTTP");} 
			catch (e){} 
		} 
	}
//	req.onreadystatechange = processRequest;
	req.open("POST", url, true);
	req.send(null);
}

