function getHTTPObject() {
  var xmlhttp = false;
  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/
  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }
  return xmlhttp;
}

function setDivTemp(response) {
	var nDiv = document.getElementById('divTemp');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'divTemp';
	}
	nDiv.innerHTML = response;
	return nDiv;
}

function ajax_updateRayonSousFamilleAndProduit(expedition) {
	oHTTP = getHTTPObject();
	if (oHTTP) {
		oHTTP.onreadystatechange = ajax_retourUpdateRayonSousFamilleAndProduit;
		oHTTP.open("POST", SERVER_ROOT + "include/lib.ajax.php", true);
		oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oHTTP.send("action=updateRayonSousFamilleAndProduit&ID_RAYONSOUS=" + document.getElementById('ID_RAYONSOUS').value + '&expedition=' + expedition);
	}
}
function ajax_retourUpdateRayonSousFamilleAndProduit() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	var ID_RAYONSOUSFAMILLE = document.getElementById('ID_RAYONSOUSFAMILLE');	
	while (ID_RAYONSOUSFAMILLE.firstChild) {
		ID_RAYONSOUSFAMILLE.removeChild(ID_RAYONSOUSFAMILLE.firstChild);
	}
	var ID_PRODUIT = document.getElementById('ID_PRODUIT');	
	while (ID_PRODUIT.firstChild) {
		ID_PRODUIT.removeChild(ID_PRODUIT.firstChild);
	}
	if (response != 'failure') {
		var nDiv = setDivTemp(response);
		cSelect = nDiv.getElementsByTagName('select');
		while (cSelect[0].firstChild) {
			ID_RAYONSOUSFAMILLE.appendChild(cSelect[0].firstChild);
		}
		ID_RAYONSOUSFAMILLE.style.width = 'auto';
		ID_RAYONSOUSFAMILLE.selectedIndex = 0;
		if (cSelect[1]) {
			while (cSelect[1].firstChild) {
				ID_PRODUIT.appendChild(cSelect[1].firstChild);
			}
			ID_PRODUIT.style.width = 'auto';
			ID_PRODUIT.selectedIndex = 0;
			_showTR(true);
		} else {
			_showTR(false);
		}
	}
}

function ajax_updateProduit(expedition) {
	if (document.getElementById('ID_RAYONSOUSFAMILLE').value != '') {
		oHTTP = getHTTPObject();
		if (oHTTP) {
			oHTTP.onreadystatechange = ajax_retourUpdateProduit;
			oHTTP.open("POST", SERVER_ROOT + "include/lib.ajax.php", true);
			oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			oHTTP.send("action=updateProduit&ID_RAYONSOUSFAMILLE=" + document.getElementById('ID_RAYONSOUSFAMILLE').value + '&expedition=' + expedition);
		}
	}
	else {
		ajax_updateRayonSousFamilleAndProduit();
	}
}
function ajax_retourUpdateProduit() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	var ID_PRODUIT = document.getElementById('ID_PRODUIT');
	while (ID_PRODUIT.firstChild) {
		ID_PRODUIT.removeChild(ID_PRODUIT.firstChild);
	}
	if (response != 'failure') {
		var nDiv = setDivTemp(response);
		cSelect = nDiv.getElementsByTagName('select');
		while (cSelect[0].firstChild) {
			ID_PRODUIT.appendChild(cSelect[0].firstChild);
		}
		ID_PRODUIT.style.width = 'auto';
		ID_PRODUIT.selectedIndex = 0;
		_showTR(true);
	}
}

function ajax_updateDeclinaison() {
	oHTTP = getHTTPObject();
	if (oHTTP) {
		oHTTP.onreadystatechange = ajax_retourUpdateDeclinaison;
		oHTTP.open("POST", SERVER_ROOT + "include/lib.ajax.php", true);
		oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oHTTP.send("action=updateDeclinaison&ID_PRODUIT=" + document.getElementById('ID_PRODUIT').value);
	}
}
function ajax_retourUpdateDeclinaison() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	var aTR = YAHOO.util.Dom.getElementsByClassName('fromXML');
	for (i=0; i<aTR.length; i++) {
		aTR[i].parentNode.removeChild(aTR[i]);
	}
	if (response != 'failure') {
		var nDiv = setDivTemp(response);
		cTr = nDiv.getElementsByTagName('tbody')[0];
		var tbody = document.getElementById('tbody');			
		while (cTr.firstChild) {
			tbody.appendChild(cTr.firstChild);
		}
	}
}

function _showTR(show) {
	var aTR = YAHOO.util.Dom.getElementsByClassName('ajax_tr');
	var display = (show)
		? (document.all) ? 'block' : 'table-row'
		: 'none';
	for (i=0; i<aTR.length; i++) {
		aTR[i].style.display = display;
	}
}

/******************/
/* Liste produits */
/******************/

function majRayon(param) {
	oHTTP = getHTTPObject();
	if (oHTTP) {
		oHTTP.onreadystatechange = retourMajRayon;
		oHTTP.open("POST", "./../nomenclature/nom_filtreXML.php", true); //ouverture asynchrone
		oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oHTTP.send("action=majRayon&" + param);
	}
}
function retourMajRayon() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	var ID_RAYONSOUS = document.getElementById('ID_RAYONSOUS');
	while (ID_RAYONSOUS.firstChild) {
		ID_RAYONSOUS.removeChild(ID_RAYONSOUS.firstChild);
	}
	ID_RAYONSOUS.style.width = 0;
	if (response != 'failure') {
		var nDiv = setDivTemp(response);
		cSelect = nDiv.getElementsByTagName('select');
		cIndex = cSelect[0].selectedIndex;
		while (cSelect[0].firstChild) {
			ID_RAYONSOUS.appendChild(cSelect[0].firstChild);
		}
		/* selectedIndex pour IE6 */
  		for (i=0;i<ID_RAYONSOUS.options.length;i++) {
			ID_RAYONSOUS.options[i].setAttribute('selected',false);
		}
		ID_RAYONSOUS.options[cIndex].setAttribute('selected',true);
		/* fin IE6 */
		ID_RAYONSOUS.selectedIndex = cIndex;
		ID_RAYONSOUS.style.width = 'auto';
	}
	cInput = nDiv.getElementsByTagName('input');
	if (cInput.length > 0) {
		majRayonSous('ID_RAYONSOUS='+cInput[0].value+'&ID_RAYONSOUSFAMILLE='+cInput[1].value+'&ID_PRODUIT='+cInput[2].value+'&ID_RAYON='+document.getElementById('ID_RAYON').value);
	}
}

function majRayonSous(param) {
	oHTTP = getHTTPObject();
	if (oHTTP) {
		oHTTP.onreadystatechange = retourMajRayonSous;
		oHTTP.open("POST", "./../nomenclature/nom_filtreXML.php", true); //ouverture asynchrone
		oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oHTTP.send("action=majRayonSous&" + param);
	}
}
function retourMajRayonSous() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	var ID_RAYONSOUSFAMILLE = document.getElementById('ID_RAYONSOUSFAMILLE');
	if (ID_RAYONSOUSFAMILLE) {
		while (ID_RAYONSOUSFAMILLE.firstChild) {
			ID_RAYONSOUSFAMILLE.removeChild(ID_RAYONSOUSFAMILLE.firstChild);
		}
		ID_RAYONSOUSFAMILLE.style.width = 0;
		if (response != 'failure') {
			var nDiv = setDivTemp(response);
			cSelect = nDiv.getElementsByTagName('select');
			cIndex = cSelect[0].selectedIndex;
			while (cSelect[0].firstChild) {
				ID_RAYONSOUSFAMILLE.appendChild(cSelect[0].firstChild);
			}
			/* selectedIndex pour IE6 */
	  		for (i=0;i<ID_RAYONSOUSFAMILLE.options.length;i++) {
				ID_RAYONSOUSFAMILLE.options[i].setAttribute('selected',false);
			}
			ID_RAYONSOUSFAMILLE.options[cIndex].setAttribute('selected',true);
			/* fin IE6 */
			ID_RAYONSOUSFAMILLE.selectedIndex = cIndex;
			ID_RAYONSOUSFAMILLE.style.width = 'auto';
		}
	}
}
function setDivHoraire(response) {
	var nDiv = document.getElementById('divHoraire');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'divHoraire';
	}
	nDiv.innerHTML = response;
	return nDiv;
}
function setDivAdresse(response) {
	var nDiv = document.getElementById('divAdresse');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'divAdresse';
	}
	nDiv.innerHTML = response;
	return nDiv;
}
function setDivCreneauLivraison(response) {
	var nDiv = document.getElementById('divCreneauLivraison');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'divCreneauLivraison';
	}
	nDiv.innerHTML = response;
	return nDiv;
}
function setDivPaiement(response) {
	var nDiv = document.getElementById('divPaiement');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'divPaiement';
	}
	nDiv.innerHTML = response;
	return nDiv;
}
function setDivLienAdresse(response) {
	var nDiv = document.getElementById('divLienAdresse');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'divLienAdresse';
	}
	nDiv.innerHTML = response;
	return nDiv;
}
function setDivDatesLivraison(response) {
	var nDiv = document.getElementById('datesLivraison');
	if (!nDiv) {
		nDiv = document.createElement('div');
		nDiv.id = 'datesLivraison';
	}
	nDiv.innerHTML = response;
	return nDiv;
}