function makeRequestAndUpdate( url, objectToUpdate ) {
  var httpRequest = null;
  if (window.XMLHttpRequest) { /* Mozilla, Safari, ... */
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) {
      httpRequest.overrideMimeType('text/xml');
    }
  }
  else if (window.ActiveXObject) { /* IE */
    try { httpRequest = new ActiveXObject('Msxml2.XMLHTTP'); }
    catch (e) {
      try { httpRequest = new ActiveXObject('Microsoft.XMLHTTP'); }
      catch (e) {}
    }
  }
  if (!httpRequest) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  httpRequest.onreadystatechange = function() { checkRequestAndUpdate( httpRequest, objectToUpdate ); };
  httpRequest.open('GET', url, true);
  httpRequest.send('');
}

function checkRequestAndUpdate( httpRequest, objectToUpdate ) {
  var xmlFieldMatch = 'search_results';
  if ( httpRequest.readyState == 4 ) {
    if ( httpRequest.status == 200 ) {
      if ( httpRequest.responseXML.getElementsByTagName( xmlFieldMatch )[0] != null ) {
        var htmlData = httpRequest.responseXML.getElementsByTagName( xmlFieldMatch )[0];
      }
      if( htmlData != null ) {
        updateObjectHTML( objectToUpdate, htmlData.firstChild.data );
      }
    } else {
      alert('Network error; you may need to reload.');
    }
  }
}

function updateObjectHTML( objectToUpdate, html ) {
  document.getElementById( objectToUpdate ).innerHTML = html;
}

function search( start, options ) {
  var base_url = 'search.php';
  var search_fields = '';
  var temp_obj = null;
  var name_last = null;
  var name_first = null;
  var state = null;
  var country = null;
  var crime = null;
  var date_convicted_year = null;
  var date_convicted_month = null;

  // set the range values; results start (rs)
  if ( start ) { rs = start; }
  else         { rs = 1; }

  // load up the search fields (if any)
  temp_obj = document.getElementById('search_field_name_last');
  if( temp_obj.value ) { name_last  = temp_obj.value; }
  temp_obj = document.getElementById('search_field_name_first');
  if( temp_obj.value ) { name_first = temp_obj.value; }
  temp_obj = document.getElementById('search_field_state');
  if( temp_obj.value ) { state      = temp_obj.value; }
  temp_obj = document.getElementById('search_field_country');
  if( temp_obj.value ) { country    = temp_obj.value; }
  temp_obj = document.getElementById('search_field_crime');
  if( temp_obj.value ) { crime      = temp_obj.value; }
  temp_obj = document.getElementById('search_field_date_convicted_year');
  if( temp_obj.value ) { date_convicted_year  = temp_obj.value; }
  temp_obj = document.getElementById('search_field_date_convicted_month');
  if( temp_obj.value ) { date_convicted_month = temp_obj.value; }
  temp_obj = document.getElementById('search_field_npp');
  if( temp_obj.value ) { npp        = temp_obj.value; }

  if( name_last  ) { search_fields = search_fields + "&name_last="  + name_last; }
  if( name_first ) { search_fields = search_fields + "&name_first=" + name_first; }
  if( state      ) { search_fields = search_fields + "&state="      + state; }
  if( country    ) { search_fields = search_fields + "&country="    + country; }
  if( crime      ) { search_fields = search_fields + "&crime="      + crime; }
  if( date_convicted_year  ) { search_fields = search_fields + "&date_year="  + date_convicted_year;  }
  if( date_convicted_month ) { search_fields = search_fields + "&date_month=" + date_convicted_month; }
  if( npp        ) { search_fields = search_fields + "&npp="        + npp; }

  // do it
  url = base_url + '?' + search_fields + '&rs=' + rs + options;
  //alert( 'url: ' + url );
  makeRequestAndUpdate( url, 'search_results' );
}

function initialLoad( start, options ) {
  search( start, options );
}

