function populateStateObject(form,dbState) {

  var country;
  var selected_state;
  for (var i = 0; i < form.elements.length; i++) {
    var e = form.elements[i];
    if (e.name == "Country") country = e.value;
    if (e.name == "State") selected_state = e.value;
  }

  if ((country == "United States") || (country == "Canada")) {
    toggleSection(new Array('state_select'), true, form);
    toggleSection(new Array('state_fill'), false, form);

    d = document.getElementById('setStateSelect');
    if (!d) { return; }

    var item_text = new Array();
    item_text['United States'] = ['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','District of Columbia','Delaware','Florida','Georgia','Guam','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming'];
      item_text['Canada'] = ['Alberta','British Columbia','Manitoba','New Brunswick','Newfoundland and Labrador','Northwest Territories','Nova Scotia','Nanavut','Ontario','Prince Edward Island','Quebec','Saskatchewan','Yukon'];

    var item_value = new Array();
    item_value['United States'] = ['AL','AK','AZ','AR','CA','CO','CT','DC','DE','FL','GA','GU','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'];
    item_value['Canada'] = ['AB','BC','MB','NB','NL','NT','NS','NU','ON','PE','QC','SK','YT'];

    d.options.length = 0;
    current_list_text = item_text[country];
    current_list_value = item_value[country];

    if (!current_list_text) { return; }

    d.options.length = current_list_text.length;
    var selected_index = 0;
    for (var i = 0; i < current_list_text.length; i++) {
      d.options[i].text = current_list_text[i];
      d.options[i].value = current_list_value[i];
      if (selected_state == current_list_value[i] || dbState == current_list_value[i]) selected_index = i;
    }
    //d.options[i].selectedIndex = selected_index;
	d.options[selected_index].selected = true;
  } else {
    toggleSection(new Array('state_select'), false, form);
    toggleSection(new Array('state_fill'), true, form);
  }
}


function populateBillStateObject(form) {
  var bill_country;
  var selected_bill_state;
  for (var i = 0; i < form.elements.length; i++) {
    var e = form.elements[i];
    if (e.name == "billCountry") bill_country = e.value;
    if (e.name == "billState1") selected_bill_state = e.value;
  }

  if ((bill_country == "United States") || (bill_country == "Canada")) {
    toggleSection(new Array('bill_state_select'), true, form);
    toggleSection(new Array('bill_state_fill'), false, form);

    d = document.getElementById('setBillStateSelect');
    if (!d) { return; }

    var item_text = new Array();
    item_text['United States'] = ['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','District of Columbia','Delaware','Florida','Georgia','Guam','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming'];
      item_text['Canada'] = ['Alberta','British Columbia','Manitoba','New Brunswick','Newfoundland and Labrador','Northwest Territories','Nova Scotia','Nanavut','Ontario','Prince Edward Island','Quebec','Saskatchewan','Yukon'];

    var item_value = new Array();
    item_value['United States'] = ['AL','AK','AZ','AR','CA','CO','CT','DC','DE','FL','GA','GU','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'];
    item_value['Canada'] = ['AB','BC','MB','NB','NL','NT','NS','NU','ON','PE','QC','SK','YT'];

    d.options.length = 0;
    current_list_text = item_text[bill_country];
    current_list_value = item_value[bill_country];

    if (!current_list_text) { return; }

    d.options.length = current_list_text.length;
    var selected_index = 0;
    for (var i = 0; i < current_list_text.length; i++) {
      d.options[i].text = current_list_text[i];
      d.options[i].value = current_list_value[i];
      if (selected_bill_state == current_list_value[i]) selected_index = i;
    }
     d.options[i].selectedIndex = selected_index;
  } else {
    toggleSection(new Array('bill_state_select'), false, form);
    toggleSection(new Array('bill_state_fill'), true, form);
  }
}


// If setActive is false, hide element with id=name, and disable all
// form elements with class=name. Vice versa for setActive=true.
function toggleSection(classes, setActive, form) {
  for (var i = 0; i < classes.length; i++) {
    var element = getElement(classes[i]);
    if (element) {
      element.style.display = '';
      element.style.display = (setActive) ? 'block'  : 'none';

      // DPV: for debug uncomment the following line
      // alert(element.id + " toggled to " + setActive);

    }
    setStatus(classes[i], setActive, form);
  }
}

// Return element with ID 'elementID'
function getElement(elementID) {
  if (document.all) {
    return document.all[elementID];
  } else if (document.getElementById) {
    return document.getElementById(elementID);
  }
  return false;
}

// Toggle disabled status of all elements in form with class=className
// setActive=true enables elements
function setStatus(className, setActive, form) {
  for (var i = 0; i < form.elements.length; i++) {
    var e = form.elements[i];
    // If element class name matches target class, set the disabled
    // status of the element.
    if (e.className.indexOf(className) >= 0) {
      e.disabled = !setActive;
    }
  }
}


