

/*
* 2009-11-27 - this code is for managing the assignment of the prize money on the Pick A Winner template:
*
* /contest_dev.php/question/pickAWinner/id/11
* 
* 
* The inputs on the form are going to look like this:
* 
*        <p>Prize: <input type="text" value="2" name="sendPaymentsToAnswerers[11]" /></p>
*
*        <select id="awardForAnswerer4" class="awardForAnswerer" name="sendPaymentsToAnswerers[13]">
*            <option>0</option>
*            <option>1</option>
*            <option>2</option>
*        </select>
*
*
* The overall prize amount will be set as a global variable called overallPrizeAmount. 
* All of the inputs must add up to that amount, before the user can submit the form. 
* We will also use this script to enforce that when the user increases any amount via
* the select boxes, the total is subtracted from the input. 
*
* In the template pickAWinnerSuccess.php we set 2 global variables that this Javascript can use.
* In the source code, the lines look like this:
*
*        var overallPrizeAmount = 5;
*        var howManyAnswers = 3;
*
*/




function getPrizeAmountAssignedSoFar() {
  var totalAssignedSoFar = 0;
  $('.awardForAnswerer').each(function() {
      totalAssignedSoFar = parseInt(totalAssignedSoFar) + parseInt($(this).val());
  });
  return totalAssignedSoFar;
}


function doesTheAssignedPrizeMoneyEqualTheQuestionsPromisedPrizeAmount() {
  var totalAssignedSoFar = getPrizeAmountAssignedSoFar();
  overallPrizeAmount = parseInt(overallPrizeAmount);

  if (totalAssignedSoFar != overallPrizeAmount) {
    $('#assignedPrizeDoesNotEqualPromisedPrize').html("You have assigned $" + totalAssignedSoFar  + " but the prize promised for this question is $" + overallPrizeAmount + ". Please make sure these amounts are equal.");
    $('#assignedPrizeDoesNotEqualPromisedPrize').fadeIn('slow');
  } else {
     $('#assignedPrizeDoesNotEqualPromisedPrize').fadeOut('slow');
  }
}

function submitFormIfAssignedPrizeMoneyEqualsThePrizePromisedForThisQuestion() {
  var totalAssignedSoFar = getPrizeAmountAssignedSoFar();
  overallPrizeAmount = parseInt(overallPrizeAmount);


  if (totalAssignedSoFar != overallPrizeAmount) {
    $('#assignedPrizeDoesNotEqualPromisedPrize').fadeOut('slow');
    alert("You have assigned $" + totalAssignedSoFar  + " but the prize promised for this question is $" + overallPrizeAmount + ". Please make sure these amounts are equal.");
    return false; 
  } else {
     document.getElementById('pickAWinnerForm').submit(); 
  }
}



$(document).ready(function() {

  $('.awardForAnswerer').change(function() {
      doesTheAssignedPrizeMoneyEqualTheQuestionsPromisedPrizeAmount();
  });
  

  $('#pickAWinnerForm').submit(function() {
    return submitFormIfAssignedPrizeMoneyEqualsThePrizePromisedForThisQuestion();
  });




});

































