/**
 * createXMLHttp()
 * 
 * Attempts to create a new XMLHTTP object for any supported type of browser.
 */
function createXMLHttp()
{
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new XMLHttpRequest()
      } catch (e) {
        //alert("Could not create XMLHttpRequest object!");
        return false;
      }
    }
  }
  return xmlhttp;
}

/**
 * toggleDisplay()
 *
 * Switches the display style of the given element.
 */
function toggleDisplay(sectionID)
{
  section = document.getElementById(sectionID);
  section.style.display = (section.style.display == 'none') ? '' : 'none' ;
}

/**
 * sortSelect()
 * 
 * Sorts a select box is ascending or descending order.  The function defaults
 * to sorting in ascending order.  Passing false as the second option will reverse
 * the sort order.
 */
function sortSelect(selectToSort, sortAscending)
{
  // default to ascending
  if(arguments.length == 1) { sortAscending = true; }
  
  // copy options into an array and sort it
  var myOptions = new Array();
  for (var i = 0; i < selectToSort.options.length; i++) {
    if(selectToSort.options[i].text == selectToSort.options[i].value) {
      myOptions[i] = selectToSort.options[i].value;
    }
    else {
      myOptions[i] = { optText:selectToSort.options[i].text, optValue:selectToSort.options[i].value };
    }
  }
  
  // Sort the options in ascending order
  myOptions.sort();
        
  // Hack to sort decending
  if(!sortAscending) {
    myOptions.reverse();
  }
        
  // copy sorted options from array back to select box
  selectToSort.options.length = 0;
  for (var i = 0; i < myOptions.length; i++) {
    var optObj = document.createElement('option');
    if(myOptions[i].text && myOptions[i].value) {
      optObj.text  = myOptions[i].text;
      optObj.value = myOptions[i].value;
    }
    else {
      optObj.text  = myOptions[i];
      optObj.value = myOptions[i];
    }
    try {
      selectToSort.options.add(optObj, null);
    }
    catch(e) {
      selectToSort.options.add(optObj);
    }
  }
}

/**
 * disableSubmit()
 *
 * Disables a forms submit button so that users dont try to double-click on it
 */
function disableSubmit(el)
{
  if(el) {
    el.disabled = true;
  }
}
