function expandComments(commentsID, expanderID) {
	var theComments = document.getElementById(commentsID);
	var theExpander = document.getElementById(expanderID);
	if (theComments.style.display == "block") {
		theComments.style.display = "none";
		theExpander.innerHTML = "Show/Add Comments";
	} else {
		theComments.style.display = "block";
		theExpander.innerHTML = "Hide Comments";
	}
}

function addComment(id) {
	var name = escape(document.getElementById("newCommentName" + id).value);
	var email = escape(document.getElementById("newCommentEmail" + id).value);
	var url = escape(document.getElementById("newCommentWebsite" + id).value);
	var comment = escape(document.getElementById("newComment" + id).value);

	errString = validateNormalString(name, "name") + validateEmailString(email, "e-mail") + validateNormalString(comment, "comment");
	
	if (errString == "") {
		ajax("mysqlgateway.php", "method=addComment&name="+name+"&email="+email+"&url="+url+"&text="+comment+"&postID=" + id + "&uniq=" + Math.random(), refreshComments)		
		document.getElementById("comments" + id).innerHTML = "Loading...";
	} else {
		document.getElementById("newCommentError"+id).innerHTML = errString;
	}
}

function addPost() {
	var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');

	var title = document.getElementById("newPostTitle").value;
	var post = oEditor.GetXHTML(false);

    doJSONRPC("http://www.jakattack.net/rpc/server.php","addPost",[title,post],1,addPostCB);

	//ajax("../mysqlgateway.php", "method=addPost&title="+title+"&post="+ post + "&uniq=" + Math.random(), displayNewPostResults);
}

function addPostCB() {};

function addDraft() {
	var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');

	var title = escape(document.getElementById("newPostTitle").value);
	var post = escape(oEditor.GetXHTML(false));

	ajax("../mysqlgateway.php", "method=addDraft&title="+title+"&post="+ post + "&uniq=" + Math.random(), displayNewPostResults);
}

function editPost() {
	var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');

	var id = getQueryStringVariable("p");
	var title = escape(document.getElementById("newPostTitle").value);
	var post = escape(oEditor.GetXHTML(false));
	
	ajax("../mysqlgateway.php", "method=editPost&id="+id+"&title="+title+"&post="+ post + "&uniq=" + Math.random(), displayNewPostResults);	
}

function deletePost(id) {
	ajax("../mysqlgateway.php", "method=deletePost&id="+id+"&uniq=" + Math.random(), displayPostTable)
}

function displayNewPostResults(theText) {
	document.getElementById("content").innerHTML = theText;
}

function validateNormalString(theStringToValidate, fieldName) {
	var errString = "";
	
	if (theStringToValidate == "") {
		errString += "The \"" + fieldName + "\" field is empty. ";
	}
	
	return errString;
}

function validateEmailString(theStringToValidate, fieldName) {
	var errString = "";
	
	if (theStringToValidate == "") {
		errString += "The \"" + fieldName + "\" field is empty. ";
	} else if (!theStringToValidate.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
		errString += "The \"" + fieldName + "\" field is not a valid e-mail address. "; 
	}
	
	return errString;
}

function refreshComments(theText) {
	document.getElementById("comments" + theText.split("\x01")[0]).innerHTML = theText.split("\x01")[1];
}

function getQueryStringVariable(qVar) {
	var url = window.location.href;
	var pairArray = (url.split("?")[1]).split("&")
	
	var queryArray = new Array();
	for (var i=0; i < pairArray.length; i++) {
		queryArray[pairArray[i].split("=")[0]] = pairArray[i].split("=")[1];
	}
	
	return queryArray[qVar];
}

function ajax(url, vars, callbackFunction) {
	var request = window.XMLHttpRequest ?
		new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	request.setRequestHeader("Content-length", vars.length);
	request.setRequestHeader("Connection", "close");
 
	request.onreadystatechange = function() {
		if (request.readyState == 4 && request.status == 200) {
			if (request.responseText) {
				callbackFunction(request.responseText);
			}
        }
	};
	request.send(vars);
}

function doJSONRPC(server, method, params, id, cb) {
    var call = '{ "method": "' + method + '", "params": ' + JSONstring.make(params) + ', "id": ' + id + " }";

    var request = window.XMLHttpRequest ?
        new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
    request.open("POST", server, true);
    request.setRequestHeader("Content-Type","application/json");
    request.setRequestHeader("Content-length", call.length);
    request.setRequestHeader("Connection", "close");

    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            if (request.responseText) {
                alert (request.responseText);
            }
        }
    };

    request.send(call);
}
