﻿$(function() {

    $.fx.speeds._default = 1000;
    var meno = $("#otazMeno"),
        password = $("#textPassword");

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    $("#LoginForm").dialog({
        autoOpen: false,
        show: "fade",
        hide: "fade",
        modal: true,
        width: 400,
        buttons: {
            "Prihlásiť": function() {
                Login();
                $(this).dialog("close");
            },
            "Späť": function() {
                $(this).dialog("close");
            }
        },
        close: function() {
            $("#textLogin").val("");
            $("#textPassword").val("");
        }
    });

    $("#loginref")
		    .click(function() {
		        $("#LoginForm").dialog("open");
		    });
});

function SendLogin() {
    $.ajax({
        url: "Sys.Services.AuthenticationService/login",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
//        data: '{"userName":"' + $('#textLogin').val() + '", "password":"' + $('#textPassword').val() + '", "createPersistentCookie":false}',
        data: '{"userName":"Zabka", "password":"init123", "createPersistentCookie":false}',
//        Sys.Services.AuthenticationService.login($('#textLogin').val(), $('#textPassword').val(), false, null, null,
//            LoginServiceCompleted, LoginServiceFailed, "Context Info"),
        success: function(result) {
            if (result.d)
                alert('Great! You successfully logged in.');
//                $(".alertmsg").html("Valid Data");
            else
                $(".alertmsg").html("Invalid Data");
//                alert('Oops! You gave us bad credentials!');
            },
        error: function(xhr, status, error) {
            alert("An error has occurred during processing: " + error);
        }
    });
}

function Login() {
    Sys.Services.AuthenticationService.login($('#textLogin').val(), $('#textPassword').val(), false, null, null,
                LoginServiceCompleted, LoginServiceFailed, "Context Info");
}

function LoginServiceFailed(error, userContext, methodName) {
    alert('Nastala chyba počas prihlasovania:\n\n' + error);
}

function LoginServiceCompleted(validCredentials, userContext, methodName) {
    if (validCredentials) {
        $.ajax({
        url: "IndexService.asmx/CreateMenu",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function(result) {
            $("#panelMenu").empty().html(result.d);
//                $("#panelMenu").append(result.d);
            }
        });
//        alert('Ste prihlásený.');
    } else {
        alert('Nesprávne prihlasovacie údaje!');
    }
}

function Logout() {
    Sys.Services.AuthenticationService.logout(null, LogoutServiceCompleted, LoginServiceFailed, "Context Info");
}

function LogoutServiceCompleted(result, userContext, methodName) {
    $.ajax({
    url: "IndexService.asmx/CreateMenu",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            $("#panelMenu").empty().append(result.d);
    }
    });
//    alert('Boli ste práe odhlásený z web stránky.');
}

$(function() {

    $.fx.speeds._default = 1000;
    var passwOrig = $("#textPasswordOrig"),
        passwNew = $("#textPasswordNew"),
        passwConf = $("#textPasswordConfirm");

    var nazov = $("#zaradNazov"),
        content = $("#zaradPopis"),
	    allFields = $([]).add(passwOrig).add(passwNew).add(passwConf),
	    tips = $(".validateTips");

    function updateTips(t) {
        tips.text(t).addClass("ui-state-highlight");
        setTimeout(function() { tips.removeClass("ui-state-highlight", 1500); }, 500);
    }

    function checkRequired(o, n) {
        if (o.val().length < 1) {
            o.addClass("ui-state-error");
            updateTips("Údaj: " + n + " je povinný.");
            return false;
        } else {
            return true;
        }
    }

    $("#ChangePasswordForm").dialog({
        autoOpen: false,
        show: "fade",
        hide: "fade",
        modal: true,
        width: 400,
        buttons: {
            "Zmeniť": function() {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkRequired(passwOrig, "#textPasswordOrig");
                bValid = bValid && checkRequired(passwNew, "#textPasswordNew");
                bValid = bValid && checkRequired(passwConf, "#textPasswordConfirm");
                if (bValid) {
                    ChangePassword();
                    $(this).dialog("close");
                }
            },
            "Späť": function() {
                $(this).dialog("close");
            }
        },
        close: function() {
            $("#textPasswordOrig").val("");
            $("#textPasswordNew").val("");
            $("#textPasswordConfirm").val("");
        }
    });
});

function ChangePasswordOpen() {
    $("#ChangePasswordForm").dialog("open");
}

function ChangePassword() {
    var passw = new Object();
    passw.PasswordOrig = $("#textPasswordOrig").val();
    passw.PasswordNew = $("#textPasswordNew").val();
    passw.PasswordConfirm = $("#textPasswordConfirm").val();

    //Define DTO i.e. Data Transfer Object as follows
    var DTO = { 'passwData': passw };

    $.ajax({
        type: "POST",
        url: "NehnSpravaService.asmx/ChangePassword",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(DTO),
        success: function(msg) {
            alert("Vaše heslo bolo zmenené: ");
        },
        error: function(xhr, status, error) {
            alert("An error has occurred during processing: " + error);
        }
    });
}

