<!-- Paste this code into an external JavaScript file  -->

/* 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["state"] ?>';
//var postCountry = '<?= $_POST["country"] ?>';
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 state = '\
United Kingdom:--- England/East Anglia ALL:--- England/East Anglia ALL|\
United Kingdom:Cambridgeshire:Cambridgeshire|\
United Kingdom:Norfolk:Norfolk|\
United Kingdom:Suffolk:Suffolk|\
United Kingdom:--- England/Midlands ALL:--- England/Midlands ALL|\
United Kingdom:Birmingham & West-Midlands:Birmingham & West-Midlands|\
United Kingdom:Derbyshire:Derbyshire|\
United Kingdom:Herefordshire:Herefordshire|\
United Kingdom:Leicestershire:Leicestershire|\
United Kingdom:Lincolnshire:Lincolnshire|\
United Kingdom:Northamptonshire:Northamptonshire|\
United Kingdom:Nottinghamshire:Nottinghamshire|\
United Kingdom:Rutland:Rutland|\
United Kingdom:Shropshire:Shropshire|\
United Kingdom:Staffordshire:Staffordshire|\
United Kingdom:Warwickshire:Warwickshire|\
United Kingdom:Worcestershire:Worcestershire|\
United Kingdom:--- England/North East ALL:--- England/North East ALL|\
United Kingdom:Durham:Durham|\
United Kingdom:East Riding:East Riding|\
United Kingdom:North Yorkshire:North Yorkshire|\
United Kingdom:Northumberland:Northumberland|\
United Kingdom:South Yorkshire:South Yorkshire|\
United Kingdom:Teesside:Teesside|\
United Kingdom:Tyneside:Tyneside|\
United Kingdom:West Yorkshire:West Yorkshire|\
United Kingdom:--- England/North West ALL:--- England/North West ALL|\
United Kingdom:Cheshire:Cheshire|\
United Kingdom:Cumbria:Cumbria|\
United Kingdom:Greater Manchester:Greater Manchester|\
United Kingdom:Lancashire:Lancashire|\
United Kingdom:Merseyside:Merseyside|\
United Kingdom:--- England/London All:--- England/London All|\
United Kingdom:Central/West End:Central/West End|\
United Kingdom:City:City|\
United Kingdom:East London:East London|\
United Kingdom:North London:North London|\
United Kingdom:North West London:North West London|\
United Kingdom:South East London:South East London|\
United Kingdom:South West London:South West London|\
United Kingdom:West London:West London|\
United Kingdom:--- England/South ALL:--- England/South ALL|\
United Kingdom:Bedfordshire:Bedfordshire|\
United Kingdom:Berkshire:Berkshire|\
United Kingdom:Buckinghamshire:Buckinghamshire|\
United Kingdom:Hampshire:Hampshire|\
United Kingdom:Hertfordshire:Hertfordshire|\
United Kingdom:Isle of Wight:Isle of Wight|\
United Kingdom:Oxfordshire:Oxfordshire|\
United Kingdom:--- England/South East ALL:--- England/South East ALL|\
United Kingdom:East Sussex:East Sussex|\
United Kingdom:Essex:Essex|\
United Kingdom:Kent:Kent|\
United Kingdom:Surrey:Surrey|\
United Kingdom:West Sussex:West Sussex|\
United Kingdom:--- England/South West ALL:--- England/South West ALL|\
United Kingdom:Bristol Area:Bristol Area|\
United Kingdom:Cornwall:Cornwall|\
United Kingdom:Devon:Devon|\
United Kingdom:Dorset:Dorset|\
United Kingdom:Gloucestershire:Gloucestershire|\
United Kingdom:Isles of Scilly:Isles of Scilly|\
United Kingdom:Jersey:Jersey|\
United Kingdom:Somerset:Somerset|\
United Kingdom:Wiltshire:Wiltshire|\
United Kingdom:--- Northern Ireland ALL:--- Northern Ireland ALL|\
United Kingdom:Antrim-Belfast:Antrim-Belfast|\
United Kingdom:Armagh:Armagh|\
United Kingdom:Down:Down|\
United Kingdom:Fermanagh:Fermanagh|\
United Kingdom:Londonderry:Londonderry|\
United Kingdom:Tyrone:Tyrone|\
United Kingdom:--- Scotland ALL:--- Scotland ALL|\
United Kingdom:Aberdeenshire:Aberdeenshire|\
United Kingdom:Angus:Angus|\
United Kingdom:Ayrshire:Ayrshire|\
United Kingdom:Borders:Borders|\
United Kingdom:Central:Central|\
United Kingdom:Dumfries and Galloway Area:Dumfries and Galloway Area|\
United Kingdom:Edinburgh and Lothians Area:Edinburgh and Lothians Area|\
United Kingdom:Fife:Fife|\
United Kingdom:Glasgow Area:Glasgow Area|\
United Kingdom:Highlands and Islands Area:Highlands and Islands Area|\
United Kingdom:Lanarkshire:Lanarkshire|\
United Kingdom:Moray:Moray|\
United Kingdom:Perth and Kinross Area:Perth and Kinross Area|\
United Kingdom:Stirling:Stirling|\
United Kingdom:--- Wales ALL:--- Wales ALL|\
United Kingdom:Cardiff:Cardiff|\
United Kingdom:Carmarthenshire:Carmarthenshire|\
United Kingdom:Ceredigion:Ceredigion|\
United Kingdom:Conwy:Conwy|\
United Kingdom:Denbighshire:Denbighshire|\
United Kingdom:Flintshire:Flintshire|\
United Kingdom:Gwynedd:Gwynedd|\
United Kingdom:Isle of Anglesey:Isle of Anglesey|\
United Kingdom:Monmouthshire:Monmouthshire|\
United Kingdom:Pembrokeshire:Pembrokeshire|\
United Kingdom:Powys:Powys|\
United Kingdom:South Wales:South Wales|\
United Kingdom:Wrexham:Wrexham|\
United States:Alabama:Alabama|\
United States:Alaska:Alaska|\
United States:Arizona:Arizona|\
United States:Arkansas:Arkansas|\
United States:California:California|\
United States:Colorado:Colorado|\
United States:Connecticut:Connecticut|\
United States:Delaware:Delaware|\
United States:Florida:Florida|\
United States:Georgia:Georgia|\
United States:Hawaii:Hawaii|\
United States:Idaho State:Idaho State|\
United States:Illinois:Illinois|\
United States:Indiana:Indiana|\
United States:Iowa:Iowa|\
United States:Kansas:Kansas|\
United States:Kentucky:Kentucky|\
United States:Louisiana:Louisiana|\
United States:Maine:Maine|\
United States:Maryland:Maryland|\
United States:Massachusetts:Massachusetts|\
United States:Michigan:Michigan|\
United States:Minnesota:Minnesota|\
United States:Mississippi:Mississippi|\
United States:Missouri:Missouri|\
United States:Montana:Montana|\
United States:Nebraska:Nebraska|\
United States:Nevada:Nevada|\
United States:New Hampshire:New Hampshire|\
United States:New Jersey:New Jersey|\
United States:New Mexico:New Mexico|\
United States:New York:New York|\
United States:North Carolina:North Carolina|\
United States:North Dakota:North Dakota|\
United States:Ohio:Ohio|\
United States:Oklahoma:Oklahoma|\
United States:Oregon:Oregon|\
United States:Pennsylvania:Pennsylvania|\
United States:Rhode Island:Rhode Island|\
United States:South Carolina:South Carolina|\
United States:South Dakota:South Dakota|\
United States:Tennessee:Tennessee|\
United States:Texas:Texas|\
United States:Utah:Utah|\
United States:Vermont:Vermont|\
United States:Virginia:Virginia|\
United States:Washington:Washington|\
United States:West Virginia:West Virginia|\
United States:Wisconsin:Wisconsin|\
United States:Wyoming Capital:Wyoming Capital|\
Australia:New South Wales:South Wales|\
Australia:Victoria:Victoria|\
Australia:Queensland:Queensland|\
Australia:South Australia:South Australia|\
Australia:Western Australia:Western Australia|\
Australia:Tasmania:Tasmania|\
Australia:Northern Territory:Northern Territory|\
';

// 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 country = '\
Albania:Albania:|\
Andorra:Andorra:|\
Australia:Australia:|\
Austria:Austria:|\
Bahrain:Bahrain|\
Belarus:Belarus:|\
Belgium:Belgium:|\
Bosnia:Bosnia:|\
Bulgaria:Bulgaria:|\
Croatia:Croatia:|\
Cyprus:Cyprus|\
Czech Republic:Czech Republic:|\
Denmark:Denmark:|\
Estonia:Estonia:|\
Faroe Island:Faroe Island:|\
Finland:Finland:|\
France:France:|\
Germany:Germany:|\
Gibraltar:Gibraltar:|\
Greece:Greece:|\
Guernsey:Guernsey:|\
Hungary:Hungary:|\
Iceland:Iceland:|\
Iran:Iran|\
Iraq:Iraq|\
Ireland:Ireland:|\
Isle of Man:Isle of Man:|\
Israel:Israel|\
Italy:Italy:|\
Jersey:Jersey:|\
Jordan:Jordan|\
Kuwait:Kuwait|\
Latvia:Latvia:|\
Lebanon:Lebanon|\
Liechtenstein:Liechtenstein:|\
Lithuania:Lithuania:|\
Luxembourg:Luxembourg:|\
Macedonia:Macedonia:|\
Malta:Malta:|\
Moldova:Moldova:|\
Monaco:Monaco:|\
Montenegro:Montenegro:|\
Netherlands:Netherlands:|\
Norway:Norway:|\
Oman:Oman|\
Poland:Poland:|\
Portugal:Portugal:|\
Qatar:Qatar|\
Romania:Romania:|\
Russia:Russia:|\
San Marino:San Marino:|\
Saudi Arabia:Saudi Arabia|\
Serbia:Serbia:|\
Slovakia:Slovakia:|\
South Africa:South Africa|\
Spain:Spain:|\
Sweden:Sweden:|\
Switzerland:Switzerland:|\
Syria:Syria|\
Turkey:Turkey:|\
Ukraine:Ukraine:|\
United Arab Emirates:United Arab Emirates|\
United Kingdom:United Kingdom:|\
United States:United States:|\
Vatican City:Vatican City:|\
Yemen:Yemen|\
';

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

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.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 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('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","state");
        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 country 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", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}

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