﻿// JScript File


var itemId = 0;
var numVotes = 0;
var aveRating = 0;
var stars = new Array();
function loadVotes(itemid)
{
    /*
    This function is called by the onload event of the page's body. This way the page will finish loading without needing to wait for the database to retrieve the votes. Once this AJAX call gets a responce, the Star Box is dynamically added to the page.
    */
    itemId = itemid;
    var url = '/scripts/xmlGetVotes.aspx?ContentID=' + itemId + '&hash=' + Math.random();
    var xmlhttp = null;

    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (xmlhttp != null)
    {
        xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var result = xmlhttp.responseXML.documentElement.getElementsByTagName('RESULT')[0];
            numVotes = result.getAttribute('numvotes');
            aveRating = result.getAttribute('averating');
            //document.getElementById('starsLoading').style.display = 'none';
            document.getElementById('RateItBox').style.display = 'block';
            if(result.getAttribute('hasvoted') == 'yes')
            {

            }
            else
            {
                document.getElementById('RateItBox').onmouseout = resetStars;
                
                               
                
                
                for(var i = 1; i <= 5; i++)
                {
                    stars[i] = document.getElementById('star' + i);
                    stars[i].onmouseover = mouseOverStar;
                    stars[i].onclick = starClick;
                    stars[i].starNum = i;
                    
                
                }
            }
            resetStars();
            setCurrentStars();
        }
        else if(xmlhttp.readyState == 4 && xmlhttp.status != 200)
        {
            //error
        }
    }
    xmlhttp.open('GET',url,true);
    xmlhttp.send(null);
    }
    else
    {

    }
}

function mouseOverStar()
{

    for(var i = 1; i <= this.starNum; i++)
    {
    stars[i].className = 'starHover';
    }
    for(var i = this.starNum + 1; i <= 5; i++)
    {
    stars[i].className = 'star';
    }
}


function resetStars()
{
    for(var i = 1; i <= 5; i++)
    {
        if(i > aveRating)
        stars[i].className = 'star';
        else
        stars[i].className = 'starHover';
    }
}

function setCurrentStars()
{
    for(var i = 1; i <= 5; i++)
    {
        if(i > aveRating)
            document.getElementById("currentStar" + i).className = 'currentstar';
        else
            document.getElementById("currentStar" + i).className = 'currentstarHover';
    }
}

function starClick()
{

    var vote = this.starNum;
    var url = "/scripts/xmlSubmitVote.aspx?ContentID=" + itemId + "&Rate=" + vote + "&h=" + Math.random();
    var xmlhttp = null;
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (xmlhttp != null)
    {
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        var someDOM = xmlhttp.responseXML.documentElement;
        if(someDOM.getElementsByTagName('SUCCESS').length !== 1)
        {
        }
        else
        {
            aveRating = aveRating * numVotes;
            aveRating = aveRating + vote;
            numVotes++;
            aveRating = Math.round(aveRating / numVotes);      
        }
    
        for(var i = 1; i <= 5; i++)
        {
            stars[i].onmouseover = null;
            stars[i].onclick = null;
           
        }
        resetStars();
        setCurrentStars();
    }
    else if(xmlhttp.readyState == 4 && xmlhttp.status != 200)
    {
        
    }
    }
    xmlhttp.open('GET',url,true);
    xmlhttp.send(null);
    }
    else
    {
    
    }
    
    //hide rating box
    //document.getElementById('RateItBox').style.visibility = "hidden";
    //document.getElementById('RateThisTitle').style.visibility = "hidden";
    
    
}
