﻿function loadStates(choice)
{
    new Ajax.Updater("state" + choice, "/common/ajax/getStates.aspx",
        { 
            parameters : "districtchoice=" + choice, 
            onFailure : function(resp)
            { 
                alert("Oops, there's been an error."); 
            },
            onComplete: function(resp)
            {
                document.getElementById('state' + choice).innerHTML += " <input type='button' style=\"margin-left: 5px; height: 20px\" class=\"txt\" value='Remove' onclick=\"loadAdd('" + choice + "');\" />";
            }
        }
    );
}

function loadDistricts(obj, choice)
{
    var value = obj.value;
    var parameterString = "districtchoice=" + choice + "&state=" + value;

    new Ajax.Updater("district" + choice, "/common/ajax/getDistricts.aspx",
        {
            parameters: parameterString,
            onFailure: function(resp) 
            {
                alert("Oops, there's been an error.");
            },
            onComplete: function(resp) 
            {
                //document.getElementById('district' + choice).innerHTML += " &nbsp; <input type='button' style=\"margin-left: 5px; height: 20px\" class=\"txt\" value='Clear' onclick=\"loadAdd('" + choice + "');\" />";
            }
        }
    );
}

function loadAdd(choice)
{
    document.getElementById('state' + choice).innerHTML = "<input style=\"margin-left: 10px; height: 20px\" class=\"txt\" type=\"button\" value=\"Add\" onclick=\"loadStates('" + choice + "');\" />";
    document.getElementById('state' + choice + 'control').innerHTML = "&nbsp;";
    document.getElementById('district' + choice).innerHTML = "&nbsp;";
    document.getElementById('district' + choice + 'control').innerHTML = "&nbsp;";
}

function finalizeChoice(obj, choice)
{
    var value = obj.value;
    var district = obj.options[obj.selectedIndex].innerHTML;

    var finalChoice = "<input type='hidden' name='distchoice" + choice + "' value='" + value + "' />" +
        "<input type='hidden' name='distname" + choice + "' value='" + district + "' />" +
        district;
        //" &nbsp; <input type='button' value='Reset' class=\"txt\" style=\"margin-left: 10px; height: 20px\" onclick=\"loadAdd('" + choice + "');\" />";
    
    document.getElementById('district' + choice).innerHTML = finalChoice;
}
