// JavaScript Document
// Open the lightbox pour afficher la boite de saisie de commentaires
function openbox(formtitle, fadin, idNouvelle) {

  var box = document.getElementById('box');
  
  //contient tout le formulaire
  box.innerHTML = '<span id="boxtitle"></span><form id="formCom" name="formCom" method="POST" action="index.php" ><p>Nom : <input type="text" name="nom" maxlength="60" size="60" required ><input type="hidden" name="ident" maxlength="60" size="60" ></p><p>Adresse email :<input type="text" name="mail" maxlength="60" size="60" required ></p>Texte :<textarea name="texte" rows="14" cols="46" required></textarea><p><input type="button" name="submit" value="Envoyer" onClick="ajaxInsertCommentaire();"><input type="button" name="cancel" value="Annuler" onclick="closeboxCom()"></p></form>';

  var btitle = document.getElementById('boxtitle');
  btitle.innerHTML = formtitle;

  if(fadin) {
	 gradient("box", 0);
	 fadein("box");
  } else {
    box.style.display='block';
  }

  document.getElementById("formCom").elements["ident"].value = idNouvelle;
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/


function ajaxInsertCommentaire() {

  var xhr = renvoiXHR();

  //recupere les valeurs pour la requete
  var ident = document.getElementById("formCom").elements["ident"].value;
  var nom = document.getElementById("formCom").elements["nom"].value;
  var mail = document.getElementById("formCom").elements["mail"].value;
  var texte = document.getElementById("formCom").elements["texte"].value;

  //on définit l'appel de la fonction au retour serveur
  xhr.onreadystatechange = function() { alert_ajaxInsertCommentaire(xhr); };
  xhr.open("POST", "ajax/validationCommentaires.php" , true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send("id="+ident+"&nom="+nom+"&mail="+mail+"&texte="+texte);


}
//fonction assurant le retour des données présentées
function alert_ajaxInsertCommentaire(xhr) {
  if (xhr.readyState == 4 && xhr.status == 200) {

    var docXML= xhr.responseXML;

    html = docXML.getElementsByTagName("message").item(0).firstChild.nodeValue;
    html += '<br /><br />';
    html += '<a href="#" onClick="closeboxCom()">Fermer</a>';

		document.getElementById("box").innerHTML = html;
  }
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/

//
function ajaxAffCommentaire(id) {

  var xhr = renvoiXHR();

  //on définit l'appel de la fonction au retour serveur
  xhr.onreadystatechange = function() { alert_ajaxAffCommentaire(xhr); };
  xhr.open("GET", "ajax/afficherCommentaires.php?id="+id , true);
  xhr.send(null);


}
//fonction assurant le retour des données présentées
function alert_ajaxAffCommentaire(xhr) {
  if (xhr.readyState == 4 && xhr.status == 200) {

    	var docXML= xhr.responseXML;
    	var commentaire = docXML.getElementsByTagName("commentaire");

    	//variable contenant le html retourné
	  	var html = '<span id="boxtitle"></span>';

      //on efface le contenu de la balise span
	    document.getElementById("box").innerHTML = '';

    	if (commentaire.length > 0)	{

        //on affiche la ligne pour chaque classement trouvé
       	for (i=0;i<commentaire.length;i++) {

  				html += '<div class="commentaire">';
            html += '<div class="desc">';
              html += '<span class="strong">' + commentaire.item(i).getElementsByTagName("auteur")[0].firstChild.nodeValue + '</span> a posté le <span class="italic">'+ commentaire.item(i).getElementsByTagName("date")[0].firstChild.nodeValue +'</span> à <span class="italic">'+ commentaire.item(i).getElementsByTagName("heure")[0].firstChild.nodeValue +'</span>';
            html += '</div>';
            html += '<div class="texte">'+ commentaire.item(i).getElementsByTagName("texte")[0].firstChild.nodeValue +'</div>';
          html += '</div>';
          html += '<br />';
        }

      } else {
        // on affiche un message spécifique dans le cas ou il n'y a pas de commentaire
        html += 'Pas de commentaires à afficher.<br /><br />';
      }
      
      html += '<a href="#" onClick="closeboxCom()">Fermer</a>';
  		//on renvoit le tableau sous forme html pour affichage dans le novigateur
			document.getElementById("box").innerHTML = html;
			document.getElementById('boxtitle').innerHTML = "Les 5 derniers commentaires : ";


  }
}





