var g_AppPath = "/"

function showIt(item,i,j,max){
   var id;
   actualItem = item;
   
   // Show the selected boxes
   for (var x=1;x<=i;x++){
      id = item + "_" + x;
      document.getElementById(id).src = g_AppPath + "siteimages/ratings/b.gif";
   }
   
   // Display the not selected ones
   for (var x=i+1;x<=max;x++){
      id = item + "_" + x;
      if (x<=j) document.getElementById(id).src = g_AppPath + "siteimages/ratings/y.gif";
      else document.getElementById(id).src = g_AppPath + "siteimages/ratings/w.gif";
   }
}

function showOriginal(i,max){
   for (var x=1;x<=max;x++){
      id = actualItem + "_" + x;
      if (x<=i) document.getElementById(id).src = g_AppPath + "siteimages/ratings/y.gif";
      else document.getElementById(id).src = g_AppPath + "siteimages/ratings/w.gif";
   }
}

// Get the HTTP Object
function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}   

// Change the value of the outputText field
function setOutput(){
   var result;
   var data;
   var rating;
   var totalRates;
   var max;
   if(httpObject.readyState == 4){
        result = httpObject.responseText;

        if(result == "NOUSER")
        {
            alert("You must be logged in to rate a video.  Log in to make your rating count!");
        }
        else if(result != "OK")
        {
            alert(result);
            alert("Hmm... There appears to be a problem with the rating system.");
        }
        else if(result == "OK")
        {
            alert("Thanks!  You rating has been added!");
        }
        /*
          data = result.split(':::');
          rating     = data[0];
          totalRates = data[1];
          max        = data[2];
          
          removeActions(Math.round(rating),max);
          updateTextRating(actualItem,rating,totalRates);
        */
   }
}

function removeActions(rating,max){
      showOriginal(rating,max);
      var t;
      for (var x=1;x<=max;x++){
         id = actualItem + "_" + x;
         t = document.getElementById(id);
         t.onmouseover = null;
         t.onmouseout  = null;
         t.onclick     = null;
      }  
}

function updateTextRating(item,rating,total){
   var itemRating;
   var totalRating;
   var id;
   
   id = item + "_ir";
   itemRating = document.getElementById(id);
   id = item + "_tr";
   totalRating = document.getElementById(id);
   
   if (itemRating != null) itemRating.innerHTML = rating;
   if (totalRating != null) totalRating.innerHTML = total;
   
}

// Implement business logic
function ratingsAdd(item,rate,max){

    httpObject = getHTTPObject();
    actualItem = item;
   if (httpObject != null) {
      httpObject.open("GET", g_AppPath + "handlers/LinkRatingsHandler.ashx?LinkID="+item+"&Rating="+rate, true);
      httpObject.send(null);
      httpObject.onreadystatechange = setOutput;
   }
}

// Implement business logic
function ratingsAddNonUser(item,rate,max){

    alert("You must first log in to rate a link.  Make your rating count!");
    //alert("NonUser: " + item + " : " + rate);
    /*
    httpObject = getHTTPObject();
    actualItem = item;
    if (httpObject != null) {
      httpObject.open("GET", g_AppPath + "SetRating.ashx?Item="+item+"&Rating="+rate+"&Max="+max, true);
      httpObject.send(null);
      httpObject.onreadystatechange = setOutput;
    }
    */
}
 
var httpObject = null;
var actualItem = null;
