var http = null;

if (window.XMLHttpRequest)
{
    http = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
    http = new ActiveXObject("Microsoft.XMLHTTP");
}

function processResponse()
{
    if (http.readyState == 4)
    {
        ratings    = new Array(2);
        ratings[0] = document.getElementsByName("rating1")[0];
        ratings[1] = document.getElementsByName("rating2")[0];
        
        response   = http.responseXML;
        helper     = new Array(2);
        
        for (i = 0; i < 2; i++)
        {
            helper[i] = ratings[i].parentNode.getElementsByTagName("div")[0];
        }
        
        imgWidth = response.getElementsByTagName("imgwidth")[0].firstChild.nodeValue;
        overall  = response.getElementsByTagName("overall")[0].firstChild.nodeValue;
        myrating = response.getElementsByTagName("myrating")[0].firstChild.nodeValue;
        votes    = response.getElementsByTagName("votes")[0].firstChild.nodeValue;
        
        helper[0].style.width = Math.round(overall * imgWidth) + "px";
        helper[1].style.width = Math.round(myrating * imgWidth) + "px";
        document.getElementById("totalvotes").innerHTML = votes;
        
        http.abort();
    }
}

function vote(imageId, userId, rating)
{
    nocache = Math.round(Math.random() * 1000000);
    url     = "./includes/rating.php?nocache=" + nocache;
    
    http.onreadystatechange = processResponse;
    http.open("GET", url + "&imageId=" + imageId + "&userId=" + userId + "&ajaxRating=" + rating, true);
    http.send(null);
}