var usStates = Array();
var canStates = Array();


$(document).ready(function()
{
    usStates.push(new textValuePair('', 'Please select'));
    usStates.push(new textValuePair('AK', 'Alaska'));
    usStates.push(new textValuePair('AL', 'Alabama'));
    usStates.push(new textValuePair('AR', 'Arkansas'));
    usStates.push(new textValuePair('AZ', 'Arizona'));
    usStates.push(new textValuePair('CA', 'California'));
    usStates.push(new textValuePair('CO', 'Colorado'));
    usStates.push(new textValuePair('CT', 'Connecticut'));
    usStates.push(new textValuePair('DC', 'District of Columbia'));
    usStates.push(new textValuePair('DE', 'Delaware'));
    usStates.push(new textValuePair('FL', 'Florida'));
    usStates.push(new textValuePair('GA', 'Georgia'));
    usStates.push(new textValuePair('HI', 'Hawaii'));
    usStates.push(new textValuePair('IA', 'Iowa'));
    usStates.push(new textValuePair('ID', 'Idaho'));
    usStates.push(new textValuePair('IL', 'Illinois'));
    usStates.push(new textValuePair('IN', 'Indiana'));
    usStates.push(new textValuePair('KS', 'Kansas'));
    usStates.push(new textValuePair('KY', 'Kentucky'));
    usStates.push(new textValuePair('LA', 'Louisiana'));
    usStates.push(new textValuePair('MA', 'Massachusetts'));
    usStates.push(new textValuePair('MD', 'Maryland'));
    usStates.push(new textValuePair('ME', 'Maine'));
    usStates.push(new textValuePair('MI', 'Michigan'));
    usStates.push(new textValuePair('MN', 'Minnesota'));
    usStates.push(new textValuePair('MO', 'Missouri'));
    usStates.push(new textValuePair('MS', 'Mississippi'));
    usStates.push(new textValuePair('MT', 'Montana'));
    usStates.push(new textValuePair('NC', 'North Carolina'));
    usStates.push(new textValuePair('ND', 'North Dakota'));
    usStates.push(new textValuePair('NE', 'Nebraska'));
    usStates.push(new textValuePair('NH', 'New Hampshire'));
    usStates.push(new textValuePair('NJ', 'New Jersey'));
    usStates.push(new textValuePair('NM', 'New Mexico'));
    usStates.push(new textValuePair('NV', 'Nevada'));
    usStates.push(new textValuePair('NY', 'New York'));
    usStates.push(new textValuePair('OH', 'Ohio'));
    usStates.push(new textValuePair('OK', 'Oklahoma'));
    usStates.push(new textValuePair('OR', 'Oregon'));
    usStates.push(new textValuePair('PA', 'Pennsylvania'));
    usStates.push(new textValuePair('RI', 'Rhode Island'));
    usStates.push(new textValuePair('SC', 'South Carolina'));
    usStates.push(new textValuePair('SD', 'South Dakota'));
    usStates.push(new textValuePair('TN', 'Tennessee'));
    usStates.push(new textValuePair('TX', 'Texas'));
    usStates.push(new textValuePair('UT', 'Utah'));
    usStates.push(new textValuePair('VA', 'Virginia'));
    usStates.push(new textValuePair('VT', 'Vermont'));
    usStates.push(new textValuePair('WA', 'Washington'));
    usStates.push(new textValuePair('WI', 'Wisconsin'));
    usStates.push(new textValuePair('WV', 'West Virginia'));
    usStates.push(new textValuePair('WY', 'Wyoming'));

    canStates.push(new textValuePair('', 'Please select'));
    canStates.push(new textValuePair('AB', 'Alberta'));
    canStates.push(new textValuePair('BC', 'British Columbia'));
    canStates.push(new textValuePair('MB', 'Manitoba'));
    canStates.push(new textValuePair('NB', 'New Brunswick'));
    canStates.push(new textValuePair('NL', 'Newfoundland and Labrador'));
    canStates.push(new textValuePair('NS', 'Nova Scotia'));
    canStates.push(new textValuePair('NT', 'Northwest Territories'));
    canStates.push(new textValuePair('NU', 'Nunavut'));
    canStates.push(new textValuePair('ON', 'Ontario'));
    canStates.push(new textValuePair('PE', 'Prince Edward Island'));
    canStates.push(new textValuePair('QC', 'Quebec'));
    canStates.push(new textValuePair('SK', 'Saskatchewan'));
    canStates.push(new textValuePair('YT', 'Yukon'));

    if (document.getElementById('country'))
    {
        showHideStateFields(document.getElementById('country'));
    }
})


// Used to allow dropdowns for US/Canada state but free text box for all other countries
function showHideStateFields(countryDropDown)
{
	
	// Create the North American State drop down
	var stateDropDown =document.getElementById('cboNAState'); 
	if (stateDropDown == null)
	{
		stateDropDown = document.createElement('select');
		// Don't add a name to this control as it is only for input. It does not get posted to the form
		stateDropDown.id = 'cboNAState';

		// Check if this is a landing form, in that case the width is set automatically by the landing page style
		if ($('.regform_right').length == 0)
			stateDropDown.style.cssText = 'display:none;width:400px';
		else
			stateDropDown.style.cssText = 'display:none;';

		stateDropDown.onchange = function() { setState(this); };

		document.getElementById('UserState').parentNode.appendChild(stateDropDown);
	}
	
	
    var selectedCountry = countryDropDown.options[countryDropDown.selectedIndex].text;
    var selectedState = '';  
    if (stateDropDown.selectedIndex > -1)
        selectedState = stateDropDown.options[stateDropDown.selectedIndex].value;
        
    if (selectedCountry == 'Canada' || selectedCountry == 'United States')
    {
        document.getElementById('UserState').style.display = 'none';
        document.getElementById('UserState').parentNode.style.display = '';
		stateDropDown.style.display = '';
		
		// Check if the text box value maps to any drop down value
		var statesArray = canStates;
		var stateFound = false;
		if (selectedCountry == 'United States')
		    statesArray = usStates;

		stateDropDown.options.length=0;
		for (var stateCount = 0; stateCount < statesArray.length; stateCount++)
		{
		    if (statesArray[stateCount].text == document.getElementById('UserState').value || statesArray[stateCount].value == document.getElementById('UserState').value)
		    {
		        stateDropDown.options[stateDropDown.options.length] = new Option(statesArray[stateCount].value, statesArray[stateCount].text, true, true);
		        stateFound = true;
		    }
		    else
		        stateDropDown.options[stateDropDown.options.length] = new Option(statesArray[stateCount].value, statesArray[stateCount].text);
		}

		if (!stateFound)
		    document.getElementById('UserState').value = '';
    }
    else
    {
		document.getElementById('UserState').parentNode.style.display = 'none';
		stateDropDown.style.display = 'none';

		// Clear the value in the state box. It's only needed for US/Canada
		//  Set it to a single space in case the field is required
		document.getElementById('UserState').value = ' '
    }
}

function setState(stateCombo)
{
    // Update the real state field (currently hidden)
    document.getElementById('UserState').value = stateCombo.options[stateCombo.selectedIndex].value;
}

// Object to return a text and value together
function textValuePair(text, value)
{
    this.text = text;
    this.value = value;

    return this;
}
