var allowVote=1;
var preload_starOn=new Image();
preload_starOn.src="/images/starLittle.gif";

var preload_starOff=new Image();
preload_starOff.src="/images/starLittleEmpty.gif";

var preload_starHalf=new Image();
preload_starHalf.src="/images/starLittleHalf.gif";

// all star votes is out of 5

// n is index of this star obj
// prefix is the prefix of the star images' ID, ie: star_ if all the star imgs are star_1, star_2 etc.
// turns stars indeces 1..this star on
function overStars(n, prefix) {
    if (allowVote) {
        for (i=1;i<=n;i++) {
            o=document.getElementById(prefix+i);
            if (o.src.indexOf('_off')>0) {
                o.src=o.src.replace('_off','_on');
            }
        }
        switchText(n,prefix);
    }
}

// turns stars indeces 1..this star off
function outStars(n, prefix) {
    if (allowVote) {
        for (i=1;i<=n;i++) {
            o=document.getElementById(prefix+i);
            if (o.src.indexOf('_on')>0) {
                o.src=o.src.replace('_on','_off');
            }
        }
        switchText(0,prefix);
    }
}

function vote(toolID, score, toolDivName) {
    if (allowVote) {
        var cmdstr="op=vote&id="+toolID+"&score="+score;
        sendAjaxRequest("/cgi/fxlabs.pl", getVotesResult, cmdstr, toolDivName);
        allowVote=0;
    }
}

function getVotes(toolID, toolDivName) {
    var cmdstr="op=getvotes&id="+toolID;
    sendAjaxRequest("/cgi/fxlabs.pl", getVotesResult, cmdstr, toolDivName);
}

//      function aCallBack(req, success, url, originalData, opaqueId)
function getVotesResult(req, success, url, odata, toolDivName){
    if (success) {
        var score=req.responseText.split("&")[0].split("=")[1];
        var votes=req.responseText.split("&")[1].split("=")[1];
        
        var stars=0;
        document.getElementById(toolDivName).innerHTML="";
        for (var i=1;i<=Math.floor(score);i++) {
            document.getElementById(toolDivName).innerHTML+="<img src='/images/starLittle.gif' width='11' height='16'>";
            stars++;
        }
        if (score-Math.floor(score)>=0.5) {
            document.getElementById(toolDivName).innerHTML+="<img src='/images/starLittleHalf.gif' width='11' height='16'>";
            stars++;
        } 
        for (var i=1;i<=5-stars;i++) {
            document.getElementById(toolDivName).innerHTML+="<img src='/images/starLittleEmpty.gif' width='11' height='16'>";
        }
        
        document.getElementById(toolDivName).innerHTML += "&nbsp;&nbsp; ("+votes+" votes)<br>";
    } else {
        document.getElementById(toolDivName).innerHTML = "Error ["+req.status+"] loading "+url;
    }
}


function switchText(n, prefix) {
    t=document.getElementById(prefix+'text');
    switch (n) {
        case 1: t.innerHTML='Hideous';break;
        case 2: t.innerHTML='Decent';break;
        case 3: t.innerHTML='Pretty good';break;
        case 4: t.innerHTML='Sweet!';break;
        case 5: t.innerHTML='Call CNN! This thing is awesome!';break;  
        default: t.innerHTML='';        
    }    
}


function formData2QueryString(docForm) {

  var submitContent = '';
  var formElem;
  var lastElemName = '';
  
  for (i = 0; i < docForm.elements.length; i++) {
    
    formElem = docForm.elements[i];
    switch (formElem.type) {
      // Text fields, hidden form elements
      case 'text':
      case 'hidden':
      case 'password':
      case 'textarea':
      case 'select-one':
        submitContent += formElem.name + '=' + escape(formElem.value) + '&';
        break;
        
      // Radio buttons
      case 'radio':
        if (formElem.checked) {
          submitContent += formElem.name + '=' + escape(formElem.value) + '&';
        }
        break;
        
      // Checkboxes
      case 'checkbox':
        if (formElem.checked) {
          // Continuing multiple, same-name checkboxes
          if (formElem.name == lastElemName) {
            // Strip of end ampersand if there is one
            if (submitContent.lastIndexOf('&') == submitContent.length-1) {
              submitContent = submitContent.substr(0, submitContent.length - 1);
            }
            // Append value as comma-delimited string
            submitContent += ',' + escape(formElem.value);
          }
          else {
            submitContent += formElem.name + '=' + escape(formElem.value);
          }
          submitContent += '&';
          lastElemName = formElem.name;
        }
        break;
        
    }
  }
  // Remove trailing separator
  submitContent = submitContent.substr(0, submitContent.length - 1);
  return submitContent;
}


// AJAX stuff
function getAjaxRequestType() {
  var xmlhttp;
  var activeXType;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject(activeXType = "Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject(activeXType = "Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return activeXType;
}

var ieActiveXType = getAjaxRequestType();

function createAjaxRequest() {
  var xmlhttp;
  
  if (ieActiveXType) {
    xmlhttp = new ActiveXObject(ieActiveXType);
  } else {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

// if you do a GET, set data, mime to be null or don't pass them
// callback should have the following prototype
//
//      function aCallBack(req, success, url, originalData, opaqueId)
// url, originalData is to make it easy to properly identify to which request this response belongs to.
function sendAjaxRequest(url, callback, opt_data, opt_opaqueId, opt_mime) {

  // use a new private instance for every request
  var req = createAjaxRequest();
  var method = 'GET';
  if (opt_data) {
    method = 'POST';
    if (!opt_mime) {
      opt_mime = 'application/x-www-form-urlencoded';
    }
  }
  req.open(method, url, true);
  if (opt_data && opt_mime) {
    req.setRequestHeader('Content-Type', opt_mime);    // must be called after open
  }
  // stick the instance data into the scope of the function literal 
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
      callback(req, req.status && req.status == 200, url, opt_data, opt_opaqueId);
      req.onreadystatechange = function () {};
    }
  };
  req.send(opt_data);
}
