function getXmlHttpObject(){
  var xmlhttp=false;
  try
  {
    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); /* for IE < 5 */
  }
  catch (e)
  {
     try
     {
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
     }
     catch (E)
     {
     xmlhttp = false;
     }
  }

  /* mozilla & opera */
  if (!xmlhttp && typeof XMLHttpRequest!='undefined')
  {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function addTip(id,tip){
  // Call tip approval page.
  xml = getXmlHttpObject();
  url = '/';
  try{
    params = "action=addtip2&id=" + id + "&tip=" + escape(tip);
    xml.open('POST', url, false);
    xml.send(params);
    if(xml.responseText=='1'){
      ret = true;
    }
    else{
      ret = false;
    }
  }
  catch(e){
    ret = false;
  }

  if(!ret){
    alert('A problem was encountered while attemping to add the tip.');
  }

  return false;
}

function approveTip(id){
  // Call tip approval page.
  xml = getXmlHttpObject();
  url = '/approvetip/' + id;
  try{
    xml.open('GET', url, false);
    xml.send(null);
    if(xml.responseText=='1'){
      document.getElementById('approveTip' + id).innerHTML = '<font color=green>Approved!</font>';
      fadeTips('tip' + id);
      inReview -= 1;
      if(inReview==0){
        fadeTips('tipsinreview');
      }
      ret = true;
    }
    else{
      ret = false;
    }
  }
  catch(e){
    ret = false;
  }
  
  if(!ret){
    alert('A problem was encountered while attemping to approve the tip.');
  }
  
  return false;
}

function deleteTip(id,fade){
  // Call tip approval page.
  if(confirm('Delete this tip.  Are you sure?')){
    xml = getXmlHttpObject();
    url = '/deletetip/' + id;
    try{
      xml.open('GET', url, false);
      xml.send(null);
      if(xml.responseText=='1'){
        fadeTips('tip' + id);
        if(fade){
          inReview -= 1;
          if(inReview==0){
            fadeTips('tipsinreview');
          }
        }
        ret = true;
      }
      else{
        ret = false;
      }
    }
    catch(e){
      ret = false;
    }

    if(!ret){
      alert('A problem was encountered while attemping to delete the tip.');
    }
  }
  else{
    ret = false;
  }

  return false;
}

function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function fadeTips(id, step){
  if(step){
    o = document.getElementById(id);
    o.style.visibility = 'hidden';
    o.style.position = 'absolute';
    if(id=='tipsinreview'){
      window.location.reload();
    }
  }
  else{
    opacity(id, 100, 0, 1000);
    setTimeout("fadeTips('" + id + "',1)", 1000);
  }
}

function voteTipUp(id){

  xml = getXmlHttpObject();
  url = '/votetipup/' + id;
  try{
    xml.open('GET', url, false);
    xml.send(null);
    if(xml.responseText=='1'){
      o = document.getElementById('arrow-' + id + '-up');
      o.src = '/images/up-green.gif';
      o = document.getElementById('arrow-' + id + '-down');
      o.src = '/images/down-gray.gif';
      ret = true;
    }
    else if(xml.responseText=='-1'){
      // Unvote.
      // Change icons for this tip.
      o = document.getElementById('arrow-' + id + '-up');
      o.src = '/images/up-gray.gif';
      o = document.getElementById('arrow-' + id + '-down');
      o.src = '/images/down-gray.gif';
      ret = true;
    }
    else if(xml.responseText=='-2'){
      alert('You cannot unvote for your own tip.');
      ret = true;
    }
    else if(xml.responseText=='-3'){
      // Have to be logged in to vote.
      alert('You must be logged in to vote.');
      ret = true;
    }
    else{
      ret = false;
    }
  }
  catch(e){
    ret = false;
  }

  if(!ret){
    alert('A problem was encountered while attemping to vote the tip up. ' + xml.responseText);
  }

  return false;
}

function voteTipDown(id){

  xml = getXmlHttpObject();
  url = '/votetipdown/' + id;
  try{
    xml.open('GET', url, false);
    xml.send(null);
    if(xml.responseText=='1'){
      // Change icons for this tip.
      o = document.getElementById('arrow-' + id + '-up');
      o.src = '/images/up-gray.gif';
      o = document.getElementById('arrow-' + id + '-down');
      o.src = '/images/down-red.gif';
      ret = true;
    }
    else if(xml.responseText=='-1'){
      // Unvote.
      // Change icons for this tip.
      o = document.getElementById('arrow-' + id + '-up');
      o.src = '/images/up-gray.gif';
      o = document.getElementById('arrow-' + id + '-down');
      o.src = '/images/down-gray.gif';
      ret = true;
    }
    else if(xml.responseText=='-2'){
      alert('You cannot vote your own tip down.');
      ret = true;
    }
    else if(xml.responseText=='-3'){
      // Have to be logged in to vote.
      alert('You must be logged in to vote.');
      ret = true;
    }
    else{
      ret = false;
    }
  }
  catch(e){
    ret = false;
  }

  if(!ret){
    alert('A problem was encountered while attemping to vote the tip down. ' + xml.responseText);
  }

  return false;
  
}

function showTipLength(o){
  left = 255 - o.value.length;
  cd = document.getElementById('countdown');
  cd.innerHTML = left;
  if(left<0){
    cd.style.color = 'red';
  }
  else if(left == 0){
    cd.style.color = 'black';
  }
  else{
    cd.style.color = 'green';
  }
  return true;
}

function deleteMessage(id){
  // Call tip approval page.
  if(confirm('Delete this message.  Are you sure?')){
    xml = getXmlHttpObject();
    url = '/deletemessage/' + id;
    try{
      xml.open('GET', url, false);
      xml.send(null);
      if(xml.responseText=='1'){
        fadeTips('message' + id);
        ret = true;
      }
      else{
        ret = false;
      }
    }
    catch(e){
      ret = false;
    }

    if(!ret){
      alert('A problem was encountered while attemping to delete the message.');
    }
  }
  else{
    ret = false;
  }

  return ret;
}

function reportTipSheet(id){
  // Call tip approval page.
  if(confirm('You should only report a tip sheet if you feel it is either SPAM or the topic is offensive.\n\nDO NOT REPORT A TIP SHEET IF JUST ONE TIP IS SPAMMY.  In that case, just vote down the offending tip.\n\nAre you SURE you want to report this tip sheet?')){
    xml = getXmlHttpObject();
    url = '/report/' + id;
    try{
      xml.open('GET', url, false);
      xml.send(null);
      if(xml.responseText=='1'){
        ret = true;
      }
      else{
        ret = false;
      }
    }
    catch(e){
      ret = false;
    }

    if(!ret){
      alert('A problem was encountered while attemping to report the tip sheet.');
    }
    else{
      alert('The tip sheet has been reported.  Thank you for helping keep TipDrop clean and valuable!');
    }
  }
  else{
    ret = false;
  }

  return ret;
}

