﻿$(document).ready(function () { LoginOnLoad(); });
function LoginOnLoad() {
    // Gestion des inputs Login et Mot de passe

    $("#login").focus(function () {
        if ($(this).val() == "Identifiant") {
            $(this).css("color", "#000").val("").select();
        }
    }).blur(function () {
        if ($.trim($(this).val()) == "") {
            $(this).val("Identifiant").css("color", "#8E8E8E");
        }
    });
    $("#motDePasse-clair").focus(function () {
        $("#motDePasse-clair").hide();
        $("#motDePasse-hidden").show();
        $("#motDePasse-hidden").focus();
    });
    $("#motDePasse-hidden").blur(function () {
        if ($.trim($(this).val()) == "") {
            $("#motDePasse-clair").show();
            $("#motDePasse-hidden").hide();
        }
    });
    $("#go").click(function () { VerifLogin(); });
    $("#LienDeconnexion").click(function () { Logout(); });
   

}
function LoadLogged() {

    $("#pnlLoginFormArea").html("<div id='h_go_espaceperso'><a href='/EspacePerso/Default.aspx'>Accéder à mon espace perso</a></div>");
    $("#pnlLoginLinkArea").html("<a id='LienDeconnexion'>Déconnexion</a>");
    $("#LienDeconnexion").click(function () {  Logout(); });

}
function Logout() {
    $.cookie('EPC', null, { expires: -1, path: '/', domain: 'entreparticuliers.com' })
    $.cookie('EPC', null, { expires: -1, path: '/' });

    $.ajax({
        timeout: 40000,
        type: "POST",
        url: "/Xaja/RemoveSession.aspx",
        contentType: "application/x-www-form-urlencoded",
        dataType: "text",
        success: function (msg) {
            if ($(location).attr('href').search(/espaceperso/i) != -1) {
                window.location.href = "/";
            }
            else {
                window.location.reload()
            }
            }
    });
    
}
function VerifLogin()
{
        var form = "<input type='text' size='16' id='login' class='form_loginbox' value='Identifiant' />&nbsp;";
        form += "<input type='text' size='16' id='motDePasse' class='form_loginbox' value='Mot de passe' />";
        form += "<input type='submit' class='bt_submit' value='' id='go'/>",
        $.ajax({
            timeout: 40000,
            type: "POST",
            url: "/Xaja/checklogin.aspx",
            contentType: "application/x-www-form-urlencoded",
            dataType: "text",
            data: 'login=' + $("#login").val() + '&pwd=' + $("#motDePasse-hidden").val(),
            beforeSend: function () { $("#LoginFormArea").html("<div align='center'><img src='/images/wait-login.gif'></div>"); },
            success: function (msg) {

                if (msg == '1' || msg == '2') {
                    var message = "";
                    switch (msg) {
                        case '1':
                            message = "Identifiant ou mot de passe invalide";
                            break;
                        case '2':
                            message = "Votre compte a été désactivé.";
                            break;
                    }
                    $("#LoginFormArea").html(form);

                    $("#motDePasse-clair").show();
                    $("#motDePasse-hidden").hide();

                    $("#login").focus(function () {
                        if ($(this).val() == "Identifiant") {
                            $(this).css("color", "#000").val("").select();
                        }
                    }).blur(function () {
                        if ($.trim($(this).val()) == "") {
                            $(this).val("Identifiant").css("color", "#8E8E8E");
                        }
                    });
                    $("#motDePasse-clair").focus(function () {
                        $("#motDePasse-clair").hide();
                        $("#motDePasse-hidden").show();
                        $("#motDePasse-hidden").focus();
                    });
                    $("#motDePasse-hidden").blur(function () {
                        if ($.trim($(this).val()) == "") {
                            $("#motDePasse-clair").show();
                            $("#motDePasse-hidden").hide();
                        }
                    });
                    alert(message);
                }
                else {

                    if ($(location).attr('href').search(/espaceperso/i) != -1) {
                        window.location.reload();
                    }
                    else {
                        LoadLogged();
                    }


                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (ajaxOptions == 'timeout') {
                    gWithReload = true;
                    alert("Désolé, une erreur technique s'est produite, merci de bien vouloir recommencer ultérieurement.");
                }
            }
        });
        return false;
    }
    jQuery.cookie = function (name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            var path = options.path ? '; path=' + (options.path) : '';
            var domain = options.domain ? '; domain=' + (options.domain) : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', escape(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = unescape(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    };

