/*
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){
    b = num.id.split("_"); // b[0]=storyid, b[1]=star number
    r = b[0]; // storyID for concatenating later
    s = b[1]; // Get the selected star
    a = 0;
    for(i=1; i<=sMax; i++){		
      if(i<=s){
        document.getElementById(r+"_"+i).className = "on";
        holder = a+1;
        a++;
      }else{
        document.getElementById(r+"_"+i).className = "";
      }
    }
  }
}

// For when you roll out of the the whole thing //
function off(me){
  b = me.id.split("_"); // b[0]=storyid, b[1]=star number
  r = b[0]; // storyID for concatenating later
  s = b[1]; // Get the selected star
  if(!rated){
    if(!preSet){	
      for(i=1; i<=sMax; i++){		
        document.getElementById(r+"_"+i).className = "";
      }
    }
  }
}

// Send the rating to star_rating.html for processing
function sendRate(sel){
  b = sel.id.split("_"); // b[0]=storyid, b[1]=star number
  r = b[0]; // storyID for concatenating later
  s = b[1]; // Get the selected star

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

  // post rating
  var url = "http://isc.sans.edu/api/star_rating.html";
  var params = 'article_id=' + r + '&vote=' + s;
  http.open("POST", url, true);

  //Send the proper header information along with the request
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");

  http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
      result = http.responseText;
      document.getElementById(r).innerHTML = result;
    }
  }
  http.send(params);
}


