var AskAnExpert = {

	blankComment : function() {
		if ($('comment_comment').value == '') {
			alert("Please enter a comment");
			return true;
		} else {
			return false;
		}
	},
	
	setConfirm : function() {
		var a = $('moderate_action').value;
		var text = a + ' selected comments?';
		return (a == '' ? false : confirm(text));
	},

	// Set default value for blank elements. 
	// Used by initSiteSearch()
	toggleFocus: function(element, val){
		var obj = $(element);
		if (obj.hasClassName('focus')) {
			obj.removeClassName('focus');
			if (obj.value.blank()) 
				obj.value = val;
		}
		else {
			obj.addClassName('focus');
			if (obj.value == val) 
				obj.value = '';
		}
	},

	addPrompt : function(id, text)
	{
		Event.observe(id, 'focus', function(){
			AskAnExpert.toggleFocus(this, text);
		});
		Event.observe(id, 'blur', function(){
			AskAnExpert.toggleFocus(this, text);
		});
		if ($F(id).blank()) 
			$(id).value = text;
	},

	escapeHTMLChars : function(text)
	{
		return text.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
	},
	
	setName : function()
	{
		// 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(AskAnExpert.escapeHTMLChars(name0));
		$('anonymity_1_label').update(AskAnExpert.escapeHTMLChars(name1));
		$('anonymity_2_label').update(AskAnExpert.escapeHTMLChars(name2));
	},
	
	showCommentForm : function(form, link)
	{
		$(form).show();
		$(link).hide();
	},

	clickTag : function(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;
		}
	}

}

