var comments_enableDebug = false;
var comments_xmlHttp = null;
var comments_currentEditor = null;

function comments_getXmlHttp() {
	if(!comments_xmlHttp) comments_xmlHttp = createXMLHttpRequest();
	comments_xmlHttp.open("POST", "index.php");
	comments_xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	comments_xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	comments_xmlHttp.onreadystatechange=comments_readyState;

	return comments_xmlHttp;
}

function comments_readyState() {
	if(!comments_xmlHttp) return false;

	var index;

	debugPrint("readystate "+comments_xmlHttp.readyState, "comments");
	if(comments_xmlHttp.readyState == 4) {
//		debugPrint("status: "+comments_xmlHttp.status, "comments");
//		if(comments_xmlHttp.status == 200) {
			comments_removeEditor();

		if(comments_xmlHttp.responseText) {
			// Tulos tulee JSON muodossa (Eli vedetään evalin läpi niin saadaan javascript -objekti)
			// yritetään evaluoida JSON
			try {
				var response = eval('(' + comments_xmlHttp.responseText + ')');
			} catch(err) {
				var sHeaders = comments_xmlHttp.getAllResponseHeaders();
				err.headers = sHeaders;
				showProps(err);
				return;
			}

			if(response.errors) {
				var msg = response.errors.join("\n");
				if(msg) alert(msg);
			}

			if(response.replacements) {
				for(index in response.replacements) {
					var el = document.getElementById(index);
					if(el) el.innerHTML = response.replacements[index];
				}
			}
			if(response.messages) {
				var msg = response.messages.join("\n");
				if(msg) alert(msg);
			}
		}
//		}
	}
}

function comments_send(form) {
//	if(!comments_xmlHttp) comments_xmlHttp = comments_createXMLHttpRequest();
//	comments_abortXMLHttpRequest(comments_xmlHttp);

	var xmlhttp = comments_getXmlHttp();

	var postData = "action=comments&comments-action=add";

	var el = null;

	el = document.getElementById("comments-hash");
	postData += "&comments-hash="+el.value;

	el = document.getElementById("comments-order");
	postData += "&comments-order="+el.value;

	el = document.getElementById("comments-page");
	postData += "&comments-page="+el.value;

	el = document.getElementById("comments-name");
	postData += "&comments-name="+el.value;
	el.value = "";

	el = document.getElementById("comments-comment");

	var trimmed = el.value.replace(/^\s+|\s+$/g, '') ;
//	if(!trimmed) return false;
	postData += "&comments-comment="+trimmed;
	el.value = "";

	debugPrint("postData "+postData, "comments");

	xmlhttp.send(postData);
	return false;
}

function comments_makePostData(action) {
	var postData = "action=comments&comments-action="+action;
	var el = null;
	el = document.getElementById("comments-hash");
	postData += "&comments-hash="+el.value;
	el = document.getElementById("comments-order");
	postData += "&comments-order="+el.value;
	el = document.getElementById("comments-page");
	postData += "&comments-page="+el.value;
	return postData;
}

function comments_deleteAll() {
	if(!confirm("Haluatko varmasti poistaa KAIKKI kommentit?")) return false;
	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("deleteAll");
	xmlhttp.send(postData);
	return false;
}

function comments_delete(id) {
	if(!confirm("Haluatko varmasti poistaa kommentin?")) return false;

	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("delete");
	postData += "&comments-id="+id;
	xmlhttp.send(postData);
	return false;
}

function comments_acceptAll() {
	if(!confirm("Haluatko varmasti hyväksiä KAIKKI kommentit?")) return false;

	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("acceptAll");
	xmlhttp.send(postData);
	return false;
}

function comments_accept(id) {
	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("accept");
	postData += "&comments-id="+id;
	xmlhttp.send(postData);
	return false;
}

function comments_modify(id) {
	var current = document.getElementById("currentComment_"+id);
	comments_currentEditor = comments_elementEditor(current);
	if(comments_currentEditor) {
		comments_currentEditor.id = id;
		comments_currentEditor.textArea.onblur = comments_comment_save;
//		comments_currentEditor.textArea.disabled = false;
		comments_currentEditor.textArea.focus();
	}
	return false;
}


function comments_response(id) {
	var current = document.getElementById("currentResponse_"+id);
	comments_currentEditor = comments_elementEditor(current);
	if(comments_currentEditor) {
		comments_currentEditor.id = id;
		comments_currentEditor.textArea.onblur = comments_response_save;
		comments_currentEditor.textArea.focus();
	}
	return false;
}

function comments_removeEditor() {
	if(comments_currentEditor) {
		// remove old editor
//		showProps(comments_currentEditor);
		comments_currentEditor.textArea.onblur = null;
		comments_currentEditor.textArea.onkeyup = null;
		comments_currentEditor.textArea.onkeydown = null;
		comments_currentEditor.textArea.onkeypress = null;
		comments_currentEditor.owner.parentNode.removeChild(comments_currentEditor.textArea);
		comments_currentEditor.owner.style.display = "";
		comments_currentEditor.textArea = null;
		comments_currentEditor.owner = null;
		comments_currentEditor = null;
	}
}

function comments_editorIsModified() {
	if(!comments_currentEditor) return false;
	if(comments_currentEditor.textArea.value == comments_currentEditor.owner.innerHTML) {
		return false;		
	} else {
		return true;
	}
}

function comments_elementEditor(el) {
	comments_removeEditor();

	var ret = null;

	if(el) {
		var textArea = document.createElement("textarea");

		var width = Math.floor(el.offsetWidth);
		textArea.style.width = width + "px";

		var height = Math.floor(el.offsetHeight);
		height = height + 2;

		textArea.setAttribute("id", "comments-editor");

		if(el.innerHTML) {
			var txt = el.innerHTML;
			if(txt) {
				txt = txt.replace(/<br>/gi, "\n");
				textArea.value = txt;
			}
		} else {
			height = 28;
		}

		textArea.style.height = height+"px";

		var className = el.className;

		textArea.setAttribute("className", className);
		textArea.setAttribute("class", className);

		textArea.style.overflow = "visible";
		if(!IE) textArea.style.overflow = "hidden";

		textArea.style.borderWidth = "1px";
		textArea.style.borderStyle = "dotted";
		textArea.style.borderColor = "#000000";

		textArea.onkeyup = comments_editor_checkSize;
		textArea.onkeydown = comments_editor_checkESC;
		textArea.onkeypress = comments_editor_checkSize;

		el.style.display = "none";
		el.parentNode.insertBefore(textArea, el.nextSibling);
		el.parentNode.disabled =false;
		ret = new Object();
		ret.textArea = textArea;
		ret.owner = el;
	}

	return ret;
}

function comments_editor_checkESC(e) {
	if(!e) e = window.event;

	if(e.keyCode == 27) {
		comments_removeEditor();
		return false;
	}

	if(this.clientHeight != this.scrollHeight) {
		this.style.height = (Math.floor(this.scrollHeight) + 2) +"px";
	}
}


function comments_editor_checkSize(who) {
	if(this.clientHeight != this.scrollHeight) {
		this.style.height = (Math.floor(this.scrollHeight) + 2) +"px";
	}
}


function comments_response_save() {
	if(!comments_currentEditor) return false;

	if(!comments_editorIsModified()) {
		comments_removeEditor();
		return false;		
	}

	var id = comments_currentEditor.id;

//	if(!comments_xmlHttp) comments_xmlHttp = comments_createXMLHttpRequest();
//	comments_abortXMLHttpRequest(comments_xmlHttp);

	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("response");
	postData += "&comments-id="+id;

	el = comments_currentEditor.textArea;
	var trimmed = el.value.replace(/^\s+|\s+$/g, '') ;
//	if(!trimmed) return false;
	postData += "&comments-response="+trimmed;
	el.value = "";

	debugPrint("postData "+postData, "comments");
//	el = document.getElementById("response_"+id);
//	el.style.display = "none";
	xmlhttp.send(postData);
	return false;
}

function comments_comment_save() {
	if(!comments_currentEditor) return false;

	if(!comments_editorIsModified()) {
		comments_removeEditor();
		return false;		
	}

	var id = comments_currentEditor.id;

	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("modify");
	postData += "&comments-id="+id;

	el = comments_currentEditor.textArea;
	var trimmed = el.value.replace(/^\s+|\s+$/g, '') ;
//	if(!trimmed) return false;
	postData += "&comments-comment="+trimmed;
	el.value = "";

	debugPrint("postData "+postData, "comments");

	xmlhttp.send(postData);
	return false;
}

function comments_reverse() {
	el = document.getElementById("comments-order");
	if(el.value == "DESC") {
		el.value = "ASC";
	} else {
		el.value = "DESC";
	}
	comments_reload();
	return false;
}

function comments_setOrder(order) {
	el = document.getElementById("comments-order");
	el.value = order;
	comments_reload();
	return false;
}

function comments_gotoPage(pageNum) {
	// ajax-lataus on tässä turha, sotkee vain back-napin yms., joten ladataan normaalisti linkin kautta
	return true;

	el = document.getElementById("comments-page");
	el.value = pageNum;
	comments_reload();
	return false;
}

function comments_reload() {
	var xmlhttp = comments_getXmlHttp();
	var postData = comments_makePostData("reload");
	xmlhttp.send(postData);
	return false;
}

function comments_showAddCommentForm() {
	var el = document.getElementById("commentForm");
	if(el) {
		el.style.display = "";
		el = document.getElementById("addCommentLink");
		el.style.display = "none";
	}
	return false;
}

