/*
Author: Addam M. Driver
Date: 10/31/2006
*/

var sMax;    // Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// Rollover for image Stars //
function rating(num)
{
    sMax = 0;    // Isthe maximum number of stars
    for(n=0; n<num.parentNode.childNodes.length; n++)
    {
        if(num.parentNode.childNodes[n].nodeName == "A")
        {
            sMax++;    
        }
    }
    
    if (!rated)
    {
        s = num.id.replace("_", ''); // Get the selected star
        a = 0;
        for(i=1; i<=sMax; i++)
        {
            if(i<=s)
            {
                document.getElementById("_"+i).className = "on";
//                document.getElementById("rateStatus").innerHTML = num.title;    
                holder = a+1;
                a++;
            }
            else
            {
                document.getElementById("_"+i).className = "";
            }
        }
    }
}

// For when you roll out of the the whole thing //
function off(me)
{
    if(!rated)
    {
        if(!preSet)
        {    
            for(i=1; i<=sMax; i++)
            {        
                document.getElementById("_"+i).className = "";
            }
        }
        else
        {
            rating(preSet);
            document.getElementById("rateMe").innerHTML = "Bedankt voor uw stem";
        }
    }
}

// When you actually rate something //
function rateIt(me, rating1)
{
    if(!rated)
    {
        preSet = me;
        rated=1;
        sendRate(rating1);
        rating(me);
    }
}

// Send the rating information somewhere using Ajax or something like that.
function sendRate(sel)
{
    //Instantiate a new XMLHttpRequest object
    var myXMLHttpRequest = GET_XMLHTTPRequest();
    
    if (myXMLHttpRequest)
    {
        urlVote = "http://www.mijnsexfotos.nl/photorate.php?id=" + document.getElementById('photoid').value + "&vote=" + sel;
        myXMLHttpRequest.open("GET", urlVote, true);
        
        //Lets define the method that gets called when the request finishes
        myXMLHttpRequest.onreadystatechange = function (aEvt) {
            if (myXMLHttpRequest.readyState == 4)
            {
                alert(myXMLHttpRequest.responseText);
            }
        };
        
        myXMLHttpRequest.send(null);
    }
    else
    {
        alert("Je stem is niet bewaard, probleem onbekend.");
    }
}

function GET_XMLHTTPRequest()
{
    var request;
    
    // Lets try using ActiveX to instantiate the XMLHttpRequest object
    try
    {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(ex1)
    {
        try
        {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(ex2)
        {
            request = null;
        }
    }

    // If the previous didn't work, lets check if the browser natively support XMLHttpRequest 
    if(!request && typeof XMLHttpRequest != "undefined")
    {
        //The browser does, so lets instantiate the object
        request = new XMLHttpRequest();
    }

    return request;
}
