
/* 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 States of America:AK:Alaska|\
United States of America:AL:Alabama|\
United States of America:AR:Arkansas|\
United States of America:AS:American Samoa|\
United States of America:AZ:Arizona|\
United States of America:CA:California|\
United States of America:CO:Colorado|\
United States of America:CT:Connecticut|\
United States of America:DC:District of Columbia|\
United States of America:DE:Delaware|\
United States of America:FL:Florida|\
United States of America:GA:Georgia|\
United States of America:GU:Guam|\
United States of America:HI:Hawaii|\
United States of America:IA:Iowa|\
United States of America:ID:Idaho|\
United States of America:IL:Illinois|\
United States of America:IN:Indiana|\
United States of America:KS:Kansas|\
United States of America:KY:Kentucky|\
United States of America:LA:Louisiana|\
United States of America:MA:Massachusetts|\
United States of America:MD:Maryland|\
United States of America:ME:Maine|\
United States of America:MP:Marianas|\
United States of America:MH:Marshall Islands|\
United States of America:MI:Michigan|\
United States of America:FM:Micronesia|\
United States of America:MN:Minnesota|\
United States of America:MO:Missouri|\
United States of America:MS:Mississippi|\
United States of America:MT:Montana|\
United States of America:NC:North Carolina|\
United States of America:ND:North Dakota|\
United States of America:NE:Nebraska|\
United States of America:NH:New Hampshire|\
United States of America:NJ:New Jersey|\
United States of America:NM:New Mexico|\
United States of America:NV:Nevada|\
United States of America:NY:New York|\
United States of America:OH:Ohio|\
United States of America:OK:Oklahoma|\
United States of America:OR:Oregon|\
United States of America:PW:Palau|\
United States of America:PA:Pennsylvania|\
United States of America:PR:Puerto Rico|\
United States of America:RI:Rhode Island|\
United States of America:SC:South Carolina|\
United States of America:SD:South Dakota|\
United States of America:TN:Tennessee|\
United States of America:TX:Texas|\
United States of America:UT:Utah|\
United States of America:VA:Virginia|\
United States of America:VI:Virgin Islands|\
United States of America:VT:Vermont|\
United States of America:WA:Washington|\
United States of America:WI:Wisconsin|\
United States of America:WV:West Virginia|\
United States of America:WY:Wyoming|\
United States of America:AA:Military Americas|\
United States of America:AE:Military Europe/ME/Canada|\
United States of America:AP:Military Pacific|\
Canada:AB:Alberta|\
Canada:BC:British Columbia|\
Canada:LA:Labrador|\
Canada:MB:Manitoba|\
Canada:NB:New Brunswick|\
Canada:NL:Newfoundland|\
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|\
Australia:AAT:Australian Antarctic Territory|\
Australia:ACT:Australian Capital Territory|\
Australia:NT:Northern Territory|\
Australia:NSW:New South Wales|\
Australia:QLD:Queensland|\
Australia:SA:South Australia|\
Australia:TAS:Tasmania|\
Australia:VIC:Victoria|\
Australia:WA:Western Australia|\
Brazil:AC:Acre|\
Brazil:AL:Alagoas|\
Brazil:AM:Amazonas|\
Brazil:AP:Amapa|\
Brazil:BA:Baia|\
Brazil:CE:Ceara|\
Brazil:DF:Distrito Federal|\
Brazil:ES:Espirito Santo|\
Brazil:FN:Fernando de Noronha|\
Brazil:GO:Goias|\
Brazil:MA:Maranhao|\
Brazil:MG:Minas Gerais|\
Brazil:MS:Mato Grosso do Sul|\
Brazil:MT:Mato Grosso|\
Brazil:PA:Para|\
Brazil:PB:Paraiba|\
Brazil:PE:Pernambuco|\
Brazil:PI:Piaui|\
Brazil:PR:Parana|\
Brazil:RJ:Rio de Janeiro|\
Brazil:RN:Rio Grande do Norte|\
Brazil:RO:Rondonia|\
Brazil:RR:Roraima|\
Brazil:RS:Rio Grande do Sul|\
Brazil:SC:Santa Catarina|\
Brazil:SE:Sergipe|\
Brazil:SP:Sao Paulo|\
Brazil:TO:Tocatins|\
Netherlands:DR:Drente|\
Netherlands:FL:Flevoland|\
Netherlands:FR:Friesland|\
Netherlands:GL:Gelderland|\
Netherlands:GR:Groningen|\
Netherlands:LB:Limburg|\
Netherlands:NB:Noord Brabant|\
Netherlands:NH:Noord Holland|\
Netherlands:OV:Overijssel|\
Netherlands:UT:Utrecht|\
Netherlands:ZH:Zuid Holland|\
Netherlands:ZL:Zeeland|\
United Kingdom:AVON:Avon|\
United Kingdom:BEDS:Bedfordshire|\
United Kingdom:BERKS:Berkshire|\
United Kingdom:BUCKS:Buckinghamshire|\
United Kingdom:CAMBS:Cambridgeshire|\
United Kingdom:CHESH:Cheshire|\
United Kingdom:CLEVE:Cleveland|\
United Kingdom:CORN:Cornwall|\
United Kingdom:CUMB:Cumbria|\
United Kingdom:DERBY:Derbyshire|\
United Kingdom:DEVON:Devon|\
United Kingdom:DORSET:Dorset|\
United Kingdom:DURHAM:Durham|\
United Kingdom:ESSEX:Essex|\
United Kingdom:GLOUS:Gloucestershire|\
United Kingdom:GLONDON:Greater London|\
United Kingdom:GMANCH:Greater Manchester|\
United Kingdom:HANTS:Hampshire|\
United Kingdom:HERWOR:Hereford & Worcestershire|\
United Kingdom:HERTS:Hertfordshire|\
United Kingdom:HUMBER:Humberside|\
United Kingdom:IOM:Isle of Man|\
United Kingdom:IOW:Isle of Wight|\
United Kingdom:KENT:Kent|\
United Kingdom:LANCS:Lancashire|\
United Kingdom:LEICS:Leicestershire|\
United Kingdom:LINCS:Lincolnshire|\
United Kingdom:MERSEY:Merseyside|\
United Kingdom:NORF:Norfolk|\
United Kingdom:NHANTS:Northamptonshire|\
United Kingdom:NTHUMB:Northumberland|\
United Kingdom:NOTTS:Nottinghamshire|\
United Kingdom:OXON:Oxfordshire|\
United Kingdom:SHROPS:Shropshire|\
United Kingdom:SOM:Somerset|\
United Kingdom:STAFFS:Staffordshire|\
United Kingdom:SUFF:Suffolk|\
United Kingdom:SURREY:Surrey|\
United Kingdom:SUSS:Sussex|\
United Kingdom:WARKS:Warwickshire|\
United Kingdom:WMID:West Midlands|\
United Kingdom:WILTS:Wiltshire|\
United Kingdom:YORK:Yorkshire|\
Ireland:CO ANTRIM:County Antrim|\
Ireland:CO ARMAGH:County Armagh|\
Ireland:CO DOWN:County Down|\
Ireland:CO FERMANAGH:County Fermanagh|\
Ireland:CO DERRY:County Londonderry|\
Ireland:CO TYRONE:County Tyrone|\
Ireland:CO CAVAN:County Cavan|\
Ireland:CO DONEGAL:County Donegal|\
Ireland:CO MONAGHAN:County Monaghan|\
Ireland:CO DUBLIN:County Dublin|\
Ireland:CO CARLOW:County Carlow|\
Ireland:CO KILDARE:County Kildare|\
Ireland:CO KILKENNY:County Kilkenny|\
Ireland:CO LAOIS:County Laois|\
Ireland:CO LONGFORD:County Longford|\
Ireland:CO LOUTH:County Louth|\
Ireland:CO MEATH:County Meath|\
Ireland:CO OFFALY:County Offaly|\
Ireland:CO WESTMEATH:County Westmeath|\
Ireland:CO WEXFORD:County Wexford|\
Ireland:CO WICKLOW:County Wicklow|\
Ireland:CO GALWAY:County Galway|\
Ireland:CO MAYO:County Mayo|\
Ireland:CO LEITRIM:County Leitrim|\
Ireland:CO ROSCOMMON:County Roscommon|\
Ireland:CO SLIGO:County Sligo|\
Ireland:CO CLARE:County Clare|\
Ireland:CO CORK:County Cork|\
Ireland:CO KERRY:County Kerry|\
Ireland:CO LIMERICK:County Limerick|\
Ireland:CO TIPPERARY:County Tipperary|\
Ireland:CO WATERFORD:County Waterford|\
';

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

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 One --','');
    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","FormAddressIDState");
        inputSel.setAttribute("class","text");
        inputSel.setAttribute("id","stateSelect");
        inputSel.setAttribute("style","width: 202px");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('-- Select One --','');
        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("class", "text");
    inputEl.setAttribute("name", "FormAddressIDState");
    inputEl.setAttribute("size", 34);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}




