function ha() {
	var a = window.location.hash;
	if (a.indexOf('#answer') != -1) {
		var id = a.slice(1, a.length);
		new Effect.Highlight(id);
	}
}

function validateAnswer() {
	if ($('answer').value.strip().length == 0) {
		alert('Answer is required');
		return false;
	}
	if ($('answer').value.strip().length > 3000) {
		alert('Answer cannot exceed 3000 characters (currently ' + $('answer').value.strip().length + ')');
		return false;
	}
	else {
		return true;
	}
}

var curAnswer = 0;
function submitComment(id) {
	if ($('comment_' + id).value.strip().length == 0)
		alert('Comment is required');
	else if ($('comment_' + id).value.strip().length > 500)
		alert('Comment cannot exceed 500 characters (currently ' + $('comment_' + id).value.strip().length + ')');
	else {
		curAnswer = id;
		Element.toggle('commentthrobber_' + id);
		var formvars = Form.serialize('commentform_' + id);
		new Ajax.Request(spath+'?fuseaction=ajax.addComment', {method: 'post', postBody: formvars, onComplete: submitCommentFinish});
	}
	return false;
}

function submitCommentFinish(r) {
	Element.toggle('commentthrobber_' + curAnswer);
	var json = ajaxError(r);
    if (json != '') {
		Element.hide('commentbox_' + json.answerid);
		$('comment_' + json.answerid).value = '';
		var element = Builder.node('li', {id:json.domid});
		$('commentlist_' + json.answerid).appendChild(element);
		$(json.domid).innerHTML = json.commenttext;
		Element.show('commentswrapper_' + json.answerid);
		new Effect.Highlight('commentswrapper_' + json.answerid, {duration: 2.0, restoreColor: '#ffffe0'});
		if (json.updatewatchlist == 1) {
			$('watchlistresult').innerHTML = 'This question is on your <a href="account/watch/">Watch List</a>';
		}
	}
}

var curRateID = 0;
function rateAnswer(answerid, rating) {	
	if (rating != '') {
		curRateID = answerid;
		Element.show('rateit_' + answerid + '_throbber');
		new Ajax.Request(spath+'?fuseaction=ajax.rateAnswer', {method: 'get', parameters: '&id='+answerid+'&rating='+rating, onComplete: rateAnswerFinish});
	}
}
function rateAnswerFinish(r) {
	Element.hide('rateit_' + curRateID + '_throbber');
	var json = ajaxError(r);
    if (json.error == 0) {
			$('ratingcount_' + json.id).innerHTML = json.ratings;
			$('ratinggrade_' + json.id).innerHTML = json.ratingimg;
		}
		$('rateit_' + json.id).innerHTML = json.rateresult;
		new Effect.Highlight('rateit_' + json.id, {duration: 2.0});
}

function rateQuestion(qid, rating) {	
	if (rating != '') {
		Element.show('rateq_throbber');
		new Ajax.Request(spath+'?fuseaction=ajax.rateQuestion', {method: 'get', parameters: '&id='+qid+'&rating='+rating, onComplete: rateQuestionFinish});
	}
}
function rateQuestionFinish(r) {
	Element.hide('rateq_throbber');
	var json = ajaxError(r);
    if (json.error == 0) {
			$('ratingQcount').innerHTML = json.ratings;
			$('ratingQgrade').innerHTML = json.ratingimg;
		}
		$('rateq').innerHTML = json.rateresult;
		new Effect.Highlight('rateq', {duration: 2.0});
}


function addToWatchList(id) {
	Element.show('watchlist_throbber');
	var notify = 0;
	if ($('qnotify').checked) notify = 1;
	new Ajax.Request(spath+'?fuseaction=ajax.addWatch', {method: 'get', parameters: 'id='+id+'&notify='+notify, onComplete: addToWatchListFinish});
}
function addToWatchListFinish(r) {
	Element.hide('watchlist_throbber');
	var json = ajaxError(r);
    if (json != '') {
		Element.hide('watchlist');
		$('watchlistresult').innerHTML = 'This question has been added to your <a href="account/watch/">Watch List</a>.';
		new Effect.Highlight('watchlistresult', {duration: 2.0});			
	}
}

function submitQComment(id) {
	if ($('comment_question').value.strip().length == 0)
		alert('Comment is required');
	else if ($('comment_question').value.strip().length > 500)
		alert('Comment cannot exceed 500 characters (currently ' + $('comment_question').value.strip().length + ')');
	else {
		Element.toggle('commentthrobber_question');
		var formvars = Form.serialize('commentform_question');
		new Ajax.Request(spath+'?fuseaction=ajax.addComment', {method: 'post', postBody: formvars, onComplete: submitQCommentFinish});
	}
	return false;
}

function submitQCommentFinish(r) {
	Element.toggle('commentthrobber_question');
	var json = ajaxError(r);
    if (json != '') {
		Element.hide('commentbox_question');
		$('comment_question').value = '';
		var element = Builder.node('li', {id:json.domid});
		$('qcommentlist').appendChild(element);
		$(json.domid).innerHTML = json.commenttext;
		Element.show('commentswrapper_question');
		new Effect.Highlight('commentswrapper_question', {duration: 2.0});
		if (json.updatewatchlist == 1) {
			$('watchlistresult').innerHTML = 'This question is on your <a href="account/watch/">Watch List</a>';
		}
	}
}

function showAnswerBox() {
	//Element.hide('qflagoptions');
	Element.hide('watchlist');
	Element.hide('commentbox_question');
	Element.hide('rateq');
	Element.show('answerbox');
	$('answer').focus();
}
function showQuestionCommentBox() {
	Element.hide('answerbox');
	Element.hide('watchlist');
	Element.hide('rateq');
	Element.show('commentbox_question');
	$('comment_question').focus();
}
function showWatchListBox() {
	//Element.hide('qflagoptions');	
	Element.hide('answerbox');
	Element.hide('commentbox_question');
	Element.hide('rateq');
	Element.show('watchlist');
}
function showQuestionFlagBox() {	
	Element.hide('watchlist');
	Element.hide('answerbox');
	Element.hide('commentbox_question');
	//Element.show('qflagoptions');
}
function showCommentBox(answerid) {
	Element.hide('rateit_' + answerid);
	//Element.hide('aflagoptions_' + answerid);
	Element.show('commentbox_' + answerid);
	$('comment_' + answerid).focus();
}
function showAnswerFlagBox(answerid) {	
	Element.hide('rateit_' + answerid);	
	Element.hide('commentbox_' + answerid);
	//Element.show('aflagoptions_' + answerid);
}
function showAnswerRateBox(answerid) {	
	//Element.hide('aflagoptions_' + answerid);
	Element.hide('commentbox_' + answerid);
	Element.show('rateit_' + answerid);
}
function showQuestionRateBox() {	
	Element.hide('commentbox_question');
	Element.hide('watchlist');
	Element.hide('answerbox');
	Element.show('rateq');
}

function loadURL(url) {
	window.location.href = url;
}
