
var xhr = null;

function SubmitVote(tid, vote) {
	if(xhr && xhr.readyState!=0) {
		xhr.abort()
	}
	xhr=GetXmlHttpObject();
	if(xhr){
		var url = APP_PATH + "/VoteCommand.aspx?tid="+tid+"&a=" + vote
		xhr.open("GET", url, true);
		xhr.onreadystatechange=function() {
			if(xhr.readyState==4&&xhr.responseText){
				retVal = xhr.responseText;
				if (retVal != null) {
					if (retVal == "login") {
						//if not logged in - do redirect with returnUri
						returnUri = "&returnUri=" + document.location;
						document.location.replace(APP_PATH + "/Login.aspx");
					} else { 
						ChangeVoteDisplay(tid, vote, retVal);
						AddVote(tid, vote);
					}
				}
			}
		}
		xhr.send(null);
	}
}

function ChangeVoteDisplay(tid, vote, percent) {
	ChangePercentDisplay(tid, percent);
	ChangeBtnDisplay(tid, vote);
}

function ChangePercentDisplay(tid, percent) {
	var percentageDiv = document.getElementById(tid + "pc");
	if (percentageDiv) {
		percentageDiv.innerHTML = '<a href="#" onmouseover="return escape(\'Yes Votes: 10<br>No Votes: 2<br>Rank Value: 123456\')"><span class=percentage>' + 	percent + 	'</span><SPAN class=percentagesign>%</SPAN></a>';
	}
}
function ChangeBtnDisplay(tid, vote) {
	var yBtnDiv = document.getElementById(tid + "yBtn");
	var nBtnDiv = document.getElementById(tid + "nBtn");
	if (yBtnDiv) {
		if (yBtnDiv != "" && vote=="y") {		
			yBtnDiv.innerHTML = '<IMG height=19 src="' + APP_PATH + '/Images/vote_yes_d.gif" width=33 border=0>';
		} else {
			yBtnDiv.innerHTML = '';
		}
	}
	if (nBtnDiv) {
		if (nBtnDiv != "" && vote=="n") {
			nBtnDiv.innerHTML = '<IMG height=19 src="' + APP_PATH + '/Images/vote_no_d.gif" width=33 border=0>';
		} else {
			nBtnDiv.innerHTML = '';
		}
	}
}


//  COOKIE STUFF:
var VOTE_COOKIE_NAME	= "tv"
var tipVoteArray = new Array();

function InitVote() {
	tipVoteArray = LoadVoteCookie();
}

function AddVote(tid, vote) {
	var ln = 0;
	if (tipVoteArray != null)
		ln = tipVoteArray.length;
	if (GetVote(tid) == null) {
		tipVoteArray[ln] = new Array(tid, vote);
		SaveVoteCookie(tipVoteArray);
	}
}

function IsYesVote(tid) { 
	if (GetVote(tid) == "y")
		return true;
	else
		return false;
}

function IsNoVote(tid) { 
	if (GetVote(tid) == "n")
		return true;
	else
		return false;
}

function GetVote(tid) { // returns null if no vote exists
	var vote = null;
	for (i=0; i<tipVoteArray.length; i++) {
		if (tipVoteArray[i][0] == tid) 
			vote = tipVoteArray[i][1]; // vote
	}
	return vote;
}

function SaveVoteCookie(voteArray) {
	var rawCookieValue = "";
	for (i=0; i<voteArray.length; i++) {
		if (i!=0)
			rawCookieValue += '#';
		rawCookieValue += voteArray[i][0] + '-'; // tip id + inner tip-vote seperator
		rawCookieValue += voteArray[i][1];
	}
	SetCookieParam(VOTE_COOKIE_NAME, VOTE_COOKIE_NAME, rawCookieValue);
}

function LoadVoteCookie() {
	var returnArray = new Array();
	var rawCookieValue = GetCookieParamValue(VOTE_COOKIE_NAME, VOTE_COOKIE_NAME);
	if (rawCookieValue != null && rawCookieValue != "") {
		var rawData = rawCookieValue.split('#');
		for (i=0; i<rawData.length; i++) {
			var nameValue = rawData[i].split('-');
			returnArray[i] = new Array(nameValue[0], nameValue[1]); // vote
			ChangeBtnDisplay(nameValue[0], nameValue[1]);
		}	
	}
	return returnArray;
}

