/*
	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
// 
// 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 state = '\
United_States_Of_America:Alaska:Alaska|\
United_States_Of_America:Alabama:Alabama|\
United_States_Of_America:Arkansas:Arkansas|\
United_States_Of_America:American_Samoa:American Samoa|\
United_States_Of_America:Arizona:Arizona|\
United_States_Of_America:California:California|\
United_States_Of_America:Colorado:Colorado|\
United_States_Of_America:Connecticut:Connecticut|\
United_States_Of_America:D.C.:D.C.|\
United_States_Of_America:Delaware:Delaware|\
United_States_Of_America:Florida:Florida|\
United_States_Of_America:Micronesia:Micronesia|\
United_States_Of_America:Georgia:Georgia|\
United_States_Of_America:Guam:Guam|\
United_States_Of_America:Hawaii:Hawaii|\
United_States_Of_America:Iowa:Iowa|\
United_States_Of_America:Idaho:Idaho|\
United_States_Of_America:Illinois:Illinois|\
United_States_Of_America:Indiana:Indiana|\
United_States_Of_America:Kansas:Kansas|\
United_States_Of_America:Kentucky:Kentucky|\
United_States_Of_America:Louisiana:Louisiana|\
United_States_Of_America:Massachusetts:Massachusetts|\
United_States_Of_America:Maryland:Maryland|\
United_States_Of_America:Maine:Maine|\
United_States_Of_America:Marshall_Islands:Marshall Islands|\
United_States_Of_America:Michigan:Michigan|\
United_States_Of_America:Minnesota:Minnesota|\
United_States_Of_America:Missouri:Missouri|\
United_States_Of_America:Marianas:Marianas|\
United_States_Of_America:Mississippi:Mississippi|\
United_States_Of_America:Montana:Montana|\
United_States_Of_America:North_Carolina:North Carolina|\
United_States_Of_America:North_Dakota:North Dakota|\
United_States_Of_America:Nebraska:Nebraska|\
United_States_Of_America:New_Hampshire:New Hampshire|\
United_States_Of_America:New_Jersey:New Jersey|\
United_States_Of_America:New_Mexico:New Mexico|\
United_States_Of_America:Nevada:Nevada|\
United_States_Of_America:New_York:New York|\
United_States_Of_America:Ohio:Ohio|\
United_States_Of_America:Oklahoma:Oklahoma|\
United_States_Of_America:Oregon:Oregon|\
United_States_Of_America:Pennsylvania:Pennsylvania|\
United_States_Of_America:Puerto_Rico:Puerto Rico|\
United_States_Of_America:Palau:Palau|\
United_States_Of_America:Rhode_Island:Rhode Island|\
United_States_Of_America:South_Carolina:South Carolina|\
United_States_Of_America:South_Dakota:South Dakota|\
United_States_Of_America:Tennessee:Tennessee|\
United_States_Of_America:Texas:Texas|\
United_States_Of_America:Utah:Utah|\
United_States_Of_America:Virginia:Virginia|\
United_States_Of_America:Virgin_Islands:Virgin Islands|\
United_States_Of_America:Vermont:Vermont|\
United_States_Of_America:Washington:Washington|\
United_States_Of_America:Wisconsin:Wisconsin|\
United_States_Of_America:West Virginia:West Virginia|\
United_States_Of_America:Wyoming:Wyoming\
';

//last entry for state is eg: United_States_Of_America:Wyoming:Wyoming\ do not use |\
// 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 = '\
United_States_Of_America:United States Of America|\
Australia:Australia|\
Austria:Austria|\
Belgium:Belgium|\
Brazil:Brazil|\
Canada:Canada|\
Denmark:Denmark|\
Finland:Finland|\
France:France|\
Italy:Italy|\
Kazakhstan:Kazakhstan|\
Norway:Norway|\
Russia:Russia|\
Spain:Spain|\
Germany:Germany|\
United_Kingdom:United Kingdom|\
Turkey:Turkey|\
Ukraine:Ukraine|\
Japan:Japan|\
Afghanistan:Afghanistan|\
Albania:Albania|\
Algeria:Algeria|\
Andorra:Andorra|\
Anguilla:Anguilla|\
Antarctica:Antarctica|\
Argentina:Argentina|\
Armenia:Armenia|\
Aruba:Aruba|\
Azerbaijan:Azerbaijan|\
Bahamas:Bahamas|\
Bahrain:Bahrain|\
Bangladesh:Bangladesh|\
Barbados:Barbados|\
Belarus:Belarus|\
Belize:Belize|\
Benin:Benin|\
Bermuda:Bermuda|\
Bhutan:Bhutan|\
Bolivia:Bolivia|\
Bosnia and Herzegovina:Bosnia Herzegovina|\
Botswana:Botswana|\
Bouvet Island:Bouvet Island|\
British Indian Ocean Territory:British Indian Ocean Territory|\
Brunei Darussalam:Brunei Darussalam|\
Bulgaria:Bulgaria|\
Burkina Faso:Burkina Faso|\
Burundi:Burundi|\
Cambodia:Cambodia|\
Cameroon:Cameroon|\
Cape Verde:Cape Verde|\
Cayman Islands:Cayman Islands|\
Central African Republic:Central African Republic|\
Chad:Chad|\
Chile:Chile|\
China:China|\
Christmas Island:Christmas Island|\
Cocos (Keeling) Islands:Cocos (Keeling) Islands|\
Colombia:Colombia|\
Comoros:Comoros|\
Congo:Congo|\
Congo, the Democratic Republic of the:Congo, the Democratic Republic of the|\
Cook Islands:Cook Islands|\
Costa Rica:Costa Rica|\
Croatia:Croatia|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic|\
Djibouti:Djibouti|\
Dominica:Dominica|\
Dominican Republic:Dominican Republic|\
East Timor:East Timor|\
Ecuador:Ecuador|\
Egypt:Egypt|\
El Salvador:El Salvador|\
England:England|\
Equatorial Guinea:Equatorial Guinea|\
Eritrea:Eritrea|\
Espana:Espana|\
Estonia:Estonia|\
Ethiopia:Ethiopia|\
Falkland Islands:Falkland Islands|\
Faroe Islands:Faroe Islands|\
Fiji:Fiji|\
French Guiana:French Guiana|\
French Polynesia:French Polynesia|\
French Southern Territories:French Southern Territories|\
Gabon:Gabon|\
Gambia:Gambia|\
Georgia:Georgia|\
Ghana:Ghana|\
Gibraltar:Gibraltar|\
Great Britain:Great Britain|\
Greece:Greece|\
Greenland:Greenland|\
Grenada:Grenada|\
Guadeloupe:Guadeloupe|\
Guam:Guam|\
Guatemala:Guatemala|\
Guinea:Guinea|\
Guinea-Bissau:Guinea-Bissau|\
Guyana:Guyana|\
Haiti:Haiti|\
Heard and Mc Donald Islands:Heard and Mc Donald Islands|\
Honduras:Honduras|\
Hong Kong:Hong Kong|\
Hungary:Hungary|\
Iceland:Iceland|\
India:India|\
Indonesia:Indonesia|\
Ireland:Ireland|\
Israel:Israel|\
Jamaica:Jamaica|\
Jordan:Jordan|\
Kenya:Kenya|\
Kiribati:Kiribati|\
Korea, Republic of:Korea, Republic of|\
Korea (South):Korea (South)|\
Kuwait:Kuwait|\
Kyrgyzstan:Kyrgyzstan|\
Latvia:Latvia|\
Lebanon:Lebanon|\
Lesotho:Lesotho|\
Liberia:Liberia|\
Libya:Libya|\
Liechtenstein:Liechtenstein|\
Lithuania:Lithuania|\
Luxembourg:Luxembourg|\
Macau:Macau|\
Macedonia:Macedonia|\
Madagascar:Madagascar|\
Malawi:Malawi|\
Malaysia:Malaysia|\
Maldives:Maldives|\
Mali:Mali|\
Malta:Malta|\
Marshall Islands:Marshall Islands|\
Martinique:Martinique|\
Mauritania:Mauritania|\
Mauritius:Mauritius|\
Mayotte:Mayotte|\
Mexico:Mexico|\
Micronesia, Federated States of:Micronesia, Federated States of|\
Moldova, Republic of:Moldova Republic of|\
Monaco:Monaco|\
Mongolia:Mongolia|\
Montserrat:Montserrat|\
Morocco:Morocco|\
Mozambique:Mozambique|\
Myanmar:Myanmar|\
Namibia:Namibia|\
Nauru:Nauru|\
Nepal:Nepal|\
Netherlands:Netherlands|\
Netherlands Antilles:Netherlands Antilles|\
New Caledonia:New Caledonia|\
New Zealand:New Zealand|\
Nicaragua:Nicaragua|\
Niger:Niger|\
Nigeria:Nigeria|\
Niue:Niue|\
Norfolk Island:Norfolk Island|\
Northern Ireland:Northern Ireland|\
Northern Mariana Islands:Northern Mariana Islands|\
Oman:Oman|\
Pakistan:Pakistan|\
Palau:Palau|\
Panama:Panama|\
Papua New Guinea:Papua New Guinea|\
Paraguay:Paraguay|\
Peru:Peru|\
Philippines:Philippines|\
Pitcairn:Pitcairn|\
Poland:Poland|\
Portugal:Portugal|\
Puerto Rico:Puerto Rico|\
Qatar:Qatar|\
Reunion:Reunion|\
Romania:Romania|\
Russian Federation:Russian Federation|\
Rwanda:Rwanda|\
Saint Kitts and Nevis:Saint Kitts and Nevis|\
Saint Lucia:Saint Lucia|\
Saint Vincent and the Grenadines:Saint Vincent and the Grenadines|\
Samoa (Independent):Samoa (Independent)|\
San Marino:San Marino|\
Sao Tome and Principe:Sao Tome and Principe|\
Saudi Arabia:Saudi Arabia|\
Scotland:Scotland|\
Senegal:Senegal|\
Serbia and Montenegro:Serbia and Montenegro|\
Seychelles:Seychelles|\
Sierra Leone:Sierra Leone|\
Singapore:Singapore|\
Slovakia:Slovakia|\
Slovenia:Slovenia|\
Solomon Islands:Solomon Islands|\
Somalia:Somalia|\
South Africa:South Africa|\
South Georgia and the South Sandwich Islands:South Georgia and the South Sandwich Islands|\
South Korea:South Korea|\
Sri Lanka:Sri Lanka|\
St. Helena:St. Helena|\
St. Pierre and Miquelon:St. Pierre and Miquelon|\
Suriname:Suriname|\
Svalbard and Jan Mayen Islands:Svalbard and Jan Mayen Islands|\
Swaziland:Swaziland|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Taiwan:Taiwan|\
Tajikistan:Tajikistan|\
Tanzania:Tanzania|\
Thailand:Thailand|\
Togo:Togo|\
Tokelau:Tokelau|\
Tonga:Tonga|\
Trinidad:Trinidad|\
Trinidad and Tobago:Trinidad and Tobago|\
Tunisia:Tunisia|\
Turkmenistan:Turkmenistan|\
Turks and Caicos Islands:Turks and Caicos Islands|\
Tuvalu:Tuvalu|\
Uganda:Uganda|\
United Arab Emirates:United Arab Emirates|\
Uruguay:Uruguay|\
Uzbekistan:Uzbekistan|\
Vanuatu:Vanuatu|\
Vatican City State (Holy See):Vatican City State (Holy See)|\
Venezuela:Venezuela|\
Viet Nam:Viet Nam|\
Virgin Islands (British):Virgin Islands (British)|\
Virgin Islands (U.S.):Virgin Islands (U.S.)|\
Wales:Wales|\
Wallis and Futuna Islands:Wallis and Futuna Islands|\
Western Sahara:Western Sahara|\
Yemen:Yemen|\
Zambia:Zambia|\
Zimbabwe:Zimbabwe\
';

//last entry for country is eg: Zimbabwe:Zimbabwe\ do not use |\
// 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; //false
var defaultCountry = false; //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; 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); 
	  inputEl.setAttribute("value", "Enter State"); 

      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 );
   }
}

