var GLOB = {
    set: function (namespace, keys) {
        if (!GLOB[namespace]) {
            GLOB[namespace] = keys;
        } else {
            for (key in keys) {
                GLOB[namespace][key] = keys[key];
            }
        }
    },
    get: function (namespace, key) {
        if (!GLOB[namespace]) {
            return;
        }
        if (!key) {
            return GLOB[namespace];
        }
        return GLOB[namespace][key];
    }
};

$(document).ready(function(){
	$('#hp_newsletter').click(
		function () {
			$('#hp_email').slideToggle("slow");
			return false;
		}
	);
	
	$('#close_hp_email').click(
		function () {
			$('#hp_email').slideUp("slow");
			return false;
		}
	);
	
	$(":input[@type!='submit']").focus(function ()	{
		$('#hp_email p.msg').hide();
		$('#hp_email').css({ height:"40px", top:"-40px" });
	});
	
	$(".jsautoempty")
	   .blur(function(event){
			if(!$(this).attr('value'))
				{$(this).css('color','#666'); $(this).attr('value',$(this).attr('alt')); }
		})
	   .focus(function(event){
			if($(this).attr('value')===$(this).attr('alt'))
				{ $(this).attr('value',''); $(this).css('color',''); }
		})
		.each(function(){
			if(!$(this).attr('value') || $(this).attr('value')===$(this).attr('alt') )
				{$(this).css('color','#666'); $(this).attr('value',$(this).attr('alt'));  }
		})
	;
	
	 /*	contrôle global	*/	   
	$("#newsletter_form").submit(function(){	
		var formulaireOK = true;  
		var regmail =	/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;	/*expr regul pour l'email */ 			 
		if(!$('#mail').val().match(regmail)) {
			$('#hp_email').css({ height:"60px", top:"-60px" });
			$('#hp_email p.msg').text('adresse e-mail invalide');
			$('#hp_email p.msg').show();
			return false;
		}else{
			$.ajax({
				type: 'POST',
				url: $('#newsletter_form').attr('action'),
				data: 'mail=' + $('#mail').val(),
				success:function(data){
		   			$('#hp_email').css({ height:"60px", top:"-60px" });
					$('#hp_email p.msg').text('adresse e-mail sauvegardée');
					$('#hp_email p.msg').show();
				}
			});
			return false;
		}
	});
	
	function autocheckform()
	{
		$(this).find('.witherror').removeClass('witherror');
		$(this).find('.autocheck_error').remove();
		var nb_errors = 0;
		$(this).find('.jsnotempty').each(function(){
			if( $(this).val()==''  )
			{
				if($(this).attr('name') == 'commande'){
					$(this).after(' <span class="autocheck_error"><span>Indiquez votre numéro de commande.</span></span>');
				}
				else if($(this).attr('name') == 'sujet'){
					$(this).after(' <span class="autocheck_error"><span>Saisie incorrecte.</span></span>');
				}
				else{
					$(this).after(' <span class="autocheck_error"><span>Incorrect !</span></span>');
				}
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
		})
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		$(this).find('.jscheckmail').each(function(){
			if( !filter.test( $(this).val())  )
			{
				$(this).after(' <span class="autocheck_error"><span>Incorrect !</span></span>');
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
		})
		
		filter = /^[0-9]{2}[/\-]?[0-9]{2}[/\-]?(([0-9]{2})|([0-9]{4}))$/;
		$(this).find('.jsdate').each(function(){
			if( $(this).val() && !filter.test( $(this).val())  )
			{
				$(this).after(' <span class="autocheck_error"><span>Incorrect !</span></span>');
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
			else{
				if ($(this).val()){
					var res = $(this).val().replace(/[\-\/]/g,"");
					var j = res.substr(0,2);
					var m = res.substr(2,2);
					
					if (res.length == 6)
						var a = "20"+res.substr(4,2);
					else
						var a = res.substr(4,4);
					var checkdate = new Date(parseInt(a,10),(parseInt(m,10)-1),parseInt(j,10));
					if ((a != checkdate.getFullYear()) || (m != (checkdate.getMonth()+1)) || (j != checkdate.getDate())){
						$(this).after(' <span class="autocheck_error"><span>Incorrect !</span></span>');
						$(this).parents('dd').addClass('witherror');
						nb_errors++;
					}
					else if ($(this).hasClass("jsdate18")){
						aint = parseInt(a,10);
						aint = aint+18;
						checkdate.setFullYear(aint);
						var today = new Date();
						today.setHours(0,0,0);
						if (checkdate >= today){
							$(this).after(' <span class="autocheck_error"><span>Vous devez être majeur</span></span>');
							$(this).parents('dd').addClass('witherror');
							nb_errors++;
						}
					}
				}
			}
		});
		
		var filter = /^[0-9]*$/;
		$(this).find('.jschecktel').each(function(){
			if( !filter.test( $(this).val())  )
			{
				alert('Votre numéro de téléphone ne doit contenir que des chiffres (pas de point ni d\'espace)')
				$(this).after(' <span class="autocheck_error"><span>Incorrect !</span></span>');
				$(this).parents('dd').addClass('witherror');
				nb_errors++;
			}
		})
		
		if(nb_errors)
		{
			return false;
		}
	}
	
	$('.jsnotempty, .jscheckmail').each(function(){
		$(this).siblings('label').append(' <span class="mandatory" title="' + GLOB.get("field_mandatory") + '"><span>oblig.</span></span>');
		$(this).parents('form').submit(autocheckform);
		
	});
	
	$('.cgvlnk').click(function(){
		$('#forground').show();
		$('#cgv').show();
		/*return false;*/
	});
	
	$('#forground').click(function(){
		$('#cgv:visible').hide();
		$('#adresse_livraison_martial:visible').hide();
		$('#adresse_livraison:visible').hide();
		$('#choix_boutique:visible').hide();
		$('#popin_modif_mdp:visible').hide();
		$('#aide_code_boutique:visible').hide();
		$('#forground:visible').hide();
	});
	
});