// summary.js

// Calculate sales tax and totals for Order Summary form.

// Defined in setup.js:
// var isW3C = (document.getElementById) ? true : false;
// var isAll = (document.all) ? true : false;

var currency, currencyMask = /^\s*[^\s\d\.,]/, zeroes = new String('000');

function getElem(elemID) {
    return isW3C ? document.getElementById(elemID) : (isAll ? document.all[elemID] : null);
}

// The next two functions assumes elem is empty or contains only text
function getText(elem) {
	if (elem) {
		if (isW3C && elem.hasChildNodes() && elem.firstChild.nodeType == 3)
			return elem.firstChild.nodeValue
		else if (isAll)
			return elem.innerText;
		}
	else return '';
}

function setText(elem,value) {
	if (elem) {
		if (isW3C) {
			if (elem.hasChildNodes() && elem.firstChild.nodeType == 3) elem.firstChild.nodeValue = value
			else {
				node = document.createTextNode(value);
				if (node) elem.appendChild(node);
			}
		}
		else if (isAll) elem.innerText=value;
	}
}

function getAmount(elemID) {
	var elem = getElem(elemID);
	if (elem) {
		var str = getText(elem);
		if (str) return parseFloat(str.replace(currencyMask,''));
	}
	return 0;
}

function getCurrency() {
	if (typeof currency == 'undefined') {
		var elem = getElem('subtotal');
		var str = getText(elem);
		if (str) {
			var i = str.search(currencyMask);
			currency = (i<0) ? '' : str.charAt(i);
		} else currency = '';
	}
	return currency;
}

function fmtCurrency(amount) {
	var cents = new String(Math.round(parseFloat(amount) * 100));
	cents = zeroes.substring(cents.length) + cents;
	return cents.substring(0,cents.length-2) + '.' + cents.substring(cents.length-2);
}

function getTaxRate() {
	with (document.forms.taxship) {
		if (SH[5].checked) return 0;		// 5 is index of International button
		if (State.selectedIndex > -1) {
			var state = State.options[State.selectedIndex].value;
			return (state=='CA') ? 0.0775 : 0;
		}
		return 0;
	}
}

function getShipCost() {
	var ShipBy = 'Priority Mail=6.00';
	with (document.forms.taxship) {
		for (var i=0; i<SH.length; i++) {
			if (SH[i].checked) {
				ShipBy = SH[i].value;
				break;
			}
		}
	}
	return parseFloat(ShipBy.split('=')[1]);
}

function updateTotal() {
	var subtotal = getAmount('subtotal');
	var salestax = subtotal * getTaxRate();
	var shipping = getShipCost();
	setText(getElem('salestax'), getCurrency() + fmtCurrency(salestax));
	setText(getElem('shipping'), getCurrency() + fmtCurrency(shipping));
	setText(getElem('total'), getCurrency() + fmtCurrency(subtotal + salestax + shipping));
}

function shipDomestic() {
// 	document.forms.taxship.elements['Outside NA'].value = '';
	updateTotal();
}

function shipIntl() {
// 	document.forms.taxship.elements['Outside NA'].focus();
	updateTotal();
}

// function countryChanged() {
// 	var country = document.forms.taxship.elements['Outside NA'].value;
// 	if (country.search(/\w/) > -1) {
// 		document.forms.taxship.SH[4].checked = true;
// 		updateShipping();
// 		updateTotal();
// 	}
// }

function sb2(url) {
	document.forms.taxship.destination.value = url;
	document.forms.taxship.submit();
	return false;
}

