/*

	*********
	*********  To be used only if country_state_select.js is already included
	*********	, but a second set of country/state dropdowns are needed
	*********
	
	
	
	Country State Drop Downs v1.1.  

	(c) Copyright 2006 by Down Home Consulting, Inc (www.DownHomeConsulting.com)

	Permission is hereby granted, free of charge, to any person obtaining a
	copy of this software and associated documentation files (the "Software"),
	to deal in the Software without restriction, including without limitation
	the rights to use, copy, modify, merge, publish, distribute, sublicense,
	and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	DEALINGS IN THE SOFTWARE.


	To install, add the following to the top of your page paste the html between the 
   form tags from demo2.html or demo.html into your page.

*/


// State table *** ALREADY INCLUDED
// 
// 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.
//

// Country data table *** ALREADY INCLUDED
//
// 
// 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.
//

// Save the country & state field names
var countryFieldCfgArray2 = document.getElementById('cs_config_country_field2').value.split(' ');
var stateFieldCfgArray2   = document.getElementById('cs_config_state_field2').value.split(' ');

// Save the names of the fields that hold the country & state default values
var countryDefaultCfgArray2 = document.getElementById('cs_config_country_default2').value.split(' ');
var stateDefaultCfgArray2   = document.getElementById('cs_config_state_default2').value.split(' ');

var defaultState2 = false;
var defaultCountry2 = false;


// Populates the country select with the counties from the country list
//
function populateCountry2(idName2) {

   var countryLineArray = country.split('|');      // Split into lines

   var selObj = document.getElementById( idName2 );

   selObj.options[0] = new Option('Select Country','');
   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 ( defaultCountry2 == countryCode ) {

         selObj.selectedIndex = loop + 1;
      }
   }
}
function populateState2( statestateIdName2, countryIdName2 ) {

   var selObj = document.getElementById( stateIdName2 );
   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(":");

      countryCode2  = TrimString(lineArray[0]);
      stateCode2    = TrimString(lineArray[1]);
      stateName2    = TrimString(lineArray[2]);

      if ( document.getElementById( countryIdName2 ).value == countryCode2 && countryCode2 != '' ) {

         // If it's a input element, change it to a select
         //
         if ( selObj.type == 'text' ) {

            parentObj = document.getElementById( stateIdName2 ).parentNode;
            parentObj.removeChild(selObj);

            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","state2"); 
            inputSel.setAttribute("id", stateIdName2 ); 

            parentObj.appendChild(inputSel) ;

            selObj = document.getElementById( stateIdName2 );
            selObj.options[0] = new Option('Select State','');
            selObj.selectedIndex = 0;
         }
   
         if ( stateCode != '' ) {

            selObj.options[optionCntr] = new Option(stateName2, stateCode2);
         }
         // See if it's selected from a previous post
         //
         if ( stateCode2 == defaultState2 && countryCode2 == defaultCountry2 ) {

            selObj.selectedIndex = optionCntr;
         }
         foundState = true;
         optionCntr++
      }
   }
   // If the country has no states, change the select to a text box
   //
   if ( ! foundState ) {

      parentObj = document.getElementById( stateIdName2 ).parentNode;
      parentObj.removeChild(selObj);
 
      // Create the Input Field
      var inputEl = document.createElement("INPUT");

      inputEl.setAttribute("id",  stateIdName2 ); 
      inputEl.setAttribute("type", "text"); 
      inputEl.setAttribute("name", "state2"); 
      inputEl.setAttribute("size", 20); 
      inputEl.setAttribute("value", defaultState2); 
      parentObj.appendChild(inputEl) ;
   }
   
}
// Called when state drop down is changed
// 
function updateState2( countryIdNameIn ) {

   for (var loop = 0; loop < countryFieldCfgArray2.length; loop++) {
   
      countryIdName2  = countryFieldCfgArray2[loop];
      stateIdName2    = stateFieldCfgArray2[loop];

      // Read the default value hidden fields
      defaultCountry2 = document.getElementById( countryDefaultCfgArray2[loop] ).value;
      defaultState2   = document.getElementById( stateDefaultCfgArray2[loop] ).value;

      if ( countryIdNameIn == countryIdName2 ) {

         populateState2( stateIdName2, countryIdName2 );
      }
   }
}
// Initialize the drop downs
// 
function initCountry2() {

   for (var loop = 0; loop < countryFieldCfgArray2.length; loop++) {
   
      countryIdName2  = countryFieldCfgArray2[loop];
      stateIdName2    = stateFieldCfgArray2[loop];

      // Read the default value hidden fields
      defaultCountry2 = document.getElementById( countryDefaultCfgArray2[loop] ).value;
      defaultState2   = document.getElementById( stateDefaultCfgArray2[loop] ).value;

      populateCountry2( countryIdName2);
      populateState2( stateIdName2, countryIdName2 );
   }
}

