﻿
// This function fires when the country dropdown list is changed.  It triggers the refreshing of the state dropdown.
function OnCountryChanged(country, state) {
    GetStateList(country, state);    
}

// Update the State field label
function ChangeStateLabel(labelID, countryList) 
{
  var labelState = document.getElementById(labelID);
  var countryCode = countryList.value;
  
  // If Canada, display as Province.
  if(countryCode=='CAN')
  {	
    labelState.innerText=wordProvince;
  }
  else
  {
    labelState.innerText=wordState; 
  }
}

function ShowRequiredFlag(country, postalCodeRequiredCtrlRef)
{
    var countryCode = country.value;

    if (typeof (postalCodeRequiredCtrlRef) != 'undefined' && postalCodeRequiredCtrlRef != null)
    {
        if (countryCode == 'USA' || countryCode == 'CAN' || countryCode == 'MEX')
        {
            postalCodeRequiredCtrlRef.style.display = "";
        }
        else
        {
            postalCodeRequiredCtrlRef.style.display = "none";
        }        
    }
}

function GetStateList(country, state) 
{
    var selectedCountry = country.value;   
    
    var handleResult = function(result) { LoadResult(result, country, state); };

    USAirways.Web.Shared.UI.Services.ControlsWebService.GetStateList(selectedCountry, handleResult); 
    
}

function LoadResult(rawStateList, country, state)
{   
    var countryCode = country.value;
    var stateList = state;
    
    //Clears the state combo box contents.
    stateList.options.length = 0;

    var stateNodes = rawStateList.split(",");
    // Per requirements (2.4.5) do not allow the entry of the state unless country is US,Mexico, or Canada.
    if( countryCode=='USA' || countryCode=='CAN' || countryCode=='MEX' ) 
    {
        stateList.options[0] = new Option((countryCode=='CAN')?textProvince:textState, ""); 
        
        var value; 
        var display;
        var optionItem;
        //Add new states list to the state combo box.
        for (count = 0; count < stateNodes.length; count++)
        {
            var optionParts = stateNodes[count].split("-");
            value = (optionParts[1]);
            display = (optionParts[0]);
            optionItem = new Option( value, display,  false, false);
            stateList.options[stateList.length] = optionItem;
        }
        
        // Ensure the state dropdown is enabled.
        stateList.disabled = false;

    }
    else 
    {        
        // A country with no state list has been selected, so grey out the state dropdown.
        stateList.disabled = true;
        stateList.options[0] = new Option("", "OTH"); // TODO: Content control these?

    }
}


