﻿function eCrmIsValidEmailAddress(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);
}

function eCrmValidateEmail() {
    var email = $("#eCrmLoginEmail").val();

    $("#eCrmValidEmail").css({
        "background-image": "url('/eCrm/validNo.png')"
    });
    //$("#lbCheckEmail").attr('disabled', 'disabled');
    if (eCrmIsValidEmailAddress(email)) {
        $("#eCrmValidEmail").css({
            "background-image": "url('/eCrm/validYes.png')"
        });
        //$("#lbCheckEmail").removeAttr('disabled');
        // Exécute PostBack de vérification en base
        //eval($("#eCrmCheckEmailReference").val());
    }
}

$(document).ready(function () {

    // Email well formating check
    $("#eCrmLoginEmail").live("keyup", function () {
        eCrmValidateEmail();
    });
    $("#eCrmLoginEmail").live("change", function () {
        eCrmValidateEmail();
    });
    $("#eCrmLoginEmail").live("autocomplete", function () {
        eCrmValidateEmail();
    });


    $('a[class*=eCrmRequiresAuth]').click(function (e) { eCrmHandleClick(this, e); });

    $('area[class*=eCrmRequiresAuth]').click(function (e) { eCrmHandleClick(this, e); });


    // Autoexec Link
    var eCrmAutoExec = $("#eCrmAutoexec");
    if (eCrmAutoExec != 0) {
        eCrmAutoExec.click();
    }

});

function eCrmHandleClick(source, e) {
    e.preventDefault();
    // Prevent the default link behavior of navigation so we can use the link to show the Window                     
    var dest = $(source).attr('href');
    var title = $(source).attr('eCrmTitle');
    var target = $(source).attr('target');

    if (title != undefined) {
        $("#LabelTitre").html(title);
    }
    else {
        if (dest.toLowerCase().startsWith('/espaceperso/catalogue.aspx')) {
            $("#LabelTitre").html('Votre demande de catalogue gratuit - Etape 1/2');
        }
        else {
            $("#LabelTitre").html('Inscription/Connexion à votre espace personnel');
        }
    }
    eCrmExecute(dest, target);

    // Set eventual focus
    var focusname = $(source).attr('eCrmFocusInput');
    if (focusname != undefined) {
        $("#" + focusname).focus();
    }

}

// Set up for displaying modal dialogs

function eCrmSendPassword(destination, textbox) {
    if (textbox) {
        var email = $("#" + textbox).val();
        location.href = destination + "?email=" + email;
    }
    else
        location.href = destination;
}

function eCrmExecute(dest, target) {
    // Determine which href was clicked if it was in fact an href (though this demo as only one.)
    var ecc = "";
    var eca = "";

    iStartCommand = dest.indexOf("_ecc=", 0);
    iEndCommand = dest.indexOf("&", iStartCommand + 5);
    if (iStartCommand > -1) {
        if (iEndCommand > iStartCommand)
            ecc = dest.substring(iStartCommand + 5, iEndCommand)
        else
            ecc = dest.substring(iStartCommand + 5);
    }

    iStartArgument = dest.indexOf("_eca=", 0);
    iEndArgument = dest.indexOf("&", iStartArgument + 5);
    if (iStartArgument > -1) {
        if (iEndArgument > iStartArgument)
            eca = dest.substring(iStartArgument + 5, iEndArgument)
        else
            eca = dest.substring(iStartArgument + 5);
    }

    if ((ecc == "") && (dest != "#")) {
        ecc = "";
        eca = dest;
    }
    $("#eCrmCommand").val(ecc);
    $("#eCrmArgument").val(eca);

    if ($("#eCrmAuthentified").val() == "False") {
        var id = "#eCrmModalWindow";
        // Determine browser windows dimensions.                     
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        // Set dimensions for the mask to opaque the screen when the modal window is displayed.  
        $('#eCrmMask').css({ 'width': maskWidth, 'height': maskHeight });
        // Make the Window Opaque          
        $('#eCrmMask').fadeIn("fast");
        $('#eCrmMask').fadeTo("slow", 0.8);

        //Get the window height and width  
        var winH = $(window).height();
        var winW = $(window).width();

        // Set the Modal Window's dimensions to center in the browser window.  
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        // Get default Email
        var email = $("#eCrmLoginDefaultEmail").val();
        if (email != 0) {
            $("#eCrmLoginEmail").val(email);
        }

        // Show the Modal Window  
        $(id).fadeIn("fast");

        eCrmValidateEmail();
        $("#eCrmLoginEmail").focus();
    }
    else {
        if (target == "") {
            if (ecc != "") {
                document.location.href = "?_ecc=" + ecc + "&_eca=" + eca;
            }
            else {
                document.location.href = eca;
            }
        }
        else {
            if (ecc != "")
                window.open("?_ecc=" + ecc + "&_eca=" + eca);
            else
                window.open(eca);
        }
    }
}


function eCrmCloseWindow() {
    $('#eCrmMask').hide();
    $('.eCrmWindow').hide();
}

// Handle Close Button Click Event  
$('.eCrmWindow .close').click(function (e) {
    // Cancel the link behavior  
    e.preventDefault();
    eCrmCloseWindow();
});

$("#eCrmCloseButton").live("click", function (e) {
    // Cancel the link behavior  
    e.preventDefault();
    eCrmCloseWindow();
});

// The user clicks OUTSIDE the Modal Window and the window will be closed without save.  
$('#eCrmMask').click(function () {
    eCrmCloseWindow();
});

$(document).keydown(function (e) {
    if (e.which == 27) { eCrmCloseWindow(); }
});



