
function getReqObj() {
/* Retrieves the request object, handling browser-specific idiosyncracies along the way */
var httpObj = null;

   try {
      httpObj = new ActiveXObject("Msxml2.XMLHTTP");
   } catch(error) {
      try {
         httpObj = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(error) {
         httpObj = null;
      }
   }
   if(!httpObj && typeof XMLHttpRequest != "undefined") {
      httpObj = new XMLHttpRequest();
   }
   if(httpObj==null){
		systemErrorCheck('',"Error initializing the XMLHttpRequest.  Please make sure you have javascript enabled.");
   }
	return httpObj;
}

function getSubjectList(termCode) {
/* AJAX function - makes a request to the server for a list of 
   subjects based on the selected campus */
	var campus = document.getElementById("campus").value;
	//alert("campus: "+campus);
	var subjectBox = document.getElementById("designator");
	
	
	var xmlhttp;
	xmlhttp = getReqObj();
	xmlhttp.open("GET","getSubjects.jsp?campus="+campus, true);
	xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState==4) { 
                //alert("xmlhttp.status is: " +xmlhttp.status);
                if(xmlhttp.status!=200&&xmlhttp.status!=500){
                    if (xmlhttp.status==404) {
                        // looks like no rows returned - format box accordingly
                        setEmptySubjectBox();
                    }
                    // Transmission error
                    //systemErrorCheck("","Error status code: "+xmlhttp.status);
                } else {
                    //No transmission error - continue
                    // convert the responseText into a JSON object
                    //alert("xmlhttp.status is: " +xmlhttp.status);
                    var jsonExpression = "(" + xmlhttp.responseText + ")";
                    //alert("Subject list returned: " + jsonExpression);
                    var object = eval(jsonExpression);
    
                    // remove all the subjects currently in the list
                    while (subjectBox.hasChildNodes()){
                        subjectBox.removeChild(subjectBox.firstChild);	
                    }
                    var subjects = object.subjects;
                    // now parse out the subjects into a new dropdown list
                    //alert("subjects is: " + subjects);
                    //alert("subjects.length is: "+subjects.length);
                    
                    // first add an "all subjects option
                    var firstOption=document.createElement('option');
                    firstOption.setAttribute('value','ALL');
                    firstOption.appendChild(document.createTextNode('All subjects'));
                    subjectBox.appendChild(firstOption);
                    
                    for(var i=0;i<subjects.length;i++){
                        var option=document.createElement('option');
                        option.setAttribute('value',subjects[i].subject);
                        option.appendChild(document.createTextNode(subjects[i].longDescription));//set subject text
                        subjectBox.appendChild(option);
                    }
                }
            }
        }
	xmlhttp.send(null);
}

function setEmptySubjectBox() {
/* handles the case where there are no subjects for the selected campus - 
   creates a disabled "no subjects" option in the subject list */
    var subjectBox = document.getElementById("designator");
    while (subjectBox.hasChildNodes()){
        subjectBox.removeChild(subjectBox.firstChild);	
    }
    var option=document.createElement('option');
    option.setAttribute('value','null');
    option.appendChild(document.createTextNode('(no subjects for this campus)'));
    option.disabled='true';
    subjectBox.appendChild(option);
}    

