/*
 * Javascript utilities for ratings and comments.
 * See ratings.inc and comments.inc for more information.
 */

/**
 * Perform a single vote (S-type) on the party.
 *
 * @param partyId The party ID.
 * @param electionId The ID of the election the party belongs to.
 * @param electionType M = multiple votes per election, X = one vote per election.
 * @param mainurl The main web application URL (relative).
 */
function ratingsVoteSingle(partyId,electionId,electionType,mainurl) {
  var voteButton = document.getElementById("votebutton"+partyId);

  // Perform server request.
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET",mainurl+"profil/vote.php?party_id="+partyId,true);
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      if(xmlHttp.responseText && xmlHttp.responseText=="") {
        // Successful vote.
        if(document.getElementById("votecount"+partyId)) {
          voteCount = parseInt(document.getElementById("votecount"+partyId).innerHTML);
          voteCount++;
          document.getElementById("votecount"+partyId).innerHTML = voteCount;
        }
        switch(electionType) {
          case "M":
            voteButton.value = "Danke";
            break;
          case "X":
            var buttons = document.getElementsByName("votebutton"+electionId);
            for(var index = 0;index<buttons.length;index++) {
              buttons[index].disabled = true;
              buttons[index].value = "Danke";
            }
            break;
          default:
            alert("Unknown election type.");
            break;
        }
      } else {
        // Error during voting.
        alert(xmlHttp.responseText);
        voteButton.disabled = false;
        voteButton.value = "Abstimmen";
      }
    }
  }
  xmlHttp.send(null);

  voteButton.value = "Beschäftigt...";
  voteButton.disabled = true;
}

/**
 * Perform a 5-star rating on this party.
 *
 * @param partyId The party ID.
 * @param rating The rating given by the user (1-5).
 * @param mainurl The main web application URL (relative).
 */
function ratingsVote5(partyId,rating,mainurl) {
  // Perform server request.
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET",mainurl+"profil/vote.php?party_id="+partyId+"&rating="+rating,true);
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      if(xmlHttp.responseText && xmlHttp.responseText!="") {
        alert(xmlHttp.responseText);
      }
    }
  }
  xmlHttp.send(null);
}

/**
 * Called when the mouse enters a rating star.
 *
 * @param partyId the party ID.
 * @param rating The star over which the mouse resides.
 * @param mainurl The main web application URL (relative).
 */
function ratingsMouseOver5(partyId,rating,mainurl) {
  var index;

  for(index=1;index<=5;index++)
    if(index<=Math.round(rating))
      document.getElementById("rating_"+partyId+"_"+index).src = mainurl+"resources/rating.gif";
    else
      document.getElementById("rating_"+partyId+"_"+index).src = mainurl+"resources/norating.gif";

  document.getElementById("rating_"+partyId).innerHTML = rating;
}

/**
 * Loads a set of comments into the ID element "comments"
 * per server request.
 *
 * @param feedbackId The feedback ID.
 * @param offset The comment offset for comments that are listed
 *               newest first. A value of 0 will list the newest comment
 *               on top, a value of 1 will skip the newest and list the
 *               second newest on top, listing a fixed number of comments
 *               (typically 100). A negative value will always list all
 *               comments, starting with the oldest on top and the newest
 *               at the very bottom.
 * @param urlOffset The hierarchical distance of the calling script's URL
 *                  from the main URL. A value of 0 results in mainurl to
 *                  be "". A value of 3 results in "../../../".
 */
function commentsShowComments(feedbackId,offset,urlOffset) {
  var mainurl = "";
  for(var index=0;index<urlOffset;index++)
    mainurl += "../";
  var div = document.getElementById("comments");

  // Perform server request.
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET",mainurl+"profil/comments.php?js&feedback_id="+feedbackId+"&offset="+offset+"&url_offset="+urlOffset,true);
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      div.innerHTML = xmlHttp.responseText;
    }
  };
  xmlHttp.send(null);

  if(!document.all) // IE bug.
    div.innerHTML = "Kommentare werden geladen...";
  location.href = "#commentslocation";
}

/**
 * Checks the validity of the input form for the comments.
 *
 * @return true if input is ok, false if there is a problem.
 */
function commentsCheckComment() {
  if(document.getElementById("comment").value=="") {
    alert("Du hast keinen Kommentartext eingegeben.");
    return false;
  }

  setTimeout('document.getElementById("commentsubmit").disabled=true;document.getElementById("commentsubmit").value="Senden...";',50);

  return true;
}

/**
 * Add the new comment and reload comments.
 *
 * @param feedbackId The feedback ID.
 * @param offset The comment offset for comments that are listed
 *               newest first. A value of 0 will list the newest comment
 *               on top, a value of 1 will skip the newest and list the
 *               second newest on top, listing a fixed number of comments
 *               (typically 100). A negative value will always list all
 *               comments, starting with the oldest on top and the newest
 *               at the very bottom.
 * @param urlOffset The hierarchical distance of the calling script's URL
 *                  from the main URL. A value of 0 results in mainurl to
 *                  be 0. A value of 3 results in "../../../".
 */
function commentsPostComment(feedbackId,offset,urlOffset) {
  var mainurl = "";
  for(var index=0;index<urlOffset;index++)
    mainurl += "../";
  var div = document.getElementById("comments");

  // Perform server request.
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("POST",mainurl+"profil/comments.php?js&feedback_id="+feedbackId+"&offset="+offset+"&url_offset="+urlOffset,true);
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      div.innerHTML = xmlHttp.responseText;
    }
  };
  var post = "action=add&comment="+encodeURIComponent(document.getElementById("comment").value);
  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xmlHttp.send(post);

  setTimeout("document.getElementById('commentsubmit').disabled = false",5);
}
