var state = '\
United States:Alabama:Alabama|\
United States:Alaska:Alaska|\
United States:Arizona:Arizona|\
United States:Arkansas:Arkansas|\
United States:California:California|\
United States:Colorado:Colorado|\
United States:Connecticut:Connecticut|\
United States:D.C.:D.C.|\
United States:Delaware:Delaware|\
United States:Florida:Florida|\
United States:Georgia:Georgia|\
United States:Guam:Guam|\
United States:Hawaii:Hawaii|\
United States:Idaho:Idaho|\
United States:Iowa:Iowa|\
United States:Illinois:Illinois|\
United States:Indiana:Indiana|\
United States:Kansas:Kansas|\
United States:Kentucky:Kentucky|\
United States:Louisiana:Louisiana|\
United States:Maine:Maine|\
United States:Maryland:Maryland|\
United States:Massachusetts:Massachusetts|\
United States:Michigan:Michigan|\
United States:Minnesota:Minnesota|\
United States:Missouri:Missouri|\
United States:Mississippi:Mississippi|\
United States:Montana:Montana|\
United States:North Carolina:North Carolina|\
United States:North Dakota:North Dakota|\
United States:Nebraska:Nebraska|\
United States:New Hampshire:New Hampshire|\
United States:New Jersey:New Jersey|\
United States:New Mexico:New Mexico|\
United States:Nevada:Nevada|\
United States:New York:New York|\
United States:Ohio:Ohio|\
United States:Oklahoma:Oklahoma|\
United States:Oregon:Oregon|\
United States:Pennsylvania:Pennsylvania|\
United States:Puerto Rico:Puerto Rico|\
United States:Rhode Island:Rhode Island|\
United States:South Carolina:South Carolina|\
United States:South Dakota:South Dakota|\
United States:Tennessee:Tennessee|\
United States:Tennessee:Tennessee|\
United States:Utah:Utah|\
United States:Virginia:Virginia|\
United States:Virgin Islands:Virgin Islands|\
United States:Vermont:Vermont|\
United States:Washington:Washington|\
United States:Wisconsin:Wisconsin|\
United States:West Virginia:West Virginia|\
United States:Wyoming:Wyoming|\
Canada:Alberta:AB|\
Canada:British Columbia:BC|\
Canada:Manitoba:MB|\
Canada:New Brunswick:NB|\
Canada:Newfoundland and Labrador:NL|\
Canada:Northwest Territories:NT|\
Canada:Nova Scotia:NS|\
Canada:NU:NU|\
Canada:Ontario:ON|\
Canada:Prince Edward Island:PE|\
Canada:Quebec:QC|\
Canada:Saskatchewan:SK|\
Canada:Yukon Territory:YT|\
';

// Country data table
//
// 
// To edit the list, just delete a line or add a line.  Order is important.  The order 
// displayed is the order it appears on the drop down.
//
var country = '\
Canada:Canada|\
United States:United States|\
';

// Save the country & state field names
var countryFieldCfgArray = document.getElementById('cs_config_country_field').value.split(' ');
var stateFieldCfgArray   = document.getElementById('cs_config_state_field').value.split(' ');

// Save the names of the fields that hold the country & state default values
var countryDefaultCfgArray = document.getElementById('cs_config_country_default').value.split(' ');
var stateDefaultCfgArray   = document.getElementById('cs_config_state_default').value.split(' ');

var defaultState = false;
var defaultCountry = false;

function TrimString(sInString) {
   
   if ( sInString ) {

      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
   }
}
// Populates the country select with the counties from the country list
//
function populateCountry(idName) {

   var countryLineArray = country.split('|');      // Split into lines

   var selObj = document.getElementById( idName );

   selObj.options[0] = new Option('Select Country','');
   selObj.selectedIndex = 0;

   for (var loop = 0; loop < countryLineArray.length-1; loop++) {

      lineArray = countryLineArray[loop].split(':');

      countryCode  = TrimString(lineArray[0]);
      countryName  = TrimString(lineArray[1]);
   
      if ( countryCode != '' ) {

         selObj.options[loop + 1] = new Option(countryName, countryCode);
      }

      if ( defaultCountry == countryCode ) {

         selObj.selectedIndex = loop + 1;
      }
   }
}
function populateState( statestateIdName, countryIdName ) {

   var selObj = document.getElementById( stateIdName );
   var foundState = false;

   // Empty options just in case new drop down is shorter
   //
   if ( selObj.type == 'select-one' ) {

      selObj.options.length = 0;

      selObj.options[0] = new Option('Select State','');
      selObj.selectedIndex = 0;
   }
   // Populate the drop down with states from the selected country
   //
   var stateLineArray   = state.split("|");        // Split into lines

   var optionCntr = 1;

   for (var loop = 0; loop < stateLineArray.length; loop++) {

      lineArray = stateLineArray[loop].split(":");

      countryCode  = TrimString(lineArray[0]);
      stateCode    = TrimString(lineArray[1]);
      stateName    = TrimString(lineArray[2]);

      if ( document.getElementById( countryIdName ).value == countryCode && countryCode != '' ) {

         // If it's a input element, change it to a select
         //
         if ( selObj.type == 'text' ) {

            parentObj = document.getElementById( stateIdName ).parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","state"); 
            inputSel.setAttribute("id", stateIdName ); 

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById( stateIdName );
            selObj.options[0] = new Option('Select State','');
            selObj.selectedIndex = 0;
         }
   
         if ( stateCode != '' ) {

            selObj.options[optionCntr] = new Option(stateName, stateCode);
         }
         // See if it's selected from a previous post
         //
         if ( stateCode == defaultState && countryCode == defaultCountry ) {

            selObj.selectedIndex = optionCntr;
         }
         foundState = true;
         optionCntr++
      }
   }
   // If the country has no states, change the select to a text box
   //
   if ( ! foundState ) {

      parentObj = document.getElementById( stateIdName ).parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  stateIdName ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "state"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", defaultState); 
      parentObj.appendChild(inputEl) ;
   }
   
}
// Called when state drop down is changed
// 
function updateState( countryIdNameIn ) {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      stateIdName    = stateFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultState   = document.getElementById( stateDefaultCfgArray[loop] ).value;

      if ( countryIdNameIn == countryIdName ) {

         populateState( stateIdName, countryIdName );
      }
   }
}
// Initialize the drop downs
// 
function initCountry() {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      stateIdName    = stateFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultState   = document.getElementById( stateDefaultCfgArray[loop] ).value;

      populateCountry( countryIdName);
      populateState( stateIdName, countryIdName );
   }
}
