$(document).ready(function () {

    // remove on final release.
   // $("div#panel").slideDown("slow");

    // Expand Panel
    $("#open").click(function () {
        $("div#panel").slideDown("slow");

    });

    // Collapse Panel
    $("#close").click(function () {
        $("div#panel").slideUp("slow");
    });

    // Switch buttons from "Log In | Register" to "Close Panel" on click
    $("#toggle a").click(function () {
        $("#toggle a").toggle();
        $('#log').focus();
    });


    $('#lbl_login_error').hide();
    $('#lbl_register_error').hide();

    var userid = 0;

    $("#bt_login").click(function () {
        username = $('#log').val();
        password = $('#pwd').val();

        defaultParameters = "{username:'" + username + "',password:'" + password + "'}";

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/models/CareersTagged_local.asmx/ValidateUserLogin",
            data: defaultParameters,
            dataType: "json",
            timeout: 4000,
            success: function (msg) {
                userid = msg.d;
                if (userid > 0) {
                    $('#lbl_login_error').hide();
                    var pathname = window.location.pathname;
                    //window.location = pathname;

                    __doPostBack('loginredirect', defaultParameters);
                }
                else {
                    $('#lbl_login_error').show();
                    $('#lbl_login_error').text('* Incorrect login credntials')
                }
            },
            error: function (msg) {
                $('#lbl_login_error').show();
                $('#lbl_login_error').text('* Incorrect login credntials')
            }
        });
    });

    $("#bt_register").click(function () {
        username = $('#signup').val();
        password = $('#password').val();
        firstname = $('#firstname').val();
        lastname = $('#lastname').val();

        $('#signup_rq').text('*');
        $('#password_rq').text('*');

        allowSignUp = false;
        if (isValidEmailAddress(username) && (password != '')) {
            allowSignUp = true;
        }

        if (!allowSignUp) {
            $('#signup_rq').text('* not a valid email address');
            if (password == '')
                $('#password_rq').text('* Required');
        }
        else {

            defaultParameters = "{username:'" + username + "',password:'" + password + "',firstname:'" + firstname + "',lastname:'" + lastname + "'}";

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/models/CareersTagged_local.asmx/RegisterNewUser",
                data: defaultParameters,
                dataType: "json",
                timeout: 4000,
                success: function (msg) {
                    userid = msg.d;
                    if (userid == -1) {
                        $('#lbl_register_error').show();
                        $('#lbl_register_error').text('* This user already exists, please log in.')
                    }
                    else if (userid == 0) {
                        $('#lbl_register_error').show();
                        $('#lbl_register_error').text('* There was a problem with registration.')
                    }
                    else {
                        $('#lbl_register_error').hide();
                        __doPostBack('loginredirect', defaultParameters);
                    }
                },
                error: function (msg) {
                    $('#lbl_register_error').show();
                    $('#lbl_register_error').text('* There was a problem with registration.')
                }
            });

        }
    });


});


function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
};

