$(function() {
    $(document).ready(function() {

        $("input#ctl00_password").bind("keydown", function(event) {
            // track enter key
            var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
            if (keycode == 13) { // keycode for enter key
                // force the 'Enter Key' to implicitly click the Update button

                clickTo("input#ctl00_user", "input#ctl00_password");

                return false;
            } else {
                return true;
            }
        }); // end of function
    });
    $(document).ready(function() {

        $("input#ctl00_content_password").bind("keydown", function(event) {
            // track enter key
            var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
            if (keycode == 13) { // keycode for enter key
                // force the 'Enter Key' to implicitly click the Update button

                clickTo("input#ctl00_content_user", "input#ctl00_content_password");

                return false;
            } else {
                return true;
            }
        }); // end of function
    });    
    //pagina di login 
    $(".buttonLogin").click(function() {

        clickTo("input#ctl00_content_user", "input#ctl00_content_password");
        return false;
    });
    //pagina master
    $(".accediBtn").click(function() {
        clickTo("input#ctl00_user", "input#ctl00_password");
        return false;
    });
    $(".logoutBtn").click(function() {
        $.ajax({
            url: servicePathLogout,
            data: "{ 'user': '" + $("input#ctl00_password").val() + "', 'password': '" + $("input#ctl00_password").val() + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function(data) {
                if (data.d != "") {
                    window.location.href = base + "/login.aspx";
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        });
        return false;
    });
});
function clickTo(user, password) {
    $.ajax({
    url: servicePath,
        data: "{ 'user': '" + $(user).val() + "', 'password': '" + $(password).val() + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            if (data.d != "") {
                //var port = (jQuery.url.attr("port") != "") ? ":" + jQuery.url.attr("port") : "";
                //window.location.href = jQuery.url.attr("protocol") + "://" + jQuery.url.attr("host") + port + "/" + jQuery.url("host") + "/" + data.d;
                window.location.href = base + "/" + data.d;
            } else {
                window.location.href = base + "/registrazioneUser.aspx";
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // alert(XMLHttpRequest);
        }
    });
    return false;
}
//runOnLoad(function() {
//    $("input#email").select().focus();
//});

