function getproduit(id,price){
	
	var key = price.split('_');
	key = key[1];
	
	
	var id = document.getElementById(id).value ;
	var pc = document.getElementById('pc').value ;
	new Ajax.Request('ajax/stock.php?p=3&id='+id+'&pc='+pc,
			  {
			    method:'get',
			    onSuccess: function(xhr){
			      var response = xhr.responseXML ;
			      
			      var label=response.getElementsByTagName("label" )[0].firstChild.nodeValue;
			      var px=response.getElementsByTagName("price" )[0].firstChild.nodeValue;
			      var point=response.getElementsByTagName("point" )[0].firstChild.nodeValue;
			      
			      //alert (point);
			      $('idp_'+key).innerHTML = label;
			      $('pxp_'+key).innerHTML = px ;
			      $('pv_'+key).innerHTML = point ;
			      $(price).value = px ;
			 
			    			    },
			    onFailure: function(){ alert('Something went wrong...') }
			  });

}

function getprodname_1(id,name){
	
	var key = name.split('_');
	key = key[1];
	
	new Ajax.Request('ajax/stock.php?p=1&id='+ document.getElementById(id).value ,
			  {
			    method:'get',
			    onSuccess: function(xhr){
				var response = xhr.responseText ;
	      
			      $('id_'+key).innerHTML = response;
  },
  onFailure: function(){ alert('Something went wrong...') }
});
}

function getprodname_2(id,name){
	var key = name.split('_');
	key = key[1];
	var id = document.getElementById(id).value ;
	var pc = document.getElementById('pc').value ;
	
	new Ajax.Request('ajax/stock.php?p=3&id='+id+'&pc='+pc,
			  {
			    method:'get',
			    onSuccess: function(xhr){
			      var response = xhr.responseXML ;
			      
			      var label=response.getElementsByTagName("label" )[0].firstChild.nodeValue;
			      var px=response.getElementsByTagName("price" )[0].firstChild.nodeValue;
			      var point=response.getElementsByTagName("point" )[0].firstChild.nodeValue;
			      
			      //alert (point);
			      $('id_'+key).innerHTML = label;
			      $('pxp_'+key).innerHTML = px ;
			      $('pv_'+key).innerHTML = point ;
			      $(price).value = px ;
			 	},
			    onFailure: function(){ alert('Something went wrong...') }
			  });
	
	
}



function getDistriId(id){
	new Ajax.Request('ajax/stock.php?p=4&id='+ document.getElementById(id).value ,
			  {
			    method:'get',
			    onSuccess: function(xhr){
				var response = xhr.responseText ;
				//alert(response)
	      
			      $('distrid').value = response ;
  },
  onFailure: function(){ alert('Something went wrong...') }
});
	
}

function getDistriId_visiteur(id){
	new Ajax.Request('ajax/stock.php?p=4&id='+ document.getElementById(id).value ,
			  {
			    method:'get',
			    onSuccess: function(xhr){
				var response = xhr.responseText ;
				//alert(response)
				 $('distrid').value = response ;
				if(response>0){
					$('visiteur').submit();
				} else {
					alert('Ce distributeur n\'existe pas!!!');
				}
  },
  onFailure: function(){ alert('Something went wrong...') }
});
	
}



function getprodname(id,name){
	var key = name.split('_');
	key = key[1];
	
	
	var perc = document.getElementById('perc').value *100 ;
	

	new Ajax.Request('ajax/stock.php?p=3&id='+ document.getElementById(id).value +'&pc='+perc,
			  {
			    method:'get',
			    onSuccess: function(xhr){
				var response = xhr.responseXML ;
			      var label=response.getElementsByTagName("label" )[0].firstChild.nodeValue;
			      var px=response.getElementsByTagName("price" )[0].firstChild.nodeValue;
			      var point=response.getElementsByTagName("point" )[0].firstChild.nodeValue;
			      
			     
			      //$('totpv_'+key).innerHTML = "";
			      if( $('tot_'+key)){
			      $('tot_'+key).innerHTML = "";
			      //$('qtep_'+key).value =0 ;
			      }
	
			      
			      $('idp_'+key).innerHTML = label;
			      $('idprice_'+key).innerHTML = px ;
			      $('idpv_'+key).innerHTML = point ;
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}
function getprodnamehb(id,name){
	var key = name.split('_');
	key = key[1];
	
	
	var perc = document.getElementById('perc').value *100 ;
	

	new Ajax.Request('ajax/stock.php?p=3&id='+ document.getElementById(id).value +'&pc='+perc+'&type=1',
			  {
			    method:'get',
			    onSuccess: function(xhr){
				var response = xhr.responseXML ;
			      var label=response.getElementsByTagName("label" )[0].firstChild.nodeValue;
			      var px=response.getElementsByTagName("price" )[0].firstChild.nodeValue;
			      var point=response.getElementsByTagName("point" )[0].firstChild.nodeValue;
			      
			     
			      //$('totpv_'+key).innerHTML = "";
			      if( $('tot_'+key)){
			      $('tot_'+key).innerHTML = "";
			      //$('qtep_'+key).value =0 ;
			      }
	
			      
			      $('idp_'+key).innerHTML = label;
			      $('idprice_'+key).innerHTML = px ;
			      $('idpv_'+key).innerHTML = point ;
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}
function totat_get(name){
	
	var key = name.split('_');
	key = key[1];
	price = document.getElementById("idprice_"+key).innerHTML * 1.0;
	pv = document.getElementById("idpv_"+key).innerHTML * 1.0;
	qty =document.getElementById("qtep_"+key).value ;
	total = price*qty
	total = total.toFixed(2);
	totalpv = pv * qty ;
	totalpv = totalpv.toFixed(2);

	
	//alert(total);
	
	$('tot_'+key).innerHTML = total ;
	$('totpv_'+key).innerHTML = totalpv  ;
	total_gen();
	
}
function total_gen(){
	var max = __ui ;
	totalGen = 0 ;
	totalGenpv = 0 ;
	totalGenQ = 0 ;
	for(i=0;i< max ; i++){
		price = document.getElementById("idprice_"+i).innerHTML * 1.0;
		pv = document.getElementById("idpv_"+i).innerHTML * 1.0;
		qty =document.getElementById("qtep_"+i).value ;
		total = price*qty
		totalpv = pv * qty ;
		totalGenpv = totalGenpv + totalpv ;
		totalGen = totalGen + total ;
		totalGenQ  = totalGenQ + (qty*1.0);
	}

	var pc = document.getElementById("transp").value ;
	pc = pc.replace(',','.') ;
	pc = pc*1.0 ;
	pc = 1+pc ;
	totaltransport = totalGen*pc;
	
	
	
	var totalGen = totalGen.toFixed(2);
	var totalGenpv = totalGenpv.toFixed(2);
	var totaltransport = totaltransport.toFixed(2);
	document.getElementById('gen').innerHTML = totalGen +" &euro;" ;
	document.getElementById('genpv').innerHTML = totalGenpv + " PV"  ; 
	document.getElementById('transportprice').innerHTML = totaltransport +" &euro;" ;
	$('genqte').innerHTML = totalGenQ ;
	
	
}

function getherbaid(id,name){
	//alert(name);
	var url = 'ajax/stock.php?p=2&id='+ document.getElementById(id).value ;
	var httpRequest = false;
	 if (window.XMLHttpRequest) { // Mozilla, Safari,...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // Voir la note ci-dessous à propos de cette ligne
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }
	 if (!httpRequest) {
            alert('Abandon :( Impossible de créer une instance XMLHTTP');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest,name); };
        httpRequest.open('GET', url, true);
        httpRequest.send(null);
			
}
function alertContents(httpRequest,name) {

    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            //alert(httpRequest.responseText);
            document.getElementById(name).innerHTML = httpRequest.responseText ;
        } else {
            alert('Un problème est survenu avec la requête.');
        }
    }

}
function getherbalifeid(id){
	
	new Ajax.Request('ajax/stock.php?p=5&id='+ document.getElementById(id).value ,
			  {
			    method:'get',
			    onSuccess: function(xhr){
				var response = xhr.responseXML ;
			 
			      var nom = response.getElementsByTagName("nom" )[0].firstChild.nodeValue;
			      var prenom = response.getElementsByTagName("prenom" )[0].firstChild.nodeValue;
			      var level = response.getElementsByTagName("label" )[0].firstChild.nodeValue;
			      var perc = response.getElementsByTagName("perc" )[0].firstChild.nodeValue;
			      		      
			      //$('herba').innerHTML = level ;
			      $('percc').innerHTML = perc * 100 +"%" ;
			      $('perc').value = perc  ;
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}

function load(name){
	 document.getElementById(name).innerHTML ="<img src='pic/ajax-loader.gif'>";
}

function stock_add_row(id){
	__ui = 21 ;
	var t = document.getElementById(id) ;
	var r  = document.createElement('tr');
	var ca = document.createElement('td');
	var cb = document.createElement('td');
	cb.innerHTML = "<div id='id_" + __ui+"'></div>"
	var cc = document.createElement('td');
	var ta = document.createElement('input');
	ta.name = 'ref_' + __ui ;
	ta.id='ref_' + __ui ;
	ta.setAttribute("onChange","getprodname_1('ref_" + __ui+"','id_" + __ui+"');");
	ta.setAttribute("style","width:51px;");
	ta.setAttribute("onKeyup","load('idp_"+ __ui+"');");
	var tb = document.createElement('input');
	tb.setAttribute("style","width:51px;");
	tb.name = 'qte_' + __ui ;
	tb.id='qte_' + __ui ;

	ca.appendChild(ta);
	cc.appendChild(tb);

	r.appendChild(ca);
	r.appendChild(cb);
	r.appendChild(cc);
	t.appendChild(r);
	
	document.getElementById('rows').value=__ui ;
	__ui++ ;
}

function stock_add_row2(id){
	
	var t = document.getElementById(id) ;
	var r  = document.createElement('tr');
	var ca = document.createElement('td');
	var cb = document.createElement('td');
	cb.innerHTML = "<div id='id_" + __ui+"'></div>"
	var cc = document.createElement('td');
	var ta = document.createElement('input');
	ta.name = 'ref_' + __ui ;
	ta.id='ref_' + __ui ;
	ta.setAttribute("onChange","getprodname_1('ref_" + __ui+"','id_" + __ui+"');");
	ta.setAttribute("style","width:51px;");
	ta.setAttribute("onKeyup","load('idp_"+ __ui+"');");
	var tb = document.createElement('input');
	tb.setAttribute("style","width:51px;");
	tb.name = 'qte_' + __ui ;
	tb.id='qte_' + __ui ;

	ca.appendChild(ta);
	cc.appendChild(tb);

	r.appendChild(ca);
	r.appendChild(cb);
	r.appendChild(cc);
	t.appendChild(r);
	
	document.getElementById('rows').value=__ui ;
	__ui++ ;
}

function totat_get_comm(name){
	var key = name.split('_');
	key = key[1];
	price = $("pxp_"+key).innerHTML *1.0 ;
	pv = $("pv_"+key).innerHTML ;
	qty =$("qte_"+key).value ;
	total = price*qty
	total = total.toFixed(2);
	totalpv = pv * qty ;
	totalpv = totalpv.toFixed(2);
	
	
	$('total_'+key).innerHTML = total + " &euro;" ;
	$('totalpv_'+key).innerHTML = totalpv + " PV"  ;
	total_gen_comm();
}

function total_gen_comm(){
	
	
	var max = __ui ;
	totalGen = 0 ;
	totalGenpv = 0 ;
	totalGenq = 0 ;
	
	
	for(i=0;i< max ; i++){
		price = $("pxp_"+i).innerHTML * 1.0;
		pv = $("pv_"+i).innerHTML * 1.0;
		qty =$("qte_"+i).value ;
		total = price*qty ;
		totalpv = pv * qty ;
		
		totalGenpv = totalGenpv + totalpv ;
		totalGen = totalGen + total ;
		totalGenq = totalGenq + (qty *1.0) ;
		
		//alert(pv) ;
	}
	var totalGen = totalGen.toFixed(2);
	var totalGenpv = totalGenpv.toFixed(2);
	//var totalGenq = totalGenq.toFixed(2);
	
	
	//var pc = document.getElementById("transp").value ;
	//pc = pc.replace(',','.') ;
	//pc = pc*1.0 ;
	//pc = 1+pc ;
	//totaltransport = totalGen*pc;
	//var totaltransport = totaltransport.toFixed(2);
	$('gen').innerHTML = totalGen +" &euro;" ;
	$('genpv').innerHTML = totalGenpv+" PV"  ;
	try {
		$('genq').innerHTML = totalGenq ;
	} 
	catch (err)
	{
	}
	//$('gentran').innerHTML = totaltransport +" &euro;"  ;
}





