//-----------------------------------------------------------------
// Captcha

var	g_answer;

function initCaptcha(id) {
	var	a, img, obj;
	a = sjcap();
	img = a[0];
	g_answer = a[1];
	obj = document.getElementById(id);
	if (obj == null) { alert('could not find id ' + id); return; }
	obj.innerHTML = '<img src='+img+' class=captcha_img>';
}

function checkCaptcha(user_answer) {
	var	uword;

	uword = hex_md5(user_answer);
	return uword == g_answer;
}

//-----------------------------------------------------------------
// Comments

function hideComments(msg_id) {
	var	s;

	$('#comments_'+msg_id).hide();

	s = $('#button_'+msg_id).html();
	s = s.replace('&lt;&lt; Hide', '&gt;&gt; View and add');
	$('#button_'+msg_id).html(s);
}

function myJSONparse(data) {
	var	obj;

	try {
		if ( typeof JSON == 'object' && typeof JSON.parse == 'function' ) {
			obj = JSON.parse(data);
		}
		else {
			obj = eval( '('+data+')' );
		}
	}
	catch(e) {
		obj = null;
	}

	return obj;
}

// Returns milliseconds since 1970
function getTime() {
       	return (new Date).getTime();
}

function boolToStr(b) {
	return b ? 'yes' : 'no';
}

function showComments(msg_id, is_show_success) {
	var	time, s, obj;

	$('#comments_'+msg_id).show();
	$('#comments_'+msg_id).html('<img src=images/working.gif>');
	$.get('get_comments.php?id='+msg_id+'&show_success='+boolToStr(is_show_success)+'&time='+getTime(), null, function(data) {
		obj = myJSONparse(data);
		$('#comments_'+msg_id).html(obj.comments);
		$('#count_'+msg_id).html(obj.n);
		if (!is_show_success) {
			initCaptcha('captcha_img_'+msg_id);
		}
	});

	s = $('#button_'+msg_id).html();
	s = s.replace('&gt;&gt; View and add', '&lt;&lt; Hide');
	$('#button_'+msg_id).html(s);
}

function toggleComments(msg_id) {
        if ($('#comments_'+msg_id).is(':visible')) {
		hideComments(msg_id);
	}
	else {
		showComments(msg_id, false);
	}
}

function enableDescendants(obj, is_enable) {
        if (obj == null) return;
        var children = obj.childNodes;
        for (var i = 0; i < children.length; i++) {
		if (children[i].nodeType == 1) { // IE only allows elements
                	children[i].disabled = !is_enable;
		}
                enableDescendants(children[i], is_enable);
        }
}

function formError(msg_id, txt) {
	$('#error_'+msg_id).html(txt);
	$('#error_'+msg_id).addClass('form_error');
}

function trimSpace(str) {
        return str.replace(/^\s+|\s+$/, '');
}

function addComment(form) {
	var	msg_id, comment, person;
	var	comment_esc, person_esc;

	enableDescendants(form, false);

	msg_id = form.id.value;
	comment = form.comment.value;
	person = form.person.value;
	user_answer = form.user_answer.value;
	if (person == '' || comment == '' || user_answer == '') {
		formError(msg_id, 'Please enter a comment, name and anti-spam word');
		enableDescendants(form, true);
		return;
	}
	if (!checkCaptcha(user_answer)) {
		formError(msg_id, 'You got the anti-spam word wrong');
		enableDescendants(form, true);
		return;
	}
	comment_esc = escape(comment);
	person_esc = escape(person);
	$.get('add_comment.php?id='+msg_id+'&comment='+comment_esc+'&person='+person_esc, null, function(data) {
		data = trimSpace(data);
		if (data != 'OK') {
			formError(msg_id, data);
			enableDescendants(form, true);
			return;
		}

		showComments(msg_id, true);
	});

	// Do NOT re-enable the form.
}

$(document).ready(function() {
	// Preload the working indicator
	var	working = new Image;
	working.src = 'images/working.gif';
});

//-----------------------------------------------------------------
// Buttons on main page

function updateCurrent() {
	$('#update_current_working').html('<img src=images/working.gif>&nbsp;');
	$('#update_current_button').attr('disabled', true);
	$.get('index.php?cmd=getcurrent&time='+getTime(), function(data) {
		if (trimSpace(data) == 'multi') {
			// Update whole page
			$('#update_current_button').attr('disabled', false);
			window.location.reload();
			return;
		}
		$('#current').fadeOut(500, function() {
			$('#current').html(data);
			$('#current').fadeIn(500);
			$('#update_current_working').html('');
			$('#update_current_button').attr('disabled', false);
		});
	});
}

function getMoreAlerts(next_id) {
	$('#more_button_working').html('<img src=images/working.gif>&nbsp;');
	$('#more_button').attr('disabled', true);
	$.get('index.php?cmd=getmore&next_id='+next_id, function(data) {
		$('#more_'+next_id).html(data);
	});
}

