// Functions specific to the price calc on building a snow globe
// order sub-total
function calcNewOrderSubTotal() {
	// get items
	var baseCost = document.getElementById('baseCost').innerHTML;
	var premiumBaseCost = document.getElementById('premiumBaseCost').innerHTML;
	var extraConfettiCost = document.getElementById('extraConfettiCost').innerHTML;
	var customPlateCost = document.getElementById('customPlateCost').innerHTML;
	var customSecondPlateCost = document.getElementById('customSecondPlateCost').innerHTML;
	var shippingCost = document.getElementById('shippingCost').innerHTML;

	// get globe quantity
	var snowGlobeCount = 1;
	if (document.getElementById('snowGlobeCount')) {
		// get count
		var selObj = document.getElementById('snowGlobeCount');
		var selIndex = selObj.selectedIndex;
		snowGlobeCount = selObj.options[selIndex].value;
	}
	document.getElementById('snowGlobeCountDisplay').innerHTML = snowGlobeCount;
	
	// check shipping price
	if (shippingCost == '0.00') {
		shippingCost = 0;	
	}

	// calculate new price
	var newSubTotal = (parseFloat(baseCost) + parseFloat(premiumBaseCost) + parseFloat(extraConfettiCost) + parseFloat(customPlateCost) + parseFloat(customSecondPlateCost)) * snowGlobeCount;
	var newGrandTotal = newSubTotal + parseFloat(shippingCost);
	newSubTotal = newSubTotal.toFixed(2);
	newGrandTotal = newGrandTotal.toFixed(2);
	
	// change inner html on orderSubTotal
	document.getElementById('orderSubTotal').innerHTML = newSubTotal;
	document.getElementById('orderGrandTotal').innerHTML = newGrandTotal;
}
// used for custom plate cost, if any, and premium base cost, if any
function updateItemCosts(newItemCost, checkoutSpanID) {
	// format newItemCost as number
	var newItemCostFormatted = parseFloat(newItemCost).toFixed(2);
	
	// inner html on newItemCost
	document.getElementById(checkoutSpanID).innerHTML = newItemCostFormatted;
	
	// update sub-total
	calcNewOrderSubTotal();
}
function updateConfettiCosts(confettiCheckStatus, currentSubTotal, confettiPrice) {
	// clear confettit total
	var confettiTotal = 0;
	
	// get new count
	if (confettiCheckStatus) {
		confettiSelectionCount+=1;
	} else {
		confettiSelectionCount-=1;
	}
	
	// calc price
	if(confettiSelectionCount > 2)	{
		confettiTotal = (parseInt(confettiSelectionCount - 2)) * parseFloat(confettiPrice);
	}
	
	// format
	var confettiTotalFormatted = confettiTotal.toFixed(2);
	
	// update confetti price
	document.getElementById("extraConfettiCost").innerHTML = confettiTotalFormatted;	
	
	// update sub-total
	calcNewOrderSubTotal();
}
function getShippingPrice() {
	// get snow globe count
	var selObj = document.getElementById('snowGlobeCount');
	var selIndex = selObj.selectedIndex;
	var snowGlobeCount = selObj.options[selIndex].value;
	
	// get shipping type
	for (var i=0; i < document.forms["form1"].shipment.length; i++) {
	   if (document.forms["form1"].shipment[i].checked) {
		  var shippingType = document.forms["form1"].shipment[i].value;
	   }
	}
	
	// figure out correct price to display
	var shippingCost = 0;
	var array_key = "";
	if (shippingType == 'ground') {
		array_key = snowGlobeCount+"_upsGround";
		updateHideInternational();
	} else if (shippingType == '3Day') {
		array_key = snowGlobeCount+"_ups3Day";
		updateHideInternational();
	} else if (shippingType == '2ndDay') {
		array_key = snowGlobeCount+"_ups2ndDay";
		updateHideInternational();
	} else if (shippingType == 'International') { 
		// call international change function
		// international is only 1-4 globes so make sure it is less than 5 or reset and show error
		// change fields for international
		updateShowInternational(snowGlobeCount);
		
		// now set price
		array_key = snowGlobeCount+"_International";
	}
	shippingCost = snowglobeshippingcosts[array_key];
	
	// update prices in radio
	document.getElementById('upsGroundPrice').innerHTML = snowglobeshippingcosts[snowGlobeCount+"_upsGround"];
	document.getElementById('ups3DayPrice').innerHTML = snowglobeshippingcosts[snowGlobeCount+"_ups3Day"];
	document.getElementById('2ndDayPrice').innerHTML = snowglobeshippingcosts[snowGlobeCount+"_ups2ndDay"];
	document.getElementById('International').innerHTML = snowglobeshippingcosts[snowGlobeCount+"_International"];	

	// hide international if globe count is over 4
	// do not hide if the user has already selected international - the error messages will catch that
	if (snowGlobeCount > 4 && shippingType != 'International') {
		hideDiv('intlshippingoption');
	} else {
		showDiv('intlshippingoption');
	}

	// update price
	updateItemCosts(shippingCost, 'shippingCost');
}
function updateShowInternational(snowGlobeCount) {
	// check multiple globes, if more than 5 show error and reset globe count to 4
	if (snowGlobeCount > 5) {
		// show error
		showDiv('intlmultierror');
		// update globe count
		  var selectBox = document.getElementById('snowGlobeCount');
		  selectedItem = selectBox[selectBox.selectedIndex];
		  selectedItem.value = 4;
	}
	// hide domestic fields
	hideDiv('shipping_state_row');
	hideDiv('shipping_zip_row');

	// show international fields
	showTableRow('shipping_providence_row');
	showTableRow('shipping_country_row');
	showTableRow('shipping_postal_code_row');
	
}
function updateHideInternational() {
	// show domestic fields
	showTableRow('shipping_state_row');
	showTableRow('shipping_zip_row');
	// hide international fields
	hideDiv('shipping_providence_row');
	hideDiv('shipping_country_row');
	hideDiv('shipping_postal_code_row');	
}