﻿function LoginPost() {
    var username = $("#userName").val();
    var password = $("#password").val();
    var rememberMe = 0;
    if ($('#rememberMe').is(":checked")) {
        rememberMe = 1;
    }

    if (username == "Kullanıcı Adınız" && password == "______")
        return;
    if (username == "" && password == "")
        return;


    var returnUrl = $("#ReturnUrl").val() == null || $("#ReturnUrl") == "" ? "/Panorama/Index.aspx" : $("#ReturnUrl").val();
    var url = "/Account/LoginCheck.aspx?userName=" + username + "&password=" + password +"&rememberMe="+ rememberMe+ "&rnd=" + Math.random() + "&isDataProtected=false";
    $.ajax({
        url: url,
        type: "POST",
        dataType: "text/html",
        success: function (result) {
            var resultArray = result.split('/');
            var resultInt = resultArray[0];
            var userId = resultArray[1] == null ? null : resultArray[1];

            if (resultInt == -1)
                alert("Bir hata oluştu");
            else if (resultInt == 2)
                alert("Hatalı kullanıcı adı/şifre");
            else if (resultInt == 3)
                window.location.href = "/Account/Activation.aspx?userId="+userId;
            else if (resultInt == 1)
                window.location.href = returnUrl;
        },
        error: function (xhr) {
        }
    })
}

function LoginPostExternal() {
    var username = $("#userName").val();
    var password = $("#password").val();
    var rememberMe = 0;
    if ($('#rememberMe').is(":checked")) {
        rememberMe = 1;
    }

    if (username == "Kullanıcı Adınız" && password == "______")
        return;
    if (username == "" && password == "")
        return;


    var returnUrl = $("#ReturnUrl").val() == null || $("#ReturnUrl") == "" ? "/Panorama/Index.aspx" : $("#ReturnUrl").val();
    var url = "/Account/LoginCheckExternal.aspx?userName=" + username + "&password=" + password + "&rememberMe=" + rememberMe + "&rnd=" + Math.random() + "";
    $.ajax({
        url: url,
        type: "POST",
        dataType: "text/html",
        success: function (result) {
            var resultArray = result.split('/');
            var resultInt = resultArray[0];
            var userId = resultArray[1] == null ? null : resultArray[1];

            if (resultInt == -1)
                alert("Bir hata oluştu");
            else if (resultInt == 2)
                alert("Hatalı kullanıcı adı/şifre");
            else if (resultInt == 3)
                window.location.href = "/Account/RegisterForFantasyFootball.aspx?userId=" + userId;
            else if (resultInt == 1)
                window.location.href = returnUrl;
        },
        error: function (xhr) {
        }
    })
}



$(document).ready(function () {
    $('#userName').focus(function () {
        $("#userName").val("");
    });

    $('#userName').focusout(function () {
        if ($("#userName").val() == "") {
            $("#userName").val("Kullanıcı Adınız");
        }
    });

    $('#password').focus(function () {
        $("#password").val("");
    });

    $('#password').focusout(function () {
        if ($("#password").val() == "") {
            $("#password").val("______");
        }
    });

    $("#password").keypress(function (e) {
        if (e != null && e != undefined) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
                LoginPostExternal();
            }
        }
    });

    

});
