// Calculate values for:
// - deposit
// - buggy hire
// - catering
// - golf
// - subtotal
// - due
function calculateTotals() {
  costs = {
    'deposit': calculateDeposit(),
    'buggy_hire': calculateBuggyHire(),
    'catering': calculateCatering(),
    'golf': calculateGolf()
  }
  costs.subtotal = calculateSubTotal(costs);
  costs.due = calculateDue(costs);
  updateTotals(costs);
}

function updateTotals (costs) {
  updateDeposit(costs['deposit']);
  updateBuggyHire(costs['buggy_hire']);
  updateCatering(costs['catering']);
  updateGolf(costs['golf']);
  updateSubtotal(costs['subtotal']);
  updateDue(costs['due']);
}

function prepCashValue (value,prefix) {
  if (value != 0) {
    change = (value*100)%100;
    if (change > 0) {
      notes = value - (change/100);
      if (change < 10) {change='0'+change};
      value = notes+'.'+change;
    }
    value = prefix+value;
  }
  else  {
    value = '--';
  }
  return value;
}

function updateFields (fields_to_update,value) {
  for(i=0; i < fields_to_update.length; i++)  {
    $(fields_to_update[i]).value = value;
  }
}

function updateDeposit (value) {
  value = prepCashValue(value,'£');
  fields_to_update = ['deposit','lessdeposit'];
  updateFields(fields_to_update,value);
}

function updateBuggyHire (value) {
  value = prepCashValue(value,'£');
  fields_to_update = ['totalbuggyhirecost','totalbuggycost'];
  updateFields(fields_to_update,value);
}

function updateCatering (value) {
  value = prepCashValue(value,'£');
  fields_to_update = ['totalcateringcost'];
  updateFields(fields_to_update,value);
}

function updateGolf (value) {
  value = prepCashValue(value,'£');
  fields_to_update = ['totalgolfcost'];
  updateFields(fields_to_update,value);
}

function updateSubtotal (value) {
  value = prepCashValue(value,'£');
  fields_to_update = ['subtotal'];
  updateFields(fields_to_update,value);
}

function updateDue (value) {
  value = prepCashValue(value,'£');
  fields_to_update = ['balancetopay'];
  updateFields(fields_to_update,value);
}


function calculateDeposit () {
  deposit = 0;
  deposit += calculateVisitorDeposit();
  return deposit;
}

function calculateBuggyHire () {
  cost_per_buggy_per_round = 17;
  return cost_per_buggy_per_round * (getNumberOfBuggiesRequired() * getNumberOfRoundsBuggiesRequiredFor())
}

function calculateCatering () {
  cost_per_coffee_and_biscuit = 1.5;
  cost_per_coffee_and_bacon_roll = 3.25;
  cost_per_soup_and_sandwich = 4;
  cost_per_lunch = 7.5;
  cost_per_high_tea = 8.5;
  cost_per_set_menu = 15;
  
  number_of_visitors = getNumberOfVisitors();
  
  return (
    (cost_per_coffee_and_biscuit * getNumberOfCoffeeAndBiscuits())
    + (cost_per_coffee_and_bacon_roll * getNumberOfCoffeeAndBaconRolls())
    + (cost_per_soup_and_sandwich * getNumberOfSoupAndSandwiches())
    + (cost_per_lunch * getNumberOfLunches())
    + (cost_per_high_tea * getNumberOfHighTeas())
    + (cost_per_set_menu * getNumberOfSetMenus())
  )
}

function calculateGolf () {
  return calculateWeekdayCost() + calculateWeekendCost();
}

function calculateSubTotal (costs) {
  return costs['buggy_hire'] + costs['catering'] + costs['golf'];
}

function calculateDue (costs) {
  return costs['subtotal'] - costs['deposit'];
}


function calculateVisitorDeposit () {
  deposit_per_visitor = 10;
  return deposit_per_visitor * getNumberOfVisitors();
}

function calculateWeekdayCost () {
  number_of_rounds = getNumberOfWeekdayRounds();
  if (number_of_rounds < 1) { return 0 };
  
  number_of_visitors = getNumberOfVisitors();
  if (number_of_visitors < 1) { return 0 };
  
// 16 or more players get a discount
  if (number_of_visitors >= 16) {
    ticket_cost = (number_of_rounds > 1) ? 32 : 22;
  }
  
// smaller numbers pay full whack
  else  {
    ticket_cost = (number_of_rounds > 1) ? 35 : 25;
  }
  
  return ticket_cost * number_of_visitors;
}

function calculateWeekendCost () {
  number_of_rounds = getNumberOfWeekendRounds();
  if (number_of_rounds < 1) { return 0 };
  
  number_of_visitors = getNumberOfVisitors();
  if (number_of_visitors < 1) { return 0 };
  
// 16 or more players get a discount
  if (number_of_visitors >= 16) {
    ticket_cost = (number_of_rounds > 1) ? 37 : 27;
  }
  
// smaller numbers pay full whack
  else  {
    ticket_cost = (number_of_rounds > 1) ? 40 : 30;
  }
  
  return ticket_cost * number_of_visitors;
}



function getNumberOfVisitors () {
  return $('numberofvisitors').value*1;
}

function getNumberOfBuggiesRequired () {
  return $('numberofbuggies').value*1;
}
function getNumberOfRoundsBuggiesRequiredFor () {
  return $('numberofroundsbuggies').value*1;
}

function getNumberOfCoffeeAndBiscuits () {
  return $('cateringA').value*1;
}
function getNumberOfCoffeeAndBaconRolls () {
  return $('cateringB').value*1;
}
function getNumberOfSoupAndSandwiches () {
  return $('cateringC').value*1;
}
function getNumberOfLunches () {
  return $('cateringD').value*1;
}
function getNumberOfHighTeas () {
  return $('cateringE').value*1;
}

function getNumberOfSetMenus () {
  return $('cateringSETMENU').value*1;
}

function getNumberOfWeekdayRounds () {
  switch (getNumberOfRounds())  {
    case '1 weekday':
      return 1;
    case '2 weekday':
      return 2;
    default:
      return 0;
  }
}
function getNumberOfWeekendRounds () {
  switch (getNumberOfRounds())  {
    case '1 weekend':
      return 1;
    case '2 weekend':
      return 2;
    default:
      return 0;
  }
}

function getNumberOfRounds () {
  number_of_rounds = $$('input[name=numberofrounds]').filter(function(item,index){return item.checked});
  return (number_of_rounds.length > 0) ? number_of_rounds[0].value : 0;
}












