// JavaScript Document


function initDefil(nbProd)
{
	var page = 1;
	var nbPages = Math.ceil(nbProd/3);
	$("#down").click(function ()
		{
			if( page < nbPages )
			{
				$("#defil").animate({top: "-=92px"}, 300);
				page++;
				return false;
			}
		});
	
	$("#up").click(function ()
		{
			if( page > 1 )
			{
				$("#defil").animate({top: "+=92px"}, 300);
				page--;
				return false;
			}
		});
}		
function addPanier(a_prod_option, a_form)
{
	document.forms[a_form][a_prod_option].value++;
	document.forms[a_form].submit();
}

function format_entier(entier)
{
    var i;
    var has_virgule = 0;
    var res = '';
    entier = entier + '';
    for(i=0;i<entier.length;i++)
    {
       var c = entier.charAt(i);
       if ((c == '.') || (c == ','))
       {
          has_virgule = 1;
	  if (c == ',')
	  {
	     c = '.';	  
	  }
       }
       if (has_virgule >= 1)
       {
	  if (has_virgule < 4)
	  {
             res = res + c;
          }
          has_virgule++;
       }
       else
       {
           res = res + c;
       }
    }
    if (has_virgule == 0)
    {
        res = res + ".00";
    }
    if (has_virgule == 2)
    {
        res = res + "00";
    } 
    if (has_virgule == 3)
    {
        res = res + "0";
    } 
    return res;
}

function effaceContenu(contenu, form)
{
	if (form.value == contenu)
	{
		form.value = '';
	}
}

function remetContenu(contenu, form)
{
	if (form.value == '')
	{
		form.value = contenu;
	} 	
}
function changeQT(id_str) 
{
	var idValue = document.formProd['champ'+id_str].value;
	if(isNaN(parseInt(idValue))) idValue = 0;
	document.formProd['champ'+id_str].value = idValue;
	document.formProd['q'+id_str].value = idValue;
	//testStock(document.formProd['champ'+id_str], stock_nb);
}
function ajouteItem(id_str,stock_nb, minCommande, priv) 
{
	if(priv == 3 && minCommande > 0)
	{
		var i = minCommande;
	}
	else
	{
		var i = 1;
	}
	var idValue = document.formProd['champ'+id_str].value;
	if(isNaN(parseInt(idValue))) idValue = 0;
	idValue = parseInt(idValue, 10) + i;
	document.formProd['champ'+id_str].value = idValue;
	document.formProd['q'+id_str].value = idValue;
	//testStock(document.formProd['champ'+id_str], stock_nb);
}

function retireItem(id_str,stock_nb, minCommande, priv) 
{
	if(priv == 3 && minCommande > 0)
	{
		var i = minCommande;
	}
	else
	{
		var i = 1;
	}
	
	var idValue = document.formProd['champ'+id_str].value;
	if(isNaN(idValue)) idValue = 0;
	if(idValue > 0) 
	{
		idValue = parseInt(idValue, 10) - i;
		if(idValue < 0)
		{
			idValue = 0 
		}
		document.formProd['champ'+id_str].value = idValue;
		document.formProd['q'+id_str].value = idValue;
	}
}

var optionAttente = '';
function ajouteOption(nb,id_obj, id)
{	
	optionAttente = id;
	var tableau = document.getElementById('options');
	var cheveux_array = tableau.getElementsByTagName('a');
	var len = cheveux_array.length;
	for(var i=0; i<len; i++)
	{
		if(cheveux_array[i].firstChild.className == 'option')
		cheveux_array[i].firstChild.style.border = '1px #FFFFFF solid';
	}
	id_obj.firstChild.style.border = '1px #CC0000 solid';
	//alert(id);
}
function validOption(nb,id_obj, id)
{	
	var nb = parseInt(optionAttente.value,10);
	nb++;
	optionAttente.value = nb.toString();
}

function setValue(arg, ifMod) {
	globalValue = arg;
	globalMod = ifMod;
}

function supprProdSel(prod){
	if (confirm('Êtes-vous sûr de vouloir retirer ce produit de votre panier ?')){
		document.formdelpanier[prod].value="0";
		document.formdelpanier.submit();
	}
	else {	
		return false;		
	}
}
