// *** Post Comment ***

function postComment(num) {

	namelength=document.getElementById("comment_name").value.length;
	commentlength=document.getElementById("comment_text").value.length;
	apos=document.getElementById("comment_name").value.indexOf("<");
	bpos=document.getElementById("comment_text").value.indexOf("<");

	if (namelength<1) {

		alert("Enter a name.");

	} else if (commentlength<1) {

		alert("Enter a comment.");

	} else if (apos!=-1 || bpos!=-1) {

		alert('Left angle brackets "<" are not allowed.');

	} else {

		commentName = escape(document.getElementById("comment_name").value);
		commentText = escape(document.getElementById("comment_text").value);
		commentIP = document.getElementById("comment_ip").value;
		document.getElementById("post_comment_btn").innerHTML="Posting Comment...";
		postCommentRequest(commentName,commentText,commentIP);
		//document.getElementById("comments_showing").innerHTML="21";
		//document.getElementById("total_comments").innerHTML=Math.floor(num/1000) + "," + (Math.floor(num/100)-(Math.floor(num/1000)*10)) + (Math.floor(num/10)-(Math.floor(num/100)*10)) + (Math.floor(num)-(Math.floor(num/10)*10));
		
	}

}

function getXMLHttpRequestObject(){
	var xmlobj;
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            xmlobj=null;
            return false;
        }
    }
	return xmlobj;
}

var commentXMLHttpObj=getXMLHttpRequestObject();

function postCommentRequest(commentName,commentText,commentIP){
	commentXMLHttpObj.open('POST','http://www.beach-fitness.com/tabata/postcomment.php',true);
	commentXMLHttpObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
   	commentXMLHttpObj.send('commentName='+commentName+'&commentText='+commentText+'&commentIP='+commentIP);
	commentXMLHttpObj.onreadystatechange=postCommentStatusChecker;
}

function postCommentStatusChecker(){
    if(commentXMLHttpObj.readyState==4){
        if(commentXMLHttpObj.status==200){
			document.getElementById("comment_form").innerHTML=content=commentXMLHttpObj.responseText;
        }
        else{
            alert('Failed to get response :'+commentXMLHttpObj.statusText);
			document.getElementById("comment_form").innerHTML="Unable to post comment. Sorry for this technical error. Please email us at beats@jeejuh.com. Thank you.";
        }
    }
}