lastId = '';
numStar = 5;

function select(id)
{
    var statusObj = document.getElementById("rate");
    var lastObj = document.getElementById(lastId);
    var count = getRating(id);

    if(statusObj.className == "done")
        return;

    if(lastId != '')
        lastObj.className = "notRated";
    
    while(count >= 1) {
        document.getElementById("rating" + count).className = "rated";
        count--;
    }

    lastId = id;
}

function unSelect(id)
{
    var statusObj = document.getElementById("rate");
    var obj = document.getElementById(id);
    var count = getRating(id);

    if(statusObj.className == "done")
        return;

    while(count >= 1) {
        document.getElementById("rating" + count).className = "notRated";
        count--;
    }
}

function getRating(id)
{
    var length = id.length;
    return id.charAt(length - 1);
}

function rate(id)
{
    var statusObj = document.getElementById("rate");
    var obj = document.getElementById(id);
    
    if(statusObj.className == "notDone") {
        //statusObj.className = "done";
        sendRating(id);
    }
}

function unrate()
{
    var xmlHttp;
    var url;
    var statusObj = document.getElementById("rate");
    var i;

    if(statusObj.className == "done") {
        statusObj.className = "notDone"
    }

    for(i=1; i<=numStar; i++) {
        document.getElementById("rating" + i).className = "notRated";
    }

    xmlHttp = getXmlHttpObject();
    xmlHttp.onreadystatechange=function()
    {
        document.getElementById("response").innerHTML = "";
    }
    url = "/rating.php?unrate=yes";
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function sendRating(id) {
    var xmlHttp;
    var url;
    var count = getRating(id);
    
    xmlHttp = getXmlHttpObject();
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4){
            //document.myForm.time.value=xmlHttp.responseText;
            document.getElementById("response").innerHTML = xmlHttp.responseText;
        }
    }
    
    url = "/rating.php?rate=" + count;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function loadCommentBox(id) {
    var xmlHttp;
    var url;

    xmlHttp = getXmlHttpObject();

    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4){
            document.getElementById(id).innerHTML = xmlHttp.responseText;
        }
    }

    url = "/rating.php?load=yes";
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function sendComment() {
    var xmlHttp;
    var url;
    var params;
    
    xmlHttp = getXmlHttpObject();

    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4) { 
            document.getElementById("responseComment").innerHTML = xmlHttp.responseText;
            document.getElementById("comment").value = "";
            document.getElementById("commentSet").innerHTML = "";
        }
    }
    
    comment = document.getElementById("comment").value;
    email = document.getElementById("commentEmail").value;
    url = "/rating.php";
    params = "comment=" + comment + "&email=" + email;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.send(params);
}

function getXmlHttpObject() {
    
    var xmlHttp = null;

    try 
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) 
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Rating cannot be submitted because your browser does not support AJAX.");
                return false;
            }
        }
    }
    
    return xmlHttp;
}

