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.
//

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(SelectCountry,'');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length - 1; 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 ) {
      if(0 == selObj.selectedIndex) 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.remove(i);
    }
    selObj.options.length=0;
    //alert('select'+selObj.options.length);
    if(document.getElementById('countrySelect').value == 'US') 
    	selObj.options[0] = new Option(SelectStateUS,'');
    else 
    	selObj.options[0] = new Option(SelectState,'');
    selObj.selectedIndex = 0;
//  }
  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;
  //alert('arr'+stateLineArray.length);
  
//  stateLineArray.sort();
  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
  
  var sec = document.getElementById("sec");
  var lab_prov = document.getElementById("lab_prov");
  var lab_zip = document.getElementById("lab_zip");
  
  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("class", "useless");    
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
*/    
    sec.style.display = 'none';  
    lab_prov.style.display = 'none';    
  } else {
  	sec.style.display = 'block';
    lab_prov.style.display = 'block';  	  
    
    if ((document.getElementById('countrySelect').value=='US') || (document.getElementById('countrySelect').value=='CA')) {
  		var lbl_lab_zip = document.getElementById("lbl_lab_zip");
  		var lbl_lab_prov = document.getElementById("lbl_lab_prov");
  		if(document.getElementById('countrySelect').value=='US') {
  			lbl_lab_zip.innerHTML = zipUS;
  			lbl_lab_prov.innerHTML = provUS;
  		} else {
  			lbl_lab_zip.innerHTML = zipCA;
  			lbl_lab_prov.innerHTML = provCA;
  		}
  		lab_zip.style.display = 'block'; 
  	} else {
  		lab_zip.style.display = 'none';  	   
  	}
  }
/*  
  var zip_u = document.getElementById("zip_u");
  zip_u.value = '';

  var address_u = document.getElementById("address_u");
  address_u.value = '';
  
  var city_u = document.getElementById("city_u");
  city_u.value = '';    
*/  

  //unit
  	var unitObj = document.getElementById('dist_unit');  	
    if (unitObj && ( document.getElementById('countrySelect').value == 'US' ) && (unitObj.selectedIndex != 1)) {
      unitObj.selectedIndex = 1;      
    }
    if (unitObj && ( document.getElementById('countrySelect').value != '' ) && ( document.getElementById('countrySelect').value != 'US' ) && (unitObj.selectedIndex != 0)) {
      unitObj.selectedIndex = 0;
    }
}

function initCountry(country) {
  populateCountry(country);
  populateState();
}