/*
	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 = '\
NE:Adams:Adams|\
NE:Antelope:Antelope|\
NE:Arthur:Arthur|\
NE:Banner:Banner|\
NE:Blane:Blane|\
NE:Boone:Boone|\
NE:Box Butte:Box Butte|\
NE:Boyd:Boyd|\
NE:Brown:Brown|\
NE:Buffalo:Buffalo|\
NE:Burt:Burt|\
NE:Butler:Butler|\
NE:Cass:Cass|\
NE:Cedar:Cedar|\
NE:Chase:Chase|\
NE:Cherry:Cherry|\
NE:Cheyenne:Cheyenne|\
NE:Clay:Clay|\
NE:Colfax:Colfax|\
NE:Cuming:Cuming|\
NE:Custer:Custer|\
NE:Dakota:Dakota|\
NE:Dawes:Dawes|\
NE:Dawson:Dawson|\
NE:Deuel:Deuel|\
NE:Dixon:Dixon|\
NE:Douglas:Douglas|\
NE:Dodge:Dodge|\
NE:Dundy:Dundy|\
NE:Fillmore:Fillmore|\
NE:Franklin:Franklin|\
NE:Frontier:Frontier|\
NE:Furnas:Furnas|\
NE:Gage:Gage|\
NE:Garden:Garden|\
NE:Garfield:Garfield|\
NE:Gosper:Gosper|\
NE:Grant:Grant|\
NE:Greeley:Greeley|\
NE:Hall:Hall|\
NE:Hamilton:Hamilton|\
NE:Harlan:Harlan|\
NE:Hayes:Hayes|\
NE:Hitchcock:Hitchcock|\
NE:Holt:Holt|\
NE:Hooker:Hooker|\
NE:Howard:Howard|\
NE:Jefferson:Jefferson|\
NE:Johnson:Johnson|\
NE:Kearney:Kearney|\
NE:Keith:Keith|\
NE:Keya Paha:Keya Paha|\
NE:Kimball:Kimball|\
NE:Knox:Knox|\
NE:Lancaster:Lancaster|\
NE:Lincoln:Lincoln|\
NE:Logan:Logan|\
NE:Loup:Loup|\
NE:Madison:Madison|\
NE:McPhearson:McPhearson|\
NE:Merrick:Merrick|\
NE:Morrill:Morrill|\
NE:Nance:Nance|\
NE:Nemaha:Nemaha|\
NE:Nuckolis:Nuckolis|\
NE:Otoe:Otoe|\
NE:Pawnee:Pawnee|\
NE:Perkins:Perkins|\
NE:Phelps:Phelps|\
NE:Pierce:Pierce|\
NE:Platte:Platte|\
NE:Polk:Polk|\
NE:Red Willow:Red Willow|\
NE:Richardson:Richardson|\
NE:Rock:Rock|\
NE:Saline:Saline|\
NE:Sarpy:Sarpy|\
NE:Saunders:Saunders|\
NE:Scotts Bluff:Scotts Bluff|\
NE:Seward:Seward|\
NE:Sheridan:Sheridan|\
NE:Sherman:Sherman|\
NE:Sioux:Sioux|\
NE:Stanton:Stanton|\
NE:Thayer:Thayer|\
NE:Thomas:Thomas|\
NE:Thurston:Thurston|\
NE:Valley:Valley|\
NE:Washington:Washington|\
NE:Wayne:Wayne|\
NE:Webster:Webster|\
NE:Wheeler:Wheeler|\
NE:York:York|\
IA:Adair:Adair|\
IA:Adams:Adams|\
IA:Allamakee:Allamakee|\
IA:Appathoose:Appathoose|\
IA:Audubon:Audubon|\
IA:Benton:Benton|\
IA:Black Hawk:Black Hawk|\
IA:Boone:Boone|\
IA:Bremer:Bremer|\
IA:Buchanan:Buchanan|\
IA:Buena Vista:Buena Vista|\
IA:Butler:Butler|\
IA:Calhoun:Calhoun|\
IA:Carroll:Carroll|\
IA:Cass:Cass|\
IA:Cedar:Cedar|\
IA:Cerro Gordo:Cerro Gordo|\
IA:Cherokee:Cherokee|\
IA:Chickasaw:Chickasaw|\
IA:Clarke:Clarke|\
IA:Clay:Clay|\
IA:Clayton:Clayton|\
IA:Clinton:Clinton|\
IA:Crawford:Crawford|\
IA:Dallas:Dallas|\
IA:Davis:Davis|\
IA:Decatur:Decatur|\
IA:Delaware:Delaware|\
IA:Des Moines:Des Moines|\
IA:Dickinson:Dickinson|\
IA:Dubuque:Dubuque|\
IA:Emmet:Emmet|\
IA:Fayette:Fayette|\
IA:Floyd:Floyd|\
IA:Franklin:Franklin|\
IA:Fremont:Fremont|\
IA:Greene:Greene|\
IA:Grundy:Grundy|\
IA:Guthrie:Guthrie|\
IA:Hamilton:Hamilton|\
IA:Hancock:Hancock|\
IA:Hardin:Hardin|\
IA:Harrison:Harrison|\
IA:Henry:Henry|\
IA:Howard:Howard|\
IA:Humboldt:Humboldt|\
IA:Ida:Ida|\
IA:Iowa:Iowa|\
IA:Jackson:Jackson|\
IA:Jasper:Jasper|\
IA:Jefferson:Jefferson|\
IA:Johnson:Johnson|\
IA:Jones:Jones|\
IA:Keckuk:Keckuk|\
IA:Kossuth:Kossuth|\
IA:Lee:Lee|\
IA:Linn:Linn|\
IA:Llucas:Llucas|\
IA:Louisa:Louisa|\
IA:Lyon:Lyon|\
IA:Madison:Madison|\
IA:Mahaska:Mahaska|\
IA:Marion:Marion|\
IA:Marshall:Marshall|\
IA:Mescatine:Mescatine|\
IA:Mills:Mills|\
IA:Mitchell:Mitchell|\
IA:Monona:Monona|\
IA:Monroe:Monroe|\
IA:Montgomery:Montgomery|\
IA:Obrien:Obrien|\
IA:Osceola:Osceola|\
IA:Page:Page|\
IA:Palo Alto:Palo Alto|\
IA:Plymouth:Plymouth|\
IA:Pocahontas:Pocahontas|\
IA:Polk:Polk|\
IA:Pottawattamie:Pottawattamie|\
IA:Poweshiek:Poweshiek|\
IA:Ringgold:Ringgold|\
IA:Sac:Sac|\
IA:Scott:Scott|\
IA:Shelby:Shelby|\
IA:Sioux:Sioux|\
IA:Story:Story|\
IA:Tama:Tama|\
IA:Taylor:Taylor|\
IA:Union:Union|\
IA:Van Buren:Van Buren|\
IA:Wapello:Wapello|\
IA:Warren:Warren|\
IA:Washington:Washington|\
IA:Wayne:Wayne|\
IA:Webster:Webster|\
IA:Winnebago:Winnebago|\
IA:Winneshek:Winneshek|\
IA:Woodbury:Woodbury|\
IA:Worth:Worth|\
IA:Wright:Wright|\
CO:Adams:Adams|\
CO:Alamosa:Alamosa|\
CO:Arapahoe:Arapahoe|\
CO:Archuleta:Archuleta|\
CO:Baca:Baca|\
CO:Bent:Bent|\
CO:Boulder:Boulder|\
CO:Broomfield:Broomfield|\
CO:Chaffee:Chaffee|\
CO:Cheyenne:Cheyenne|\
CO:Clear Creek:Clear Creek|\
CO:Conejos:Conejos|\
CO:Costilla:Costilla|\
CO:Crowley:Crowley|\
CO:Custer:Custer|\
CO:Delta:Delta|\
CO:Denver:Denver|\
CO:Dolores:Dolores|\
CO:Douglas:Douglas|\
CO:Eagle:Eagle|\
CO:El Paso:El Paso|\
CO:Elbert:Elbert|\
CO:Fremont:Fremont|\
CO:Garfield:Garfield|\
CO:Gilpin:Gilpin|\
CO:Grand:Grand|\
CO:Gunnison:Gunnison|\
CO:Hinsdale:Hinsdale|\
CO:Huerfano:Huerfano|\
CO:Jackson:Jackson|\
CO:Jefferson:Jefferson|\
CO:Kiowa:Kiowa|\
CO:Kit Carson:Kit Carson|\
CO:La Plata:La Plata|\
CO:Lake:Lake|\
CO:Larimer:Larimer|\
CO:Las Animas:Las Animas|\
CO:Lincoln:Lincoln|\
CO:Logan:Logan|\
CO:Mesa:Mesa|\
CO:Mineral:Mineral|\
CO:Moffat:Moffat|\
CO:Montezuma:Montezuma|\
CO:Montrose:Montrose|\
CO:Morgan:Morgan|\
CO:Otero:Otero|\
CO:Ouray:Ouray|\
CO:Park:Park|\
CO:Phillips:Phillips|\
CO:Pitkin:Pitkin|\
CO:Prowers:Prowers|\
CO:Pueblo:Pueblo|\
CO:Rio Blanco:Rio Blanco|\
CO:Rio Grande:Rio Grande|\
CO:Routt:Routt|\
CO:Saguache:Saguache|\
CO:San Juan:San Juan|\
CO:San Miguel:San Miguel|\
CO:Sedgwick:Sedgwick|\
CO:Summit:Summit|\
CO:Teller:Teller|\
CO:Washington:Washington|\
CO:Weld:Weld|\
CO:Yuma:Yuma|\
';

// 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 = '\
AK:Alaska|\
AL:Alabama|\
AR:Arkansas|\
AZ:Arizona|\
CA:California|\
CO:Colorado|\
CT:Connecticut|\
DC:D.C.|\
DE:Delaware|\
FL:Florida|\
GA:Georgia|\
HI:Hawaii|\
IA:Iowa|\
ID:Idaho|\
IL:Illinois|\
IN:Indiana|\
KS:Kansas|\
KY:Kentucky|\
LA:Louisiana|\
MA:Massachusetts|\
MD:Maryland|\
ME:Maine|\
MI:Michigan|\
MN:Minnesota|\
MO:Missouri|\
MS:Mississippi|\
MT:Montana|\
NC:North Carolina|\
ND:North Dakota|\
NE:Nebraska|\
NH:New Hampshire|\
NJ:New Jersey|\
NM:New Mexico|\
NV:Nevada|\
NY:New York|\
OH:Ohio|\
OK:Oklahoma|\
OR:Oregon|\
PA:Pennsylvania|\
RI:Rhode Island|\
SC:South Carolina|\
SD:South Dakota|\
TN:Tennessee|\
TX:Texas|\
UT:Utah|\
VA:Virginia|\
VT:Vermont|\
WA:Washington|\
WI:Wisconsin|\
WV:West Virginia|\
WY:Wyoming|\
';

// 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 State','');
   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 County','');
      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 County','');
            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 );
   }
}

