taxRate = .1;

function calculateExclusive(amount, taxRate) {
	return roundAndPad(amount / (1 + taxRate),2);
}

function calculateTax(inclusive, exclusive) {
	return inclusive - exclusive;
}

function calculateOrder() {
	orderTotal = 0;
	taxTotal = 0;
	currentItem = 1;
	giftIdentifier = 'Gift';
	quantityGifts = 0;
	quantityOther = 0;
	orderValid = false;
	for (repetition = 1; repetition <= entryCount; repetition++) {
		theQuantity = document.getElementById('quantity_' + repetition).value;
		theQuantity = Math.round(theQuantity);
		document.getElementById('quantity_' + repetition).value = theQuantity;
		theAmountInclusive = document.getElementById('product_inclusive_' + repetition).value;
		theTax = document.getElementById('product_tax_' + repetition).value;
		theSubtotal = theQuantity * theAmountInclusive;
		orderTotal = orderTotal + theSubtotal;
		document.getElementById('subtotal_' + repetition).innerHTML = roundAndPad(theSubtotal);
		if (document.getElementById('quantity_' + repetition).value >= 1) {
			if (document.getElementById('item_name_' + repetition).value.substr(0,giftIdentifier.length) == giftIdentifier) {
				quantityGifts += theQuantity;
			} else {
				quantityOther += theQuantity;
			}
			document.getElementById('item_name_' + repetition).name = 'item_name_' + currentItem;
			document.getElementById('quantity_' + repetition).name = 'quantity_' + currentItem;
			document.getElementById('amount_' + repetition).name = 'amount_' + currentItem;
			taxTotal = taxTotal + (theQuantity * theTax);
			orderValid = true;
			currentItem ++;
		} else {
			document.getElementById('item_name_' + repetition).name = '';
			document.getElementById('quantity_' + repetition).name = '';
			document.getElementById('amount_' + repetition).name = '';
		}
	}
	if (quantityGifts) {
		shippingInclusive = 11;
		if (quantityGifts > 1) {
			shippingInclusive += (quantityGifts - 1) * 6
		}
		if (quantityOther > 0) {
			shippingInclusive += (quantityOther * 2);
		}
	} else if (quantityOther) {
		shippingInclusive = 6;
		if (quantityOther > 1) {
			shippingInclusive += (quantityOther - 1) * 2
		}
	} else {
		shippingInclusive = 0;
	}
	
	if (shippingInclusive > 30) {
		shippingInclusive = 30;
	}

	if (orderTotal >= 200) {
		shippingInclusive = 0;
	} else if (orderTotal == 0) {
		shippingInclusive = 0;
	} else {
		shippingInclusive = 13;
	}
	
	
	shippingExclusive = roundAndPad(calculateExclusive(shippingInclusive, taxRate));
	shippingTax = calculateTax(shippingInclusive, shippingExclusive);
	document.getElementById('shipping_1').value = shippingExclusive;
	document.getElementById('shipping_shown').innerHTML = roundAndPad(shippingInclusive);
	orderTotal = orderTotal + shippingInclusive;
	taxTotal = taxTotal + shippingTax;
		
	document.getElementById('tax_cart').value = roundAndPad(taxTotal);
	document.getElementById('total').innerHTML = roundAndPad(orderTotal);
}

function payOrder() {
	calculateOrder();
	if (orderValid) {
		return true;
	} else {
		alert('Please enter a quantity for each item you want to order.');
		return false;
	}
}

function roundAndPad(theNumber) {
	theInflatedNumber = Math.round(theNumber * 100)
	theRoundedNumber = theInflatedNumber / 100;
	theNumberString = theInflatedNumber.toString();
	if (theRoundedNumber == 0) {
		thePaddedNumber = '0.00';
	} else if (theNumberString.substr(theNumberString.length - 2,2) == '00') {
		thePaddedNumber = theRoundedNumber.toString() + '.00';
	} else if (theNumberString.substr(theNumberString.length - 1,2) == '0') {
		thePaddedNumber = theRoundedNumber.toString() + '0';
	} else {
		thePaddedNumber = theRoundedNumber;
	}
	return thePaddedNumber;
}