var brBaseUri = "";
var mode="add";
var hash="";
var _restaurantId='';
function addBoosAndRahs(baseUri, restaurantId, personId, isRah, IpAddress, restHash) {
	_restaurantId=restaurantId;
	hash=restHash;
		if (brBaseUri == "") {
			brBaseUri = baseUri; 
			var booRahHandlerURL = baseUri + "RestaurantBoosRahs.do?rId="+restaurantId+"&pId="+personId+"&isRah="+isRah+"&ipA="+IpAddress;
			var myAjax = new Ajax.Request(
				booRahHandlerURL, 
				{
					method: 'get', 
					onComplete: handleBoosRahsUpdateSuccess
				});
		}
		return true;
}

function clearVote(baseUri, voteId, restaurantId, restHash) {
	mode="clear";
	brBaseUri = baseUri; 
	var booRahHandlerURL = baseUri + "RestaurantBoosRahs.do?rId="+restaurantId+"&clear=1&voteId="+voteId;
	var myAjax = new Ajax.Request(
		booRahHandlerURL, 
		{
			method: 'get', 
			onComplete: handleBoosRahsUpdateSuccess
		});
	document.cookie ='brVoted'+restHash+'=deleted; expires=Fri, 27 Jul 1999 02:47:11 UTC;'
	return true;
}

var handleBoosRahsUpdateSuccess = function(o){
    var rsp=o.responseText;
    if (rsp != null) {
        rsparr = rsp.split(" ");
        if (rsparr.length >= 5) {
                        if (rsparr[0] == "OK:" ) {
                            var boocount = document.getElementById("boocount");
                            if (boocount != null) {
                                boocount.innerHTML = rsparr[1]+' Votes';
                            }
                            var rahcount = document.getElementById("rahcount");
                            if (rahcount != null) { 
                                    rahcount.innerHTML = rsparr[2]+' Votes';
                                }
                                var percentPop = document.getElementById("percentpop");
                                if (percentPop != null) {
                                        percentPop.innerHTML = rsparr[3] + "%";
                                }
                                var booButton = document.getElementById("boobutton");
                                if (booButton != null) {
                                        booButton.src=brBaseUri+ "static/images/booButton1.jpg";
                                        booButton.style.cursor="";
                                        booButton.style.cursor="pointer";
                                        booButton.onclick = "";
                                }

                                var rahButton = document.getElementById("rahbutton");
                                if (rahButton != null) {
                                        rahButton.src=brBaseUri+ "static/images/rahButton1.jpg";
                                        rahButton.style.cursor="";
                                        rahButton.style.cursor="pointer";
                                        rahButton.onclick = "";
                                }
                         
                                var clickHere = document.getElementById("voteclickhere");
                                if (clickHere != null) {
                                        clickHere.innerHTML='<span style="font-size:0.8em;">Thanks for voting! <a style="border-bottom:0;font-size:0.8em;" href="" onclick="javascript:clearVote(baseUri,'+rsparr[4]+','+_restaurantId+',\''+hash+'\');trackClick(\'detail\',\'clear-vote\','+_restaurantId+');return false;"><span style="margin-left:25px;">Clear vote</span></a></span>';
                                }
                                var pcRahdhead = document.getElementById("pcrahdhead");
                                if (pcRahdhead != null) {
                                        pcRahdhead.innerHTML =  "&nbsp;-&nbsp;" + rsparr[3] + "% Rah'd";
                                }
                                if(mode=='add'){
                                        var now = new Date();
                                        var expire = new Date();
                                        var cookieName = "brVoted"+hash;
                                        expire.setTime(now.getTime() + 3600000*24*1000);
                                        document.cookie = cookieName+"="+escape(rsparr[4])+";expires="+expire.toGMTString();
                                }else{           
                                        window.location=_baseUri+hash;
                                }
                            

                        }

                }
        }
        return false;
}

