$(document).ready(function ()
    {
        $("#country").change(function()
            {
                if ($("#country")[0].selectedIndex != 0)
                {
                    getStates($("#country")[0].options[$("#country")[0].selectedIndex].id);
                }
                else
                {
                    $("#state")[0].selectedIndex = 0;
                    $("#state")[0].disabled = true;
                    
                    $("#city")[0].selectedIndex = 0;
                    $("#city")[0].disabled = true;
                }
                $("#city").change();
            }
        );
        
        $("#state").change(function()
            {
                if ($("#state")[0].selectedIndex != 0)
                {
                    getCities($("#country")[0].options[$("#country")[0].selectedIndex].id, $("#state")[0].value);
                }
                else
                {
                    $("#city")[0].selectedIndex = 0;
                    $("#city")[0].disabled = true;
                }
                $("#city").change();
            }
        );
        
        $("#city").change(function()
            {
                if ($("#city")[0].value != "")
                {
                    getDealers($("#country")[0].options[$("#country")[0].selectedIndex].id, $("#state")[0].value, $("#city")[0].value);
                }
                else
                {
                    $("#results")[0].innerHTML = "";
                }
            }
        );
        
        $("#country").change();
    }
);

function getDealers(country, state, city)
{
    var str = "country=" + country + "&city=" + city + "&state=" + state + "&paragraphID=" + $("#dealerSearchPID")[0].value;
	$.ajax({
       type: "POST",
       url: "/WebService/dealerfinder.asmx/GetDealers",
       data: str,
       success: function(data)
       {
            res = data.documentElement;
			var str = "";
			for (var i = 0; i < res.childNodes.length; i++)
				str += res.childNodes[i].nodeValue;
			
			$("#results").html(str);
       }
     });
}

function getStates(country)
{
    var str = "country=" + country;
	$.ajax({
       type: "POST",
       url: "/WebService/dealerfinder.asmx/GetStates",
       data: str,
       success: function(data)
       {
            res = data.documentElement;
			ClearStorage();
			
			var str = "";
			for (var i = 0; i < res.childNodes.length; i++)
				str += res.childNodes[i].nodeValue;
			
			eval(str);
			updateStates();
       }
     });
}

function getCities(country, state)
{
	var str = "country=" + country + "&state=";
	if (state != null)
	    str = str + state;

	$.ajax({
       type: "POST",
       url: "/WebService/dealerfinder.asmx/GetCities",
       data: str,
       success: function(data)
       {
            res = data.documentElement;
			ClearStorage();
			
			var str = "";
			for (var i = 0; i < res.childNodes.length; i++)
				str += res.childNodes[i].nodeValue;
			
			eval(str);
			updateCities();
       }
     });
}

function updateStates()
{
    resetStates();
    
    if (RA.length == 0 || RA.length == 1 && RA[0] == "")
    {
        $("#state")[0].disabled = true;
        
        // Then update the cities
        $("#city").disabled = false;
        getCities($("#country")[0].options[$("#country")[0].selectedIndex].id, null);
        return;
    }
    
    resetCities();
    
    $("#state")[0].disabled = false;
    
    var i = 0;
    for (i = 0; i < RA.length; i++)
    {
        var current = RA[i];
        var newOption = document.createElement("option");
        newOption.id = i;
        newOption.title = current;
        newOption.value = current;
        newOption.innerHTML = current;
        document.getElementById("state").appendChild(newOption);
    }
        
    if (RA.length == 1)
    {   
        // select singular option
        $("#state option")[1].selected = true;
        getCities($("#country")[0].options[$("#country")[0].selectedIndex].id, $("#state option")[1].value);
    }
}

function updateCities()
{
    resetCities();
    
    if (RA.length == 0)
    {
        return;
    }
    
    $("#city")[0].disabled = false;
    
    var i = 0;
    for (i = 0; i < RA.length; i++)
    {
        var current = RA[i];
        var newOption = document.createElement("option");
        newOption.id = i;
        newOption.title = current;
        newOption.value = current;
        newOption.innerHTML = current;
        document.getElementById("city").appendChild(newOption);
    }
}

function resetStates()
{
    $("#state")[0].disabled = true;
    
    var first = $("#state option")[0];
    $("#state")[0].innerHTML = "";
    $("#state")[0].appendChild(first);
}

function resetCities()
{
    $("#city")[0].disabled = true;
    
    var first = $("#city option")[0];
    $("#city")[0].innerHTML = "";
    $("#city")[0].appendChild(first);
}



