// Fonction de creation de l'objet XMLHttpRequest qui resservira pour chaques fonctions AJAX 
function getXhr() 
{ 
	var xhr = null;
	
	if(window.XMLHttpRequest) 
		xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) 
	{ 
		try 
		{ 
			xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e) 
		{ 
			xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
	} 
	else 
	{ 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest, veuillez le mettre à jour"); 
		xhr = false; 
	} 
	return xhr;
} 

function getEl(el)
{
	return document.getElementById(el);
}

// Premiere fonction : remplacer le contenu d'un div // Sans recuperation de valeur 

function liste_genre(div ,v) 
{ 
	d = document.getElementById(div);
	
	var xhr = getXhr(); 
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 
			// Nous remplacons le contenu du div iris par le retour de "ajax/ajaxiris.php" 
			document.getElementById(div).innerHTML = xhr.responseText; 
		} 
	} 
	
	xhr.open("POST",'../../controls/ajax/genre.php',true); 
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
	xhr.send("type="+fam); 
} 

function liste(div, fic ,v) 
{ 
	d = document.getElementById(div);
	
	var xhr = getXhr(); 
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 
			// Nous remplacons le contenu du div iris par le retour de "ajax/ajaxiris.php" 
			if(xhr.responseText == "")
				d.style.display = "none";
			else
			{				
				if(d.style.display == "none")
					d.style.display = "";
			}
			document.getElementById(div).innerHTML = xhr.responseText; 
		} 
	} 
	
	xhr.open("POST",'/ajax/'+fic+'.php',true); 
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
	xhr.send("text="+v); 
} 

function valide_liste(div, id, nom, champ)
{
	d = document.getElementById(div);
	d.style.display = "none";
	document.getElementById(champ+"_nom").value = nom;
	document.getElementById(champ+"_id").value = id;
}

function valide_date(div, j, m, a, champ)
{
	d = document.getElementById(div);
	d.style.display = "none";
	/*document.getElementById(champ+"_j").value = j;
	document.getElementById(champ+"_m").value = m;
	document.getElementById(champ+"_a").value = a;*/
	document.getElementById(champ).value = a;
	
}

function connexion(fic)
{
	var xhr = getXhr(); 
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 
			alert(xhr.responseText); 
		} 
	} 
	
	xhr.open("GET",'/ajax/'+fic+'.php',true); 
	xhr.send(null); 
}

function checkLogin()
{
	var xhr = getXhr(); 
	getEl("loginMessage").innerHTML = "Veuillez patienter.";
	getEl("loginMessage").className = "loginWait";
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 
			if(xhr.responseText != "")
			{
				getEl("loginMessage").style.display = "";
				getEl("loginMessage").innerHTML = xhr.responseText+"<br /><br /><span>R&eacute;essayer</span>";
				getEl("loginMessage").className = "loginMessage"; 
			}
			else
			{
				getMembreInfos();
				changeLink("link_add_book_2", "linkAddBook");
				changeLink("link_biblio_2", "linkBiblio");
			}
		} 
	} 
	
	lelogin = getEl("login").value;
	pwd = getEl("loginPwd").value;
	
	xhr.open("GET","/ajax/checkLogin.php?login="+lelogin+"&pw="+pwd+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function getMembreInfos()
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 
			if(xhr.responseText != "")
			{
				getEl("loginDiv").innerHTML = xhr.responseText; 
			}
			else
			{
				getConnexionForm();
			}
		} 
	} 

	
	xhr.open("GET","/ajax/membreInfos.php?d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function getConnexionForm()
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 
			getEl("loginDiv").innerHTML = xhr.responseText; 
		} 
	} 

	
	xhr.open("GET","/ajax/connexionForm.php?d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function deconnect()
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getConnexionForm();
			changeLink("link_add_book_1", "linkAddBook");
			changeLink("link_biblio_1", "linkBiblio");
		} 
	} 

	
	xhr.open("GET",'/ajax/checkLogin.php?deconnect=1',true); 
	xhr.send(null); 
}

function changeLink(page, div)
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getEl(div).innerHTML = xhr.responseText;
		} 
	} 

	
	xhr.open("GET",'/template/'+page+'.template',true); 
	xhr.send(null); 
}

function getAuteursForm()
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getEl("listAuteurs").innerHTML = xhr.responseText;
		} 
	} 

	
	xhr.open("GET","/ajax/formAuteurs.php?d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function addAuteursForm(aId, nom, prenom, insert)
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getEl("listAuteurs").innerHTML = xhr.responseText;
		} 
	} 
	
	params = "";
	if(typeof nom != "undefined")
	{
		params += "&nom="+nom;
	}
	if(typeof prenom != "undefined")
	{
		params += "&prenom="+prenom;
	}
	if(typeof insert != "undefined")
	{
		params += "&insert=1";
	}
	xhr.open("GET","/ajax/formAuteurs.php?add="+aId+params+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function delAuteursForm(aId)
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getEl("listAuteurs").innerHTML = xhr.responseText;
		} 
	} 

	xhr.open("GET","/ajax/formAuteurs.php?del="+aId+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function getLivres(nump, kw)
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getEl("grid").innerHTML = xhr.responseText;
		} 
	} 
	
	params = "";
	if(typeof kw != "undefined")
	{
		params = "&kw="+kw;
	}

	xhr.open("GET","/ajax/bibliotheque.php?nump="+nump+params+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function delLivre(id)
{
	var xhr = getXhr(); 

	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			getEl("grid").innerHTML = xhr.responseText;
		} 
	} 

	xhr.open("GET","/ajax/bibliotheque.php?nump=1&del="+id+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function getISBN(isbn)
{
	var xhr = getXhr(); 
	getEl("isbn_load").className = "new_1 isbn_load";
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			if(xhr.responseText)
			{
				getEl("isbn_response").innerHTML = xhr.responseText;
				getEl("isbn_load").className = "new_1";
			}
			else
			{
				location.href = "/ajouter_un_livre_1.html";
			}
		} 
	} 

	xhr.open("GET","/ajax/getISBN.php?isbn="+isbn+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}

function getWS(kw, nump)
{
	var xhr = getXhr(); 
	getEl("kw_load").className = "new_1 isbn_load";
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			if(xhr.responseText)
			{
				getEl("kw_response").innerHTML = xhr.responseText;
				getEl("kw_load").className = "new_1";
			}
			else
			{
				//location.href = "/ajouter_un_livre_1.html";
			}
		} 
	} 

	xhr.open("GET","/ajax/getWS.php?kw="+kw+"&nump="+nump+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}


function noteComment(note, comId)
{
	var xhr = getXhr(); 
	xhr.onreadystatechange = function() 
	{ 
		if(xhr.readyState == 4 && xhr.status == 200) 
		{ 			
			if(xhr.responseText)
			{
				
			}
			else
			{
				//location.href = "/ajouter_un_livre_1.html";
			}
		} 
	} 

	xhr.open("GET","/ajax/noteComment.php?note="+note+"&comId="+comId+"&d="+ (new Date()).getTime(),true); 
	xhr.send(null); 
}