﻿$(document).ready(function () {

    /*jQuery(".msgError").hide();*/
    jQuery(".emailEmpty").hide();
    jQuery(".emailError").hide();
    jQuery("#mask").hide();
    jQuery("#box").hide();
    jQuery("#result").hide();
    jQuery("#wait").hide();
	jQuery("#wait").css('margin-top', -$('#wait').height() / 2);
	jQuery("#wait").css('margin-left', -$('#wait').width() / 2);
	
    var txtDefault = jQuery("input[id$='_View_txtEmailAdresse']").val();

    jQuery("input[id$='_View_txtEmailAdresse']").focus(function () {
        if (jQuery(this).val() == txtDefault) {
            jQuery(this).val("")
            jQuery(this).removeClass('italique');
        }
        jQuery(this).blur(function () {
            if (jQuery(this).val() == '') {
                jQuery(this).val(txtDefault);
                jQuery(this).addClass('italique');
            }
        });
    });
    /* CLIC sur le bouton SUBSCRIBE */
    jQuery("a[id$='_View_cmdSubmitNewsletter']").click(function () {
        jQuery(".emailEmpty").hide();
        jQuery(".emailError").hide();
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var emailVal = jQuery("input[id$='_View_txtEmailAdresse']").val();
        if (emailVal == txtDefault) {
            jQuery(".emailEmpty").show();
            hasError = true;
        } else if (!emailReg.test(emailVal)) {
            jQuery(".emailError").show();
            hasError = true;
        };

        if (hasError == false) {
			/*jQuery(".lgWait").hide();*/
            jQuery("#mask").fadeTo('slow', 0.8).fadeIn(1500);
            jQuery("#box").css('margin-top', -$('#box').height() / 2);
            jQuery("#box").css('margin-left', -$('#box').width() / 2);
            jQuery("#box").fadeIn(1500);
        };
        return false;
    });
    jQuery("a[id$='_View_lgOK']").click(function () {
        /*jQuery(this).hide();*/
		jQuery("#box").fadeOut(1500);
		jQuery("#wait").fadeIn(1500);
		
        var langue = jQuery("#box input:checked").attr("id");
        var langue = langue.substring(langue.length - 5);

        var emailVal = jQuery("input[id$='_View_txtEmailAdresse']").val();
        jQuery.ajax({
            type: "POST",
            url: window.location.href,
            data: "{ 'email':'" + emailVal + "', 'langue':'" + langue + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("DNN-Register-Service", "true");
                xhr.setRequestHeader("DNN-Register-Service-Method", "Register");
            },
            success: function (msg, textStatus, XMLHttpRequest) {
                if (msg.Exception == undefined) {
					jQuery("#result span").append(msg.result);
					jQuery("#result").css('margin-top', -$('#result').height() / 2);
					jQuery("#result").css('margin-left', -$('#result').width() / 2);
					jQuery("#wait").fadeOut(500, function(){
						jQuery("#result").fadeIn(1500, function () {
							jQuery("#result .lgClose").click(function () {
								jQuery("#result").hide();
								jQuery("#mask").hide();
							});
						});
					});
					
				} else {
                    jQuery("#result span").append(msg.result);
					jQuery("#result").css('margin-top', -$('#result').height() / 2);
					jQuery("#result").css('margin-left', -$('#result').width() / 2);
					jQuery("#wait").fadeOut(500, function(){
						jQuery("#result").fadeIn(1500, function () {
							jQuery("#result .lgClose").click(function () {
								jQuery("#result").hide();
								jQuery("#mask").hide();
							});
						});
					});
                };
            },
			error: function (XMLHttpRequest, textStatus, errorThrown) {
                    jQuery(".result").append("Error");
					jQuery("#result").css('margin-top', -$('#result').height() / 2);
					jQuery("#result").css('margin-left', -$('#result').width() / 2);
					jQuery("#wait").fadeOut(500, function(){
						jQuery("#result").fadeIn(1500, function () {
							jQuery("#result .lgClose").click(function () {
								jQuery("#result").hide();
								jQuery("#mask").hide();
							});
						});
					});
			}
		});
        return false;
	});
});

