﻿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 eCrmSendPassword(destination, textbox) {
    var email = $("#" + textbox).val();
    location.href = destination + "?email=" + email;
}

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();
    });

    function HandleAuthentication(e) {

    }
    // Set up for displaying modal dialogs                 
    $('a[class=eCrmRequiresAuth]').click(
        function (e) {
            // Prevent the default link behavior of navigation so we can use the link to show the Window                     
            e.preventDefault();
            // Determine which href was clicked if it was in fact an href (though this demo as only one.)                     
            var dest = $(this).attr('href');

            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 = "GOTO";
                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 (ecc != "") {
                    document.location.href = "?_ecc=" + ecc + "&_eca=" + 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(); }
});


