       
              var postState = '';
var postCountry = '';
            
function TrimString(sInString) {
    if ( sInString ) {
        sInString = sInString.replace( /^\s+/g, "" );// strip leading
        return sInString.replace( /\s+$/g, "" );// strip trailing
    }
}


var state   = '';
var country = '';

function setState(stateValue) {
    state   = stateValue;
}

function setCountry(countryValue)
{
    country  = countryValue;
}

function setPostState(postStateValue)
{
    postState   = postStateValue;
}

function setPostCountry(postCountryValue)
{
    postCountry = postCountryValue;
}

// 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++) {
    countryName = TrimString(countryLineArray[loop]);
    if ( countryName != '' ) {
      selObj.options[loop + 1] = new Option(countryName,  countryName);
    }
    if ( defaultCountry == countryName ) {
      selObj.selectedIndex = loop + 1;
    }
  }

  if(defaultCountry != '' && selObj.selectedIndex==0)
{
    selObj.options[selObj.length] = new Option(defaultCountry, defaultCountry);
    selObj.selectedIndex    = selObj.length-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/Province','');
    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[1]);

  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","ADDRESS_STATEPROV");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select State/Province','');
        selObj.selectedIndex = 0;
      }
      if ( stateName != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateName);
      }
      // See if it's selected from a previous post
      if ( stateName == 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", "ADDRESS_STATEPROV");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}
        