﻿function postComment()
{
	if(!document.getElementById("commentForm").author.value){
		showCaution("Моля, въведете име.")
		document.getElementById("commentForm").author.focus();
		return false;
	} else clearCaution();
	
	if(!document.getElementById("commentForm").email.value){
		showCaution("Моля, въведете e-mail.")
		document.getElementById("commentForm").email.focus();
		return false;
	}
	else{
		clearCaution();
		if(document.getElementById("commentForm").email.value.search(/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ig)==-1){
			showCaution("Моля, въведете валиден e-mail.")
			document.getElementById("commentForm").email.focus();
			return false;
		}
		else clearCaution();
	}
	
	if(!document.getElementById("commentForm").comment.value){
		document.getElementById("commentForm").comment.focus();
		showCaution("Моля, въведете коментар.")
		return false;
	} else clearCaution();
	
	
	var xmlhttp;
	if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
	if(window.ActiveXObject) xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	if(xmlhttp){
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4){
				if (xmlhttp.status==200){
					clearCaution();
					if(xmlhttp.responseText=="1"){
						$("#messageSent").show("slow");
						window.setTimeout('$("#messageSent").hide("slow");',5000);
						document.getElementById("commentForm").reset();
						document.getElementById("antispamImg").src="hristof/antispam/index/"+Math.floor(Math.random()*99000)+"/"+Math.floor(Math.random()*99000)
					}
					if(xmlhttp.responseText=="2"){
						showCaution("Грешен антиспам код, опитайте пак.")
						document.getElementById("commentForm").antispam.focus();
					}
				}
				else{
					showCaution("Съжалявам, коментара не може да бъде изпратен. По-късно може и да стане.");			
				}
			}
		}					
	xmlhttp.open("POST","http://hristof.com/wp-comments-post.php",true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send("author="+encodeURIComponent(document.getElementById("commentForm").author.value)+"&email="+encodeURIComponent(document.getElementById("commentForm").email.value)+"&url="+encodeURIComponent(document.getElementById("commentForm").url.value)+"&comment="+encodeURIComponent(document.getElementById("commentForm").comment.value)+"&antispam="+encodeURIComponent(document.getElementById("commentForm").antispam.value)+"&comment_post_ID="+encodeURIComponent(document.getElementById("commentForm").comment_post_ID.value)+"&ajax=1");
	}
	return false;
}

function showCaution(message)
{
	document.getElementById('caution').innerHTML=message;
	document.getElementById('caution').style.display="block";
}

function clearCaution(){document.getElementById('caution').style.display="none";}