var ratedtips = new Object();
var currenttip = null;
var currentvote;
var xho = false; 

function ratetip(id, rating) {
	if (ratedtips['t'+id]) {
		setMessage(id, 'Sorry, you can only vote once for each tip.');
		return;
	}	
	ratedtips['t'+id] = true;
		
    if (window.XMLHttpRequest) {
        try {
            xho = new XMLHttpRequest();
        } catch(e) {
            xho = false;
        }
    } else 
    if (window.ActiveXObject) {
        try {
            xho = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xho = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                xho = false;
            }
        }
    }
    if (!xho) {
        alert('Sorry, your browser does not support XMLHttpRequest'); // temp
    } else {
        var url = '/rating.do?form=ratingForm'+
                  '&section=ratings'+
                  '&template=rating'+
                  '&type=page'+
                  '&tip='+id+
                  '&rating='+rating;
        currenttipid = id;
        currentvote = rating;
        xho.onreadystatechange = getRatingResponse;
        xho.open("POST", url, true);
        xho.send(''); 
    }
}

function setMessage(id,msg) {
	getElement('msg-'+id).innerHTML = msg; 
	setTimeout("getElement('msg-"+id+"').innerHTML =''",2000); 
}

function getElement(id) {
	if (document.getElementById)
		return document.getElementById(id);
	if (document.all)
		return document.all[id];
	return null;
}

function getRatingResponse() {
    if (xho.readyState==4 && xho.status==200) {
		setMessage(currenttipid, 'Thank you, your opinion has been registered'); 
		var per = getElement('per-'+currenttipid);
		var peo = getElement('peo-'+currenttipid);
		if (per) {
			var count = parseInt(peo.innerHTML,10);
			var p = per.innerHTML;
			p = p.substr(0, p.indexOf('%')); // should lop the % sign for Safari
			var percent = parseInt(p,10); 
			var newperc = Math.round(((count * percent)+(currentvote*100)) / (count+1));
			per.innerHTML = newperc+'%';
			//window.status = 'percent:='+newperc;
			count++;
			peo.innerHTML = count;
		}
	}
}

function getQueryStringParam(pName) {
    var qs = location.search.substring(1,location.search.length);
	if (qs == null || qs.length == 0) return null;
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=');
		var name = unescape(pair[0]);
		if (name == pName) {
    		if (pair.length == 2)
    			value = unescape(pair[1]);
    		else
    			value = name;
    	    return value;
		}
	}    
}

// Check, in the querystring, for a rateTip=tipId,rating qs param.
// If found, then call the ratetip() method.
// This method is intended to be run in an onLoad=".." attribute.
function checkForRateTip() {
    var rateTipParam = getQueryStringParam("rateTip");
    if (rateTipParam!=null) {
        var pair = rateTipParam.split(",");
        if (pair.length==2) {
            var tipId  = pair[0];
            var rating = pair[1];
            if (getQueryStringParam("kierans-magic-debug-flag")!=null) {
                alert("Calling ratetip(" + tipId + "," + rating + ")");
            }
            ratetip(tipId, rating);
        }
    }
}


