$(document).ready(function() {
	// Règle pour les regexp
	$.validator.addMethod("regex",
	        function(value, element, regexp) {
	            var check = false;
	            var re = new RegExp(regexp);
	            return this.optional(element) || re.test(value);
	        },
	        "Format invalide"
	);
	
	// Règle pour les groupes de champs
	/*$.validator.addMethod("required_group",
		function(val, el) {
			var $module = $(el).parent();
			return $module.find('.required_group:filled').length;
		}, "Remplissez au moins l'un des champs 2"
	);*/
	
	// Règle pour les champs date en 3 parties
	$.validator.addMethod("date_group",
		function(val, el) {
			var $module = $(el).parent();
			if ( $module.find('.date_group:filled').length == 3) return true;
			return false;
		}, "Saisissez une date de naissance complète"
	);

	// --- Ajout aux favoris
    // add a "rel" attrib if Opera 7+
    if(window.opera) {
        if ($("#bookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
            $("#bookmark").attr("rel","sidebar");
        }
    }
    $("#bookmark").click(function(event){
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = 'http://www.nocibe.fr/';
        var title = 'Nocibe';

        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if( window.external ) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
        }
    });
    // --- Fin ajout aux favoris
});

function addGlobalParams(url) {
	window.location = url+"&"+paramsGlobal; 
}

function addGlobalParamsToBegin(url) {
	window.location = url+"?"+paramsGlobal; 
}

function toUpperCase(element){
	document.getElementById(element).value = document.getElementById(element).value.toUpperCase();
	return true;
}

var timerInitial=750;
var timer=2000;
var myInterval;
var nb=0;
var nbMax=5;

function launchTimer(ajaxToExecute, closeIfError) {
	myInterval = setInterval("timerWS('"+ajaxToExecute+"', '"+ closeIfError +"')", timer);
    setTimeout("timerWS('"+ajaxToExecute+"', '"+ closeIfError +"')",timerInitial);
}

function timerWS(ajaxToExecute, closeIfError){
	checkResponse(ajaxToExecute, closeIfError);
	if (nb >= nbMax) {
		if(closeIfError == 'true') {
			Box._close();
		}
		clearInterval(myInterval);		
		nb = 0;
	} else {
		nb++;
	}

}

function checkResponse(ajaxToExecute, closeIfError) {
	$.ajax({
		type: "GET",
		data: paramsGlobal + "&nb=" + nb,
		url: ajaxToExecute,
		cache: false,
		success: function(data){
			if ($.trim(data) != null && $.trim(data) != "") {
				eval(data);
				clearInterval(myInterval);
				nb = 0;

				if(closeIfError == 'true') {
					Box._close();
				}
			}
		}
	});
}

// Controle lors de la saisie d'un texteara de sa longueur
// l'input type text doit avoir un id de type 'id'_i
// le p d'affichage du message du reste de caratère doit avoir un id de type 'id'_c
function controlTextAreaLength(id, maxLength) {
	var content = $('#'+id+'_i').val(); 

	if (content.length > maxLength) {
		$('#'+id+'_c').html('Vous avez atteind la limite de caractère pour votre message.');
		this.val(content.substring(0,maxLength));
	} else{
		$('#'+id+'_c').html('Plus que '+(maxLength - content.length) +' catactères pour votre message.');
	}
}

//Affichage ou disparition d'un element en fonction d'une checkbox
//la checbox doit avoir un id de type 'id'_c
//le bloc doit avoir un id de type 'id'_d
function showHideWithCheckbox(id) {
	if($('#'+id+'_c').attr('checked')) {
		$('#'+id+'_d').show();
	} else {
		$('#'+id+'_d').hide();
	}
}

/* Appel un contenu en ajax pour page marque*/
function getAjaxContent(idRub){
	if(idRub != null && idRub != ""){
		$.get("/nocibe/AjaxContentDisplay", paramsGlobal +"&pub=1&rub="+ idRub, function(data){
			$("#content_cms_marque").html(data);
			$("#contenu_droite .intro").remove();
			$("#contenu_droite .produit_d_dior").remove();
		});		
	}
}



function format(valeur,decimal) {
		var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
		var val=Math.floor(Math.abs(valeur));
		if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
		var val_format=val+"";		
		
		if (decimal>0) {
			var decim=""; 
			for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
			deci=decim+deci.toString();
			val_format=val_format+"."+deci;
		}
		if (parseFloat(valeur)<0) {val_format="-"+val_format;}
		return val_format;
	}

