function showAdvancedSearch()
{
    if(!javaLoaded){return;}

    $get(advClientId + "_hiddenMake").value = "";
    keepAdvancedSearchVisible();
    doPostBackAsync(adSearchDummyId);
}

function showAdvancedSearchWithDefaults(makeID, typeValue)
{
    if(!javaLoaded){return;}

    //set make
    $get(advClientId + "_hiddenMake").value = makeID;
    
    //set vehicle type
    $get(advClientId + "_hiddenType").value = typeValue;

    //show popup
    keepAdvancedSearchVisible();
   
    //postback
    doPostBackAsync(adSearchDummyId);
}

function RefreshScreen(args, context)
{
    alert('RefreshScreen');
}

function keepAdvancedSearchVisible()
{
   var el;
   el = $get('advancedSearch');
   if (null != el  && el)
   {
      removeCssClass(el, 'hide');
      hideDropDownVarList();
   }
   
   el = $get('alertBackground');
   if (null != el  && el)
   {
      removeCssClass(el, 'hide');
   }
   fixAdvancedPNG();
}

function AdvSearchCheckKey(evt)
{
    // evt is a Sys.UI.DomEvent object
 
    if (mapKeyToAction(evt))
    { 
        performAdvancedSearch();
        setFixValidation();
        return false;
    }
}

var AdvancedSS = null;
function fixAdvancedPNG()
{
  if ($get('hiddenIE6'))
  {
    if(null == AdvancedSS)
    {
        AdvancedSS = new SuperSleight();
    }
    
    AdvancedSS.limitTo('advancedSearch');
    AdvancedSS.run();
  }
}


function hideAdvancedSearch()
{
  var el;
   el = $get('advancedSearch');
   if (null != el  && el)
   {
      addCssClass(el, 'hide');
   }
   el = $get('alertBackground');
   if (null != el  && el)
   {
      addCssClass(el, 'hide');
   }
   showDropDownVarList();
}

function BeginRequest(sender, args)   
{  
    showDIV('advSearchProgress');
}   
     
function EndRequest(sender, args)   
{  
    hideDIV('advSearchProgress');
    $addHandler($get("advancedInnerContainer"), 'keypress', AdvSearchCheckKey);
} 

function performAdvancedSearch()
{
    if(validateAdvancedSearch())
    {
        // We are not using viewstate with this control. Save values that cannot already be retrieved
        // as part of the form in the code behind.
        
        // Make Description 1-3
        if(0 < $get(advClientId + "_ddlMake1").selectedIndex)
            $get(advClientId + "_hidMakeDesc1").value = $get(advClientId + "_ddlMake1").options[$get(advClientId + "_ddlMake1").selectedIndex].text
        if(0 < $get(advClientId + "_ddlMake2").selectedIndex)
            $get(advClientId + "_hidMakeDesc2").value = $get(advClientId + "_ddlMake2").options[$get(advClientId + "_ddlMake2").selectedIndex].text
        if(0 < $get(advClientId + "_ddlMake3").selectedIndex)
            $get(advClientId + "_hidMakeDesc3").value = $get(advClientId + "_ddlMake3").options[$get(advClientId + "_ddlMake3").selectedIndex].text
        // Style
        if(0 < $get(advClientId + "_ddlStyle").selectedIndex)
            $get(advClientId + "_hidStyleDesc").value = $get(advClientId + "_ddlStyle").options[$get(advClientId + "_ddlStyle").selectedIndex].text
        // Color
        if(0 < $get(advClientId + "_ddlExtColor").selectedIndex)
            $get(advClientId + "_hidColorDesc").value = $get(advClientId + "_ddlExtColor").options[$get(advClientId + "_ddlExtColor").selectedIndex].text
        
        hideAdvancedSearch();
        fnShowSearchingProgress();
        $get(advClientId + "_btnAdSearch").click();
    }
    
    return false;
}

function validateAdvancedSearch()
{
    valid = true;
    var elm;
    var elmFocus = null;
    
    // Check the zip code value.
    addCssClass($get("asreqZip"), "hide");
    addCssClass($get("aserrZip"), "hide");
    elm = $get(advClientId + "_txtZipAdv");
    if((0 == elm.value.length) || ("Enter Zip Code") == elm.value)
    {
        valid = false;
        removeCssClass($get("asreqZip"), "hide");
        elmFocus = elm;
    }
    else
    {
        if (!g_regexZip.test(elm.value)) 
        {
	        removeCssClass($get("aserrZip"), "hide");
	        valid = false;
	        elmFocus = elm;
        }
    }
    
    if (!$get(advClientId + "_checkNew").checked && !$get(advClientId + "_checkUsed").checked && !$get(advClientId + "_checkCertified").checked)
    {
        removeCssClass($get("aserrCat"), "hide");
        valid = false;
        elmFocus = $get(advClientId + "_checkNew");
    }
    
    if(null != elmFocus)
    { elmFocus.focus(); }
    
    return valid;
}

function PopulateMaxYear()
{
    // Save the selected max year before repopulating.
    selectedMax = $get(advClientId + "_ddlMaxYear").value;

    // Mark the drop down as "Loading".    
    SetDDLInnerHtml(advClientId + "_ddlMaxYear", "<option>[Loading...]</option>");
    $get(advClientId + "_ddlMaxYear").disabled = true;
    
    Reynolds.CL.Web.WebServices.VehicleFunctions.GetMaxYearDDList(
        $get(advClientId + "_ddlMinYear").value, WSKey,
        GetMaxYearsSuccess, 
        GetMaxYearsFailure, selectedMax);
} 

// Trims drop down list successfully received from web service.
function GetMaxYearsSuccess(result, context)
{
    SetDDLInnerHtml(advClientId + "_ddlMaxYear", result);
    $get(advClientId + "_ddlMaxYear").disabled = false;
    // Restore the max selection if it exists.
    fnSetDDLByValue(advClientId + "_ddlMaxYear", context);
} 

// Failure trying to get max years drop down list from web service.
function GetMaxYearsFailure(err)
{
    alert(err.get_message());
    
    $get(advClientId + "_ddlMaxYear").innerHTML = "";
    $get(advClientId + "_ddlMaxYear").disabled = false;
}

function PopulateMaxPrice()
{
    // Save the selected max Price before repopulating.
    selectedMax = $get(advClientId + "_ddlMaxPrice").value;

    // Mark the drop down as "Loading".    
    SetDDLInnerHtml(advClientId + "_ddlMaxPrice", "<option>[Loading...]</option>");
    $get(advClientId + "_ddlMaxPrice").disabled = true;
    
    Reynolds.CL.Web.WebServices.VehicleFunctions.GetMaxPriceDDList(
        $get(advClientId + "_ddlMinPrice").value, WSKey,
        GetMaxPricesSuccess, 
        GetMaxPricesFailure, selectedMax);
} 

// Trims drop down list successfully received from web service.
function GetMaxPricesSuccess(result, context)
{
    SetDDLInnerHtml(advClientId + "_ddlMaxPrice", result);
    $get(advClientId + "_ddlMaxPrice").disabled = false;
    // Restore the max selection if it exists.
    fnSetDDLByValue(advClientId + "_ddlMaxPrice", context);
} 

// Failure trying to get max price drop down list from web service.
function GetMaxPricesFailure(err)
{
    alert(err.get_message());
    
    $get(advClientId + "_ddlMaxPrice").innerHTML = "";
    $get(advClientId + "_ddlMaxPrice").disabled = false;
}

function PopulateModels1()
{
    PopulateModels(advClientId + "_ddlMake1", advClientId + "_ddlModel1");
}
function PopulateModels2()
{
    PopulateModels(advClientId + "_ddlMake2", advClientId + "_ddlModel2");
}
function PopulateModels3()
{
    PopulateModels(advClientId + "_ddlMake3", advClientId + "_ddlModel3");
}

function PopulateModels(makeListId, modelListId)
{
    // Mark the drop down as "Loading".    
    SetDDLInnerHtml(modelListId, "<option>[Loading...]</option>");
    $get(modelListId).disabled = true;

    if(0 < $get(makeListId).value.length)
    {
        Reynolds.CL.Web.WebServices.VehicleFunctions.GetModelDDList(
            $get(makeListId).value, WSKey,
            GetModelsSuccess, 
            GetModelsFailure, modelListId);
    }
    else
    {
        SetDDLInnerHtml(modelListId, "");
    }
} 

// Trims drop down list successfully received from web service.
function GetModelsSuccess(result, context)
{
    SetDDLInnerHtml(context, result);
    $get(context).disabled = false;
} 

// Failure trying to get max price drop down list from web service.
function GetModelsFailure(err, context)
{
    alert(err.get_message());
    
    $get(context).innerHTML = "";
}
