// Save the country & city field names
var countryFieldCfgArray = document.getElementById('cs_config_country_field').value.split(' ');
var cityFieldCfgArray   = document.getElementById('cs_config_city_field').value.split(' ');
var city2FieldCfgArray   = document.getElementById('cs_config_city2_field').value.split(' ');

// Save the names of the fields that hold the country & city default values
var countryDefaultCfgArray = document.getElementById('cs_config_country_default').value.split(' ');
var cityDefaultCfgArray   = document.getElementById('cs_config_city_default').value.split(' ');
var city2DefaultCfgArray   = document.getElementById('cs_config_city2_default').value.split(' ');

var selectCityText = document.getElementById('cs_select_city_text').value;
var selectCountryText = document.getElementById('cs_select_country_text').value;
var allCountriesText = document.getElementById('cs_all_countries_text').value;
var allCitiesText = document.getElementById('cs_all_cities_text').value;

var defaultcity = false;
var defaultcity2 = false;
var defaultCountry = false;

if (document.getElementById('searchFlDD').value == '1') {
	var search = 1;
} else {
	var search = 0;
}
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 );

   if (search == '1') {
	selObj.options[0] = new Option(allCountriesText,'');
   } else {
    selObj.options[0] = new Option(selectCountryText,'');
   }
   
   selObj.selectedIndex = 0;

   for (var loop = 0; loop < countryLineArray.length; 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 populatecity( citycityIdName, countryIdName ) {

   var selObj = document.getElementById( cityIdName );
   var foundcity = false;

   // Empty options just in case new drop down is shorter
   //
   if ( selObj.type == 'select-one' ) {

      selObj.options.length = 0;

      if (search == '1') {
       selObj.options[0] = new Option(allCitiesText,'');
	  } else {
		selObj.options[0] = new Option(selectCityText,'');
	  }
      selObj.selectedIndex = 0;
   }
   // Populate the drop down with cities from the selected country
   //
   var cityLineArray   = city.split("|");        // Split into lines

   var optionCntr = 1;

   for (var loop = 0; loop < cityLineArray.length; loop++) {

      lineArray = cityLineArray[loop].split(":");

      countryCode  = TrimString(lineArray[0]);
      cityCode    = TrimString(lineArray[1]);
      cityName    = 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( cityIdName ).parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","city"); 
            inputSel.setAttribute("id", cityIdName );

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById( cityIdName );
            if (search == '1') {
             selObj.options[0] = new Option(allCitiesText,'');
            } else {
             selObj.options[0] = new Option(selectCityText,'');
            }
            selObj.selectedIndex = 0;
         }
   
         if ( cityCode != '' ) {

            selObj.options[optionCntr] = new Option(cityName, cityCode);
         }
         // See if it's selected from a previous post
         //
         if ( cityCode == defaultcity && countryCode == defaultCountry ) {

            selObj.selectedIndex = optionCntr;
         }
         foundcity = true;
         optionCntr++
      }
   }
   // If the country has no cities, change the select to a text box
   //
   if ( ! foundcity && document.getElementById( countryIdName ).value == 999) {

	//alert(document.getElementById( countryIdName ).value);
	
      parentObj = document.getElementById( cityIdName ).parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  cityIdName ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "city"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", defaultcity); 
      parentObj.appendChild(inputEl) ;
   }
   
}
// Called when city drop down is changed
// 


function populatecity2( citycityIdName, countryIdName ) {

   var selObj = document.getElementById( city2IdName );
   var foundcity = 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(selectCityText,'');
      selObj.selectedIndex = 0;
   }
   // Populate the drop down with cities from the selected country
   //
   var cityLineArray   = city.split("|");        // Split into lines

   var optionCntr = 1;

   for (var loop = 0; loop < cityLineArray.length; loop++) {

      lineArray = cityLineArray[loop].split(":");

      countryCode  = TrimString(lineArray[0]);
      cityCode    = TrimString(lineArray[1]);
      cityName    = 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( city2IdName ).parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","city2"); 
            inputSel.setAttribute("id", city2IdName ); 

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById( city2IdName );
            selObj.options[0] = new Option(selectCityText,'');
            selObj.selectedIndex = 0;
         }
   
         if ( cityCode != '' ) {

            selObj.options[optionCntr] = new Option(cityName, cityCode);
         }
         // See if it's selected from a previous post
         //
         if ( cityCode == defaultcity2 && countryCode == defaultCountry ) {

            selObj.selectedIndex = optionCntr;
         }
         foundcity = true;
         optionCntr++
      }
   }
   // If the country has no cities, change the select to a text box
   //
   if ( ! foundcity && document.getElementById( countryIdName ).value == 99) {

	//alert(document.getElementById( countryIdName ).value);
	
      parentObj = document.getElementById( city2IdName ).parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  city2IdName ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "city2"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", defaultcity2); 
      parentObj.appendChild(inputEl) ;
   }
   
}
// Called when city drop down is changed
// 


function updatecity( countryIdNameIn ) {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      cityIdName    = cityFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultcity   = document.getElementById( cityDefaultCfgArray[loop] ).value;

      if ( countryIdNameIn == countryIdName ) {

         populatecity( cityIdName, countryIdName );
      }
   }

}
function updatecity2( countryIdNameIn ) {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      city2IdName    = city2FieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultcity2   = document.getElementById( city2DefaultCfgArray[loop] ).value;

      if ( countryIdNameIn == countryIdName ) {

         populatecity2( city2IdName, countryIdName );
      }
   }

}

// Initialize the drop downs
// 
function initCountry() {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      cityIdName    = cityFieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultcity   = document.getElementById( cityDefaultCfgArray[loop] ).value;

      populateCountry( countryIdName);
      populatecity( cityIdName, countryIdName );
   }
}
function initCountry2() {

   for (var loop = 0; loop < countryFieldCfgArray.length; loop++) {
   
      countryIdName  = countryFieldCfgArray[loop];
      city2IdName    = city2FieldCfgArray[loop];

      // Read the default value hidden fields
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
      defaultcity2   = document.getElementById( city2DefaultCfgArray[loop] ).value;

      populateCountry( countryIdName);
      populatecity2( city2IdName, countryIdName );
   }
}
