// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function addPrompt(id, text)
{
	var obj = $(id);
	if (obj.value == '' || obj.value == text) {
	   obj.value = text;
	   obj.addClassName('prompt');
	   Event.observe(obj, 'click', removePrompt);
	}
}


function removePrompt(event)
{
	var elm = Event.element(event);
	elm.value='';
	elm.removeClassName('prompt')
	Event.stopObserving(elm, 'click', removePrompt);
}

function escapeHTMLChars(text)
{
	return text.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
}

function blankComment() {
	if ($('comment_comment').value == '') {
		alert("Please enter a comment");
	return true;
	} else {
		return false;
	}
}

function setName()
{
	// Get values
	var f = $('person_first_name').value;
	var l = $('person_last_name').value;

	if (f == '') { f = 'John'; l = 'Doe'; }

	var name0 = f;
	var name1 = f + (l == '' ? '' : ' ' + l.substring(0,1) + '.');
	var name2 = (l == '' ? f : f.substring(0,1) + '.' + l.substring(0,1) + '.');

	// Update labels
	$('anonymity_0_label').update(escapeHTMLChars(name0));
	$('anonymity_1_label').update(escapeHTMLChars(name1));
	$('anonymity_2_label').update(escapeHTMLChars(name2));
}

function showCommentForm(form, link)
{
	$(form).show();
	$(link).hide();
}

function clickTag(tag)
{
	// the input field
	var tag_list = $('question_tag_list');

	if (tag.hasClassName('selected')) {
		// Remove class
		tag.removeClassName('selected');
		// Remove keyword globally
		var pat = new RegExp(tag.innerHTML, 'gi');
		tags = tag_list.value.replace(pat, '');
		// Now rebuild the tag list
		tags = $A(tags.split(/\s*,\s*/));
		// Remove empty elements and duplicates
		tag_list.value = tags.uniq().without('').join(', ');
	} else {
		// Set class
		tag.addClassName('selected');
		// Add keyword to the end
		tag_list.value += ', ' + tag.innerHTML;
	}
}

function setConfirm() {
    var a = $('moderate_action').value;
    var text = a + ' selected comments?';
    return (a == '' ? false : confirm(text));
}
