// JavaScript Document


/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Down Home Consulting :: http://downhomeconsulting.com */

/*
Country State Drop Downs v1.0.
(c) Copyright 2005 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.

*/

// If you have PHP you can set the post values like this
//var postState = '<?= $_POST["bstate"] ?>';
//var postCountry = '<?= $_POST["bcountry"] ?>';
var postState = '';
var postCountry = '';

// State table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var bstate = '\
United States:ALABAMA:AL|\
United States:ALASKA:AK|\
United States:AMERICAN SAMOA:AS|\
United States:ARIZONA:AZ|\
United States:ARKANSAS:AR|\
United States:ARMED FORCES AM:AA|\
United States:ARMED FORCES EU:AE|\
United States:ARMED FORCES PA:AP|\
United States:CALIFORNIA:CA|\
United States:COLORADO:CO|\
United States:CONNECTICUT:CT|\
United States:DELAWARE:DE|\
United States:DISTRICT OF COL:DC|\
United States:FLORIDA:FL|\
United States:GEORGIA:GA|\
United States:GUAM:GU|\
United States:HAWAII:HI|\
United States:IDAHO:ID|\
United States:ILLINOIS:IL|\
United States:INDIANA:IN|\
United States:IOWA:IA|\
United States:KANSAS:KS|\
United States:KENTUCKY:KY|\
United States:LOUISIANA:LA|\
United States:MAINE:ME|\
United States:MARYLAND:MD|\
United States:MASSACHUSETTS:MA|\
United States:MICHIGAN:MI|\
United States:MINNESOTA:MN|\
United States:MISSISSIPPI:MS|\
United States:MISSOURI:MO|\
United States:MONTANA:MT|\
United States:NEBRASKA:NE|\
United States:NEVADA:NV|\
United States:NEW HAMPSHIRE:NH|\
United States:NEW JERSEY:NJ|\
United States:NEW MEXICO:NM|\
United States:NEW YORK:NY|\
United States:NORTH CAROLINA:NC|\
United States:NORTH DAKOTA:ND|\
United States:NOTHERN MARIANA:MP|\
United States:OHIO:OH|\
United States:OKLAHOMA:OK|\
United States:OREGON:OR|\
United States:PENNSYLVANIA:PA|\
United States:PUERTO RICO:PR|\
United States:RHODE ISLAND:RI|\
United States:SOUTH CAROLINA:SC|\
United States:SOUTH DAKOTA:SD|\
United States:TENNESSEE:TN|\
United States:TEXAS:TX|\
United States:UTAH:UT|\
United States:VERMONT:VT|\
United States:VIRGIN ISLANDS:VI|\
United States:VIRGINIA:VA|\
United States:WASHINGTON:WA|\
United States:WEST VIRGINIA:WV|\
United States:WISCONSIN:WI|\
United States:WYOMING:WY|\
Canada:ALBERTA:AB|\
Canada:BRITISH COLUMBIA:BC|\
Canada:MANITOBA:MB|\
Canada:NEW BRUNSWICK:NB|\
Canada:NEWFOUNDLAND:NL|\
Canada:NORTHWEST TERRITORIES:NT|\
Canada:NOVA SCOTIA:NS|\
Canada:NUNAVUT:NU|\
Canada:ONTARIO:ON|\
Canada:PRINCE EDWARD ISLAND:PE|\
Canada:QUEBEC:QC|\
Canada:SASKATCHEWAN:SK|\
Canada:YUKON:YT|\
';

// Country data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var bcountry = '\
United States:United States';/*|\
Afghanistan:Afghanistan|\
Andorra:Andorra|\
Anguilla:Anguilla|\
Argentina:Argentina|\
Armenia:Armenia|\
Aruba:Aruba|\
Australia:Australia|\
Austria:Austria|\
Bahamas:Bahamas|\
Bahrain:Bahrain|\
Barbados:Barbados|\
Belarus:Belarus|\
Belgium:Belgium|\
Belize:Belize|\
Bermuda:Bermuda|\
Bolivia:Bolivia|\
Brazil:Brazil|\
Brunei Darussalam:Brunei Darussalam|\
Canada:Canada|\
Cayman Islands:Cayman Islands|\
Chile:Chile|\
China:China|\
Colombia:Colombia|\
Costa Rica:Costa Rica|\
Croatia:Croatia|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic|\
Denmark:Denmark|\
Dominica:Dominica|\
Dominican Republic:Dominican Republic|\
Ecuador:Ecuador|\
El Salvador:El Salvador|\
Falkland Islands:Falkland Islands|\
Fiji:Fiji|\
Finland:Finland|\
France:France|\
French Polynesia:French Polynesia|\
Germany:Germany|\
Gibraltar:Gibraltar|\
Greece:Greece|\
Grenada:Grenada|\
Guatemala:Guatemala|\
Honduras:Honduras|\
Hong Kong:Hong Kong|\
Hungary:Hungary|\
Iceland:Iceland|\
India:India|\
Iraq:Iraq|\
Ireland:Ireland|\
Israel:Israel|\
Italy:Italy|\
Jamaica:Jamaica|\
Japan:Japan|\
Jordan:Jordan|\
Kenya:Kenya|\
Korea - South:Korea - South|\
Kuwait:Kuwait|\
Latvia:Latvia|\
Liechtenstein:Liechtenstein|\
Luxembourg:Luxembourg|\
Malaysia:Malaysia|\
Malta:Malta|\
Marshall Islands:Marshall Islands|\
Martinique:Martinique|\
Mauritius:Mauritius|\
Mexico:Mexico|\
Monaco:Monaco|\
Mongolia:Mongolia|\
Netherlands:Netherlands|\
New Caledonia:New Caledonia|\
New Zealand:New Zealand|\
Norway:Norway|\
Oman:Oman|\
Panama:Panama|\
Papua New Guinea:Papua New Guinea|\
Paraguay:Paraguay|\
Peru:Peru|\
Poland:Poland|\
Portugal:Portugal|\
Qatar:Qatar|\
Saudi Arabia:Saudi Arabia|\
Singapore:Singapore|\
South Africa:South Africa|\
Spain:Spain|\
Sweden:Sweden|\
Switzerland:Switzerland|\
Taiwan:Taiwan|\
Thailand:Thailand|\
Trinidad And Tobago:Trinidad And Tobago|\
Turkey:Turkey|\
United Arab Emirates:United Arab Emirates|\
United Kingdom:United Kingdom|\
Uruguay:Uruguay|\
';*/

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the bcountry selected with the counties from the bcountry list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = bcountry.split('|');  // Split into lines
  var selObj = document.getElementById('countrySelect');
  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() {
  var selObj = document.getElementById('stateSelect');
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Select State','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected bcountry
  var stateLineArray = bstate.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('countrySelect').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","bstate");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        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 == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the bcountry has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "bstate");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}

function initCountry(bcountry) {
  populateCountry(bcountry);
  populateState();
}