function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}

function rechercheReference(nb)
{
	var reference = encodeURIComponent(document.getElementById("reference" +nb).value); // protège les données
	
	// on vérifie si la taille recherchée correspond à celle d'une référence
	if(reference.length >= 7)
	{	
		// on définit le masque pour une référence
		var mask = "^[0-9]{2}.[0-9]{4}$";
		var reg = new RegExp(mask,"g");
		if(reg.test(reference))
		{
			var xhr = getXMLHttpRequest();
			xhr.onreadystatechange = function()
			{
				if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0))
				{
					if (xhr.responseText != '')
					{
						document.getElementById("ref_valide" + nb).style.display = "inline";
						document.getElementById("ref_invalide" +nb).style.display = "none";
						document.getElementById("image_vide"+ nb).style.display = "none";
						
						document.getElementById("nomproduit"+ nb).innerHTML = xhr.responseText;
					}
					else
					{
						document.getElementById("ref_invalide"+ nb).style.display = "inline";
						document.getElementById("ref_valide"+ nb).style.display = "none";
						document.getElementById("image_vide"+ nb).style.display = "none";
						
						document.getElementById("nomproduit"+ nb).innerHTML = "";
					}
				} 
				else if (xhr.readyState < 4)
				{
					document.getElementById("ref_invalide"+ nb).style.display = "inline";
					document.getElementById("ref_valide"+ nb).style.display = "none";
					document.getElementById("image_vide"+ nb).style.display = "none";
					
					document.getElementById("nomproduit"+ nb).innerHTML = "";
				}
			};
			
			xhr.open("POST", "/ajax_verif_bon_de_commande.php", true);
			xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
			xhr.send("var1=" + reference);
		}
		else
		{
			alert("Référence non valide.");
		}
	}
	return false;
}

function readData(sData)
{
	(xhr.reponseText) = (sdata);
	alert(sData);	
}

function animationProduitSuivant()
{
	elementMaximumAfficheActuellement = document.getElementById("iBlocProduitAfficheActuellement").value;
	nombreTotalProduitAssocieeFiche = document.getElementById("iTotalProduitAffiche").value;
	
	// on désaffiche tous les élèments actuels
	for(i = 1; i <=  nombreTotalProduitAssocieeFiche; i++)
	{
		document.getElementById("eltProduit" + i).style.display = "none";
	}
		
	iElementAffiche = 0;
	for(j = (parseInt(elementMaximumAfficheActuellement) + 1); j <=  (parseInt(elementMaximumAfficheActuellement) + 3); j++)
	{
		if(document.getElementById("eltProduit" + j))
		{
			document.getElementById("eltProduit" + j).style.display = "block";
			// on conserve dans le formulaire l'indice du dernier produit affiché
			document.getElementById("iBlocProduitAfficheActuellement").value = j;
			iElementAffiche++;
		}
	}

	if(iElementAffiche < 3)
	{
		for(k = 1; k <=  (3-(parseInt(iElementAffiche))); k++)
		{
			document.getElementById("eltProduit" + k).style.display = "block";
			// on conserve dans le formulaire l'indice du dernier produit affiché
			document.getElementById("iBlocProduitAfficheActuellement").value = k;
		}
	}
}

function animationProduitPrecedent()
{
	elementMaximumAfficheActuellement = document.getElementById("iBlocProduitAfficheActuellement").value;
	elementMaximumAfficheActuellement = elementMaximumAfficheActuellement - 2;
	nombreTotalProduitAssocieeFiche = document.getElementById("iTotalProduitAffiche").value;
	
	// on désaffiche tous les élèments actuels
	for(i = 1; i <=  nombreTotalProduitAssocieeFiche; i++)
	{
		document.getElementById("eltProduit" + i).style.display = "none";
	}
		
	iElementAffiche = 0;
	j = (parseInt(elementMaximumAfficheActuellement) - 1);
	for(j; j >= (parseInt(elementMaximumAfficheActuellement) - 3); j--)
	{
		if(document.getElementById("eltProduit" + j))
		{
			document.getElementById("eltProduit" + j).style.display = "block";
			// on conserve dans le formulaire l'indice du dernier produit affiché
			document.getElementById("iBlocProduitAfficheActuellement").value = j;
			iElementAffiche++;
		}
	}

	if(iElementAffiche < 3)
	{
		passage = false;
		for(k = nombreTotalProduitAssocieeFiche; k >  (nombreTotalProduitAssocieeFiche-((3 - parseInt(iElementAffiche)))); k--)
		{
			document.getElementById("eltProduit" + k).style.display = "block";

			if(iElementAffiche == 0 && passage == false)
			{
				// on conserve dans le formulaire l'indice du dernier produit affiché
				document.getElementById("iBlocProduitAfficheActuellement").value = k;
			}
			passage = true;
		}
	}
}

