
// --- SELECT VALUE IN A DROPDOWN BOX --- //
function populateDropDown(theField, theValue)
{
	for (i=0; i<theField.length; i++) 
	{
		if(theField.options[i].value == theValue)
		{
			theField.options[i].selected = true;
		}
	}
}

// --- SELECT BOX JUMP TO URL --- //
function changePage(thePage, theField, theAnchor) {

	if (typeof(theAnchor) == "undefined" ) {
		theAnchor = "";
	}
	oLength = thePage.length;
	for (i=0; i<theField.length; i++) {
		if (!theField.disabled && theField.options[i].value != "" && theField.options[i].selected) {
			if (thePage.length > oLength) {
				thePage = thePage + "," + theField.options[i].value;
			} else {
				thePage = thePage + theField.options[i].value;
			}
		}
	}
	document.body.style.cursor = "wait";
	location.href = thePage + theAnchor;
	theField.selectedIndex = 0;
}

// --- AUTO OPTION FINDER FOR SELECT BOX --- //
var dKeys = "";
var theTimer = "";
var searchField = "";
var searchPoint = 0;
function autoFinder(theField, key) {
	document.body.style.cursor = "wait";
	searchField = theField;
	clearTimeout(theTimer);
	dKeys += String.fromCharCode(key);
	dKeys = dKeys.toLowerCase();
	theTimer = setTimeout("findOption(searchField)",750);
	if (dKeys.length > 1) {
		return false;
	}
}
function findOption(theField) {
	searchPoint = theField.selectedIndex;
	for (i=searchPoint; i<theField.length; i++) {
		oText = theField.options[i].text.toLowerCase();
		oText = oText.replace(/[^a-zA-Z 0-9()]+/g,'');
		if (oText.indexOf(dKeys) == 0) {
			theField.value = theField.options[i].value;
			break;
		}
	}
	document.body.style.cursor = "";
	dKeys = "";
}

// --- SET LEFT FOR POP-UP WINDOWS --- //
function setLeft() {
	if (document.all) {
		theLeft = window.screenLeft;
	}
	else if (document.layers) {
		theLeft = window.screenX;
	}
	else {
		theLeft = 0;
	}
	return(theLeft);
}


// --- POP-UP WINDOW --- //
function displayWindow(url, width, height, windowname, resizable) {

	if (typeof(windowname) == "undefined" ) {
		windowname = "";
	}
	if (typeof(resizable) == "undefined" ) {
		resizable = "no";
	}

	var theWindow = window.open(url,windowname,'width=' + width + ',height=' + height + ',resizable=' + resizable + ',scrollbars=yes,menubar=no,status=no,top=0,left=' + setLeft());
}



// --- OPEN CENTERED POP-UP --- //
function openCenteredWin(theURL, theWidth, theHeight) {
	if (document.all || document.layers) {
	   w = screen.availWidth;
	   h = screen.availHeight;
	}
	else {
	   w = window.innerWidth;
	   h = window.innerHeight;
	}
	var leftPosition = (w-theWidth)/2;
	var topPosition = (h-theHeight)/2;
	var centeredWin = window.open(theURL,'centeredWin','width=' + theWidth + ',height=' + theHeight + ',top=' + topPosition + ',left=' + leftPosition + ',resizable=no,scrollbars=no,menubar=no,status=no');
}


// --- VERIFY DELETE --- //
function verifyDelete(Message,DeletePage) {
	if(confirm(Message)) {
		location.replace(DeletePage);
		return true;
	}
}


// --- ADD AN OPTION TO A SELECT MENU --- //
function addOption(theField,theText,theValue,theColor) {
	theField.selectedIndex = -1;
	if (!theColor) {
		theColor = "";
	}
	var defaultSelected = true;
	var selected = true;
	var optionName = new Option(theText, theValue, defaultSelected, selected);
	var length = theField.length;
	theField.options[length] = optionName;
	theField.options[length].style.color = theColor;
}


// --- ALPHABETICALLY SORT OPTIONS IN A SELECT MENU --- //
function sortOptions(theField) {
	var optionArrayLookUp = new Array();
	var optionArray = new Array();
	for (i=0; i<theField.options.length; i++) {
		optionArrayLookUp[theField.options[i].text] = theField.options[i].value;
		optionArray[i] = theField.options[i].text;
	}
	theField.length = 0;
	optionArray.sort();
	for(c=0; c<optionArray.length; c++) {
		addOption(theField,optionArray[c],optionArrayLookUp[optionArray[c]]);
   }
}


// --- CHECK/UNCHECK ALL CHECKBOXES --- //
function checkAll(theField, isChecked) {
	if (theField.length == 1 && !theField.disabled) {
		theField.checked = isChecked;
	}
	if (theField.length > 1) {
		for (i=0; i<theField.length; i++) {
			if (!theField[i].disabled) {
				theField[i].checked = isChecked;
			}
		}
	}
	else if (theField) {
		theField.checked = isChecked;
	}
}


// --- CHECK/UNCHECK ALL CHECKBOXES BY ID --- //
function checkAllByID(theField, theID, isChecked) {
	if (theField[theID].length == 1) {
		theField.checked = isChecked;
	}
	if (theField[theID].length > 1) {
		for (i=0; i<theField[theID].length; i++) {
			if (!theField[theID].disabled) {
				theField[theID][i].checked = isChecked;
			}
		}
	}
}


// --- SELECT/DESELECT ALL OPTIONS --- //
function selectAll(theField, isSelected) {
	for (i=0; i<theField.length; i++) {
		if (!theField.disabled && theField.options[i].value != "") {
			theField.options[i].selected = isSelected;
		}
	}
}


// --- DISABLE/ENABLE ALL FIELDS IN A FORM --- //
function disableForm(theForm, ignoreField, disableIt, clearValues) {
	if (typeof(ignoreField) == "object") {
		var ignoreFieldName = ignoreField.name;
	}
	else {
		var ignoreFieldName = "";
	}
	var theColor = "";
	var radioNameArray = new Array;
	var radioNameExists;
	
	if (typeof(clearValues) == "undefined" ) {
		clearValues = true;
	}
	
	if (disableIt) {
		theColor = "#E1E1E1";
	}
	for (i=0; i<theForm.elements.length; i++) {
		if (theForm.elements[i].type != "submit" && theForm.elements[i].type != "button" && theForm.elements[i].type != "hidden" && theForm.elements[i].name != ignoreFieldName) {
			if (theForm.elements[i].type != "radio" && theForm.elements[i].type != "checkbox") {
				if (disableIt && clearValues) {
					theForm.elements[i].value = "";
				}
				theForm.elements[i].style.backgroundColor = theColor;
			}
			if (theForm.elements[i].type == "radio" && disableIt) {
				radioNameExists = false;
				for (x=-1; x<radioNameArray.length; x++) {
					if (radioNameArray[x+1] == theForm.elements[i].name) {
						radioNameExists = true;
						break;
					}
				}
				if (!radioNameExists) {
					radioNameArray[x+1] = theForm.elements[i].name;
					theForm.elements[i].checked = true;
				}
			}
			else if (theForm.elements[i].type == "checkbox" && disableIt && clearValues) {
				theForm.elements[i].checked = false;
			}
			else if (theForm.elements[i].type.indexOf("select") > -1 && disableIt && clearValues) {
				theForm.elements[i].selectedIndex = -1;
			}
			theForm.elements[i].disabled = disableIt;
		}
	}
}


// --- MOVE OPTION UP OR DOWN --- //
var continueToMove = false;
var tempField = "";
var tempDirection = "";
function callmoveOptionUpDown(theField, theDirection) {
	tempField = theField;
	tempDirection = theDirection;
	if (continueToMove) {
		setTimeout("moveOptionUpDown(tempField, tempDirection); callmoveOptionUpDown(tempField, tempDirection);",200);
	}
	else {
		clearTimeout();
	}
}
function moveOptionUpDown(theField, theDirection) {
	var originalText = "";
	var originalValue = "";
	var originalColor = "";
	var swapText = "";
	var swapValue = "";
	var swapPosition = 0;
	var swapColor = "";
	var okToMove = false;
	
	if (theField.selectedIndex >= 0) {
		originalPosition = theField.selectedIndex;
		originalText = theField.options[theField.selectedIndex].text;
		originalValue = theField.options[theField.selectedIndex].value;
		originalColor = theField.options[theField.selectedIndex].style.color;
		if (theDirection == "up" && originalPosition > 0) {
			swapPosition = originalPosition - 1;
			okToMove = true;
		}
		else if (theDirection == "down" && originalPosition < theField.options.length-1) {
			swapPosition = originalPosition + 1;
			okToMove = true;
		}
		if (okToMove) {
			swapText = theField.options[swapPosition].text;
			swapValue = theField.options[swapPosition].value;
			swapColor = theField.options[swapPosition].style.color;
			
			theField.options[swapPosition].text = originalText;
			theField.options[swapPosition].value = originalValue;
			theField.options[swapPosition].style.color = originalColor;
			
			theField.options[originalPosition].text = swapText;
			theField.options[originalPosition].value = swapValue;
			theField.options[originalPosition].style.color = swapColor;

			for (i=0; i<theField.options.length; i++) {
				theField.options[i].selected = false;
			}
			theField.options[swapPosition].selected = true;
		}
	}
}


//MOVE OPTIONS BETWEEN SELECT MENUS
function moveSelected(fromBox, toBox, sortItems) {
	if (typeof(sortItems) == "undefined" ) {
		sortItems = true;
	}
	var arrayFromBox = new Array();
	var arrayToBox = new Array();
	var arrayLookup = new Array();
	for (i = 0; i < toBox.options.length; i++) {
		arrayLookup[toBox.options[i].text] = toBox.options[i].value;
		arrayToBox[i] = toBox.options[i].text;
	}
	var fromLength = 0;
	var toLength = arrayToBox.length;
	for(i = 0; i < fromBox.options.length; i++) {
		arrayLookup[fromBox.options[i].text] = fromBox.options[i].value;
		if (fromBox.options[i].selected && fromBox.options[i].value != "") {
			arrayToBox[toLength] = fromBox.options[i].text;
			toLength++;
		}
		else {
			arrayFromBox[fromLength] = fromBox.options[i].text;
			fromLength++;
	   }
	}
	if (sortItems) {
		arrayFromBox.sort();
		arrayToBox.sort();
	}
	fromBox.length = 0;
	toBox.length = 0;
	for(c=0; c < arrayFromBox.length; c++) {
		var newOption = new Option();
		newOption.value = arrayLookup[arrayFromBox[c]];
		newOption.text = arrayFromBox[c];
		fromBox[c] = newOption;
	}
	for(c=0; c < arrayToBox.length; c++) {
		var newOption = new Option();
		newOption.value = arrayLookup[arrayToBox[c]];
		newOption.text = arrayToBox[c];
		toBox[c] = newOption;
	}
}
			

//MOVE ALL OPTIONS BETWEEN SELECT MENUS
function moveAll(fromBox, toBox) {
	selectAll(fromBox, true);
	moveSelected(fromBox, toBox);
}


// --- VALIDATE UPLOAD FIELD --- //
function checkUpload(theField, extList, allowSpaces) {
	//Set value of allowSpaces if it is not passed to function
	if (typeof(allowSpaces) == "undefined" ) {
		allowSpaces = false;
	}
	//Process function
	if (theField.value.length) {
		var extArray = extList.split(",");
		var theFilePath = theField.value.split("\\");
		var theFile = theFilePath[theFilePath.length-1].toLowerCase();
		var formatIsOK = false;
		for (i=0; i<extArray.length; i++) {
			if (theFile.indexOf(Trim(extArray[i])) > -1 ) {
				formatIsOK = true;
				break;
			}
		}
		if (!formatIsOK) {
			alert("The file you have selected cannot be uploaded.\n\nPlease select one of the following file formats:\n" + extList);
			theField.focus();
			return false;
		}
		if (!allowSpaces && theFile.indexOf(" ") > -1) {
			alert("The file you have selected cannot be uploaded.\n\nPlease rename the file so that there are no blank spaces in the name, then re-upload it.")
			theField.focus();
			return false;
		}
		if (theFile.indexOf("#") > -1) {
			alert("The file you have selected cannot be uploaded.\n\nPlease rename the file so that there are no pound signs (#) in the name, then re-upload it.");
			return false;
		}
		return true;
	}
	else {
		return true;
	}
}


// --- REMOVE DOLLAR SIGNS FROM TEXT INPUT --- //
function removeDollarSigns(theField) {
	var theString = theField.value;
	theString = theString.replace("$", "");
	theString = theString.replace(" ", "");
	theField.value = theString;
}


// --- FORMAT PHONE NUMBERS --- //
function formatPhone(theField) {
	var theValue = theField.value;
	if (theValue.length == 10 && !isNaN(theValue)) {
		theValue = theValue.toString();
		theValue = theValue.substring(0,3) + "-" + theValue.substring(3,6) + "-" + theValue.substring(6,10);
	}
	theField.value = theValue;
}


// --- OPEN PROCESSING POP-UP --- //
function openProcessingWin() {
	var isComplete = false;
	if (document.all || document.layers) {
	   w = screen.availWidth;
	   h = screen.availHeight;
	}
	else {
	   w = window.innerWidth;
	   h = window.innerHeight;
	}
	var popW = 300, popH = 100;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	var ProcessingWin = window.open('processing.cfm','ProcessingWin','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',resizable=no,scrollbars=no,menubar=no,status=no');
}


// ---  TRIM FUNCTIONS --- //
function LTrim(theString) {
	var whitespace = new String(" \t\n\r");
	var s = new String(theString);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j = 0;
		var i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
			j++;
		}
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(theString) {
	var whitespace = new String(" \t\n\r");
	var s = new String(theString);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
			i--;
		}
		s = s.substring(0, i+1);
	}
	return s;
}

function Trim(theString) {
	return RTrim(LTrim(theString));
}


// --- STRING REPLACE --- //
function Replace(string, replaceText, replaceWith) {
    var strLength = string.length, txtLength = replaceText.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(replaceText);
    if ((!i) && (replaceText != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newString = string.substring(0,i) + replaceWith;

    if (i+txtLength < strLength)
        newString += Replace(string.substring(i+txtLength,strLength),replaceText,replaceWith);

    return newString;
}


// --- SELECT PROPER ORGANIZATION FROM DROP-DOWN --- //
function setOrganization(theField) {
	var theValue = theField.options[theField.selectedIndex].value;
	if (theValue > 0) { 
		if (theValue == theField.options[theField.selectedIndex-1].value) {
			theField.selectedIndex = theField.selectedIndex-1;
		}
	}
	else if (theValue == 0) {
		theField.selectedIndex = 0;
	}
}


// --- SHOW / HIDE DIV --- //
function displayDiv(theDivID, showDiv) {
	//Set value of showDiv if it is not passed to function
	if (typeof(showDiv) == "undefined" ) {
		if (document.getElementById(theDivID).style.display == "") {
			var showDiv = "false";
		}
		else {
			var showDiv = "true";
		}
	}
	//Show/hide div
	if (showDiv) {
		document.getElementById(theDivID).style.display = "";
		document.getElementById(theDivID).style.visibility = "visible";
	}
	else {
		document.getElementById(theDivID).style.display = "none";
		document.getElementById(theDivID).style.visibility = "hidden";
	}
}


// --- SHOW / HIDE SUB-NAVIGATION --- //
function hideNavDivs() {
	var divArray = document.getElementsByTagName("div");
	for(i=0; i<divArray.length; i++) {
		if (divArray[i].getAttribute("id").indexOf("Nav_") == 0) {
			divArray[i].style.display = "none";
		}
	}
}


// --- LAUNCH ACTIVEDIT SPELL CHECKER ON A FORM SUBMIT --- //
function aeapi_local_onLoad(aeObject, fieldname) {
	checkSpelling = function(formName) {
		ActivSpell.init();
		
		ActivSpell.formName = formName;

	    for (i=1; i<aeObjects.length; i++) {
	      	ActivSpell.fieldRefs[i] = "aeObjects[" + i + "].DOM.body.innerHTML";
		}

		// Override nextField since registering onpropertychange fires itself.
		nextField = function() {
			ActivSpell.argsIndex++;
			if(ActivSpell.argsIndex < ActivSpell.fieldRefs.length) {
				ActivSpellWin = window.open(inc + "spellchecker/window.cfm?jsvar=" + ActivSpell.fieldRefs[ActivSpell.argsIndex], "ActivSpellWin", "height=230,width=450,status=no,toolbar=no,menubar=no,location=no");
			}
			else {
				spellCheckComplete();
			}
		}
		
		//index ActivSpell.argsIndex
		ActivSpell.argsIndex++;
		
		if ((ActivSpell.fieldRefs[ActivSpell.argsIndex] == null) || eval(ActivSpell.fieldRefs[ActivSpell.argsIndex]).length == 0) {
			nextField();
		}
		
		//send the first field to spellcheck
		if(typeof(ActivSpellWin) == "undefined" || ActivSpellWin.closed) {
			ActivSpell.argsIndex = -1;
			ActivSpell.argsIndex++;
			
			if ((ActivSpell.fieldRefs[ActivSpell.argsIndex] == null) || eval(ActivSpell.fieldRefs[ActivSpell.argsIndex]).length == 0) {
				nextField();
			}
			
			ActivSpellWin = window.open(inc + "spellchecker/window.cfm?jsvar=" + ActivSpell.fieldRefs[0], "ActivSpellWin", "height=230,width=450,status=no,toolbar=no,menubar=no,location=no");	
			
		}
		ActivSpellWin.focus();	
		return false;
	}
	
	spellCheckComplete = function() {
		alert("Spell check complete!");
		ActivSpell.argsIndex = -1;
		ActivSpell.fieldRefs = null;
		nextField = function() {}
		ActivSpellWin.close();
		ae_onSubmit();
		var sSubmit = "document." + ActivSpell.formName + ".submit();";
		eval(sSubmit);
	}
}


// --- TEST FUNCTION --- //
function testIt() {
	alert("OK");
}


// --- REMOVE NON NUMERIC CHARACTERS FROM A STRING --- //
function fixNumber(theField,theValue) {
	newValue = "";
	theValue  =theValue.toString();
	for (i=0; i<theValue.length; i++) {
		n = theValue.substring(i,i+1);
		if (!isNaN(n) && n != " ") {
			newValue = newValue+n;
		}
	}
	theField.value = newValue;
}


// --- HIGHLIGHT SELECTED TEXT INPUT --- //
function highlightField(theField,isOn) {
	if (isOn) {
		theField.style.backgroundColor = "#AAB1FC";
	}
	else {
		theField.style.backgroundColor = "";
	}
}