/**
 * Variable to avoid having to hard-code the web context across the script.
 */
var webContext = "/conap";


/**
 * Perform when the page is ready:
 */
$(document).ready(function() {
    $.ajaxSetup({cache : false});   // generate a timestamp parameter to prevent browser caching

    $("#school-actual-enrolldate-input").livequery(function() {
        //noinspection JSUnusedLocalSymbols
        $(this).datepicker({
            showOn: "both",  // show when they click on the field or the icon
            buttonImage: webContext + "/images/calendar.gif",
            buttonImageOnly: true,
            minDate: '-0d', // no past dates
            onSelect: function(datepickerValue, datepickerObject) {
                setActualEnrollmentDate(datepickerValue);
            }
        });
    }).addClass("embed");

    $("#expected-entry-date").livequery(function() {
        //noinspection JSUnusedLocalSymbols
        $(this).datepicker({
            showOn: "both",  // show when they click on the field or the icon
            buttonImage: webContext + "/images/calendar.gif",
            buttonImageOnly: true,
            minDate: '-0d' // no past dates
        });
    }).addClass("embed");

    $("#school-states-select").livequery(function() {
        //noinspection JSUnresolvedFunction
        $(this).change(function() {
            showSchools($("#school-states-select").val());
        });
    });

    $("#soldier-schools-select").livequery(function() {
        //noinspection JSUnresolvedFunction
        $(this).change(function() {
            sendLoginLink();
        });
    });

});


/**
 * Shows the application that the soldier submitted to the school.
 * @param prId id of the student
 * @param schoolId id of the school
 * @param majorCd the soldier's desired major
 */
function showApplication(prId, schoolId, majorCd) {
    $("#conap-wait").show("slow");
    $("#school-message").html("Loading application ...");
    var params = {
        prId: prId,
        schoolId: schoolId,
        majorCd: majorCd
    };
    $.get(webContext + "/application/view-for-school.action", params, function(data) {
        $("#school-application-title").show();
        $("#school-application").html(data);
        $("#conap-wait").hide("slow");
        $("#school-message").html("Application loaded");
    });
}

/**
 * Sets the status of an application.
 * @param prId pr id of the soldier
 * @param schoolId id of the school
 * @param majorCd major code value
 * @param statusCd new status code
 */
function setApplicationStatus(prId, schoolId, majorCd, statusCd) {
    $("#conap-wait").show("slow");
    $("#school-message").html("Setting application status ...");
    var params = {
        prId    : prId,
        schoolId: schoolId,
        majorCd : majorCd,
        statusCd: statusCd
    };
    $.get(webContext + "/application/set-status.action", params, function(data) {
        $("#conap-wait").hide("slow");
        $("#school-message").html(data);
    });

}

/**
 * Acknowledges all of a school's applications.
 * @param schoolId id of the school
 */
function acknowledgeAll(schoolId) {
    $("#conap-wait").show("slow");
    $("#school-message").html("Acknowledging all applications ...");
    $.get(webContext + "/application/acknowledge-all.action", {schoolId: schoolId}, function(data) {
        $("#conap-wait").hide("slow");
        $("#school-message").html(data);
    });
}


/**
 * Sets the actual enrollment date of an application
 * @param datepickerValue
 */
function setActualEnrollmentDate(datepickerValue) {
    $("#school-message").html("Setting actual enrollment date ...");
    var prId = $("#soldier-prid").val();
    var schoolId = $("#school-schoolid").val();
    var majorCd = $("#application-majorcd").val();
    var params = {
        prId    : prId,
        schoolId: schoolId,
        majorCd : majorCd,
        actualEnrollmentDate: datepickerValue
    };
    $("#conap-wait").show("slow");
    $.get(webContext + "/application/set-actual-enrollment-date.action", params, function(data) {
        $("#conap-wait").hide("slow");
        $("#school-message").html(data);
        showApplication(prId, schoolId, majorCd);
    });
}


/**
 * Open new windows because _new & _blank are not strict xhtml 1.0
 */
$(function() {
    $("a.new-window").livequery(function() {
        $(this).attr({
            target: "conap-application"
        });
    });
});


/**
 * Generates Ajax get of the schools for the selected state.
 * @param stateCd
 */
function showSchools(stateCd) {
    $("#conap-wait").show('slow');
    $("#school-message").html("Retrieving schools ...");
    $.get(webContext + "/school/list.action", {stateCd: stateCd}, function(data) {
        $("#schools-select").html(data);
        $("#school-message").html("Schools retrieved");
        $("#conap-wait").hide('slow');
    });
}


/**
 * Requests the login link be sent to the school POC.
 */
function sendLoginLink() {
    $("#conap-wait").show('slow');
    $("#school-message").html("Sending login link ...");
    var schoolId = $("#soldier-schools-select").val();
    $.get(webContext + "/school/send-login-link.action", {schoolId: schoolId}, function(data) {
        $("#school-message").html("");
        $("#school-result").html(data);
        $("#conap-wait").hide('slow');
    });
}


/**
 * Search application functionality for schools.
 */
function searchApplications() {
    $("#conap-wait").show('slow');
    $("#school-message").html("Searching applications ...");
    var params = {
        schoolId: $("#school-id").val(),
        expectedEntryDate: $("#expected-entry-date").val(),
        lastName: $("#last-name").val(),
        statusCd: $("#status-cd").val()
    };
    $.get(webContext + "/application/search.action", params, function(data) {
        $("#search-results-title").show();
        $("#search-results").html(data);
        $("#conap-wait").hide("slow");
        $("#school-message").html("Search finished");
    });
}