/*
 * $Id:$
 *
 * This is a javascript file used for recording votes to the system
 */


function voteRecorder(urlId, choice, updater) {
  return function() {
    updater(votingMessage(urlId, "Recording your vote..."));
    new Ajax.Request("vote.php",
                     {
                       method: 'POST',
                       parameters: {url_id: urlId, choice: choice},
                       onSuccess: function(t) {
                         var data = t.responseText.evalJSON();
                         if(data.status == 'OK') {
                           updater(alreadyVoted(urlId, choice, updater));
                         } else {
                           alert(t.responseText);
                         }
                       }
                     });
    return false;
  }
}

function voteRemover(urlId, updater) {
  return function() {
    updater(votingMessage(urlId, "Undoing your vote..."));
    new Ajax.Request("unvote.php",
                     {
                       method: 'POST',
                       parameters: {url_id: urlId},
                       onSuccess: function(t) {
                         try {
                           var data = t.responseText.evalJSON();
                           if(data.status == 'OK') {
                             updater(yesNoOptions(urlId, updater));
                           } else {
                             alert(t.responseText);
                           }
                         } catch(e) {
                           alert(t.responseText);
                         }
                       }
                     });
    return false;
  }
}

