/*
    ajax client code
    xmlHttp should be different if you call more than one client js function,
        or receive data will have problem.
*/

var xmlHttp;
var xmlHttp1;
var showNoOptionStr="----------------------------------------";


/*****************************************for search student ***************************************/


/*
       function: client send information to get students records,
                when the result return, call the define function to get the data
      parameters:
         str: url parameter. eg: ProgramId for the record.
         clientProgramCode: client prog code in  ajax msg. for security reason
 */
function setSearchStudCSRequest(clientProgramCode) {
    var selectedValue = getSelectedRadioValue(document.studentSearchForm.studentSearchSelect);
    var findString = document.getElementById("studentSearchFind").value;
    
    // get course schedule id from document
    var selectedCsid = document.getElementById("courseschedule").value;

    var url="";
    
     
    if(selectedValue=="") {
        // select nothing to search , do nothing
       }
    else  {
        if(findString=="")
            url= "jsp/common/searchStudentWithCourseProcHtml.jsp?by="+selectedValue+"&findValue=no"+"&selectedcsid="+selectedCsid;
        else {
            url = "jsp/common/searchStudentWithCourseProcHtml.jsp?by="+selectedValue+"&findValue="+ findString +"&selectedcsid="+selectedCsid;
        }
        // need to send url to server
        
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        url=url + "&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp.onreadystatechange=getResultOfStudentWithCourseSearch;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
        
        }
}

/* 
   get selected in radio group
*/
   function getSelectedRadioValue(radioGroup)
   {
      var selectedRadioValue = "";
      var radIndex;

      for (radIndex = 0; radIndex < radioGroup.length; radIndex++)
      {
         if (radioGroup[radIndex].checked)
         {
            selectedRadioValue = radioGroup[radIndex].value;
            break;
         }
      }
   
      return selectedRadioValue;
   }


/*
   get search student result html table, response text
*/

function getResultOfStudentWithCourseSearch() { 
    // define variables
    var searchResultDiv="searchStudentResultArea";
    
 
    
    if (xmlHttp.readyState==4)// request is complete
    {
        if (xmlHttp.status == 200) { // status is OK
            document.getElementById(searchResultDiv).innerHTML = xmlHttp.responseText;
                    
        } 
        else {
            alert('There was a problem with the request.');
        }
        
    }
}



/*************************************************************************************/
/*
      function: client send information to get option value,
                when the result return, call the define function to get the data
      parameters:
         str: url parameter. eg: ProgramId for the record.
         clientProgramCode: prog client code in sending ajax msg. for security reason
 */
function setDepartmentOption(sId,clientProgramCode){ 
    // define constant
    var doOptionId="department";  // option id name
    // if sId == "nooption"  means no selection, this option should be empty 
    if (sId == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getDepartment_xml.jsp";
        url=url+"?id="+sId+"&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp.onreadystatechange=stateChangedForOption_department;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    } // end of if "nooption"
}




/*************************************************************************************/
/*
      function: client send information to get option value,
                when the result return, call the define function to get the data
      parameters:
         str: url parameter. eg: ProgramId for the record.
         clientProgramCode: prog client code in sending ajax msg. for security reason
 */
function setProgramOption(sId,clientProgramCode){ 
    // define constant
    var doOptionId="program";  // option id name
    alert("Your browser department id:"+sId);
    // if sId == "nooption"  means no selection, this option should be empty 
    if (sId == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getProgram_xml.jsp";
        
        url=url+"?id="+sId+"&clientprog="+clientProgramCode;
        
        // define the receive data function.
        xmlHttp.onreadystatechange=stateChangedForOption_program;
        
        xmlHttp.open("GET",url,true);
        
        xmlHttp.send(null);
        
    } // end of if "nooption"
}

function setProgramOptionView(sId,clientProgramCode){ 
    // define constant
    var doOptionId="program";  // option id name
    // if sId == "nooption"  means no selection, this option should be empty 
    if (sId == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getProgramview_xml.jsp";
        url=url+"?id="+sId+"&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp.onreadystatechange=stateChangedForOption_program;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    } // end of if "nooption"
}


/*
      function: client send information to get option value,
                when the result return, call the define function to get the data
      parameters:
         str: url parameter. eg: CourseId for the record.
         clientProgramCode: client prog code in ajax msg. for security reason
 */
function setCourseOption(sId,clientProgramCode){ 
    // define constant
    var doOptionId="course";  // option id name
    // if sId == "nooption"  means no selection, this option should be empty 
    if (sId == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp1=GetXmlHttpObject();
        if (xmlHttp1==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getCourse_xml.jsp";
        url=url+"?id="+sId+"&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp1.onreadystatechange=stateChangedForOption_course;
        xmlHttp1.open("GET",url,true);
        xmlHttp1.send(null);
    } // end of if "nooption"
}

function setCourseOptionView(sId,clientProgramCode){ 
    // define constant
    var doOptionId="course";  // option id name
    // if sId == "nooption"  means no selection, this option should be empty 
    if (sId == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp1=GetXmlHttpObject();
        if (xmlHttp1==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getCourseview_xml.jsp";
        url=url+"?id="+sId+"&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp1.onreadystatechange=stateChangedForOption_course;
        xmlHttp1.open("GET",url,true);
        xmlHttp1.send(null);
    } // end of if "nooption"
}

/*
      function: client send information to get option value,
                when the result return, call the define function to get the data
      parameters:
         str: url parameter. eg: CourseId for the record.
         clientProgramCode: client prog code in ajax msg. for security reason
 */
function setCourseScheduleOption(progid,courseid,clientProgramCode){ 
    // define constant
    var doOptionId="courseschedule";  // option id name
    // if sId == "nooption"  means no selection, this option should be empty 
    if (courseid == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getCourseSchedule_xml.jsp";
        url=url+"?progid="+progid+"&courseid="+courseid+"&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp.onreadystatechange=stateChangedForOption_CourseSchedule;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    } // end of if "nooption"
}

/*
     calll setProgramScheduleOption and setCourseOption
*/
function setPs_course_option(progid,clientProgramCode) {
	setProgramScheduleOption(progid,clientProgramCode);
	setCourseOption(progid,clientProgramCode);

}
/*
      function: client send information to get option value,
                when the result return, call the define function to get the data
      parameters:
         str: url parameter. eg: CourseId for the record.
         clientProgramCode: client prog code in ajax msg. for security reason
 */
function setProgramScheduleOption(progid,clientProgramCode){ 
    // define constant
    var doOptionId="programschedule";  // option id name
    // if sId == "nooption"  means no selection, this option should be empty 
    if (progid == "nooption") {
        // set option to null
        var progOption=document.getElementById(doOptionId);
        progOption.options.length=0;
        progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
    }
    else { // need ajax send data to server to get xml
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)  {
            alert("Your browser does not support AJAX!");
            return;
        }
        var url="jsp/common/getProgramSchedule_xml.jsp";
        url=url+"?progid="+progid+"&clientprog="+clientProgramCode;
        // define the receive data function.
        xmlHttp.onreadystatechange=stateChangedForOption_ProgramSchedule;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    } // end of if "nooption"
}


function stateChangedForOption_department() {
    stateChangedForOption("department","departments",xmlHttp);
    
}
function stateChangedForOption_program() {
    stateChangedForOption("program","programs",xmlHttp);
    
}

function stateChangedForOption_course() {
//  use xmlhttp1
    stateChangedForOption("course","courses",xmlHttp1);
}

function stateChangedForOption_CourseSchedule() {
    stateChangedForOption("courseschedule","courseschedules",xmlHttp);
    
}

function stateChangedForOption_ProgramSchedule() {
    stateChangedForOption("programschedule","programschedules",xmlHttp);  
}

/*
      function:  get the xml response from server
                and fill the option with xml data
            received xml format : data in 3 level
               eg.   <programs>
                          <program>
                              <id>
                              </id>
                              <name>
                              </name>
                           </program>
                     </program>
 */
function stateChangedForOption(doOptionName,xmlRootName,xmlhttp_in) { 
    // define variables
    /* for example
    var doOptionName="program";
    var xmlRootName="programs";
     */
    
    var MaxFieldsNumber=100;
    var xmlHttp;
    
    xmlHttp=xmlhttp_in
    
    if (xmlHttp.readyState==4)// request is complete
    {
        if (xmlHttp.status == 200) { // status is OK
            var xmldoc = xmlHttp.responseXML;
            
            var progOption=document.getElementById(doOptionName);
            progOption.options.length=0
            progOption.options[progOption.options.length] = new Option(showNoOptionStr,"nooption");
            
            var root = xmldoc.getElementsByTagName(xmlRootName).item(0);
            if (root==null) {
                
                // root is null
                // to see if there are errors from server
                // xml format <errors><error>----<error><errors>
                var errors=xmldoc.getElementsByTagName("errors").item(0);
                var errmsg=""; // error msg from ajax-xml server side.
                if (errors!=null) {
                    // have error message
                    for (var iNode = 0; iNode < errors.childNodes.length; iNode++) { // for root.son
                        // second level
                        var node = errors.childNodes.item(iNode);
                        var arr = new Array(MaxFieldsNumber);  // how many fields
                        var cnt = 0;
                        
                        for (var i = 0; i < node.childNodes.length; i++) { 
                            //third level
                            //var sib1Text = node.childNodes[i].data;
                            var sib1Text = node.childNodes.item(i).data;
                            arr[cnt] = sib1Text;
                            cnt++;
                            errmsg=errmsg+sib1Text+"\n";
                        }
                        
                    } // end for root.son
                    // show alert of errormsg
                    alert(errmsg);
                } 
                else {
                    // means empty data from server.
                    // do nothing.
                }
                
            }
            else  { 
                // root is not null
                for (var iNode = 0; iNode < root.childNodes.length; iNode++) { // for root.son
                    // second level
                    var node = root.childNodes.item(iNode);
                    var arr = new Array(MaxFieldsNumber);  // how many fields
                    var cnt = 0;
                    
                    for (var i = 0; i < node.childNodes.length; i++) { 
                        //third level
                        //var sib1Text = node.childNodes[i].childNodes[0].data;
                        var sib1Text = node.childNodes.item(i).childNodes[0].data;
                        arr[cnt] = sib1Text;
                        cnt++;
                    }
                    if (cnt>0)   setOption(doOptionName, arr);
                } // end for root.son
            } // end of root==null
            
        } 
        else {
            alert('There was a problem with the request.');
        }
        
    }
}



/*
     function:
         add one option of select box
      input prameter:
           optionname: optionId
           arr:  array of pair (thevalue, thename)
 */
function setOption(optionId,arr){
    var progOption=document.getElementById(optionId);
    var theName; 
    var theValue;
    
    theValue=arr[0];
    theName=arr[1];
    
    progOption.options[progOption.options.length] = new Option(theName,theValue);
}



/*  
     create XmlHttpObject in browser
 */
function GetXmlHttpObject(){
    var xmlHttp=null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
} 


