if(typeof(js) == "undefined") {
	js = {};
}
toutou.require("toutou.html.dom", "toutou.js.ajax", "toutou.html.event", "toutou.event.manager");

js.commandes = function(tabProduit, fraisDePort, reduction) 
{
	if (reduction)	{ this.reduction = parseFloat(reduction); }
	this.produits = eval('(' + tabProduit + ')');
	this.fraisDePort = fraisDePort
	
//	var closure 	= toutou.js.closure.bind(this, this.valideSubmit);
//	toutou.html.event.addEventListener(tt$('form_adresse'), "submit", closure);
}

js.commandes.prototype.supprime = function(reference,indice)
{	
	delete this.produits[reference];
  	
 	var soustotal = 0;
  	for (refProd in this.produits)
  	{
  		soustotal = soustotal + this.produits[refProd]['prix']*this.produits[refProd]['quantite'];
  	}
  	if (soustotal == 0)
  	{
  		window.location.replace('/panier_vide.htm');
  	}
  	tt$('soustotal').innerHTML = soustotal + ' &euro;'
  	
  	if (soustotal < 50)
  	{
  		tt$('reduction').innerHTML = 0 + ' &euro';
  		$url = '/php/ajax/bonReduc.php?code=NO_REDUC';
		var value = toutou.js.ajax.request($url);
  		this.reduction = false;
  	}
  	var total = soustotal;
	if (this.reduction)		
  	{ 
  		if (this.reduction > total)	{ reduction = total; }
  		else					{ reduction = this.reduction;}
  		total-= reduction; 
  		tt$('reduction').innerHTML = reduction + ' &euro';
	}
  	total+= parseFloat(this.fraisDePort);
  	
  	tt$('total').innerHTML = total +  ' &euro';
  	
  	var tabProduit = tt$('liste_produit');
	var ligneProduit = tt$('ligne_produit_'+indice)
	tabProduit.removeChild(ligneProduit);
	$url = '/php/ajax/removeFromCart.php?reference=' + reference;
	toutou.js.ajax.request($url);	
}

js.commandes.prototype.modifieQuantite = function(reference,indice)
{
	var quantiteSelect = tt$('quantite_'+indice);
	var quantiteSelected = quantiteSelect.selectedIndex;
	var quantite = quantiteSelect.options[quantiteSelected].value;

	this.produits[reference]['quantite'] = quantite;
  	tt$('total_produit_'+indice).innerHTML = quantite * this.produits[reference]['prix'] + ' &euro;';
  	
 	var soustotal = 0;
  	for (refProd in this.produits)
  	{
  		soustotal = soustotal + this.produits[refProd]['prix']*this.produits[refProd]['quantite'];
  	}
  	tt$('soustotal').innerHTML = soustotal + ' &euro;'
  	
  	var total = soustotal;
  	if (soustotal < 50)
  	{
  		tt$('reduction').innerHTML = 0 + ' &euro';
  		$url = '/php/ajax/bonReduc.php?code=NO_REDUC';
		var value = toutou.js.ajax.request($url);
  		this.reduction = false;
  	}
	if (this.reduction)		
  	{ 
  		if (this.reduction > total)	{ reduction = total; }
  		else					{ reduction = this.reduction;}
  		total-= reduction; 
  		tt$('reduction').innerHTML = reduction + ' &euro';
	}
  	total+= parseFloat(this.fraisDePort);
  	$url = '/php/ajax/changeQuantity.php?reference=' + reference + '&quantite=' + quantite;
  	toutou.js.ajax.request($url);	
  	tt$('total').innerHTML = total +  ' &euro';
}

js.commandes.prototype.valideSubmit = function()
{
	var msg 	= '';
	var field 	= '';

	// cgv
	if (!tt$('cgv').checked)
	{
		msg = 'Veuillez accepter les conditions générales de vente\n' + msg;
		field = tt$('cgv');
	}
	
	// facturation
	if (tt$('telephone_FACTURATION').value == '')
	{
		msg = 'Indiquez votre numéro de téléphone (adresse de facturation)\n' + msg;
		field = tt$('telephone_FACTURATION');
	}
	
	if (tt$('ville_FACTURATION').value == '')
	{
		msg = 'Indiquez votre ville (adresse de facturation)\n' + msg;
		field = tt$('ville_FACTURATION');
	}
	
	if (tt$('code_postal_FACTURATION').value == '')
	{
		msg = 'Indiquez votre code postal (adresse de facturation)\n' + msg;
		field = tt$('code_postal_FACTURATION');
	}
	
	if (tt$('rue_FACTURATION').value == '')
	{
		msg = 'Indiquez votre rue (adresse de facturation)\n' + msg;
		field = tt$('rue_FACTURATION');
	}
	
	if (tt$('prenom_FACTURATION').value == '')
	{
		msg = 'Indiquez votre prénom (adresse de facturation)\n' + msg;
		field = tt$('prenom_FACTURATION');
	}
	
	if (tt$('nom_FACTURATION').value == '')
	{
		msg = 'Indiquez votre nom (adresse de facturation)\n' + msg;
		field = tt$('nom_FACTURATION');
	}
	
	var civiliteSelected = tt$('civilite_FACTURATION').selectedIndex;
	if (tt$('civilite_FACTURATION').options[civiliteSelected].value == '')
	{
		msg = 'Indiquez votre civilité (adresse de facturation)\n' + msg;
		field = tt$('civilite_FACTURATION');
	}
	
	// livraison
	if (tt$('telephone_LIVRAISON').value == '')
	{
		msg = 'Indiquez votre numéro de téléphone (adresse de livraison)\n' + msg;
		field = tt$('telephone_LIVRAISON');
	}
	
	if (tt$('ville_LIVRAISON').value == '')
	{
		msg = 'Indiquez votre ville (adresse de livraison)\n' + msg;
		field = tt$('ville_LIVRAISON');
	}
	
	if (tt$('code_postal_LIVRAISON').value == '')
	{
		msg = 'Indiquez votre code postal (adresse de livraison)\n' + msg;
		field = tt$('code_postal_LIVRAISON');
	}
	
	if (tt$('rue_LIVRAISON').value == '')
	{
		msg = 'Indiquez votre rue (adresse de livraison)\n' + msg;
		field = tt$('rue_LIVRAISON');
	}
	
	if (tt$('prenom_LIVRAISON').value == '')
	{
		msg = 'Indiquez votre prénom (adresse de livraison)\n' + msg;
		field = tt$('prenom_LIVRAISON');
	}
	
	if (tt$('nom_LIVRAISON').value == '')
	{
		msg = 'Indiquez votre nom (adresse de livraison)\n' + msg;
		field = tt$('nom_LIVRAISON');
	}
	
	var civiliteSelected = tt$('civilite_LIVRAISON').selectedIndex;
	if (tt$('civilite_LIVRAISON').options[civiliteSelected].value == '')
	{
		msg = 'Indiquez votre civilité (adresse de livraison)\n' + msg;
		field = tt$('civilite_LIVRAISON');
	}
	if (msg) 
	{ 
		alert(msg); 
		if (field) { field.focus(); }
		return false;
	}
	
	return true;
}

js.commandes.prototype.setCodePromo = function()
{
	var codePromo = tt$('code_promo').value;
	$url = '/php/ajax/bonReduc.php?code=' + codePromo;
	var value = toutou.js.ajax.request($url);
	
	if (value != 0)
	{
		var soustotal = 0;
	  	for (refProd in this.produits)
	  	{
	  		soustotal = soustotal + this.produits[refProd]['prix']*this.produits[refProd]['quantite'];
	  	}
	  	if (soustotal == 0)
	  	{
	  		window.location.replace('/panier_vide.htm');
	  	}
	  	tt$('soustotal').innerHTML = soustotal + ' &euro;'
	  	
	  	if (soustotal < 50)
	  	{
	  		alert('Pour utiliser un bon de réduction, le total de votre commande hors frais de port doit être d\'un minimum de 50€');	
	  	}
	  	else
	  	{
	  		this.reduction = parseFloat(value);
	  	}
	  	var total = soustotal;
	  	
	  	total+= parseFloat(this.fraisDePort);
	  	
	  	
	  	if (this.reduction)		
	  	{ 
	  		if (this.reduction > total)	{ reduction = total; }
	  		else					{ reduction = this.reduction;}
	  		
	  		total-= parseFloat(reduction); 
	  		tt$('reduction').innerHTML = reduction + ' &euro';
		}
	  	tt$('total').innerHTML = total.toFixed(2) +  ' &euro';
  	
	}
	else
	{
		alert('Votre code promo n\'est pas valable.');
	}
	return false;
		
}

