// ***************** Les couleurs *****************
//     on = "#3F3FA3" 
//    off = '#F5A400' 
// select = '#EEEEEE' 


// ***************** Les fonctions DreamWeaver pour les roll over *****************
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];}
}

// ***************** Les fonctions pour les onglets *****************
function mouse_over(obj) { obj.backgroundColor='#EEEEEE'; }
function mouse_out(obj) { obj.backgroundColor='#F5A400'; }



// ***************** Fonction générant les onglets *****************
// nom est un tableau alternant entiers et châines de caractères
// lien est un tableau alternant entiers et liens
function onglet( nom , lien ) {
	for ( i = 0 ; i < nom.length ; i++ ) {
		//cellule de largeur 0
		if (lien[i] == 0) {
			window.document.writeln( "<td width=" + nom[i] + "></td>" );
		} else {
			//lien[i] est l'URL courante OU (lien est index.php ET URL courante pas en .php) ??????
			if (
				 window.document.URL.indexOf( lien[i] ) != -1 ||
 				//window.document.URL.indexOf( lien[i].substring(0, lien[i].indexOf(".php")-1) ) != -1 ||
				(window.document.URL.indexOf( ".php" ) == -1 && lien[i] == "index.php" )
			) 
			// onglet de couleur bleue SANS lien
			{
				window.document.writeln("<td width=80 background='image/onglet.gif' bgcolor='#071045'>");
				window.document.writeln("<div align='center'><font color='#F5A400'>" + nom[i] + "</font></div>");
				window.document.writeln("</td>");
			// onglet de couleur jaune AVEC liens
			} else {
//				window.document.writeln("<td width=80 class=onglet onMouseOver='mouse_over(this.style);' onMouseOut='mouse_out(this.style);' background='image/onglet.gif'>");
//				window.document.writeln("<div align='center'><a href='" + lien[i] + "' class=onglet>" + nom[i] + "</a></div>");
//				window.document.writeln("</td>");
				window.document.writeln("<a href='" + lien[i] + "' class=onglet><td width=80 bgcolor='#F5A400' class=onglet onMouseOver='mouse_over(this.style);' onMouseOut='mouse_out(this.style);' background='image/onglet.gif'>");
				window.document.writeln("<div align='center'><a href='" + lien[i] + "' class=onglet>" + nom[i] + "</a></div>");
				window.document.writeln("</td></a>");
			}
		} 
	}
}



// ***************** Fonction pour la carte du monde *****************
function big(source) {
	map = window.open("","map","HEIGHT=548,WIDTH=930");
	map.document.write("<html><head><title>LEONIDAS DANS LE MONDE</title></head>");
	map.document.write("<body marginwidth=0 leftmargin=0 marginheight=0 topmargin=0>");
	map.document.write("<a href='javascript:window.close()'>");
	map.document.write("<img src='image/map.jpeg' alt='Cliquez pour fermer la fenêtre' border=0></a>");
	map.document.write("</body></html>");
}



// ***************** Fonction pour message d'alerte (commande) *****************
function alert_commm() {
	alert("Vous ne pouvez modifier que les quantités");
}


// ***************** Fonction pour confirmer la suppression d'un assortiment *****************
function del_assort(page) {
	if ( confirm("Etes vous sur de vouloir supprimer cet assortiment ?") ) {
		window.location.href = page ;
	}
}



// ***************** Modifie la quantité d'un assortiment puis affiche (commande) *****************
function modif_quantite_comm( quantite , key , case_prix ) {
	if ( Math.floor(quantite.value) >= 0 ) {	
		var q_temp = quantite.value ;
	} else {
		quantite.value = 1 ;
		var q_temp = 1 ;
		alert( "La valeur que vous avez entrée est incorrecte" ) ;
	}
	assort_quantite[key] = Math.floor(q_temp) ;
	case_prix.value = ( assort_quantite[key]*assort_prix[key] ) ;
	print_total_comm() ;
}



// ***************** Fonction calculant le prix/poids/quantité + liv de la commande *****************
function print_total_comm() {
	total_poid = 0 ;
	total_unit = 0 ;
	total_prix = 0;
	
	for( var i = 0 ; i < assort_quantite.length ; i++ ) {
		if ( assort_quantite[i] && assort_quantite[i] != 0  ) {
			total_unit = Math.floor(total_unit) + assort_quantite[i] ; 
			total_poid = Math.floor(total_poid) + (assort_quantite[i]*assort_poids[i]) ;
			total_prix = Math.floor(total_prix) + (assort_quantite[i]*assort_prix[i]) ;
		}
	}
	total_prix = Math.floor(total_prix) + Math.floor(prix_liv[document.commande.N_Type_Liv.options[document.commande.N_Type_Liv.selectedIndex].value]) ;
	
	
	window.document.commande.total_poid_comm.value = total_poid ;
	window.document.commande.total_unit_comm.value = total_unit ;
	window.document.commande.total_prix_comm.value = total_prix ;
}

// ***************** Fonction ouvrant une fenêtre contenant une image *****************
//var fen_ouverte = false;
//var fen;
function pop_up(image,larg,haut) {


//	fen = window.open("" , "info" , "HEIGHT=264,WIDTH=450");
/*	if (!fen_ouverte)
	{
		fen = window.open("" , "info" , "HEIGHT="+haut+",WIDTH="+larg);
		fen_ouverte = true;
	}
*/
	fen = window.open("" , "info" , "HEIGHT="+haut+",WIDTH="+larg);
//	fen.document.clear();
	fen.document.write("<HTML><BODY OnClick='window.close();'><div align='center'><img src=\""+image+"\"></div>");
	fen.document.write("<H3 align='center'><FONT COLOR='#3F3FA3'>Clickez dans la fenêtre pour fermer</FONT></H3></BODY></HTML>");
}


// ***************** Fonction appelant le popup info.php (assortiment) *****************
function info(rand,quantite) {
	var url;
	url = "info.php?Num_assort=" + rand + "&quantite=" + quantite;
	win_info = window.open(url , "info" , "scrollbars,HEIGHT=264,WIDTH=450");
}



// ***************** Fonction appelant le popup info2.php (a la carte) *****************
function info2(rand) {
	var url = "info2.php?Num_produit=" + rand ;
	win_info2 = window.open(url,"info2","HEIGHT=160,WIDTH=450");
}



// ***************** Fonction pour ajouter un produit *****************
function add_pro2( Num_Produit , quantite , Libelle_Pro , Prix_Au_Kg , Poids_Pro ) {
	boite_quantite[Num_Produit] = quantite.value ;
	boite_nom[Num_Produit] = Libelle_Pro ;
	boite_prix[Num_Produit] = Prix_Au_Kg ;
	boite_poids[Num_Produit] = Poids_Pro ;
	
	window.document.alacarte.assortiment.value = "" ;
	total_quantite = 0 ;
	total_prix = 0 ;
	total_poids = 0 ;
	
	print_total();
}



// ***************** Fonction pour ajouter une boite *****************
function add_pro( Num_Produit , quantite , Libelle_Pro , Prix_Au_Kg , Poids_Pro ) {
	if ( Math.floor(quantite.value) >= 0 ) {	
		var q_temp = quantite.value ;
	} else {
		quantite.value = 0 ;
		var q_temp = 0 ;
		alert( "La valeur que vous avez entrée est incorrecte" ) ;
	}
	boite_quantite[Num_Produit] = q_temp ;
	boite_nom[Num_Produit] = Libelle_Pro ;
	boite_prix[Num_Produit] = Prix_Au_Kg ;
	boite_poids[Num_Produit] = Poids_Pro ;
	
	window.document.alacarte.assortiment.value = "" ;
	total_quantite = 0 ;
	total_prix = 0 ;
	total_poids = 0 ;

	print_total();
}



// ***************** Affichage & calcul du total + affichage liste produit*****************
function print_total() {
var txt1, txt2, txt3, txt4 ;
	for( var i = 0 ; i < boite_quantite.length ; i++ ) {
		if ( boite_quantite[i] && boite_quantite[i] != 0 )  {
			var temp2 = (boite_quantite[i]*boite_poids[i]) ;	
			var temp = ( ( (temp2) /1000) * boite_prix[i] ) ;
			

			txt1 = txt1_dec( boite_quantite[i] ) ;	// Quantité d'un produit
			txt2 = txt2_dec( temp2 ); 				// Poids total d'un produit
			//txt3 = txt3_dec( temp );				// Prix total d'un produit
			txt4 = txt4_dec( boite_nom[i] );		// Nom du produit complété à 25 caractères
			
			if ( i != 0 ) {
// suppression de l'affichage du prix par produit et reformatage
//				window.document.alacarte.assortiment.value += txt1 + boite_quantite[i]
//					+ txt2 + Math.floor(temp2) + txt3 + Math.floor(temp)
//					+ "CFP | " + boite_nom[i] + "\n" ;
				window.document.alacarte.assortiment.value += txt1 + txt4 + txt2  + " g" + "\n";//+ Math.floor(temp2) + " g \n" ;

					total_quantite = Math.floor(total_quantite) + Math.floor(boite_quantite[i]) ;
					total_poids = Math.floor(total_poids) + Math.floor(temp2) ;
			}

			total_prix = Math.floor(total_prix) + Math.floor(temp) ;
			
			
		}
	}	
	
	txt1 = txt1_dec( total_quantite ) ;
	txt2 = txt4_dec( "" ) ; 
	txt3 = txt2_dec( total_poids );
	txt4 = txt2_dec( total_prix );
	
	if ( boite_poids[0] && boite_poids[0] >= total_poids ) {
		window.document.alacarte.total_assort.value = txt1 + txt2 + txt3  + " g | " + txt4 + " CFP" ;
	} else {
		if ( total_poids != 0 && boite_poids[0] > 0 ) {
			window.document.alacarte.total_assort.value = "Depassement de poids : "
				+ total_poids + "g > " + boite_poids[0] + "g" ;
		}
	}
	if ( window.document.alacarte.boite.options[document.alacarte.boite.selectedIndex].value == 0 ) {
		window.document.alacarte.total_assort.value = "Vous n\'avez pas choisi de boîte" ;
	}
}



// ***************** Lien avec transmittion d'information (produit) *****************
function click_link( page , mode ) {
	if ( mode != 3 ) {
		var request = "" ;
		
		for( var i = 0 ; i < boite_quantite.length ; i++ ) {
			if ( i == 0 ) {
				request += "Poids_Pro[" + i + "]=" + boite_poids[i] + "&" ;
			} else {
				request += "Quantite_Compo[" + i + "]=" + boite_quantite[i] + "&" ;
				if ( mode == 2 ) { request += "Poids_Pro[" + i + "]=" + boite_poids[i] + "&" + "Libelle_Pro[" + i + "]=" + boite_nom[i] + "&"+ "Prix_Au_Kg[" + i + "]=" + boite_prix[i] + "&" ; }
			}
		}
		
		var libelle_descrition = "Libelle_Assort=" + window.document.alacarte.Libelle_Assort.value + "&Description_Assort=" + window.document.alacarte.Description_Assort.value ;
		var alert_txt = "Attention, vous n\'avez pas :\n\n";

		//On valide l'assortiment
		if ( mode == 1 && confirm("Valider votre assortiment ?") ) {
			if ( window.document.alacarte.boite.options[document.alacarte.boite.selectedIndex].value == 0 ) { alert_txt += "• choisi de boîte\n" ; }
			if ( total_poids != 0 && boite_poids[0] < total_poids ) { alert_txt += "• respecté le poids de la boîte\n"; }
			if ( total_quantite == 0 ) { alert_txt += "• choisi de produit\n"; }
			if ( window.document.alacarte.Libelle_Assort.value == "Mettre un nom" ) { alert_txt += "• changé le nom de l'assortiment\n"; }
			if ( window.document.alacarte.Description_Assort.value == "Mettre une description" ) { alert_txt += "• changé la description de l'assortiment"; }
			if ( alert_txt == "Attention, vous n\'avez pas :\n\n" ) { window.location.href = page + "?" + request + libelle_descrition ; } else { alert(alert_txt); }
		}
		if ( mode == 2 ) { window.location.href = page + "&" + request + libelle_descrition; }

	  // On est non connecté
	} if ( mode == 3 ) {
		window.location.href = page ;
	}
}



// ***************** Formattage du texte (colonne) *****************
function txt1_dec( x1 ) {
	var txt1 ="  ";
	if ( x1 > 9 ) { txt1 = " " ; }
	if ( x1 > 99 ) { txt1 = "" ; } 
	txt1 += x1;
	return txt1 + " | " ;
}
function txt2_dec( x2 ) {
//	var txt_2 = " produits | " ;
	var txt2;
	if (x2 >= 0) { txt2 = "   " ; }
	if ( x2 > 9 ) { txt2 = "  " ; }
	if ( x2 > 99 ) { txt2 = " " ; }
	if ( x2 > 999 ) { txt2 = ""; } 
	txt2 += x2;
	return txt2;
}
// obsolete
function txt3_dec( x3 ) {
	var txt_3 = "g | " ;
	if (x3 >= 0) { txt3 = txt_3 + "    " ; }
	if ( x3 > 9 ) { txt3 = txt_3 + "   " ; }
	if ( x3 > 99 ) { txt3 = txt_3 + "  " ; }
	if ( x3 > 999 ) { txt3 = txt_3 + " " ; }
	if ( x3 > 9999 ) { txt3 = txt_3 ; } 
	return txt3 ;
}
// complete a droite la chaine a 30 caracteres
function txt4_dec( chaine ) {
	var txt = chaine;
	for (var i = chaine.length; i <= 25 ; i++)
		txt += " ";
	return txt + " | ";
}

//*************************** Changement d'URL ***********************
	
function chargeUrl(Obj_select){
	index = Obj_select.selectedIndex;
	url   = Obj_select.options[index].value;
	location.href = url;
}
	