//OPENS NEW WINDOW//
function newWin(url,size) {
	if (size == "s"){ var width = 300; var height = 300 }
	else if (size == "m"){ var width = 550; var height = 475 }
	else { var width = 700; var height = 500 }

	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) / 2);
	var winParms = ",top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
	if (parseInt(navigator.appVersion) >= 4) { window.focus(); }
	window.open(url,"","scrollbars=1,resizable=1" + winParms)
}

// MANAGE COOKIES
function createCookie(name,value,days,domain){
	if (days){
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else var expires = "";
	var theDomain = (domain) ? "; domain=" + domain : "";
	document.cookie = name + "=" + value + expires + "; path=/" + theDomain;
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i = 0; i < ca.length; i++){
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}

//	OPENS NEW WINDOW WHEN class ATTRIBUTE IS SET
function popup(){
	var classArray = new Array();
	if(document.getElementsByTagName){
		var aTags = document.getElementsByTagName("a");
		for (var i = 0; i < aTags.length; i++){
			var classArray = aTags[i].className.split(" ");
			for (var j = 0; j < classArray.length; j++){
				if (classArray[j] == "pop") aTags[i].onclick = function(){ newWin(this); return false }
				else if (classArray[j] == "popm") aTags[i].onclick = function(){ newWin(this,"m"); return false }
				else if (classArray[j] == "pops") aTags[i].onclick = function(){ newWin(this,"s"); return false }
			}
		}
	}
}

//LIMITS THE MAX CHARACTERS IN A TEXTAREA INPUT//
function textCounter(input, countid, maxlimit) {
	var maxLimit = (maxlimit) ? maxlimit : 2000;
	var counter = document.getElementById(countid);
	if(input.value.length > maxLimit) {
		counter.className = "tip error"
		counter.firstChild.nodeValue = input.value.length + " of " + maxLimit + " character limit used! Please edit text.";
	} else {
		counter.className = "tip"
		counter.firstChild.nodeValue = input.value.length + " of " + maxLimit + " character limit used.";
	}
}

//DELETE CONFIRMATION//
function ConfirmDelete(){ 
	if (confirm("REALLY DELETE THIS ITEM?\n----------\nWarning: you will not be able to undo this action.")) { 
		delete_record = true;
	} else {  
		delete_record = false;
	} return delete_record;
}

//CLEARS A SPECIFIED FIELD - USED FOR onFocus//
function textClear(input){
	if ( input.value == input.defaultValue ) input.value = ""
}

//RESTORES A SPECIFIC FIELD TO IT"S DEFAULT - USED FOR onBlur//
function textRestore(input) {
	if ( input.value == "" ) input.value = input.defaultValue
}

function cbCheckAll(){
	if(document.getElementById("cb-toggler")){
		var cbToggler = document.getElementById("cb-toggler");
		var cbRows = document.getElementsByName("cb");
		if (cbToggler.checked) {
			for (var i = 0; i < cbRows.length; i++) {
				cbRows[i].checked = true;
			}
		} else {
			for (var i = 0; i < cbRows.length; i++) {
				cbRows[i].checked = false
			}
		}
	} else alert("Your browser does not support this functionality.")
}
function cbCheckAllInit(){
	// check all checkboxes in table
	if(document.getElementById("cb-toggler")){
		var cbToggler = document.getElementById("cb-toggler");
		cbToggler.onclick = cbCheckAll;
		// if any checkbox becomes unchecked, uncheck the "check all" checkbox as well
		var cbRows = document.getElementsByName("cb");
		for (var i = 0; i < cbRows.length; i ++) {
			cbRows[i].onclick = function(){ if(this.checked == false) cbToggler.checked = false; }
		}
	}
}

//CONFIRMS DELETION OF SELECTED CHECKBOXES IN A LIST//
function cbDelete() { 
	var flag = 0 ;
	var cbRows = document.getElementsByName('cb');
	for (var i = 0; i < cbRows.length; i++) {
		if (cbRows[i].checked == true) flag = 1;
	}
	if (flag == 0 ) {
		alert ("NO SELECTIONS WERE MADE\n\nPlease select the items you wish to delete from your list.");
		return false; 
	} else {
		if (confirm("Are you sure you want to delete these items?  IMPORTANT: You will not be able to undo this action.")) {
			return true;
		} else {
		    return false;
		}
	}
}

//FORM VALIDATOR FUNCTIONS//
function isEmpty(s){ return ((s == null) || (s.length == 0)) }
function isWhitespace(s){
    var i;
    var whitespace = " \t\n\r";
	if (isEmpty(s)) return true;
	for (var i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	} return true
}

//FUNCTIONS TO REMOVE LEADING AND TRAILING SPACES FROM A STRING USED IN CheckInput()
function ltrim(string){
	while (1){
		if (string.substring(0, 1) != " ") break;
		string = string.substring(1, string.length);
	}
	return string;
}
function rtrim(string){
	while (1){
		if (string.substring(string.length - 1, string.length) != " ") break;
		string = string.substring(0, string.length - 1);
	}
	return string;
}
function trim(string){
	var tmpstr = ltrim(string);
	return rtrim(tmpstr);
}

//CHECKS THE SPELLING OF INPUT FIELD//
function checkSpelling(input){
	if (isWhitespace(input.value)){
		alert("SPELL CHECKER\n----------\nEnter your text in the textbox provided to check your spelling.");
		input.focus()
 	} else {
		var string = input.value
		var string = string.replace(/\n/gi ,"<br>")
		var string = escape(string)
		newWin("/app/global/spellcheck.adp?text=" + string,"m")
	}
}

// FOCUS THE NAVTAB ONMOUSEOVER
function clearTabs(){
	var thisTab = "";
	if(document.getElementById){
		var tabs = document.getElementById("nav").childNodes;
		for(var i = 0; i < tabs.length; i++){
			var thisTab = tabs[i];
			if(thisTab.tagName != undefined && thisTab.className == "on") thisTab.className = "";
		}
	}
}
function resetTabs(){
	if(document.getElementById){
		clearTabs();
		document.getElementById("nav-on").className = "on";
	}
}
function focusTab(){
	if(document.getElementById){
		var timer;
		var tabs = document.getElementById("nav").childNodes;
		for(var i = 0; i < tabs.length; i++){
			var thisTab = tabs[i];
			if(thisTab.tagName && thisTab.className != "opptab"){
				thisTab.onmouseover = function(){
					clearTimeout(timer)
					clearTabs();
					this.className = "on";
				}
				thisTab.onmouseout = function(){
					timer = setTimeout(resetTabs,1000)
				}
			}
		}
	}
}

// TOGGLER - TOGGLES HIDE/SHOW OF A BLOCK ELEMENT
// 1.	if you just pass an id, toggler just toggles between id-on and id-off
// 2.	if you pass argument "-slide" followed by another argument "n" where n is a series of space-separated ids...
// 	toggler will show id-on (and hide id-off) and hide all elements with n-on (and open all elements with n-off)
// 3.	if you pass argument "-radio" followed by another two arguments "n" and "m"...
//		where "n" is a reference to the current radio element and "m" is a value...
// 	toggler will show id-off if the value of the current radio element "n" equals "m"
function toggler(id){
	var ar = arguments;
	var on = (document.getElementById(id + "-on")) ? document.getElementById(id + "-on") : "";
	var off = (document.getElementById(id + "-off")) ? document.getElementById(id + "-off") : "";
	
	if (ar.length > 1) {
		for (var i = 0; i < ar.length; i++){
			switch (ar[i]) {
				case "-slide":
					var idArray = ar[i+1].split(" ");
					for (var j = 0; j < idArray.length; j++){
						if (idArray[j] != id) {
							var onSlide = (document.getElementById(idArray[j] + "-on")) ? document.getElementById(idArray[j] + "-on") : "";
							var offSlide = (document.getElementById(idArray[j] + "-off")) ? document.getElementById(idArray[j] + "-off") : "";
							onSlide.className = "hide";
							offSlide.className = "";
						}
						on.className = "";
						off.className = "hide";
					}
				case "-radio":
					theRadio = ar[i+1]
					theValue = ar[i+2]
					if (theRadio.value == theValue) {
						off.className = "";
					} else {
						off.className = "hide";
						// this code resets all form elements within the hidden block to null so as to not interfere with validator
						var elementsTypeArray = new Array("select","input","textarea"); // array of all elements to nullify
						for (var x = 0; x < elementsTypeArray.length; x++){
							theElements = off.getElementsByTagName(elementsTypeArray[x]);
							for (y = 0; y < theElements.length; y++){
								theElements[y].value = "";
							}
						}
					}
			}
		}
	} else {
		var onClass = (on.className == "hide") ? "" : "hide";
		var offClass = (off.className == "hide") ? "" : "hide";
		
		on.className = onClass;
		off.className = offClass;
	}
}
