// JavaScript Document
function listen(){
	editclik = document.getElementsByTagName("span");
	for(spp=0;spp<editclik.length;spp++){
		if(editclik[spp].className =="editthecom"){
			editclik[spp].onclick = function(){editTheCommnow(this);}
		}
		if(editclik[spp].className =="delete_comment"){
			editclik[spp].onclick = function(){deleteComment(this);}
		}
	}
}
function deleteComment(this_span){
	if(confirm('Delete this comment?')){
		var comment_id = this_span.getAttribute('name');
		xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null){
			  alert ("Your browser does not support AJAX!");
			  return;
			  } 
		var url="http://www.euroherp.com/ajax.php";
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4){ 
					var res = xmlHttp.responseText;
					if(res == '1'){
						alert('Error, comment was not deleted')
					}else{
						this_span.parentNode.parentNode.parentNode.removeChild(this_span.parentNode.parentNode);
					}
				}
			};
		myData = "ajax=TRUE";
		myData += "&delete_comment=TRUE";
		myData += "&comment="+comment_id;
		xmlHttp.send(myData);
	}
}
function editTheCommnow(id){
	theTxt = id.parentNode.getElementsByTagName("span");
	for(t=0;t<theTxt.length;t++){
		if(theTxt[t].className == "theCommentToEdit"){
			comid = theTxt[t].parentNode.getElementsByTagName("span");
				for(ty=0;ty<comid.length;ty++){
					if(comid[ty].className == "editthecom"){
						thecmid = comid[ty].getAttribute("name");
						}
					}
			textToEdit = theTxt[t].innerHTML;
			var text_content = theTxt[t].innerText || theTxt[t].textContent;
			var len =  text_content.length;
			lentoedit = 500-len;
			if (textToEdit.indexOf ("<textarea", 0) == -1) {
				textToEdit2 = theTxt[t].innerHTML;
				theTxt[t].innerHTML = '<textarea id="'+thecmid+'" name="textarea" cols="50" rows="4" onKeyUp="toCount(thecmid,\'comedit\',\'{CHAR} characters left\',500);">'+textToEdit+'</textarea><br /><input onclick="savetheinfo(this)" type="submit" name="Submit" value="Save" /><input onclick="cancel(this, textToEdit2)" name="button" type="button" id="button" value="Cancel" /><br /><span id="comedit" class="minitext">'+lentoedit+' characters left</span>';
				}
			}
		
		}
	}
function cancel(id, content){
	id.parentNode.innerHTML = content;
	}
function savetheinfo(id){
	txtarea = id.parentNode.getElementsByTagName("textarea");
	for(ta=0;ta<txtarea.length;ta++){
		if(txtarea[ta].getAttribute("name") == "textarea"){
			eddited = '<span class="alreadyEdited">This comment was edited on the: ' + Date()+ '</span><br />';
			settingtheeditdate = txtarea[ta].parentNode.parentNode.getElementsByTagName("span");
			for(edd=0;edd<settingtheeditdate.length;edd++){
				if(settingtheeditdate[edd].className =="edittted"){
					settingtheeditdate[edd].innerHTML = eddited;
					}
				}
			newCom = txtarea[ta].value;
			
			comid = txtarea[ta].getAttribute("id");
			txtarea[ta].parentNode.innerHTML = newCom;
			UpdateInfo(newCom, comid, eddited);
			}
		}
	}
function UpdateInfo(data, comid, eddited){
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
		  alert ("Your browser does not support AJAX!");
		  return;
		  } 
	var url="http://www.euroherp.com/updateCommentaire.php";
    xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	myData = "comupdated="+ data;
	myData += "&comid="+comid;
	myData += "&editt="+eddited;
	xmlHttp.send(myData);
}
