// Create array of states in the United States
var StateNamesUS = new Array();
var StateCodesUS = new Array();

function addStateUS(name, code)
{
    StateNamesUS[StateNamesUS.length++] = name;
    StateCodesUS[StateCodesUS.length++] = code;
}

addStateUS("Alaska", "AK");
addStateUS("Alabama", "AL");
addStateUS("Arkansas", "AR");
addStateUS("Arizona", "AZ");
addStateUS("California", "CA");
addStateUS("Colorado", "CO");
addStateUS("Connecticut", "CT");
addStateUS("District of Columbia", "DC");
addStateUS("Delaware", "DE");
addStateUS("Florida", "FL");
addStateUS("Georgia", "GA");
addStateUS("Hawaii", "HI");
addStateUS("Iowa", "IA");
addStateUS("Idaho", "ID");
addStateUS("Illinois", "IL");
addStateUS("Indiana", "IN");
addStateUS("Kansas", "KS");
addStateUS("Kentucky", "KA");
addStateUS("Louisiana", "LA");
addStateUS("Massachusetts", "MA");
addStateUS("Maryland", "MD");
addStateUS("Maine", "ME");
addStateUS("Michigan", "MI");
addStateUS("Minnesota", "MN");
addStateUS("Mississippi", "MS");
addStateUS("Missouri", "MO");
addStateUS("Montana", "MT");
addStateUS("North Carolina", "NC");
addStateUS("North Dakota", "ND");
addStateUS("Nebraska", "NE");
addStateUS("New Hampshire", "NH");
addStateUS("New Jersey", "NJ");
addStateUS("New Mexico", "NM");
addStateUS("Nevada", "NV");
addStateUS("New York", "NY");
addStateUS("Ohio", "OH");
addStateUS("Oklahoma", "OK");
addStateUS("Oregon", "OR");
addStateUS("Pennsylvania", "PA");
addStateUS("Puerto Rico", "PR");
addStateUS("Rhode Island", "RI");
addStateUS("South Carolina", "SC");
addStateUS("South Dakota", "SD");
addStateUS("Tennessee", "TN");
addStateUS("Texas", "TX");
addStateUS("Utah", "UT");
addStateUS("Virginia", "VA");
addStateUS("Vermont", "VT");
addStateUS("Washington", "WA");
addStateUS("Wisconsin", "WI");
addStateUS("West Virginia", "WV");
addStateUS("Wyoming", "WY");


// Create array of Canadian provinces
var StateNamesCA = new Array();
var StateCodesCA = new Array();

function addStateCA(name, code)
{
    StateNamesCA[StateNamesCA.length++] = name;
    StateCodesCA[StateCodesCA.length++] = code;
}

addStateCA("Alberta", "AB");
addStateCA("British Columbia", "BC");
addStateCA("Manitoba", "MB");
addStateCA("New Brunswick", "NB");
addStateCA("Newfoundland and Labrador", "NL");
addStateCA("Northwest Territories", "NT");
addStateCA("Nova Scotia", "NS");
addStateCA("Nunavut", "NU");
addStateCA("Ontario", "ON");
addStateCA("Prince Edward Island", "PE");
addStateCA("Quebec", "QC");
addStateCA("Saskatchewan", "SK");
addStateCA("Yukon", "YT");


function addSelectOption(select, label, value)
{
    select[select.length++] = new Option(label, value);
}

function addSelectOptions(select, labels, values)
{
    for (var i = 0; i < labels.length && values.length; i++)
        addSelectOption(select, labels[i], values[i]);
}

// When the country changes, update the state dropdown box to load the
// countries states (just US or Canada for now) and update the "other state"
// selection box accordingly
function countryChanged(state_select, state_other, country_select)
{
    // Clear the state select box and load the new options
    state_select.length = 0;

    // Add US or Canadian provinces if that country was selected
    var country = country_select[country_select.selectedIndex].value;
    if (country == "United States")
        addSelectOptions(state_select, StateNamesUS, StateCodesUS);
    else if (country == "Canada")
        addSelectOptions(state_select, StateNamesCA, StateCodesCA);

    // Add the "Other" option last
    addSelectOption(state_select, "Other:", "Other");

    // Select the top option and trigger the changed function
    state_select.selectedIndex = 0;
    stateChanged(state_select, state_other);
}


function setSelectedCountry(country_name,locale)
{
	if(locale == "office")
		cs = document.getElementById('office_country_select');
	else
		cs = document.getElementById('home_country_select');
	for (var i=0; i < cs.length; i++)
		if (cs.options[i].value == country_name)
		{
			cs.selectedIndex = i;
			break;
		}
}

function setSelectedState(state_name,locale)
{
	if(locale == "office")
	{
		ss = document.getElementById('office_state_select');
		sother = document.getElementById('office_state_other');
	}
	else
	{
		ss = document.getElementById('home_state_select');
		sother = document.getElementById('home_state_other');
	}
	var good = "false";
	for (var i=0; i < ss.length; i++)
		if (ss.options[i].value == state_name)
		{
			ss.selectedIndex = i;
			sother.value = "";
			good = "true";
			break;
		}
	if(good!="true")
	{
		sother.value = state_name;
		ss.selectedIndex = ss.length - 1;
	}
}


// When the state dropdown changes, if the "Other" option isn't selected,
// delete the contents of the other text box
function stateChanged(state_select, state_other)
{
    // Clear the text box if the "Other" option isn't selected
    // NOTE: This assumes the last entry is always "Other"
    if (state_select.selectedIndex < state_select.length - 1)
        state_other.value = "";
}

// When typing in the other text box, automatically select "Other" in the
// state dropdown selection.
function stateOtherChanged(state_select, state_other)
{
    // NOTE: This assumes the last entry is always "Other"
    if (state_other.value != "")
        state_select.selectedIndex = state_select.length - 1;
}

