// JavaScript Document
// Author: Kir Talmage (www.metasilkwebworks.com), 2006.
// Feel free to adapt this to your use. Credit would be lovely. So would feedback!

	// declare all variables
	var c = 0  // cost, calculated
	var v = 0  // vermont sales tax, calculated
	var h = 0  // shipping and handling, calculated
	var q = 0 // total quantities, calculated
	var sc = 0 // subtotal cost, calculated
	var tc = 0 // total cost, calculated
	
	function calcCost(form) {
	// Step 1: is there enough data? Check the quantity field
	// When she has more books, we'll need to do this for all the future quantity fields
		// first, makes sure it's numeric 
		var regex = /^[a-zA-Z]+$/ // match nonnumeric input
		if (regex.test(form.StratQuant.value) == true ) {
			alert("Please enter a number in the Quantity box. Thanks.");
			form.StratQuant.focus();
			return false; // don't go on
		}
		else {
			// if it's blank, make it zero
			if ( (form.StratQuant.value == null) || (form.StratQuant.value == "") ) {
				form.StratQuant.value = 0.00; 
				}
			// then calculate the cost
			c = (form.StratPriceEach.value*parseInt(form.StratQuant.value));
			form.StratCost.value = c.toFixed(2); 
		}
	
	// Step 2: Calculate the total quantities.	
		//alert("q = " + q );
		q = parseInt(form.StratQuant.value) ; // there would be a proper sum when there are more books
		form.TotQuant.value = q ;
		
	// Step 3: Calculate the subtotal cost
		sc = c; // with more books, we will have a sum here.
	
	// Step 4: Calculate the sales tax only on the book subtotal, not the S&H
		// if they're a Vermonter, they get sales tax
		if ( (form.VTer.checked == true )) {
			v = 0.07*c;  // c would later be replaced by sc when there are more books
			form.VTTax.value = v.toFixed(2);
		}
		// As you may know, Burlington's new 1% sales tax goes into effect July 1, 2006.  
		// All Vermont customers will be subject to this tax because I, the book vendor, 
		// sell from Burlington.   
		// VT tax line assesses VT buyers for 7%, and then say, perhaps in parenthesis, 
		//  (6% state tax; 1% Burlington City tax) ?  
		else {
			v = 0;
			form.VTTax.value = v.toFixed(2);
		}
		
	// Step 5: Calculate the shipping and handling.	
		if ( q == 1) {
			h = 4.50;
			form.ShipHandling.value = h.toFixed(2);
		}
		else if ( q > 1 && q < 5 ) {
			h = 6.50;
			form.ShipHandling.value = h.toFixed(2);
		}
		else if ( q > 4 && q < 11 ) {
			h = 9.50;
			form.ShipHandling.value = h.toFixed(2);
		}
		else if ( q > 10 && q < 21 ) {
			h = 11.00;
			form.ShipHandling.value = h.toFixed(2);
		}
		else if ( q > 20) {
			h = 13.00;
			//5.00 + (1.50*(q-4));
			form.ShipHandling.value = h.toFixed(2);
		}
		else {
			alert("All book quantities are 0.");
			h = 0.00;
			form.ShipHandling.value = h.toFixed(2);
		}

	// Step 6: Calculate the total cost.
		// Total costs	: TotCost (tc)
		tc = sc + v + h ;
		form.TotCost.value = tc.toFixed(2) ;
	}