/*
	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:AK:Alaska|\
United States:AL:Alabama|\
United States:AR:Arkansas|\
United States:AS:American Samoa|\
United States:AZ:Arizona|\
United States:CA:California|\
United States:CO:Colorado|\
United States:CT:Connecticut|\
United States:DC:D.C.|\
United States:DE:Delaware|\
United States:FL:Florida|\
United States:FM:Micronesia|\
United States:GA:Georgia|\
United States:GU:Guam|\
United States:HI:Hawaii|\
United States:IA:Iowa|\
United States:ID:Idaho|\
United States:IL:Illinois|\
United States:IN:Indiana|\
United States:KS:Kansas|\
United States:KY:Kentucky|\
United States:LA:Louisiana|\
United States:MA:Massachusetts|\
United States:MD:Maryland|\
United States:ME:Maine|\
United States:MH:Marshall Islands|\
United States:MI:Michigan|\
United States:MN:Minnesota|\
United States:MO:Missouri|\
United States:MP:Marianas|\
United States:MS:Mississippi|\
United States:MT:Montana|\
United States:NC:North Carolina|\
United States:ND:North Dakota|\
United States:NE:Nebraska|\
United States:NH:New Hampshire|\
United States:NJ:New Jersey|\
United States:NM:New Mexico|\
United States:NV:Nevada|\
United States:NY:New York|\
United States:OH:Ohio|\
United States:OK:Oklahoma|\
United States:OR:Oregon|\
United States:PA:Pennsylvania|\
United States:PR:Puerto Rico|\
United States:PW:Palau|\
United States:RI:Rhode Island|\
United States:SC:South Carolina|\
United States:SD:South Dakota|\
United States:TN:Tennessee|\
United States:TX:Texas|\
United States:UT:Utah|\
United States:VA:Virginia|\
United States:VI:Virgin Islands|\
United States:VT:Vermont|\
United States:WA:Washington|\
United States:WI:Wisconsin|\
United States:WV:West Virginia|\
United States:WY:Wyoming|\
United States:AA:Military Americas|\
United States:AE:Military Europe/ME/Canada|\
United States:AP:Military Pacific|\
Canada:AB:Alberta|\
Canada:MB:Manitoba|\
Canada:AB:Alberta|\
CA:BC:British Columbia|\
CA:MB:Manitoba|\
CA:NB:New Brunswick|\
Canada:Newfoundland:Newfoundland|\
Canada:Nova Scotia:Nova Scotia|\
Canada:Northwest Territories:Northwest Territories|\
Canada:Nunavut:Nunavut|\
Canada:Ontario:Ontario|\
Canada:Prince Edward Island:Prince Edward Island|\
Canada:Quebec:Quebec|\
Canada:Saskatchewan:Saskatchewan|\
Canada:Yukon Territory:Yukon Territory|\
Australia:Australian Antarctic Territory:Australian Antarctic Territory|\
Australia:Australian Capital Territory:Australian Capital Territory|\
Australia:Northern Territory:Northern Territory|\
Australia:New South Wales:New South Wales|\
Australia:Queensland:Queensland|\
Australia:South Australia:South Australia|\
Australia:Tasmania:Tasmania|\
Australia:Victoria:Victoria|\
Australia:Western Australia:Western Australia|\
Brazil:AC:Acre|\
Brazil:AL:Alagoas|\
Brazil:AM:Amazonas|\
Brazil:AP:Amapa|\
Brazil:BA:Baia|\
Brazil:CE:Ceara|\
Brazil:DF:Distrito Federal|\
Brazil:ES:Espirito Santo|\
Brazil:FN:Fernando de Noronha|\
Brazil:GO:Goias|\
Brazil:MA:Maranhao|\
Brazil:MG:Minas Gerais|\
Brazil:MS:Mato Grosso do Sul|\
Brazil:MT:Mato Grosso|\
Brazil:PA:Para|\
Brazil:PB:Paraiba|\
Brazil:PE:Pernambuco|\
Brazil:PI:Piaui|\
Brazil:PR:Parana|\
Brazil:RJ:Rio de Janeiro|\
Brazil:RN:Rio Grande do Norte|\
Brazil:RO:Rondonia|\
Brazil:RR:Roraima|\
Brazil:RS:Rio Grande do Sul|\
Brazil:SC:Santa Catarina|\
Brazil:SE:Sergipe|\
Brazil:SP:Sao Paulo|\
Brazil:TO:Tocatins|\
Netherlands:DR:Drente|\
Netherlands:FL:Flevoland|\
Netherlands:FR:Friesland|\
Netherlands:GL:Gelderland|\
Netherlands:GR:Groningen|\
Netherlands:LB:Limburg|\
Netherlands:NB:Noord Brabant|\
Netherlands:NH:Noord Holland|\
Netherlands:OV:Overijssel|\
Netherlands:UT:Utrecht|\
Netherlands:ZH:Zuid Holland|\
Netherlands:ZL:Zeeland|\
Ireland:CO ANTRIM:County Antrim|\
Ireland:CO ARMAGH:County Armagh|\
Ireland:CO DOWN:County Down|\
Ireland:CO FERMANAGH:County Fermanagh|\
Ireland:CO DERRY:County Londonderry|\
Ireland:CO TYRONE:County Tyrone|\
Ireland:CO CAVAN:County Cavan|\
Ireland:CO DONEGAL:County Donegal|\
Ireland:CO MONAGHAN:County Monaghan|\
Ireland:CO DUBLIN:County Dublin|\
Ireland:CO CARLOW:County Carlow|\
Ireland:CO KILDARE:County Kildare|\
Ireland:CO KILKENNY:County Kilkenny|\
Ireland:CO LAOIS:County Laois|\
Ireland:CO LONGFORD:County Longford|\
Ireland:CO LOUTH:County Louth|\
Ireland:CO MEATH:County Meath|\
Ireland:CO OFFALY:County Offaly|\
Ireland:CO WESTMEATH:County Westmeath|\
Ireland:CO WEXFORD:County Wexford|\
Ireland:CO WICKLOW:County Wicklow|\
Ireland:CO GALWAY:County Galway|\
Ireland:CO MAYO:County Mayo|\
Ireland:CO LEITRIM:County Leitrim|\
Ireland:CO ROSCOMMON:County Roscommon|\
Ireland:CO SLIGO:County Sligo|\
Ireland:CO CLARE:County Clare|\
Ireland:CO CORK:County Cork|\
Ireland:CO KERRY:County Kerry|\
Ireland:CO LIMERICK:County Limerick|\
Ireland:CO TIPPERARY:County Tipperary|\
Ireland:CO WATERFORD:County Waterford|\
United Kingdom:	Aberdeen	:	Aberdeen	|\
United Kingdom:	Aberdeenshire	:	Aberdeenshire	|\
United Kingdom:	Anglesey	:	Anglesey	|\
United Kingdom:	Angus	:	Angus	|\
United Kingdom:	Argyll and Bute	:	Argyll and Bute	|\
United Kingdom:	Bedfordshire	:	Bedfordshire	|\
United Kingdom:	Berkshire	:	Berkshire	|\
United Kingdom:	Blaenau Gwent	:	Blaenau Gwent	|\
United Kingdom:	Bridgend	:	Bridgend	|\
United Kingdom:	Buckinghamshire	:	Buckinghamshire	|\
United Kingdom:	Caerphilly	:	Caerphilly	|\
United Kingdom:	Cambridgeshire	:	Cambridgeshire	|\
United Kingdom:	Cardiff	:	Cardiff	|\
United Kingdom:	Carmarthenshire	:	Carmarthenshire	|\
United Kingdom:	Cerdigion	:	Cerdigion	|\
United Kingdom:	Cheshire	:	Cheshire	|\
United Kingdom:	Clackmannanshire	:	Clackmannanshire	|\
United Kingdom:	Conwy	:	Conwy	|\
United Kingdom:	Cornwall	:	Cornwall	|\
United Kingdom:	Cumbria	:	Cumbria	|\
United Kingdom:	Denbighshire	:	Denbighshire	|\
United Kingdom:	Derbyshire	:	Derbyshire	|\
United Kingdom:	Devon	:	Devon	|\
United Kingdom:	Dorset	:	Dorset	|\
United Kingdom:	Dumfries and Galloway	:	Dumfries and Galloway	|\
United Kingdom:	Durham	:	Durham	|\
United Kingdom:	East Ayrshire	:	East Ayrshire	|\
United Kingdom:	East Dunbartonshire	:	East Dunbartonshire	|\
United Kingdom:	East Lothian	:	East Lothian	|\
United Kingdom:	East Riding	:	East Riding	|\
United Kingdom:	East Sussex	:	East Sussex	|\
United Kingdom:	East Yorkshire	:	East Yorkshire	|\
United Kingdom:	Edinburgh	:	Edinburgh	|\
United Kingdom:	Eilean Siar	:	Eilean Siar	|\
United Kingdom:	Essex	:	Essex	|\
United Kingdom:	Falkirk	:	Falkirk	|\
United Kingdom:	Fife	:	Fife	|\
United Kingdom:	Flintshire	:	Flintshire	|\
United Kingdom:	Glasgow	:	Glasgow	|\
United Kingdom:	Gloucestershire	:	Gloucestershire	|\
United Kingdom:	Greater London	:	Greater London	|\
United Kingdom:	Gwynedd 	:	Gwynedd 	|\
United Kingdom:	Hampshire	:	Hampshire	|\
United Kingdom:	Hertfordshire	:	Hertfordshire	|\
United Kingdom:	Highland	:	Highland	|\
United Kingdom:	Huntingdonshire	:	Huntingdonshire	|\
United Kingdom:	Inverclyde	:	Inverclyde	|\
United Kingdom:	Kent	:	Kent	|\
United Kingdom:	Lancashire	:	Lancashire	|\
United Kingdom:	Leicestershire	:	Leicestershire	|\
United Kingdom:	Lincolnshire	:	Lincolnshire	|\
United Kingdom:	Merseyside	:	Merseyside	|\
United Kingdom:	Merthyr Tydfil	:	Merthyr Tydfil	|\
United Kingdom:	Middlesex	:	Middlesex	|\
United Kingdom:	Midlothian	:	Midlothian	|\
United Kingdom:	Monmouthshire	:	Monmouthshire	|\
United Kingdom:	Moray	:	Moray	|\
United Kingdom:	Neath Port Talbot	:	Neath Port Talbot	|\
United Kingdom:	Newport	:	Newport	|\
United Kingdom:	Norfolk	:	Norfolk	|\
United Kingdom:	North Ayrshire	:	North Ayrshire	|\
United Kingdom:	North Lanarkshire	:	North Lanarkshire	|\
United Kingdom:	Northamptonshire	:	Northamptonshire	|\
United Kingdom:	Northumberland	:	Northumberland	|\
United Kingdom:	Nottinghamshire	:	Nottinghamshire	|\
United Kingdom:	Orkney Islands	:	Orkney Islands	|\
United Kingdom:	Oxfordshire	:	Oxfordshire	|\
United Kingdom:	Pembrokeshire	:	Pembrokeshire	|\
United Kingdom:	Perth and Kinross	:	Perth and Kinross	|\
United Kingdom:	Powys	:	Powys	|\
United Kingdom:	Renfrewshire	:	Renfrewshire	|\
United Kingdom:	Rhondda Cynon Taff	:	Rhondda Cynon Taff	|\
United Kingdom:	Rutland	:	Rutland	|\
United Kingdom:	Shetland Islands	:	Shetland Islands	|\
United Kingdom:	Shropshire	:	Shropshire	|\
United Kingdom:	Somerset	:	Somerset	|\
United Kingdom:	South Ayrshire	:	South Ayrshire	|\
United Kingdom:	South Lanarkshire	:	South Lanarkshire	|\
United Kingdom:	South Yorkshire	:	South Yorkshire	|\
United Kingdom:	Staffordshire	:	Staffordshire	|\
United Kingdom:	Stirling	:	Stirling	|\
United Kingdom:	Suffolk	:	Suffolk	|\
United Kingdom:	Surrey	:	Surrey	|\
United Kingdom:	Swansea	:	Swansea	|\
United Kingdom:	The Scottish Borders	:	The Scottish Borders	|\
United Kingdom:	The Vale of Glamorgan	:	The Vale of Glamorgan	|\
United Kingdom:	Torfaen	:	Torfaen	|\
United Kingdom:	Tyne and Wear	:	Tyne and Wear	|\
United Kingdom:	Warwickshire	:	Warwickshire	|\
United Kingdom:	West Dunbartonshire	:	West Dunbartonshire	|\
United Kingdom:	West Lothian	:	West Lothian	|\
United Kingdom:	West Sussex	:	West Sussex	|\
United Kingdom:	West Yorkshire 	:	West Yorkshire 	|\
United Kingdom:	Westmorland	:	Westmorland	|\
United Kingdom:	Wiltshire	:	Wiltshire	|\
United Kingdom:	Worcestershire	:	Worcestershire	|\
United Kingdom:	Wrexham	:	Wrexham	|\
';

// 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:United States|\
Afghanistan:Afghanistan|\
Albania:Albania|\
Algeria:Algeria|\
American Samoa:American Samoa|\
Andorra:Andorra|\
Angola:Angola|\
Anguilla:Anguilla|\
Antigua and Barbuda:Antigua and Barbuda|\
Argentina:Argentina|\
Armenia:Armenia|\
Aruba:Aruba|\
Australia:Australia|\
Austria:Austria|\
Azerbaijan:Azerbaijan|\
Azores:Azores|\
Bahamas:Bahamas|\
Bahrain:Bahrain|\
Bangladesh:Bangladesh|\
Barbados:Barbados|\
Belarus:Belarus|\
Belgium:Belgium|\
Belize:Belize|\
Benin:Benin|\
Bermuda:Bermuda|\
Bhutan:Bhutan|\
Bolivia:Bolivia|\
Bosnia And Herzegowina:Bosnia And Herzegowina|\
Bosnia-Herzegovina:Bosnia-Herzegovina|\
Botswana:Botswana|\
Bouvet Island:Bouvet Island|\
Brazil:Brazil|\
British Indian Ocean Territory:British Indian Ocean Territory|\
British Virgin Islands:British Virgin Islands|\
Brunei Darussalam:Brunei Darussalam|\
Bulgaria:Bulgaria|\
Burkina Faso:Burkina Faso|\
Burundi:Burundi|\
Cambodia:Cambodia|\
Cameroon:Cameroon|\
Canada:Canada|\
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|\
Cook Islands:Cook Islands|\
Corsica:Corsica|\
Costa Rica:Costa Rica|\
Ivory Coast:Cote d` Ivoire (Ivory Coast)|\
Croatia:Croatia|\
Cuba:Cuba|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic|\
Denmark:Denmark|\
Djibouti:Djibouti|\
Dominica:Dominica|\
Dominican Republic:Dominican Republic|\
East Timor:East Timor|\
Egypt:Egypt|\
El Salvador:El Salvador|\
Equatorial Guinea:Equatorial Guinea|\
Eritrea:Eritrea|\
Estonia:Estonia|\
Ethiopia:Ethiopia|\
Faroe Islands:Faroe Islands|\
Fiji:Fiji|\
Finland:Finland|\
France:France|\
Gabon:Gabon|\
Gambia:Gambia|\
Georgia:Georgia|\
Germany:Germany|\
Ghana:Ghana|\
Gibraltar:Gibraltar|\
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|\
Iran:Iran|\
Iraq:Iraq|\
Ireland:Ireland|\
Ireland (Eire)Ireland:Ireland (Eire)|\
Israel:Israel|\
Italy:Italy|\
Jamaica:Jamaica|\
Japan:Japan|\
Jordan:Jordan|\
Kazakhstan:Kazakhstan|\
Kenya:Kenya|\
Kiribati:Kiribati|\
Korea:Korea|\
Kuwait:Kuwait|\
Kyrgyzstan:Kyrgyzstan|\
Laos:Laos|\
Latvia:Latvia|\
Lebanon:Lebanon|\
Lesotho:Lesotho|\
Liberia:Liberia|\
Libya:Libya|\
Liechtenstein:Liechtenstein|\
Lithuania:Lithuania|\
Luxembourg:Luxembourg|\
Macao:Macao|\
Macedonia:Macedonia|\
Madagascar:Madagascar|\
Madeira Islands:Madeira Islands|\
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:Micronesia|\
Moldova:Moldova|\
Monaco:Monaco|\
Mongolia:Mongolia|\
Montserrat:Montserrat|\
Morocco:Morocco|\
Mozambique:Mozambique|\
Myanmar (Burma):Myanmar (Burma)|\
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 Mariana Islands:Northern Mariana Islands|\
Norway:Norway|\
Oman:Oman|\
Pakistan:Pakistan|\
Palau:Palau|\
Palestinian Territory:Palestinian Territory|\
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|\
San Marino:San Marino|\
Sao Tome and Principe:Sao Tome and Principe|\
Saudi Arabia:Saudi Arabia|\
Senegal:Senegal|\
Serbia-Montenegro:Serbia-Montenegro|\
Seychelles:Seychelles|\
Sierra Leone:Sierra Leone|\
Singapore:Singapore|\
Slovak Republic:Slovak Republic|\
Slovenia:Slovenia|\
Solomon Islands:Solomon Islands|\
Somalia:Somalia|\
South Africa:South Africa|\
South Georgia And The South Sand:South Georgia And The South Sand|\
South Korea:South Korea|\
Spain:Spain|\
Sri Lanka:Sri Lanka|\
St. Christopher and Nevis:St. Christopher and Nevis|\
St. Helena:St. Helena|\
St. Lucia:St. Lucia|\
St. Pierre and Miquelon:St. Pierre and Miquelon|\
St. Vincent and the Grenadines:St. Vincent and the Grenadines|\
Sudan:Sudan|\
Svalbard And Jan Mayen Islands:Svalbard And Jan Mayen Islands|\
Swaziland:Swaziland|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Syrian Arab Republic:Syrian Arab Republic|\
Taiwan:Taiwan|\
Tajikistan:Tajikistan|\
Tanzania:Tanzania|\
Thailand:Thailand|\
Togo:Togo|\
Tokelau:Tokelau|\
Tonga:Tonga|\
Trinidad and Tobago:Trinidad and Tobago|\
Tristan da Cunha:Tristan da Cunha|\
Tunisia:Tunisia|\
Turkey:Turkey|\
Turkmenistan:Turkmenistan|\
Turks and Caicos IslandsC:Turks and Caicos Islands|\
Tuvalu:Tuvalu|\
Uganda:Uganda|\
Ukraine:Ukraine|\
United Arab Emirates:United Arab Emirates|\
United Kingdom:United Kingdom|\
United States Minor Outlying Isl:United States Minor Outlying Isl|\
Uruguay:Uruguay|\
Uzbekistan:Uzbekistan|\
Vanuatu:Vanuatu|\
Vatican City:Vatican City|\
Venezuela:Venezuela|\
Vietnam:Vietnam|\
Virgin Islands (U.S.):Virgin Islands (U.S.)|\
Wallis and Furuna Islands:Wallis and Furuna Islands|\
Western Sahara:Western Sahara|\
Western Samoa:Western Samoa|\
Yemen:Yemen|\
Yugoslavia:Yugoslavia|\
Zaire:Zaire|\
Zambia:Zambia|\
Zimbabwe:Zimbabwe\
';

// 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
	  sInString = CleanInput(sInString); // clean for bad data
      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", 10); 
      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 = CleanInput(defaultCountry);
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
	  defaultState = CleanInput(defaultState);
      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];
	  	countryIdName  = CleanInput(countryIdName);
      stateIdName    = stateFieldCfgArray[loop];
	  	stateIdName  = CleanInput(stateIdName);

      // Read the default value hidden fields
	  
      defaultCountry = document.getElementById( countryDefaultCfgArray[loop] ).value;
		defaultCountry   = CleanInput(defaultCountry);
      defaultState   = document.getElementById( stateDefaultCfgArray[loop] ).value;
		defaultState   = CleanInput(defaultState);	  

      populateCountry( countryIdName);
      populateState( stateIdName, countryIdName );
   }
}
//
      function CleanInput(s) {
      return s.replace(/\\&/g, '&amp;').replace(/\\</g, '&lt;').replace(/\\>/g, '&gt;').replace(/\\t/g, '&nbsp;&nbsp;&nbsp;').replace(/\\n/g, '<br />').replace(/%20/g, " ").replace(/http/gi, " ").replace(/%3E/gi, " ").replace(/3E/gi, " ").replace(/3C/gi, " ").replace(/;/gi, " ").replace(/\(/gi, " ").replace(/\;/gi, " ").replace(/\|/gi, " ");
      }
	  

