/*
	ClientSide Handling for Document Comments Module
	Version: 1.0
*/

function PostComment(formId) {

	var commentTxtArea = document.forms['DocumentCommentsForm_' + formId].DocumentComment.value
	
	if ( commentTxtArea != '(Din kommentar)' && commentTxtArea != "") {
		document.forms['DocumentCommentsForm_' + formId]['Action'].value = "Post";
		document.forms['DocumentCommentsForm_' + formId].submit();
	}
}

function Approve(commentId, formId) {
	document.forms['DocumentCommentsForm_' + formId]['Action'].value = "Accept";
	document.forms['DocumentCommentsForm_' + formId]['CommentId'].value = commentId;
	document.forms['DocumentCommentsForm_' + formId].submit();
}

function Reject(commentId, formId) {
	document.forms['DocumentCommentsForm_' + formId]['Action'].value = "Reject";
	document.forms['DocumentCommentsForm_' + formId]['CommentId'].value = commentId;
	document.forms['DocumentCommentsForm_' + formId].submit();
}

// misc variables for the document comments moderation
var editField = null;
var originalComment = null;
var originalControl = null;
var editCommentId = -1;
var editFormId = -1;

function Edit(commentId, formId) {
	var comment = null;
	var control = null;
	
	if (editCommentId != -1) {
		// restore original comment and controls
		RestoreCommentAndControl();
	}
	editCommentId = commentId;
	editFormId = formId;
	
	comment = GetComment();
	
	// create and append textarea for editing
	originalComment = comment.innerHTML;
	editField = document.createElement("textarea");
	editField.value = SanitizeComment(originalComment);
	editField.className = "DocumentCommentsModerate";
	comment.innerHTML = "";
	comment.appendChild(editField);
	
	// remove current controls
	control = GetControl();
	originalControl = control.innerHTML;
	control.innerHTML = "";
	
	// append OK button
	var ok = document.createElement("button");
	ok.appendChild(document.createTextNode("OK"));
	ok.onclick = EditOk;
	ok.className = "DocumentCommentsModerate OK";
	control.appendChild(ok);
	
	// append Cancel button
	var cancel = document.createElement("button");
	cancel.appendChild(document.createTextNode("Cancel"));
	cancel.onclick = EditCancel;
	cancel.className = "DocumentCommentsModerate Cancel";
	control.appendChild(cancel);
}

function EditOk() {
	if (editCommentId == -1 || editFormId == -1 || editField == null) {
		return;
	}
	document.forms['DocumentCommentsForm_' + editFormId]['DocumentComment'].value = editField.value;
	document.forms['DocumentCommentsForm_' + editFormId]['Action'].value = "Edit";
	document.forms['DocumentCommentsForm_' + editFormId]['CommentId'].value = editCommentId;
	document.forms['DocumentCommentsForm_' + editFormId].submit();
}

function EditCancel() {
	RestoreCommentAndControl();
}

function RestoreCommentAndControl() {
	if (editCommentId == -1 || editFormId == -1) {
		return;
	}
	// restore original comment and controls
	comment = GetComment();
	comment.innerHTML = originalComment;			
	control = GetControl();
	control.innerHTML = originalControl;
	
	editCommentId = -1;
	editFormId = -1;
}

function GetComment() {
	return document.getElementById("Comment" + editCommentId + "_" + editFormId);
}

function GetControl() {
	return document.getElementById("Control" + editCommentId + "_" + editFormId);
}

function SanitizeComment(data) {
	return data.replace(/<br\s*\/?>/gi, '\n');
}

