// TheSTUDIO.net order form JavaScript
// written by Schuyler Erle
// last mod 31 May 2000

var productTag  = "product",
    shippingTag = "shipping";

function toCurrency (v) {
    var w = Math.floor(v * 100 + .999) / 100;
    var x = w.toString(); w = x.indexOf(".");

    if (w == -1)
	x += ".00";
    else if (w == x.length - 2)
	x += "0";
 
    return x;
}

function getProduct (elem, name, num) {
    if (!num) num = elem.name.substring( productTag.length, elem.name.indexOf("_") );
    return elem.form.elements[ productTag + num + "_" + name ];
}

function getShipping (elem, name, num) {
    if (!num) num = elem.name.substring( shippingTag.length, elem.name.indexOf("_") );
    return elem.form.elements[ shippingTag + num + "_" + name ];
}

function calculateTotal (elem) {
    var units = new Object;

    with (elem.form) {
	var subtotal = 0, ship = 0, i;

	for (i = 1; i <= numProducts.value; i++) {
	    var check = getProduct(elem, "check", i),
		qty   = getProduct(elem, "qty", i),
		tot   = getProduct(elem, "total", i),
		cost  = getProduct(elem, "cost", i)
		snh   = getProduct(elem, "ship", i);

	    if (qty.value < 1 || isNaN(parseInt(qty.value))) {
		qty.value = 0;
		if (check) check.checked = false;
	    } else {
		qty.value = Math.floor(qty.value);
		if (check) check.checked = true;
	    }

	    tot.value = toCurrency( qty.value * cost.value );
	    subtotal += parseFloat( tot.value );

	    if (!units[snh.value])
		units[snh.value]  = parseInt( qty.value );
	    else 
	    	units[snh.value] += parseInt( qty.value ); 
	}

	tax.value = toCurrency(
	    (state.options[state.selectedIndex].value.toUpperCase() 
		== salesState.value.toUpperCase()) ? subtotal * salesTax.value : 0);

	for (var n in units) {
	    // alert("shipping " + n + " : " + units[n]);
	    if (units[n]) {
		var base  = getShipping(elem, "base",  n), 
		    extra = getShipping(elem, "extra", n);
		ship += parseFloat(base.value) + parseFloat(extra.value) * (units[n] - 1);
	    }
	}

	shipping.value = toCurrency( ship ? ship : 0 );
	total.value = toCurrency( subtotal ? 1 * subtotal + 1 * tax.value + 1 * shipping.value : 0 );
    }
    return true;
}

function toggleProduct (check) {
    var qty = getProduct(check, "qty");

    if (check.checked) {
	if (qty.value < 1) qty.value = qty.defaultValue || 1;
    } else {
	qty.defaultValue = qty.value;
	qty.value = 0;
    }
    return calculateTotal(check);
}

function validateForm (f) {	
    if (!f.first.value)
        alert("You must supply a valid First Name.");
    else if (!f.last.value)
        alert("You must supply a valid Last Name.");
    else if (!f.address.value)
        alert("You must supply a valid Shipping Address.");
    else if (!f.city.value)
        alert("You must supply a valid City.");
    else if (!f.zip.value)
        alert("You must supply a valid Zip Code.");
    else if (!f.card_num.value)
        alert("You must supply a valid credit card number.");
    else if (!f.card_month.options[f.card_month.selectedIndex].value || 
	!f.card_year.options[f.card_year.selectedIndex].value)
	alert("You must supply a valid credit card expiration date.");
    else return true;
    return false;
}

