/* Standard AJAX stuff */

var response = '';

function createRequestObject() {
  var req;
  if(window.XMLHttpRequest) {
    // Firefox, Safari, Opera...
    req = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    // Internet Explorer 5+
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {
    // There is an error creating the object,
    // just as an old browser is being used.
    alert('Problem creating the XMLHttpRequest object');
  }
  return req;
}

function handleResponse() {
  if(http.readyState == 4 && http.status == 200) {
    // Text returned FROM the PHP script
    response = http.responseText;
  }
}

// Make the XMLHttpRequest object
var http = createRequestObject();


/* Custom functions */

function changeQty() {

  qtys = document.getElementsByName("qty[]");
  prices = document.getElementsByName("price[]");
  groups = document.getElementsByName("group[]");
  wine_ids = document.getElementsByName("wine_id[]");

  var total_bottles = 0;
  var spare_space = 0;
  var wine_total = 0;
  var total_cases = 0;
  var shipping_per_case = 0;
  var gst_rate = 0;

  /* url for save cart */
  var url = "/cart-save.php?1=1";
  
  for(i = 0; i < wine_ids.length; i++) {

    var qty = qtys[i].value;
    var price = prices[i].value;
    var subtotal = qty * price;
    wine_total += subtotal;

    sn = 'subtotal_disp_' + wine_ids[i].value + '_' + groups[i].value;
    subtotal_ref = document.getElementById(sn);
    
    if(subtotal == 0) {
      subtotal_ref.innerHTML = '';
    }
    else {
      subtotal_ref.innerHTML = '($' + subtotal + ')';
      total_bottles += groups[i].value * qty;
      total_cases = Math.ceil(total_bottles / 12);
      spare_space = total_cases * 12 - total_bottles;
    }
    
    url += '&wine_ids[]=' + wine_ids[i].value + '&group_ids[]=' + groups[i].value + '&qtys[]=' + qtys[i].value;
  }

  document.getElementById("wine_total").innerHTML = '$' + formatAsMoney(wine_total);
  document.getElementById("total_cases").innerHTML = total_cases;
  document.getElementById("total_cases").innerHTML += ' (' + total_bottles + ' bottles)';
  
  if(spare_space != 0) {
    document.getElementById("total_cases").innerHTML += ' (buy ' + spare_space + ' more bottles to fill up the case)';
  }

  zone = document.getElementsByName('shipping_zone')[0][document.getElementsByName('shipping_zone')[0].selectedIndex].value;
  url += '&shipping_zone=' + zone;
  if(zone == 'nz_ni') {
    shipping_per_case = 10;
    gst_rate = 0.125;
  }
  else if(zone == 'nz_si') {
    shipping_per_case = 7;
    gst_rate = 0.125;
  }
  else if(zone == 'aus') {
    shipping_per_case = 9999;
  }
  else if(zone == 'usa') {
    shipping_per_case = 9999;
  }
  else if(zone == 'row') {
    shipping_per_case = 9999;
  }
  shipping = shipping_per_case * total_cases;

  document.getElementById('shipping_cost').innerHTML = "$" + shipping;
  var wine_and_shipping = wine_total + shipping;
  document.getElementById('wine_and_shipping').innerHTML = "$" + formatAsMoney(wine_and_shipping);
  var gst = wine_and_shipping*gst_rate;
  document.getElementById('gst').innerHTML = "$" + formatAsMoney(gst);
  var grand_total = wine_and_shipping + gst;
  document.getElementById('grand_total').innerHTML = "$" + formatAsMoney(grand_total);
 
  if(total_bottles >= 2) {
    document.getElementById('submit_button').value = 'Continue >>';
    document.getElementById('submit_button').disabled = false;
  }
  else {
    document.getElementById('submit_button').value = 'Minimum order is 2 bottles';
    document.getElementById('submit_button').disabled = true;
  }  

  url += "&wine_total=" + wine_total;
  url += "&shipping_cost=" + shipping;
  url += "&wine_and_shipping=" + wine_and_shipping;
  url += "&gst=" + gst;
  url += "&grand_total=" + grand_total;
  
  //alert(url);
  http.open('get', url);
  http.onreadystatechange = handleResponse;
  http.send(null);
   
}


function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}


