
/*
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.

*/

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 = '\
NA:US:AK:Alaska|\
NA:US:AL:Alabama|\
NA:US:AR:Arkansas|\
NA:US:AS:American Samoa|\
NA:US:AZ:Arizona|\
NA:US:CA:California|\
NA:US:CO:Colorado|\
NA:US:CT:Connecticut|\
NA:US:DC:D.C.|\
NA:US:DE:Delaware|\
NA:US:FL:Florida|\
NA:US:FM:Micronesia|\
NA:US:GA:Georgia|\
NA:US:GU:Guam|\
NA:US:HI:Hawaii|\
NA:US:IA:Iowa|\
NA:US:ID:Idaho|\
NA:US:IL:Illinois|\
NA:US:IN:Indiana|\
NA:US:KS:Kansas|\
NA:US:KY:Kentucky|\
NA:US:LA:Louisiana|\
NA:US:MA:Massachusetts|\
NA:US:MD:Maryland|\
NA:US:ME:Maine|\
NA:US:MH:Marshall Islands|\
NA:US:MI:Michigan|\
NA:US:MN:Minnesota|\
NA:US:MO:Missouri|\
NA:US:MP:Marianas|\
NA:US:MS:Mississippi|\
NA:US:MT:Montana|\
NA:US:NC:North Carolina|\
NA:US:ND:North Dakota|\
NA:US:NE:Nebraska|\
NA:US:NH:New Hampshire|\
NA:US:NJ:New Jersey|\
NA:US:NM:New Mexico|\
NA:US:NV:Nevada|\
NA:US:NY:New York|\
NA:US:OH:Ohio|\
NA:US:OK:Oklahoma|\
NA:US:OR:Oregon|\
NA:US:PA:Pennsylvania|\
NA:US:PR:Puerto Rico|\
NA:US:PW:Palau|\
NA:US:RI:Rhode Island|\
NA:US:SC:South Carolina|\
NA:US:SD:South Dakota|\
NA:US:TN:Tennessee|\
NA:US:TX:Texas|\
NA:US:UT:Utah|\
NA:US:VA:Virginia|\
NA:US:VI:Virgin Islands|\
NA:US:VT:Vermont|\
NA:US:WA:Washington|\
NA:US:WI:Wisconsin|\
NA:US:WV:West Virginia|\
NA:US:WY:Wyoming|\
NA:CA:AB:Alberta|\
NA:CA:BC:British Columbia|\
NA:CA:MB:Manitoba|\
NA:CA:NB:New Brunswick|\
NA:CA:NF:Newfoundland and Labrador|\
NA:CA:NS:Nova Scotia|\
NA:CA:ON:Ontario|\
NA:CA:PE:Prince Edward Island|\
NA:CA:QC:Quebec|\
NA:CA:SK:Saskatchewan|\
PA:AU:AC:Australian Capital Territory|\
PA:AU:SW:New South Wales|\
PA:AU:NT:Northern Territory|\
PA:AU:QL:Queensland|\
PA:AU:SA:South Australia|\
PA:AU:TA:Tasmania|\
PA:AU:VC:Victoria|\
PA:AU:WA:Western Australia|\
';

// 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 = '\
NA:US:United States|\
SS:AF:Afghanistan|\
EU:AL:Albania|\
AF:DZ:Algeria|\
PA:AS:American Samoa|\
EU:AD:Andorra|\
AF:AO:Angola|\
SA:AR:Argentina|\
NS:AM:Armenia|\
PA:AU:Australia|\
EU:AT:Austria|\
NS:AZ:Azerbaijan|\
CA:BS:Bahamas|\
ME:BH:Bahrain|\
SS:BD:Bangladesh|\
CA:BB:Barbados|\
NS:BY:Belarus|\
EU:BE:Belgium|\
CA:BZ:Belize|\
AF:BJ:Benin|\
CA:BM:Bermuda|\
SA:BO:Bolivia|\
EU:BA:Bosnia and Herzegovina|\
AF:BW:Botswana|\
SA:BR:Brazil|\
SE:BN:Brunei Darussalam|\
EU:BG:Bulgaria|\
AF:BF:Burkina Faso|\
AF:BI:Burundi|\
SE:KH:Cambodia|\
AF:CM:Cameroon|\
NA:CA:Canada|\
AF:CF:Cent. African Republic|\
SA:CL:Chile|\
ES:CN:China|\
SA:CO:Colombia|\
AF:CG:Congo|\
CA:CR:Costa Rica|\
EU:HR:Croatia|\
CA:CU:Cuba|\
ME:CY:Cyprus|\
EU:CZ:Czech Republic|\
AF:CI:Cote D Ivoire|\
EU:DK:Denmark|\
CA:DO:Dominican Republic|\
SA:EC:Ecuador|\
AF:EG:Egypt|\
CA:SV:El Salvador|\
EU:EE:Estonia|\
AF:ET:Ethiopia|\
PA:FJ:Fiji|\
EU:FI:Finland|\
EU:FR:France|\
AF:GA:Gabon|\
AF:GM:Gambia|\
NS:GE:Georgia|\
EU:DE:Germany|\
AF:GH:Ghana|\
EU:GR:Greece|\
CA:GD:Grenada|\
CA:GP:Guadeloupe|\
CA:GT:Guatemala|\
AF:GN:Guinea|\
AF:GW:Guinea-Bissau|\
CA:HT:Haiti|\
CA:HN:Honduras|\
ES:HK:Hong Kong|\
EU:HU:Hungary|\
EU:IS:Iceland|\
SS:IN:India|\
SE:ID:Indonesia|\
ME:IR:Iran|\
ME:IQ:Iraq|\
EU:IE:Ireland|\
ME:IL:Israel|\
EU:IT:Italy|\
CA:JM:Jamaica|\
ES:JP:Japan|\
ME:JO:Jordan|\
NS:KZ:Kazakhstan|\
AF:KE:Kenya|\
ES:KR:Korea, Republic of|\
ME:KW:Kuwait|\
NS:KG:Kyrgyzstan|\
SE:LA:Lao, PDR|\
EU:LV:Latvia|\
ME:LB:Lebanon|\
AF:LR:Liberia|\
AF:LY:Libyan Arab Jamahiriya|\
EU:LT:Lithuania|\
EU:LU:Luxembourg|\
EU:MK:Macedonia|\
AF:MG:Madagascar|\
AF:MW:Malawi|\
SE:MY:Malaysia|\
AF:ML:Mali|\
EU:MT:Malta|\
CA:MQ:Martinique|\
AF:MR:Mauritania|\
AF:MU:Mauritius|\
NA:MX:Mexico|\
NS:MD:Moldova, Republic of|\
EU:MC:Monaco|\
ES:MN:Mongolia|\
EU:ME:Montenegro|\
AF:MA:Morocco|\
AF:MZ:Mozambique|\
SE:MM:Myanmar|\
PA:NR:Nauru|\
SS:NP:Nepal|\
EU:NL:Netherlands|\
EU:AN:Netherlands Antilles|\
PA:NC:New Caledonia|\
PA:NZ:New Zealand|\
CA:NI:Nicaragua|\
AF:NE:Niger|\
AF:NG:Nigeria|\
PA:MP:Northern Mariana Islands|\
EU:NO:Norway|\
ME:OM:Oman|\
SS:PK:Pakistan|\
CA:PA:Panama|\
PA:PG:Papua New Guinea|\
SA:PY:Paraguay|\
SA:PE:Peru|\
SE:PH:Philippines|\
EU:PL:Poland|\
EU:PT:Portugal|\
CA:PR:Puerto Rico|\
ME:QA:Qatar|\
EU:RO:Romania|\
NS:RU:Russian Federation|\
AF:RW:Rwanda|\
AF:RE:Reunion|\
CA:KN:Saint Kitts and Nevis|\
ME:SA:Saudi Arabia|\
AF:SN:Senegal|\
EU:RS:Serbia|\
AF:SC:Seychelles|\
AF:SL:Sierra Leone|\
SE:SG:Singapore|\
EU:SK:Slovakia|\
EU:SI:Slovenia|\
PA:SB:Solomon Islands|\
AF:ZA:South Africa|\
EU:ES:Spain|\
SS:LK:Sri Lanka|\
AF:SD:Sudan|\
AF:SZ:Swaziland|\
EU:SE:Sweden|\
EU:CH:Switzerland|\
ME:SY:Syrian Arab Republic|\
ES:TW:Taiwan|\
AF:TZ:Tanzania|\
SE:TH:Thailand|\
AF:TG:Togo|\
CA:TT:Trinidad and Tobago|\
AF:TN:Tunisia|\
ME:TR:Turkey|\
AF:UG:Uganda|\
NS:UA:Ukraine|\
ME:AE:United Arab Emirates|\
EU:GB:United Kingdom|\
NA:US:United States|\
SA:UY:Uruguay|\
SA:VE:Venezuela|\
SE:VN:Vietnam|\
CA:VI:Virgin Islands (U.S.)|\
ME:YE:Yemen|\
AF:ZM:Zambia|\
AF:ZW:Zimbabwe|\
';
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  = lineArray[0]+":"+lineArray[1];
    countryName  = lineArray[2];
    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(":");
    continentCode  = TrimString(lineArray[0]);
    countryCode  = lineArray[0]+":"+lineArray[1];
    stateCode    = lineArray[0]+":"+lineArray[1]+":"+lineArray[2];
    stateName    = lineArray[3];
  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();
}


