﻿/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" />

var $schoolLookupDialog;
var $windowHeight;
var $windowWidth;
var $windowscrollTop;
var $windowscrollLeft;
var $newWindowHeight;
var $newWindowWidth;
var $dataDiv;
var $selectSchoolLink;
var $schoolType;

var $txtHighSchoolName;
var $txtHiddenHighSchoolName;
var $txtHiddenHighSchoolCode;
var $txtHiddenHighSchoolCity;
var $txtHiddenHighSchoolState;
var $txtHiddenHighSchoolZip;

var $txtCollegeName;
var $txtHiddenCollegeName;
var $txtHiddenCollegeCode;
var $txtHiddenCollegeCity;
var $txtHiddenCollegeState;
var $txtHiddenCollegeZip;

var $txtOtherCollegeName;
var $txtHiddenOtherCollegeName;
var $txtHiddenOtherCollegeCode;
var $txtHiddenOtherCollegeCity;
var $txtHiddenOtherCollegeState;
var $txtHiddenOtherCollegeZip;

//###################################################################################################################
function openDialog($theOverlay, $theDialog) {
    $theOverlay
        .addClass("modal-screen")
        .show();
    $("body").addClass("blocked");
    $schoolLookupDialog
        .centerInClient()
        .fadeIn("slow", setFocusToSchoolInput);
}
//###################################################################################################################
function setFocusToSchoolInput() {
    $("#txtSchoolName").focus();
}
//###################################################################################################################
function closeDialog($theOverlay, $theDialog) {
    $dataDiv.slideUp("fast");
    $("#txtSchoolName").val("");
    clearTable();
    $theDialog.fadeOut("fast");
    $("body").removeClass("blocked");
    $theOverlay.removeClass("modal-screen").hide();
}
//###################################################################################################################
function loadMatchingSchoolsTable($schoolType) {

    var $SchoolNameFragment = $("#txtSchoolName").val();
    var $stateAbbreviation = $("#ddlSchoolState").val();

    if ($SchoolNameFragment.length < 3) {
        alert("Please enter at least 3 characters in the School Name search field.");
        return;
    }

    $.ajax({
        type: 'GET',
        url: 'handlers/getSchools.ashx',
        data: { schoolType: $schoolType,
            schoolNameFragment: $SchoolNameFragment,
            stateAbbreviation: $stateAbbreviation
        },
        success: function(schoolsList) {
        refreshTable(schoolsList);
        }
    });
}
//###################################################################################################################
function refreshTable(schools) {
    if (schools !== "[]") {
        $dataDiv.slideUp("fast");
        clearTable();
        $("#schoolTemplate").tmpl(schools).appendTo("#schoolsTable tbody");
        $dataDiv.slideDown("fast");
    }
    else {
        $dataDiv.slideUp("fast");
    }
    $("#NoSchool").show();
}
//###################################################################################################################
function clearTable() {
    $("#schoolsTable tbody tr").remove();
}
//###################################################################################################################
function changeHeading(newHeading) {
    $("#schoolLookupDialog h3").text(newHeading);
}
//###################################################################################################################
function populateHighSchoolFields(schoolName, schoolCode, schoolCity, schoolState, schoolZipCode) {
    $txtHighSchoolName.val(schoolName);
    $txtHiddenHighSchoolName.val(schoolName);
    $txtHiddenHighSchoolCode.val(schoolCode);
    $txtHiddenHighSchoolCity.val(schoolCity);
    $txtHiddenHighSchoolState.val(schoolState);
    $txtHiddenHighSchoolZip.val(schoolZipCode);
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
function populateCollegeFields(schoolName, schoolCode, schoolCity, schoolState, schoolZipCode) {
    $txtCollegeName.val(schoolName);
    $txtHiddenCollegeName.val(schoolName);
    $txtHiddenCollegeCode.val(schoolCode);
    $txtHiddenCollegeCity.val(schoolCity);
    $txtHiddenCollegeState.val(schoolState);
    $txtHiddenCollegeZip.val(schoolZipCode);
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
function populateOtherCollegeFields(schoolName, schoolCode, schoolCity, schoolState, schoolZipCode) {
    $txtOtherCollegeName.val(schoolName);
    $txtHiddenOtherCollegeName.val(schoolName);
    $txtHiddenOtherCollegeCode.val(schoolCode);
    $txtHiddenOtherCollegeCity.val(schoolCity);
    $txtHiddenOtherCollegeState.val(schoolState);
    $txtHiddenOtherCollegeZip.val(schoolZipCode);
}
//###################################################################################################################
$(document).ready(function() {

    $schoolLookupDialog = $("#schoolLookupDialog");
    $dataDiv = $("#dataDiv");

    //var $chkSchoolNotFound = $("#chkSchoolNotFound");
    var $overlay = $("#overlaySchoolSearch");
    var $btnSearchSchool = $("#btnSearchSchool");
    var $dialogCancelLink = $("#schoolLookupDialog #dialogCancelLink");
    var $selectSchoolLink = $("#schoolLookupDialog #dataDiv #schoolsTable tbody td a");
    var $ddlStates = $("#ddlStates");
    var $ddlSchoolState = $("#ddlSchoolState");

    ////////////////////////////////
    var $lnkSchoolNotFound = $("#lnkSchoolNotFound");

    $txtHighSchoolName = $("#txtHighSchoolName");
    $txtHiddenHighSchoolName = $("#txtHiddenHighSchoolName");
    $txtHiddenHighSchoolCode = $("#txtHiddenHighSchoolCode");
    $txtHiddenHighSchoolCity = $("#txtHiddenHighSchoolCity");
    $txtHiddenHighSchoolState = $("#txtHiddenHighSchoolState");
    $txtHiddenHighSchoolZip = $("#txtHiddenHighSchoolZip");

    $txtCollegeName = $("#txtCollegeName");
    $txtHiddenCollegeName = $("#txtHiddenCollegeName");
    $txtHiddenCollegeCode = $("#txtHiddenCollegeCode");
    $txtHiddenCollegeCity = $("#txtHiddenCollegeCity");
    $txtHiddenCollegeState = $("#txtHiddenCollegeState");
    $txtHiddenCollegeZip = $("#txtHiddenCollegeZip");

    $txtOtherCollegeName = $("#txtOtherCollegeName");
    $txtHiddenOtherCollegeName = $("#txtHiddenOtherCollegeName");
    $txtHiddenOtherCollegeCode = $("#txtHiddenOtherCollegeCode");
    $txtHiddenOtherCollegeCity = $("#txtHiddenOtherCollegeCity");
    $txtHiddenOtherCollegeState = $("#txtHiddenOtherCollegeState");
    $txtHiddenOtherCollegeZip = $("#txtHiddenOtherCollegeZip");

    $txtHighSchoolName.attr("disabled", "disabled");
    $txtCollegeName.attr("disabled", "disabled");
    $txtOtherCollegeName.attr("disabled", "disabled");
    ////////////////////////////////

    var $btnHSLookup = $("#btnHSLookup");
    var $btnCollegeLookup = $("#btnCollegeLookup");
    var $btnOtherCollegeLookup = $("#btnOtherCollegeLookup");

    $btnHSLookup.click(function($e) {
        $e.preventDefault();
        $schoolType = "HighSchool";
        changeHeading("High School Lookup");
        var $schoolSate = $ddlStates.val();
        if ($schoolSate === "") {
            $btnSearchSchool.disable();
        }
        else {
            $btnSearchSchool.enable();
        }
        $ddlSchoolState.val($schoolSate);
        openDialog($overlay, $schoolLookupDialog);
    });

    $btnCollegeLookup.click(function($e) {
        $e.preventDefault();
        $schoolType = "College";
        changeHeading("College Lookup");
        openDialog($overlay, $schoolLookupDialog);
    });

    $btnOtherCollegeLookup.click(function($e) {
        $e.preventDefault();
        $schoolType = "OtherCollege";
        changeHeading("Other College Lookup");
        openDialog($overlay, $schoolLookupDialog);
    });

    $btnSearchSchool.live('click', function($e) {
        $e.preventDefault();
        loadMatchingSchoolsTable($schoolType);
    });

    $lnkSchoolNotFound.live('click', function() {
        if ($schoolType === "HighSchool") {
            $txtHighSchoolName.removeAttr("disabled").focus();
        }
        if ($schoolType === "College") {
            $txtCollegeName.removeAttr("disabled").focus();
        }
        if ($schoolType === "OtherCollege") {
            $txtOtherCollegeName.removeAttr("disabled").focus();
        }
        $("#NoSchool").hide();
        closeDialog($overlay, $schoolLookupDialog);
    });

    $txtHighSchoolName.blur(function() {
        if (!$(this).is(":disabled")) {
            if ($(this).val().length > 0) {
                populateHighSchoolFields($(this).val(), "0", "", "", "");
            }
        }
    });

    $txtCollegeName.live("blur", function() {
        if (!$(this).is(":disabled")) {
            if ($(this).val().length > 0) {
                populateCollegeFields($(this).val(), "0", "", "", "");
            }
        }
    });

    $txtOtherCollegeName.live("blur", function() {
        if (!$(this).is(":disabled")) {
            if ($(this).val().length > 0) {
                populateOtherCollegeFields($(this).val(), "0", "", "", "");
            }
        }
    });

    $ddlSchoolState.live('blur', function() {
        var $selectedState = $ddlSchoolState.val();
        if ($selectedState === '-Choose-' || $selectedState === '') {
            $btnSearchSchool.disable();
        }
        else {
            $btnSearchSchool.enable();
            $("#txtSchoolName").focus();
        }
    });

    $dialogCancelLink.live('click', function($e) {
        $e.preventDefault();
        $("#NoSchool").hide();
        closeDialog($overlay, $schoolLookupDialog);
    });

    $selectSchoolLink.live('click', function($e) {
        $e.preventDefault();
        var $parent = $(this).parent().parent();
        var $schoolName = $parent.find("td:eq(1)").html()
        var $schoolCode = $parent.find("td:eq(6)").html()
        var $schoolCity = $parent.find("td:eq(3)").html()
        var $schoolState = $parent.find("td:eq(4)").html()
        var $schoolZipCode = $parent.find("td:eq(5)").html()
        if ($schoolType === "HighSchool") {
            populateHighSchoolFields($schoolName, $schoolCode, $schoolCity, $schoolState, $schoolZipCode);
        }
        if ($schoolType === "College") {
            populateCollegeFields($schoolName, $schoolCode, $schoolCity, $schoolState, $schoolZipCode);
        }
        if ($schoolType === "OtherCollege") {
            populateOtherCollegeFields($schoolName, $schoolCode, $schoolCity, $schoolState, $schoolZipCode);
        }
        $("#NoSchool").hide();
        closeDialog($overlay, $schoolLookupDialog);
    });

    //    $(window).scroll(function() {
    //        $schoolLookupDialog.centerInClient();
    //    });

    //    $(window).resize(function() {
    //        $schoolLookupDialog.centerInClient();
    //    });
});

