/**
 * To clear the default message in the textarea
 * not use but can be useful for later
 */
/*function clearTextArea(txtArea){
	var clickedIt = false;
	if (clickedIt == false){
		txtArea.value="";
		clickedIt=true;
	} 
}*/

/**
 * Function which allow to hide or show the comment area when clicking on the button
 */
function display(divId){
	var div = document.getElementById(divId);
	var display = div.style.display;
	var button = document.getElementById("buttonComment");
	
	if(display=="")display="none";
	
	if(display!="none"){
		button.value="Ajouter un commentaire";
		div.style.display="none";
	}else{
		button.value="Annuler";
		div.style.display="block";
	}
}

/**
 * Function which send the comment to the database
 * This is a dynamic process which use Ajax request and the ExtJS framework
 * @TODO : gerer les id
 */
function sendComment(divId, table, id){
	var text=document.getElementById("txtComment"+id).value;
	var email=document.getElementById("inputMail"+id).value;
	var pseudo=document.getElementById("inputPseudo"+id).value;
	var boolOk=true;
	var strOut="";
	
	//checkVar
	if(pseudo==""){
		strOut+="\n Veuillez compléter le champ pseudo.";
		boolOk=false;
	}
	if(text=="" || text=="Entrez votre commentaire..."){
		strOut+="\n Veuillez compléter le champ texte";
		boolOk=false;
	}
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
	if(email!="" && !reg.test(email)){
		strOut+="\n Adresse email invalide";
		boolOk=false;
	}
	
	if(table=="page")id=document.getElementById("idPage").value;
	else if(table=="draw")id=document.getElementById("idDraw").value;
	
	if(boolOk){
		if(window.XMLHttpRequest) // Firefox
			xhr_object = new XMLHttpRequest();
		else if(window.ActiveXObject) // Internet Explorer
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		else { // XMLHttpRequest non supporté par le navigateur
				alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
				return;
			}
			
		xhr_object.open("POST", 'db_access/insertComment.php', true);
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 4){
				alert("Votre message a été enregistré. Merci!");
				display(divId);
			}
		};
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=iso-8859-1");
		var post = "table="+table+"&id="+id+"&text="+text+"&user="+pseudo+"&mail="+email;
		xhr_object.send(post);
	}else alert(strOut);
}

function showComment_BD(id, table){	
	pop=window.open("popupComment.php?id="+id+"&table="+table,"fenetre","height=550,width=400, top=0, left=0, menubar=yes,scrollbars=yes,statusbar=yes");
}

function showComment_page(table){
	cleanComment();
	if(window.XMLHttpRequest) // Firefox
	xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
	}
	
	if(table=="page")var id=document.getElementById("idPage").value;
	else if(table=="draw")var id=document.getElementById("idDraw").value;
	
	xhr_object.open("POST", "db_access/searchComment.php", true);
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 4){
			var reponse=eval("(" + xhr_object.responseText + ")");
			loadReponse(reponse);
		}
	};
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=iso-8859-1");
	var post = "id="+id+"&table="+table;
	xhr_object.send(post);
}

function returnToPage(){
	this.close();
}
function loadReponse(tabData){
	var divPrinc=document.getElementById("zoneComment"); 
	
	for(var i=0; i<tabData.length; i++){
		var table = document.createElement('table');
		var ligne = table.insertRow(-1);
		var cell = ligne.insertCell(0);
		
		/*var input = document.createElement("input");
		input.type="text";
		input.disabled="disabled";
		input.value=tabData[i]['nomPosteur'];
		input.id="inputPosteur"+i;
		cell.appendChild(input);*/
		var date = mysqlTimeStampToDate(tabData[i]['dateHeure']);
		var strTxt="Posté par "+tabData[i]['nomPosteur']+" le "+date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()
			+" à "+date.getHours()+":"+date.getMinutes();
		cell.innerHTML = strTxt;
		
		/*cell = ligne.insertCell(1);
		var input = document.createElement("input");
		input.type="text";
		input.disabled="disabled";
		input.value=tabData[i]['mail'];
		input.id="inputMail"+i;
		cell.appendChild(input);*/
		
		var ligne = table.insertRow(-1);
		var cell = ligne.insertCell(0);
		cell.colSpan=2;
		var textArea = document.createElement("textarea");
		textArea.disabled="disabled";
		textArea.value=tabData[i]['texte'];
		textArea.id="text"+i;
		textArea.cols="30";
		cell.appendChild(textArea);
		 
		divPrinc.appendChild(table);
	}
}

function cleanComment(){
	var divPrinc=document.getElementById("zoneComment");
	//nettoyage avant tout
	while(divPrinc.firstChild){
		divPrinc.removeChild(divPrinc.firstChild);
	}
}

function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}
