function requeteHttp()
{
	var xhr_object = null;
	
	try{
		xhr_object = new XMLHttpRequest();
	}
	catch(Error){
		try{
			xhr_object = new ActiveXObject("MSXML2.XMLHTTP");
		}
		catch(Error){
			try{
				xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(Error){
				alert("Votre navigateur ne supporte pas la technologie AJAX.");
			}
		}
	}
	return xhr_object;
}

Array.prototype.in_array = function(val)
{
	for(var i = 0; i < this.length; i++)
	{
		if(this[i] == val)
			return true;
	}
	return false;
}

function envoiRequete(ressource, donnees, element, method, callback)
{
	document.body.style.cursor = "progress";
	
	var xhr_object = requeteHttp();
	if(xhr_object)
	{
		if(method == "get")
			ressource+= "?"+donnees;
		xhr_object.open(method, ressource, true);
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		
		xhr_object.send(donnees);
		
		xhr_object.onreadystatechange = function()
		{
			if(xhr_object.readyState == 4)
			{
				if(xhr_object.status == 200)
				{
					if(element != null)
						element.innerHTML = xhr_object.responseText;
					else
						callback(xhr_object.responseText);
				}
				else
				{
					setEtat('Erreur de récupération des données.')				
				}
				document.body.style.cursor = "default";
			}
		}
	}
	else
		return 0;
}

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

function confirm(msg, callback)
{
	
	var span = document.createElement('span');
	var message = document.createTextNode(msg);
	
	var inputOui = document.createElement('input');
	inputOui.setAttribute('value', 'Oui');
	inputOui.setAttribute('name', 'Oui');
	inputOui.setAttribute('type', 'button');
	inputOui.setAttribute('onclick', callback);
	inputOui.setAttribute('style', 'margin-right: 20px');
	
	var inputNon = document.createElement('input');
	inputNon.setAttribute('value', 'Non');
	inputNon.setAttribute('name', 'Non');
	inputNon.setAttribute('type', 'button');
	inputNon.setAttribute('onclick', 'switchEtat()');
	inputNon.setAttribute('style', 'margin-left: 20px');
	
	span.appendChild(message);
	span.appendChild(document.createElement('br'));
	span.appendChild(document.createElement('br'));
	span.appendChild(inputOui);
	span.appendChild(inputNon);
	
	setEtat(span.innerHTML);
}

function sautDeLigne(texte)
{
	texte = texte.replace(/\n/g, "<br />");
	return texte;
}

function setEtat(texte)
{
	var text = getEl('message_text');
	
	text.innerHTML = texte;
	calculDimensionsMessage();
	switchEtat();
}

function switchEtat()
{
	var div = getEl('message_zone');
	if(div.style.display == "block")
		div.style.display = "none";
	else
		div.style.display = "block";
	return false;
}

function calculDimensionsMessage()
{
	var div = getEl('message_background');
	
	div.style.height = "0px";
	div.style.width = "0px";
	
	dimensions = getDimensions();
	dimensionsConteneur = getDimensionsConteneur();
	
	var largeurConteneur = dimensionsConteneur[0];
	var largeurWindow = dimensions[0];
	
	var hauteurConteneur = dimensionsConteneur[1];
	var hauteurWindow = dimensions[1];
	
	//text.innerHTML = "C [W "+largeurConteneur+" H "+hauteurConteneur+"]<br />W [W "+largeurWindow+" H "+hauteurWindow+"]";
	
	if(hauteurConteneur > hauteurWindow)
		hauteurDiv = hauteurConteneur;
	else
		hauteurDiv = hauteurWindow;
		
	if(largeurConteneur > largeurWindow)
		largeurDiv = largeurConteneur;
	else
		largeurDiv = largeurWindow;
	
	div.style.height = hauteurDiv+"px";
	div.style.width = largeurDiv+"px";
}

function getDimensions() 
{
	var dimensions = new Array(2);
	
	var width;
	var height;
	
	width = document.documentElement.clientWidth;
	height = document.documentElement.clientHeight;

	dimensions[0] = width;
	dimensions[1] = height;

	return dimensions;
}

function getDimensionsConteneur()
{
	var dimensions = new Array(2);
	
	var conteneur = getEl('conteneur');

	dimensions[0] = conteneur.clientWidth;
	dimensions[1] = conteneur.clientHeight;

	return dimensions;
}

function recherche(msg)
{
	var q = getEl('input_recherche').value;
	if(q.length >= 3)
		window.location.href = "recherche.php?q="+q;
	else
		setEtat(msg);
	return false;
}

function getRepresentation(idPays,Pays)
{
	var representation_AL=getEl("representation_AL");
	var representation_HA=getEl("representation_HA");
	var representation_HE=getEl("representation_HE");
	var nomPays=getEl("nomPays");
	var xhr_object = requeteHttp();
	if(xhr_object)
	{
		xhr_object.open("POST", "ajax_get_representation.php", true);
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = "IdPays="+idPays;
		xhr_object.send(data);
		
		xhr_object.onreadystatechange = function()
		{
			if(xhr_object.readyState == 4)
			{
				//var xml = xhr_object.responseXML;
				var tab = eval("("+xhr_object.responseText+")");
				representation_AL.innerHTML = sautDeLigne(tab["AL"]);
				representation_HA.innerHTML = sautDeLigne(tab["HA"]);
				representation_HE.innerHTML = sautDeLigne(tab["HE"]);
				
				nomPays.innerHTML = Pays;
			}
		}
	}
}

function getActualite(idActualite)
{
	var div_affichage=getEl("affichageActualite");
	var actualite=getEl("actualite");
	var actualiteAffichee=getEl("actualite_aff");
	var titre_new_actualite=getEl("titre"+idActualite);
	if(actualiteAffichee!=null && actualiteAffichee.value != "")
	{
		var titre_old_actualite=getEl("titre"+actualiteAffichee.value);
	}
	if (actualiteAffichee!=null && idActualite==actualiteAffichee.value)
	{
		$('#affichageActualite').slideUp("normal",null);
		titre_old_actualite.setAttribute("className","actualiteOrange");
		titre_old_actualite.setAttribute("class","actualiteOrange");
		actualiteAffichee.value='';
	}
	else
	{
		var xhr_object = requeteHttp();
		if(xhr_object)
		{
			xhr_object.open("POST", "ajax_get_actualite.php", true);
			xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			var data = "IdActualite="+idActualite;
			xhr_object.send(data);
			
			xhr_object.onreadystatechange = function()
			{
				if(xhr_object.readyState == 4)
				{
					if(div_affichage.style.display=="none")
					{
						actualite.innerHTML=xhr_object.responseText;
						$('#affichageActualite').slideDown();
						titre_new_actualite.setAttribute("className","actualiteRouge");
						titre_new_actualite.setAttribute("class","actualiteRouge");
					}
					else
					{
						$('#affichageActualite').slideUp("normal",function()
								{
									titre_old_actualite.setAttribute("className","actualiteOrange");
									titre_old_actualite.setAttribute("class","actualiteOrange");
									actualite.innerHTML=xhr_object.responseText;
									$('#affichageActualite').slideDown();
									titre_new_actualite.setAttribute("className","actualiteRouge");
									titre_new_actualite.setAttribute("class","actualiteRouge");
								});
					}
				}
			}
		actualiteAffichee.value=idActualite;
		}
		location.hash=actualiteAffichee.value;
	}
}

function initActualite(idActualite)
{
	//on recupere le hash de depart
	hash_en_cours = document.location.hash;
	hash_en_cours = hash_en_cours.replace(/#/g, "");
	
	//Si on a un hash au chargement on met a jour la page en concequence
	if (hash_en_cours != "")
		getActualite(hash_en_cours);
	else
		getActualite(idActualite);
	
}

//Admin
function deconnexion()
{
	confirm('Voulez-vous vous déconnecter ?', "window.location.href = 'connexion.php?Deconnexion'");	
}

function supprimerActualite(lien)
{
	confirm('Voulez-vous supprimer cette actualité ?', "window.location.href = '"+lien+"'");	
}

function modifierActualite(lien)
{
	window.location.href = lien;
}

function supprimerTitre(IdTitre, NomCotage)
{
	confirm('Voulez-vous supprimer le cotage: '+NomCotage+' ?', "window.location.href = 'vente_supprimer.php?IdTitre="+IdTitre+"'");	
}

function supprimerTitreChoudens(IdTitre, NomCotage)
{
	confirm('Voulez-vous supprimer le cotage: '+NomCotage+' ?', "window.location.href = 'choudens_supprimer.php?IdTitre="+IdTitre+"'");	
}

function setOrdreActualites(tableauActualites)
{
	var xhr_object = requeteHttp();
	if(xhr_object)
	{
		xhr_object.open("POST", "ajax_set_ordre_actualite.php", true);
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = tableauActualites;
		xhr_object.send(data);
		
		xhr_object.onreadystatechange = function()
		{
			if(xhr_object.readyState == 4)
			{
				window.location="index.php";
			}
		}
	}
	
}

//Autocomplétion pour la recherche d'oeuvres
function initAutoCompleteInstrument(couleur)
{
	initAutoComplete(document.getElementById('form'), document.getElementById('instrument'), document.getElementById('valider'), couleur, "ajax_autocompletion_instruments.php", null);
}

//Autocomplétion pour la recherche d'oeuvres
function initAutoCompleteCompositeur(couleur)
{
	initAutoComplete(document.getElementById('form'), document.getElementById('compositeur'), document.getElementById('valider'), couleur, "ajax_autocompletion_auteurs.php", null);
}

//Autocomplétion pour la recherche d'oeuvres Choudens
function initAutoCompleteInstrumentChoudens(couleur)
{
	initAutoComplete(document.getElementById('form'), document.getElementById('instrument'), document.getElementById('valider'), couleur, "ajax_autocompletion_instruments_choudens.php", null);
}

//Autocomplétion pour la recherche d'oeuvres Choudens
function initAutoCompleteCompositeurChoudens(couleur)
{
	initAutoComplete(document.getElementById('form'), document.getElementById('compositeur'), document.getElementById('valider'), couleur, "ajax_autocompletion_auteurs_choudens.php", null);
}

//Autocomplétion pour l'administration
function initAutoCompleteCategorie()
{
	initAutoComplete(document.getElementById('form'), document.getElementById('categorie_autocompletion'), document.getElementById('envoyer'), "Rouge", "ajax_autocompletion_instruments.php", setCategorie);
}

//Autocomplétion pour l'administration
function initAutoCompleteAuteur()
{
	initAutoComplete(document.getElementById('form'), document.getElementById('auteur_autocompletion'), document.getElementById('envoyer'), "Rouge", "ajax_autocompletion_auteurs.php", setAuteur);
}

//Autocomplétion pour l'administration
function initAutoCompleteAuteurChoudens()
{
	initAutoComplete(document.getElementById('form'), document.getElementById('auteur_autocompletion'), document.getElementById('envoyer'), "Rouge", "ajax_autocompletion_auteurs_choudens.php", setAuteur);
	typeAuteur = "choudens";
}

//Autocomplétion pour l'administration
function initAutoCompleteFormationChoudens()
{
	initAutoComplete(document.getElementById('form'), document.getElementById('formation_autocompletion'), document.getElementById('envoyer'), "Rouge", "ajax_autocompletion_formations_choudens.php", null);
}

function setCategorie(IdCategorie, Categorie)
{
	if(IdCategorie == null && _inputField.value != "")
	{
		//Création d'un auteur
		envoiRequete("ajax_formulaire_ajout_categorie.php", "Categorie="+_inputField.value, getEl('message_text'), "get");
		switchEtat();
		_inputField.blur()
	}
	else if(IdCategorie != null && Categorie != null)
	{
		var liste = getEl('liste_categories');
		var span = document.createElement('span');
		span.setAttribute('id', IdCategorie);
		var Nom = document.createTextNode(Categorie);
		
		var spanImg = document.createElement('span');
		spanImg.setAttribute('class', 'boutonSup');
		spanImg.setAttribute('onclick', 'supCategorie(\''+IdCategorie+'\')');
		
		//PAtch car IE c'est de la merde
		if(document.all)
		{
			spanImg.setAttribute('className', 'boutonSup');
			spanImg.setAttribute('onclick', supCategoriePourIE);
		}
		
		var input = document.createElement('input');
		input.setAttribute('value', IdCategorie);
		input.setAttribute('name', 'RefCategorie[]');
		input.setAttribute('type', 'hidden');
		
		span.appendChild(spanImg);
		span.appendChild(Nom);
		span.appendChild(input);
		span.appendChild(document.createElement('br'));
		
		liste.appendChild(span);
		setTimeout("_inputField.focus()", 500);
	}
}

function supCategoriePourIE()
{
	supCategorie(this.parentNode.id);
}

function supCategorie(IdCategorie)
{
	liste = getEl('liste_categories');
	liste.removeChild(getEl(IdCategorie));
}

function ajouterCategorie()
{
	Categorie = getEl("Categorie_NEW").value;
	
	envoiRequete("ajax_ajout_categorie.php", "Categorie="+Categorie, null, "post", function (reponse)
		{
			if(parseInt(reponse))
			{
				switchEtat();
				setCategorie(reponse, Categorie);
			}
			else
				getEl('message_text').innerHTML = reponse;
		});
}

function setAuteur(IdAuteur, NomAuteur)
{
	if(IdAuteur == null && _inputField.value != "")
	{
		//Création d'un auteur
		envoiRequete("ajax_formulaire_ajout_auteur.php", "NomAuteur="+_inputField.value, getEl('message_text'), "get");
		switchEtat();
		_inputField.blur()
	}
	else if(IdAuteur != null && NomAuteur != null)
	{
		var liste = getEl('liste_auteurs');
		var span = document.createElement('span');
		span.setAttribute('id', IdAuteur);
		var Nom = document.createTextNode(NomAuteur);
		
		var spanImg = document.createElement('span');
		spanImg.setAttribute('class', 'boutonSup');
		spanImg.setAttribute('onclick', 'supAuteur(\''+IdAuteur+'\')');
		
		//PAtch car IE c'est de la merde
		if(document.all)
		{
			spanImg.setAttribute('className', 'boutonSup');
			spanImg.setAttribute('onclick', supAuteurPourIE);
		}
		
		var input = document.createElement('input');
		input.setAttribute('value', IdAuteur);
		input.setAttribute('name', 'RefAuteur[]');
		input.setAttribute('type', 'hidden');
		
		span.appendChild(spanImg);
		span.appendChild(Nom);
		span.appendChild(input);
		span.appendChild(document.createElement('br'));
		
		liste.appendChild(span);
		setTimeout("_inputField.focus()", 100);
	}
}

function supAuteurPourIE()
{
	supAuteur(this.parentNode.id);
}

function supAuteur(IdAuteur)
{
	liste = getEl('liste_auteurs');
	liste.removeChild(getEl(IdAuteur));
}

function ajouterAuteur()
{
	if(typeAuteur == "choudens")
		var url = "ajax_ajout_auteur_choudens.php"
	else
		var url = "ajax_ajout_auteur.php"
	
	NomAuteur = getEl("NomAuteur_NEW").value;
	PrenomAuteur = getEl("PrenomAuteur_NEW").value;
	
	envoiRequete(url, "NomAuteur="+NomAuteur+"&PrenomAuteur="+PrenomAuteur, null, "post", function (reponse)
		{
			if(parseInt(reponse))
			{
				switchEtat();
				setAuteur(reponse, NomAuteur+" "+PrenomAuteur);
			}
			else
				getEl('message_text').innerHTML = reponse;
		});
}

function envoiForm()
{
	if(_lastKeyCode == 13)
		return false;
	else
		return true;
}

function switchOrchestre(select)
{
	var page=select.name;
	var initiale=select.value;
	/*
	if(initiale != "initiale")
		window.location.href = "orchestre_recherche_oeuvres.php?valider=1&p="+page +"&l="+initiale.toLowerCase();
	*/
	window.open("../orchdata/"+page + "_" + initiale.toLowerCase() +".htm")
	//window.open("http://www.alphonseleduc.com/orchdata/"+page + "_" + initiale.toLowerCase() +".htm")
}

var typeAuteur;
typeAuteur = null;