//  <script type="text/javascript">
//  These scripts are for ldcomsearch.php
//  Derived from search3.js
//

	  // see comment in js_FormLoad
		var optNewestYearCIdx;
    var optHighPriceCIdx;
    var optHighMilesCIdx;
    

    var ChangeLock=false;   // used to prevent a mated change event from
                            // executing it's change event in response to
                            // the first change event changing
                            // it's mated value.
    function js_FormLoad()
    {       
    	// this set of variables maintain the "current" selected index of their
    	// respective options.  If a change event occurs and the new values
    	// are invalid, the "current" value is restored.
	    	if(showYears) optNewestYearCIdx=document.forms['ldcomsearch'].newestYear.selectedIndex;
	    	if(showPrice) optHighPriceCIdx=document.forms['ldcomsearch'].highPrice.selectedIndex;
	    	if(showMiles) optHighMilesCIdx=document.forms['ldcomsearch'].highMiles.selectedIndex;		  
        // reset scroll bars from last window
        document.body.scrollLeft=document.forms['ldcomsearch'].windowScrollX.value;
        document.body.scrollTop=document.forms['ldcomsearch'].windowScrollY.value;
		} //end function
		
    
    function js_YearChanged(WhoChanged)
    {
			// verifies that the newest year is always greater than or equal to
			// the earliest year.
			
			// skip out if this change was triggered by the other yearchange event
      if (ChangeLock) {return;}
     
			var newidx=-1;	// if a change is necessary, this will be the new index
											// but, if the change is valid, the value of -1 will
											// trigger the form submit to update the search
											
      // determine current value for earliest and newest Year
      var optNewestYear=document.forms['ldcomsearch'].newestYear;
      var optEarliestYear=document.forms['ldcomsearch'].earliestYear;
      if (optEarliestYear.value == "")
			{	if(optNewestYear.value == "")
      	{ // just reset text of newestyear to empty field
      		optNewestYear.options[0].text="";
      	}
      	// else any other value is ok
      }
      else	// earliest year has a value
      {	// first check if newestyear is "", if so just change it's text
				if (optNewestYear.value == "")
				{ optNewestYear.options[0].text="And Newer";
		    }
				else
				{	//both have values, make sure high is greater than low
        	var earlyyear = parseInt(optEarliestYear.value);
        	var newyear=parseInt(optNewestYear.value);
        	var newyearprev=optNewestYear.options[optNewestYearCIdx].value;
        	if (newyearprev == "") 
					{	newyearprev="0"; }  // catch the default entry
        	newyearprev=parseInt(newyearprev);

        	if (newyear < earlyyear)
        	{	        		
						// if previous high was the default restore it
						if(optNewestYearCIdx == 0)
						{	newidx=0;
							optNewestYear.options[0].text="And Newer";
						}
						else
						{	//now check if previous value is still valid
							if (newyearprev >= earlyyear)
		        	{	// prev value is still valid, so restore it
		        	  newidx=optNewestYearCIdx;
							}
							else
							{ // change newyear to same as earlyyear
			          newidx=optEarliestYear.selectedIndex;
			        }
		        }
		      	ChangeLock=true;
						optNewestYear.selectedIndex=newidx;
			    	ChangeLock=false;
		      }
		    }
      }
      //reset "currentindex" for newest year
      optNewestYearCIdx=optNewestYear.selectedIndex;
      if(newidx==-1)
      {	//submit
      	js_RefineSearch(WhoChanged);
      }
    } // end function
    

    function js_PriceChanged(WhoChanged)
    {
     // verify that the value for highest price is > lowest price.
     // Note that the two price option arrays have the same number
     // of values, but the actual values for a given index are different
			     
      // skip out if this is a change triggered by the other Price change event
      if (ChangeLock) {return;}

			var newidx=-1;	// if a change is necessary, this will be the new index
											// but, if the change is valid, the value of -1 will
											// trigger the form submit to update the search
	
      // determine current value for lowPrice
      var optHighPrice=document.forms['ldcomsearch'].highPrice;
      var optLowPrice=document.forms['ldcomsearch'].lowPrice;
      if (optLowPrice.value == "")
			{ if (optHighPrice.value == "")
      	{	// reset text in case it had been overwritten(see below)
					optHighPrice.options[0].text="And Up";
     		}
     		// else any other price is ok
      }
      else	//low price has a value
			{	//if high price is empty, just change it's text
				if (optHighPrice.value=="")
				{	optHighPrice.options[0].text="And Up";
      	}
      	else
				{	//both have values, make sure high is greater than low  
	        var lprice = parseInt(optLowPrice.value);
	        var hprice=parseInt(optHighPrice.value);
	        var hpriceprev=optHighPrice.options[optHighPriceCIdx].value;
	        // change "" to 0 for prev value
	        if (hpriceprev == "") { hpriceprev="0"; }
					hpriceprev=parseInt(hpriceprev);
	
	        if (hprice <= lprice )
	        {	
	          if (optHighPriceCIdx == 0)
	          {	newidx=0;
	          	optHighPrice.options[0].text="And Up";
	          }
	          else
	          {	// use prev value if still valid
							if (lprice < hpriceprev)
							{	newidx=optHighPriceCIdx;
							}
							else
							{ // change highPrice.value to value at the same index as lowPrice
		          	newidx=optLowPrice.selectedIndex;
		          }
		        }
						ChangeLock=true;
	          optHighPrice.selectedIndex=newidx;
	          ChangeLock=false;
	        }
        }
      }
      optHighPriceCIdx=optHighPrice.selectedIndex;
      if(newidx==-1)
      {	//submit
      	js_RefineSearch(WhoChanged);
      }      
    } // end function


    function js_MilesChanged(WhoChanged)
    {
     // verify that the value for highest miles is > lowest miles.
     // Note that the two mileage option arrays have the same number
     // of entries, but the values for a given index are different
			
      // skip out if this is a change triggered by the other Miles change event
      if (ChangeLock) {return;}

			var newidx=-1;	// if a change is necessary, this will be the new index
											// but, if the change is valid, the value of -1 will
											// trigger the form submit to update the search
	
      // determine current values
      var optHighMiles=document.forms['ldcomsearch'].highMiles;
			var optLowMiles=document.forms['ldcomsearch'].lowMiles;
      if (optLowMiles.value == "")
			{	if(optHighMiles.value == "")
      	{	// Reset highmiles text in case it had been overwritten(see below)
      		optHighMiles.options[0].text="";
      	}
      	// else any setting for highmiles is ok
      }
      else	// lowmiles has a value
      {	//first check for highmiles empty, if so just change its text to "And Up"
      	if (optHighMiles.value == "")
				{
      		optHighMiles.options[0].text="And Up";
     		}
      	else	
				{	// both low and high have values, make sure high is greater than low
					var lmiles = parseInt(optLowMiles.value);
	        var hmiles=parseInt(optHighMiles.value);
	        var hmilesprev=optHighMiles.options[optHighMilesCIdx].value;
	        if(hmilesprev == "") {hmilesprev="0";}
	        hmilesprev=parseInt(hmilesprev);
	        if (hmiles <= lmiles)
	        {	
	        	if (optHighMilesCIdx == 0)
	        	{	newidx=0;
	        		optHighMiles.options[0].text="And Up";
	        	}
						else
						{	if(lmiles < hmilesprev)	// use prev value if still valid
							{	newidx=optHighMilesCIdx;
							} 
							else 
							{ // change highPrice value to value at the same index as lowPrice
		          	newidx=optLowMiles.selectedIndex;
		          }
		        }
	          ChangeLock=true;
          	optHighMiles.selectedIndex=newidx;
          	ChangeLock=false;
	        }
	      }
        optHighMilesCIdx=optHighMiles.selectedIndex;
        if(newidx==-1)
        {	//submit
        	js_RefineSearch(WhoChanged);
        }
      }
    } //end function
  
  	function js_RadioChanged(radiobutton)
  	{	// set $type
  		document.forms['ldcomsearch'].type.value=radiobutton.value;
  		// and submit with all parameters left as is
  		var optmake=document.forms['ldcomsearch'].body;
  		js_RefineSearch(null);
 		} // end function
  
  	function js_SearchReset()
  	{	// tell browse3 to "RESET"
      with (document.forms['ldcomsearch'])
      {
        postList.value="RESET";
        windowScrollX.value=document.body.scrollLeft;
        windowScrollY.value=document.body.scrollTop;
        js_Reload();
      }
 		} // end function
  
    function js_RefineSearch(WhoChanged)
		{ with (document.forms['ldcomsearch'])
		  { 
        postList.value=(WhoChanged != null) ? WhoChanged.name : 'None';
        windowScrollX.value=document.body.scrollLeft;
        windowScrollY.value=document.body.scrollTop;
        js_Reload();
      }
    }

    function js_ExpandSearch(buttoncode)
		{ with (document.forms['ldcomsearch'])
		  { seb.value=buttoncode;
        windowScrollX.value=document.body.scrollLeft;
        windowScrollY.value=document.body.scrollTop;
        js_Reload();
      }
    }
    
    function js_Reload()
    { document.getElementById("TblSearch1").border = 0;
      document.getElementById("TblSearch2").style.display = "none";
      document.getElementById("TblSearchBusy").style.display = "inline";
      document.forms['ldcomsearch'].submit();
    }
// </script>

