
//Rechercher un objet 
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function calcul(ids_vins){
	
	var i,total=0,qte=0,remise=0,vin_remise=100;
	for (i=0; i < ids_vins.length; i++){
		//findObj('p' + ids_vins[i]).value = parseFloat(findObj('p' + ids_vins[i]).value);
		findObj('qte' + ids_vins[i]).value = parseFloat(findObj('qte' + ids_vins[i]).value);
		total += parseFloat(findObj('p' + ids_vins[i]).value * findObj('qte' + ids_vins[i]).value);
		qte += eval(findObj('qte' + ids_vins[i]).value);
		if (findObj('p' + ids_vins[i]).value < vin_remise){
			vin_remise = parseFloat(findObj('p' + ids_vins[i]).value);
		}
	}
	if (qte >23){
		remise = Math.floor(qte / 12);
		if (remise * 12 == qte){
			remise--;
		}
		remise--;
	}else{
		remise = 0;
	}
	total -= remise * vin_remise;
	total = Math.round(total*100)/100;
	findObj('total').value = total;
	if ((total*100)%100 == 0){
		total = total + ".00";
	}else if ((total*100)%10 == 0){
		total = total + "0";
	}
	findObj('aff_total').value = total + "€";
}

//teste la validité du form
function valid(){
	document.validForm = (!isNaN(findObj('total').value) && findObj('total').value > 0);
	if (!document.validForm){
		alert(findObj('msg_erreur').innerHTML + '');
	}
	
}


