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,countryinput) {
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById(countryinput);
  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 != '' && countryName != '') {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateState(defaultState,stateinput,countryinput) {
  var selObj = document.getElementById(stateinput);
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
	for (i=selObj.length;i>=0;i--)
    {
        selObj.options[i]=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(countryinput).value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
       if ( selObj.type == 'text' ) {
          parentObj = document.getElementById(stateinput).parentNode;
          parentObj.removeChild(selObj);
	      var inputSel = document.createElement("SELECT");
          inputSel.setAttribute("name",stateinput);
          inputSel.setAttribute("id",stateinput);
          parentObj.appendChild(inputSel) ;
          selObj = document.getElementById(stateinput);
          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 == defaultState) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById(stateinput).parentNode;
    parentObj.removeChild(selObj);
  	// Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", stateinput);
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", stateinput);
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", defaultState);
    parentObj.appendChild(inputEl) ;
  }
}

function initCountry(country,state,countryinput,stateinput) {
  populateCountry(country,countryinput);
  populateState(state,stateinput,countryinput);
}