﻿$(function () {
    /*grab subjdata in JSON format from the acalc subjpage.*/
    $.ajax({ cache: false,
        type: "GET",
        dataType: "text json",
        url: "/acalc/subj.aspx",
        success: function (data) {
            /*add the subject information to the drop down list*/
            $.each(data.subjects, function () {
                $('#stdy').append('<option value="' + this.id + '">' + this.subj + '</option>');
            });
        }
    });
    /*check for the wiNam*/
    chkNam();
    /*hide a couple of divs.*/
    $('#wt').hide();
    $('#fsEm').hide();
    /*build and add a date to the start date field*/
    var tDat = new Date();
    var day = tDat.getDate();
    var month = tDat.getMonth() + 1;
    var year = tDat.getFullYear();
    $('#stDat').val(month + "/" + day + "/" + year);

    /*when the form is submitted, build a url, grab the data from that url and append it to our page*/
    $('#subFm').click(function () {
        $('#subFm').val('Recalulate');
        var sDat = $("#stDat").val();
        var eDat = $("#enDat").val();
        var sbj = $("#stdy").val();
        /*this sets the window.name property to the url.  used for navigating back to the acalc*/
        window.top.name = 'http://library.uncg.edu/acalc/str.aspx?stDat=' + sDat + '&enDat=' + eDat + '&stdy=' + sbj + ' div#aCal';
        $('#cntnr').empty();
        $('<div id="info" />').load('http://library.uncg.edu/acalc/str.aspx?stDat=' + sDat + '&enDat=' + eDat + '&stdy=' + sbj + ' div#aCal', function () {
            $(this).hide()
                .appendTo('#cntnr')
                .fadeIn()
        });
        $('#fsEm').slideDown(700);
        return false;
    })

    /*when the email button is clicked, build a url, grab the data from that url and append it to our page*/
    $('#subEm').click(function () {
        var fsDat = $("#stDat").val();
        var feDat = $("#enDat").val();
        var fSbj = $("#stdy").val();
        var fChk = $("#chk").val();
        var sMl = $("#sel:checked").val();
        var fn = $("#fNm").val();
        var ln = $("#lNm").val();
        var eml = $("#eMl").val();
        $('#subEm').attr("disabled", true);
        $('#cntnr').empty();
        $('#wt').fadeIn("slow");
        $('<div id="info" />').load('http://library.uncg.edu/acalc/mlr.aspx?chk=' + fChk + '&stdy=' + fSbj + '&stDat=' + fsDat + '&enDat=' + feDat + '&sel=' + sMl + '&fn=' + fn + '&ln=' + ln + '&em=' + eml + ' div#aCal', function () {
            $('#wt').hide();
            $('#subEm').attr("disabled", false);
            $(this).hide()
                .appendTo('#cntnr')
                .slideDown(500);
        });

        return false;
    })
});

/*this function checks to see if window.name has a value, if it does, then reload the web page*/
function chkNam() {
    var wiNam = (window.top.name);
    if (wiNam != '') {
        $('#cntnr').empty();
        $('<div id="info" />').load(wiNam, function() {
            $(this).hide()
                .appendTo('#cntnr')
                .fadeIn()
        });
    }
}

