

function askJudieCount() {
	var frm = document.getElementById('askJudieForm');
	if ( frm == null ) {
		frm = document.getElementById('askJudieFullForm');
		if (frm == null) {
			return;
		}
	}
	var ta = frm.elements['ajq'];
	var max = 200;
	var counter = document.getElementById('ajcCount');

	if ( ta.value.length > max ) {
		ta.value = ta.value.substring(0, max);
	}

	var new_count = max - ta.value.length;
	counter.innerHTML = new_count;
}

function validateAskJudie() {
	var frm = document.getElementById('askJudieForm');
	var ta = frm.elements['ajq'];
	var af = frm.elements['ask_from'];

	if ( ta.value.length == 0 ) {
		alert('Please ask a question to continue');
		return false;
	}

	af.value = window.location.toString();

	return true;
}

function validateFullAskJudie() {
	var frm = document.getElementById('askJudieFullForm');
	var ta = frm.elements['ajq'];
	var name = frm.elements['name'];
	var email = frm.elements['email'];

	if ( ta.value.length == 0 ) {
		alert('Please ask a question to continue');
		return false;
	}
	if ( name.value.length == 0 ) {
		alert('Please enter your name to continue');
		return false;
	}
	if ( email.value.length == 0 ) {
		alert('Please enter your email address to continue');
		return false;
	}

	return true;
}

function commentLightbox(qid, fbid) {
	if ( fbid == null ) {
		alert('Please connect via Facebook before leaving a comment');
		return;
	}

	var divid = 'ajComment_' + qid;

	if (document.getElementById(divid) != null) {
		return;
	}

	var arow = document.getElementById('arow_'+qid);
	var arheight = $(arow).height();
	arow.setAttribute('oldheight', arheight);
	arow.style.height = arheight + 'px';
	arow.style.overflow = 'hidden';

	var div = document.createElement('div');
	div.id = divid;
	div.style.background = '#ffffff';
	div.style.margin = '5px -5px 0px -5px';
	div.style.padding = '6px';
	div.innerHTML =
		'<textarea id="ajCommentTA_' + qid + '" class="ajCommentTA" ' +
		'onkeypress="return limitText(event, this, 200);"></textarea>';

	arow.appendChild(div);
	var new_height = arheight + $(div).height() + 12; // Add 12 for top and bottom padding

	$(arow).animate({
		height: new_height
	}, 'slow');

	// Rewrite the onclick handler for the "Leave a comment" tab
	var cl = document.getElementById('commentLink_'+qid);
	var old_onclick = cl.onclick;
	cl.onclick = function() {
		sendComment(qid, fbid);
	}

	cl.style.background = 'url(\'/images/Submit_Comment.png\')';

	//cl.innerHTML = 'Submit comment';

	setTimeout(function() {
		document.getElementById('ajCommentTA_'+qid).focus();
		document.getElementById('ajCommentTA_'+qid).select();
	}, 700);
}

function sendComment(qid, fbid) {
	var divid = 'ajComment_' + qid;
	var div = document.getElementById(divid);
	var arow = document.getElementById('arow_'+qid);
	var ta = document.getElementById('ajCommentTA_' + qid);
	var cl = document.getElementById('commentLink_'+qid);

	var _url = WEB_ROOT + '/ajax/ask-judie-comment';
	var _data = 'ajq='+qid+'&fbuid='+fbid+'&comment='+ta.value.substring(0, 200);

	$.ajax({
		type: 'POST',
		url: _url,
		data: _data,
		success: function(msg) {
			if (msg.indexOf('status=success') != -1) {
				// Create a new div, set the innerHTML to the comment and
				// inject the new div into the list of comments.
				// If there is no list of comments

				alert('Comment sent successfully. It will appear on this page when it has been approved.');

				var oldheight = parseInt(arow.getAttribute('oldheight'));
				arow.style.overflow = 'hidden';
				$(arow).animate({
					height: oldheight
				}, 'slow');

				setTimeout(function() {
					arow.removeChild(div);
					arow.style.overflow = '';
					arow.style.height = 'auto';
					cl.onclick = function() {
						commentLightbox(qid, fbid);
					}
					//cl.innerHTML = 'Leave a comment';
					cl.style.background = 'url(\'/images/Leave_A_Comment.png\')';
				}, 700);
			} else {
				alert(msg);
			}
		}
	});

	
}

function getFullAnswer( id ) {

	var answerDiv = document.getElementById('answer_' + id);
	var morelessLink = document.getElementById('ajMoreLessLink_' + id);
	var _url = WEB_ROOT + '/ajax/getajanswer';
	var _data = 'id=' + id + '&len=more';

	morelessLink.innerHTML = 'Loading...';

	$.ajax({
		type: 'POST',
		url: _url,
		data: _data,
		success: function(msg) {
			var arow = document.getElementById('arow_'+id);
			var oldheight = arow.style.height; //$(arow).height();
			var divid = 'ajComment_' + id;
			var commentdiv = document.getElementById(divid);
			
			if ( oldheight != '' && commentdiv != null ) {
				arow.style.height = '';
				arow.style.overflow = '';
			}
			
			answerDiv.innerHTML = msg;

			if ( oldheight != '' && commentdiv != null ) {
				//arow.style.height = ($(arow).height() + $('#'+divid).height() + 12) + 'px';
				arow.style.height = $(arow).height() + 'px';
				arow.style.overflow = 'hidden';
			}
		}
	});

}

function getLessAnswer( id ) {

	var answerDiv = document.getElementById('answer_' + id);
	var morelessLink = document.getElementById('ajMoreLessLink_' + id);
	var _url = WEB_ROOT + '/ajax/getajanswer';
	var _data = 'id=' + id + '&len=less';

	morelessLink.innerHTML = 'Loading...';

	$.ajax({
		type: 'POST',
		url: _url,
		data: _data,
		success: function(msg) {
			var arow = document.getElementById('arow_'+id);
			var oldheight = arow.style.height; //$(arow).height();
			var divid = 'ajComment_' + id;
			var commentdiv = document.getElementById(divid);

			if ( oldheight != '' && commentdiv != null ) {
				arow.style.height = '';
				arow.style.overflow = '';
			}

			answerDiv.innerHTML = msg;

			if ( oldheight != '' && commentdiv != null ) {
				//arow.style.height = (parseInt(oldheight) - $('#'+divid).height() - 12) + 'px';
				arow.style.height = $(arow).height() + 'px';
				arow.style.overflow = 'hidden';
			}
		}
	});

}

