onload=initJS;
var bLoaded = 0;
function initJS(){
	bLoaded = 1;
}
var menuTextArray = [];
var msgWindow = null;

var isNSv4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 4);
var menuTextArray = [];

var platform = navigator.platform;
var appName = navigator.appName;
var appVersion = parseInt(navigator.appVersion);

re = /win/gi;
isWin = (re.test(platform))?1:0;
re = /mac/gi;
isMac = (re.test(platform))?1:0;

re = /internet explorer/gi;
isIE = (re.test(appName))?1:0;
re = /netscape/gi;
isNS = (re.test(appName))?1:0;

function initMenuOptions(formName, selectMenuName, optionsValueArray, optionsTextArray, defaultOptionValue){
	
	//alert(formName + ' ' + selectMenuName + ' ' + optionsValueArray.length);

	var valueArray = new Array();
	var textArray = new Array();

	var valueArrayString = '';
	var textArrayString = '';

	if(optionsValueArray){

		valueArray = optionsValueArray;
		
		if(!optionsTextArray){

			textArray = optionsValueArray;
		}
		else{

			textArray = optionsTextArray;
		}
	}

	eval('var thisSelection = document.' + formName + '.' + selectMenuName + ';');
	initOptions(thisSelection, valueArray, textArray, defaultOptionValue);
}


function emptySelectOptions(thisSelectionArray){

	for(var i=0; i < thisSelectionArray.length; i++){

		var thisSelection = thisSelectionArray[i];

		while((index=thisSelection.options.length-1) && !(index < 0)){

			thisSelection[index] = null;
		}
		
		eval('thisText = menuTextArray\[\"' + thisSelection.name + '_Off\"\];');

		thisSelection[0] = new Option(thisText, "", false, false);
		thisSelection[0].defaultSelected = false;
	}
}

function initOptions(thisSelection, thisValueArray, thisTextArray, defaultOption){

	// last modified 
	// 	06-05-2000: allow for no -- title -- in menu if selection option values available 
	// menuTextArray for 'ON' is set to null options
	//	Syntax for creating new options:
	//		defaultSelected and selected should be set to true or false
	//		optionName = new Option([optionText, optionValue, defaultSelected, selected]);
	//		selectName.options[index] = optionName;
	
	var thisText = new String ("");
	var selectObjectsArray = new Array(

			thisSelection
	);
	
	// set the first selection text (value is empty)
	emptySelectOptions(selectObjectsArray);

	if(thisValueArray.length){

		thisText = eval('menuTextArray\["'+thisSelection.name+'_On"\]');
	}
	else{

		thisText = eval('menuTextArray\["'+thisSelection.name+'_Off"\]');
	}
	var bMenuHeader = 0;
	if(thisText){thisSelection[0].text = thisText;bMenuHeader = 1;}
	
	var thisSelectedIndex = -1;

	if(thisValueArray && thisTextArray){

		var optionIndex = null;

		for (var index=0; index < thisTextArray.length; index++){
			
			// if menu header, the first option (index 0) will be the header
			// text, else the first option will be thisValueArray[0]

			if(bMenuHeader){optionIndex = index + 1;}else{optionIndex = index;}

			var thisValue =  new String(thisValueArray[index]);
			var thisText = new String(thisTextArray[index]);
			
			if(thisValue == defaultOption){

				thisSelectedIndex = optionIndex;
			}
			thisSelection[optionIndex] = new Option(thisText, thisValue, false, false);
			// modified 02-15-2001: have to set to false here for NS cause the above
			// didn't work (it should have)
			thisSelection[optionIndex].selected = false;
		}
	}

	// modified 02-11-2001: only set menu if passed a default option
	// necessary for those menu's without an initial empty option
	// if creates problem with old code format (not using getFieldValue)
	// then set default if index 0 option is empty
	// if(thisValueArray[0] == '' || defaultOption)
	if(thisSelectedIndex >= 0){
		thisSelection.selectedIndex = thisSelectedIndex;
		thisSelection[thisSelectedIndex].defaultSelected = true;
	}
}

function isset(thisValue,bTest){

	if(thisValue != null){

		var thisType = typeof thisValue;

		if(thisType == "boolean" && thisValue){
			// regardless of true or false
		} else if(thisType == "number"){
			// have to convert to string or thinks it's null when 0
			thisValue = thisValue.toString();
		} else if(thisType == "string"){
			thisValue = thisValue.replace(" ","");
		} else if(thisType == "object" && thisValue.length){ // should be an array
			thisValue = thisValue.join("");
		} else { thisValue = null;}
	}
	if(bTest){alert(thisValue+ ' '+thisType);}

	// shouldn't get here unless string or number
	return (thisValue == null || thisValue == "" ? 0 : 1);
}

function setFieldValue(formObj, elementName, elementValue, arrayDelimeter, bTesting){	

	var alertMsg = [];
	// convert to object if user has sent form name as opposed to form object
	var thisType = typeof formObj;
	if(thisType != "object"){formObj = eval('document.'+formObj);}
	if(!formObj || !elementName){return;}

	var thisType = typeof elementName;
	if(thisType == "object" && elementName.length){ 
		// should be an array
		var arElementName = elementName;
	} else {
		// force to an array
		var arElementName = [elementName];
	}

	var thisType = typeof elementValue;
	if(thisType == "object" && elementValue.length){ 
		// should be an array
		var arElementValue = elementValue;
	} else {
		// force to an array
		// if only one value passed to set the field name(s), the 
		// elementValue will be same for all field names and is coded below
		var arElementValue = [elementValue];
	}

	if(!arrayDelimeter){var arrayDelimeter = '::';}

	// IMPORTANT: DO NOT PASS FIELD TYPES OTHER THAN INPUT TYPES
	// THAT CAN GET AN OBJECT TYPE RETURNED!! 
	// Also, if you try to set a form or variable that is non-existant, 
	// it will return without error message
	// for example: dont' pass action or target or image source

	// FOR SELECT MENU ONLY - field value may be an array or a string
	// if a string is passed it is split using '::'
	// this will cause a single string to be converted
	// to an array regardless if it contains the split value
	// an array should only be passed for menus with MULTIPLE setting

	//alert('arElementName: '+arElementName+'\n'+'arElementValue: '+arElementValue);
	for(var y=0; y < arElementName.length; y++){
		
		var elementName = arElementName[y];
		var elementValue = arElementValue[y];
var char = elementName.charAt(0);
		re = /[0-9]/;
		var bSpecialCase = (re.test(char))?1:0;
		alertMsg.push('bSpecialCase '+bSpecialCase);
		// SPECIAL CASE: for field names that start with a number
		if(bSpecialCase){
			alertMsg.push('elements.length '+formObj.elements.length);
			for(var e=0; e < formObj.elements.length; e++){
				if(formObj.elements[e].name == elementName){
					alertMsg.push('e '+e);
					var elObj = formObj.elements[e];
					break;
				}
			}
		} else { // normal field name
			var elObj = formObj[elementName];
		}
		if(elObj){
			// get element type
			var elObjType = elObj.type;
			// if no type returned then check to see if it's a radio button
			if(!elObjType){elObj = elObj[0];if(elObj){elObjType = elObj.type;}}

			if(elObj && elObjType){

				var elObjName = elObj.name;
				var elObjValue = elObj.value;
				
				switch(elObjType){

					case "radio":
						var radios = formObj[elementName].length;
						for(var x=0; x < radios; x++){
							// get selected radio value
							if(formObj[elementName][x].value ==  elementValue){formObj[elementName][x].checked = true;}
							else{formObj[elementName][x].checked = false;}
						}
						break;
					
					case "checkbox":
						if(elObjValue ==  elementValue){elObj.checked = true;}else{elObj.checked = false;}
						break;							
					
					case "select-one": // don't break; use same code for select-multiple
					case "select-multiple":

						// see above for documentation
						var arTemp = [];

						var thisValueType = typeof elementValue;
						if(thisValueType.toLowerCase() == 'string'){
							arTemp = elementValue.split(arrayDelimeter);
						}
						else{arTemp = elementValue;} // should already be an array
						
						formObj[elementName].options.selectedIndex = -1;
						for(var x=0;x < formObj[elementName].options.length;x++){
							formObj[elementName].options[x].selected = false;
							for(var so=0;so < arTemp.length; so++){
								elementValue = arTemp[so];
								if(formObj[elementName].options[x].value == elementValue){
									formObj[elementName].options[x].selected = true;
								}
							}
						}
						break;							
					
					case "password":
					case "hidden":
					case "file":
					case "textarea":
					case "text":
						elObj.value = elementValue;
						break;							

					default:
						break;							
				}
			}
		}
	}
	if(bTesting){alert(alertMsg.join('\n')+'\n\n');}
}

function getFieldValue(formObj, elementName, arrayDelimeter,bTesting){	

	var alertMsg = [];
	// convert to object if user has sent form name as opposed to form object
	var thisType = typeof formObj;
	if(thisType != "object"){formObj = eval('document.'+formObj);}
	if(!formObj || !elementName){return;}

	var thisType = typeof elementName;
	if(thisType == "object" && elementName.length){ 
		// should be an array
		var arElementName = elementName;
		var bReturnArray = 1;
	} else {
		// force to an array
		var bReturnArray = 0;
		var arElementName = [elementName];
	}

	var arElementValue = [];

	if(!arrayDelimeter){var arrayDelimeter = '::';}

	// IMPORTANT: DO NOT PASS FIELD TYPES OTHER THAN INPUT TYPES
	// THAT CAN GET AN OBJECT TYPE RETURNED!! 
	// Also, if you try to set a form or variable that is non-existant, 
	// it will return without error message
	// for example: dont' pass action or target or image source

	// FOR SELECT MENU ONLY - field value may be an array or a string
	// if a string is passed it is split using '::'
	// this will cause a single string to be converted
	// to an array regardless if it contains the split value
	// an array should only be passed for menus with MULTIPLE setting

	//alert('arElementName: '+arElementName+'\n'+'arElementValue: '+arElementValue);
	for(var y=0; y < arElementName.length; y++){
		
		var elementName = arElementName[y];
		var char = elementName.charAt(0);
		re = /[0-9]/;
		var bSpecialCase = (re.test(char))?1:0;
		// SPECIAL CASE: for field names that start with a number
		alertMsg.push('bSpecialCase '+bSpecialCase);
		if(bSpecialCase){
			alertMsg.push('elements.length '+formObj.elements.length);
			for(var e=0; e < formObj.elements.length; e++){
				if(formObj.elements[e].name == elementName){
					alertMsg.push('e '+e);
					var elObj = formObj.elements[e];
					break;
				}
			}
		} else { // normal field name
			var elObj = formObj[elementName];
		}
		
		if(elObj){
				// get element type
				var elObjType = elObj.type;
				// if no type returned then check to see if it's a radio button
				if(!elObjType){elObj = elObj[0];if(elObj){elObjType = elObj.type;}}
			
			if(elObj && elObjType){

				var elObjName = elObj.name;
				var elObjValue = elObj.value;
				alertMsg.push('elObj.name '+elObj.name);
				alertMsg.push('elObj.type '+elObj.type);
				
				switch(elObjType){

					case "radio":
						var radios = formObj[elementName].length;
						var selectedRadioIndex = null;
						for(var x=0; x < radios; x++){
							// get selected radio value
							if(formObj[elementName][x].checked){arElementValue[y] = formObj[elementName][x].value;selectedRadioIndex=x;}
						}
						break;
					
					case "checkbox":
						arElementValue[y] = formObj[elementName].checked?elObjValue:'';
						break;							
					
					case "select-one": // don't break; use same code for select-multiple
					case "select-multiple":
						
						if(elObjType == "select-multiple"){arElementValue[y] = [];}
						for(var i=0,x=0; i < formObj[elementName].options.length; i++){
							if(formObj[elementName].options[i].selected == true){
								var tempValue = formObj[elementName].options[i].value;
								if(elObjType == "select-multiple"){arElementValue[y][x++] = tempValue;}
								else{arElementValue[y] = tempValue;}
							}
						}
						break;							
					
					case "password":
					case "hidden":
					case "file":
					case "textarea":
					case "text":
						arElementValue[y] = elObjValue;
						break;							

					default:
						break;							
				}
			}
		}
	}
	if(bTesting){alert(alertMsg.join('\n')+'\n\n');}
	
	return bReturnArray ? arElementValue : arElementValue.join(arrayDelimeter);
}


function getObject(formName, fieldName){	

	// IMPORTANT: DO NOT PASS FIELD TYPES OTHER THAN INPUT TYPES
	// THAT CAN GET AN OBJECT TYPE RETURNED!!
	// for example: dont' pass action or target or image source

	var thisObjType = null;
	var fieldValue = '';

	// FOR SELECT MENU ONLY - field value may return an array or a string
	// an array will be returned for menus with MULTIPLE setting

	// check to see if there are special characters in the field name
	// and if so set field by using form element array; normal characters
	// are [a-zA-Z0-9_] a field name with any char other than this set will
	// be considered a special case

	re = /\W+/gi;
	bSpecialCase = (re.test(fieldName))?1:0;
	
	if(bSpecialCase){
		
		eval('tempFormObj = document.'+formName);

		for(var i=0; i < tempFormObj.elements.length; i++){

			tempObj = tempFormObj.elements[i];
			tempObjType = tempFormObj.elements[i].type;
			tempObjName = tempObj.name;
			tempObjValue = tempObj.value;
		}
	}
	else{ 
		
		eval('tempObj = document.'+formName+'.'+fieldName);

		if(!tempObj){ return fieldValue;}

		tempObjType = tempObj.type;

		// if no type returned then check to see if it's a radio button
		if(!tempObjType){tempObjType = tempObj[0].type;}
	}

	return tempObj;
}

function getRadioInfo(formName, fieldName){

	var tempObj = null;
	var fieldValue = '';
	var radioCount = 0;
	var radioIndex = -1;

	tempObj = getObject(formName, fieldName);
	if(!tempObj){ return fieldValue;}

	tempObjType = tempObj.type;

	// if no type returned then check to see if it's a radio button
	if(!tempObjType){tempObjType = tempObj[0].type;}

	if(tempObjType == "radio"){
	
		radioCount = tempObj.length;
		for(var x=0; x < radioCount; x++){
			// get selected radio value
			if(tempObj[x].checked == true){fieldValue = tempObj[x].value;radioIndex=x;}
		}
	}

	return new Array(radioIndex, fieldValue, radioCount);
}

function openWindow(url,windowName,width,height,directories,location,menubar,scrollbars,status,toolbar,resizable,top,screenY,left,screenX) {

	var features =
		 'width='        + width +
		 ',height='      + height +
		 ',directories=' + directories +
		 ',location='    + location +
		 ',menubar='     + menubar +
		 ',scrollbars='  + scrollbars +
		 ',status='      + status +
		 ',toolbar='     + toolbar +
		 ',resizable='   + resizable +
		 ',top='	 + top +
		 ',left='	 + left;


		if(screenY && screenX){
		 features +=
		 ',screenY='	 + screenY +
		 ',screenX='	 + screenX;
		}

	var windowString = (windowName?windowName + '=':'') + ' window.open(url, windowName, features);'
	eval(windowString);
	if(windowName){
		eval('if\(' + windowName + '.opener == null\)\{' + windowName + '.opener = self;\}');
	}
}

function displayMsgInWindow(windowName, thisMsg, width, height, bFocus){

	if(parent.frames.length){bFrames = 1;}else{bFrames = 0;} // using frames
	var bOpenNewWindow = 0;
	
	// check to see if window is closed or null
	eval('if \(!' + windowName + ' || ' + windowName + '.closed\)\{bOpenNewWindow=1;}');

	if(bOpenNewWindow){

		// default values
		var x = 0;
		var y = 0;

		// display msg window in center of screen
		x = (640 - width)/2;
		y = (480 - height)/2;

		if (screen) {
			y = (screen.availHeight - height)/2;
			x = (screen.availWidth - width)/2;
		}
		openWindow('about:blank','msgWindow',width,height,0,0,0,1,0,0,1,y,x,y,x);
	}

	if(!bFrames){
		
		if(width && height){msgWindow.resizeTo(width, height);}
		if(bFocus==0){msgWindow.blur();}
		else if(bFocus==1){msgWindow.focus();}
	}

	msgWindow.document.open('text/html');
	msgWindow.document.write('<form><TABLE align=center border=0 cellpadding=3>');
	msgWindow.document.write('<tr><td align=left><font face="Verdana, Arial, Helvetica">' + thisMsg + '</font></td></tr>');
	if(!bFrames){msgWindow.document.write('<tr><td align=center><font face="Verdana, Arial, Helvetica"><input type=button name=close_button value=OK onclick=window.blur();></font></td></tr>');}
	msgWindow.document.write('</table></form>');
	msgWindow.document.close();
	msgWindow.document.bgColor=document.bgColor;
	msgWindow.document.fgColor=document.fgColor;
}

// create an array of dynamic menu field objects in case need
// to use in conditional loop; array is populated whenever
// a new DynamicMenuField object is created
DynamicMenuFieldArray = new Array();
function DynamicMenuField(){

	// fieldName: 
	//		this select menu field name
	//
	// formName: 
	//		this select menu form name
	//
	// optionTextArray: 
	//		array used for filling menu options text;
	//		if this array is empty and optionValueArray filled, 
	//		options text will equal value
	//
	// optionValueArray: 
	//		array used for filling menu options text;
	//		if this array is empty and optionTextArray filled, 
	//		options value will equal text
	//
	// selectedOptionValue: 
	//		the initial value to be selected (if any)
	//
	// selectMenuSize: 
	//		number of options visible at once in menu window (i.e. height)
	//
	// bAllowMultipleSelections: 
	//		boolean to allow multiple selections? 1=yes, 0=no
	//
	// bInitOptionFilled: 
	//		boolean to set option indexed 0 to have a value or not;
	//		1=value, 0=no value (empty field value)
	//
	// onchange: 
	//		string for menu's 'onchange' event; set to null or '' for no action
	//
	// childObjectName: 
	//		the field name of the menu that dependent on this selected menu value;
	//		used for my search query processing code; leave null
	//
	// tableName: 
	//		this field's table name (where it's option values
	//		are retrieved from); used for my search query processing 
	//		code; leave null
	//
	// fieldType: 
	//		this field's type ('string' or 'int'); used for 
	//		my search query processing code; leave null
	//
	// NOTES:	
	//		
	//		I had to precede the new object's name with 
	//		'DynamicMenuObject_' due to errors caused by
	//		certain (I assume reserved) field names such 
	//		as 'status' or 'date'. It is REQUIRED that the name 
	//		following the underscore is actually a field name
	//		you want to create the menu for.
	//
	//

	this.fieldName;
	this.formName;
	optionTextArray = new Array();
	this.optionTextArray;
	optionValueArray = new Array();
	this.optionValueArray;
	this.selectedOptionValue;
	this.selectMenuSize;
	this.bAllowMultipleSelections;
	this.bInitOptionFilled;
	this.onchange;
	this.childObjectName;
	this.tableName;
	this.fieldType;
	DynamicMenuFieldArray[DynamicMenuFieldArray.length] = this.fieldName;
}


function createDynamicMenu(thisObj,formName,fieldName,optionTextArray,optionValueArray,selectedOptionValue,text_on,text_off,selectMenuSize,selectMenuSize,bAllowMultipleSelections,bInitOptionFilled,onchange){

	var tempString = '';

	formName = isset(formName) ? formName : (isset(thisObj) ? eval(thisObj).formName : "");
	fieldName = isset(fieldName) ? fieldName : (isset(thisObj) ? eval(thisObj).fieldName : "");
	optionTextArray = isset(optionTextArray) ? optionTextArray : (isset(thisObj) ? eval(thisObj).optionTextArray : []);
	optionValueArray = isset(optionValueArray) ? optionValueArray : (isset(thisObj) ? eval(thisObj).optionValueArray : []);
	selectedOptionValue = selectedOptionValue != null ? selectedOptionValue : (isset(thisObj) ? eval(thisObj).selectedOptionValue : "");
	text_on = isset(text_on) ? text_on : (isset(thisObj) ? eval(thisObj).text_on : "");
	text_off = isset(text_off) ? text_off : (isset(thisObj) ? eval(thisObj).text_off : "");
	menuTextArray[fieldName+"_on"] = text_on;
	menuTextArray[fieldName+"_off"] = text_off;

	selectMenuSize = isset(selectMenuSize) ? selectMenuSize : (isset(thisObj) ? eval(thisObj).selectMenuSize : "");
	bAllowMultipleSelections = isset(bAllowMultipleSelections) ? bAllowMultipleSelections : (isset(thisObj) ? eval(thisObj).bAllowMultipleSelections : 0);
	bInitOptionFilled = isset(bInitOptionFilled) ? bInitOptionFilled : (isset(thisObj) ? eval(thisObj).bInitOptionFilled : 0);
	onchange = isset(onchange) ? onchange : (isset(thisObj) ? eval(thisObj).onchange : "");

	// if option values and text are the same just pass same array for both
	// option text and values

	if(!optionTextArray.length && optionValueArray.length){
		optionTextArray = optionValueArray;
	}
	else if(!optionValueArray.length && optionTextArray.length){
		optionValueArray = optionTextArray;
	}
	// else both already set OR will only create menu if the text_off value(?)
	
	var bMulitpleSelected = 0;

	var selectedArray = isset(selectedOptionValue) ? selectedOptionValue.split("::") : [];
	
	if(selectedArray.length > 1){

		// muliple selections have been stored in DB so need to set 
		// menu option as selected if found in $line for each option created 
		bMulitpleSelected = 1;

		// must allow for multiple selections since can store mulitple
		bAllowMultipleSelections = 1;
	}

	if(bAllowMultipleSelections || bMulitpleSelected){
		fieldName += '[]'; 
		if(isset(thisObj)){eval(thisObj).fieldName = fieldName;}
	}

	// begin define for single select menu
	tempString += '<select name="'+fieldName+'" ';
	tempString += ' onchange="'+onchange+'" ';
	tempString += ' size='+selectMenuSize;
	tempString += (bAllowMultipleSelections ? ' MULITPLE ' : '');
	tempString += '>';

	if(!bInitOptionFilled){

		tempString += '<option value="">';
		tempString += (optionValueArray.length ? text_on : text_off);
		if(bAllowMultipleSelections){
			tempString += ' (use ctrl key for multiple)';
		}
		tempString += '</option>';
	}

	for(var index=0; index < optionValueArray.length; index++){

		tempString += '<option value="'+optionValueArray[index]+'">'+optionTextArray[index]+'</option>';
	}
	tempString += '</select>';
	document.write(tempString);

	setFieldValue(formName,fieldName,selectedOptionValue);
}

Array.prototype.push = function(thisValue){
	this[this.length] = thisValue;
}

function extractMultiDemArray(thisMultiArray,thisIndex){
	var tempArray = [];

	if(isset(thisMultiArray)){
		for(var i=0; i < thisMultiArray.length; i++){
			tempArray[i] = thisMultiArray[i][thisIndex];
		}
	}
	return tempArray;
}

function createCaption(origString,bForceLowerCase){

	var caption = [];
	if(isset(origString)){
		re = /_/g;
		var temp = origString.toString().replace(re," ");
		var words = temp.split(" ");
		for(var x=0; x < words.length; x++){
			var temp = words[x].substr(1,words[x].length-1);
			if(bForceLowerCase){temp.toLowerCase();}
			caption[x] = words[x].charAt(0).toUpperCase() + temp;
		}
	}
	return caption.join(" ");
}

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};


