	/*
	Enable repopulation of some form fileds on the client, no need to reload the whole page.
	Change Control
	CID			Date		Who	Description
	Cid050405   			ST	Implement SwSelectedProducts switch
	Cid1		21/06/2004	ST	Rephrase error message.
	CID2        22/05/2008  AF  Add trapping of key enter on SearchName Field
	*/

	//Global vars
	var DisplayLength = 1024;		/* Maximum number of records to return from Database? */
	var allOptArrays = new Array(DisplayLength); /* keep additional product details */
	//var companyID = 0;		/*current company id*/
	var selIndex = 0;			/*number of selected situations, used to enable or disable buttons*/
	var iSelCount = 0;			/*number of selected situations*/
	var iSelType = 0;			/*ID of product type, used to narrow down number of returned pests*/
	var iMaxSituation = 32;		/*maximum number of situations allowed to be sumbited*/
	var bSearch = true;      /*to prevent resubmiting search requests while previous search is in progress*/
	var sSearchString = '';  /*holds current search string*/
	var keyTimeout;
	var bEnter = false;

    function refresh_list(){
		if (keyTimeout){
		 clearTimeout(keyTimeout);
		}
		keyTimeout= setTimeout('doRefresh_list()',50);
	}
    function refresh_list2(){
		if (keyTimeout){
		 clearTimeout(keyTimeout);
		}
		if (!bEnter){	
			keyTimeout= setTimeout('doRefresh_list()',4000);
		}
	}
    function doRefresh_list(){

		//called on 'onkeyup'
		//make remote call back to server

		//jsrsPOST doesn't work, GET used instead
		//jsrsPOST = document.forms['frmPost']['chkPost'].checked;
		//jsrsPOST = true;
		//alert(DisplayLength);

		jsrsPOST = '';
        var iLookCrop = "0";

	    document.frmRmt.NameList.length = 0					/*reset current list*/

	    var inValue  = document.frmRmt.SearchName.value;	/*current typed value, used for remote lookup*/
	    //iSelType = document.frmRmt.SelectedType.value;
	    if (bSearch){
			if (inValue.length > 0) /*number of characters after we go search*/
			{
				showLong();

				bSearch = false;
                //add crop search options
                if (document.forms['frmRmt'].rbLookCrop[1].checked){iLookCrop = "1";}
                if (document.forms['frmRmt'].rbLookCrop[1].checked){iLookCrop = "2";}
                if (document.forms['frmRmt'].rbLookCrop[1].checked){iLookCrop = "3";}


				sSearchString = inValue;
				if (document.forms['frmRmt'].rbLookWhere[1].checked){
					inValue = "%" + inValue;
				}
				inValue = inValue.replace(/\?/g,'_');
				//alert(inValue);
                //add crop search options

                if (document.forms['frmRmt'].rbLookCrop[0].checked){inValue += "~^1";}
                if (document.forms['frmRmt'].rbLookCrop[1].checked){inValue += "~^3";}
                if (document.forms['frmRmt'].rbLookCrop[2].checked){inValue += "~^2";}
                if (document.forms['frmRmt'].rbLookCrop[3].checked){inValue += "~^0";}

    			//alert(inValue);


				if (document.forms['frmRmt'].rbLookWhat[0].checked){
					//name
					//Cid050405
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "oneSituationName"));		
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "oneSituationName"));
				}
				if (document.forms['frmRmt'].rbLookWhat[1].checked){
					//scientific name
					//Cid050405
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "oneSituationScfName"));
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "oneSituationScfName"));
				}
				if (document.forms['frmRmt'].rbLookWhat[2].checked){
					//both
					//Cid050405
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "oneSituation"));
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "oneSituationGroup"));
				}
				if (document.forms['frmRmt'].rbLookWhat[3].checked){
					//by group name
					//Cid050405
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "oneSituationGroup"));
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "oneSituationGroup"));
				}	
				//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "oneSituation"));		
			}	
		}
    }
    function fCallBack( returnstring ){
		//parse returned string and populate NameList options
		//alert(returnstring);
		//window.prompt("SQL",returnstring);

		bSearch = true;
		document.frmRmt.SearchName.value = sSearchString;
		var myArray = returnstring.split(":");

		/*
		for (i = 0; i < myArray.length-1; i++)
		{
			var myOpt = new Option
			var optArray = myArray[i].split("^");
			allOptArrays[i] = optArray;

			lastIndex = i;			  
			myOpt.value = optArray[1];
			myOpt.text  = optArray[0];			

			if (i < myArray.length) 
			{
			  document.frmRmt.NameList.options[i] = myOpt;
			}
		}
		*/
		for (i = 0; i < myArray.length-1; i++)		
		{
			var myOpt = new Option
			var optArray = myArray[i].split("^");
			allOptArrays[i] = optArray;

			lastIndex = i;			  
			myOpt.value = optArray[1];
			if (optArray[2].length > 0){
				if (document.forms['frmRmt'].rbLookWhat[1].checked){
					//scientific name
					myOpt.text  = ' (' + optArray[2] +') ;' + optArray[0];								
				}else{			
					myOpt.text  = optArray[0]+ ' (' + optArray[2] +')';			
				}
			}else{
				myOpt.text  = optArray[0];			
			}	
			if (i < myArray.length) 
			{
			  document.frmRmt.NameList.options[i] = myOpt;
			}
		}
		
		if (document.frmRmt.NameList.length > 0){
			if (document.getElementById)
			{
				document.getElementById("msgSearchName").style.color = "black";
				document.getElementById("msgSearchName").innerText = " ";
			}
			else if (document.all)
			{
				document.all["msgSearchName"].style.color = "black";				
				document.all["msgSearchName"].innerText = " ";
			}
			else if (document.layers)
			{
				document.layers["msgSearchName"].style.color = "black";							 	
				document.layers["msgSearchName"].innerText = " ";
			}

			document.frmRmt.NameList.options[0].selected = true;
			showLong(document.frmRmt.NameList);			
		}else{	
			if (document.getElementById)
			{
				document.getElementById("msgSearchName").style.color = "red";
				//Cid1	21/06/2004	ST	Rephrase error message.				
				//document.getElementById("msgSearchName").innerText = " No situations found, please try again for different name!";
				document.getElementById("msgSearchName").innerText = " No situations found, please try a different name!";				
			}
			else if (document.all)
			{
				document.all["msgSearchName"].style.color = "red";				
				document.all["msgSearchName"].innerText = " No situations found, please try a different name!";
			}
			else if (document.layers)
			{
				document.layers["msgSearchName"].style.color = "red";							 	
				document.layers["msgSearchName"].innerText = " No situations found, please try a different name!";
			}
		
		}	
      
    } 
	function frmSubmit(){
		//update ProductID with selected option before submiting the form, as it is needed for next step

		objList = document.frmRmt.NameList;

		if (objList.selectedIndex > -1){
			document.frmRmt.SituationID.value = objList.options[objList.selectedIndex].value;
			document.frmRmt.SelectedList.value = objList.options[objList.selectedIndex].value;
			if (document.frmRmt.SituationID.value >0){
				document.frmRmt.submit();
			}else{	
				//alert("Please select an Option!");
				alert("Please enter the search terms!");				
				document.frmRmt.SearchName.focus();
			}
		}else{	
			//alert("Please select an Option!");
			alert("Please enter the search terms!");			
			document.frmRmt.SearchName.focus();
		}

	}	
	function showLong(objList){
		//display long names

		var sText;
		if (typeof objList == "object"){
			sText = objList.options[objList.selectedIndex].text;
		}else{
			sText = "";
		}	
		if (document.getElementById)
		{
			document.getElementById("spShowLong").innerText = sText;
		}
		else if (document.all)
		{
			document.all["spShowLong"].innerText = sText;
		}
		else if (document.layers)
		{
			document.layers["spShowLong"].innerText = sText;
		}
        updateSituation();
	}
	function updateSituation(){
		//make remote call to database to retrive all situations for current company and selected pests

		//objList = document.frmRmt.SelectedList;
		objList = document.frmRmt.NameList;

		if (objList.length == 0){
			document.frmRmt.SituationList.length = 0;
			//showSitCounts();
		}else{
			lstSelectedPests = objList.options[objList.selectedIndex].value;
            //alert(lstSelectedPests);
			jsrsExecute("/webservices/rmtServerPest_rs.asp", showPests, "getRmt", Array(lstSelectedPests, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "Pests4Situation"));
		}

	}
	function showPests(returnstring){
		//display returned pests
		//alert(returnstring);
		//prompt("ok",returnstring);
		iSelCount = 0; //reset selected counter
	    document.frmRmt.SituationList.length = 1
		var myArray = returnstring.split(":");

		for (i = 0; i < myArray.length-1; i++)
		{
			var myOpt = new Option
			var optArray = myArray[i].split("^");
			allOptArrays[i] = optArray;

			lastIndex = i;
			myOpt.value = optArray[0];
			myOpt.text  = optArray[1];
			if (i < myArray.length)
			{
			  document.frmRmt.SituationList.options[i] = myOpt;
			  /* select all checkbox removed
			  if (document.frmRmt.cbSelAll.checked){
				 document.frmRmt.SituationList.options[i].selected = true;
			  }
			  */
			}
		}

		document.frmRmt.SituationList.options[0].selected = true;
		iSelCount = 1;
		//showSitCounts();
	}
   	function pdaShowLong_nonPdaView(objList){
		//
		//alert("display long names");

		var sText;
		if (typeof objList == "object"){
			sText = objList.options[objList.selectedIndex].text;
		}else{
			sText = "";
		}	
		if (document.getElementById)
		{
			document.getElementById("spShowLong").innerText = sText;
		}
		else if (document.all)
		{
			document.all["spShowLong"].innerText = sText;
		}
		else if (document.layers)
		{
			document.layers["spShowLong"].innerText = sText;
		}
		
	}

	function pdaShowLong(objList){
		//
		//alert("display long names");
		var sText;
		if (typeof objList == "object"){
			sText = objList.options[objList.selectedIndex].text;
		}else{
			sText = "";
		}	
		if (document.getElementById)
		{
			document.getElementById("spShowLong").innerText = sText;
		}
		else if (document.all)
		{
			document.all["spShowLong"].innerText = sText;
		}
		else if (document.layers)
		{
			document.layers["spShowLong"].innerText = sText;
		}
		else {
			document.spShowLong.innerText = sText;
		}
	}
	function frmPdaSubmit(){
		//submit searchString
		document.frmRmt.SituationID.value = 0;			//reset situationid for new search
		document.frmRmt.SearchWhere.value = 0;			//starth with
		if (document.forms['frmRmt'].rbLookWhere[1].checked){
			document.frmRmt.SearchWhere.value = 1;		//anywhere
		}

		document.frmRmt.SearchWhat.value = 0;			//Common Name
		if (document.forms['frmRmt'].rbLookWhat[1].checked){
			document.frmRmt.SearchWhat.value = 1;		//Scientific Name
		}
		if (document.forms['frmRmt'].rbLookWhat[2].checked){
			document.frmRmt.SearchWhat.value = 2;		//Both
		}
		if (document.forms['frmRmt'].rbLookWhat[3].checked){
			document.frmRmt.SearchWhat.value = 3;		//group name
		}
		document.frmRmt.submit();

	}	
	

	function showHelp() {
	    if (document.getElementById("trhelp")!= null)
	        document.getElementById("trhelp").style.display = document.getElementById("trhelp").style.display == "" ? "none" : "";

	}
