﻿function order_calculator()
{
quantity = document.order.quantity.value
shipping_location = document.order.shipping_location.value

errorFlag = false

if (quantity == ""){ errorFlag = true }

	
switch (shipping_location)
{
case "1":
  shipping_charge = 10
  if (quantity > 1){
  	shipping_charge += ((quantity - 1) * 4)
		shipping_type = "UPS)"
  }
  break    
case "2":
    shipping_charge = 25
  if (quantity > 1){
  	shipping_charge += ((quantity - 1) * 5)
  	shipping_type = "UPS)"
  }
  break
case "3":
if (quantity == 1){ shipping_charge = 12.50 }
else if (quantity == 2){ shipping_charge = 25.00 }
else if (quantity > 2){shipping_charge = 25.00 + ((quantity - 2) * 6)}
shipping_type = "United States Postal Service)"
	break
case "4":
	if (quantity == 1){ shipping_charge = 25.00 }
	else if(quantity > 1){ shipping_charge = 25.00 + ((quantity - 1) * 10) }
	shipping_type = "Air Mail Parcel Post and Variable Weight/Global Priority)"
	//alert(shipping_charge)
	break;	

default:
	errorFlag = true
	break
}

if (errorFlag == false){
	document.getElementById("subtotal").innerHTML = "$" + formatAsMoney(quantity * 29.99)
	document.getElementById("shipping").innerHTML = "$" +  formatAsMoney(shipping_charge)
	document.getElementById("total").innerHTML = "$" + formatAsMoney(shipping_charge + (quantity * 29.99))	

	document.order.shipping.value = formatAsMoney(shipping_charge)
	document.order.item_name.value = "Clearview-F1 Williams FW14B (Book &" + Shipping_Type
	}
}

function submit_check(){
//alert("test")
if ((quantity == "") || shipping_location == "--"){
	alert("You must enter both a Quantity and a Shipping Location.")
	
	return false
	}
}

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);
}
