// JavaScript Document
<!-- 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 = '\
King:Auburn 408:Auburn 408|\
King:Bellevue 405:Bellevue 405|\
King:Enumclaw 216:Enumclaw 216|\
King:Federal Way 210:Federal Way 210|\
King:Highline 401:Highline 401|\
King:Issaquah 411:Issaquah 411|\
King:Kent 415:Kent 415|\
King:Lake Washington 414:Lake Washington 414|\
King:Mercer Island 400:Mercer Island 400|\
King:Northshore 417:Northshore 417|\
King:Renton 403:Renton 403|\
King:Riverview Special Services:Riverview Special Services|\
King:Seattle 1:Seattle 1|\
King:Shoreline 412:Shoreline 412|\
King:Skykomish 404:Skykomish 404|\
King:Snoqualmie 410:Snoqualmie 410|\
King:Tahoma 409:Tahoma 409|\
King:Tukwila 406:Tukwila 406|\
King:Vashon Island 402:Vashon Island 402|\
Pierce:Bethel 403:Bethel 403|\
Pierce:Clover Park 400:Clover Park 400|\
Pierce:Dieringer 343:Dieringer 343|\
Pierce:Eatonville 404:Eatonville 404|\
Pierce:Fife 417:Fife 417|\
Pierce:Franklin Pierce 402:Franklin Pierce 402|\
Pierce:Orting 344:Orting 344|\
Pierce:Peninsula 401:Peninsula 401|\
Pierce:Puyallup 3:Puyallup 3|\
Pierce:Sumner 320:Sumner 320|\
Pierce:Tacoma 10:Tacoma 10|\
Pierce:University Place 83:University Place 83|\
Pierce:White River 416:White River 416|\
Snohomish:Arlington 16:Arlington 16|\
Snohomish:Darrington 330:Darrington 330|\
Snohomish:Edmonds 15:Edmonds 15|\
Snohomish:Everett 2:Everett 2|\
Snohomish:Granite Falls 332:Granite Falls 332|\
Snohomish:Lake Stevens 4:Lake Stevens 4|\
Snohomish:Lakewood 306:Lakewood 306|\
Snohomish:Marysville 25:Marysville 25|\
Snohomish:Monroe 103:Monroe 103|\
Snohomish:Mukilteo 6:Mukilteo 6|\
Snohomish:Snohomish 201:Snohomish 201|\
Snohomish:401:401|\
Snohomish:Sultan Home School:Sultan Home School|\
Thurston:North Thurston 3:North Thurston 3|\
Thurston:Olympia 111:Olympia 111|\
Thurston:Rainier 307:Rainier 307|\
Thurston:Rochester 401:Rochester 401|\
Thurston:Tenino 402:Tenino 402|\
Thurston:Tumwater 33:Tumwater 33|\
Thurston:Yelm Community Schools:Yelm Community Schools|\
';

// 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 = '\
King:King|\
Pierce:Pierce|\
Snohomish:Snohomish|\
Thurston:Thurston|\
Alaska:Alaska|\
Other:Other|\
';

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 County','');
  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 District','');
    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 District','');
        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();
}
