﻿$(document).ready(function () {
    $('#alCloseBtn').click(function () {
        $('#divagentlogin').fadeOut();
    });

    $('#btnAgentSignIn').click(function () {
        var userid = $('#frmAgentLogin').find("input[name='userid']");
        var pw = $('#frmAgentLogin').find("input[name='password']");

        if (userid.attr('value') && pw.attr('value')) {
            var txtFrom = $('#frmAgentLogin').find("input[name='txtFrom']");
            txtFrom.attr('value', '');
            $('#frmAgentLogin').submit();
        }
        else {
            if (!userid.attr('value')) {
                $('#alError').text('Please enter your User ID in the \"User ID"\ box.');
                userid.focus();
                return false;
            }

            if (!pw.attr('value')) {
                $('#alError').text('Please enter a password.');
                pw.focus();
                return false;
            }
        }
    });

    $('#frmAgentLogin input').keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#btnAgentSignIn').click();
            return false;
        } else {
            return true;
        }
    });

    $('#lnkAgentForgotPW').click(function () {
        var userid = $('#frmAgentLogin').find("input[name='userid']");

        if (userid.attr("value")) {
            var txtFrom = $('#frmAgentLogin').find("input[name='txtFrom']");
            txtFrom.attr("value", "forgotpw");
            $('#frmAgentLogin').submit();
        }
        else {
            $('#alError').text('Please enter your User ID in the \"User ID"\ box.');
            userid.focus();
        }
    });
});

function toggleAgentLoginBox(elem) {
    if ($("#divagentlogin").css('display') == 'none') {
        elem = $(elem);
        //get the position of the placeholder element
        var pos = elem.offset();
        var width = elem.width();
        var height = elem.height();
        //show the menu directly over the placeholder
        $("#divagentlogin").css({ "left": (pos.left + width - 5) + "px", "top": pos.top + "px" });
        $('#divagentlogin').fadeIn();
    }
    else {
        $('#divagentlogin').fadeOut();
    }

    return false;
    
}
