/* ----------------------------------------------------------
		Admin Tools - v2.0b - par Cédric TUMMINELLO


  -----------------------------------------------------------*/

liste_description = new Array ;
deplacer_id_site = "" ;
deplacer_id_cat = "" ;
liste_titre = new Array ;
liste_href = new Array ;

// ------ Effacement des sites -----------
function deleteSite(prmIdSite) {
	if (confirm("Etes-vous sûr de vouloir supprimer ce site?")) {
		conn = new Ajax(true) ;
		conn.appendData("idSite",prmIdSite) ;
		conn.sendAndLoad("scripts/deleteSite.php","POST",deleteSiteHandler) ;
	}
	else alert("La suppression a été annulée!") ;
}

function deleteSiteHandler(conn) {
	if (conn.responseText == "1") alert("La suppression a bien été effectuée!") ;	
	else {
		if (conn.responseText == "-1") alert("La suppression a échouée... Veuillez réessayer plus tard!") ;
	}
}

// ------ Effacement des catégories --------------
function deleteCat(prmIdCat) {
	if (confirm("Etes-vous sûr de vouloir supprimer cette catégorie?")) {
		conn = new Ajax(true) ;
		conn.appendData("idCat",prmIdCat) ;
		conn.sendAndLoad("scripts/deleteCat.php","POST",deleteCatHandler) ;
	}
	else alert("La suppression a été annulée!") ;	
}


function deleteCatHandler(conn) {
	if (conn.responseText == "1") alert("La suppression a bien été effectuée!") ;	
	else {
		if (conn.responseText == "-1") alert("La suppression a échouée... Veuillez réessayer plus tard!") ;
	}
}
// ------- Changement des descriptions -----------
function transformDescription(prmId) {
	//déclaration des variables locales
	var textarea, obj, texte, p ;
	
	if (liste_description[prmId] != true ) {
		obj = document.getElementById(prmId + '_description') ;	// on récupère le texte 
		textarea = document.createElement('textarea') ; //on crée un noeud
		
		textarea.style.width = '620px';
		textarea.style.height = '75px';
		textarea.style.border= "1px gray solid" ;
		textarea.style.fontSize = "10pt";
		textarea.style.fontFamily = "tahoma" ;
		
		texte = document.createTextNode(obj.firstChild.firstChild.nodeValue) ;
		textarea.appendChild(texte) ;

		obj.replaceChild(textarea,obj.firstChild) ; //on remplace le texte figé par un textarea
		liste_description[prmId] = true ;
	
	}
	else {
		obj = document.getElementById(prmId + '_description') ;	// on récupère le texte 
		p = document.createElement('p') ; //on crée un noeud
		
		texte = document.createTextNode(obj.firstChild.value) ;
		the_texte = obj.firstChild.value ;
		p.appendChild(texte) ;
			
	
		obj.replaceChild(p,obj.firstChild) ; //on remplace le texte figé par un textarea
		liste_description[prmId] = false ;
		
		if (window.confirm("Voulez-vous enregistrer les modifications ?")) {
			conn = new Ajax(true) ;
			conn.appendData("idSite",prmId) ;
			conn.appendData("description",the_texte) ;
			conn.sendAndLoad("/ajax/updateDescription.php","POST",updateDescriptionHandler) ;
		}
		else alert("L'enregistrement de la description a été annulé.") ;
	}
}


function updateDescriptionHandler(conn) {
	if (conn.responseText == "1") alert("La description a bien été mise à jour !") ;		
	else alert("Erreur: mise à jour impossible") ;
}

// ------------- Changement des titres ---------------------------------------------
function transformTitre(prmId) {
//déclaration des variables locales
	var input, obj, texte, a ;
	
	if (liste_titre[prmId] != true ) {
		obj = document.getElementById(prmId + '_titre') ;	// on récupère le texte 
		input = document.createElement('input') ; //on crée un noeud
		
		input.style.width = '500px';
		input.style.border = "1px gray solid";
		input.value = obj.firstChild.nodeValue ;
		
		obj.replaceChild(input,obj.firstChild) ; //on remplace le texte figé par un textarea
		liste_titre[prmId] = true ;
	
	}
	else {
		obj = document.getElementById(prmId + '_titre') ;	// on récupère le texte 
		
		a = document.createElement('a') ; //on crée un noeud
		
		texte = document.createTextNode(obj.firstChild.value) ;
		the_texte = obj.firstChild.value ;
		a.appendChild(texte) ;
		
			
		obj.replaceChild(a,obj.firstChild) ; //on remplace le texte figé par un textarea
		liste_titre[prmId] = false ;
		
		if (window.confirm("Voulez-vous enregistrer les modifications ?")) {
			conn = new Ajax(true) ;
			conn.appendData("idSite",prmId) ;
			conn.appendData("titre",the_texte) ;
			conn.sendAndLoad("scripts/updateTitre.php","POST",updateTitreHandler) ;
		}
		else alert("L'enregistrement du titre a été annulé.") ;
	}
}

function updateTitreHandler(conn) {
		alert("Le titre a bien été mis à jour !") ;		
}


// ------- Déplacement des catégories ------------
function deplacerCat(prmId) {
	if (document.getElementById(prmId + '_CAT').style.display == 'none') {
		var conn = new Ajax(true) ;
		conn.sendAndLoad("scripts/listeCat.php?name=catTocat&id=" + prmId,"GET",deplacerCatHandler) ;
		deplacer_id_cat = prmId ;
	}
	else {
		document.getElementById(prmId + '_CAT').style.display = 'none' ;	
	}	
}

function deplacerCatHandler(conn) {
	var tmp = document.getElementById(deplacer_id_cat + '_CAT') ;
	tmp.style.display = 'block'; 
	
	tmp.innerHTML = conn.responseText ;
}

function moveCat(prmId) {
	select1 = document.getElementById("catTocat_" + prmId) ;
	id_cat = select1.options[select1.selectedIndex].value ;
	var conn = new Ajax(true) ;
	conn.sendAndLoad("scripts/deplacerCat.php?id_from=" + prmId + "&id_to=" + id_cat + "&" + Math.random(),"GET",moveCatHandler) ;
}

function moveCatHandler(conn) {
	document.getElementById(parseInt(conn.responseText) + '_CAT').style.display = 'none'; 
	alert("La catégorie a bien été déplacée !");	
}

// ------- Déplacement des sites -----------------
function deplacerSite(prmId) {
	if (document.getElementById(prmId + '_siteMove').style.display == 'none') {
		var conn = new Ajax(true) ;
		conn.sendAndLoad("scripts/listeCat.php?name=siteTocat&id=" + prmId,"GET",deplacerSiteHandler) ;
		deplacer_id_site = prmId ;
	}
	else {
		document.getElementById(prmId + '_siteMove').style.display = 'none' ;	
	}
}

function deplacerSiteHandler(conn) {
	var tmp = document.getElementById(deplacer_id_site + '_siteMove') ;
	tmp.style.display = 'block'; 
	
	tmp.innerHTML = conn.responseText ;
}

function moveSite(prmId) {
	select1 = document.getElementById("siteTocat_" + prmId) ;
	id_cat = select1.options[select1.selectedIndex].value ;
	var conn = new Ajax(true) ;
	conn.sendAndLoad("scripts/deplacerSite.php?id_site=" + prmId + "&id_cat=" + id_cat + "&" + Math.random(),"GET",moveSiteHandler) ;
}

function moveSiteHandler(conn) {
	document.getElementById(conn.responseText + '_siteMove').style.display = 'none'; 
	alert("Le site a bien été déplacé !");	
}

// ------- Ajouter une catégorie ---------
function ajoutCategorie(prmParentCat) {
	titre = prompt("Entrez ici le titre de la catégorie","") ;
	if (confirm("Cette opération va créer une catégorie dont le titre sera '" + titre + "'.\n\nSouhaitez-vous confirmer cette opération ?")) {
		conn = new Ajax(true) ;
		conn.sendAndLoad("scripts/catCreate.php?id_parent=" + prmParentCat + "&nom="+ escape(titre),"GET",ajouterCategorieHandler) ;
	}
}

function ajouterCategorieHandler(conn) {
	alert("La catégorie a bien étée crée") ;
}


function changeCat() {
	description = document.getElementById("parentcat").description.value ;
	id = document.getElementById("parentcat").cat_id.value ;
	
	conn = new Ajax(true) ;
	conn.appendData("cat_id",id) ;
	conn.appendData("description",description) ;
	conn.sendAndLoad("scripts/changeDescription.php","POST",changeCatHandler) ;
}

function changeCatHandler(conn) {
	if (conn.responseText == 1) alert("Description mise à jour avec succès!") ;	
	else alert("error") ;
}