//<!-- begin script

/* These functions are mainly used to initialise the page when it's loading, setting the Booking Fee
   and Cancellation Insurance boxes, and calculating the total amount due, including any credit card
   charge. It also amends the amount due if any of the figures in the table are altered and capitalises
   any new property code that the user has entered.
   
   The charges for the Booking Fee, Cancellation Insurance and Credit Card payment are stored in the
   reservation page.*/

function initialize() {

  // Set the Booking Fee and Cancellation Insurance in the form.
  // In the latter case only set it if this is the first time the
  // form has been displayed, or if the user has not cancelled it
  // - in both cases canc_ins_cancelled is not set - so that if
  // the user has deleted it it won't be added back again.
  if (initial == 'no') return;
  booking_fee = parseFloat(booking_fee);
  var book_fee = document.getElementsByName("book_fee")[0];
  book_fee.value = String.fromCharCode(163) + formatNumber(booking_fee,2);
  var canc_ins_cancelled = document.getElementsByName("canc_ins_cancelled")[0].value;
  var canc_ins_obj = document.getElementsByName("canc_ins")[0];
  if (canc_ins_cancelled == 0) {
    var no_weeks = document.getElementsByName("weeks")[0].value;
    cancellation_ins = parseFloat(cancellation_ins * no_weeks);
    canc_ins_obj.value = String.fromCharCode(163) + formatNumber(cancellation_ins,2);
  }
  var em = document.getElementsByName("expiry_month")[0];
  var ey = document.getElementsByName("expiry_year")[0];
  var sy = document.getElementsByName("start_year")[0];
  var opn = new Array(7);
  var today = new Date();
  var year = today.getFullYear();
  var counter;
  var start_year;
  var end_year;

  // Calculate the total payment due. Set the box name to 'none' so we know we're coming from
  // inialisation
  calculate_payment('none');

  // Fill out the listboxes for the start and expiry years, setting up the start years as a
  // blank and then the last four years (finishing with the current one), and the expiry years
  // as a blank then the current and the next three years,
  // If the hidden field showing the expiry year is set, indicating that the user has
  // completed the form, then select the start and expiry years from the two hidden values after
  // filling out these listboxes.

  var hey = document.getElementsByName("exp_year")[0];
  var sy_selected;
  var ey_selected;
  if (hey.value == "") {
    sy_selected = "";
    ey_selected = "";
  }
  else {
    var hsy = document.getElementsByName("st_year")[0];
    sy_selected = hsy.value;
    ey_selected = hey.value;
  }

  for (counter = 1; counter <= 7; counter++) {
    if (counter > 1) {
      start_year = year - 6;
      end_year = year - 1;
    }
    else {
      start_year = "";
      end_year = "";
    }
    opn[counter] = document.createElement("option");
    sy.options.add(opn[counter]);
    opn[counter].innerHTML = start_year.toString();
    opn[counter].value = start_year;
    if (start_year == sy_selected) {
      opn[counter].selected = true
    }
    opn[counter] = document.createElement("option");
    ey.options.add(opn[counter]);
    opn[counter].innerHTML = end_year.toString();
    opn[counter].value = end_year;
    if (end_year == ey_selected) {
      opn[counter].selected = true
    }
    year++;
  }

} //initialize

function calculate_payment(box) {
/* This routine is used when the page is loaded to show the total payment due after the
   booking fee and the cancellation insurance has been added, and later when the user
   makes any change to the payment boxes or adds or removes credit card details.
   Every time the customer makes such a change it recalculates the total payment due. */
  var regexp = /([^\d\.])/g;
  var deposit_obj = document.getElementsByName("deposit")[0];
  var deposit = deposit_obj.value;
  deposit = parseFloat(deposit.replace(regexp, ""));
  var payment_obj = document.getElementsByName("payment")[0];
  
  // Clear all the entries if the deposit is zero or blank.
  if (deposit == 0 || isNaN(deposit)) {
    deposit_obj.value = '';
    var book_fee = document.getElementsByName("book_fee")[0];
    book_fee.value ='';
    var canc_ins = document.getElementsByName("canc_ins")[0];
    canc_ins.value = '';
    var heat_linen = document.getElementsByName("heat_linen")[0];
    heat_linen.value = '';
    var pet_cost = document.getElementsByName("pet_cost")[0];
    pet_cost.value = '';
    var hol_ins = document.getElementsByName("hol_ins")[0];
    hol_ins.value = '';
    var cc_charge = document.getElementsByName("cc_charge")[0];
    cc_charge.value = '';
    payment_obj.value = '';
  }
  else {
    
    // If the user has deleted the cancellation insurance amount then set canc_ins_cancelled
    // so that we don't put it back in again.
    // Box is only set when routine called by a change in table entries by the user.    
    if (box != 'none' && box.name == 'canc_ins' && isNaN(box.value)) {
      document.getElementsByName('canc_ins_cancelled').value = 1;
    }
    var basic_payment = sum_basic_payment();
    var charge_obj = document.getElementsByName("cc_charge")[0];
    
    // Have we got a credit card charge to add to the payment?
    // We're dealing with the initialisation of the card here
    // so the user can't have changed the card details.
    // (Changes that affect the credit card charge are dealt with by
    // adjust_cc_charge.)
    var charge = calculate_cc_charge(basic_payment);
    if (!charge) {
      var payment = basic_payment + charge;
      charge_obj.value = '';
      payment_obj.value = String.fromCharCode(163) + formatNumber(basic_payment,2);
    }
    else {
      var payment = basic_payment + charge;
      charge_obj.value = String.fromCharCode(163) + formatNumber(charge,2);
      payment_obj.value = String.fromCharCode(163) + formatNumber(payment,2);
    }
  }
  return true;

} //calculate_payment

function sum_basic_payment () {

  // This totals up the basic payment required before adding any credit card charge.
  var regexp = /([^\d\.])/g;
  var deposit = document.getElementsByName("deposit")[0].value;
  deposit = parseFloat(deposit.replace(regexp, ""));
  var book_fee = document.getElementsByName("book_fee")[0].value;
  book_fee = parseFloat(book_fee.replace(regexp, ""));
  if (isNaN(book_fee)) {book_fee = 0};
  var canc_ins = document.getElementsByName("canc_ins")[0].value;
  canc_ins = parseFloat(canc_ins.replace(regexp, ""));
  if (isNaN(canc_ins)) {canc_ins = 0};
  var heat_linen = document.getElementsByName("heat_linen")[0].value;
  heat_linen = parseFloat(heat_linen.replace(regexp, ""));
  if (isNaN(heat_linen)) {heat_linen = 0};
  var pet_cost = document.getElementsByName("pet_cost")[0].value;
  pet_cost = parseFloat(pet_cost.replace(regexp, ""));
  if (isNaN(pet_cost)) {pet_cost = 0};
  var hol_ins = document.getElementsByName("hol_ins")[0].value;
  hol_ins = parseFloat(hol_ins.replace(regexp, ""));
  if (isNaN(hol_ins)) {hol_ins = 0};
  return (deposit + book_fee + canc_ins + heat_linen + pet_cost + hol_ins);

}// sum_basic_payment

function adjust_cc_charge(type) {
  
  // Card details are being changed so alter the charges as necessary.
  // If the payment is zero then we don't need to do anything.
  var payment_obj = document.getElementsByName("payment")[0];
  if (!payment_obj.value) {
    return true;
  }
  var card_no = document.getElementsByName("card_no")[0].value;
  var charge_obj = document.getElementsByName("cc_charge")[0];
  var basic_payment = sum_basic_payment();

  if (type.name == 'card_type') {
    
    // CARD TYPE ALTERED.
    // If there's no card number then there won't be any credit card charge
    // calculated so we can exit.
    if (!card_no) {
      return true;
    }

    // Calculate any cc charge
    var charge = calculate_cc_charge(basic_payment,type.value);
    if (charge) {
      
      //If there is a charge store it away.
      var payment = basic_payment + charge;
      charge_obj.value = String.fromCharCode(163) + formatNumber(charge,2);
      payment_obj.value = String.fromCharCode(163) + formatNumber(payment,2);
    }
    else {
      
      // Delete any credit charge.
      charge_obj.value = '';
      payment_obj.value = String.fromCharCode(163) + formatNumber(basic_payment,2);
    }
  } //END OF CARD TYPE ALTERED.

  else {

    // CARD NUMBER ALTERED.
    if (card_no) {
        
      // A card number has been added or altered - calculate any charge.
      var charge = calculate_cc_charge(basic_payment);
      if (!charge) {

        // Delete any charge.
        charge_obj.value = '';
        payment_obj.value = String.fromCharCode(163) + formatNumber(basic_payment,2);
      }
      else {
        var payment = basic_payment + charge;
        charge_obj.value = String.fromCharCode(163) + formatNumber(charge,2);
        payment_obj.value = String.fromCharCode(163) + formatNumber(payment,2);
      }
    }
    else {
      
      // The card number has been deleted. Remove any charge.
      charge_obj.value = '';
      payment_obj.value = String.fromCharCode(163) + formatNumber(basic_payment,2);      
    }
  } //END OF CARD NUMBER ALTERED.
  return true;

}//adjust_cc_charge

function calculate_cc_charge(basic_payment,type) {
  
  // This calculates the credit card charge if there is one due.
  // If there's no credit card number then there won't be a charge.
  var card_no = document.getElementsByName("card_no")[0].value;  
  if (!card_no) {
    return 0
  }
  // If we know the type of the card because it's been passed to adjust_cc_charge
  // then type will be set.
  if (!type) {

    // Get the name of the card type.
    var card_type_buttons = document.getElementsByName("card_type");
    
    // Need to loop round the radio buttons to find which one, if any, is checked.
    var i;
    for (i=0; i<card_type_buttons.length; i++) {
      if (card_type_buttons[i].checked) {
        type = card_type_buttons[i].value;
      }
    }
  }
  
  if (type == 'visa_credit' || type == 'mastercard') {
    
    // Work out the credit charge and store it away.
    var charge = basic_payment * credit_card_percent / 100;
    return charge;
  }
  else {
    return 0
  }

}//calculate_cc_charge

function prop_detail_change(box) {
  
  // The property code, arrival or departure date has been changed.
  var code = box.value;
  var box_names;
  
  // Capitalises any property code that the user enters, so that it's not rejected later.
  if (box.name == 'prop_code') {
    var uc_code = code.toUpperCase();
    box.value = uc_code;
    box_names = new Array('propname','changeover_day','price','deposit','book_fee','canc_ins','heat_linen','pet_cost','payment','cc_charge');
  }
  else {
    
    // The property is still the same so we don't need to delete the property name and changeover day.
    box_names = new Array('price','deposit','book_fee','canc_ins','heat_linen','pet_cost','payment','cc_charge');
  }
  
  // Now delete the things that may or will be altered as a result.
  // Delete any cc charge, so there will not be any cc card alert when Make Reservation is clicked.
  var name;
  for (var i=0;i<box_names.length;i++) {
    name = document.getElementsByName(box_names[i])[0];
    name.value = '';
  }
  
}//prop_detail_change

function formatNumber(num,decplaces) {

/* This formats a number to decplaces. It assumes that the number passed is a legitimate
  positive number and is within range; it pads zeros to the left for small numbers. */

  // Multiply by 10 to the decplaces power and round.
  var str = "" + Math.round(eval(num) * Math.pow(10,decplaces));

  // Pad small numbers with zeros to the left.
  while (str.length <= decplaces) {
    str = "0" + str;
  }

  // Calculate the decimal point position
  var decpoint = str.length - decplaces;
  return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);

} //formatNumber

//--> end script
