function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject();

function requestContent(type) {
  if (http) {
    var region = document.getElementById('jsSelectedRegion').value;
    var url = "/school_select.php?region=" + region + "&scht=" + type;
    toggleSelectList(false);
    http.open("GET", url, true); 
    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http.onreadystatechange = handleHttpResponse; 
    http.send(null);
    return true;
  } else {
    return false;
  }
}

function handleHttpResponse() {
  if (http.readyState == 4) {
    var schoolChoices = http.responseText.split("|");
    if (schoolChoices.length < 1) {
      schoolChoices = "";
      schoolChoices.length = 0;
    }
    updateSelectList(schoolChoices);
  }
}

function updateSelectList(schoolChoices) {
  var sch = document.getElementById('sch[]');
  sch.options.length = 0;
  sch.selectedIndex = 0;
  
  var parts, optionText, optionValue;
  for (i = 0; i < schoolChoices.length; i++) {
    
   parts = schoolChoices[i].split(":");
   
   optionText = parts[0];   
   if (typeof(parts[1]) != 'undefined') {
     optionValue = parts[1];
   } else {
     optionValue = optionText;
   }
   
   sch.options[i] = new Option(optionText, optionValue);
   
  }
  
  
  toggleSelectList(true);
}

function updateSchools(theRadioButton) {
  return requestContent(theRadioButton.value) || updateSchoolsViaFormSubmission();
}

function updateSchoolsViaFormSubmission() {
  document.getElementById('jsSchoolChange').value = '1';
  document.getElementById('jsSchoolChange').form.submit();
  return true;
}

function resetSchool() {
  var sch = document.getElementById('sch[]');
  sch.options.length = 0;
  sch.selectedIndex = 0;
  sch.options[0] = new Option("Select a School Type", "");
  sch.disabled = true;
}

function toggleSelectList(enabled) {
  var sch = document.getElementById('sch[]');
  sch.disabled = !enabled;
  if (!enabled) {
    sch.options.length = 0;
    sch.selectedIndex = 0;
    sch.options[0] = new Option("Retrieving Data...");
  }
}
