// JavaScript Document
<!--
// Changes list of States in Select when Country is changed

/* 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, fitness 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 postCountry = '<?= $_POST["country"] ?>';
//var postState = '<?= $_POST["state"] ?>';

var postCountry = '';
var postState = '';

// 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.
//LIMIT TO 20 CHARS FOR DB
var country = '\
United States:United States|\
Argentina:Argentina|\
Australia:Australia|\
Belize:Belize|\
Bolivia:Bolivia|\
Brazil:Brazil|\
Canada:Canada|\
Mexico:Mexico|\
Chile:Chile|\
Colombia:Colombia|\
Costa Rica:Costa Rica|\
Ecuador:Ecuador|\
El Salvador:El Salvador|\
Falkland Islands:Falkland Islands|\
French Guiana:French Guiana|\
Guatemala:Guatemala|\
Guyana:Guyana|\
Honduras:Honduras|\
Nicaragua:Nicaragua|\
Panama:Panama|\
Paraguay:Paraguay|\
Peru:Peru|\
Suriname:Suriname|\
Uruguay:Uruguay|\
Venezuela:Venezuela|\
';

// 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 States:AK:Alaska|\
United States:AL:Alabama|\
United States:AR:Arkansas|\
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:GA:Georgia|\
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:MI:Michigan|\
United States:MN:Minnesota|\
United States:MO:Missouri|\
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: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|\
Canada:AB:Alberta|\
Canada:BC:British Columbia|\
Canada:MB:Manitoba|\
Canada:NB:New Brunswick|\
Canada:NL:Newfoundland and Labrador|\
Canada:NS:Nova Scotia|\
Canada:NT:Northwest Territories|\
Canada:NU:Nunavut|\
Canada:ON:Ontario|\
Canada:PE:Prince Edward Island|\
Canada:QC:Quebec|\
Canada:SK:Saskatchewan|\
Canada:YT:Yukon Territory|\
Mexico:Aguascalientes:Aguascalientes|\
Mexico:Baja California:Baja California|\
Mexico:Baja California Sur:Baja California Sur|\
Mexico:Campeche:Campeche|\
Mexico:Chiapas:Chiapas|\
Mexico:Chihuahua:Chihuahua|\
Mexico:Coahuila:Coahuila|\
Mexico:Colima:Colima|\
Mexico:Durango:Durango|\
Mexico:Federal District:Federal District (Mexico City)|\
Mexico:Guanajuato:Guanajuato|\
Mexico:Guerrero:Guerrero|\
Mexico:Hidalgo:Hidalgo|\
Mexico:Jalisco:Jalisco|\
Mexico:Michoacan:Michoacan|\
Mexico:Morelos:Morelos|\
Mexico:Mexico:Mexico|\
Mexico:Nayarit:Nayarit|\
Mexico:Nuevo Leon:Nuevo Leon|\
Mexico:Oaxaca:Oaxaca|\
Mexico:Puebla:Puebla|\
Mexico:Queretaro:Queretaro|\
Mexico:Quintana Roo:Quintana Roo|\
Mexico:San Luis Potosí:San Luis Potosí|\
Mexico:Sinaloa:Sinaloa|\
Mexico:Sonora:Sonora|\
Mexico:Tabasco:Tabasco|\
Mexico:Tamaulipas:Tamaulipas|\
Mexico:Tlaxcala:Tlaxcala|\
Mexico:Veracruz:Veracruz|\
Mexico:Yucatan:Yucatan|\
Mexico:Zacatecas:Zacatecas|\
Australia:Australian Capital Territory:Australian Capital Territory|\
Australia:New South Wales:New South Wales|\
Australia:Northern Territory:Northern Territory|\
Australia:Queensland:Queensland|\
Australia:South Australia:South Australia|\
Australia:Tasmania:Tasmania|\
Australia:Victoria:Victoria|\
Australia:Western Australia:Western Australia|\
';

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 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('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("maxlength", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}

function setToAllStates() {
  var selObj = document.getElementById('stateSelect');
  if ( selObj.type == 'select-one' ) {
    selObj.options[0].text = "All States/Provinces";
  }
}

// -->
