﻿var xmlHttpReq;
if (window.XMLHttpRequest) { xmlHttpReq = new XMLHttpRequest(); }
else if (window.ActiveXObject) { xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); }

var AjaxServerPageName = "/ajax.aspx"

var DropDownList_Event;
var DropDownList_MW;
var DropDownList_Division;
var DropDownList_Conference;
var DropDownList_School;

var HiddenField_PreviousEvent;
var HiddenField_PreviousMW;
var HiddenField_PreviousPerformance;
var HiddenField_PreviousDivision;
var HiddenField_PreviousConference;
var HiddenField_PreviousSchool;

var HiddenField_Performance;
var HiddenField_Division;
var HiddenField_Conference;
var HiddenField_School;

function GetReferences() {

    var IDPrefix = 'ctl00_ContentPlaceHolder_PageContent1_Results1_'

    DropDownList_Event = getElement(IDPrefix + 'DropDownList_Events');
    DropDownList_MW = getElement(IDPrefix + 'DropDownList_MW');
    DropDownList_Division = getElement(IDPrefix + 'DropDownList_Division');
    DropDownList_Conference = getElement(IDPrefix + 'DropDownList_Conference');
    DropDownList_School = getElement(IDPrefix + 'DropDownList_School');

    HiddenField_PreviousEvent = getElement(IDPrefix + 'HiddenField_PreviousEvent');
    HiddenField_PreviousMW = getElement(IDPrefix + 'HiddenField_PreviousMW');
    HiddenField_PreviousPerformance = getElement(IDPrefix + 'HiddenField_PreviousPerformance');
    HiddenField_PreviousDivision = getElement(IDPrefix + 'HiddenField_PreviousDivision');
    HiddenField_PreviousConference = getElement(IDPrefix + 'HiddenField_PreviousConference');
    HiddenField_PreviousSchool = getElement(IDPrefix + 'HiddenField_PreviousSchool');

    HiddenField_Performance = getElement(IDPrefix + 'HiddenField_Performance');
    HiddenField_Division = getElement(IDPrefix + 'HiddenField_Division');
    HiddenField_Conference = getElement(IDPrefix + 'HiddenField_Conference');
    HiddenField_School = getElement(IDPrefix + 'HiddenField_School');

}

function LoadPreviousValues() {

    GetReferences();

    //alert('LoadPreviousValues');

    //alert('HiddenField_PreviousConference.value = ' + HiddenField_PreviousConference.value);

    SetDropDownValue(DropDownList_Event, HiddenField_PreviousEvent.value);
    SetDropDownValue(DropDownList_MW, HiddenField_PreviousMW.value);
    SetDropDownValue(DropDownList_Division, HiddenField_PreviousDivision.value);
    SetDropDownValue(DropDownList_Conference, HiddenField_PreviousConference.value);
    SetDropDownValue(DropDownList_School, HiddenField_PreviousSchool.value);

    HiddenField_Performance.value = HiddenField_PreviousPerformance.value;
    HiddenField_Division.value = HiddenField_PreviousDivision.value;
    HiddenField_Conference.value = HiddenField_PreviousConference.value;
    HiddenField_School.value = HiddenField_PreviousSchool.value;

    //alert('previous values loaded');

    //alert('HiddenField_Conference.value = ' + HiddenField_Conference.value);

}

function SetDropDownValue(myDropDown, myValue) {
    var i;
    for (i = 0; i < myDropDown.options.length; i++) {
        if (myDropDown.options[i].value == myValue) {
            if (myDropDown.selectedIndex != i) {
                //alert('index changed from ' + myDropDown.selectedIndex + ' to ' + i);
                myDropDown.selectedIndex = i;
            }
            break;
        }
    }
    //alert('index not changed (' + myDropDown.selectedIndex + ')');

}

function GetResultsQueryString(){
    var mystring = '';
    if (DropDownList_Event) {
        mystring = mystring + '&event=' + encodeURIComponent(DropDownList_Event.value);
    }
    if (DropDownList_MW) {
        mystring = mystring + '&mw=' + encodeURIComponent(DropDownList_MW.value);
    }
    if (DropDownList_Division) {
        mystring = mystring + '&division=' + encodeURIComponent(DropDownList_Division.value);
    }
    if (DropDownList_Conference) {
        mystring = mystring + '&conference=' + encodeURIComponent(DropDownList_Conference.value);
    }
    if (DropDownList_School) {
        mystring = mystring + '&school=' + encodeURIComponent(DropDownList_School.value);
    }
    return mystring;   
}
function EventChanged() {    

    //alert('EventChanged');

    GetReferences()

    GetConferences()

    //GetSchools()
    
}
function MWChanged() {

    //alert('MWChanged');

    GetReferences()

    GetConferences()

    //GetSchools()
    
}
function DivisionChanged() {

    //alert('DivisionChanged');

    GetReferences()

    GetConferences()

    //GetSchools()

    if (DropDownList_Division.value == '') {
        HiddenField_Conference.value = '';
        DropDownList_Conference.value = '';
        DropDownList_Conference.selectedIndex = 0;
        HiddenField_School.value = '';
        DropDownList_School.value = '';
        DropDownList_School.selectedIndex = 0;
    }
    
}
function ConferenceChanged() {

    //alert('ConferenceChanged');

    GetReferences()

    GetSchools()

    if (HiddenField_Conference && DropDownList_Conference) {
        HiddenField_Conference.value = DropDownList_Conference.value // fix for dropdowns forgetting their selected values
    }

    if (DropDownList_Conference.value == '') {
        HiddenField_School.value = '';
        DropDownList_School.value = '';
        DropDownList_School.selectedIndex = 0;
    }

    //alert("HiddenField_Conference.value = " + HiddenField_Conference.value);

}
function SchoolChanged() {

    GetReferences()
    
    if (HiddenField_School && DropDownList_School) {
        HiddenField_School.value = DropDownList_School.value // fix for dropdowns forgetting their selected values
    }

    //alert("HiddenField_School.value = " + HiddenField_School.value);
    
}
function GetConferences() {

    //alert('GetConferences');

    $('#loading').mask('Loading...');

    var requestUrl = AjaxServerPageName + "?action=GetConferences" + GetResultsQueryString()

    if (xmlHttpReq) {
        if (xmlHttpReq.readyState != 0) {
            xmlHttpReq.abort();
        }
        xmlHttpReq.open("GET", requestUrl, true);
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4 || xmlHttpReq.readyState == "complete") {
                ClearAndSetConferenceDropDown(xmlHttpReq.responseXML.documentElement);
                GetSchools()
            }
        }
        xmlHttpReq.send("");
    }
    else {
        //alert("xmlHttpReq = " + xmlHttpReq);
        $('#loading').unmask();
    }
   
}
function GetSchools() {

    //alert('GetSchools');

    $('#loading').mask('Loading...');

    var requestUrl = AjaxServerPageName + "?action=GetSchools" + GetResultsQueryString()

    if (xmlHttpReq) {
        if (xmlHttpReq.readyState != 0) {
            xmlHttpReq.abort();
        }
        xmlHttpReq.open("GET", requestUrl, true);
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4 || xmlHttpReq.readyState == "complete") {
                ClearAndSetSchoolDropDown(xmlHttpReq.responseXML.documentElement);
            }
        }
        xmlHttpReq.send("");
    }
    else {
        //alert("xmlHttpReq = " + xmlHttpReq);
        $('#loading').unmask();     
    }
    
}
function ClearAndSetConferenceDropDown(xmlDoc) {

    if (DropDownList_Conference) {

        var conferenceNodes;

        var optionItem;
        var optionName;
        var optionValue;

        var selectedItem = DropDownList_Conference.value;

        DropDownList_Conference.options.length = 0;

        conferenceNodes = xmlDoc.getElementsByTagName("conference");

        for (i = 0; i < conferenceNodes.length; i++) {
            optionName = conferenceNodes.item(i).getAttribute("name");
            optionValue = conferenceNodes.item(i).getAttribute("value");
            optionItem = new Option(optionName, optionValue, false, false);
            if (optionValue == selectedItem) {
                optionItem.selected = true;
            }
            DropDownList_Conference.options[DropDownList_Conference.length] = optionItem;
        }

    }

    $('#loading').unmask();
    
}
function ClearAndSetSchoolDropDown(xmlDoc)
{

    if (DropDownList_School) {

        var schoolNodes;

        var optionItem;
        var optionName;
        var optionValue;

        var selectedItem = DropDownList_School.value;

        DropDownList_School.options.length = 0;

        schoolNodes = xmlDoc.getElementsByTagName("school");

        for (i = 0; i < schoolNodes.length; i++) {
            optionName = schoolNodes.item(i).getAttribute("name");
            optionValue = schoolNodes.item(i).getAttribute("value");
            optionItem = new Option(optionName, optionValue, false, false);
            if (optionValue == selectedItem) {
                optionItem.selected = true;
            }
            DropDownList_School.options[DropDownList_School.length] = optionItem;
        }

    }

    $('#loading').unmask();
    
}
