/**
 *
 * Javascript for editing forms.
 *
 */

/**
 *
 * Adds a comment to the page
 *
 */
function add_user_comment(form,empty_message) {
	if (form.user_comment.value != "") {
		form.operation.value = "add_user_comment";
		form.submit();
	}
	else
		alert(empty_message);
}

/**
 *
 * Deletes a comment
 *
 */
function delete_user_comment(form,ID) {
	var check = confirm("Do you really want to delete this comment?");
	if (check) {
		form.operation.value = "delete_user_comment";
		form.comment_ID.value = ID;
		form.submit();
	}
}

/**
 *
 * Enables a comment to be editable
 *
 */
function edit_user_comment(ID) {
	// Show textarea and hide text
	var commentText = document.getElementById("comment_text_"+ID);
	var commentTextarea = document.getElementById("comment_textarea_"+ID);
	
	commentText.className = "hidden";
	commentTextarea.className = "";
}

/**
 *
 * Function that checks if email is valid.
 *
 */
function is_email(email) {
	var parts = email.split("@");
	
	if (parts.length != 2)
		return false;
		
	if (parts[0] == "" || parts[1] == "")
		return false;
		
	return true;
}

/**
 *
 * Saves a comment via Ajax.
 *
 */
function save_user_comment(table,ID) {
	var commentText = document.getElementById("comment_text_"+ID);
	var commentTextarea = document.getElementById("comment_textarea_"+ID);
	var userComment = document.getElementById("user_comment_"+ID);
	
	var xmlhttp = create_xml_http("commenthandler","operation=save_comment&table="+table+"&ID="+ID+"&comment="+encodeURIComponent(userComment.value));
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var result = xmlhttp.responseText.split(";");
						
			if (result[0] == "false") {
				// Error occurred
				alert(result[1]);
			}
			else {
				// No error => Show Text
				commentText.innerHTML = decodeURIComponent(result[1]);
				
				// Show text and hide textarea	
				commentText.className = "";
				commentTextarea.className = "hidden";
			}
		}
	}
	xmlhttp.send("");
}
