/*
 * Liste les propriétés d'un objet
 */
function getProperties(obj){
    var msg = "";
    for (prop in obj) {
        msg += "property : " + prop + "\t\t value : " + obj[prop] + "\n";
    }
    alert(msg);
}

Event.observe(window, 'load', function() {
	//on init les inputs quantite si le tableau tabQuantite est créé (info du tableau passé en get)
	if (tabQuantite!=null) {
		for(n in tabQuantite) {
			if (tabQuantite[n].valeur!=null)
				$(tabQuantite[n].quantite).value=tabQuantite[n].valeur;
		}
		recalcule(); 
	}
	
	
	/*inputs = Form.getInputs('form1');
	for(i=0;i<inputs.length;i++)
		Event.observe(inputs[i], 'keypress', function(event)
		{
			if((event.which < 48 || event.which > 57)  &&  event.which != 32  &&  event.which != 8  &&  event.which != 0) Event.stop(event);
			if((event.keyCode < 48 || event.keyCode > 57) &&  event.keyCode != 32 && event.keyCode != 37  && event.keyCode != 39 && event.keyCode != 8 &&  event.keyCode != 0) Event.stop(event);
		});*/
});


/**
 * Assign un edit caché pour garder les infos quantités
 */
function assignBackInfos()
{
	inputs = Form.getInputs('form1');
	res = '';
	for(i=0;i<inputs.length;i++)
	  if($(inputs[i]).name=='quantite' && $F(inputs[i])!='0')
		if (res=='')
			res = $(inputs[i]).id+'='+$F(inputs[i]);
		else
			res = res+'&'+$(inputs[i]).id+'='+$F(inputs[i]);
    $('back_infos').value=res;
}

/**
 * Recalcule le soustotal d'un article.
 */
function recalcule()
{
	index=1;
	arrayArticles = new Array();
	arrayArticle = new Array();
	
	while (document.getElementById('td'+index)!=null)
	{
		var p = document.getElementById('td'+index);
		var total = 0;			    			
		for(var i=0; i<p.childNodes.length; i++) {
			  q = p.childNodes[i];
		    	for(var j=0; j<q.childNodes.length; j++){
		    		o= q.childNodes[j];
		    		
		    		for(var k=0; k<o.childNodes.length; k++){
    	    			if (o.childNodes[k].name=='idarticle') arrayArticle.push(o.childNodes[k].value);
    	    			if (o.childNodes[k].name=='ref') arrayArticle.push(o.childNodes[k].value);
		    			if (o.childNodes[k].name=='taille') arrayArticle.push(o.childNodes[k].value);
		    			if (o.childNodes[k].name=='prix') arrayArticle.push(o.childNodes[k].value);
		    			if (o.childNodes[k].name=='quantite') { 
		    				arrayArticle.push(o.childNodes[k].value);
		    				if (o.childNodes[k].value!=0) {
		    					total = total+(arrayArticle[3]*arrayArticle[4]);
		    					arrayArticles.push(arrayArticle);
		    					$('soustotal'+index).value=total;
		    				}
		    				arrayArticle = new Array();
		    			}
		    		}
			}
		}
	index = index+1;
	}
	recalculeTotal();	
	$('myquery').value = $H(arrayArticles).toQueryString();		
	assignBackInfos();
}

/**
 * Recalcule le total.
 */
function recalculeTotal()
{
  index=1;	
  var total = 0;
  while (document.getElementById('soustotal'+index)!=null)
  {
  	total = total + parseFloat($('soustotal'+index).value);
  	index = index + 1;
  }
  $('total').value = total;
}
