function noenter( event ) {
	return !( event && event.keyCode == 13 );
}

function add_article_keyword( keyword, keyword_text_input_id, keyword_ul_id, keyword_label ) {
	keyword = smartQuote( keyword );
	var remove_onclick = 'document.getElementById( \'' + keyword_ul_id + '\').removeChild( document.getElementById( \'' + keyword + '\' ) ); return false;';
	$( keyword_ul_id ).appendChild( Builder.node( 'li', {id:keyword}, [ Builder.node( 'strong', keyword ), ' ', Builder.node( 'a', {href:'#',onclick:remove_onclick}, 'Remove' ), Builder.node( 'input', {name:keyword_label+'[]',type:'hidden',value:keyword} ) ] ) );
	document.getElementById( keyword_text_input_id ).value = '';
	return false;
}

function editComment(obj, user, comment) {
	Element.hide(obj);

	var textarea ='<div id="' + obj.id + '_editor"><textarea id="' + obj.id + '_edit" name="' + obj.id + '" rows="4" cols="60">' + obj.innerHTML + '</textarea>';
	var button = '<input id="' + obj.id + '_save" type="button" value="Save" /> or <input id="' + obj.id + '_cancel" type="button" value="Cancel" /></div>';

	new Insertion.After(obj, textarea+button);

	Event.observe(obj.id+'_save', 'click', function(){saveEdit(obj, user, comment)}, false);
	Event.observe(obj.id+'_cancel', 'click', function(){makeUneditable(obj)}, false);
}

function makeUneditable( obj ) {
	Element.remove(obj.id+'_editor');
	Element.show(obj);
}

function saveEdit( obj, user, comment ) {
	var new_content = escape($F(obj.id+'_edit'));

	obj.innerHTML = "Saving...";
	makeUneditable(obj);

	var success = function(t){editComplete(t, obj);}
	var failure = function(t){editFailed(t, obj);}

	var url = '/edit-comment.php';
	var pars = 'comment_id=' + comment + '&user_id=' + user + '&body=' + new_content;
	var ajax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function editComplete(t, obj){
	obj.innerHTML = t.responseText;
}

function editFailed(t, obj){
	obj.innerHTML = 'Sorry, the update failed.';
	makeUneditable(obj);
}

function deleteComment( comments_block, comment_block, user, comment ) {
	if (!confirm('Are you sure you want to delete this comment?')) {
		return false;
	}
	var url = '/delete-comment.php';
	var pars = 'comment_id=' + comment + '&user_id=' + user;
	var ajax = new Ajax.Request(url, {method:'post', postBody:pars});
	comments_block.removeChild( comment_block );
}

function personalizeComments() {
	var web_user_id = getUserdataByName( 'id' );
	var username = getUserdataByName( 'username' );
	if( web_user_id && username ) {
		var comments = document.getElementById( 'comments' ).childNodes;
		for( var i = 0; i < comments.length; i++ ) {
			var comment = comments[ i ];
			if( comment.id && comment.id.match( /comment_block_/ ) ) {
				var id = comment.id.split( '_' );
				var comment_id = id[ 2 ];
				var commenter = comment.firstChild.nextSibling.textContent.split( ' ' );
				var user_name = commenter[ 0 ];
				
				if( user_name == username ) {
					edit_delete_text = ' <a href="#" onclick="editComment($(\'comment_' + comment_id + '\'),' + web_user_id + ',' + comment_id + '); return false;">Edit</a> | <a href="#" onclick="deleteComment($(\'comments\'),$(\'comment_block_' + comment_id + '\'),' + web_user_id + ',' + comment_id + '); return false;">Delete</a>';
					comment.firstChild.nextSibling.nextSibling.nextSibling.innerHTML += edit_delete_text;
				}
			}
		}
	}
}

function smartQuote( txt ) {
	var quote = /'/g;
	txt.replace( quote, "\'" );
	return txt;
}

function getUserdataByName( name ) {
	var userdata = decodeURIComponent( readCookie( 'userdata' ) );
	var values = userdata.split( '|' );
	var nameEQ = name + '=';
	for( var i = 0; i < values.length; i++ ) {
		var pair = values[ i ].split( '=' );
		if( pair[ 0 ] == name ) {
			return pair[ 1 ];
		}
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCommentUsername() {
	var username = getUserdataByName( 'username' );
	if( !username ) {
		hideCommentForm();
	}
	else {
		document.getElementById( 'comment-username' ).setAttribute( 'value', username );
	}
}

function setCommentUserEmailAddress() {
	var userEmailAddress = getUserdataByName( 'email_address' );
	if( !userEmailAddress ) {
		hideCommentForm();
	}
	else {
		document.getElementById( 'comment-user-email-address' ).setAttribute( 'value', userEmailAddress );
	}
}

function hideCommentForm() {
	document.getElementById( 'comment-form' ).style.display = 'none';
}