var httpRequest = getHTTPObject();
var httpRequest2 = getHTTPObject();
var httpRequest3 = getHTTPObject();

//var httpRequest = new XMLHttpRequest();

var url = "/cgi-bin/get_contigs_json.pl"; // This URL should give us JSON data. 
var url_t = "/cgi-bin/get_tissue_json.pl"; // This URL should give us JSON data.
var url_x = "/cgi-bin/get_taxonomy_json.pl"; // This URL should give us JSON data.

var ctgData = new Object;
var ctgData2 = new Object;
var ctgData3 = new Object;

// work in both IE and firebox 
// stolen from 
// http://developer.apple.com/internet/webcontent/xmlhttpreq.html
function getHTTPObject() {
  var req = false;
  // branch for native XMLHttpRequest object
  if(window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  // branch for IE/Windows ActiveX version
  } else if(window.ActiveXObject) {
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        req = false;
      }
    }
  }
  return req;
}


function getJson() { 
  // Download the JSON data from the server.
  httpRequest.onreadystatechange = parseContigs;
  httpRequest2.onreadystatechange = parseTissue;
  httpRequest3.onreadystatechange = parseTaxonomy;
  httpRequest.open("GET", url, true);
  httpRequest2.open("GET", url_t, true);
  httpRequest3.open("GET", url_x, true);
  httpRequest.send(null);
  httpRequest2.send(null);
  httpRequest3.send(null);
}

function sortObject(o) {
    var sorted = {},
    key, a = [];

    for (key in o) {
        if (o.hasOwnProperty(key)) {
                a.push(key);
        }
    }

    a.sort();

    for (key = 0; key < a.length; key++) {
        sorted[a[key]] = o[a[key]];
    }
    return sorted;
}


function parseContigs() {
  if (httpRequest.readyState == 4) {
    if (httpRequest.status == 200) {
      var jsonData = httpRequest.responseText; 
      eval("ctgData = ("+jsonData+")");

      /* For some reason, the sorted organism list gets jumbled */
      /* Hack to work around */
      var sortedData = sortObject( ctgData );

      var i = 0;
      /* one field on each of two forms needs setting */
      document.genomic.species[ i ] = new Option( "Choose species:" );
      document.cluster.org[ i ] = new Option( "Choose species:" );
	i++;
      for( org in sortedData ) {
	document.genomic.species[ i ] = new Option( sortedData[org]['desc'], org );
	document.cluster.org[ i ] = new Option( sortedData[org]['desc'], org );
	i++
      }
    } else {
      alert('There was a problem with the URL.');
    }
    httpRequest = null;
  }
}


function parseTaxonomy() {
  if (httpRequest3.readyState == 4) {
    if (httpRequest3.status == 200) {
      var jsonData = httpRequest3.responseText; 
      eval("ctgData3 = ("+jsonData+")");

      /* For some reason, the sorted organism list gets jumbled */
      /* Hack to work around */
//      var sortedData = sortObject( ctgData3 );

      var i = 0;
      /* one field on each of two forms needs setting */
      document.sequence.taxon[ i ] = new Option( "No species filter", 'ALL' );
      i++;
      document.sequence.taxon[ i ] = new Option( "-------", 'ALL' );
      i++;

      for(j=0; j<ctgData3.length; j++) {
	var tag = ctgData3[j]['tag'];
	var name = ctgData3[j]['name'];
	var order = ctgData3[j]['order'];
	name = name.replace( /_/g, ". " );
	document.sequence.taxon[(j+2)] = new Option( name, tag );
//	document.sequence.taxon[(j+2)].style.paddingLeft('10px');
      }
    } else {
      alert('There was a problem with the URL.');
    }
    httpRequest3 = null;
  }
}


function parseTissue() {
  if (httpRequest2.readyState == 4) {
    if (httpRequest2.status == 200) {
      var jsonData = httpRequest2.responseText; 
      eval("ctgData2 = ("+jsonData+")");

      /* For some reason, the sorted organism list gets jumbled */
      /* Hack to work around */
      var sortedData = sortObject( ctgData2 );

      var i = 0;
      /* one field on each of two forms needs setting */
      document.experiments.organism[ i ] = new Option( "Choose species:" );
	i++;
      for( org in sortedData ) {
	document.experiments.organism[ i ] = new Option( sortedData[org]['desc'], org );
	i++
      }
    } else {
      alert('There was a problem with the URL.');
    }
    httpRequest2 = null;
  }
}

function populateChromo( org ) {
  var i = 0;
  document.genomic.chromosome.options.length = i;
//  document.genomic.chromosome[ i++ ] = new Option( "Chr:" );
  for ( chromo in ctgData[org]['chr'] ) {
    document.genomic.chromosome[ i++ ] = new Option( ctgData[org]['chr'][chromo] );
  }
}

function populateTissue( organism ) {
  var i = 0;
  document.experiments.tissue.options.length = i;
  document.experiments.tissue[ i++ ] = new Option( "All" );
  for ( tis in ctgData2[organism]['tissue'] ) {
    document.experiments.tissue[ i++ ] = new Option( ctgData2[organism]['tissue'][tis] );
  }
}


