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

