function getXhr(){
    var xhr = null; 
		if(window.XMLHttpRequest) // Firefox et autres
		   xhr = new XMLHttpRequest(); 
		   
		else if(window.ActiveXObject){ // Internet Explorer 
		   try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
		}
		else { // XMLHttpRequest non supporté par le navigateur 
		   displayError("Votre navigateur ne supporte pas les technologies ajax."); 
		   xhr = false; 
		} 
    return xhr;
}

function setCheck(entId, isCheck){
	var xhr = getXhr();
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			displayError(xhr.responseText);
		}
	}
	xhr.open("POST","inc/ajax/surcheck.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	if (isCheck == true) {
		isCheck = 1;	
	}
	else
		isCheck = 0;
	xhr.send("id=" + entId + "&check=" + isCheck);
}

function detail(id, div, script){
	var xhr = getXhr();
	// On définit ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			div.innerHTML = xhr.responseText;
		}
	}
	xhr.open("POST","inc/ajax/" + script ,true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	xhr.send("id=" + id);
}

function naceForLine(entId){
	var div = document.getElementById("naceSpan" + entId);
	div.style.margin = "20px";
	div.style.display= "";
	detail(entId, div, "naceforline.php");
}

function detailForLine(entId){
	var div = document.getElementById("detailSpan" + entId);
	div.style.display = "";
	detail(entId, div, "detailforline.php");
}

function deleteEnt(id){
	var xhr = getXhr();
	// On définit un xhr pour récupérer le nom de l'entreprise à supprimer
	xhr.onreadystatechange = function(){
		// On demande confirmation en affichant le nom de l'entreprise prete à etre supprimee
		if(xhr.readyState == 4 && xhr.status == 200){
			var entnom = xhr.responseText;
			if (confirm("Voulez-vous vraiment supprimer l'entreprise " + entnom + "?"))	{
				// On définit un second xhr pour supprimer
				var xhr2 = getXhr();
				xhr2.onreadystatechange = function(){
					// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
					if(xhr2.readyState == 4 && xhr.status == 200){
						displayError(xhr2.responseText + ": " + xhr.responseText);
						document.getElementById("ln" + id).innerHTML = "--- supprim&eacute;. Cette ligne aura disparu lorsque vous aurez changé de page ---";
					}
				}
				xhr2.open("POST","inc/ajax/deleteperid.php",true);
				xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				
				xhr2.send("id=" + id);
			}
		}
	}
	xhr.open("POST","inc/ajax/nameperid.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	xhr.send("id=" + id);
		
}


function addEnt(){
	var xhr = getXhr();
	var nom = document.getElementById('entnom').value;
	var phone = document.getElementById('enttel').value;
	var fax = document.getElementById('entfax').value;
	var email = document.getElementById('entemail').value;
	var adresse = document.getElementById('entadresse').value;
	var date = document.getElementById('entdate').value;
	var ca = document.getElementById('entca').value;
	var nbemploi = document.getElementById('entnbemploi').value;
	var tva = document.getElementById('enttva').value;
	var cat = document.getElementById('entcat').options[document.getElementById('entcat').selectedIndex].value;
	var loc = document.getElementById('entloc').options[document.getElementById('entloc').selectedIndex].value;
	
	// On définit un xhr pour récupérer le nom de l'entreprise à supprimer
	xhr.onreadystatechange = function(){
		// On demande confirmation en affichant le nom de l'entreprise prete à etre supprimee
		if(xhr.readyState == 4 && xhr.status == 200){
			displayError(xhr.responseText);
		}
	}
	
	xhr.open("POST","inc/ajax/addEntreprise.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	xhr.send("entnom=" + nom + "&entloc=" + loc + "&entcat=" + cat + "&enttel=" + phone + "&entfax=" + fax + "&entemail=" + email + "&entadresse=" + adresse + "&entdate=" + date + "&entca=" + ca + "&entnbemploi=" + nbemploi + "&enttva=" + tva);
		
}

function ModifOneProprio(proprioId) {
	var xhr = getXhr();
	// On définit un xhr pour récupérer le nom de l'entreprise à supprimer
	xhr.onreadystatechange = function(){
		// On demande confirmation en affichant le nom de l'entreprise prete à etre supprimee
		if(xhr.readyState == 4 && xhr.status == 200){
			var t = xhr.responseText.split("|");
			var nom = t[0];
			var prenom = t[1];
			var phone = t[2];
			var email = t[3];
			ModifProprioForm(nom, prenom, phone, email, proprioId);
			
		}
	}
	xhr.open("POST","inc/ajax/proprio/detailProprio.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	xhr.send("id=" + proprioId);
}


function UpdateProprio() {
	
	var xhr = getXhr();
	hideSombre();
	var id = document.getElementById('proprioId').value;
	var nom = document.getElementById('proprioNom').value;
	var prenom = document.getElementById('proprioPrenom').value;
	var phone = document.getElementById('proprioPhone').value;
	var email = document.getElementById('proprioEmail').value;
	
	// On définit un xhr pour récupérer le nom de l'entreprise à supprimer
	xhr.onreadystatechange = function(){
		// On demande confirmation en affichant le nom de l'entreprise prete à etre supprimee
		if(xhr.readyState == 4 && xhr.status == 200){
			var entid = document.getElementById('entrepriseId').value;
			searchPers(entid);
			loadPers(entid);
			
			displayError(xhr.responseText);
		}
	}
	xhr.open("POST","inc/ajax/proprio/updateProprio.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	xhr.send("id=" + id + "&nom=" + nom + "&prenom=" + prenom + "&phone=" + phone + "&email=" + email);
}


function LoadCategorieInCombo() {
	var xhr = getXhr();
	var typObj = document.getElementById("typ");
	var typ = typObj.options[typObj.selectedIndex].value;	
	var cat = document.getElementById("cat");
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
				cat.innerHTML = xhr.responseText;
		}	
	}
	
	xhr.open("POST", "inc/ajax/getCatList.php", true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("type=" + typ);
}


function LoadCategorieInDetailCombo() {
	var xhr = getXhr();
	var typObj = document.getElementById("type");
	var typ = typObj.options[typObj.selectedIndex].value;	
	var cat = document.getElementById("categorie");
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
				cat.innerHTML = xhr.responseText;
		}	
	}
	
	xhr.open("POST", "inc/ajax/getCatList.php", true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("type=" + typ);
}


function LoadDistinctStreets() {
	var xhr = getXhr();
	var rue = document.getElementById("placeRue");
	rue.innerHTML = "<img src='img/loader.gif' />";
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
				rue.innerHTML = xhr.responseText;
		}	
	}
	
	xhr.open("POST", "inc/ajax/getStreets.php", true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("name=rue");
}
