﻿var total=0;
var searchLink;
var majors = '';
var submajors = '';
var locations = '';
var degrees = '';
var submajorslimit = 10;
var ugsearchHandler = "/Ajax/UndergradAdvancedSearchHandler.ashx";

try 
{
    $(document).ready(function () {
        if (isUndergrad()) {
            jQuery(".adv_btn").colorbox({ onOpen: function () { loadMajors(); }, width: "810px", inline: true, href: "#advSearchFormUndergrad" });
        }
    });
} 
catch (e){}

function loadMajors() 
{
    $.getJSON(ugsearchHandler+"?mode=majors&id=" + uniqid(), null, function(data) {$.each(data, function(index, optionData) {createCheckbox("majors", optionData.Name, optionData.ID, "majors");});

        $('#majors input').change(function() { majorsOnChange(); });

        //$('#uglocations input').change(function() { updateUgCount(); });

        $('#ugdegrees input').change(function() { updateUgCount(); });

    });
}
function majorsOnChange() 
{
    ClearOptions('submajors');
    //ClearOptions('uglocations');
    majors = GetCheckboxListValues('majors');
    if (majors == "") {
        document.getElementById("submajors").innerHTML = '<div style="padding:3px;"Complete step 1 before selecting a program.</div>';
        changeUgCount(0);
        return;
    }
    var url = ugsearchHandler + "?mode=submajors&majors=" + majors;
    $.getJSON(url, null, function(data) {

        $.each(data, function(index, optionData) {
        createCheckbox("submajors", optionData.Name, optionData.ID, "submajors");

        });
        $('#submajors input').change(function() {
            submajorsOnChange();
        });
    });
}
function submajorsOnChange() {
    submajors = GetCheckboxListValues('submajors');
    var length = submajors.split(',').length;
    if (length > submajorslimit) {
        msgug('Please limit your selection to' + submajorslimit + ' majors or less. You currently have chosen ' + length + ' majors.');
    }
    if (submajors != "" && length <= submajorslimit) {
        msgug('clear');
        updateUgCount();
        //updateUgLocations();
    }
    else {
        //ClearOptions('uglocations');
        changeUgCount(0);
    }
}
//function updateUgLocations() {
//    ClearOptions('uglocations');
//    majors = GetCheckboxListValues('majors');
//    submajors = GetCheckboxListValues('submajors');

//    var url = ugsearchHandler + "?mode=locations&submajors=" + submajors;
//    var i = 0;
//    var currCountryCode = "";
//    $.getJSON(url, null, function(data) {
//        $.each(data, function(index, json) {
//            i++;
//            if (i == 1) createCheckbox("uglocations", "All Locations", "", "uglocations");

//            if (json.CountryCode != currCountryCode) {
//                createCheckbox("uglocations", json.Country, json.Country, "uglocations");
//                currCountryCode = json.CountryCode;
//            }
//            if (json.State != "" && json.StateCode != "") {
//                createCheckbox("uglocations", '-' + json.State, json.StateCode, "uglocations");
//            }

//        });
//        $('#uglocations input').change(function() {
//            UnselectUgLocations($(this).val());
//            updateUgCount();
//        });
//    });
//}

function updateUgCount() 
{
    var url = ugsearchHandler + "?mode=search&" + getUgSearchQuery();
    $.getJSON(url, null, function(data) {
        if (parseInt(data) != "NaN") {
            total = parseInt(data);
            changeUgCount(total);
        }
    });
}
function changeUgCount(total) 
{
    document.getElementById('ugschoolCount').innerHTML = total;
    $('#ugschoolCount').fadeIn('slow');
}
function createCheckbox(name, labelText, value, parentId) {
    if (!labelText) return;
    var lb = document.createElement("label");
    var cb = document.createElement("input");
    cb.setAttribute("type", "checkbox");
    cb.setAttribute("name", name);
    cb.setAttribute("value", value);
    lb.appendChild(cb);
    lb.innerHTML = lb.innerHTML + labelText;
    var item = document.getElementById(parentId);
    item.appendChild(lb);
}
function ClearOptions(item) {
    try {
        var itm = document.getElementById(item);
        if (itm.hasChildNodes) {

            itm.removeChild(itm.firstChild);
            ClearOptions(item);
        }
    } catch (e) { }
}
//function GetCheckboxListValues(parentId) {
//    var value = "";
//    var parent = document.getElementById(parentId);
//    for (var i = 0; i < parent.childNodes.length; i++) {
//        var child = parent.childNodes[i].childNodes[0];
//        if (child) {
//            if (child.checked) {
//                value = value + child.value + ",";
//            }
//        }
//    }
//    value = value.substring(0, value.length - 1);
//    return value;
//}

function GetCheckboxListValues(parentId) {
    var value = "";
    var selector = '#' + parentId + ' input';
    $(selector).each(function () {
        var checked = $(this).attr('checked');
        if (checked) {
            value = value + $(this).val() + ",";
            //console.log(value);
        }
    });
    value = value.substring(0, value.length - 1);
    return value;
}



//function UnselectUgLocations(itemValue) 
//{
//    var parent = document.getElementById('uglocations');
//    if (itemValue == "") 
//    {
//        for (var i = 0; i < parent.childNodes.length; i++) {
//            var child = parent.childNodes[i].childNodes[0];
//            if (child) {
//                child.checked = false;
//            }
//            parent.firstChild.firstChild.checked = true;
//        }
//    }
//    else 
//    {
//        parent.firstChild.firstChild.checked = false;
//    }
//    
//}
function getUgSearchQuery() {
    majors = GetCheckboxListValues("majors");
    submajors = GetCheckboxListValues("submajors");
    //locations = GetCheckboxListValues("uglocations");
    degrees = GetCheckboxListValues("ugdegrees");
    //alert(degrees);
    // return "majors=" + majors + "&submajors=" + submajors + "&locations=" + locations + "&degrees=" + degrees + "&c=UG";
    return "majors=" + majors + "&submajors=" + submajors + "&locations=" +"&degrees=" + degrees + "&c=UG";
}
function SearchUg(searchField) {
    majors = GetCheckboxListValues("majors");
    submajors = GetCheckboxListValues("submajors");
    // locations = GetCheckboxListValues("uglocations");
    degrees = GetCheckboxListValues("ugdegrees");

    if (!majors && !submajors) 
    {
        msgug("Please complete Steps 1 and 2 above by selecting a subject area and a major.");
        return;
    }
    if (!submajors) {
        msgug("Please complete Step 2 above by selecting a major.");
        return;
    }
    else {
        var length = submajors.split(',').length;
        if (length > submajorslimit) { return; }
    }
    if (total == 0) 
    {
        msgug("Your query returned 0 results, try again");
        return;
    }
    else 
    {
        searchLink = "/college-search/searchresults.aspx?" + getUgSearchQuery();
        //searchLink = "/college-search/faceted-search.aspx?" + getUgSearchQuery(); // FS TBD: We will need to update it with the searchresults url before deployment
        document.location.href = searchLink;
    }
    
}
function uniqid() {
    var newDate = new Date;
    return newDate.getTime();
}
function msgug(msg) 
{
    if (msg != "clear") {

        $('#ugerror').html(msg);
        $('#ugerror').show();
    }
    else {
        $('#ugerror').hide('100');
    }
}
function isUndergrad() {
    return document.location.href.indexOf('college-search') > 0;

}
