﻿
function voteComment(itemnum, vote, currentVote) {

	var http = false;
	try {
		http = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				http = false;
			}
		}
	}

	var url = '/ajax/voteComment.aspx?itemnum=' + itemnum + '&vote=' + vote + '&currentVote=' + currentVote;

	document.getElementById(itemnum + 'msgcommentTR').style.display = 'none';
	
	var responseTxt;

	http.open("GET", url);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {

			responseTxt = http.responseText;

			if (responseTxt == 'ERROR:NOTLOGGEDIN') {
				window.location = '/login/?goto=' + window.location;
			}
			else if (responseTxt == 'ERROR:TOOMANYVOTES') {
				alert('You have voted too many times today.\n\n Come back tomorrow to vote more!');
			}
			else if (responseTxt == 'ERROR:ALREADYVOTED') {
			document.getElementById(itemnum + 'msgcommentTR').style.display = '';
				document.getElementById(itemnum + 'msgcomment').innerHTML = 'You already voted<br>on this!';
			}
			else if (responseTxt == 'ERROR:VOTEENDED') {
			document.getElementById(itemnum + 'msgcommentTR').style.display = '';
				document.getElementById(itemnum + 'msgcomment').innerHTML = 'Voting ended<br>for this item!';
			}
			else if (responseTxt.substring(0, 8) == 'SUCCESS:') {
				document.getElementById(itemnum + 'commentvote').innerHTML = responseTxt.substring(8);
				document.getElementById(itemnum + 'msgcommentTR').style.display = '';
				document.getElementById(itemnum + 'msgcomment').innerHTML = 'Vote Submitted!';
			}
			else {
				document.getElementById(itemnum + 'msgcommentTR').style.display = '';
				document.getElementById(itemnum + 'msgcomment').innerHTML = 'Error Happened.<br>Try Again Later';
			}

		}
	}
	http.send(null);
}

function showCounter(countID) {

	if (document.getElementById("lastShownCount").value != "")
		document.getElementById(document.getElementById("lastShownCount").value).style.display = "none";

	document.getElementById(countID).style.display = "";

	document.getElementById("lastShownCount").value = countID;


}

function hideCounter() {

	if (document.getElementById("lastShownCount").value != "")
		document.getElementById(document.getElementById("lastShownCount").value).style.display = "none";
}


function voteAJAX(itemnum, vote, currentVote) {
		
	var http = false;
	try {
		http = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				http = false;
			}
		}
	}

	var url = '/ajax/vote.aspx?itemnum=' + itemnum + '&vote=' + vote + '&currentVote=' + currentVote;

	document.getElementById(itemnum + 'msgTR').style.display = 'none';
	
	var responseTxt;

	http.open("GET", url);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {

			responseTxt = http.responseText;

			if (responseTxt == 'ERROR:NOTLOGGEDIN') {
				window.location = '/login/?goto=' + window.location;
			}
			else if (responseTxt == 'ERROR:TOOMANYVOTES') {
				alert('You have voted too many times today.\n\n Come back tomorrow to vote more!');
			}
			else if (responseTxt == 'ERROR:ALREADYVOTED') {
				document.getElementById(itemnum + 'msgTR').style.display = '';
				document.getElementById(itemnum + 'msg').innerHTML = 'You already voted<br>on this!';
			}
			else if (responseTxt == 'ERROR:VOTEENDED') {
			document.getElementById(itemnum + 'msgTR').style.display = '';
				document.getElementById(itemnum + 'msg').innerHTML = 'Voting ended<br>on this item!';
			}
			else if (responseTxt.substring(0, 8) == 'SUCCESS:') {
				document.getElementById(itemnum + 'vote').innerHTML = responseTxt.substring(8);
				document.getElementById(itemnum + 'msgTR').style.display = '';
				document.getElementById(itemnum + 'msg').innerHTML = 'Vote Submitted!';
			}
			else {
				document.getElementById(itemnum + 'msgTR').style.display = '';
				document.getElementById(itemnum + 'msg').innerHTML = 'Error Happened.<br>Try Again Later';
			}





		}
	}
	http.send(null);
}

