var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp = false
    }
}
function myXMLHttpRequest() {
    var xmlhttplocal;
    try {
        xmlhttplocal = new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
        try {
            xmlhttplocal = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (E) {
            xmlhttplocal = false;
        }
    }

    if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttplocal = new XMLHttpRequest();
        } catch (e) {
            xmlhttplocal = false;
            alert('couldn\'t create xmlhttp object');
        }
    }
    return(xmlhttplocal);
}
function sndReq(vote, id_num, uid, units, context) {
    var theUL = document.getElementById('unit_ul' + id_num);
    theUL.innerHTML = '<div class="loading" style="width: 150px;"></div>';
    // Handle AJAX request
    xmlhttp.open('get', context + '/plugins/rates/rating.action?j=' + vote + '&q=' + id_num + '&k=' + uid + '&c=' + units);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
}
function handleResponse(cookieName) {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var response = xmlhttp.responseText;
            var update = new Array();

            if (response.indexOf('|') != -1) {
                update = response.split('|');
                changeText(update[0], update[1]);
            }
        }
    }
}
function changeText(div2show, text) {
    var IE=(document.all) ? 1 : 0;
    var DOM=0;
    if (parseInt(navigator.appVersion) >= 5) {
        DOM=1;
    }
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML=text;
    } else if (IE) {
        document.all[div2show].innerHTML=text;
    }
}
var ratingAction = {
    'a.rater' : function(element) {
        element.onclick = function() {
            var parameterString = this.href.replace(/.*\?(.*)/, "$1");
            var parameterTokens = parameterString.split("&");
            var parameterList = new Array();
            for (j = 0; j < parameterTokens.length; j++) {
                var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1");
                parameterList[parameterName] = parameterTokens[j].replace(/.*=(.*)/, "$1");
            }
            var theContext = parameterList['o'];
            var theRatingID = parameterList['q'];
            var theVote = parameterList['j'];
            var theUnits = parameterList['c'];
            var theUid = parameterList['k'];
            sndReq(theVote, theRatingID, theUid, theUnits, theContext);
            document.getElementById("info").innerHTML="Thanks for voting!";
            return false;
        }
        element.onmouseover = function() {
            var info=document.getElementById("info");
            if(this.textContent=="1")
                info.innerHTML="Didn't like it, Sorry!";
            else if(this.textContent=="2")
                info.innerHTML="Ok Presentation";
            else if(this.textContent=="3")
                info.innerHTML="Good Presentation";
            else if(this.textContent=="4")
                info.innerHTML="Very Good Presentation";
            else if(this.textContent=="5")
                info.innerHTML="Excellent Presentation";
            return true;
        }
        element.onmouseout = function() {
            document.getElementById("info").innerHTML="</br>";
            return true;
        }
    }

};
Behaviour.register(ratingAction);