// JavaScript Document

function menuHL(el, onoff) {
	//get the elements.
	if (navigator.appName == "Microsoft Internet Explorer") {
		mdate = el.firstChild
		mheader = el.firstChild.nextSibling
	} else {
		mdate = el.firstChild.nextSibling
		mheader = el.firstChild.nextSibling.nextSibling.nextSibling
	}
	if (mdate.className == "menudate on") {
		return	
	}
	
	if (onoff == "on") {
		mdate.style.color = "#ffffff"
		mheader.style.color = "#666666"
	} else {
		mdate.style.color = "#666666"
		mheader.style.color = "#333333"
	}
}

//form functions

function clearInp(el, val, restore) {
	if (el.value == val) {
		el.value = ""
	}
	if (restore) {
		if (el.value == "" ) {
			el.value = val
		}
	}
}

var dispF
var hideF

function loadEntry(ID) {
	window.location = "diary.php?id="+ID	
}

function showform() {
	clearInterval(hideF)
	document.getElementById('postcommentheader').style.borderBottom = "1px solid #911619";
	dispF = setInterval(displayform, 70);
	
}
function hideform(notice) {
	clearInterval(dispF);
	hideF = setInterval(function() {collapseform(notice)}, 70);
}

function displayform() {
	form = document.getElementById('commentformwrapper');
	fh = form.style.height
	if (fh == "" || fh == "undefined") {
		form.style.height = 0 + "px"
		fh = form.style.height
		h = 0;
	} else {
		h = parseFloat(fh)
		
	}

	if (h < 450) {
		h = h + 25;
		form.style.height = h + "px"
		document.getElementById('postcommentheader').scrollIntoView()
	} else {
		clearInterval(dispF);
		

	}
}

function collapseform(notice) {
	form = document.getElementById('commentformwrapper');
	fh = form.style.height
	h = parseFloat(fh)
	if (h > 0) {
		h = h - 25;
		form.style.height = h + "px"
	} else {
		form.style.height = 0 + "px"
		clearInterval(hideF);
		document.getElementById('postcommentheader').style.borderBottom = "";
		if (notice == 'on') {
			thanksNotice('on')
		}
	}
}

// funt for post commentssss ========================
function GetXmlHttpObject() {
	var xmlHttp=null;
	try  {// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {// Internet Explorer
		try	  {
		 	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 }
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function HtmlEncode(s)
{
var y=document.createElement('span');
y.innerHTML=s;
return y.innerHTML;
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

/*
function html_entity_decode(str) {

try

	{
	
	var tarea=document.createElement('textarea');
	
	tarea.innerHTML = str; return tarea.value;
	
	tarea.parentNode.removeChild(tarea);
	
	}

catch(e)

	{
	
	//for IE add <div id="htmlconverter" style="display:none;"></div> to the page
	
	document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
	
	var content = document.getElementById("innerConverter").value;
	
	document.getElementById("htmlconverter").innerHTML = "";
	
	return content;
	
	}

}*/

function postComment(language) { //language is req for getting the correct form
	//get the values
	
	name = document.getElementById('name').value
	Email = document.getElementById('email').value
	Comment = document.getElementById('comment').value
	Verifycode = document.getElementById('verifycode').value //hardcoded
	Verify = document.getElementById('verify').value
	Entryid = document.getElementById('entryid').value
	//trim
	name = name.replace(/^\s+|\s+$/g, '') ;
	Email = Email.replace(/^\s+|\s+$/g, '') ;
	
	Comment = Comment.replace(/^\s+|\s+$/g, '') ;
	//validate
	msg = ""
	if (language == "english") {
		if (Verifycode != Verify) {
			msg = "You have entered the code incorrectly.";	
		}
		
		if (Email == "" || Email == "your email (this will not be published on screen)") {
			msg = "Please enter your email address.\nYour email address will ot be published on the site."
		}
		if (name == "" || name == "your name") {
			msg = "Please enter your name."
		}
		if (Comment == "" || Comment == "your comment") {
			msg = "Please enter your comment."
		}

		
	} else {
		if (Verifycode != Verify) {
			msg = "Transcriba el codigo en rojo.";
		}/**/
		eBlank = html_entity_decode('correio electr&oacute;nico (no fer&aacute; publicada en esta p&aacute;gina)')
		if (Email == "" || Email == eBlank) {
			msg="correio electr&oacute;nico (no fer&aacute; publicada en esta p&aacute;gina)"
		}
		if (name == "" || name == "nombre") {
			msg = "Nombre";
		}
		if (Comment == "" || Comment == "comentario") {
			msg = "Commentario"
		}/**/
	}
	//alert('Email='+Email+'\n\neBlank='+eBlank)
	
	if (msg != "") {
		msg = HtmlEncode(msg)
		alert(msg)
		return
	} else {
		//encode urls send to script
		name = encodeURIComponent(name);
		Email = encodeURIComponent(Email);
		Comment = encodeURIComponent(Comment);
		
		formw = document.getElementById('commentformwrapper');
		loader = document.getElementById('loadergif');
		loader.className = ""
		
		var post = "name="+name+"&email="+Email+"&comment="+Comment+"&entryid="+Entryid
		
		PAGExmlHttp=GetXmlHttpObject()
		url = "../addcomment.php";
		if (PAGExmlHttp==null)  {
			  alert ("Your browser does not support AJAX!");
			  return;
		} 
		PAGExmlHttp.onreadystatechange=function () { 
			if (PAGExmlHttp.readyState==4) { 
					
					if(PAGExmlHttp.responseText != "failed") {
												
						hideform('on');
						loader.className = "hidden"
						td = PAGExmlHttp.responseText.split('||')
						
						d = td[0]
						t = td[1]
						
						name = decodeURIComponent(name);
						Comment = decodeURIComponent(Comment);
						//increaseCommentCount()
						//addNewComment(name, Comment, d, t)
						refreshform(Entryid, language)
						
						
					} else {
						alert('Sorry the comment couldn\'t be added\n There was a connection error. Please try again.');
					}
									
			}
		}
		url=url+"?sid="+Math.random()

		PAGExmlHttp.open("POST",url,true);
		PAGExmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		PAGExmlHttp.send(post);/**/	
		
	}//eof if message
}

function addNewComment(name, comment, dt, tm) {
	
		
	
	//these are the functions to add
	holder = document.getElementById('commentsHolder');
	cw = document.createElement('div');
	cw.className = "commentswrapper"
	
			
	
	cd = document.createElement('div');
	cd.className = "commentdetails"
	
	c = document.createElement('div');
	c.className = "comment"
	
	com = document.createTextNode(comment);
	c.appendChild(com);
	
	cn = document.createElement('span');
	cn.className = "commentname"
	
	cnt = document.createTextNode(name)
	cn.appendChild(cnt);
	
	cda = document.createElement('span');
	cda.className = "commentdate"
	
	cdat = document.createTextNode(dt)
	cda.appendChild(cdat);
	br = document.createElement('br')
	cda.appendChild(br)
	ctime = document.createTextNode(tm)
	cda.appendChild(ctime);
	//add two spans to div
	br2 = document.createElement('br')
	cd.appendChild(cn)
	cd.appendChild(br2);
	cd.appendChild(cda)
	
	//add the comment details and comment to cw
	cw.appendChild(cd)
	cw.appendChild(c)
	//add the whole thing to the holder
	
	//this is for the fade in
	if (navigator.appName != "Microsoft Internet Explorer") {
		cw.style.opacity = 0
	} else {
		
		cw.style.filter = "alpha(opacity:"+0+")";
	}
	holder.appendChild(cw)
	//fIn = setInterval(fadeIn,100,cw)
	fIn = setInterval(function() {fadeIn(cw)}, 120);
}
var fIn
function fadeIn (el) {
	if (navigator.appName != "Microsoft Internet Explorer") {
		op = el.style.opacity;
		op=parseFloat(op);
	} else {
		
		op = el.filters.alpha.opacity
		op = parseFloat(op)
		op = op/100
	}
	
	if (op < 1) {
		op = op+0.05
		if (navigator.appName != "Microsoft Internet Explorer") {
			el.style.opacity = op
		} else {
			op = op * 100
			el.style.filter = "alpha(opacity:"+op+")";
		}
	} else {
		clearInterval(fIn);
		if (navigator.appName != "Microsoft Internet Explorer") {
			el.style.opacity = 1
		} else {
			el.style.filter = "alpha(opacity:"+100+")";
		}
	}
}

function refreshform(entryid, language) {
		url = "../commentform.php"
		var post = "&entryid="+entryid+"&language="+language
		PAGExmlHttp=GetXmlHttpObject()
		
		if (PAGExmlHttp==null)  {
			  alert ("Your browser does not support AJAX!");
			  return;
		} 
		PAGExmlHttp.onreadystatechange=function () { 
			if (PAGExmlHttp.readyState==4) { 
					document.getElementById('commentformwrapper').innerHTML = PAGExmlHttp.responseText
						
			}
		}
		url=url+"?sid="+Math.random()+post

		PAGExmlHttp.open("GET",url,true);
		//PAGExmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
		PAGExmlHttp.send(null);/**/	
}
function increaseCommentCount() {
	comheadertext = document.getElementById('commentheader').innerHTML
	comheadertext = comheadertext.replace(/^\s+|\s+$/g, '') ;
	if (comheadertext != "") {
		temp = comheadertext.split('(')
		temp2 = temp[1].split(")")
		n = parseInt(temp2)
		n = n+1
		document.getElementById('commentheader').innerHTML ='<img src="../images/speechbubble.png" alt="speech bubble"  />comments ('+n+')'
	} else {
		document.getElementById('commentheader').innerHTML ='<img src="../images/speechbubble.png" alt="speech bubble"  />comments (1)'
	}
}

//thanks notice
var sn
function thanksNotice(onoff) {
	box = document.getElementById('commentadded')
	if (onoff == "off") {
		box.className =	"hidden"
	} else {
		box.className =""
		//alert('offset='+document.body.offsetHeight)
		//window.scrollTo(0, document.body.offsetHeight)
		window.location="diary.php#thankyou";
	}
}


