	/*
	Enable repopulation of some form fileds on the client, no need to reload the whole page.
	*/

	//Global vars
	var DisplayLength = 32;		/* Maximum number of records to return from Database? */
	var allOptArrays = new Array(DisplayLength); /* keep additional product details */
	var companyID = document.frmRmt.CompanyID.value;		/*current company id*/
	//alert(companyID);
	var selIndex = 0;			/*number of selected pests, used to enable or disable buttons*/
	var iSelCount = 0;			/*number of selected situations*/
	var iSelType = 0; 			/*Turf - ID of product type, used to narrow down number of returned pests*/
	var iMasterTypeID = 0; 			/*MasterTypeID*/
	if (companyID == 452828){
		iMasterTypeID = 4; /* turf only for GreenCast (Syngenta)*/
	}else{
		iMasterTypeID = 4; /* all for the others*/
	}
	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 sourceDB = 'pg';  /*holds current database*/
	var sGroupSituations = '';  /*holds individual situation ids that belong to a situation group*/
    var searchType='BYTYPE'


    function refresh_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;

		jsrsPOST = '';

	    var inValue  = document.frmRmt.slSituation.value;	/*current selected value, used for remote lookup*/
        //alert(inValue);
		jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBack, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, iMasterTypeID, iSelType, "groupSituationPest"));
    }

    function fCallBack( returnstring ){
		//parse returned string and populate slSituation options
		//alert(returnstring);
		//prompt('ok',returnstring);
		document.frmRmt.slPest.length = 0					/*reset current list*/
		var myTopArray = returnstring.split("%");

		//var myArray = returnstring.split(":");
		var myArray = myTopArray[0].split(":");
        sGroupSituations = myTopArray[1];
        //alert(sGroupSituations);
		//Include default Selection
		var myOpt = new Option
		myOpt.value = 0;
		myOpt.text  = "Select Pest...";
		document.frmRmt.slPest.options[0] = myOpt;

		//add retrieved options
		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.slPest.options[i+1] = myOpt;
			}
		}
		document.frmRmt.slPest.options[0].selected = true;
		checkButtons();
    }

	function checkButtons(){
		//enable or disable submit button for current situation

		if (document.frmRmt.slPest.value > 0){
			document.frmRmt.btnSubmit.disabled = false;
		}else{
			//not disablinh since html button is not in use anymore
			//document.frmRmt.btnSubmit.disabled = true;
		}

		/*
		if (document.frmRmt.slSituation.value > 0){
			if (document.layers)
			{
				document.layers["divSearch"].visibility = true;
			}
			else if (document.getElementById || document.all)
			{
				document.all["divSearch"].style.visibility = "visible";
			}
		}else{
			if (document.layers)
			{
				document.layers["divSearch"].visibility = false;
			}
			else if (document.getElementById || document.all)
			{
				document.all["divSearch"].style.visibility = "hidden";
			}
		}
		*/

	}

	function selAllOnSubmit(){
		//populate hidden PestID and SituationID fields before submiting the form.
		if (document.frmRmt.slPest.value > 0){
			objList = document.frmRmt.slPest;
			//document.frmRmt.PestID.value = objList.options[objList.selectedIndex].value;
			document.frmRmt.SituationList.value = objList.options[objList.selectedIndex].value;

            //situations for the group situation
            var sTmp =sGroupSituations.replace(/\:/g,',');
			document.frmRmt.SelectedList.value = sTmp.slice(0,sTmp.length -1);

    		//jsrsPOST = '';

    	    //var inValue  = document.frmRmt.slSituation.value;	/*current selected value, used for remote lookup*/
            //alert(inValue);
    		//jsrsExecute("/webservices/rmtServerPest_rs.asp", fCallBackGS, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, iMasterTypeID, iSelType, "getGroupSituations"));
            //
            
			document.frmRmt.submit();
		}else{
			alert("Please select Pest and Situation before submit!");
		}
	}


    function sel_productSearch() {
    
		if (document.frmRmt.lbCompProds.value > 0){
			objList = document.frmRmt.lbCompProds;
            var iDocID =  document.frmRmt.ProductDetDocID.value

            var sURL = "default.asp?V_DOC_ID=" + iDocID +"&function=GetProduct&PestGenie=N&MasterTypeID="
                + iMasterTypeID +"&CompanyID=" + companyID+"&ProductID=" +objList.value +"&Details=Y  "
            window.location =   sURL
		}else{
			alert("Please select product first");
		}
	}


    function sel_prodNameSearch() {
		if (document.frmRmt.lbProdNameSearch.value > 0){
			objList = document.frmRmt.lbProdNameSearch;
            var iDocID =  document.frmRmt.ProductDetDocID.value

            var sURL = "default.asp?V_DOC_ID=" + iDocID +"&function=GetProduct&PestGenie=N&MasterTypeID="
                + iMasterTypeID +"&CompanyID=" + companyID+"&ProductID=" +objList.value +"&Details=Y  "
            window.location =   sURL
		}else{
			alert("Please select product first");
		}
	}



    function fCallBackGS( returnstring ){
		//parse returned string and populate slSituation options
		//alert(returnstring);
		//prompt('ok',returnstring);
		document.frmRmt.slPest.length = 0					/*reset current list*/
		var myArray = returnstring.split(":");

		//Include default Selection
		var myOpt = new Option
		myOpt.value = 0;
		myOpt.text  = "Select Pest...";
		document.frmRmt.slPest.options[0] = myOpt;

		//add retrieved options
		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.slPest.options[i+1] = myOpt;
			}
		}
		document.frmRmt.slPest.options[0].selected = true;
		checkButtons();
    }

function do_getProdByName(){
    var sRequestor = ''
   	jsrsPOST = '';
    var textValue = document.frmRmt.txtfSeach.value
    if (textValue==''){
        alert('Please enter at least 1 characters for the product name')
        return false;
    }
    searchType='BYPRODNAME'
    jsrsExecute("webservices/rmtProductFinder.asp", fdo_getProducts, "getRmt", Array(textValue, DisplayLength, sourceDB, companyID, iMasterTypeID, 0, sRequestor, "getCompanyProducts"), false);
    //jsrsExecute("webservices/rmtServerPest_rs_Req.asp", fdo_getProducts, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, iMasterTypeID, iSelType, sRequestor, "products"),true);

}


var keyTimeout;
function auto_do_getProdByName(){
	//return true;
    if (keyTimeout){
     clearTimeout(keyTimeout);
    }
    keyTimeout= setTimeout('do_getProdByName()',50);
}




function do_getProducts(objList){
    var sRequestor = ''
        	jsrsPOST = '';
    if (objList.value == "0")
        return
    var inValue =      ''
    iSelType    =       objList.value
    jsrsExecute("webservices/rmtProductFinder.asp", fdo_getProducts, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, iMasterTypeID, iSelType, sRequestor, "getCompanyProducts"), false);
    //jsrsExecute("webservices/rmtServerPest_rs_Req.asp", fdo_getProducts, "getRmt", Array(inValue, DisplayLength, sourceDB, companyID, iMasterTypeID, iSelType, sRequestor, "products"),true);

}

 function fdo_getProducts( returnstring ){
		//parse returned string and populate slSituation options
		//prompt('ok',returnstring);
        var objList = null

        if (searchType == 'BYPRODNAME' )
            objList = document.frmRmt.lbProdNameSearch
        else
            objList = document.frmRmt.lbCompProds

		objList.length = 0					/*reset current list*/
		var myTopArray = returnstring.split("%");

		//var myArray = returnstring.split(":");
		var myArray = myTopArray[0].split(":");
        sGroupSituations = myTopArray[1];
        //alert(sGroupSituations);
		//Include default Selection
		var myOpt = new Option
		myOpt.value = 0;
		myOpt.text  = "Select Product...";
		objList.options[0] = myOpt;

		//add retrieved options
		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)
			{
			  objList.options[i+1] = myOpt;
			}
		}
		objList.options[0].selected = true;
		//checkButtons();
        searchType = 'BYTYPE'
    }



// Trap enter

function trapEnter(e){
      var key
      if (window.event) key = window.event.keyCode
      else key = e.which
      if ( key == 13){ // if keypress == Enter do not submit page
          refresh_list();
          bEnter = true;
          if (e.preventDefault){
            e.preventDefault();
            e.stopPropagation();
          }else
            return false
      }else{
        bEnter = false
        return true
      }
}

function addEvent(obj, evtType,fn, UseCapture){
  if (obj.addEventListener){
      obj.addEventListener( evtType,fn, UseCapture)
      return true
  }else if (obj.attachEvent){
      var r = obj.attachEvent ("on" +evtType, fn);
      return r
  }else{
    //alert('Cannot attach event handler')
    return false;
  }
}

function addKeyEvent(){
  var e = (document.addEventListener) ? 'keypress':'keydown';
  addEvent(document.frmRmt.txtfSeach,e,trapEnter,false)
}

addKeyEvent()