function menu_niv1(id){
	d = document.getElementById("menu_id_"+id).style.display;
	if (d!="block"){
		document.getElementById("menu_id_"+id).style.display="block";
		document.getElementById("menu_id_"+id).style.visibility="visible";
	}else{
		document.getElementById("menu_id_"+id).style.display="none";
		document.getElementById("menu_id_"+id).style.visibility="hidden";
	}
}

function menu_niv2(niv){
	self.location.href=currP+"?niv_id="+niv;
}

function addToCart(prod_id){
	var f= document.produit	;
	var refs="";
	try{
		for(i=0;i<f.length;i++){
			if(f[i].name.indexOf("reference")>=0){
				refs+= "&" + f[i].name + "=" + f[i].value;
			}
		}
	}catch(e){};
	if (!document.personnalisation){
		self.location.href=currP+"?addToCart=oui&prod_id="+prod_id+refs;
	}else{
		var persos="&pers=";
		if (test_perso()){
			var inputs = getElementsByClass("poidstotal");
			for(i=0;i<inputs.length;i++){
				if(parseInt(inputs[i].value)>0){
					id=inputs[i].id.replace("poidstotal_","");
					qteObj=elt("quantite_"+id);
					qte=qteObj.value;
					persos+=id+":"+qte+"|";
				}
			}
		}
		self.location.href=currP+"?addToCart=oui&prod_id="+prod_id+refs+persos;
	}
}

function validCaddie(){
	self.location.replace('index.php?valideCaddie=oui');
}


function popup(loc,larg,haut,scro){
	nouvelle_fenetre = window.open(loc,"","menubar=0,resizable=1,scrollbars="+scro+",width="+larg+",height="+haut+",top=100,left=100");
	if (parseInt(navigator.appVersion) > 2) { nouvelle_fenetre.focus();}	
}


/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* raccourcis */
function elt(id){
	return document.getElementById(id);
}
function inner(id){
	var tmp = elt(id);
	return tmp.innerHTML;
}


/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* rollover */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* test formulaire */
function isEmail(value){
  var re=/^[a-z0-9_-](\.?[a-z0-9_-])*[@]([a-z0-9_-]+\.)+([a-z]{2,3})$/i;
  return re.test(value)?true:false;
}

function only_space(chaine) {
  if (chaine=="") {return true;}
  if (chaine==" ") {return true;}
  for (var i=0 ; i<chaine.length ; i++) {
    if (chaine.charAt(i)!=" ") {
      return false;
    }
  }
  return true;
}


/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* personnalisation */
function quantiteplus(prod){
	poids=parseInt(inner("poids_"+prod));
	nom=inner("poids_"+prod);
	var qte = elt("quantite_"+prod);
	var tot = elt("poidstotal_"+prod);
	
	qte.value++;
	tot.value=(poids*qte.value);
	
	updatePoidsTotal();
}

function quantitemoins(prod){
	poids=parseInt(inner("poids_"+prod));
	nom=inner("poids_"+prod);
	var qte = elt("quantite_"+prod);
	var tot = elt("poidstotal_"+prod);
	if (qte.value>0){
		qte.value--;
	}
	tot.value=(poids*qte.value);
	
	updatePoidsTotal();
}

function remplir(prod){
	poids=parseInt(inner("poids_"+prod));
	nom=inner("poids_"+prod);
	var pmax = inner("poidsproduit");
	var tot = elt("poidstotal");
	var inputs = getElementsByClass("poidstotal");
	somme=0;
	for(i=0;i<inputs.length;i++){
		somme+=parseInt(inputs[i].value);
	}
	tot.value=somme;
	//totlbl.innerHTML=somme;
	
	while((parseInt(pmax)-somme)>5){
		somme=somme+poids;
		quantiteplus(prod);
	}
	//updatePoidsTotal();
}

	
function updatePoidsTotal(){
	var tot = elt("poidstotal");
	//var totlbl = elt("poidstotallbl");
	var msg = elt("messagePerso");
	var pmax = inner("poidsproduit");
	var inputs = getElementsByClass("poidstotal");
	somme=0;
	for(i=0;i<inputs.length;i++){
		somme+=parseInt(inputs[i].value);
	}
	tot.value=somme;
	//totlbl.innerHTML=somme;
	msg.innerHTML=somme;
	if (somme<parseInt(pmax)){
		if (parseInt(pmax)-somme<=5){
			_show("mea_53");
			msg.innerHTML = "Vous disposez encore de "+(parseInt(pmax)-somme)+" grammes.<br />Pour compléter votre ballotin, vous pouvez ajouter une citronette en <a href=\"javascript:quantiteplus('53');\">cliquant ici</a>.<br />";	
			_hide("boutonValider");
		}else{
			msg.innerHTML = "Vous disposez encore de "+(parseInt(pmax)-somme)+" grammes";	
			_hide("boutonValider");
		}
	}else{
		if (somme==parseInt(pmax)){
			msg.innerHTML = "Votre ballotin est plein !<br />Vous pouvez le valider en cliquant sur le bouton ci-dessous.";
			_show("boutonValider");
		}else{
			msg.innerHTML = "Attention : le poids limite pour ce ballotin est de "+pmax+" grammes.\r\nVotre sélection pèse actuellement "+somme+" grammes.";
			_hide("boutonValider");
		}
	}
	
	if (somme>parseInt(pmax)){
		alert("Attention : le poids limite pour ce ballotin est de "+pmax+" grammes.\r\nVotre sélection pèse actuellement "+somme+" grammes.");	
	}
}

function test_perso(){
	var tot = elt("poidstotal");
	//var totlbl = elt("poidstotallbl");
	var pmax = inner("poidsproduit");
	var inputs = getElementsByClass("poidstotal");
	somme=0;
	for(i=0;i<inputs.length;i++){
		somme+=parseInt(inputs[i].value);
	}
	tot.value=somme;
	//totlbl.innerHTML=somme;
	if (somme>parseInt(pmax)){
		alert("Attention : le poids limite pour ce ballotin est de "+pmax+" grammes.\r\nVotre sélection pèse actuellement "+somme+" grammes.");	
		return false;
	}
	if (somme==0){
		alert("Vous devez d'abord choisir la composition de votre ballotin.");
		return false;
	}
	if ((somme+5)<parseInt(pmax)){
		alert("Il manque encore "+(parseInt(pmax)-somme)+" grammes dans votre ballotin.");	
		return false;
	}
	return true;
	
}

