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 menu.
//
var state = '\
USA:AK:Alaska|\
USA:AL:Alabama|\
USA:AR:Arkansas|\
USA:AS:American Samoa|\
USA:AZ:Arizona|\
USA:CA:California|\
USA:CO:Colorado|\
USA:CT:Connecticut|\
USA:DC:D.C.|\
USA:DE:Delaware|\
USA:FL:Florida|\
USA:FM:Micronesia|\
USA:GA:Georgia|\
USA:GU:Guam|\
USA:HI:Hawaii|\
USA:IA:Iowa|\
USA:ID:Idaho|\
USA:IL:Illinois|\
USA:IN:Indiana|\
USA:KS:Kansas|\
USA:KY:Kentucky|\
USA:LA:Louisiana|\
USA:MA:Massachusetts|\
USA:MD:Maryland|\
USA:ME:Maine|\
USA:MH:Marshall Islands|\
USA:MI:Michigan|\
USA:MN:Minnesota|\
USA:MO:Missouri|\
USA:MP:Marianas|\
USA:MS:Mississippi|\
USA:MT:Montana|\
USA:NC:North Carolina|\
USA:ND:North Dakota|\
USA:NE:Nebraska|\
USA:NH:New Hampshire|\
USA:NJ:New Jersey|\
USA:NM:New Mexico|\
USA:NV:Nevada|\
USA:NY:New York|\
USA:OH:Ohio|\
USA:OK:Oklahoma|\
USA:OR:Oregon|\
USA:PA:Pennsylvania|\
USA:PR:Puerto Rico|\
USA:PW:Palau|\
USA:RI:Rhode Island|\
USA:SC:South Carolina|\
USA:SD:South Dakota|\
USA:TN:Tennessee|\
USA:TX:Texas|\
USA:UT:Utah|\
USA:VA:Virginia|\
USA:VI:Virgin Islands|\
USA:VT:Vermont|\
USA:WA:Washington|\
USA:WI:Wisconsin|\
USA:WV:West Virginia|\
USA:WY:Wyoming|\
USA:AA:Military Americas|\
USA:AE:Military Europe/ME/Canada|\
USA:AP:Military Pacific|\
Canada:AB:Alberta|\
Canada:MB:Manitoba|\
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|\
';

// 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 = '\
USA:USA|\
Canada:Canada|\
American Samoa:American Samoa|\
Argentina:Argentina|\
Belize:Belize|\
Bolivia:Bolivia|\
Brazil:Brazil|\
Chile:Chile|\
Colombia:Colombia|\
Costa Rica:Costa Rica|\
Dominican Republic:Dominican Republic|\
Ecuador:Ecuador|\
El Salvador:El Salvador|\
Falkland Islands (Malvinas):Falkland Islands (Malvinas)|\
Guam:Guam|\
Guatemala:Guatemala|\
Haiti:Haiti|\
Honduras:Honduras|\
Jamaica:Jamaica|\
Mexico:Mexico|\
Nicaragua:Nicaragua|\
Northern Mariana Islands:Northern Mariana Islands|\
Panama:Panama|\
Paraguay:Paraguay|\
Peru:Peru|\
Puerto Rico:Puerto Rico|\
Suriname:Suriname|\
Virgin Islands (U.S.):Virgin Islands (U.S.)|\
Uruguay:Uruguay|\
Venezuela:Venezuela|\
';


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
  countryLineArray.pop();
  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 && 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");
        inputSel.setAttribute("class","formText");
        inputSel.style.width = '180px';
        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);
    inputEl.setAttribute("class","formText");
    inputEl.style.width = '10px';
    // REMOVE THIS CSS STYLE IF YOU WANT TO SEE A BLANK TEXT BOX FOR THE USER TO INPUT TEXT.
    inputEl.style.visibility = "hidden";    
    parentObj.appendChild(inputEl);
  }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}