var DefaultButton;
function CtlSetFirstFieldFocus(ignoreId,isanarray) {
	ignoreId = ((typeof(ignoreId) == 'undefined') ? '' : ignoreId);
	var frm, ctl;
	var numForms = document.forms.length;
	if (isanarray) {
		var FormFieldExclusions = new Array();
		var x,y;
		d = ",";
		x = ignoreId.split(d);
		for(y=1; y <= x.length; y++){
			FormFieldExclusions[y - 1] = x[y - 1];
		}
	}
	if (numForms) {
		for(i = 0; i < numForms; i++) {
			frm = document.forms[i];
			var numElements = frm.elements.length;
			for (j = 0; j < numElements; j++) {
				ctl = frm.elements[j];
				if (ctl.type == 'text' || ctl.type == 'select-one' || ctl.type == 'radio' || ctl.type == 'textarea'){
					try {
						// We've set readonly to 'readonly' in the HTML. IE interprets this as true (boolean);
						// Firefox interprets it as 'readonly' (string). So, we have to check both cases.
					var setFocusOnThisElement = true;
					if (isanarray) {
						for(ai = 0; ai < FormFieldExclusions.length; ai++){
							if (ctl.id == FormFieldExclusions[ai]) {
								var setFocusOnThisElement = false;
							}
						}
					} else {
						if (ctl.id == ignoreId) {
							var setFocusOnThisElement = false;
						}
					}
					if (setFocusOnThisElement){
						if (ctl.getAttribute('readonly') != 'readonly' && !ctl.getAttribute('readonly')){
							CtlSetFocus(ctl);
							return true;
						}
					}
					} catch (e) {
						// just move along
					}
				}
			}
		}
	}
}
// This routine sets the focus on a control in a manner that is usable in onblur() events in Firefox
// (which doesn't handle onblur() and onfocus() very well).
function CtlSetFocus(ctl){
	try{
		var ctlTest = document.getElementById(ctl.id);
		if(ctl.id > ''){
			setTimeout('try {document.getElementById("' + ctl.id + '").focus();} catch (e) {} FinishValidation();', 1);
			if (ctl.type == 'text'){
				setTimeout('try {document.getElementById("' + ctl.id + '").select()} catch (e) {} FinishValidation();', 1);
			}
		}
	}catch(e){
		//skip this control if it has no id
	}
}
function CtlSetSBValue(ctl, testValue){
	var i;
	for (i = 0; i < ctl.options.length; i++) {
		if (ctl.options[i].value == testValue) {
			ctl.selectedIndex = i;
		}
	}
}
function CanMoveItem(FromBox){
	return ((FromBox.selectedIndex >= 0) && !FromBox.options[FromBox.selectedIndex].disabled);
}
function CtlMoveSBItemToOtherSB(FromBox, ToBox, CustomSort) {
	if (CanMoveItem(FromBox)){
		CustomSort = (CustomSort == null ? false : CustomSort);
		var FromBoxOptionKeys = new Array();
		var FromBoxDisabledOptions = new Array();
		var ToBoxOptionKeys = new Array();
		var ToBoxDisabledOptions = new Array();
		var arrLookup = new Array();
		var i, opt, sortKey;
		// extract options and place into arrays for reordering
		for (i = 0; i < ToBox.options.length; i++) {
			opt = ToBox.options[i];
			if (opt.disabled){
				ToBoxDisabledOptions[ToBoxDisabledOptions.length] = opt;
			} else {
				sortKey = CustomSort ? buildSortKey(opt) : opt.text.toLowerCase();
				ToBoxOptionKeys[ToBoxOptionKeys.length] = sortKey;
				arrLookup[sortKey] = opt;
			}
		}
		for(i = 0; i < FromBox.options.length; i++) {
			opt = FromBox.options[i];
			if(opt.disabled){
				FromBoxDisabledOptions[FromBoxDisabledOptions.length] = opt;
			} else {
				sortKey = CustomSort ? buildSortKey(opt) : opt.text.toLowerCase();
				if (opt.selected && opt.value != "") {
					opt.selected = false;
					ToBoxOptionKeys[ToBoxOptionKeys.length] = sortKey;
				} else {
					FromBoxOptionKeys[FromBoxOptionKeys.length] = sortKey;
			 	}
				arrLookup[sortKey] = opt;
			}
		}
		// sort arrays and reset select boxes
		FromBoxOptionKeys.sort();
		ToBoxOptionKeys.sort();
		FromBox.length = 0;
		ToBox.length = 0;
		// repopulate select boxes, adding disabled items first
		for(i = 0; i < FromBoxDisabledOptions.length; i++) {
			FromBox[FromBox.options.length] = FromBoxDisabledOptions[i];
		}
		for(i = 0; i < FromBoxOptionKeys.length; i++) {
			FromBox[FromBox.options.length] = arrLookup[FromBoxOptionKeys[i]];
		}
		for(i = 0; i < ToBoxDisabledOptions.length; i++) {
			ToBox[ToBox.options.length] = ToBoxDisabledOptions[i];
		}
		for(i = 0; i < ToBoxOptionKeys.length; i++) {
			ToBox[ToBox.options.length] = arrLookup[ToBoxOptionKeys[i]];
		}
		try{
			SetDisabledOptionsToGray();
		} catch(e) {
			//nothing to see here
		}
	}
}
function CtlMoveSBItemsToFormField(FormName, ListBox, Field, Delimiter){
	Delimiter = ((typeof(Delimiter) == 'undefined') ? '|' : Delimiter);
	var ListField = document.getElementById(ListBox);
	var FormField = document.getElementById(Field);
	var loopLength = ListField.options.length;
	if (loopLength > 0) {
		TempString = '';
		var opt;
		for (var i = 0; i < loopLength; i++) {
			opt = ListField.options[i];
			if(!opt.disabled){
				if(isNaN(opt.value)){
					if (opt.value > ' ') {
						TempString += ListField.options[i].value + Delimiter;
					}			
				} else {
					if (ListField.options[i].value > 0) {
						TempString += ListField.options[i].value + Delimiter;
					}
				}
			}
		}
		FormField.value = TempString.slice(0, TempString.length - 1);
	} else {
		FormField.value = "";
	}
}
function CtlReloadSBFromParentSBValueGTE(parentControl, childControlName, listControlName, strictMatch, preserveSelection){
	var parentValue = (parentControl.value == '', 0, parentControl.value);
	var ctlChild = document.getElementById(childControlName);
	var ctlList = document.getElementById(listControlName);
	var childValues = ctlList.value.split('|');
	var thisChild, childValue, childName;

	preserveSelection = ((typeof(preserveSelection) == 'undefined') ? true : preserveSelection);
	if(preserveSelection){
		var CurrentValue = ctlChild.value;
	}
	ctlChild.length = 0;
	ctlChild.options[0] = new Option('', '');
	strictMatch = ((typeof(strictMatch) == 'undefined') ? true : strictMatch);
	for (var i in childValues){
		thisChild = childValues[i].split('~');
		childValue = thisChild[0];
		childName = thisChild[1];
		if (childValue >= parentValue || (!strictMatch && (parentValue == ''))){
			ctlChild.options[ctlChild.options.length] = new Option(childName, childValue);
		}
	}
	if(preserveSelection){
		CtlSetSBValue(ctlChild, CurrentValue);
	}
}
function CtlReloadSBFromParentSBValue(parentControl, childControlName, listControlName, strictMatch, preserveSelection){
	var testValue = parentControl.value;
	var ctlChild = document.getElementById(childControlName);
	preserveSelection = ((typeof(preserveSelection) == 'undefined') ? true : preserveSelection);
	if(preserveSelection){
		var CurrentValue = ctlChild.value;
	}
	ctlChild.length = 0;
	ctlChild.options[0] = new Option('', '');
	if (testValue != '' || !strictMatch){
		strictMatch = ((typeof(strictMatch) == 'undefined') ? true : strictMatch);
		var ctlList = document.getElementById(listControlName);
		var childValues = ctlList.value.split('|');
		var thisChild, parentId, childId, childName;
		for (var i in childValues){
			thisChild = childValues[i].split('~');
			parentId = thisChild[0];
			if (parentId == testValue || (!strictMatch && (testValue == '' || parentId == ''))){
				childId = thisChild[1];
				childName = thisChild[2];
				ctlChild.options[ctlChild.options.length] = new Option(childName, childId);
			}
		}
		if(preserveSelection){
			CtlSetSBValue(ctlChild, CurrentValue);
		}
	}
}
// Helper function for CtlReloadSBFromParentSBValues (below)
// Does the actual work of determining whether an item is a match.
function IsMatch(arrLookupValues, arrDependencyValues, strictMatch){
	var match = true;
	var i;
	for(i = 0; i < arrDependencyValues.length; i++){
		match = match && ((arrLookupValues[i + 1] == arrDependencyValues[i]) 
							|| (arrDependencyValues[i] == "") 
							|| (!strictMatch && arrLookupValues[i + 1] == ""));
	}
	return match;
}
// Reloads the values of a select box based on the values of an arbitrary number of other select boxes.
function CtlReloadSBFromParentSBValues(childControlName, childValuesControlName, childDependencyListControlName, childValueLookupControlName, strictMatch, preserveSelection){
	var ctlChild = document.getElementById(childControlName);
	try{
		if (ctlChild.type.substr(0,6) != 'select'){
			return false;
		}
	} catch (e) {
		return false;
	}
	strictMatch = ((typeof(strictMatch) == 'undefined') ? true : strictMatch);
	preserveSelection = ((typeof(preserveSelection) == 'undefined') ? true : preserveSelection);
	var ctlChildValues = document.getElementById(childValuesControlName);
	var ctlDependencyList = document.getElementById(childDependencyListControlName);
	var ctlChildValueLookup = document.getElementById(childValueLookupControlName);
	var i;
	// Determine the values of the target select box's dependency controls.
	var arrDependencyValues = new Array();
	var arrDependencies = ctlDependencyList.value.split(',');
	for(i = 0; i < arrDependencies.length; i++){
		arrDependencyValues[arrDependencyValues.length] = document.getElementById(arrDependencies[i]).value;
	}
	// Find the matching values in the lookup list for the target select box.
	var arrMatches = new Array();
	var arrLookupItems = ctlChildValueLookup.value.split('|');
	for(i = 0; i < arrLookupItems.length; i++){
		arrLookupValues = arrLookupItems[i].split('~');
		if(IsMatch(arrLookupValues, arrDependencyValues, strictMatch)){
			// create keyed array to hold the matching values
			arrMatches['id' + arrLookupValues[0]] = arrLookupValues[0];
		}
	}
	// Reinitialize the target select box - preserve value if necessary
	if(preserveSelection){
		var CurrentValue = ctlChild.value;
	}
	ctlChild.length = 0;
	ctlChild.options[0] = new Option('', '');
	// Repopulate the target select box with the matches found above.
	var arrChildValues = ctlChildValues.value.split('|');
	for(i = 0; i < arrChildValues.length; i++){
		arrChildValue = arrChildValues[i].split('~');
		if(arrMatches['id' + arrChildValue[0]] == arrChildValue[0]){
			ctlChild.options[ctlChild.options.length] = new Option(arrChildValue[1], arrChildValue[0]);	
		}
	}
	if(preserveSelection){
		CtlSetSBValue(ctlChild, CurrentValue);
	}
}
//
//	Script used to rebuild dependent drop-downs.
//
function CtlCallDependentDropDownReload(FormField,LoadFunction) {
	var i;
	for(i = 0; i < FormField.options.length; i++) {
		if (FormField.options[i].selected && FormField.options[i].value != "") {
			eval(LoadFunction)(document.forms[0], FormField.options[i].value);
			break;
	   }
	}
}
function CtlReloadTBFromParentSBValue(parentControl, childControlName, listControlName, childControl2Name, strictMatch){
	var testValue = parentControl.value;
	var ctlChild = document.getElementById(childControlName);
	ctlChild.length = 0;
	ctlChild.value = '';
	if (testValue != '' || !strictMatch){
		strictMatch = ((typeof(strictMatch) == 'undefined') ? true : strictMatch);
		var ctlList = document.getElementById(listControlName);
		var childValues = ctlList.value.split('|');
		var thisChild, parentId, childName;
		for (var i in childValues){
			thisChild = childValues[i].split('~');
			parentId = thisChild[0];
			if (parentId == testValue || (!strictMatch && (testValue == '' || parentId == ''))){
				ctlChild.value = thisChild[1];
				if (typeof(childControl2Name) != 'undefined'){
					var ctlChild = document.getElementById(childControl2Name);
					ctlChild.value = '';
				}
			}
		}
	}
}
//
//	Script facilitates moving down a selection box using consecutive characters provided.
//
function CtlFillTextFieldFromHiddenSBSelection(field, ResultField, select, property, forcematch) {
	var found = false;
	for (var i = 0; i < select.options.length; i++) {
		if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
			found=true;
			break;
		}
	}
	if (found) { 
		select.selectedIndex = i;
		ResultField.value = select.options[i].value;
	} else {
		select.selectedIndex = i - 1;
		ResultField.value = select.options[i - 1].value;
	}
	if (field.createTextRange) {
		if (forcematch && !found) {
			field.value=field.value.substring(0,field.value.length-1); 
			return;
		}
		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) {
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
			}
		}
	}
}
function CtlFindFirstButtonOnPage(DefaultButtonId){
	DefaultButton = null;
	var frm, ctl;
	var i, j;
	if (typeof(DefaultButtonId) == 'undefined'){
		if (document.forms.length) {
			for (i=0; i < document.forms.length; i++){
				frm = document.forms[i];
				for (j = 0; j < frm.elements.length; j++) {
					try {
						ctl = frm.elements[j];
						if (ctl.type == 'button'){
							DefaultButton = ctl;
							return true;
						}
					} catch (e) {
						// just move along
					}
				}
			}
		}
	} else {
		DefaultButton = document.getElementById(DefaultButtonId);
	}
}
function CtlCaptureKeyPressed(evt){
	var e = evt ? evt : window.event;
	var key;
	if (e.keyCode) { 
		key = e.keyCode; 
	} else if (typeof(e.which)!= 'undefined') { 
		key = e.which; 
	}
	if (key == 13){
		if(DefaultButton != null){
			DefaultButton.click();
		}
	}
}
function ReturnNavigation(msg) {
	if (msg != '') {
		alert(msg);
	}
	if (history.length == 0) {
		window.close();
	} else {
		history.go(-1);
	}
}
function reLoadPage(pagename, token, field, value) {
	var re = new RegExp('&' + field + '=[A-Za-z0-9]+');
	var newtoken = token.replace(re, '');
	window.location = pagename + '?' + newtoken + '&' + field + '=' + value; 
}
function reDirectToNetPage(pagename, value, Parm) {
	window.location = pagename + value + Parm; 
}
function AddNew(w,x,y) {
	var Other = document.getElementById(x);
	var Next = document.getElementById(y);
	if (document.all){
		if (Other.style.display == '') {
			Other.style.display = 'none';
			document.getElementById(w).value = 'List'
			Next.style.display = '';
			document.getElementById(y).focus();
		} else {
			Other.style.display = '';
			Next.style.display = 'none';
			document.getElementById(w).value = 'New';
		}
	} else if (document.getElementById){
		if (Other.style.display == 'block') {
			Other.style.display = 'none';
			document.getElementById(w).value = 'List'
			Next.style.display = 'block';
			document.getElementById(y).focus();
		} else {
			Other.style.display = 'block';
			Next.style.display = 'none';
			document.getElementById(w).value = 'New';
		}
	}
}
function AddToSelectList(x,a) {	
	if (x != "") {
		var len = document.getElementById(a).length++;
		//insert select variables
		document.getElementById(a).options[len].value = "N_" + x;
		document.getElementById(a).options[len].text = x;
		document.getElementById(a).selectedIndex = len;
	}
} 