var base = '/';

function changeVote(user, id, htmlid, thumbup) {
	if (user == '0') {
		window.location="/login.php?return="+location.href;
		return;
	}
	$.ajax({
		url: base + "ivote.php",
		data: "storyid=" + id + "&user=" + user +"&thumbup=" + thumbup,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$("#votecnt-" + htmlid).html(rc);
				if (thumbup == 1 || thumbup == 0) {
					$("#myvote-" + htmlid).html("Voted");
				} else {
					$("#myvote-" + htmlid).html("Buried");
				}
			}			
		} 
	});
}

function report(user, id, htmlid) {
	if (user == '0') {
		window.location="/login.php?return="+location.href;
		return;
	}
	$.ajax({
		url: base + "ivote.php",
		data: "storyid=" + id + "&user=" + user + "&report=1",
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$('#vbox-' + htmlid).html("<span class=\"error\">spam</span>");
			}
		} 
	});
}

function changeCommentVote(user, id, thumbup) {
	if (user == '0') {
		window.location="/login.php?return="+location.href;
		return;
	}
	params = "commentid=" + id + "&user=" + user;
	if (!thumbup) {
		params += "&unvote=true";
	}
	$.ajax({
		url: base + "ivote.php",
		data: params,
		type: "POST",
		success: function(rc) {
			errorMatch = new RegExp("^ERROR:");
			if (rc.match(errorMatch)) {
				rc = rc.substring(6, rc.length);
				alert(rc);
			} else {
				$("#cvote-" + id).html(rc);
				$('#ratebuttons-' + id).hide();
			}
		} 
	});
}