/*<![CDATA[*/ 

//kept for compatibility
function wigoupdate(req,avg)
{
  if (req.readyState == 4 && req.status == 200)
  {
    arr = req.responseText.split(':');
    span = document.getElementById(arr[0]);
    if (span)
    {
      origvote = parseInt(span.innerHTML,10);
      if (avg != null && avg) {
        newvote = Math.round((arr[1]/arr[2])*100)/100;
      } else {
        newvote = arr[1];
      }
      if (origvote != newvote || span.title != arr[3]) {
        span.innerHTML = newvote;
        span.title = arr[3];
        wigoinvalidate();
      }
    }
  } else {
    alert('An error occured: ' + req.responseText);
  }
}

//for new up-down voting
//ajax return value is in the form of "$pollid:$plus:$minus:$zero:$totalvotes:$totaltooltip:$distribtitle:$myvote:$result"
function wigoupdate2(req)
{
  if (req.readyState == 4 && req.status == 200)
  {
    arr = req.responseText.split(':');
    span = document.getElementById(arr[0]);
    if (span)
    {
      origvote = parseInt(span.innerHTML,10);
      plus = arr[1];
      minus = arr[2];
      zero = arr[3];
      newvote = plus - minus;
      newtotal = plus + minus + zero;
      newtotalvotes = arr[4];
      newtotaltooltip = arr[5];
      newdistribtitle = arr[6];
      myvote = arr[7];

      upbutton = document.getElementById(arr[0] + "-up");
      neutralbutton = document.getElementById(arr[0] + "-neutral");
      downbutton = document.getElementById(arr[0] + "-down");
 
      if (upbutton && upbutton.style.display=='none' ) {
        removeSpinner(arr[0]+'-up');
        upbutton.style.display='';
      }
        
      if (neutralbutton && neutralbutton.style.display=='none' ) {
        removeSpinner(arr[0]+'-reset');
        neutralbutton.style.display='';
      }
      
      if (downbutton && downbutton.style.display=='none' ) {
        removeSpinner(arr[0]+'-down');
        downbutton.style.display='';
      }
 
      if (origvote != newvote || span.title != newtotaltooltip) {
        span.innerHTML = newvote;
        span.title = newtotaltooltip;
        //update buttons

        //up
        if (upbutton) {
          if (myvote == 1) {
            //add class
            if (!upbutton.className || upbutton.className == "") {
              upbutton.className = "myvotebutton";
            } else {
              upbutton.className += " myvotebutton";
            }
          } else {
            //remove class
            if (upbutton.className.match(/(\s*|^)myvotebutton(\s*|$)/)) {
              upbutton.className = upbutton.className.replace(/(\s*|^)myvotebutton(\s*|$)/g,' ')
            }
          }
        }
        //neutral
        if (neutralbutton) {
          if (myvote == 0) {
            //add class
            if (!neutralbutton.className || neutralbutton.className == "") {
              neutralbutton.className = "myvotebutton";
            } else {
              neutralbutton.className += " myvotebutton";
            }
          } else {
            //remove class
            if (neutralbutton.className.match(/(\s*|^)myvotebutton(\s*|$)/)) {
              neutralbutton.className = neutralbutton.className.replace(/(\s*|^)myvotebutton(\s*|$)/g,' ')
            }
          }
        }
        //down
        if (downbutton) {
          if (myvote == -1) {
            //add class
            if (!downbutton.className || downbutton.className == "") {
              downbutton.className = "myvotebutton";
            } else {
              downbutton.className += " myvotebutton";
            }
          } else {
            //remove class
            if (downbutton.className.match(/(\s*|^)myvotebutton(\s*|$)/)) {
              downbutton.className = downbutton.className.replace(/(\s*|^)myvotebutton(\s*|$)/g,' ')
            }
          }
        }
        //update distribution bar
        distribtable = document.getElementById(arr[0] + "-dist");
        if (distribtable) {
          distribtable.title = newdistribtitle;
          //calculate percentages
          if (newtotalvotes != 0) {
            uppercent = (plus / newtotalvotes) * 100;
            downpercent = (minus / newtotalvotes) * 100;
            neutralpercent = (zero / newtotalvotes) * 100;
          } else {
            uppercent = 0;
            downpercent = 0;
            neutralpercent = 0;
          }
          if (uppercent != 0) {
            uppercent += "%";
          }
          if (neutralpercent != 0) {
            neutralpercent += "%";
          }
          if (downpercent != 0) {
            downpercent += "%";
          }
          //update widths
          tr = distribtable.getElementsByTagName("tr")[0]
          //tr.style.display = "";
          tds = tr.getElementsByTagName("td");
          tds[0].style.display = "";
          tds[1].style.display = "";
          tds[2].style.display = "";
          tds[0].style.width = uppercent;
          tds[1].style.width = neutralpercent;
          tds[2].style.width = downpercent;
        }
        wigoinvalidate();
      }
    }
  } else {
    alert('An error occured: ' + req.responseText);
  }
}

function wigoinvalidated(req)
{
  if (req.readyState == 4 && req.status == 200)
  {
    if (req.responseText != "ok") {
      alert('An error occured while invalidating the cache' + req.responseText);
    }
  } else {
    alert('An error occured: ' + req.responseText);
  }

}

function wigoinvalidate() {
  sajax_do_call('wigoinvalidate',[wgPageName],wigoinvalidated);
}

function wigovoteup(voteid)
{
  button = document.getElementById(voteid+'-up');
  button.style.display = 'none';
  removeSpinner(voteid+'-up');
  injectSpinner(button,voteid+'-up');
  sajax_do_call('wigovote2',[voteid,1],wigoupdate2);
}

function wigovotedown(voteid)
{
  button = document.getElementById(voteid+'-down');
  button.style.display = 'none';
  removeSpinner(voteid+'-down');
  injectSpinner(button,voteid+'-down');
  sajax_do_call('wigovote2',[voteid,-1],wigoupdate2);
}

function wigovotereset(voteid)
{
  button = document.getElementById(voteid+'-neutral');
  button.style.display = 'none';
  removeSpinner(voteid+'-reset');
  injectSpinner(button,voteid+'-reset');
  sajax_do_call('wigovote2',[voteid,0],wigoupdate2);
}

function wigoupdateavg(req)
{
  wigoupdate(req,true);
}

function wigovotesend(voteid,val,min,max)
{
  if (val < min || val > max) {
    alert('Invalid value');
  } else if (voteid.substr(0,6) != 'slider') {
    alert('Invalid vote id');
  } else {
    sajax_do_call('wigovote',[voteid,val,min,max],wigoupdateavg);
  }
}

/*]]>*/
