	/*
	Enable repopulation of some form fileds on the client, no need to reload the whole page.
	Change Control
	CID			Date		Who	Description
	Cid050221				ST	search pest by group name
	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 = 2048;		/* 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 pests, 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(){
		//alert("refresh list")
		if (keyTimeout){
		 clearTimeout(keyTimeout);		
		} 
		keyTimeout= setTimeout('doRefresh_list()',30);
	}
    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("doRefresh list")
		
		jsrsPOST = '';

	    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;		
	    //reset GroupByName flag
		document.frmRmt.GroupByName.value = 0;

        //document.getElementById("divWhere").style.visibility = "visible";
        //document.getElementById("divWhat").style.visibility = "visible";

	    if (bSearch){
			if (inValue.length > 0) /*number of characters after we go search*/
			{
				showLong();

				bSearch = false;
				sSearchString = inValue;

				if (document.forms['frmRmt'].rbLookWhere[1].checked){
					inValue = "%" + inValue;
				}
				inValue = inValue.replace(/\?/g,'_');

				if (document.forms['frmRmt'].rbLookWhat[0].checked){
					//pest name
					//alert("doRefresh list Pest")
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "onePestPestName"));
                    //alert(inValue);
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "onePestPestName"));

				}
				if (document.forms['frmRmt'].rbLookWhat[1].checked){
					//scientific name
					//alert("doRefresh list sf name")
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "onePestScfName"));
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "onePestScfName"));
				}
				if (document.forms['frmRmt'].rbLookWhat[2].checked){
					//both
					//alert("doRefresh list both")
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "onePest"));
					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "onePest"));
				}
				if (document.forms['frmRmt'].rbLookWhat[3].checked){
					//Cid050221
					document.frmRmt.GroupByName.value = 1;
					//grouped by common name
					//alert("doRefresh list group by name")
					//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "onePestGroupName"));
                    //alert(inValue);
   					jsrsExecute("/webservices/rmtServerPest_rs_Req.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, sRequestor, "onePestGroupName"));
            	}
			}
		}
    }
    function fCallBack( returnstring ){
		//parse returned string and populate NameList options
		//alert(returnstring);
		//prompt('ok',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];
			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").innerHTML = " ";
			}
			else if (document.all)
			{
				document.all["msgSearchName"].style.color = "black";
				document.all["msgSearchName"].innerHTML = " ";
			}
			else if (document.layers)
			{
				document.layers["msgSearchName"].style.color = "black";							 	
				document.layers["msgSearchName"].innerHTML = " ";
			}

			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").innerHTML = " No pests found, please try again for different name!";
				document.getElementById("msgSearchName").innerHTML = " No pests found, please try a different name!";
			}
			else if (document.all)
			{
				document.all["msgSearchName"].style.color = "red";
				document.all["msgSearchName"].innerHTML = " No pests found, please try a different name!";
			}
			else if (document.layers)
			{
				document.layers["msgSearchName"].style.color = "red";							 	
				document.layers["msgSearchName"].innerHTML = " No pests found, please try a different name!";
			}
		
		}	
      
    }
	function frmSubmit(){
		//update PestID with selected option before submiting the form, as it is needed for the next step

		objList = document.frmRmt.NameList;
		objSituationList = document.frmRmt.SituationList;
        //alert(objList.options[objList.selectedIndex].value);
        //alert(objSituationList.options[objSituationList.selectedIndex].value);
		if (objList.selectedIndex > -1){
			document.frmRmt.SituationGroupName.value = objSituationList.options[objSituationList.selectedIndex].text;
			document.frmRmt.PestID.value = objList.options[objList.selectedIndex].value;
			if (document.frmRmt.PestID.value >0){
				document.frmRmt.submit();
			}else{
				//if Group by name PestId can be 0
				if (document.frmRmt.GroupByName.value == 1) {
					//if Group by name PestId can be 0
					//make it look as a multi search as we have grouped pest name
                    //alert(objList.options[objList.selectedIndex].text);
					document.frmRmt.GroupName.value = objList.options[objList.selectedIndex].text;
                    //alert(document.forms['frmRmt'].rbLookWhat[3].checked);
        		    if (document.forms['frmRmt'].rbLookWhat[3].checked){
        			    //grouped by common name
    					document.frmRmt.SituationGroupName.value = objSituationList.options[objSituationList.selectedIndex].value;
                  	}else{
    					document.frmRmt.SituationGroupName.value = objSituationList.options[objSituationList.selectedIndex].text;
                    }

					document.frmRmt.AdvSearch.value = 0;
					document.frmRmt.submit();
				}else{
					alert("Please enter the search terms!");
					document.frmRmt.SearchName.focus();
				}
			}
		}else{
				alert("Please enter the search terms!");
			document.frmRmt.SearchName.focus();
		}

	}	
	function showLong(objList){
		//display long names
        //IS000149 and populate situations
		var sText;
		if (typeof objList == "object"){
			sText = objList.options[objList.selectedIndex].text;
            //alert(objList.options[objList.selectedIndex].value);
		}else{
			sText = "";
		}
		if (document.getElementById)
		{
			document.getElementById("spShowLong").innerHTML = sText;
		}
		else if (document.all)
		{
			document.all["spShowLong"].innerHTML = sText;
		}
		else if (document.layers)
		{
			document.layers["spShowLong"].innerHTML = sText;
		}
		updateSituation();

	}
	function showLong_old(objList){
		//display long names
		var sText;
		if (typeof objList == "object"){
			sText = objList.options[objList.selectedIndex].text;
		}else{
			sText = "";
		}
		if (document.getElementById)
		{
			document.getElementById("spShowLong").innerHTML = sText;
		}
		else if (document.all)
		{
			document.all["spShowLong"].innerHTML = sText;
		}
		else if (document.layers)
		{
			document.layers["spShowLong"].innerHTML = sText;
		}

	}
	function updateSituation(){
		//make remote call to database to retrive all situations for current company and selected pests

		objList = document.frmRmt.NameList;
		if (objList.length == 0){
			document.frmRmt.SituationList.length = 0;
			//showSitCounts();
		}else{
			var lstSelectedPests = '';
			var inValue  = '';
			if (objList.length > 0){
				for (ii=0;ii<objList.length;ii++){
					lstSelectedPests += objList.options[ii].value + ',';
					//objSelected.options[ii].selected = true;
				}
			}
			//lstSelectedPests = lstSelectedPests.substr(0,lstSelectedPests.length - 1);
            //alert(lstSelectedPests);

		    if (document.forms['frmRmt'].rbLookWhat[3].checked){
			    //grouped by common name
    			lstSelectedPests = objList.options[objList.selectedIndex].text;
      			jsrsExecute("/webservices/rmtServerPest_rs.asp", showSituations, "getRmt", Array(lstSelectedPests, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "sits4GroupPest"));
          	}else{
    			lstSelectedPests = objList.options[objList.selectedIndex].value;
                //alert(lstSelectedPests);
      			jsrsExecute("/webservices/rmtServerPest_rs.asp", showSituations, "getRmt", Array(lstSelectedPests, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "sits4Pest"));
            }
        }
	}
	function showSituations(returnstring){
		//display returned situations
		//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();
        //document.getElementById("divBanner_42").innerText = "a";
        //window.status = lstSelectedPests;
    /*banner test
		lstSelectedPests = objList.options[objList.selectedIndex].text;

        window.status = lstSelectedPests;
        window.status = sourceDB;
        if (lstSelectedPests.indexOf('Bollworm') > -1)
        {
		    jsrsExecute("/webservices/rmtServerPest_rs.asp", showBanners, "getRmt", Array(lstSelectedPests, DisplayLength, sourceDB, companyID, MasterTypeID, iSelType, "banner4pest"));
        }
        //window.status = "42a";
    */
	}
	function showBanners(returnstring){
		//display returned situations
		prompt('ok',returnstring);

        document.getElementById("divBanner_42").innerText = returnstring;
        //window.status = "42b";
	}
	function pdaShowLong(objList){
		//display long names
		var sText;
		if (typeof objList == "object"){
			sText = objList.options[objList.selectedIndex].text;
		}else{
			sText = "";
		}
		document.spShowLong.innerHTML = sText;
	}
	function frmPdaSubmit(){
		//submit searchString
		document.frmRmt.PestID.value = 0;			//reset pestid 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
		}

		document.frmRmt.submit();

	}	

	function showHelp() {
	    if (document.getElementById("trhelp")!= null)
	        document.getElementById("trhelp").style.display = document.getElementById("trhelp").style.display == "" ? "none" : "";

	}

	function startAgain() {
	    document.frmRmt.reset();
	    document.frmRmt.SituationList.options.length = 0;
	    document.frmRmt.NameList.options.length = 0;
	}
