
function ajout (id, max, pt_livraison)
{
  var idname = 'LPAC_'+id;
	if (xGetElementById(idname))
	{
		// on force à être en entier... 
	        var val=parseInt( xGetElementById(idname).value );
		if (isNaN(val)) { val=0; }
		val++;
		if (val > max)
		{
			alert ('La commande maximale est de '+max+'.');
		        xGetElementById(idname).value = max;
			return false;
		}
	        xGetElementById(idname).value = val;

		
		return update_pac_temp_cart(xGetElementById('ID_form_400'), pt_livraison);
	}
}

function enlev (id, pt_livraison)
{
  var idname = 'LPAC_'+id;
	if (xGetElementById(idname))
	{
		// on force à être en entier... 
	        var val=parseInt( xGetElementById(idname).value );
		if (isNaN(val)) { val=0; }

		if (val == 0)
		{
			alert ('Déjà vide.');
		        xGetElementById(idname).value = 0;
			return false;
		}

	        xGetElementById(idname).value = val - 1;
		
		return update_pac_temp_cart(xGetElementById('ID_form_400'), pt_livraison);
	}
}

function modif (id, max, pt_livraison)
{
  var idname = 'LPAC_'+id;
	if (xGetElementById(idname))
	{
        	var val=parseInt( xGetElementById(idname).value );
		if (isNaN(val)) { val=0; }

		if ((val > max) || (val < 0))
		{
			alert ('La quantité doit être comprise entre 0 et '+max+'.');
		        val = 0;
		}
	       	xGetElementById(idname).value = val;

		return update_pac_temp_cart(xGetElementById('ID_form_400'), pt_livraison);
	}
}




function getQte (prefix)
{
  var q = 0;

  var el = xGetElementById (prefix);
  if (el)
  {
    q = parseInt( el.value );
  }

  return q;
}

function setQte (prefix, qty)
{
  var el = xGetElementById (prefix);
  if (el)
  {
    el.value = qty;
  }
}

function addQte (prefix, value)
{
  var qty = 1;
  var n = -1;
  var len = infoProduct.length;

  var i;
  for(i = 0; i < len; i++)
  {
    if (infoProduct[i][0] == prefix)
    {
      n = i;
      break;
    }
  }

  if (n >= 0)
  {
    qty = getQte (prefix);
    qty += value;
    var max = infoProduct[n][3];

    if (qty < 1)
    {
      //qty = 1; // On limite à 1 article minimum
      return deleteProduct (prefix);
    }
    else if (qty > max)
    {
      qty = max; // On limite à max
      alert ('La commande maximale est de '+max+'.');
    }
    setQte (prefix, qty);
    updateProduct (prefix, qty * infoProduct[n][1]);
    update_pac_big_cart (xGetElementById('FORM_ACTIVE_CART'));
    updateTotal ();
  }
  else
  {
    alert('oups...');
  }
}

function updateQte (prefix)
{
   // http://www.w3schools.com/jsref/jsref_events.asp  
   addQte (prefix, 0);
}

function updateProduct (prefix, val)
{
  var div_price = xGetElementById ('PRIX_' + prefix);
  if (div_price)
  {
    div_price.innerHTML = getEuroFormat (val);
  }
}



// same as php

// {{{ number_format
function number_format( number, decimals, dec_point, thousands_sep ) {
    // Format a number with grouped thousands
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
    // +       version: 803.2015
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var i, j, kw, kd, km;

    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ".";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ",";
    }

    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }

    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


    return km + kw + kd;
}// }}}



function getEuroFormat (val)
{
  var decimals = 0;
  if (val.toString().indexOf('.') != -1)
  {
    decimals = 2;
  }
  return number_format (val, decimals, ',', '') + ' &euro;';
}

function deleteProduct (prefix)
{
  var forceUpdate = false;

  // Faire une demande de confirmation ?
  if (confirm('Confirmez-vous la suppression de cet article de votre panier à la carte ?'))
  {
    // Update couleur...  
    var len=infoProduct.length;
    var i=0;
    var j=0;

    for(i=0;i<len;i++)
    {
      if (infoProduct[i][0]==prefix)
      {
        infoProduct[i][2]=false;
      	setQte(prefix,0);
      	xGetElementById( 'TR_' + infoProduct[i][0] ).style.display='none';
      	forceUpdate = true;
      }
      if (infoProduct[i][2])
      {
        if (j%2 == 1)
	{
          xGetElementById( 'TR_' + infoProduct[i][0] ).className='pair';
	}
        else
	{
      	  xGetElementById( 'TR_' + infoProduct[i][0] ).className='impair';
	}
      	j++;
      }
    }
    if (forceUpdate)
    {
      update_pac_big_cart (xGetElementById('FORM_ACTIVE_CART'));
      updateTotal ();
    }
  }
}

function updateTotal()
{
  var total=0;
  var t;
  var qty=1;
  var len=infoProduct.length;
  
  for(i=0;i<len;i++)
  {
     if (infoProduct[i][2])
	 {
       qty=getQte( infoProduct[i][0] );
	   
	   if (qty==0)
	   {
	     // Erreur... La ligne n'aurait pas du apparaitre (reload d'une page...)
		 // On force à 1 la quantité...
		 qty=1; // ou bien infoProduct[i][3] en supposant que [3] serve à stocker la quantité initiale...
		 setQte( infoProduct[i][0], qty);
	   }
	   
       t=infoProduct[i][1] * qty;
	   updateProduct( infoProduct[i][0], t );
	   total+=t;
	 }
  }

  total += frais_livraison_PAC;
   
  var div_total = xGetElementById( 'TOTAL' );
  if (div_total) 
  {
    div_total.innerHTML = getEuroFormat(total); 
  }
  
 
}
