﻿function ZipEntered(formName) {
    var form = document.getElementById(formName);
    
    if (form.RefinedZipList.value != '') {
        form.frmFips.value='';
        form.frmState.value='';
    }
}

function StateSelected(formName, sel) {
    var form = $get(formName);    
    var selectedState = sel.value;
    
    form.RefinedZipList.value='';
    
    ClearCounties(formName);
    RefreshCounties(formName, selectedState);
}

function ClearCounties(formName) {
    $get(formName).frmFips.options.length = 0;
}

// This function uses AJAX to submit an HTTP POST to the counties script.
function RefreshCounties(formName, state) { 
    if (state != null && state != "") ListsService.GetCounties(state, counties_Completed, counties_Error, formName);
} 

function PropSearch_OnSubmit(formName) {    
    var form = document.getElementById(formName); 
    var sel = form.frmFips;
    
    sel.disabled = false;
    
    return true;
}
				
function states_Completed(result, args) {   
    var form;
    var fipsCombo;
    var stateCombo;
    var states;
    
    states = eval(result);	
    form = (args  != null) ? form = $get(args) : $get("PropSearchTop");             
    fipsCombo = form.frmFips;    
    stateCombo = form.frmState;  
    
    stateCombo.options.length = 0;
    stateCombo.options.add(new Option("State", ""));
    for (var i = 0; i < states.length; i++) {
        var state = states[i];
        stateCombo.options.add(new Option(state.FullName, state.Abbreviation));
        if (state.Abbreviation == selectedState) stateCombo.selectedIndex = i + 1;
    }            
    if (form.id == "PropSearchTop") {
	    $get("topSearchPanel").style.display = "block";
	    $get("topSearchLoading").style.display = "none";   
	    RefreshSummaryStats();
    	statesInitialized = true;
    }
}
function states_Error(result, args) {}

function counties_Completed(result, args) {     
    var form;
    var fipsCombo;
    var stateCombo;
    var counties;
    
    counties = eval(result);	
    form = (args  != null) ? form = $get(args) : $get("PropSearchTop");             
    fipsCombo = form.frmFips;    
    stateCombo = form.frmState;  
    
    fipsCombo.options.length = 0;
    fipsCombo.options.add(new Option("County", ""));
    for (var i = 0; i < counties.length; i++) {
        var county = counties[i];
        var option = new Option(county.Name, county.FIPS);
        if (!county.HasData) {
            option.disabled = true;
            option.style.color = clrDarkGray;
        }
        fipsCombo.options.add(option);
        if (county.FIPS == selectedFips) fipsCombo.selectedIndex = i + 1;
    }  
    
    if (stateCombo.value == "DC") {
        fipsCombo.value = counties[0].FIPS;
        fipsCombo.disabled = true;
    } else {
        fipsCombo.disabled = false;
    }   
    if (form.id == "PropSearchTop") {
	    countiesInitialized = true;    	
    }
}
function counties_Error(result, args) {}

function RefreshSummaryStats() 
{    
    var fipsCombo;
    var stateCombo;
    var state, fips;
    
    fipsCombo = $get("PropSearchTop").frmFips;    
    stateCombo = $get("PropSearchTop").frmState;  
    
    state = (!statesInitialized) ? selectedState : stateCombo.value;
    fips = (!countiesInitialized) ? selectedFips : fipsCombo.value;
    
   // ListsService.FetchStatsSummary(state, fips, 
   //         stats_Complete, stats_Error, null);
}
function stats_Complete(result, args)
{ 
    var fipsCombo;
    var stateCombo;
    var div = $get("topStatsDiv");
    var type, pre, auc, reo, lbl;                    
    var summary = eval(result);
    
    fipsCombo = $get("PropSearchTop").frmFips;    
    stateCombo = $get("PropSearchTop").frmState; 
    
    type = summary.Type;
    pre = summary.Preforeclosures;
    auc = summary.Auctions;
    reo = summary.REOs;
    
    switch (type) 
    {
        case "Nationwide": lbl = "Nationwide Stats"; break;
        case "Statewide":
            lbl = stateCombo[stateCombo.selectedIndex].text + " Stats";
            break;
        case "County":
            lbl = fipsCombo[fipsCombo.selectedIndex].text + " County, " + stateCombo.value;
            break;
    }
    
    pre = (pre == null) ? "Preforeclosures N/A" : String.format("<strong>{0}</strong> Preforeclosures", pre);        
    auc = (auc == null) ? "Auctions N/A" : String.format("<strong>{0}</strong> Auctions", auc);        
    reo = (reo == null) ? "REOs N/A" : String.format("<strong>{0}</strong> REOs", reo);
    
    div.innerHTML = 
        String.format("{0}: &nbsp;{1}, &nbsp;{2}, &nbsp;{3}", lbl, pre, auc, reo);
} 

function stats_Error(result, args) {}			