CORE.answers = {};

CORE.answers.validateAskForm = function() {
	
	var errors = new Array();
	
	if ($('#categoryid').val() == '') {
		errors[0] = 'Category is required';
	}	
	
	if ($.trim($('#question').val()).length == 0) {
		errors[errors.length] = 'Question is required';
	}
	
	if ($.trim($('#question').val()).length > 150) {
		errors[errors.length] = 'Question cannot exceed 150 characters (currently ' + $.trim($('#question').val()).length + ')';
	}
	
	if (errors.length) {
	
		var msgs = '';
		
		for (i = 0; i < errors.length; i++) {
			msgs += '- ' + errors[i] + '\n';
		}
		
		alert(msgs);		
		return false;
		
	} else {
		return true;
	}
	
};

CORE.answers.validateAnswerForm = function() {
	
	if ($.trim($('#answer').val()).length == 0) {	
		alert('Answer is required');
		return false;
	}	
	if ($.trim($('answer').val()).length > 3000) {	
		alert('Answer cannot exceed 3000 characters (currently ' + $.trim($('answer').val()).length + ')');
		return false;		
	} else {
		return true;
	}
	
};

CORE.answers.currentAnswer = 0;

CORE.answers.submitComment = function(answerid) {
	
	var len = $.trim($('#comment_' + answerid).val()).length;
	
	if (len == 0) {
		alert('Comment is required');
	} else if (len > 500) {
		alert('Comment cannot exceed 500 characters (currently ' + len + ')');
	} else {
		
		CORE.answers.currentAnswer = answerid;
		
		$('#commentthrobber_' + answerid).toggle();
				
		$.ajax(
			{
				type: 'POST',
		   		url: '/advice/questions/display/comments/index.cfm?a=add',
		   		data: $('#commentform_' + answerid).serialize(),
		   		dataType: 'json',
				success: function(json) {

					$('#commentthrobber_' + curAnswer).toggle();
					
					Element.hide('commentbox_' + json.answerid);
		
					$('#comment_' + json.answerid).html('');
					
					$('<li id="' + json.domid + '">' + json.commenttext + '</li>').appendTo('#commentlist_' + json.answerid);

					$('#commentswrapper_' + json.answerid)
					.css('backgroundColor','#ffffcc')
					.show()
					.animate(
						{backgroundColor: '#ffffcc'}, 
						1000
					)
					.animate(
						{backgroundColor: '#ffffe0'}, 
						1000
					);					
					
					if (json.updatewatchlist == 'Y') {
						
						$('#watchlistresult')
						.val(
							'This question is on your <a href="/account/index.cfm?watch">Watch List</a>'
						);
				
					}					

		   		}
		 	}
		);		

	}

	return false;
	
};

CORE.answers.submitQComment = function(questionid) {

	var len = $.trim($('#comment_question').val()).length;

	if (len == 0) {
		alert('Comment is required');
	} else if (len > 500) {
		alert('Comment cannot exceed 500 characters (currently ' + len + ')');
	} else {
		
		$('#commentthrobber_question').toggle();
		$('#commentform_question').serialize();
		
		$.ajax(
			{
				type: 'POST',
		   		url: '/advice/questions/display/qcomments/index.cfm?a=add',
		   		data: $('#commentform_question').serialize(),
		   		dataType: 'json',
				success: function(json) {
					
					$('#commentthrobber_question').toggle();
					$('#commentbox_question').hide();
					$('#comment_question').html('');
					
					$('<li id="' + json.domid + '">' + json.commenttext + '</li>').appendTo('#qcommentlist');
					
					$('#commentswrapper_question')
					.css('backgroundColor','#ffffcc')
					.show()
					.animate(
						{backgroundColor: '#ffffcc'}, 
						1000
					)
					.animate(
						{backgroundColor: '#ffffe0'}, 
						1000
					);			
						
					if (json.updatewatchlist == 'Y') {
						$('#watchlistresult').html('This question is on your <a href="/account/do.html?watch">Watch List</a>');
					}
						
				}
				
			}
		);
		
	}
	
	return false;
	
};

CORE.answers.curRateID = 0;

CORE.answers.rateAnswer = function(answerid, rating) {
	
	if (rating != '') {
		CORE.answers.curRateID = answerid;
		$('#rateit_' + answerid + '_throbber').show();
		
		$.ajax(
			{
				type: 'GET',
		   		url: '/advice/questions/display/rate/index.cfm?answer=' + answerid + '&rating=' + rating,
		   		dataType: 'json',
				error: function() {
					alert('oops');
				}	,
				success: function(json) {
					
					$('#rateit_' + CORE.answers.curRateID + '_throbber').hide();
					$('#ratingcount_' + json.answerid).html(json.ratingcount);
					$('#ratinggrade_' + json.answerid).html(json.ratinggrade);
					$('#rateit_' + json.answerid).html(json.rateresult);
					
					$('#rateit_' + json.answerid)
					.css('backgroundColor','#ffffcc')
					.show()
					.animate(
						{backgroundColor: '#ffffcc'}, 
						1000
					)
					.animate(
						{backgroundColor: '#ffffe0'}, 
						1000
					);		
					
				}
				
			}
		);
		
	}
	
};

CORE.answers.showAnswerBox = function() {
	$('#watchlist').hide();
	$('#commentbox_question').hide();
	$('#answerbox').show();
	$('#answer').focus();	
};

CORE.answers.showQuestionCommentBox = function() {	
	$('#answerbox').hide();
	$('#watchlist').hide();
	$('#commentbox_question').show();
	$('#comment_question').focus();
};

CORE.answers.showWatchListBox = function() {
	$('#answerbox').hide();
	$('#commentbox_question').hide();
	$('#watchlist').show();
};

CORE.answers.showCommentBox = function(answerid) {
	$('#rateit_' + answerid).hide();
	$('#commentbox_' + answerid).show();
	$('#comment_' + answerid).focus();
};

CORE.answers.showAnswerRateBox = function(answerid) {
	$('#commentbox_' + answerid).hide();
	$('#rateit_' + answerid).show();
};

CORE.answers.saveAnswer = function(url) {
	window.location.href = url;
};
