﻿$.event.add(window, "resize", resizeFrame);

function resizeFrame() {
    var maskHeight = $(document).height();
    var maskWidth = $(document).width();
    //Set height and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
};

$(document).ready(function () {

    $('#loading_image').hide();

    $('#lnkLogin').click(function (e) {

        $('embed').hide();

        //Cancel the link behavior
        e.preventDefault();

        var id = $(this).attr('href');

        var maskHeight = $(document).height();
        var maskWidth = $(document).width();

        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect		
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.8);

        //Tweak this to keep the login window centered no matter what the screen size.
        $(id).css('top', '38%');
        $(id).css('left', '36%');

        //transition effect
        $(id).fadeIn(2500);

        $('#username').focus();
    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        e.preventDefault();
        $('#mask, .window').hide();
        $('embed').show();
    });

    //Handle the Cancel button images to make it act more like a real button
    var chkcancelmousedown = 0;

    $('#cancel').mousedown(function () {
        $('#cancel').attr('src', '../images/KVBLoginCancelSelect.png');
        chkcancelmousedown++;
    });
    $('#cancel').hover(function () {
        if (chkcancelmousedown > 0) {
            $('#cancel').attr('src', '../images/KVBLoginCancelSelect.png');
        };
    });
    $('#cancel').mouseout(function () {
        $('#cancel').attr('src', '../images/KVBLoginCancel.png');
    });

    //Handle the Login button images to make it act more like a real button
    var chkloginmousedown = 0;

    $('#login').mousedown(function () {
        $('#login').attr('src', '../images/KVBLoginSelect.png');
        chkloginmousedown++;
    });
    $('#login').hover(function () {
        if (chkloginmousedown > 0) {
            $('#login').attr('src', '../images/KVBLoginSelect.png');
        };
    });
    $('#login').mouseout(function () {
        $('#login').attr('src', '../images/KVBLogin.png');
    });


    $('body').mouseup(function () {
        if (chkcancelmousedown > 0) {
            $('#cancel').attr('src', '../images/KVBLoginCancel.png');
            chkcancelmousedown = 0;
        };
        if (chkloginmousedown > 0) {
            $('#login').attr('src', '../images/kvblogin.png');
            chkloginmousedown = 0;
        };
    });

    $('#login').click(function (e) {

        $('#loading_image').show();
        $('#active_panel').hide();

        var spChars = "'/*;-";
        var eCheck = 0;

        if ($.trim($('#username').val()) == '') {
            alert("The username cannot be blank.  Please correct your entry and try again.");
            eCheck++;
            $('#username').select();
            $('#username').focus();
            replaceBP();
            return false;
        }

        if ($.trim($('#password').val()) == '') {
            alert("The password cannot be blank.  Please correct your entry and try again.");
            eCheck++;
            $('#password').select();
            $('#password').focus();
            replaceBP();
            return false;
        }

        for (var i = 0; i < $('#username').val().length; i++) {
            if (spChars.indexOf($('#username').val().charAt(i)) != -1) {
                alert("The username you have entered contains an invalid character.  Please remove the following characters and try again: ' / * ; -");
                eCheck++;
                $('#username').select();
                $('#username').focus();
                replaceBP();
                return false;
            }
        }

        for (var i = 0; i < $('#password').val().length; i++) {
            if (spChars.indexOf($('#password').val().charAt(i)) != -1) {
                alert("The password you have entered contains an invalid character.  Please remove the following characters and try again: ' / * ; -");
                eCheck++;
                $('#password').select();
                $('#password').focus();
                replaceBP();
                return false;
            }
        }

        function replaceBP() {
            $('#loading_image').hide();
            $('#active_panel').show();
        }

        if (eCheck == 0) {
            $.ajax({
                type: "POST",
                url: "../LoginCheck.aspx",
                data: { username: $('#username').val(), password: $('#password').val() },

                success: function (response) {
                    //e: is used to designate an error
                    if (response.substring(0, 2) == "e:") {
                        alert(response.substring(2));
                        replaceBP();
                        $('#username').select();
                        $('#username').focus();
                    }
                    //s: is used to designate success
                    else if (response.substring(0, 2) == "s:") {
                        e.preventDefault();
                        $('#mask, .window').hide();

                        if (($('#CameFrom').val()) == null) {
                            location.reload();
                        }
                        else {
                            window.location = $('#CameFrom').val();
                        };
                    }
                },

                error: function (response) {
                    alert("There was an error accepting the username and password you entered.  Please contact your system administrator.");
                },
                datatype: "text"
            });
        }
    });

    $('#password').keypress(function (e) {
        if (e.which == 13) {
            $('#login').focus().click();
        }
    });
});
