// You are welcome to use this code.  Some of this I borrowed from others.  But a lot of it
// I had to rewrite to make the numbers line up etc. - check out CheckDigits.  
// Dena Ringen   email: dena@eurekanet.org

var localTax;

// validate item quantities and keep a running total
function validateQuantity(num, price)
{     
	var numProdct = eval("document.products.item" +thing[num] +".value");
	if(IsNumber(numProdct,2)){
		var total=eval(numProdct * price);
		//alert('Price = ' +price);
		//alert('numProdct = ' +numProdct);
	}
	else var total = 0; 
	total = CheckDigits(total/100); 
	var prodTotal = eval("document.products.item_total"+ thing[num]);
	prodTotal.value = total;
}    

//Calculate shipping handling charges and total.
function CalculateTotal()
{
	var softShipping =0
	var paperShipping =0
	var grandTotal =0
	var subTotal =0

  if(eval(CheckTax()) != -1) {
	for(i=1;i<=numProducts;i++){
		prodTotal = eval("document.products.item_total"+ thing[i] +".value")
		if(eval(prodTotal)){ 
			subTotal = eval(eval(subTotal) + eval(prodTotal))
		}
		//	if(i<numSoftware){ //if this is software 5% shipping/handling
		//	 softShipping = eval(eval(softShipping) + eval(prodTotal) * 0.05) 
		//  } //  else 12% handling
		//	else paperShipping = eval(eval(paperShipping) + eval(prodTotal) * 0.12)
		//}
	}
	//totalShipping = eval(softShipping + paperShipping)
	//if (totalShipping < 6.00) totalShipping = 6.00
	document.products.sub_total.value = CheckDigits(subTotal)
	if (California == -1){
		var tax = eval(subTotal * localTax)
		tax = tax/10000
	}	
	else tax = 0;
	document.products.tax.value = "$" + CheckDigits(tax)
	//document.products.shipping.value = "$" + CheckDigits(totalShipping)
	//document.products.total_cost.value = "$" + CheckDigits(eval(eval(tax) + eval(subTotal) + eval(totalShipping)))
	document.products.total_cost.value = "$" + CheckDigits(eval(eval(tax) + eval(subTotal)))
  }
}//calculate 

//Verify they've selected a county.  Set the tax rate accordingly.
function CheckTax()
{
	var countyTax = new Array(0,0,800,700,850,700,700,700,800,750,792.5,700,800,700,700,762.5,700,700,750,750,700,700,700,700,800,750,700,750,700,700,700,700,700,750,712.5,750,700,725,700,750,750,700,750,750,825,750,700,800,750,800,775,700,700,700,712.5,725,712.5,700,700,700,762.5,700,700,700,750,700,700);

	if(document.products.county.selectedIndex==0){
		alert('Please select your county so I can calculate your taxes.')
		return -1;
		//return 0;
		}
	index = document.products.county.selectedIndex;
//alert('index = ' +index);
	localTax = countyTax[index];
//alert('localTax rate is ' +localTax);
	return localTax;
}

function IsNumber(strFieldValue, size)
{
	for ( var i=0; i < size; i++){
		if(strFieldValue.charAt(i)!=""){
			if (strFieldValue.charAt(i) < "0" || strFieldValue.charAt(i) > "9"){
            			alert("Please enter a number")
				return false;        
			}
		}
	}
	return true    
}

//This verifies that the cents will come out properly.  Dena Ringen
function CheckDigits(price)
{
	var i = 0
	price = String(price)
	var strLength = price.length;
	decimalPt = price.indexOf(".",0)

	if (decimalPt == -1){			//has no decimal point, so add .00
		return (eval(price) + ".00")
	}

	if(decimalPt == 0){
		price = "0" + eval(price)
		decimalPt = 1
	}
	if(price.length == decimalPt + 2){	// in xx.x
		return (eval(price) + "0")
	}

	if(price.length == decimalPt + 3){	//already in x.xx format
		return price
	}
	
	else {					//cut off trailing numbers
		a = price.charAt(decimalPt +1)
		b = price.charAt(decimalPt +2)
		price = parseInt(price)
		price = eval(price + "." + a + b)
		if (b == 0) return (eval(price) + "0")
		return price
	}
}


//get and validate form data	
function Validate()
{	
  	isValid = true  	  
	if (f.total_cost.value == "$0"){	
    		alert("Please choose an item")
            	isValid = false          
	}
	if(isValid){	
    		IsNotEmpty(f.customer.value,	"Please enter your name")
		IsNotEmpty(f.address.value,	"Please enter your address")
		IsNotEmpty(f.city.value,	"Please enter a city")
		IsNotEmpty(f.state.value,	"Please enter a state")
		if(isValid){
			CheckLength(f.state.value,  2, "Invalid state abbreviation") 
	        }	
		IsNotEmpty(f.zip.value,	"Please enter a zip code")
		if(isValid){
			CheckLength(f.zip.value,  5, "Invalid zip code")            
		}
		if(isValid){
			IsNonZeroNumber(f.zip.value, 5, "Invalid zip code")
	        }
	}
	if (isValid) get_items()

	//code to upload your order to the server goes here   	
}


