
<!-- 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 = 'Avon';
//var postCountry = 'England';

// 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 = '\
England:Avon:Avon|\
England:Berkshire:Berkshire|\
England:Bristol:Bristol|\
England:Buckinghamshire:Buckinghamshire|\
England:Bedfordshire:Bedfordshire|\
England:Berkshire:Berkshire|\
England:Bristol:Bristol|\
England:Buckinghamshire:Buckinghamshire|\
England:Cambridgeshire:Cambridgeshire|\
England:Cheshire:Cheshire|\
England:Cornwall:Cornwall|\
England:County Durham:County Durham|\
England:Cumbria:Cumbria|\
England:Derbyshire:Derbyshire|\
England:Devon:Devon|\
England:Dorset:Dorset|\
England:East Yorkshire:East Yorkshire|\
England:East Sussex:East Sussex|\
England:Essex:Essex|\
England:Gloucestershire:Gloucestershire|\
England:Hampshire:Hampshire|\
England:Herefordshire:Herefordshire|\
England:Hertfordshire:Hertfordshire|\
England:Humberside:Humberside|\
England:Isle of Wight:Isle of Wight|\
England:Kent:Kent|\
England:Lancashire:Lancashire|\
England:Leicestershire:Leicestershire|\
England:Lincolnshire:Lincolnshire|\
England:London:London|\
England:Manchester:Manchester|\
England:Merseyside:Merseyside|\
England:Middlesex:Middlesex|\
England:Norfolk:Norfolk|\
England:North Yorkshire:North Yorkshire|\
England:Northamptonshire:Northamptonshire|\
England:Northumberland:Northumberland|\
England:Nottinghamshire:Nottinghamshire|\
England:Oxfordshire:Oxfordshire|\
England:Rutland:Rutland|\
England:Shropshire:Shropshire|\
England:Somerset:Somerset|\
England:South Yorkshire:South Yorkshire|\
England:Staffordshire:Staffordshire|\
England:Suffolk:Suffolk|\
England:Surrey:Surrey|\
England:Tyne & Wear:Tyne & Wear|\
England:Warwickshire:Warwickshire|\
England:West Midlands:West Midlands|\
England:West Sussex:West Sussex|\
England:West Yorkshire:West Yorkshire|\
England:Wiltshire:Wiltshire|\
England:Worcestershire:Worcestershire|\
Scotland:Aberdeen:Aberdeen|\
Scotland:Aberdeenshire:Aberdeenshire|\
Scotland:Angus:Angus|\
Scotland:Argyll:Argyll|\
Scotland:Borders:Borders|\
Scotland:Clackmannanshire:Clackmannanshire|\
Scotland:Dumfries & Galloway:Dumfries & Galloway|\
Scotland:Dundee:Dundee|\
Scotland:East Ayrshire:East Ayrshire|\
Scotland:East Dunbartonshire:East Dunbartonshire|\
Scotland:East Lothian:East Lothian|\
Scotland:East Renfrewshire:East Renfrewshire|\
Scotland:Edinburgh:Edinburgh|\
Scotland:Falkirk District:Falkirk District|\
Scotland:Fife:Fife|\
Scotland:Glasgow:Glasgow|\
Scotland:Highland:Highland|\
Scotland:Inverclyde:Inverclyde|\
Scotland:Inverness:Inverness|\
Scotland:Midlothian:Midlothian|\
Scotland:Moray:Moray|\
Scotland:North Ayrshire:North Ayrshire|\
Scotland:Orkney:Orkney|\
Scotland:Perth & Kinross:Perth & Kinross|\
Scotland:Renfrewshire:Renfrewshire|\
Scotland:Shetland:Shetland|\
Scotland:South Ayrshire:South Ayrshire|\
Scotland:South Lanarkshire:South Lanarkshire|\
Scotland:Stirling District:Stirling District|\
Scotland:West Dunbartonshire:West Dunbartonshire|\
Wales:Blaenau Gwent:Blaenau Gwent|\
Wales:Bridgend:Bridgend|\
Wales:Caerphilly:Caerphilly|\
Wales:Cardiff:Cardiff|\
Wales:Carmarthenshire:Carmarthenshire|\
Wales:Ceredigion:Ceredigion|\
Wales:Conwy:Conwy|\
Wales:Denbighshire:Denbighshire|\
Wales:Flintshire:Flintshire|\
Wales:Gwynedd:Gwynedd|\
Wales:Isle of Anglesey:Isle of Anglesey|\
Wales:Merthyr Tydfil:Merthyr Tydfil|\
Wales:Monmouthshire:Monmouthshire|\
Wales:Neath Port Talbot:Neath Port Talbot|\
Wales:Newport:Newport|\
Wales:Pembrokeshire:Pembrokeshire|\
Wales:Powys:Powys|\
Wales:Rhondda Cynon Taff:Rhondda Cynon Taff|\
Wales:Swansea:Swansea|\
Wales:Torfaen:Torfaen|\
Wales:Vale of Glamorgan:Vale of Glamorgan|\
Wales:Wrexham:Wrexham|\
Wales:West Lothian:West Lothian|\
Wales:Western Isles:Western Isles|\
';

// 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 = '\
England:England|\
Scotland:Scotland|\
Wales:Wales|\
Outside UK:Outside UK|\
';

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 Area','');
    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 Area','');
        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();
}
