//GET RID OF IMAGE FLICKERING UPON HOVER ON IE6//
try {
document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

//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(message){
	if (!message) {
		var message = "REALLY DELETE THIS ITEM?\n----------\nWarning: you will not be able to undo this action."
	} 
	
	if (confirm(message)) { 
		delete_record = true;
	} else {  
		delete_record = false;
	} return delete_record;
}

//Toggle Shareability//
function ConfirmToggle(message){

	if (!message) {
		var message = "REALLY TOGGLE MY SHAREABILITY?"
	} 
	
	if (confirm(message)) { 
		delete_record = true;
	} else {  
		delete_record = false;
	} return delete_record;
}

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(message1,message2) { 
	if (!message1) {
		var message1 = "NO SELECTIONS WERE MADE\n\nPlease select the items you wish to delete from your list."
	}
	
	if (!message2) {
		var message2 = "Are you sure you want to delete these items?\n----------\nIMPORTANT: You will not be able to undo this action."
	} 
	
	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 (message1);
		return false; 
	} else {
		if (confirm(message2)) {
			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,message){

	if (!message) {
		var message = "SPELL CHECKER\n----------\nEnter your text in the textbox provided to check your spelling."
	} 
	
	if (isWhitespace(input.value)){
		alert(message);
		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.indexOf("on") > -1) thisTab.className = thisTab.className.replace("on", "");
		}
	}
}
function resetTabs(){
	if(document.getElementById){
		clearTabs();
		document.getElementById("nav-on").className = 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.indexOf("opptab") == -1 ){
				thisTab.onmouseover = function(){
					clearTimeout(timer)
					clearTabs();
					this.className = this.className + " " + "on";
				}
				thisTab.onmouseout = function(){
					timer = setTimeout(resetTabs,1000)
				}
			}
		}
	}
}

function toggle(id){
	// Toggle the visiblility between the block elements at {id}-on and {id}-off.
	// id : the prefix id of the block element that will be toggled.
	
	var on = document.getElementById(id + "-on");
	var off = document.getElementById(id + "-off");
	if(on){
		on.className = (on.className.indexOf("hide") > -1) ? on.className.replace("hide", "") : on.className + " hide";
	}
	if(off){
		off.className = (off.className.indexOf("hide") > -1) ? off.className.replace("hide", "") : off.className + " hide";
	}
	
	var icon = document.getElementById(id + "-expand");
	
	if(icon){
		icon.className = (icon.className.indexOf("expand") > -1) ? icon.className.replace("expand", "collapse") : icon.className.replace("collapse", "expand");		
	}	
}

function toggleSlide(id,idArray){
	// Set the block element at {id}-on as visible and hide all other block elements with id {idArray[n]}-on.
	// The block element at {id}-off is also hidden.
	// id : the prefix id of the block element that will be affected, where the block element id is [id]-on.
	// idArray : an array of space-separated prefix ids for all block elements that will be hidden by toggleSlide.
	
	var idArray = idArray.split(" ");
	for (var j = 0; j < idArray.length; j++){
		var thisId = document.getElementById(idArray[j] + "-on");
		if (idArray[j] == id) {
			thisId.className = thisId.className.replace("hide", "")
		} else {
			thisId.className = (thisId.className.indexOf("hide") > -1) ? thisId.className : thisId.className + " hide";
		}
	}
}

function toggleRadio(id,theRadio,theValue){
	// Toggle the block element at {id}-off between hidden/visible when the radio <input> at {theRadio} with value {theValue} is checked.
	// All <form> elements within the block element {id}-off is set to null and to not required when {id}-off is hidden.
	// id : the prefix id of the block element that will be toggled, where the block element id is {id}-off.
	// theRadio : the reference to the radio <input> that will determine whether {id}-off is hidden or visible.
	// theValue : the value of the radio <input> at theRadio that tells toggleRadio to make {id}-off hidden.

	var thisId = document.getElementById(id + "-off"); // define the block element {id}-off
	var elementsTypeArray = new Array("select","input","textarea"); // define the array of all <form> elements within {id}-off
	
	// the current checked radio value is not equal to theValue
	// --> hide the block element and nullify all the <form> elements within.
	if (theRadio.value != theValue) {
		thisId.className = (thisId.className.indexOf("hide") > -1) ? thisId.className : thisId.className + " hide"; 
		
		for (var x = 0; x < elementsTypeArray.length; x++){
			var theElements = thisId.getElementsByTagName(elementsTypeArray[x]);
			for (var y = 0; y < theElements.length; y++){
				theElements[y].value = "";
				if ( !theElements[y].className.toLowerCase().match("reqhide") ) {
					theElements[y].className = theElements[y].className.replace("req", "reqHide");
				}
			}
		}
	// the current checked radio value is equal to theValue
	// --> show the block element and reset all <form> elements within to their original value.
	} else {
		thisId.className = thisId.className.replace("hide", "");	
		
		for (var x = 0; x < elementsTypeArray.length; x++){
			var theElements = thisId.getElementsByTagName(elementsTypeArray[x]);
			for (var y = 0; y < theElements.length; y++){
				thisElement = theElements[y];
				thisElement.className = thisElement.className.replace("reqHide", "req");
				
				// reset the form element value to it's original value
				switch (thisElement.tagName){
					case "SELECT" :
						for (var z = 0; z < thisElement.options.length; z++){
							if (thisElement.options[z].defaultSelected) thisElement.options[z].selected = true;
							else  thisElement.options[z].selected = false;
						}
						break;
					default :
						// <input> and <textarea> elements
						thisElement.value = thisElement.defaultValue
						break;
				}
			}
		}
	}
}

//JUST PROVIDE THE NAME OF THE DIV's WITHOUT THE NUMBERS.
function addMore(div_id) {

	for (i = 1; i <= 10; i++) {
		var div = document.getElementById(div_id+i);
		
		if (div && (div.className.indexOf("hide") > -1) ) {
			div.className = div.className.replace("hide", "");
			div.className = div.className.replace("reqHide", "req");
		
		// 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++) {
				var theElements = div.getElementsByTagName(elementsTypeArray[x]);
				for (var y = 0; y < theElements.length; y++){
					theElements[y].className = theElements[y].className.replace("hide", "");
					theElements[y].className = theElements[y].className.replace("reqHide", "req");
				}
			}
			return;
		}
	}

}

function remove(divID) {
	var div = document.getElementById(divID);
	
	if (div && (div.className.indexOf("hide") == -1) ) {
			div.className = div.className + " hide";
			div.className = div.className.replace("req", "reqHide");
		
		// 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++) {
				var theElements = div.getElementsByTagName(elementsTypeArray[x]);
				for (var y = 0; y < theElements.length; y++){
					theElements[y].className = theElements[y].className + " hide";
					theElements[y].className = theElements[y].className.replace("req", "reqHide");
					theElements[y].value = "";
				}
			}
			return;
	}
}

function GoToWithReferer (url) {  
// This function will force the IE to send the referrer when using location.href
    var fakeLink = document.createElement ("a");
    if (typeof(fakeLink.click) == 'undefined')
        location.href = url;  // sends referrer in FF, not in IE
    else {
        fakeLink.href = url;
        document.body.appendChild(fakeLink);
        fakeLink.click();   // click() method defined in IE Only
    }    
}

function removeURLParam(url, param){
// REMOVE PARAM FROM THE PASSED URL IF IT EXIST
	var start = url.indexOf(param+'=');
	if (start != -1) {
		var end = url.indexOf('&',start+1);
		if ( end == -1 )
			var url = url.substring(0,start-1);
		else
			var url = url.substring(0,start) + url.substring(end+1);
		
	}
	return url;
}