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(answerid) {
	if ($('comment_' + answerid).value.strip().length == 0) {
		alert('Comment is required');
	}
	else if ($('comment_' + answerid).value.strip().length > 500) {
		alert('Comment cannot exceed 500 characters (currently ' + $('comment_' + answerid).value.strip().length + ')');
	}
	else {
		curAnswer = answerid;
		Element.toggle('commentthrobber_' + answerid);
		var formvars = Form.serialize('commentform_' + answerid);
		new Ajax.Request('/questions/display/comments/do.html?a=add', {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 == 'Y') {
			$('watchlistresult').innerHTML = 'This question is on your <a href="/account/do.html?watch">Watch List</a>';
		}
	}
}

var curRateID = 0;
function rateAnswer(answerid, rating) {	
	if (rating != '') {
		curRateID = answerid;
		Element.show('rateit_' + answerid + '_throbber');
		new Ajax.Request('/questions/display/rate/do.html?answer=' + answerid + '&rating=' + rating, {method: 'get', onComplete: rateAnswerFinish});
	}
}
function rateAnswerFinish(r) {
	Element.hide('rateit_' + curRateID + '_throbber');
	var json = ajaxError(r);
    if (json != '') {
		$('ratingcount_' + json.answerid).innerHTML = json.ratingcount;
		$('ratinggrade_' + json.answerid).innerHTML = json.ratinggrade;
		$('rateit_' + json.answerid).innerHTML = json.rateresult;
		new Effect.Highlight('rateit_' + json.answerid, {duration: 2.0});
	}
}


function addToWatchList(questionid) {
	Element.show('watchlist_throbber');
	if ($('qnotify').checked) {
		var notify = 'Y';
	}
	else {
		var notify = 'N';
	}	
	new Ajax.Request('/questions/display/watch/do.html?question=' + questionid + '&notify=' + notify, {method: 'get', 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/do.html?watch">Watch List</a>.';
		new Effect.Highlight('watchlistresult', {duration: 2.0});			
	}
}

var curFlagID = 0;
function flagPost(type, recordkey, flagresult, id) {
	if (flagresult != '') {
		curFlagID = id;
		Element.show(id + '_throbber');
		new Ajax.Request('/questions/display/flag/do.html?recordkey=' + recordkey + '&type=' + type + '&flagresult=' + flagresult + '&id=' + id, {method: 'get', onComplete: flagPostFinish});
	}
}
function flagPostFinish(r) {
	Element.hide(curFlagID + '_throbber');
	var json = ajaxError(r);
    if (json != '') {
		$(json.domid).innerHTML = json.displayresult;
		new Effect.Highlight(json.domid, {duration: 2.0});
		setTimeout('Element.hide(\'' + json.domid + '\')', 2000);
	}
}

function submitQComment(questionid) {
	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('/questions/display/qcomments/do.html?a=add', {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 == 'Y') {
			$('watchlistresult').innerHTML = 'This question is on your <a href="/account/do.html?watch">Watch List</a>';
		}
	}
}

function showAnswerBox() {
	//Element.hide('qflagoptions');
	Element.hide('watchlist');
	Element.hide('commentbox_question');
	Element.show('answerbox');
	$('answer').focus();
}
function showQuestionCommentBox() {
	//Element.hide('qflagoptions');	
	Element.hide('answerbox');
	Element.hide('watchlist');
	Element.show('commentbox_question');
	$('comment_question').focus();
}
function showWatchListBox() {
	//Element.hide('qflagoptions');	
	Element.hide('answerbox');
	Element.hide('commentbox_question');
	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 saveAnswer(url) {
	window.location.href = url;
}
