/******************************************************************************
  This script uses XMLHttpRequest to obtain a list of sequence databases 
  supported for motif searching. It updates a select element with the elements
  from the list. The select element is identified by the id string passed as
  a paramter.
 ******************************************************************************/
function update_supported_db_list(select_id, short_only) {
  var request;

  // Browser compatibility code
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  else {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    if (! request) {
      alert("Unable to load ActiveXObject.Msxml2.XMLHTTP");
    }
  }
  request.open(
    "GET", 
    "http://meme.nbcr.net/meme4_1/cgi-bin/get_db_list.cgi?short_only=" + short_only + "&db_names=mast_db_names.csv",
    false
  );
  request.send(null);
  if (request.status != 200) {
    // Request failed
    alert('Unable to obtain list of supported databases. Contact MEME web site administrator.');
  }
  else { 

    // Request succeeded

    var databases = request.responseText.split('\n');

    output = document.getElementById(select_id);
    // Clear any existing elements in the select element.
    while (output.hasChildNodes()) {
      output.removeChild(output.firstChild);
    }
    
    // Add the empty option.
    option = document.createElement('option');
    output.appendChild(option);

    // Update the select element with the  supported databases.
    for (i = 0; i < databases.length; i++) {
      // Only use lines that aren't comments and not all blank.
      var re = /\S/;
      if (databases[i].charAt(0) != '#' && re.test(databases[i])) {
        option = document.createElement('option');
        // This is a supported database
        // The description is in the 5th field.
        values = databases[i].split(',')
        option.setAttribute('value', databases[i]);
        text = document.createTextNode(values[4]);
        option.appendChild(text);
        output.appendChild(option);
      }
    }
  }
}
