﻿/*****************************************************************
Gestionnaire de la popup de connexion et de création de compte
*****************************************************************/
var accountPopupManager;

$(function() {
    accountPopupManager = {
        originURL: null
        ,
        redirectURLOnError: null
        ,
        displayPopup: function(targetURL, redirectURLOnError) {

            accountPopupManager.originURL = targetURL;
            accountPopupManager.redirectURLOnError = redirectURLOnError;
            
            $('#div_popupErreur').hide();
            $('#div_popupInformation').hide();

            $.blockUI({
                message: $('#div_accountPopupForm'),
                css: {
                    top: ($(window).height() - 400) / 2 + 'px',
                    left: ($(window).width() - 600) / 2 + 'px',
                    width: '640px',
                    height: '450px',
                    backgroundColor: '#fff'
                }
            });
        },
        unblockPopup: function() {
            $.unblockUI();
        },
        createAccount: function(identifier, password, rememberMe, serviceURL) {

            $('#div_popupErreur').hide();
            $('#div_popupInformation').hide();

            $.ajax({
                type: "POST",
                url: serviceURL,
                data: "{'login' : '" + identifier + "'," +
   					       "'pwd' : '" + password + "'," +
   					       "'rememberMe' : '" + rememberMe + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function(xhr) {
                    xhr.setRequestHeader("Content-type",
   				                             "application/json; charset=utf-8");
                },
                success: function(msg) {
                    if (new Boolean(msg.d.Success) == true) {
                        $('#p_popupInformationMsg').text(msg.d.InformativeMessage);
                        $('#div_popupInformation').show();
                        setTimeout(function() { $.unblockUI(); window.location.href = accountPopupManager.originURL; }, 2000);
                    }
                    else {
                        $('#p_popupErreurMsg').text(msg.d.ErrorMessage);
                        $('#div_popupErreur').show();
                    }
                }
                    ,
                error: function(xhr, ajaxOptions, thrownError) {
                    $('#p_popupErreurMsg').text("An error occured during method call. Error :" + xhr.status);
                    $('#div_popupErreur').show();
                    setTimeout(function() { $.unblockUI(); window.location.href = accountPopupManager.redirectURLOnError; }, 2000); 
                }
            });
        },
        connect: function(identifier, password, updateCookie, serviceURL) {

            $('#div_popupErreur').hide();
            $('#div_popupInformation').hide();

            $.ajax({
                type: "POST",
                url: serviceURL,
                data: "{'login' : '" + identifier + "'," +
   					   "'pwd' : '" + password + "'," +
   					   "'updateCookie' : '" + updateCookie + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function(xhr) {
                    xhr.setRequestHeader("Content-type",
   				                         "application/json; charset=utf-8");
                },
                success: function(msg) {
                    if (new Boolean(msg.d.Success) == true) {
                        $('#p_popupInformationMsg').text(msg.d.InformativeMessage);
                        $('#div_popupInformation').show();
                        setTimeout(function() { $.unblockUI(); window.location.href = accountPopupManager.originURL; }, 2000);
                    }
                    else {
                        $('#p_popupErreurMsg').text(msg.d.ErrorMessage);
                        $('#div_popupErreur').show();
                    }
                }
                ,
                error: function(xhr, ajaxOptions, thrownError) {
                    $('#p_popupErreurMsg').text("An error occured during method call. Error :" + xhr.status);
                    $('#div_popupErreur').show();
                    setTimeout(function() { $.unblockUI(); window.location.href = accountPopupManager.redirectURLOnError; }, 2000); 
                }
            });
        }
    }
});  





