var SUNDAY = 0;

function calc() {

    var frm = document.getElementById('order1');

    //var itemCost = document.getElementById('prodCost').value;
    var itemCost = 0;
    var priceType = document.getElementById('priceType');
    if (priceType.options[priceType.selectedIndex].value.indexOf('|') != -1) {
      itemCost = priceType.options[priceType.selectedIndex].value.split('|')[1];
    }

    var total = 0;

    var teddyCost = 0;
    if (document.getElementById('teddy').value.indexOf('|') != -1) {
      teddyCost = document.getElementById('teddy').value.split('|')[1];
      teddyCost = parseFloat(teddyCost);
    }

    var balloonCost = 0;
    if (document.getElementById('balloon').value.indexOf('|') != -1) {
      balloonCost = document.getElementById('balloon').value.split('|')[1];
      balloonCost = parseFloat(balloonCost);
    }

    var chocolateCost = 0;
    if (document.getElementById('chocolate').value.indexOf('|') != -1) {
      chocolateCost = document.getElementById('chocolate').value.split('|')[1];
      chocolateCost = parseFloat(chocolateCost);
    }

    var wineCost = 0;
    if (document.getElementById('wine').value.indexOf('|') != -1) {
      wineCost = document.getElementById('wine').value.split('|')[1];
      wineCost = parseFloat(wineCost);
    }

    var vaseCost = 0;
    if (document.getElementById('vase').value.indexOf('|') != -1) {
      vaseCost = document.getElementById('vase').value.split('|')[1];
      vaseCost = parseFloat(vaseCost);
    }

    itemCost = parseFloat(itemCost);

    total = teddyCost + balloonCost + chocolateCost + wineCost + vaseCost + itemCost;

    ROUNDING = 100;
    total = Math.round(total * ROUNDING) / ROUNDING;

    total = total.toFixed(2);

    frm.totalValue.value = total;
    document.getElementById('price').innerHTML = 'Total AUD $' + total;
          
}

function validateColor() {
    var colour1 = document.getElementById('colour1');
    var colour1Selected = colour1.options.selectedIndex;

    var colour2 = document.getElementById('colour2');
    var colour2Selected = colour2.options.selectedIndex;

    if (colour1Selected == colour2Selected) {
      alert('You must have two seperate colours: \n\n Colour Selection 2 (must contain a different value)');
      return false;
    }
    
    return validateForm(document.getElementById('order1'));
}

function validateDeliveryDate() {
  var frm = document.getElementById('order2');
  var day = frm.deliveryDay.options[frm.deliveryDay.options.selectedIndex].value;
  var month = frm.deliveryMonth.options[frm.deliveryMonth.options.selectedIndex].value;
  var year = frm.deliveryYear.options[frm.deliveryYear.options.selectedIndex].value;
  
  var date = new Date(year, month, day);
    
  if (date.getDay() == SUNDAY) {
    alert('Unfortunately we are unable to deliver on Sunday - please select another day.');
    frm.deliveryDay.options.selectedIndex = 0; // reset day
  }
}

function preselectToday() {
  var date = new Date();
  
  return doPreselect(date);
}

function preselectTomorrow() {
  var date = new Date();
  date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
  
  return doPreselect(date);
}

function doPreselect(date) {
  var frm = document.getElementById('order2');
  
  if (date.getDay() == SUNDAY) {
    alert('Unfortunately we are unable to deliver on Sunday - please select another day.');
    return false;
  }
  
  frm.deliveryDay.options.selectedIndex = date.getDate();
  frm.deliveryMonth.options.selectedIndex = date.getMonth() + 1;
  
  for (i=0; i<frm.deliveryYear.options.length; i++) {
    if (frm.deliveryYear.options[i].value == date.getFullYear()) {
      frm.deliveryYear.options[i].selected = true;
    }
  }
  
  return false;
}

function calc2() {
    var frm = document.getElementById('order2');
    var subtotal = document.getElementById('initialValue').value;
    var shippingCost = 0;
    var tmp;
    
    for(var i = 0; i < frm.deliveryOptions.length; i++) {
        if(frm.deliveryOptions[i].checked) {
            tmp = frm.deliveryOptions[i].value;
        }
    }

    if (tmp.indexOf('|') != -1) {
      shippingCost = tmp.split('|')[1];
    }
    
    shippingCost = parseFloat(shippingCost);
    subtotal = parseFloat(subtotal);

    subtotal = subtotal + shippingCost;
    
    ROUNDING = 100;
    subtotal = Math.round(subtotal * ROUNDING) / ROUNDING;
    
    subtotal = subtotal.toFixed(2);

    frm.totalValue.value = subtotal;
    document.getElementById('price').innerHTML = 'Total AUD $' + subtotal;
}
