// JavaScript Document

var state = '\
China:\Anhui:Anhui|\
China:\Beijing:Beijing|\
China:\Chongqing:Chongqing|\
China:\Fujian:Fujian|\
China:\Gansu:Gansu|\
China:\Guangdong:Guangdong|\
China:\Guangxi:Guangxi|\
China:\Guizhou:Guizhou|\
China:\Hainan:Hainan|\
China:\Hebei:Hebei|\
China:\Heilongjiang:Heilongjiang|\
China:\Henan:Henan|\
China:\Hubei:Hubei|\
China:\Hunan:Hunan|\
China:\Jiangsu:Jiangsu|\
China:\Jiangxi:Jiangxi|\
China:\Jilin:Jilin|\
China:\Liaoning:Liaoning|\
China:\Nei Mongol:Nei Mongol|\
China:\Ningxia:Ningxia|\
China:\Qinghai:Qinghai|\
China:\Shaanxi:Shaanxi|\
China:\Shandong:Shandong|\
China:\Shanghai:Shanghai|\
China:\Shanxi:Shanxi|\
China:\Sichuan:Sichuan|\
China:\Tianjin:Tianjin|\
China:\Xinjiang:Xinjiang|\
China:\Xizang:Xizang|\
China:\Yunnan:Yunnan|\
China:\Zhejiang:Zhejiang|\
Hong Kong:\Kowloon:Kowloon|\
Hong Kong:\Hong Kong Island:Hong Kong Island|\
Hong Kong:\New Territories:New Territories|\
Hong Kong:\Islands:Islands|\
Taiwan:\KeeLung City:KeeLung City|\
Taiwan:\Taipei City:Taipei City|\
Taiwan:\Taipei County:Taipei County|\
Taiwan:\TaoYuan County:TaoYuan County|\
Taiwan:\HsinChu City:HsinChu City|\
Taiwan:\HsinChu County:HsinChu County|\
Taiwan:\MiaoLi County:MiaoLi County|\
Taiwan:\TaiChung City:TaiChung City|\
Taiwan:\TaiChung County:TaiChung County|\
Taiwan:\ChangHua County:ChangHua County|\
Taiwan:\NanTou County:NanTou County|\
Taiwan:\YunLin County:YunLin County|\
Taiwan:\ChiaYi City:ChiaYi City|\
Taiwan:\ChiaYi County:ChiaYi County|\
Taiwan:\TaiNan City:TaiNan City|\
Taiwan:\TaiNan County:TaiNan County|\
Taiwan:\KaoHsiung City:KaoHsiung City|\
Taiwan:\KaoHsiung County:KaoHsiung County|\
Taiwan:\PingTung County:PingTung County|\
Taiwan:\TaiTung County:TaiTung County|\
Taiwan:\HuaLien County:HuaLien County|\
Taiwan:\ILan County:ILan County|\
Taiwan:\PengHu County:PengHu County|\
Taiwan:\KinMen County:KinMen County|\
Taiwan:\LienChiang County:LienChiang County|\
';

var country = '\
China:China|\
Hong Kong:Hong Kong|\
Macao:Macao|\
Taiwan:Taiwan|\
South Korea:South Korea|\
Japan:Japan|\
Indonesia:Indonesia|\
Thailand:Thailand|\
Vietnam:Vietnam|\
Laos:Laos|\
Singapore:Singapore|\
Malaysia:Malaysia|\
Myanmar:Myanmar (Burma)|\
India:India|\
Worldwide:Other Country|\
';

// 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--','--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--','--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 );
   }
}
