function connectURL(url)
{
	if (window.XMLHttpRequest)
		objXHR = new XMLHttpRequest();
	else if(window.ActiveXObject)
		objXHR = new ActiveXObject("Microsoft.XMLHTTP");
	else
		return false;

	objXHR.open("GET", url ,false);
	objXHR.send(null);

	if (objXHR.readyState == 4)
		return objXHR.responseText;
	else
		return false;
}

function is_numeric(num)
{
	var exp = new RegExp("^[0-9-.]*$","g");
	return exp.test(num);
}

function verifExisteDossier(kdo)
{
	if(kdo != '' && is_numeric(kdo))
	{	
		libelle	=	connectURL('template/ajax/verif_kdo.php?kdo='+kdo);
		if(libelle == 'erreur')
			alert('Erreur ce produit n\'existe pas dans ce catalogue');
	}
}

function validFormulaire(form)
{
	var erreur	=	0;
	var color	=	'1px solid #e3001b';
	var messerreur	=	'Veuillez remplir tous les champs';
	
	if(document.getElementById('agence').selectedIndex == 0)
	{
		erreur++;
		document.getElementById('agence').style.border	=	color;
		document.getElementById('agence').style.backgroundColor	=	'#fbcfeb';
	}
	if(document.getElementById('compte').value != '')
	{
		if(!is_numeric(document.getElementById('compte').value) || document.getElementById('compte').value.length != 6)
		{
			alert('Le numéro de compte client n\'est pas correct');
			return false;
		}
	}
	
	var nbProduit	=	0;
	for (var i=0;i<=nbChamp;i++)
    {
		if(document.getElementById("kdo_"+i))
		{
			if(document.getElementById("kdo_"+i).value == '' && document.getElementById("qte_"+i).value == '')
			{
				
			}
			else
			{
				nbProduit++;
				if(document.getElementById("kdo_"+i).value == '')
				{
					erreur++;
					document.getElementById("kdo_"+i).style.border	=	color;
					document.getElementById("kdo_"+i).style.backgroundColor	=	'#fbcfeb';
				}
				
				if(document.getElementById("kdo_"+i).value.length < 4)
				{
					erreur++;
					document.getElementById("kdo_"+i).style.border	=	color;
					document.getElementById("kdo_"+i).style.backgroundColor	=	'#fbcfeb';
					messerreur = 'Code produit erroné';
				}
				
				if(document.getElementById("qte_"+i).value == '')
				{
					erreur++;
					document.getElementById("qte_"+i).style.border	=	color;
					document.getElementById("qte_"+i).style.backgroundColor	=	'#fbcfeb';
				}
			}
		}
    }
	
	if(nbProduit == 0)
	{
		erreur++;
		alert('Veuillez saisir au moins 1 article');
	}

	if(erreur == 0)
	{
		if(confirm('Êtes-vous certain de vouloir valider cette commande ?'))
			form.submit();
	}
	else
		alert(messerreur);
}

function die(champ, msg)
{
	document.getElementById(champ).focus();
	return false;
}

function afficheBeneficiaire(value)
{
	if(value.length == 6)
	{
		libelle	=	connectURL('template/ajax/beneficiaire.php?compte='+value);
		if(libelle == 'erreur')
		{
			alert('Erreur ce numéro de compte n\'existe pas');
			document.getElementById('compte').value	=	'';
		}
		else
			document.getElementById('gagnant').value	=	libelle;
	}
}

function afficheLibelle(kdo, idChamp)
{
	//alert(numDossier);
	if(kdo != '' && is_numeric(kdo))
	{	
		retour	=	connectURL('template/ajax/verif_kdo.php?kdo='+kdo+'&dossier='+numDossier);
		if(retour != '')
		{
			//alert(retour);
			libelle	=	connectURL('template/ajax/libelle_kdo.php?kdo='+kdo+'&new='+retour+'&dossier='+numDossier);
			if(libelle == 'erreur')
			{
				alert('Erreur ce produit n\'existe pas');
				document.getElementById('libelleKdo_'+idChamp).innerHTML	=	'';
			}
			else
			{
				document.getElementById('libelleKdo_'+idChamp).innerHTML	=	libelle;
				//remplissage du nombre de points
				points	=	connectURL('template/ajax/points_kdo.php?kdo='+kdo+'&new='+retour+'&dossier='+numDossier);
				document.getElementById('points_'+idChamp).value	=	points;
			}
			
			calculTotalPoints();
		}
		else
		{
			alert('La référence "'+kdo+'" n\'existe pas dans votre catalogue');
			document.getElementById('kdo_'+idChamp).value	=	'';
			document.getElementById('qte_'+idChamp).value	=	'';
			document.getElementById('libelleKdo_'+idChamp).innerHTML	=	'';
		}		
	}
}

function afficheLibelleBatifoot(kdo, idChamp, dossier)
{
	//alert(dossier);
	if(kdo != '')
	{	
		//alert('template/ajax/verif_kdo_batifoot.php?kdo='+kdo+'&dossier='+dossier);
		var retour	=	connectURL('template/ajax/verif_kdo_batifoot.php?kdo='+kdo+'&dossier='+dossier);
		if(retour != '')
		{
			//alert(retour);
			var libelle	=	connectURL('template/ajax/libelle_kdo_batifoot.php?kdo='+kdo+'&new='+retour+'&dossier='+dossier);
			if(libelle == 'erreur')
			{
				alert('Erreur ce produit n\'existe pas');
				document.getElementById('libelleKdo_'+idChamp).innerHTML	=	'';
			}
			else
			{
				document.getElementById('libelleKdo_'+idChamp).innerHTML	=	libelle;
			}
		}
		else
		{
			alert('La référence "'+kdo+'" n\'existe pas dans votre catalogue');
			document.getElementById('kdo_'+idChamp).value	=	'';
			document.getElementById('qte_'+idChamp).value	=	'';
			document.getElementById('libelleKdo_'+idChamp).innerHTML	=	'';
		}		
	}
}

function contour(champ, kdo)
{
	if(document.getElementById(champ).value != '')
	{
		document.getElementById(champ).style.border				=	'1px solid #ca027f';
		document.getElementById(champ).style.backgroundColor	=	'#ffffff';
	}

	if(document.getElementById(champ).value.length == 4)
	{
		var champ	=	champ.replace("kdo_", "");
		afficheLibelle(kdo, champ);
	}
}

function contour2(champ)
{
	champ	=	champ.replace("qte_", "");
	champ	=	'qte_'+champ;
	
	if(document.getElementById(champ).value != '')
	{
		document.getElementById(champ).style.border				=	'1px solid #ca027f';
		document.getElementById(champ).style.backgroundColor	=	'#ffffff';
	}
	
	//gestion du total des points
	calculTotalPoints();
}

function contourBatifoot(champ, kdo, dossier)
{
	if(document.getElementById(champ).value != '')
	{
		document.getElementById(champ).style.border				=	'1px solid #ca027f';
		document.getElementById(champ).style.backgroundColor	=	'#ffffff';
	}

	if(document.getElementById(champ).value.length > 3)
	{
		var champ	=	champ.replace("kdo_", "");
		afficheLibelleBatifoot(kdo, champ, dossier);
	}
}

function contour2Batifoot(champ)
{
	champ	=	champ.replace("qte_", "");
	champ	=	'qte_'+champ;
	
	if(document.getElementById(champ).value != '')
	{
		document.getElementById(champ).style.border				=	'1px solid #ca027f';
		document.getElementById(champ).style.backgroundColor	=	'#ffffff';
	}
}

function calculTotalPoints()
{
	var totalPoints	=	0;
	for (var i=0;i<=nbChamp;i++)
    {
		if(document.getElementById("qte_"+i))
		{
			qte	=	document.getElementById("qte_"+i).value;
			pts	=	document.getElementById("points_"+i).value;
			
			
			if(is_numeric(qte) && qte>0 && is_numeric(pts) && pts>0)
			{
				totalPoints	+=	qte * pts;
			}
		}
    }
	
	if(totalPoints > 0)
	{
		document.getElementById("totalPoints").style.display	=	'block';
		document.getElementById("totalPoints").innerHTML		=	totalPoints+' pts';
	}
	else
	{
		document.getElementById("totalPoints").style.display	=	'none';
		document.getElementById("totalPoints").innerHTML		=	'';
	}
	
	//alert(totalPoints);
}

var	nbChamp	=	0;

function addProduitPanier()
{
	nbChamp++;
	
	var table 		= 	document.getElementById('tableCommande');
	var lignetr 	= 	document.createElement("tr");
	var lignetd1	=	document.createElement("td");
	var lignetd2	=	document.createElement("td");
	var lignetd3	=	document.createElement("td");
	var lignetd4	=	document.createElement("td");
	var lignetd5	=	document.createElement("td");
	var lignetd6	=	document.createElement("td");
	
	var inputRef	=	document.createElement("input");
	var inputQte	=	document.createElement("input");
	var inputPts	=	document.createElement("input");
	
	var divLibelle	=	document.createElement("div");
	
	var spanEspace	=	document.createElement("span");
	
	var lienSuppr	=	document.createElement("a");
	var imgSuppr	=	document.createElement("img");
	
	/****************************************************************************/
	
	lignetr.className				=	'ligne_1';
	lignetd1.style.border			=	'none';
	lignetd2.style.border			=	'none';
	lignetd3.style.border			=	'none';
	lignetd4.style.border			=	'none';
	lignetd5.style.border			=	'none';
	lignetd6.style.border			=	'none';
	inputRef.style.width			=	'50px';
	inputQte.style.width			=	'50px';
	lignetd1.style.verticalAlign	=	'top';
	lignetd2.style.verticalAlign	=	'top';
	lignetd3.style.verticalAlign	=	'top';
	lignetd4.style.verticalAlign	=	'top';
	lignetd5.style.verticalAlign	=	'top';
	lignetd6.style.verticalAlign	=	'top';
	
	inputPts.type			=	'hidden';
	inputPts.id				=	'points_'+nbChamp;

	lignetd2.className		=	'champs2';
	lignetd6.className		=	'champs2';
	
	lienSuppr.href			=	'#';
	lienSuppr.id			=	'lienSuppr_'+nbChamp;
	imgSuppr.src			=	'template/img/cancel.jpg';
	
	inputRef.name			=	'kdo[]';
	inputQte.name			=	'qte[]';
	
	lignetr.id				=	'lignetr_'+nbChamp;
	inputRef.id				=	'kdo_'+nbChamp;
	inputQte.id				=	'qte_'+nbChamp;
	divLibelle.id			=	'libelleKdo_'+nbChamp;
	
	inputRef.onchange		=	function(){contour(this.id, this.value);};
	inputQte.onkeyup		=	function(){contour2(this.id);};	
	lienSuppr.onclick		=	function(){deleteLigne(this.id);};

	lignetd1.innerHTML		=	'R&eacute;f Stimactiv :';
	lignetd3.innerHTML		=	'Libell&eacute; :';
	lignetd5.innerHTML		=	'Qt&eacute; :';
	
	spanEspace.innerHTML	=	'&nbsp;';

	divLibelle.style.width		=	'175px';
	divLibelle.style.textAlign	=	'left';
	divLibelle.style.color		=	'#555555';
	divLibelle.style.fontStyle	=	'italic';
	
	/****************************************************************************/
	//Ajout des éléments les uns dans les autres
	
	lienSuppr.appendChild(imgSuppr);

	lignetd2.appendChild(inputRef);
	lignetd4.appendChild(divLibelle);
	lignetd6.appendChild(inputQte);
	lignetd6.appendChild(spanEspace);
	lignetd6.appendChild(lienSuppr);
	lignetd6.appendChild(inputPts);
	       
	lignetr.appendChild(lignetd1); 
	lignetr.appendChild(lignetd2); 
	lignetr.appendChild(lignetd3); 
	lignetr.appendChild(lignetd4); 
	lignetr.appendChild(lignetd5); 
	lignetr.appendChild(lignetd6);
	table.getElementsByTagName('tbody')[0].appendChild(lignetr);
}

function addProduitPanierBatifoot(dossierClient)
{
	nbChamp++;
	
	var table 		= 	document.getElementById('tableCommande');
	var lignetr 	= 	document.createElement("tr");
	var lignetd1	=	document.createElement("td");
	var lignetd2	=	document.createElement("td");
	var lignetd3	=	document.createElement("td");
	var lignetd4	=	document.createElement("td");
	var lignetd5	=	document.createElement("td");
	var lignetd6	=	document.createElement("td");
	
	var inputRef	=	document.createElement("input");
	var inputQte	=	document.createElement("input");
	var inputPts	=	document.createElement("input");
	
	var divLibelle	=	document.createElement("div");
	
	var spanEspace	=	document.createElement("span");
	
	var lienSuppr	=	document.createElement("a");
	var imgSuppr	=	document.createElement("img");
	
	/****************************************************************************/
	
	lignetr.className				=	'ligne_1';
	lignetd1.style.border			=	'none';
	lignetd2.style.border			=	'none';
	lignetd3.style.border			=	'none';
	lignetd4.style.border			=	'none';
	lignetd5.style.border			=	'none';
	lignetd6.style.border			=	'none';
	inputRef.style.width			=	'50px';
	inputQte.style.width			=	'50px';
	lignetd1.style.verticalAlign	=	'top';
	lignetd2.style.verticalAlign	=	'top';
	lignetd3.style.verticalAlign	=	'top';
	lignetd4.style.verticalAlign	=	'top';
	lignetd5.style.verticalAlign	=	'top';
	lignetd6.style.verticalAlign	=	'top';
	
	inputPts.type			=	'hidden';
	inputPts.id				=	'points_'+nbChamp;

	lignetd2.className		=	'champs2';
	lignetd6.className		=	'champs2';
	
	lienSuppr.href			=	'#';
	lienSuppr.id			=	'lienSuppr_'+nbChamp;
	imgSuppr.src			=	'template/img/cancel.jpg';
	
	inputRef.name			=	'kdo[]';
	inputQte.name			=	'qte[]';
	
	lignetr.id				=	'lignetr_'+nbChamp;
	inputRef.id				=	'kdo_'+nbChamp;
	inputQte.id				=	'qte_'+nbChamp;
	divLibelle.id			=	'libelleKdo_'+nbChamp;
	
	inputRef.onchange		=	function(){contourBatifoot(this.id, this.value, dossierClient);};
	inputQte.onkeyup		=	function(){contour2Batifoot(this.id);};	
	lienSuppr.onclick		=	function(){deleteLigne(this.id);};

	lignetd1.innerHTML		=	'R&eacute;f Stimactiv :';
	lignetd3.innerHTML		=	'Libell&eacute; :';
	lignetd5.innerHTML		=	'Qt&eacute; :';
	
	spanEspace.innerHTML	=	'&nbsp;';

	divLibelle.style.width		=	'175px';
	divLibelle.style.textAlign	=	'left';
	divLibelle.style.color		=	'#555555';
	divLibelle.style.fontStyle	=	'italic';
	
	/****************************************************************************/
	//Ajout des éléments les uns dans les autres
	
	lienSuppr.appendChild(imgSuppr);

	lignetd2.appendChild(inputRef);
	lignetd4.appendChild(divLibelle);
	lignetd6.appendChild(inputQte);
	lignetd6.appendChild(spanEspace);
	lignetd6.appendChild(lienSuppr);
	lignetd6.appendChild(inputPts);
	       
	lignetr.appendChild(lignetd1); 
	lignetr.appendChild(lignetd2); 
	lignetr.appendChild(lignetd3); 
	lignetr.appendChild(lignetd4); 
	lignetr.appendChild(lignetd5); 
	lignetr.appendChild(lignetd6);
	table.getElementsByTagName('tbody')[0].appendChild(lignetr);
}

function deleteLigne(idLigne)
{
	valeur	=	idLigne.replace("lienSuppr_", "");

	var tableau	=	document.getElementById('tableCommande');
	var ligne 	= 	document.getElementById('lignetr_'+valeur);
	tableau.removeChild(ligne);
}

function  validLigneFacture(idLigne)
{
	if(document.getElementById('ligne_'+idLigne).checked == false)
	{
		if (confirm("Êtes-vous certain de vouloir décocher ce produit ?"))
			retour	=	connectURL('template/ajax/valide_facture.php?id='+idLigne);
	}
	else
		retour	=	connectURL('template/ajax/valide_facture.php?id='+idLigne);
}

function  validLigneFactureProdCli(idLigne)
{
	if(document.getElementById('ligne_'+idLigne).checked == false)
	{
		if (confirm("Êtes-vous certain de vouloir décocher ce produit ?"))
			retour	=	connectURL('template/ajax/valide_facture_prod_cli.php?id='+idLigne);
	}
	else
		retour	=	connectURL('template/ajax/valide_facture_prod_cli.php?id='+idLigne);
}

/********************************/

function confirmation(url,Qu)//demande confirmation avant d'allez a l'url
{
   var Question = confirm(Qu);
   if (Question) window.location=url;
}

/********************************/

//valide la livraison de produits Doras pour l'opération Batifoot
function validLivraisonBatifoot(idLigne)
{
	retour	=	connectURL('template/ajax/valide_livraison_batifoot.php?id='+idLigne);
}

function cacheAnnee(annee)
{
	if(document.getElementById('annee_'+annee).style.display == 'none')
		document.getElementById('annee_'+annee).style.display = 'block';
	else
		document.getElementById('annee_'+annee).style.display = 'none';
}
