var Tab_Prod     = ["Pain Biscornu: ", "Pain Gros-de-Vaud: ", "Pain aux Olives: ", "Pain Paysan: ",

					"Tresse a 1 Paton: ", "Tresse a 2 Patons: ", "St-Nicolas: ", "Baguette Parisette: ",

					"Pain de seigle: ", "Amandines aux raisins: ", "  ", "  ", "  ", "Pizza: ", "TOTAL en SFr:  "];

var Tab_Prix     = [4, 4, 4, 5, 

					3, 6, 3, 3,

					4, 6, 6, 6, 

					6, 10, 0];

var Tab_ProdAout = ["Brunch 15 ans et plus: ", "Brunch 7 a 14 ans: ", "Brunch 0 a 6 ans: ", "TOTAL en SFr: "];

var Tab_PrixAout = [25, 15, 0, 0];



// ************************************************************************************************************************

// Fonction permettant la redéfiniton de la mise en page lors du redimensionnement d'une fenêtre.

// ************************************************************************************************************************

function MM_reloadPage(init) {  //reloads the window if Nav4 resized

  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {

    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}

  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();

}



// ************************************************************************************************************************

// Fonction permettant l'affichage d'une fenêtre de type PopUp avec une panoplie de paramètres très utiles :)

// ************************************************************************************************************************

function OpenPop(theURL,winName,popWidth,popHeight,center,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v1.1

  var autoCloseTimeoutHandle, ontopIntervalHandle, w = 1024, h = 768;  

  var features = 'fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';

  

  // adds wanted width and height to popup features

  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;

  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;

  

  // if wanted, centers the popup

  if (center && center != "" && popWidth > 0 && popHeight > 0) {

    if (document.all || document.layers || document.getElementById) {

      w = screen.availWidth; h = screen.availHeight;}

    var leftPos = (w-popWidth)/2, topPos = (h-popHeight)/2;

    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}



  // if wanted, fullscreens the popup

  if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";



  // opens the popup

  popupWindow = window.open(theURL,winName,features);

  if (popupWindow.opener == null) popupWindow.opener = self;   

  

  // sets behaviour handler to popup if wanted (onTop, autoClose)

  if (document.all || document.layers || document.getElementById) {

    if (borderless && borderless != "") {popupWindow.resizeTo(popWidth,popHeight); popupWindow.moveTo(leftPos, topPos);}

    if (alwaysOnTop && alwaysOnTop != "") {

    	ontopIntervalHandle = popupWindow.setInterval("window.opener.popupWindow.focus();", 50);

    	popupWindow.document.body.onload = function() {popupWindow.setInterval("window.opener.popupWindow.focus();", 50);}; }

    if (autoCloseTime && autoCloseTime > 0) {

    	popupWindow.document.body.onbeforeunload = function() {

  			if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);

    		window.onbeforeunload = null;	}  

   		autoCloseTimeoutHandle = window.setTimeout("popupWindow.close()", autoCloseTime * 1000); }

  	window.onbeforeunload = function() {popupWindow.close();}; }   

  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;

}



// ************************************************************************************************************************

 // Vérife la manif choisie et affiche le formulaire en conséquence

// ************************************************************************************************************************					 

function CheckManif()

{

	Date_Today = new Date();

	Day    	   = Date_Today.getDate();

	Month      = Date_Today.getMonth() + 1;

	Str_Day    = Day < 10 ? "0" : "";

	Str_Month  = Month < 10 ? "0" : "";

	Str_Date   = Str_Month + Month + "/" + Str_Day + Day;				



	if (document.Form_Adresse.Manifestations.value < Str_Date)

	{

		window.alert("Veuillez svp choisir une manifestation qui aura lieu plus tard cette année !");

		return "NON";

	}

	else

	{

		var obj1, obj2;

		

		if (document.all)

		{

			obj1 = document.all.Produits;	

			obj2 = document.all.Aout;

		}

		

		if (document.getElementById)

		{

			obj1 = document.getElementById('Produits');	

			obj2 = document.getElementById('Aout');

		}

			

		if (document.Form_Adresse.Manifestations.value == "08/01")

		{

			obj1.style.visibility = "hidden";

			obj2.style.visibility = "visible";

		}

		else

		{

			obj1.style.visibility = "visible";

			obj2.style.visibility = "hidden";

		}

	}

	return "OUI";

}	



// ************************************************************************************************************************

// Chaque modif sur le formulaire est automatiquement reportée dans le calcul du total

// ************************************************************************************************************************

function CalcTotal(test)

{

	var Cout = 0;

	if (document.Form_Adresse.Manifestations.value == "08/01")

	{ 					

		for (var i = 0; i < document.Form_Aout.elements.length - 2; i++)

		{

			if (isNaN(document.Form_Aout.elements[i].value) || 

				document.Form_Aout.elements[i].value.indexOf("-") != -1 ||

				document.Form_Aout.elements[i].value.indexOf(".") != -1 ||

				document.Form_Aout.elements[i].value.indexOf(",") != -1)

			{

				window.alert("PROBLEME: Vérifiez svp les quantités que vous avez saisies !");

				document.Form_Aout.Total.value = 0;

				return "NON";			

			}

			else

				Cout += document.Form_Aout.elements[i].value * Tab_PrixAout[i]; 				

		}



		document.Form_Aout.Total.value = Cout; 

	}

	else

	{

		for (var i = 0; i < document.Form_Produits.elements.length - 2; i++)

		{

			if (isNaN(document.Form_Produits.elements[i].value) || 

				document.Form_Produits.elements[i].value.indexOf("-") != -1 ||

				document.Form_Produits.elements[i].value.indexOf(".") != -1 ||

				document.Form_Produits.elements[i].value.indexOf(",") != -1)

			{

				window.alert("PROBLEME: Vérifiez svp les quantités que vous avez saisies !");

				document.Form_Produits.Total.value = 0;

				return "NON";			

			}

			else

			{

								

				Cout += document.Form_Produits.elements[i].value * Tab_Prix[i]; 				

			}

		}



		document.Form_Produits.Total.value = Cout;

	}			

	if (test == "tester" && Cout == 0)

	{

		window.alert("PROBLEME: Vous n'avez pas selectionné de produit !");

		document.Form_Produits.Total.value = 0;

		return "NON";				

	}

	return "OUI";

}



// ************************************************************************************************************************

// Vérifie les saisies de l'utlisateur et gueule s'il le faut !

// ************************************************************************************************************************

function CheckSendData()

{		

	var IsOK = "OUI";

	

	// Teste si la manif choisie est ultérieure à la date actuelle

	IsOK = CheckManif();

	

	// Teste la présence des coordonnées

	if (IsOK == "OUI")

	{

		for (var i = 0; i < document.Form_Adresse.elements.length - 1; i++)

			if (document.Form_Adresse.elements[i].value.length < 1)

			{

				window.alert("PROBLEME: Vérifiez svp les coordonnées que vous avez saisies !");

				IsOK = "NON";

				break;

			}

	}



	// Teste l'adresse e-mail

	if (IsOK == "OUI")

	{

		var Mail = new String(document.Form_Adresse.Email.value);

		

		var PosAt = Mail.indexOf("@");

		if (PosAt < 1)

		{

			window.alert("PROBLEME: Vérifiez svp le E-mail que vous avez saisi !");

			IsOK = "NON";

		}

		else

		{

			var PosPt = Mail.indexOf(".", PosAt);

			if (PosPt == -1 || PosPt == PosAt + 1 || PosPt == Mail.length - 1)

			{

				window.alert("PROBLEME: Vérifiez svp le E-mail que vous avez saisi !");

				IsOK = "NON";

			}

		}

	}

	

	// Teste les quantités commandées

	if (IsOK == "OUI")

		IsOK = CalcTotal("tester");

	

	// La confirmatin de commande n'est affichée que si tout est OK !!!

	if (IsOK == "OUI")

		DisplayConf();

}



// ************************************************************************************************************************

// Informe et demande une confirmation à l'utlisateur 

// ************************************************************************************************************************

function DisplayConf()

{

	var total, i = 0;

	var features = 'fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no';

	features    += ',height=640,width=500,top=100,left=200';  

	

	var ConfWindow = window.open('','ConfirmationDeCommande', features);



	var message = "<html><head><title>\"Les Boulangers Sympa\" | Confirmation de commande</title>";

	message    += "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>";

	message    += "<link href='autres/BoulangersStyles.css' rel='stylesheet' type='text/css'></head>";

	message    += "<body><form name='form' method='post' action='autres/mail.php'>";

	message    += "Vos coordonnées:<p align='center'><textarea rows='3' cols='44' name='Coord' readonly>";

	message    += document.Form_Adresse.Prenom.value + " " + document.Form_Adresse.Nom.value + "\n";

	message    += document.Form_Adresse.Adresse.value + "\n" + document.Form_Adresse.NoTel.value;

	message    += "</textarea><br><textarea rows='1' cols='44' name='Mail' readonly>";

	message    += document.Form_Adresse.Email.value + "</textarea></p>";

	

	message	   += "Manifestation choisie:<p align='center'><textarea rows='1' cols='47' name='Manif' readonly>";

	i = document.Form_Adresse.Manifestations.selectedIndex;

	message    += document.Form_Adresse.Manifestations[i].text + "</textarea></p>";



	message	   += "Vos produits:<p align='center'><textarea rows='10' cols='50' name='Prod' readonly>";

	message    += "Description                     Nbre   Prix/piece\n";

	message    += "-------------------------------------------------\n\n";

	if (document.Form_Adresse.Manifestations.value == "08/01")

	{

		// Insère 1 à 1 les brunchs commandés

		for (i = 0; i < document.Form_Aout.elements.length - 2; i++)

			if (document.Form_Aout.elements[i].value > 0)

			{

				var space = FillSpace(Tab_ProdAout[i], document.Form_Aout.elements[i].value, 36);

				message += Tab_ProdAout[i] + space + document.Form_Aout.elements[i].value;

				space = FillSpace("_", Tab_PrixAout[i].toString(), 11);

				message += space + Tab_PrixAout[i] + ".--\n";

			}

		message += "\n========================\n";

		message += Tab_ProdAout[document.Form_Aout.elements.length - 2] + " ";

		message += document.Form_Aout.elements[document.Form_Aout.elements.length - 2].value;

		total = document.Form_Aout.elements[document.Form_Aout.elements.length - 2].value;

	}

	else

	{

		// Insère 1 à 1 les produits commandés

		for (i = 0; i < document.Form_Produits.elements.length - 2; i++)

			if (document.Form_Produits.elements[i].value > 0)

			{

				var space = FillSpace(Tab_Prod[i], document.Form_Produits.elements[i].value, 36);

				message += Tab_Prod[i] + space + document.Form_Produits.elements[i].value;				

				if (i == 10 || i == 11)

				{

					space = FillSpace("_", Tab_Prix[i].toString(), 11);

					message += space + Tab_Prix[i] + ".--\n";

				}

				else

				{

					space = FillSpace("_", Tab_Prix[i].toString(), 11);					

					message += space + Tab_Prix[i] + ".--\n";

				}

			}

		message += "\n========================\n";

		message += Tab_Prod[document.Form_Produits.elements.length - 2] + " ";

		message += document.Form_Produits.elements[document.Form_Produits.elements.length - 2].value;

		total = document.Form_Produits.elements[document.Form_Produits.elements.length - 2].value;

	}	



	if (total.indexOf(".") != -1)

		message += "0";

	else

		message += ".--";



	message += "\n========================</textarea></p>";

	message += "Si vous désirez laisser un petit message aux boulangers sympa :";

	message += "<p align='center'><textarea rows='6' cols='53' name='Communic'></textarea></p>";

	message += "<table align='center' width='350' border='0' cellspacing='1'><tr><td align='center'>";

	message += "<input type='button' name='Btn_KO' value='Annuler' onclick='javascript:window.self.close()'>";

	message += "</td><td><input type='submit' name='Btn_OK' value='OK'></td></tr></table>";

	message += "</form></body></html>";

	

	// Ercit le fichier

	ConfWindow.document.write(message);

	ConfWindow.focus();

}



// ************************************************************************************************************************

// Remplit d'espace 2 textes à séparer quand on connaît la taille pour les écrire les deux

// ************************************************************************************************************************

function FillSpace(txt1, txt2, size)

{

	var str = "";

	var size1 = 0, size2 = 0;

	if (txt1 != "")

	  size1 = txt1.length;

	if (txt2 != "")

	  size2 = txt2.length;



	for (var i = 0; i < (size - (size1 + size2)); i++)

	  str += " ";



	return str;

}



// ************************************************************************************************************************

// Affiche une boîte de dialogue indiquant qu'il n'y a pas de bon de commande au format papier.

// ************************************************************************************************************************

function PasDeCommande()

{

	window.alert("Il n'y a pas de bon de commande au format papier pour cette manifestation, merci.");

}







	
