var xmlHttp;
var global_callback;
var global_prefix;
var global_altid;
var global_postid;

//**************************************************************************************************************

function submitForm(url,callback) {
	var poststr = $("form").serialize();
	makePOSTRequest(url, poststr, callback);
}

//inserts whatever's returned from a url to the inner text of a tag
function insertURL(url, tagId) {
	makeGETRequest(url, "" , GETReturn_Insert(tagId));
}
//builds a function to be used for xmlHttp.onreadystatechange
// uses closure to remember a tag to insert the HTML into
function GETReturn_Insert(tagId) {
	function toReturn() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			document.getElementById(tagId).innerHTML=xmlHttp.responseText;
		} 
	}
	return toReturn;
}

function appendURL(url, tagId) {
	makeGETRequest(url, "", GETReturn_Append(tagId));
}
function GETReturn_Append(tagId) {
	function toReturn() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			document.getElementById(tagId).innerHTML=document.getElementById(tagId).innerHTML + xmlHttp.responseText;
		}
	}
	return toReturn;
}
//**************************************************************************************************************
function refresh() {
	location.reload(true);
}

function logout() {
	poststr="";
	makePOSTRequest('sub_logout.php', poststr, POSTReturn_Logout);
}

function makeCaptcha(inDiv) {
    //for nxserve.net 6Lc71AQAAAAAANYFpBC0_onPE2OOpjRNmTrnwEjO
    //for x10hosting.com 6Ld8HgUAAAAAAPXcA3vblix4oc7TQP_pXyqc5pzo
    //for absolutemaximumratings.com 6Le5JgUAAAAAAOhRml0mQXVblDEgqM6Ims-vyR-z
	Recaptcha.create("6Le5JgUAAAAAAOhRml0mQXVblDEgqM6Ims-vyR-z",
	inDiv, {
	theme: "blackglass"
	//callback: Recaptcha.focus_response_field
	});
}

//**************************************************************************************************************

function deletePost(delPostID) {
    var poststr = "postid=" + delPostID;
    makePOSTRequest("del_post.php", poststr, POSTReturn_Refresh);
}


//**************************************************************************************************************

function deleteComment(delCommentID) {
	var poststr = "commentid=" + delCommentID;
	makePOSTRequest("/del_comment.php", poststr, POSTReturn_Refresh);
	
}

function postComment(pre) {
	var poststr = "challenge=" + Recaptcha.get_challenge()
		+ "&response=" + Recaptcha.get_response()
		+ "&postedby=" + encodeURI(document.getElementById(pre+"_postedby").value)
		+ "&comment=" + encodeURI(document.getElementById(pre+"_comment").value)
		+ "&postid=" + document.getElementById(pre+"_postid").value
		+ "&altid=" + document.getElementById(pre+"_altid").value;
		
	makePOSTRequest('/sub_comment.php', poststr, POSTReturn_AddComment);
}

function commentsLabelAltClick(altid, prefix) {	
	global_prefix=prefix;
	if (document.getElementById(prefix+"_label").innerHTML=="hide comments")
	{
		hideComments(prefix);
		document.getElementById(prefix+"_label").innerHTML="show comments";
	}
	else
	{
		showCommentsAlt(altid, prefix);
		document.getElementById(prefix+"_label").innerHTML="hide comments";
	}
} 



function commentsLabelClick(postid, prefix) {
	global_postid=postid;
	global_altid="";
	
	if (document.getElementById(prefix+"_label").innerHTML=="hide comments") {
		hideComments(prefix);
		document.getElementById(prefix+"_label").innerHTML="show comments";
	} else {
		showComments(postid, prefix);

		document.getElementById(prefix+"_label").innerHTML="hide comments";
	}
} 

function hideComments(prefix) {
	document.getElementById(prefix+"_span").innerHTML="";
}


//inserts whatever's returned from a url to the inner text of a tag
function insertURL(url, tagId) {
	makeGETRequest(url, "" , GETReturn_Insert(tagId));
}
//builds a function to be used for xmlHttp.onreadystatechange
// uses closure to remember a tag to insert the HTML into
function GETReturn_Insert(tagId) {
	function toReturn() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			document.getElementById(tagId).innerHTML=xmlHttp.responseText;
		} 
	}
	return toReturn;
}

function appendURL(url, tagId) {
	makeGETRequest(url, "", GETReturn_Append(tagId));
}
function GETReturn_Append(tagId) {
	function toReturn() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			document.getElementById(tagId).innerHTML=document.getElementById(tagId).innerHTML + xmlHttp.responseText;
		}
	}
	return toReturn;
}

function showComments(postid, prefix) {
	global_postid=postid;
	global_altid="";
	global_prefix=prefix;
	if (postid==0) 	{ 
		document.getElementById(prefix+"_span").innerHTML="";
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) 	{
	  	alert ("Browser does not support HTTP Request");
		return;
	} 
	var url="getcomments.php";
	url=url+"?postid="+postid;
	url=url+"&pre="+prefix;
	//url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=GETReturn_Comments;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showCommentsAlt(altid, prefix) {
	global_altid=altid;
	global_postid="";
	global_prefix=prefix;
	if (altid.length==0) { 
		document.getElementById(prefix+"_span").innerHTML="";
		return;
	}
	
	var params = "altid=" + altid + "&pre=" + prefix;
	
	makeGETRequest("/getcomments.php", params, GETReturn_Comments);
}


//**************************************************************************************************************

function makeGETRequest(url, params, callback) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  	alert ("Browser does not support HTTP Request");
		return;
	} 
	
	if (params!="") url += "?" + params;
	xmlHttp.onreadystatechange = callback;
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
	
}

function makePOSTRequest(url, params, callback) {
	xmlHttp = GetXmlHttpObject();
	
	xmlHttp.onreadystatechange = callback;
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params)
}

//**************************************************************************************************************

function GETReturn_Comments() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById(global_prefix+"_span").innerHTML=xmlHttp.responseText;

		makeCaptcha(global_prefix+"_captcha");
	} 
}


function POSTReturn_Refresh() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		refresh();
	}
}

function POSTReturn_AddComment() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		
		response=xmlHttp.responseText;
		if(response=="Comment added") {
			if(global_postid=="") {
				//was an altid
				showCommentsAlt(global_altid, global_prefix);
			} else {
				//was a postid
				showComments(global_postid, global_prefix);
			}
			document.getElementById(global_prefix+"_result").innerHTML="Comment added";
		}
		else if(response=="incorrect-captcha-sol") {
			document.getElementById(global_prefix+"_result").innerHTML="Incorrect captcha guess; try again.";
			Recaptcha.reload();
			Recaptcha.focus_response_field();
		} else {
			document.getElementById(global_prefix+"_result").innerHTML=response;
		}
	}
}

function POSTReturn_Logout() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		response=xmlHttp.responseText;
		document.getElementById("div_welcome").innerHTML=response;
	}
}

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;
}