/*
'*************************************************************************************************************
' Copyright (C) 2001, The Hartford Steam Boiler Inspection and Insurance Company.  All Rights Reserved.
'*************************************************************************************************************
*/

// === GLOBAL DECLARATIONS ======================================
var appRoot="/framework/applications/Toga";
var appName="TOGA";
//constants for validation
//users
var LEN_FIRSTNAME = 15;
var LEN_LASTNAME =  20;
var LEN_EMAIL = 50;
var LEN_ADDRESSLINE1 = 60;
var LEN_ADDRESSLINE2 = 60;
var LEN_BUS_TITLE = 20;
var LEN_PASSWORD = 20;
var MIN_PASSWORD = 4;

//Units
var DEFAULT_UOM_WEIGHT = 3;
var DEFAULT_UOM_VOLUME = 1;

//locations
var LEN_LOCATION_NAME = 40;
var LEN_STREET = 240;
var LEN_CITY = 30;
var LEN_STATE = 2;
var LEN_ZIP = 10;
var LEN_HSB = 10;

//roles
var COMPANY = 1;
var CLIENT = 2;
var ACCOUNT_MANAGER = 16;
var INSURANCE_AGENT = 25;
var CLIENT_SHIPPING_CONTACT = 31;
var LAB_BUSINESS_CONTACT = 32;
var LAB_SHIPPING_CONTACT = 33;
var TOGA_ADMINISTRATOR = 34;
var TOGA_EXPERT = 35;
var LAB_ORGANIZATION = 37;
var SHIPPING_CARRIER_ORGANIZATION = 38;


//Customer Organization
var LEN_ORG_NAME = 120;
var LEN_MAIN_PHONE = 20;
var LEN_URL	= 50;
var LEN_SHIPPING_CARRIER_ORG_URL = 255;
var LEN_ACCOUNT_ID = 20;

var arrInvalidChars = new Array('~','&','<','>','"',"'", " ");
var arrMonths =["January","February","March","April","May","June","July","August","September","October","November","December"]

// variables for loading Locations by Account
var delim = "~";
//var delim1 = ":";
//var delim2 = "^";
var acctStr;
var arrAcct = new Array();
var arrLoc = new Array();
var arrNameID = new Array();
//================================================================================
if (NS==true ){
	document.write( "<link href='scripts/ns_style.css'  rel='Stylesheet'>");
}else if(IE==true){	     
	document.write("<link href='scripts/ie_style.css'  rel='Stylesheet'>");
}else if(NS6==true){	     
	document.write("<link href='scripts/ns6_style.css'  rel='Stylesheet'>");
}


//=================================================================
//workbench images
		var arrImgs = new Array();
		arrImgs[0] = new Image();
		arrImgs[0].src = "images/corner-ulh.gif";
		arrImgs[1] = new Image();
		arrImgs[1].src = "images/line-vert.gif";
		arrImgs[2] = new Image();
		arrImgs[2].src = "images/line-horiz.gif";
		arrImgs[3] = new Image();
		arrImgs[3].src = "images/menubartop.gif";
		arrImgs[4] = new Image();
		arrImgs[4].src = "images/hsbtoga.gif";
		arrImgs[5] = new Image();
		arrImgs[5].src = "images/menu-user.gif";
		arrImgs[6] = new Image();
		arrImgs[6].src = "images/spacer.gif";
		arrImgs[7] = new Image();
		arrImgs[7].src = "images/button-submit.gif";
//===Pre load other images============================
		var arrWKImgs = new Array()
		var imageOn = new Array();
		var imageOff = new Array();
		var imageOvr = new Array();
		arrWKImgs[1] = new Image();
		arrWKImgs[1].src = "images/icon-filter.gif";
		arrWKImgs[2] = new Image();
		arrWKImgs[2].src = "images/ovr-icon-filter.gif";
		arrWKImgs[3] = new Image();
		arrWKImgs[3].src = "images/arrow-up.gif";
		arrWKImgs[4] = new Image();
		arrWKImgs[4].src = "images/arrow-dwn.gif";
		arrWKImgs[5] = new Image();
		arrWKImgs[5].src = "images/icon-changelocation.gif";
		arrWKImgs[6] = new Image();
		arrWKImgs[6].src = "images/icon-changestatus.gif";
		arrWKImgs[8] = new Image();
		arrWKImgs[8].src = "images/icon-commentunit.gif";
		arrWKImgs[9] = new Image();
		arrWKImgs[9].src = "images/icon-printdata.gif";
		arrWKImgs[10] = new Image();
		arrWKImgs[10].src = "images/icon-exportdata.gif";
		arrWKImgs[11] = new Image();
		arrWKImgs[11].src = "images/icon-printreport.gif";
		
		arrWKImgs[12] = new Image();
		arrWKImgs[12].src = "images/ovr-icon-changelocation.gif";
		arrWKImgs[13] = new Image();
		arrWKImgs[13].src = "images/ovr-icon-changestatus.gif";
		arrWKImgs[14] = new Image();
		arrWKImgs[14].src = "images/ovr-icon-commentunit.gif";
		arrWKImgs[15] = new Image();
		arrWKImgs[15].src = "images/ovr-icon-printdata.gif";
		arrWKImgs[16] = new Image();
		arrWKImgs[16].src = "images/ovr-icon-exportdata.gif";
		arrWKImgs[17] = new Image();
		arrWKImgs[17].src = "images/ovr-icon-printreport.gif";
		arrWKImgs[18] = new Image();
		arrWKImgs[18].src = "images/icon-changesample.gif";
		arrWKImgs[19] = new Image();
		arrWKImgs[19].src = "images/ovr-icon-changesample.gif";
		arrWKImgs[20] = new Image();
		arrWKImgs[20].src = "images/button-selall.gif";
		arrWKImgs[21] = new Image();
		arrWKImgs[21].src = "images/button-replace-contact.gif";
		arrWKImgs[22] = new Image();
		arrWKImgs[22].src = "images/button-add.gif";
		arrWKImgs[23] = new Image();
		arrWKImgs[23].src = "images/button-back.gif";
		arrWKImgs[24] = new Image();
		arrWKImgs[24].src = "images/button-home.gif";
		arrWKImgs[25] = new Image();
		arrWKImgs[25].src = "images/button-new.gif";
		arrWKImgs[26] = new Image();
		arrWKImgs[26].src = "images/button-remove.gif";
		arrWKImgs[27] = new Image();
		arrWKImgs[27].src = "images/button-update.gif";
		arrWKImgs[28] = new Image();
		arrWKImgs[28].src = "images/button-save.gif";
		
		imageOn[0] = new Image();
		imageOn[0].src="images/tab-on-unit.gif";
		imageOff[0] = new Image();
		imageOff[0].src="images/tab-off-unit.gif";
		imageOvr[0] = new Image();
		imageOvr[0].src="images/tab-ovr-unit.gif";
		imageOn[1] = new Image();
		imageOn[1].src="images/tab-on-cnct.gif";
		imageOff[1] = new Image();
		imageOff[1].src="images/tab-off-cnct.gif";
		imageOvr[1] = new Image();
		imageOvr[1].src="images/tab-ovr-cnct.gif";
		imageOn[2] = new Image();
		imageOn[2].src="images/tab-on-loc.gif";
		imageOff[2] = new Image();
		imageOff[2].src="images/tab-off-loc.gif";
		imageOvr[2] = new Image();
		imageOvr[2].src="images/tab-ovr-loc.gif";
		imageOn[3] = new Image();
		imageOn[3].src="images/tab-on_report.gif";
		imageOff[3] = new Image();
		imageOff[3].src="images/tab-off_report.gif";
		imageOvr[3] = new Image();
		imageOvr[3].src="images/tab-ovr_report.gif";
		imageOn[4] = new Image();
		imageOn[4].src="images/tab-on_lab.gif";
		imageOff[4] = new Image();
		imageOff[4].src="images/tab-off_lab.gif";
		imageOvr[4] = new Image();
		imageOvr[4].src="images/tab-ovr_lab.gif";		

// ========= the next functions handle the Add/Remove/Update and associated list boxes ===

// Commands
var commandsAll = new Array();
commandsAll['edit_cust_org'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'cust_org_maint')\" class=\"menuitemwhite\">Edit Customer Organizations</a>";
commandsAll['cust_org_maint'] = "<a href=\"javascript:submitLink(0,'custorg')\" class=\"menuitemwhite\">Add Customer Organizations</a>";

commandsAll['view_my_profile'] = "<a href=\"javascript:submitLink(-1,'contact')\" class=\"menuitemwhite\">View My Profile</a>";


commandsAll['add_unit'] = "<a href=\"javascript:submitLink(0,'unit')\" class=\"menuitemwhite\">Add Unit</a>";
commandsAll['add_location'] = "<a href=\"javascript:submitLink(0,'location')\" class=\"menuitemwhite\">Add Location</a>";
commandsAll['add_contact'] = "<a href=\"javascript:submitLink(0,'contact')\" class=\"menuitemwhite\">Add Contact</a>";
//commandsAll['return_shipping'] = "<a href=\"none\" class=\"menuitemwhite\">Return Shipping</a>";
commandsAll['search'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'get_search_criteria_client')\" class=\"menuitemwhite\">Search</a>";
commandsAll['search_expert'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'get_search_criteria_expert')\" class=\"menuitemwhite\">Search (TOGA Expert)</a>";

commandsAll['upload_shipp_recev'] = "<a href=\"javascript:submitForm(document.forms[1], document.forms['display'],'upload_syringes')\" class=\"menuitemwhite\">Upload Shipping / Receiving</a>";
commandsAll['upload_shipp_recev_fake'] = "<a href=\"javascript:submitForm(document.forms[1], document.forms['display'],'upload_syringe_fake')\" class=\"menuitemwhite\">Upload Shipping / Receiving</a>";
commandsAll['upload_sample_result'] = "<a href=\"javascript:submitForm(document.forms[1], document.forms['display'],'upload_samples')\" class=\"menuitemwhite\">Upload Sample Results</a>";
commandsAll['upload_sample_result_fake'] = "<a href=\"javascript:submitForm(document.forms[1], document.forms['display'],'upload_tjh2b_samples')\" class=\"menuitemwhite\">Upload Sample Results</a>";

commandsAll['upload_lube_sample_result'] = "<a href=\"javascript:submitForm(document.forms[1], document.forms['display'],'upload_lube_sample')\" class=\"menuitemwhite\">Upload Lube Sample Results</a>";//07/05/2006.VH end

commandsAll['ship_info'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'lab_order_shipping_info')\" class=\"menuitemwhite\">Shipping Info</a>";
commandsAll['receiv_info'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'receive_info')\" class=\"menuitemwhite\">Report Order Receiving Info</a>";
commandsAll['print_export'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'print_new_orders')\" class=\"menuitemwhite\">Print/Export New Orders</a>";
commandsAll['add_lab_org'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'add_lab_org')\" class=\"menuitemwhite\">Add Lab Organization</a>";
commandsAll['edit_lab_org'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'edit_lab_org')\" class=\"menuitemwhite\">Edit Lab Org</a>";
commandsAll['add_ship_org'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'shipp_org_maint')\" class=\"menuitemwhite\">Add Shipping Organization</a>";

commandsAll['view_unit_loc_changes'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'approve_unit_changes')\" class=\"menuitemwhite\">View Unit/Loc Changes</a>";
commandsAll['view_sample_date_changes'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'approve_next_sample')\" class=\"menuitemwhite\">View Sample Date Changes</a>";
commandsAll['approve_sample_results'] = "<a href=\"javascript:submitForm(document.forms[1],document.forms['display'],'approve_sample_results')\" class=\"menuitemwhite\">Approve Samples</a>";
commandsAll['shipping_administration'] = "<a href=\"none\" class=\"menuitemwhite\">Shipping Administration</a>";
//commandsAll['pricing_change'] = "<a href=\"none\" class=\"menuitemwhite\">Pricing Change</a>";
//commandsAll['view_quotes'] = "<a href=\"none\" class=\"menuitemwhite\">View Quotes</a>";
	
function addToList(cboCtrl,secondCtrl,ctrlToUpdate,delimiter){
	//add a value to a list
		var firstValue;
		var secondValue;
		var i = ctrlToUpdate.options.length ;
		var exists = false;		
		var id;
		var addDelim;
		
		if (cboCtrl.options.length == 0){
			return;
		}
		
		if (secondCtrl == ""){
			secondValue = ""
		}else{
			secondValue = secondCtrl.value;
		}	
		firstValue = cboCtrl.options[cboCtrl.options.selectedIndex].text;
		id = cboCtrl.options[cboCtrl.options.selectedIndex].value;
		
		addDelim = (secondValue=="")?"":delim;
		
		//check if value already exits
		
		
			var len = firstValue.length;
			for(var opt = 0; opt < i; opt++){
				if (ctrlToUpdate.options[opt].text.substr(0,len) == firstValue){
					alert("This value has already been set." );
					exists = true;
					break;
				}
			}
		
		
		
		
		if (exists == false){
			ctrlToUpdate.options[i] = new Option (firstValue + delimiter + secondValue, id + addDelim + secondValue); 
			//alert(ctrlToUpdate.options[i].value)
			if (secondCtrl != ""){
				secondCtrl.value = "";
			}	
			ctrlToUpdate.selectedIndex=0;
		}
	}
	
	function updateList(ctrlToUpdate, secondCtrl, delimiter){
		var currValue;
		var newValue;
		var arrVal = new Array();
		var value;
		var arrID = new Array();
		
		value = secondCtrl.value;
		if (ctrlToUpdate.options.selectedIndex == -1){
			alert("You need to select an item to update");
		}else{	
			if( ctrlToUpdate.options.length >0){
				currValue = ctrlToUpdate.options[ctrlToUpdate.options.selectedIndex].text;
				arrVal = currValue.split(delimiter);
				arrVal[1] = value;
				newValue = arrVal[0] + delimiter + arrVal[1];
				ctrlToUpdate.options[ctrlToUpdate.options.selectedIndex].text = newValue;
				arrID = ctrlToUpdate.options[ctrlToUpdate.options.selectedIndex].value.split(delim);
				arrID[1] = newValue;
				ctrlToUpdate.options[ctrlToUpdate.options.selectedIndex].value = arrID[0] + delim + arrID[1];
				secondCtrl.value = "";
			}
		}	
	}
	
	
	function removeList(ctrlToUpdate){
		if (ctrlToUpdate.options.selectedIndex == -1){
			alert("You need to select an item to remove");
		}else{	
			if( ctrlToUpdate.options.length >0){
				ctrlToUpdate.options[ctrlToUpdate.options.selectedIndex] = null;
			}
			if (ctrlToUpdate.options.length > 0){
				ctrlToUpdate.options[0].selected = true;
			}	
		}	
	}
	

	function removeSelectedFromList(ctrlToUpdate){
		//Added 4/16/2002 AJT to remove all selected items from a listbox.
		if (ctrlToUpdate.options.selectedIndex == -1){
			alert("You need to select an item to remove");
		}else{	
			for (var i = ctrlToUpdate.options.length;i--;){
				if (ctrlToUpdate.options[i].selected){
					ctrlToUpdate.options[ctrlToUpdate.options.selectedIndex] = null;
				}
			}	
		}	
	}

	function clearListbox(ctrlToUpdate) {
		// Added 2002-08-06 by GRS to clear all items from a listbox
		var intOptions = ctrlToUpdate.options.length;
		for (var i = 0; i <= intOptions; i++) {
			ctrlToUpdate.options[0] = null;
		}
	}


	
	function moveValue(selectedCtrl,ctrlToPopulate,delimiter){
		var val ;
		var arrVal = new Array();

		if(selectedCtrl.options.length == 0 ){
			return;
		}	
			val = selectedCtrl.options[selectedCtrl.options.selectedIndex].text;
			arrVal = val.split(delimiter);
			ctrlToPopulate.value = arrVal[1];	
			
	
	
	}
	//=========================================================================================
	
	function validatePhone(ctrl){
		var x;
		for(var i = 0 ; i< ctrl.value.length ; i++){
			x = ctrl.value.substr(i,1);
			if(isNaN(x)){
				switch (x){
					case"(": case ")": case".": case"#": case"-":	
						break;
					default:
						alert(x + " is not a valid phone number character");
						break;
					}
			}
		}					
	}
	
	function checkForChanges(){
	//review the contents of the controls against the original javascript session object
	// to see if there were any changes
	// this will only work if the control names have a 4 character prefix, 
	// and the rest of the control name matches the session variable. 
	// also the values passed through the dropdowns need to be integers (id's )
		// modified 2002-08-20 to accept strings as values of drop-downs of type "select-one"
	var isChanged = false;
	var i ;
	var doc=document.forms[0];
	var ctrl;
	var ses = new JS_State;
	var item;
	var regExp = / /;
	var rep = "";
	var regExp2 = "\\\\";
	var repStr = "\\";
	
  //alert("about to check for changes");
  	for(i = 0; i < doc.elements.length; i++){
		ctrl = doc.elements[i];
			for(item in ses){
				
				if (item == ctrl.name.substr(4)){
					switch(ctrl.type){
						case "text": case "textarea": case "password":
							if (ctrl.value.replace(regExp,rep).replace(regExp2,repStr)  != ses[item].replace(regExp,rep).replace(regExp2,repStr)){
								isChanged = true;
								//alert(  "ctrl.value:" + ctrl.value +"  " + ctrl.value.replace(regExp,rep).replace(regExp2,repStr) +"  " + "ses[item]:" + ses[item] + "  " +  ses[item].replace(regExp,rep).replace(regExp2,repStr) + " " + ctrl.name);
							}
							break;
						case "checkbox":
							if ((ctrl.checked) && ses[item].replace(regExp,rep)=="False") {
								isChanged = true;
								//alert("Checkbox 1");
							}
							if (!(ctrl.checked) && ses[item].replace(regExp,rep)=="True") {
								isChanged = true;
								//alert("Checkbox 2");
							}
							break;
						case "select-one": 
							if (ctrl.value.replace(regExp,rep) == "" || ses[item].replace(regExp,rep)=="") //check for empty strings 
								{
								if (ctrl.value.replace(regExp,rep) != ses[item].replace(regExp,rep)) {
									isChanged = true;
									//alert("Select-One " + " ses=" + ses[item] + " ctrl=" + ctrl.value + " itm=" + item);
								}
								else
									break;	
								}
							if (isInteger(ctrl.value) && isInteger(ses[item]) ) {
								if (parseInt(ctrl.value,10) != parseInt(ses[item],10) ){
									isChanged = true;
									//alert("parsInt() " + ctrl.value +"  " + ses[item] + "  " + ctrl.name);
									break;
							} else 
								// if values are not numeric, then just compare strings
								if (ctrl.value != ses[item]){
									isChanged = true;
									//alert("compare strings: " + ctrl.value +"  " + ses[item] + "  " + ctrl.name);
								}
							}	
							break;
						
						case "select-multiple":
							if (ses[item].length > ctrl.options.length ||
								ses[item].length < ctrl.options.length){  // if unequal elemements then we know it's been changed
									isChanged = true;
									//alert("Select Multiple " + ses[item].length +  " " + ctrl.options.length + " " + item);
									break;
									}
							for(var idx=0;idx<ctrl.options.length;idx++) {
								var val = ctrl.options[idx].value;	
								if (val.replace(regExp,rep) != ses[item][idx].replace(regExp,rep)){
									isChanged = true;
									//alert("For each select multiple");
									break;
									}
								}
							break;		
					}	
				}	
			}	
	}
	return isChanged;
}	

function writeimg(height, width){
//insert a blank image.  Used for positioning in Netscape, when NS is 
//being difficult about positioning tables etc.
	if  (NS == true){
		var tempstring;
		if (height == "" || height == null){
			height = 1;
		}	
		if (width == "" || width == null){
			width = 175;
		}
		tempstring = "<img border=0 src='images/spacer.gif' height=" + height + " width="+ width + ">";
		document.write(tempstring);
	}	
}		



function overIcon(currImg){
			var srcString=currImg.src;
			srcString = srcString.slice(srcString.lastIndexOf("/")+1);
			if (srcString.slice(0,3)=="ovr"){
				srcString = "images/" + srcString.slice(4);
			}else{
				srcString = "images/ovr-"  + srcString;
			}
			currImg.src = srcString;
		}
		
		
function submitLink(value, linkData){
//make sure that controls required here are added to writeApplicationControls()
		var command;

		document.forms[1].target="_self";
		
		switch(linkData){
			case'location':
			// if we are on the location page, just call the "new" function
				if (document.forms["location"]){
					newLocation();
					return;
				}else{
					//zero out the key field so we know we are doing an add
					document.forms["display"].txt_location.value=value;
					command="location";					

//					document.forms[1].target="_blank";
// commented out to display location in same window. 5/1/2002 AJT
				}	
				break;
			case'unit':
				if (document.forms["unitprofile"]){
					newUnit();
					return;
				}else{
					document.forms["display"].txt_unitid.value=value;
					command="unitprofile";
				}
				break;
			case'unitprofile':
					document.forms["display"].txt_unitid.value=value;
					command="unitprofile";
					document.forms[1].target="_blank";
				break;
			case'unitworkbench':
					command="getunitworkbench";
					document.forms[1].target="_blank";
				break;
			case'contact':
			// if we are on the user page, just call the "new" function				
				if (document.forms["userprofile"]){					
					if (value==-1){
						//-1 = view my profile
						document.forms["display"].txt_personid.value=ses.login_user_party_id;
						command="userprofile";						
					}else{
						//0 = new contact					
						newPerson();
						return;
					}	
				}else{
					document.forms["display"].txt_personid.value=value;
					command="userprofile";
				}	
				break;
			case'laborderdetail':
					document.forms["display"].txt_labOrderID.value=value;
					command="getlaborderdetail";
				break;				
			case'custorg':
			// if we are on the customer org page, just call the "new" function
				if (document.forms["custorg"]){
					newCustOrg();
					return;
				}else{
					//document.forms["display"].stt_accountid.options(document.forms["display"].stt_accountid.selectedIndex).value=value;
					if(document.forms["display"].stt_accountid) {
						document.forms["display"].stt_accountid.value=value;	//this should work for select lists or for hidden or text fields
					}
					command="cust_org_maint";
				}
				break;
			case 'sample_report':
				document.forms["display"].txt_reportorderid.value=value;
				command="sample_report";
				document.forms[1].target="_blank";
				break;
			case 'location_from_unit':
				document.forms["display"].txt_location.value=value;
				command="locationpicker";

				locationFromUnit(command)
				return;
				
				//document.forms[1].target="_blank";
				//break;				
			case 'company_location_from_user_profile':
				document.forms["display"].txt_location.value=value;
				command="companylocationpicker";				
				locationFromUserProfile(command)
				return;
			case 'shippingpkginfo':
				command="shippingpkginfo";				
				shippingPkgFromLabOrg(command,value);
				return;
				
			case 'new_location_from_location_picker':
				document.forms["display"].txt_location.value=value;
				command="location";
				document.forms[1].target="_blank";
				break;				
			case 'reportorder':
				document.forms["display"].stt_accountid.value=value;
				command="reportorder";		
				document.forms[1].target="_blank";							
				break;				
			}
			
			submitForm(document.forms[1],document.forms["display"],command);	
			document.forms[1].target="_self";			
	}
	
	function writeApplicationControls(){
		document.write('<input type="hidden" name="txt_location" id="txt_location" value="" />');
		document.write('<input type="hidden" name="txt_unitid" id="txt_unitid" value="" />');
		document.write('<input type="hidden" name="txt_personid" id="txt_personid" value="" />');
	}
	function chgAccount(command){
			var doc=document.forms["display"];
			if (doc.cbo_locfilter){doc.cbo_locfilter.options.selectedIndex = -1;}
			if (doc.cbo_dtfilter){doc.cbo_dtfilter.options.selectedIndex = -1;}
			if (doc.cbo_condCodefilter){doc.cbo_condCodefilter.options.selectedIndex = -1;}
			submitForm(document.forms[1], document.forms['display'], command);
			
		}
		
	function selAccount()	{
	
		if (ses.accountid == "0" || ses.accountid==""){
			if (document.forms["display"].stt_accountid.type == "select-one"){
				document.forms["display"].stt_accountid.selectedIndex=-1;
			}
		}else{	
			if (document.forms["display"].stt_accountid.type == "select-one"){
				selectMatch(ses.accountid,document.forms["display"].stt_accountid);	
			}	
		}	
		
	}	
	// populate location based on account
	function objAccount(){
		this.acctId;
		this.locations = new Array();
	}
	
	function objLocations(){
		this.location_name;
		this.location_id;
	}
	
	function loadLocationsArray(){
	arrAcct = acctStr.split(delim);

		for( var i=0; i< arrAcct.length -1; i++){
			var strHold = arrAcct[i];
			arrLoc = arrAcct[i].split(pairDelim);
			arrAcct[i] = new objAccount()
			arrAcct[i].acctId = arrLoc[0];
			for(var x = 1; x < arrLoc.length  ; x++){
				arrNameID = arrLoc[x].split(singleDelim);
				arrAcct[i].locations[x-1] = new objLocations();
				arrAcct[i].locations[x-1].location_id = arrNameID[0];
				//alert(arrNameID[1])
				arrAcct[i].locations[x-1].location_name = arrNameID[1];
			}	
		}
	
		ses.arrAccount = arrAcct;
		
		}
		
		
	function loadLocations(ctrl){
	var acctSelected;
	var locArray;
	var index;
	var ind;
	var strHold;
	var objAcctValue;
	//populate locations based on the selected company
	ind = 0;
	//if(!document.forms["display"].stt_accountid){return;}
	if(!ctrl){
		document.forms["display"].cbo_locations.options.length = 0;
				locArray = ses.arrAccount[0].locations;
				if (locArray){
					for (var x = 0; x<locArray.length; x++){
						document.forms["display"].cbo_locations.options[x] = new Option(locArray[x].location_name,locArray[x].location_id);
					}
				}
	}else{
		index = ctrl.options.selectedIndex;
		strHold = ctrl.options[index].text;
	
		ind = strHold.indexOf("/") + 2;
		acctSelected = strHold.slice(ind);
		
		for (var i = 0; i < ses.arrAccount.length;i++){
		objAcctValue = ses.arrAccount[i].acctId;
			if (acctSelected == objAcctValue){
				document.forms["display"].cbo_locations.options.length = 0;
				locArray = ses.arrAccount[i].locations;
				for (var x = 0; x<locArray.length; x++){
					document.forms["display"].cbo_locations.options[x] = new Option(locArray[x].location_name,locArray[x].location_id);
				}
			}	
		
		}
	}	
}

// these 3 functions are used to pull the account id out of the text of a combo box
function selAccountbyText(ctrl){
	var acct;
	var regExp = / /;
	var regExp2 = / \(InActive\)/;
	var rep = "";
	if (!ctrl){return;}

	for (var i = 0; i< ctrl.options.length ; i++){
		acct = checkAcct(ctrl.options[i]);
		acct = acct.replace(regExp2,rep);
		acct = acct.replace(regExp,rep);
		
		if (acct == ses.accountid){
			ctrl.options.selectedIndex = i;
			break;
		}
	}
}

function writAcct(ctrl){
	var acct;
	acct = checkAcct(ctrl.options[ctrl.options.selectedIndex]);
	document.forms["display"].stt_accountid.value = acct;
}

function checkAcct(opt){
	var accountid;
	var acctDelim="/";
	var optText;
	
	optText = opt.text;
	accountid = optText.substr(optText.indexOf(acctDelim)+1);
	
	return accountid;
}



//Added the following two functions to get the value of the control in question
//instead of the text as the writAcct function does.AJT 4/29/2002

function writAcct2(ctrl){
	var acct;
	acct = checkAcctValue(ctrl.options[ctrl.options.selectedIndex]);
	document.forms["display"].stt_accountid.value = acct;
}

function checkAcctValue(opt){
	var accountid;
	var acctDelim="^";
	var optText;
	
	optText = opt.value;
	accountid = optText.substr(optText.indexOf(acctDelim)+1);
	
	return accountid;
}

//Added the previous two functions to get the value of the control in question
//instead of the text as the writAcct function does.AJT 4/29/2002


function submitReport(objForm,objDataForm,command){
	hWindow = window.open("rptpending.html",command,"status,resizable,height=400,width=600");
	hWindow.focus();
	objForm.target=command;
	doSubmit(objForm,objDataForm,command);
}

function getAddress(){
		var doc = document.forms["display"];
//		var index = doc.cbo_shippingContact.options.selectedIndex;
//		var shipID = (index>-1)?doc.cbo_businessContact.options[index].value:"";

		//Update bill to address with Business Contact's address (from array in session object) AJT 5/8/2002
		var index = doc.cbo_shippingContact.options.selectedIndex;
//		doc.cbo_shippingContact.value = arrBusinessAddress[index];
		//addresstext.innerHTML = arrBusinessAddress[index];
		addresstext.innerHTML = arrShippingAddress[index];
		//doc.lblAddress.innerhtml = arrBusinessAddress[index];
		//Update bill to address with Business Contact's address (from array in session object) AJT 5/8/2002			

		
//		doc.txt_billToAddress.value = "";
//
//		for(var i=0 ; i<arrContactIDAddress.length ; i++){
//			if (shipID == arrContactIDAddress[i]){
//			 	doc.txt_billToAddress.value = (arrContactAddress[i]=="")?"Address is not available":arrContactAddress[i];
//				break;
//			}
//		}	
	}
	
function pgFocus(ctrl){

	if(ctrl.disabled){
		ctrl.blur();
	}
}

function stripSpace(value){
	var regExp = / /;
	var rep = "";
	
	value = value.replace(regExp,rep);
	return value;
}

function checkPhone(ctrl){
	var doc = document.forms["display"];
	
	if(validateNumber(ctrl) == false){
		for(var i=0;i<ctrl.value.length;i++){
			if(isNaN(ctrl.value.charAt(i)) && ctrl.value.charAt(i)!="."){
				alert(ctrl.value.charAt(i) + " is not a valid character for a phone number");
				return false;
			}	
		}
	}	
	
	switch(ctrl.name){
		case"txt_phone":
			if(ctrl.value.length >= 20){
				doc.txt_phone_ext.focus();
			}
			break;
		case"txt_phone_ext":
			if(ctrl.value.length >= 7){
				document.images["btn_add_phone"].focus();
			}
			break;	
		case"txt_mainPhoneNumber":
			if(ctrl.value.length >= 20){
				doc.txt_customerURL.focus();
			}
		break	
	}
}

function checkReadOnly(edit,loginRole){
		var el;
		if(!edit){
			for (el = 0 ; el <document.forms['display'].elements.length; el++){
				document.forms['display'].elements[el].disabled = true;
			}
		}
	}

function setSamplingFreq(ctrl)
{
	var ses1 = new JS_State();
	var oneMinute = 60 * 1000;  // milliseconds in a minute
	var oneHour = oneMinute * 60;
	var oneDay = oneHour * 24;
	var oneWeek = oneDay * 7;
		
	var futureDate;
	var myDate;
	var origDate = ses1.nextSampleDate;
	
	//If we are adding a new unit, there will be no next sample date
	// We do not want to mess with the next sample date if we are dealing with a new unit
	//  It is set once during initialize and we want the onchange event of the frequency to 
	//   leave this alone.
	if (origDate == '') 
	{	return;
		//origDate = new Date();
		//origDate = formatDate(origDate);
	}		
		
	//Turn the next sample date into a time value so we can do some arithmetic
	myDate = new Date(origDate).getTime();  
	myDate = myDate + ses.samplingFreqs[ctrl.options.selectedIndex] * oneDay;
		
	//Format a new date value in the form "04/30/2002"
	futureDate = new Date(myDate);
	myDate = formatDate(futureDate);

	//If the freq is zero, use the original next sample date
	if (ctrl.options.selectedIndex == 0) 
		document.forms["display"].txt_nextSampleDate.value = origDate;
	else
		document.forms["display"].txt_nextSampleDate.value = myDate; 
}


//pass this function document.all.wait (wait is the id of the image being passed)
function centerImage(myDocument) 
{
	//Simple browsercheck
	var n = (document.layers) ? 1:0;
	var ie = (document.all) ? 1:0;
	var oCont;
	
    //Getting the height and width of the document
    pageWidth=(n)?innerWidth:document.body.offsetWidth;
    pageHeight=(n)?innerHeight:document.body.offsetHeight;
    
    //Making a simple object for the divCont
    //oCont=(n)?eval('document.Searching'):eval('Searching.style')
    oCont=(n) ? eval(myDocument): eval(myDocument.style);
    
    //Moving the divCont to the center of the page.
    oCont.left=pageWidth/2-175;
    oCont.top=pageHeight/2;
    myDocument.style.visibility="";
}

	function mouseChangeTab(over,ctrl){
		var ctrlSrc = new String(ctrl.src);
		var ImgOnSrc;
		var index;
		
		switch(ctrl.name){
			case "tab1":
				index=0;
				break;
			case "tab2":
				index=1;
				break;
			case "tab3":
				index=2;
				break;
			case "tab4":
				index=3;
				break;
			case "tab5":
				index=4;
				break;				
			}			
			
		ImgOnSrc = new String(imageOn[index].src);
		
		ctrlSrc=ctrlSrc.slice(ctrlSrc.lastIndexOf("/"));
		ImgOnSrc=ImgOnSrc.slice(ImgOnSrc.lastIndexOf("/"));
		
		if (ctrlSrc == ImgOnSrc ){
			return;
		}	
			if(over == 'true'){
				ctrl.src = imageOvr[index].src
			}else{
				ctrl.src=imageOff[index].src	;
			}
	}
	
	function tabChange(onTab){
			var	doc = document.forms["display"];
			var index;
			var command;
		switch(onTab.name){
			case "tab1":
				index=0;
				command="getUnitWorkbench";
				break;
			case "tab2":
				index=1;
				command="getContactWorkbench";
				break;
			case "tab3":
				index=2;
				command="getLocationWorkbench";
				break;
			case "tab4":
				index=3;
				command="reportorder";
				break;
			case "tab5":
				index=4;
				command="getLabOrderWorkbench";
				break;				
			}			
			submitForm(document.forms["workbench"],document.forms["display"],command);
		}
		
		
			
	function pgMouseOver(ctrl){
		if( ctrl.name =="tab1" || ctrl.name=="tab2" || ctrl.name=="tab3" || ctrl.name=="tab4" || ctrl.name=="tab5"){
			mouseChangeTab('true',ctrl);
		}
	}  
	function pgMouseOut(ctrl){
		if( ctrl.name =="tab1" || ctrl.name=="tab2" || ctrl.name=="tab3" || ctrl.name=="tab4" || ctrl.name=="tab5"){
			mouseChangeTab('false',ctrl);
		}
	}  
	
	function pgClick(ctrl){
		if( ctrl.name =="tab1" || ctrl.name=="tab2" || ctrl.name=="tab3" || ctrl.name=="tab4" || ctrl.name=="tab5"){
			tabChange(ctrl);
		}  
	}

function getNextMonday(mydate)
{
	var nextMonday = new Date(mydate);
	daystoAdd = 8 - nextMonday.getDay();
	if (daystoAdd==8) daystoAdd=1;
	nextMonday.setDate(nextMonday.getDate()+daystoAdd);
	
	return nextMonday;
}

function formatDate(inputDt) 
{ 
	var newDate;
	newDate = inputDt.getMonth()+1+'/'+inputDt.getDate()+'/'+inputDt.getFullYear();
	return newDate; 
} 

function locationFromUnit(command)
{
	var ls='toolbar=0,location=0,directories=0,status=0,menubar=0,right=200,top=250,width=400,height=200,scrollbars=1,resizable=1';
	var objDataForm = document.forms["unitprofile"];
	var objForm = document.forms['display'];
	objForm.ctrl_CallerID.value = objForm.name;
	objForm.ctrl_Command.value = command;
	objForm.ctrl_ApplicationID.value = 'TOGA';	
	objDataForm.ctrl_XML.value =  buildRequestXML(objForm);
	
	var xmlString = document.forms["unitprofile"].ctrl_XML.value;
	var URLString = 'controller.asp?ctrl_XML=' + xmlString;

	//	Form is being submitted instead of using window.open as 
	//  any %26 (&) in a querystring is reconverted back to '&' in ASP page and it
	// fails when loading the XML DOM - KPS 08/27/02
	
	//alert(URLString);	
	//window.open(URLString,"menu",ls);
	//window.open(URLString);
	objDataForm.action = "controller.asp"
	objDataForm.target = "_blank"
	//alert('just before submitting');
	objDataForm.submit()
	objDataForm.action = ""
	objDataForm.target = ""

	return;
}

function locationFromUserProfile(command)
{
	var ls='toolbar=0,location=0,directories=0,status=0,menubar=0,right=150,top=200,width=550,height=450,scrollbars=1,resizable=1';
	var objDataForm = document.forms["userprofile"];
	var objForm = document.forms['display'];
	objForm.ctrl_CallerID.value = objForm.name;
	objForm.ctrl_Command.value = command;
	objForm.ctrl_ApplicationID.value = 'TOGA';
	
	var strXML = "<data>";
	strXML += "<txt_editacct value='" +objForm.txt_editacct.value + "'/>";
	strXML += "<txt_access value='" +objForm.txt_access.value + "'/>";
	strXML += "<ctrl_CallerID value='display'/><ctrl_Command value='companylocationpicker'/><ctrl_ApplicationID value='TOGA'/>"
	strXML += "<ctrl_GUID value='" + ses.GUID + "' />";
	strXML += "</data>";
	var strURL = "controller.asp?ctrl_XML=" + strXML;

	window.open(strURL,"menu",ls);
	return;
}

function exportData(form, command)
{
	document.forms["display"].export_flag.value="1";

	doSubmit(document.forms[form],document.forms["display"],command);	
//	submitReport(document.forms[form],document.forms["display"],command);	
	document.forms["display"].export_flag.value="0";

}	

function submitLinkSearch(parm1,parm2,accountid)
{
	try {
		document.forms["display"].stt_accountid.value = accountid;
	}
	catch(e) {}
	submitLink(parm1,parm2);
}