function numberFormat(nombre, nbDecimales, sepDecimales, sepMilliers)
{
	if (nbDecimales == null || isNaN(nbDecimales) || nbDecimales < 0)
		nbDecimales = 0;
	if (sepDecimales == null)
		sepDecimales = ".";
	if (sepMilliers == null)
		sepMilliers = ",";
	
	var arrondi = 1;
	for (var i = 0; i < nbDecimales; ++i)
		arrondi *= 10;
	
	var entier = parseInt(nombre, 10);
	var decimal = nombre - entier;
	var decimal_result = Math.abs(parseInt(Math.round(decimal * arrondi), 10));
	
	var strEntier = entier + "";
	var nbChiffres = 0;
	for (var i = strEntier.length - 1; i >= 0; --i)
	{
		if ((strEntier.length - i) % 4 == 0)
			strEntier =
				strEntier.substr(0, i + 1) +
				sepMilliers +
				strEntier.substr(i + 1, strEntier.length - (i + 1));
	}
	if (nbDecimales == 0)
		return strEntier;
	
	var strDecimales = decimal_result + "";
	while(strDecimales.length < nbDecimales)
	{
		if (decimal_result < arrondi / 10)
			strDecimales = "0" + strDecimales;
		else
			strDecimales += "0";
	}
	
	return strEntier + sepDecimales + strDecimales;
}

function arrondi(valeur, nbDecimales)
{
	var facteur = Math.pow(10, parseInt(nbDecimales));
	return Math.round(parseFloat(valeur) * facteur) / facteur;
}

var arrayVins = new Array();
var arrayPrix = new Array();
var arrayFraisDePort = new Array();
var tauxRemise = 0.05;

var nbBouteilles = 0;
var sousTotal;
var remise;
var fraisPort;
var total;
function calculer(idcalcul)
{
	nbBouteilles = 0;
	sousTotal = 0;
	remise = 0;
	fraisPort = 0;
	total = 0;
	
	for (var i = 0; i < arrayVins.length; ++i)
	{
		var quantite = parseInt(document.forms["commander"].elements["quantite[" + arrayVins[i] + "]"].value);
		var prix = arrayPrix[arrayVins[i]];
		
		nbBouteilles += quantite;
		sousTotal += quantite * prix;
	}
	
	if (nbBouteilles == 0)
		fraisPort = 0;
	else
	{
		for (var i = 0; i < arrayFraisDePort.length; ++i)
		{
			if (nbBouteilles <= arrayFraisDePort[i].nbCartons * 6 || i == arrayFraisDePort.length - 1)
			{
				fraisPort = parseFloat(arrayFraisDePort[i].tarif);
				break;
			}
		}
	}
	
	//if (nbBouteilles > 60)
		//remise = sousTotal * 0.05;
	
	total = sousTotal - remise + fraisPort;
	
	document.getElementById("nb_bouteilles").innerHTML = nbBouteilles;
	document.getElementById("sous_total").innerHTML = numberFormat(sousTotal, 2, ",", " ");
	document.getElementById("remise").innerHTML = numberFormat(remise, 2, ",", " ");
	document.getElementById("ligne_remise").style.display = remise != 0 ? "block" : "none";
	document.getElementById("frais_de_port").innerHTML = numberFormat(fraisPort, 2, ",", " ");
	document.getElementById("total").innerHTML = numberFormat(total, 2, ",", " ");
	
	if(idcalcul)
	{
		var quantite = parseInt(document.forms["commander"].elements["quantite[" + idcalcul + "]"].value);
		ajax_commander('quantite[' + idcalcul + ']= ' + quantite );
	}
}

function validerEmail(email)
{
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(@\-)/; // not valid
	var reg2 = /^[a-zA-Z0-9\-\._!#\$%&\*\+=\^\{\}~]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	return (!reg1.test(email) && reg2.test(email));
}

var strErreur = "";
function erreur(nouvelleErreur)
{
	strErreur += (strErreur != "" ? "\n" : "") + "• " + nouvelleErreur;
}

function validerCommande()
{
	strErreur = "";
	
	if (nbBouteilles == 0)
		erreur("Vous n'avez sélectionné aucun produit");
	
	var emailLivraison = document.forms["commander"].elements["email_livraison"].value;
	if (document.forms["commander"].elements["nom_livraison"].value == "")
		erreur("Votre nom est obligatoire dans l'adresse de livraison");
	if (document.forms["commander"].elements["prenom_livraison"].value == "")
		erreur("Votre prénom est obligatoire dans l'adresse de livraison");
	if (document.forms["commander"].elements["adresse1_livraison"].value == "" && document.forms["commander"].elements["adresse2_livraison"].value == "")
		erreur("Votre adresse est obligatoire pour la livraison");
	if (document.forms["commander"].elements["cp_livraison"].value == "")
		erreur("Votre code postal est obligatoire dans l'adresse de livraison");
	if (document.forms["commander"].elements["ville_livraison"].value == "")
		erreur("Votre ville est obligatoire dans l'adresse de livraison");
	if (document.forms["commander"].elements["tel_livraison"].value == "")
		erreur("Votre numéro de téléphone est obligatoire dans l'adresse de livraison");
	if (emailLivraison == "")
		erreur("Votre adresse email est obligatoire dans l'adresse de livraison");
	else if (!validerEmail(emailLivraison))
		erreur("Votre adresse email pour la livraison n'est pas valide");
	
	var nomFacturation = document.forms["commander"].elements["nom_facturation"].value;
	var prenomFacturation = document.forms["commander"].elements["prenom_facturation"].value;
	var adresse1Facturation = document.forms["commander"].elements["adresse1_facturation"].value;
	var adresse2Facturation = document.forms["commander"].elements["adresse2_facturation"].value;
	var cpFacturation = document.forms["commander"].elements["cp_facturation"].value;
	var villeFacturation = document.forms["commander"].elements["ville_facturation"].value;
	var telFacturation = document.forms["commander"].elements["tel_facturation"].value;
	var paysFacturation = document.forms["commander"].elements["pays_facturation"].value;
	var emailFacturation = document.forms["commander"].elements["email_facturation"].value;
	if (nomFacturation != "" || prenomFacturation != "" || adresse1Facturation != "" || adresse2Facturation != "" || cpFacturation != "" || villeFacturation != "" || telFacturation != "" || paysFacturation != "" || emailFacturation != "")
	{
		if (nomFacturation == "")
			erreur("Votre nom est obligatoire dans l'adresse de facturation");
		if (prenomFacturation == "")
			erreur("Votre prénom est obligatoire dans l'adresse de facturation");
		if (adresse1Facturation == "" && adresse2Facturation == "")
			erreur("Votre adresse est obligatoire pour la facturation");
		if (cpFacturation == "")
			erreur("Votre code postal est obligatoire pour l'adresse de facturation");
		if (villeFacturation == "")
			erreur("Votre ville est obligatoire pour l'adresse de facturation");
		if (telFacturation == "")
			erreur("Votre numéro de téléphone est obligatoire pour l'adresse de facturation");
		if (paysFacturation == "")
			erreur("Votre pays est obligatoire pour l'adresse de facturation");
		if (emailFacturation == "")
			erreur("Votre adresse email est obligatoire pour l'adresse de facturation");
		else if (!validerEmail(emailFacturation))
			erreur("Votre adresse email pour la facturation n'est pas valide");
	}
	if (!document.forms["commander"].elements["age"].checked)
		erreur("Vous devez accepter les conditions générales de vente pour continuer");
	
	if (strErreur != "")
	{
		alert(strErreur);
		return false;
	}
	return true;
}

/*------------------------------------------- */
/*------------------------------------------- */
/*------------------------------------------- */
/*ouvrirPhototheque = function()
{
	if (document.getElementById('phototheque_iframe').src == "")
		document.getElementById('phototheque_iframe').src = "diaporama.php?statPage=" + statPage + "&langue=" + langue;
		
	Element.hide('phototheque_iframe');

	var arrayPageSize = getPageSize();
	Element.setWidth('phototheque_fond', arrayPageSize[0]);
	Element.setHeight('phototheque_fond', arrayPageSize[1]);
	new Effect.Appear('phototheque_fond', { duration: 0.2, from: 0.0, to: 0.75 });
	
	var arrayPageScroll = getPageScroll();
	Element.setTop('phototheque', arrayPageScroll[1] + (arrayPageSize[3] / 10));
	document.getElementById('phototheque').style.visibility = "visible";
	new Effect.Appear('phototheque_iframe', { duration: 0.6, queue: 'end', afterFinish: function() {
		new Effect.Parallel(
		[ new Effect.SlideDown( 'photothequeDataContainer', { sync: true, duration: 0.6, from: 0.0, to: 1.0 }), 
		  new Effect.Appear('photothequeDataContainer', { sync: true, duration: 0.6 }) ], { duration: 0.6 });
		}
	});
	
	return false;
}
fermerPhototheque = function()
{
	document.getElementById('phototheque').style.visibility = "hidden";
	Element.hide('photothequeDataContainer');
	new Effect.Fade('phototheque_fond', { duration: 0.2 });
	
	return false;
}*/
/*------------------------------------------- */
/*------------------------------------------- */
/*------------------------------------------- */

function show_vin(id)
{
	document.getElementById('phototheque').style.display = "none";
	document.getElementById('showvin_loading').style.display = "none";
	var arrayPageSize = getPageSize();
	Element.setWidth('showvin_fond', arrayPageSize[0]);
	Element.setHeight('showvin_fond', arrayPageSize[1]);
	new Effect.Appear('showvin_fond', { duration: 0.2, from: 0.0, to: 0.75 });
	
	var arrayPageScroll = getPageScroll();
	Element.setTop('showvin', arrayPageScroll[1] + (arrayPageSize[3] / 10));
	document.getElementById('showvin').style.visibility = "visible";
	new Effect.Appear('showvin_contenu', { duration: 0.6, queue: 'end', afterFinish: function() {
		new Effect.Parallel(
		[ new Effect.SlideDown( 'showvinDataContainer', { sync: true, duration: 0.6, from: 0.0, to: 1.0 }), 
		  new Effect.Appear('showvinDataContainer', { sync: true, duration: 0.6 }) ], { duration: 0.6 });
		}
	});
	
	var url = 'http://www.astros.fr/includes/lightbox_vins.php?id='+id;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			//alert(transport.responseText);
			document.getElementById("div_vins").innerHTML = '<img src="images/loading.gif" alt="" width="32" height="32" style="margin-top: 250px; margin-right: 140px; float: right;" />';
			document.getElementById("showvin_texte").innerHTML = transport.responseText;
			rechargerElement("div_vins", 'images_vins.php?id='+id);
		}
	});
}

function hide_vins()
{
	document.getElementById('showvin').style.visibility = "hidden";
	Element.hide('showvinDataContainer');
	new Effect.Fade('showvin_fond', { duration: 0.2 });
	document.getElementById("div_vins").innerHTML = '<img src="images/loading.gif" alt="" width="32" height="32" style="margin-top: 250px; margin-right: 140px; float: right;" />';
	document.getElementById("showvin_texte").innerHTML = '';
	
	return false;
}



