function comingSoon() {
	alert("Coming Soon!");
	return false;
}

// disables all the links in a page
function disableLinks() 
{   
	for (var i = 0; i < document.links.length; i++) {     
		window.document.links[i].onclick = function() { return false; }  
	}
} 

// clears all the options in the specified select box
function clearSelectBox(fieldRef) {
	count = fieldRef.options.length;
	for (var i = 0; i < count; i++) {
		fieldRef.options[0] = null;
	}
}

//loads a select box with the options array (see saveSelectOptions)
function loadSelectBox(fieldRef,options) {
	for (var i = 0; i < options.length; i++) {
		//alert(options[i]);
		fieldRef.options[i] = new Option(options[i][0],options[i][1],booleanToInt(options[i][2]),booleanToInt(options[i][3]));
	}
}

// returns the text of the selected option
function getSelectedText(selectRef) {
	if (selectRef.type == "hidden") {
		return selectRef.text;
	} else {
		return selectRef[selectRef.selectedIndex].text;
	}
}

// Returns an array of pairs containing the text and value of each option object in the 
// select box specified in fieldRef
function saveSelectOptions(fieldRef) {
	optionsArray = new Array();
	for (var i = 0; i < fieldRef.options.length; i++ ){
  			var o = fieldRef.options[i];
			//originally created an Option object for each item, but it causes unpredictable results in netscape 4 (browser crash).
  			optionsArray[i] = [o.text, o.value, o.defaultSelected, o.selected];  
	}
	return optionsArray;
}	

// converts true or false into 1 or 0.  Needed for netscape 4.*, because in some instances, it does not interpret true and false correctly.
function booleanToInt(bool) {
	if (bool) {
		return 1;
	} else {
		return 0;
	}
}

// sets the action of the form specified in formRef to 
function setFormAction(formRef,action) {
	formRef.action = action;
}

// shows or hides an element
function setVisibility(element,visibility) {
	if (visibility == "hidden") {
		if (document.getElementById) {
			document.getElementById(element).style.visibility = "hidden";
		} else if (document.layers) {
			document.layers[element].visibility = "hide";
		} else if (document.all) {
			document.all[element].style.visibility = "hidden";
		}
	} else if (visibility == "visible") {
		if (document.getElementById) {
			document.getElementById(element).style.visibility = "visible";
		} else if (document.layers) {
			document.layers[element].visibility = "show";
		} else if (document.all) {
			document.all[element].style.visibility = "visible";
		}
	}
}

// shows or hides an element based on the show variable
function showSpan(element,show) {
	if (show) {
		setVisibility(element,"visible");
	} else {
		setVisibility(element,"hidden");
	}
}

// gets a browser independent reference to an element
function getElementRef(element) {
	if (document.getElementById) {
		return document.getElementById(element);
	} else if (document.layers) {
		return document.layers[element];
	} else if (document.all) {
		return document.all[element];
	}
}

// gets a browser independent reference to a form residing in a layer  
// this is to compensate the fact that netscape does not see forms in layers as being part of the main document.
function getLayerForm(layer,formName) {
	if (document.getElementById) {
		return eval("document."+formName);
	} else if (document.layers) {
		return eval("document.layers[layer].document."+formName);
	} else if (document.all) {
		return eval("document."+formName);
	}
}

// adds an option to the select
function addOption(option,selectRef,pos) {
	oldOptions = saveSelectOptions(selectRef);
	clearSelectBox(selectRef);

	newOptions = new Array(oldOptions.length+1);
	offset = 0;
	for (i = 0; i < newOptions.length; i++) {
		if (pos == i) {
			newOptions[i] = [option.text,option.value,option.defaultSelected,option.selected];
			offset = 1;
		} else {
			newOptions[i] = oldOptions[i-offset];
		}
	}

	selectRef.length++;
	loadSelectBox(selectRef,newOptions);
	if (option.selected) {
		selectRef.options[pos].selected = true;
	}
}

// for debugging - alerts name and value of form elements
function getFormInfo(output) {
	var info = new String();
	for (i = 0; i < document.forms.length; i++) {
		for (j = 0; j < document.forms[i].elements.length; j++) {
			element = document.forms[i].elements[j];
			if (element.type == "text" || element.type == "hidden" || element.type == "textarea") {
				info += element.name+"="+element.value+"\n";
			} 
			if (element.type == "select-one") {
				info += element.name+"="+element[element.selectedIndex].value+"\n";
			} 
			if (element.type == "checkbox" || element.type == "radio") {
				info += element.name+"("+element.value+")=";
				if (element.checked) {
					info += "checked";
				} 
				info += "\n";
			}
		}
	}
	if (output == "print") {
		document.write(info);
	} else {
		alert(info);
	}
}

// for debugging - evaluates what's entered in the prompt
function testAlert() {
	x = prompt();
	eval(x); //
}

// for debugging - displays the contents of an array
function displayArray(array1)  {
	var elements = ""
	for (var i = 0; i < array1.length; i++) {
		elements += array1[i]+",";
	}
	alert(elements);
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

// sets the option with the value specified to selected
function setSelectOption(field,value) {
	for (var i = 0; i < field.options.length; i++) {
		if (field[i].value == value) {
			field[i].selected = true;
		}
	}
}

// checks if the array has duplicate elements then returns the first element that's duplicate
// returns -1 if nothing is duplicated
function getFirstDup(check) {
	for (var i = 0; i < check.length; i++) {
		if (i != i.length-1) {
			for (var j = i+1; j < check.length; j++) {
				if (check[i] == check[j]) {
					return check[i];
				}
			}
		}
	}
	return -1;
}

// sets the radio button with the value of value to checked
function setSelectedRadio(radioRef,value) {
	for (var i = 0; i < radioRef.length; i++) {
		if (radioRef[i].value == value) {
			radioRef[i].checked = true;
		}
	}
}

// if the checkbox passed in checked, it sets the span specified to visible, if not, to hidden
function setVisibilityByCheckbox(fieldRef,spanName) {
	if (fieldRef.checked) {
		document.getElementById(spanName).style.visibility = "visible";
	} else {
		document.getElementById(spanName).style.visibility = "hidden";
	}
}

// use in conjunction with the 'collapse' value for visiblilityType in the data:span tag library
// if show is true, replaces the contents of the span with the contents of a temporary span (name: spanName+Temp)
// if show is false, saves the contents to the temporary span and sets the span to blank
function collapseSpan(spanName,show) {
	var mainSpan = document.getElementById(spanName);
	var tempSpan = document.getElementById(spanName+"Temp");

	if (show) {
		if (mainSpan.innerHTML == "") {
			mainSpan.innerHTML = tempSpan.innerHTML;
			tempSpan.innerHTML = ""
		}
	} else {
		if (mainSpan.innerHTML != "") {
			tempSpan.innerHTML = mainSpan.innerHTML;
			mainSpan.innerHTML = "";
		}
	}
}

// switches the contents of the two specified spans
function switchSpan(span1Name,span2Name) {
	var span1 = document.getElementById(span1Name);
	var span2 = document.getElementById(span2Name);
	var temp;

	temp = span1.innerHTML;
	span1.innerHTML = span2.innerHTML;
	span2.innerHTML = temp;
}

function getArrayString(array) {
	var arrayString = "";
	for (var i = 0; i < array.length; i++) {
		arrayString += array[i];
		if (i < array.length-1) {
			arrayString += ",";
		}
	}
	return arrayString;
}

// returns the number of elements in a specific form
// if type is specified, it returns the number of that type of element
function getNumFormElements(formRef,type) {
	var count = 0;
	for (var i = 0; i < formRef.elements.length; i++) {
		if (type != "") {
			if (formRef.elements[i].type == type) {
				count++;
			}
		} else {
			count++;
		}
	}
	return count;
}

// returns an array of the form elements of the specified type.
function getFormElements(formRef,type) {
	var formElements = new Array();
	var count = 0;
	for (var i = 0; i < formRef.elements.length; i++) {
		if (formRef.elements[i].type == type) {
			
			formElements[count] = formRef.elements[i];
			count++;
		}
	}
	return formElements;
}

// returns the position in the array of the value specified.
// returns -1 if the value is not found.
function getPosition(array,value) {
	for (var i = 0; i < array.length; i++) {
		if (array[i] == value) {
			return i;
		}
	}
	return -1;
}

// used by the <data:form> tag library
// do not call directly
// this is the default - if you need to validate for additional information, redefine this function in the page.
// note: this overrides it completely, it does not extend it.
function isPageChanged() {
	return isFormChanged(formRef);
}

// used by the <data:form> tag library
// do not call directly
function setLinks() {
	for (var i = 0; i < document.links.length; i++) {
		linkHref = window.document.links[i].href;
		target = window.document.links[i].target;
		if (linkHref.indexOf("javascript") == -1 && target != "_blank") {
			linkHref = "javascript: leavePage('" + linkHref + "'	);";
			window.document.links[i].href = linkHref;
		}
	}
}

// used by the <data:form> tag library
// do not call directly
function leavePage(urlString) {
	if (isPageChanged()) {
		if (confirm("You have changes that have not been saved.\nWould you like to save your changes?")) {
			if (callValidate()) {
				formRef.redirect.value = urlString;
				formRef.submit();
			}
		} else {
		    window.location = urlString;
			//formRef.action = urlString;
			//formRef.submit();
		}
	} else {
		window.location = urlString;
			//formRef.action = urlString;
			//formRef.submit();
	}
}

// Replaces special characters form fields
function replaceSpecialChars(fieldRef) {
	var value = fieldRef.value;
	var newValue = new String();
	var charAt = new String();

	var apostrophe = new Array(/‘/g, /’/g, /“/g, /”/g, /`/g, /´/g, /‚/g, /„/g);
	var hyphen = new Array(/—/g, /–/g);

	newValue = value;

	for (var i = 0; i < apostrophe.length; i++) {
		newValue = newValue.replace(apostrophe[i], "'");
	}

	for (var i = 0; i < hyphen.length; i++) {
		newValue = newValue.replace(hyphen[i], "-");
	}
    newValue = newValue.replace(/™/g,"&#8482;");

	fieldRef.value = newValue;
}

// determines if the hashMap passed in has an entry with the key specified.
function hasKey(hashMap,key) {
	var undefined;
	return hashMap[key] != undefined;
}

// used by the <data:textarea> tag library to emulate the maxlength property
function checkMaxLength (textarea, evt, maxLength) {
  if (textarea.selected && evt.shiftKey) 
    // ignore shift click for select
    return true;
  var allowKey = false;
  if (textarea.selected && textarea.selectedLength > 0)
    allowKey = true;
  else {
    var keyCode = 
      document.layers ? evt.which : evt.keyCode;
    if (keyCode < 32 && keyCode != 13)
      allowKey = true;
    else           
      allowKey = textarea.value.length < maxLength;
  }
  textarea.selected = false;
  return allowKey;
}

// Trims white space from heginning and end of a string
function trim(value) {
    var i = 0;
    var pos = -1;
    while (i < value.length) {
        if (value.charCodeAt(i) != 32 && !isNaN(value.charCodeAt(i))) {
            pos = i;
            break;
        }
        i++;
    }

    var lastpos = -1;
    i = value.length;
    while (i >= 0) {
        if (value.charCodeAt(i) != 32 && !isNaN(value.charCodeAt(i))) {
            lastpos = i;
            break;
        }
        i--;
    }

    return value.substring(pos, lastpos + 1);
}

// gets an array based on the values in a group of hidden fields with the same name.
function getIdsFromHiddens(fieldRef) {
	var undefined;
	if (fieldRef.length == undefined) {
		var arr = new Array(1);
		arr[0] = fieldRef.value;
		return arr;
	} else {
		var hiddenIds = new Array(fieldRef.length);
		for (i = 0; i < fieldRef.length; i++) {
			hiddenIds[i] = fieldRef[i].value;
		}
		return hiddenIds;
	}
}

// returns the nameId from a field 
// i.e. if fieldRef is named 
function getNameId(fieldRef) {
	return (fieldRef.name.split("_"))[1];
}

// returns true if element is in array1
function containsElement(array1,element) {
	for (var i = 0; i < array1.length; i++) {
		if (array1[i] == element) {
			return true;
		}
	}
	return false;
}

// returns true if array1 contains any of the elements from elements
function containsElements(array1,elements) {
	for (var i = 0; i < array1.length; i++) {
		for (var j = 0; j < elements.length; j++) {
			if (array1[i] == elements[j]) {
				return true;
			}
		}
	}
	return false;
}

// returns an array containing the elements in array1 that are not in array2
function getElementsNotInArray(array1,array2) {
	var elements = new Array();
	var count = 0;
	for (var i = 0; i < array1.length; i++) {
		if (!containsElement(array2,array1[i])) {
			elements[count] = array1[i];
			count++;
		}
	}
	return elements;
}

// returns an array of the keys in the associative array passed in
function getArrayKeys(assocArray) {
	var keys = new Array();
	var count = 0;
	for (var i in assocArray) {
		keys[count] = i;
		count++;
	}
	return keys;
}

// adds the contents of array2 to array1
function addArrays(array1,array2) {
	var count = array1.length-1;
	for (var i = 0; i < array2.length; i++) {
		array1[++count] = array2[i];
	}
	return array1;
}

// returns the intersection of the elements of the two arrays
function getIntersection(array1,array2) {
//alert(array1 + "-" + array2);
	var intersection = new Array();	var count = -1;
	for (var i = 0; i < array1.length; i++) {
		if (containsElement(array2,array1[i])) {
			intersection[++count] = array1[i];
		}
	}
	return intersection;
}

// removes the elements in array2 (if they exist) from array1
function removeElements(array1,array2) {
	var newArray = new Array();
	var count = -1;
	for (var i = 0; i < array1.length; i++) {
		if (!containsElement(array2,array1[i])) {
			newArray[++count] = array1[i];
		}
	}
	return newArray;
}

function getColumnStart(numRecs,numCols,col) {
    var baseRecsCol = removeDecimal(numRecs / numCols);
	var remainRecsCol = numRecs % numCols;

	//We have an uneven division, return the adjusted result
	if (remainRecsCol > 0) {
	    //Cols up to 1 after the remainder
	    if (col <= (remainRecsCol + 1)) {
	        return (baseRecsCol + 1) * (col - 1);
	    }
	    //Cols beyond 1 after the remainder
	    else {
	        var retRecs = remainRecsCol * (baseRecsCol + 1);
	        retRecs += ((col - 1) - remainRecsCol) * baseRecsCol;
	        return retRecs;
	    }
	}
	//even division
	return baseRecsCol * (col - 1);
}

function getRecordsInColumn(numRecs, numCols, col) {
    var baseRecsCol = removeDecimal(numRecs / numCols);
    var remainRecsCol = numRecs % numCols;
    //We have an uneven division, return the adjusted result
    if (remainRecsCol > 0) {
        if (col <= remainRecsCol) {
            return baseRecsCol + 1;
        }
    }
    //even division
    return baseRecsCol;
}
	
// removes the decimal from a number, if it exists
function removeDecimal(number) {
	var splitted = (""+number).split(".");
	return parseInt(splitted[0]);
}

// rounds the number up to the nearest whole number
function roundUp(number) {
	var splitted = (""+number).split(".");
	if (splitted.length > 1 && parseInt(splitted[1]) > 0) {
		return parseInt(splitted[0]) + 1;
	}
	return parseInt(splitted[0]);
}

function getNumRows(cols,numItems) {
	var numRows = parseInt(numItems)/parseInt(cols);
	return roundUp(numRows);
}

function getCurrentPosition(row,column,numRows,numCols,numItems) {
	row = parseInt(row);
	column = parseInt(column);
	numRows = parseInt(numRows);
	numCols = parseInt(numCols);
	numItems = parseInt(numItems);

	var itemsOnLastLine = numItems % numCols;
	if (itemsOnLastLine > 0 && column > itemsOnLastLine) {
		if (row == numRows) {
			return -1;
		} else if (column == itemsOnLastLine + 1) {
			return (numRows * (column - 1)) + row;
		} else {
			return ((numRows * (column - 1)) + row) - (column-(itemsOnLastLine+1));
		}		
	} else {
		return (numRows * (column - 1)) + row;
	}
}

function changeCategorySelectBoxes(currentSelectBox, showSelectText) {
    var fieldPrefix = (currentSelectBox.name.split("_"))[0];
    var selectedLineNum = parseInt(getNameId(currentSelectBox));
    var nextSelectBox = eval("formRef."+fieldPrefix+"_"+(selectedLineNum + 1));
    var selectedOptions = options[getSelectedValue(currentSelectBox)];

    if(showSelectText == true) {
        var selectText = "";
        for (var j in selectedOptions) {
            if(j < 0){
                selectText = selectedOptions[j];
            }
        }
        // no options were selected, so grab default
        if(selectText == "") {
            selectText = options[1][-100];
        }
    }

    var selectedKeys = getArrayKeys(selectedOptions);
    var nextSelectBoxJSObject = eval(nextSelectBox.name+"JS");

    if(getSelectedValue(nextSelectBox) <= 0 || (containsElement(selectedKeys,getSelectedValue(nextSelectBox)) == false)) {
        nextSelectBoxJSObject.setDisabled(false);
        clearSelectBox(nextSelectBox);
        for (var i = 0; i < selectedKeys.length; i++) {
            nextSelectBox.options[i] = new Option(selectedOptions[selectedKeys[i]],selectedKeys[i],false,false);
        }
    }

    var emptySelectBoxOptions = new Array();

    if(showSelectText == true) {
        emptySelectBoxOptions[0] = [selectText, -100, true, true];
    }
    else {
        // the -99 corresponds to the ALL_ID value in WoodObject on the Java side
        emptySelectBoxOptions[0] = ["All", -99, true, true];
    }

    var lineCount = selectedLineNum + 1;
    var remainingSelectBox = eval("formRef."+fieldPrefix+"_"+lineCount);
    var remainingSelectBoxJSObject;
    while (remainingSelectBox != null) {

         if (getSelectedValue(currentSelectBox) == -99 ||
                getSelectedValue(currentSelectBox) == -100 ||
                lineCount > selectedLineNum + 1) {
             clearSelectBox(remainingSelectBox);
             loadSelectBox(remainingSelectBox,emptySelectBoxOptions);
             remainingSelectBoxJSObject = eval(remainingSelectBox.name+"JS");
             remainingSelectBoxJSObject.setDisabled(true);
         }

         lineCount++;
         remainingSelectBox = eval("formRef."+fieldPrefix+"_"+lineCount);
    }
}

function restoreDisabledStatus(fieldNamePrefix,pageName, showSelectText) {

    var values = retrieveSelectBoxValuesCookie(pageName);
    var field;
    if (values != null) {
        for (var i = 0; i < values.length; i++) {
            field = eval("formRef."+fieldNamePrefix+"_"+i);
            eval(field.name+"JS.setDisabled(false);");
            setSelectOption(field,values[i]);
            if (!isLastSelectBox(field,fieldNamePrefix)) {
                changeCategorySelectBoxes(field, showSelectText);
            }
        }
    }
}

function isLastSelectBox(currentSelectBox, fieldNamePrefix) {
    var lineCount = 0;
    var testField = eval("formRef."+fieldNamePrefix+"_"+lineCount);
    var lastField;
    while (testField != null) {
        lastField = testField;
        lineCount++;
        testField = eval("formRef."+fieldNamePrefix+"_"+lineCount);
    }
    return (lastField == currentSelectBox);
}

function saveSelectBoxValuesCookie(fieldNamePrefix,pageName) {
    var lineCount = 0;
    var field = eval("formRef."+fieldNamePrefix+"_"+lineCount);
    //alert("field: "+field);
    var values = new Array();
    while (field != null && field.disabled == false) {
        values[lineCount] = getSelectedValue(field);
        lineCount++;
        field = eval("formRef."+fieldNamePrefix+"_"+lineCount);
    }
    document.cookie = "selectBoxValues"+pageName+"="+values;
}

function retrieveSelectBoxValuesCookie(pageName) {
    var values = getCookie("selectBoxValues"+pageName);
    document.cookie = "selectBoxValues"+pageName+"=null";
    if (values == null || values == -1) {
        return null;
    } else {
        return values.split(",");
    }
}

/*
    Disables all other fields in a form except for the one
    passed in if the selected field has data entered or selected
    and enables them if it doesn't.
    - currently only does text and select
    - requires that formFieldObjects be declared for all fields
*/
function disableAllButSelected(fieldRef) {
    var formRef = fieldRef.form;
    var undefined;

    var disabled;
    if (fieldRef.type == "text") {
        disabled = fieldRef.value.length > 0
    } else if (fieldRef.type == "select-one") {
        disabled = getSelectedValue(fieldRef) > 0
    }

    var element;
    for (var i = 0; i < formRef.elements.length; i++) {
         element =  formRef.elements.item(i);
         if (element.name != undefined && element.type != "hidden" && element.type != "submit" && element.name != fieldRef.name) {
            eval(element.name+"JS.setDisabled("+disabled+")");
        }
    }
}

function retainDisableAllButSelected(formRef) {
    var element;
    for (var i = 0; i < formRef.elements.length; i++) {
        element =  formRef.elements.item(i);
        if ((element.type == "text" && element.value.length > 0) || (element.type == "select-one" && getSelectedValue(element) > 0)) {
            disableAllButSelected(element);
        }
    }
}
