

function editPost(id) {

	sendRequest(id, "action=get&id=" + id + "&utf=0", "get")
	if (document.getElementById("info" + id).innerHTML != '')
		lang[2] = document.getElementById("info" + id).innerHTML;
	document.getElementById("info" + id).style.display = "inline";
}

function onReadyGet(id) {
	document.getElementById("info" + id).style.display = "none";

	res = http.responseText.split("<!-- -->");
	lang[0] = res[0]; // Update
	lang[1] = res[1]; // Cancel
	lang[3] = res[2]; // Saving
	html = res[3];
	bbcode = res[4];

	lastedit = res[5];
	if (typeof lastedit == 'undefined')
		lastedit = '';

	postwidth = document.getElementById('postmsg' + id).offsetWidth;
	postheight = document.getElementById('postmsg' + id).offsetHeight;


	posteditwidth = postwidth - 10; //10 = padding-left + padding-right of postmsg
	posteditheight = postheight - 30;

	if (posteditheight < 200) //min postedit height
		posteditheight = 200;

	if (posteditheight > 400) //max postedit height
		posteditheight = 400;

	document.getElementById("postmsg" + id).innerHTML = "<textarea id='postedit' style='width:" + posteditwidth + "px; height: " + posteditheight + "px'>" + html + "</textarea><input type=button onclick='updatePost(" + id + ")' value='" + lang[0] + "' id='updatePost'> <input type=button onclick='cancelEdit(" + id + ")' value='" + lang[1] + "' id='cancelUpdate'>";


	tempmsg = bbcode;
}

function updatePost(id) {
	document.getElementById("postedit").disabled = true;
	document.getElementById("updatePost").disabled = true;
	document.getElementById("cancelUpdate").disabled = true;

	sendRequest(id, "action=update&id=" + id + "&message=" + encodeURIComponent(document.getElementById("postedit").value) + "&utf=0", "upd")
	document.getElementById("info" + id).style.display = "inline";
	document.getElementById("info" + id).innerHTML = lang[3];
}

function onReadyUpdate(id) {
	document.getElementById("postedit").disabled = false;
	document.getElementById("updatePost").disabled = false;
	document.getElementById("cancelUpdate").disabled = false;

	document.getElementById("info" + id).style.display = "none";
	document.getElementById("info" + id).innerHTML = lang[2];
	document.getElementById("postmsg" + id).innerHTML = http.responseText;
}

function cancelEdit(id) {
	msg = tempmsg;
	if (lastedit != '')
		msg += lastedit;

	document.getElementById("postmsg" + id).innerHTML = msg;
}


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

function sendRequest(id, data, act) {
	http.onreadystatechange = function() { onReadyFunction(id, act); };

	http.open("POST", "include/ajax_post_edit/edit.php", true);
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");

	http.send(data);
}

function onReadyFunction(id, act) {
	if (http.readyState == 4) {
		if (act == "get") {
			onReadyGet(id);
		} else if (act == "upd") {
			onReadyUpdate(id);
		}
	}
}

function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}

var http = getHTTPObject(); // We create the HTTP Object
var tempmsg;
var lastedit = '';
var lang = new Array('Update', 'Cancel', 'Loading...', 'Saving...');