// JavaScript Document
//find number of decimal places in x
function getDec(xx) {
	var x = xx;
	x = x + ""; //ensure x is a string
	pos = x.indexOf('.') + 1;  //position of first decimal digit
	
	//if 0, then no decimals (-1+1)
	if (pos>0) {
		return x.length-pos;  // number of decimal places in x
	} else {
		return 0;
	}
}//end of getDec(x)

function totalcharge() {
/*
	var numTickets = document.form1.tickets.value;
	var myCountry = document.form1.country.value;
	var result;
	var state = document.form1.state.value;
	var tax = 0;
	var shipping;
	var subtotal;
*/	

	var subtotal = 0;
	var total = 0;
	var tax = 0;
	var shipping = 0;
	var state = document.form1.state.value;
	var quantity_tr = document.form1.quantity_tr.value;
	//var quantity_qr = document.form1.quantity_qr.value;
	var quantity_qr = 0;

/*	
	if (numTickets != 0) {
		//if (myCountry == "USA") {
		if ((myCountry == "USA") || (myCountry == "Canada")) {
			subtotal = parseFloat(numTickets * 12.95);
			subtotal = parseInt((subtotal*100))/100;
			document.form1.subtotal.value = "$" + subtotal;
		
			shipping = parseFloat(numTickets * 2.50);
			shipping = parseInt((shipping*100))/100;
			document.form1.shipping.value = "$" + shipping;
								
			if (state == "WA") {
				result = parseFloat(numTickets) * parseFloat((12.95 + 2.50) * 1.093);
				result = parseInt((result*100))/100;
				document.form1.total.value = "$" + result;
				
				tax = parseFloat(numTickets) * parseFloat((12.95 + 2.50) * .093);
				tax = parseInt((tax*100))/100;
				document.form1.tax.value = "$" + tax;
							
			} else {	
				result = parseFloat(numTickets) * parseFloat(14.95);
				result=parseInt((result*100))/100;
				document.form1.total.value = "$" + result;
				
				tax = parseFloat(numTickets * 0);
				tax = parseInt((tax*100))/100;
				document.form1.tax.value = + tax;				
			}

		} else {
			alert ("Please select a country.");
		}
		
	} else {
		
		document.form1.subtotal.value = 0;
		document.form1.total.value = 0;
		document.form1.tax.value = 0;
		document.form1.shipping.value = 0;
	}	
}
*/

	if(isNaN(quantity_tr)) {
		quantity_tr = 0;
		alert("DSM-IV Tabs quantity must be a number.  Please try again.");
		document.form1.quantity_tr.value = 0;
	}
	if(isNaN(quantity_qr)) {
		quantity_qr = 0;
		alert("Quick Reference tabs quantity must be a number.  Please try again.");
		document.form1.quantity_qr.value = 0;
	}	
	
	if ((quantity_tr !== 0) || (quantity_qr !== 0)) {
	
			subtotal = quantity_tr * 12.95;
			subtotal += quantity_qr * 8.95;
			subtotal = Math.ceil(subtotal * 100)/100;  // round up to two decimal places to fix JavaScript number handling quirk
			if(getDec(subtotal) === 0) {
				document.form1.subtotal.value = "$" + subtotal + ".00";
			} else if(getDec(subtotal) == 1) {
				document.form1.subtotal.value = "$" + subtotal + "0";
			} else {
				document.form1.subtotal.value = "$" + subtotal;
			}
		
			shipping = (quantity_tr * 2.50);
			if(quantity_qr > quantity_tr) {
				shipping += ((quantity_qr - quantity_tr) * 1.25);
			}			
			
			//document.form1.shipping.value = "$" + shipping + ".00";
			if(getDec(shipping) === 0) {
				document.form1.shipping.value = "$" + shipping + ".00";
			} else if(getDec(shipping) == 1) {
				document.form1.shipping.value = "$" + shipping + "0";
			} else {
				document.form1.shipping.value = "$" + shipping;
			}			
								
			if (state == "WA") {
				//total = Math.ceil((quantity_tr * (14.95 * 1.088)) * 100)/100;  // WA charges sales tax on shipping ($12.95 + 2.00 = 14.95)
				total = quantity_tr * 12.95 * 1.093;
				total += quantity_qr * 8.95 * 1.093;
				total += shipping * 1.093;  // WA charges sales tax on shipping
				total = Math.ceil(total * 100)/100;
								
				if(getDec(total) === 0) {
					document.form1.total.value = "$" + total + ".00";
				} else if(getDec(total) == 1) {
					document.form1.total.value = "$" + total + "0";
				} else {
					document.form1.total.value = "$" + total;
				}
				
				//tax = Math.ceil((quantity * (14.95 * 0.088)) * 100)/100;				
				tax = quantity_tr * 12.95 * 0.093;
				tax += quantity_qr * 8.95 * 0.093;
				tax += shipping * 0.093;  // WA charges sales tax on shipping
				tax = Math.ceil(tax * 100)/100;
				if(getDec(tax) === 0) {
					document.form1.tax.value = "$" + tax + ".00";
				} else if(getDec(tax) == 1) {
					document.form1.tax.value = "$" + tax + "0";
				} else {
					document.form1.tax.value = "$" + tax;
				}
							
			} else {
				//total = Math.ceil((quantity * 14.95) * 100)/100;				
				total = quantity_tr * 12.95;
				total += quantity_qr * 8.95;
				total += shipping;
				total = Math.ceil(total * 100)/100;
				if(getDec(total) === 0) {
					document.form1.total.value = "$" + total + ".00";
				} else if(getDec(total) == 1) {
					document.form1.total.value = "$" + total + "0";
				} else {
					document.form1.total.value = "$" + total;
				}

				document.form1.tax.value = 0;
			}
		
	} else {
		
		document.form1.subtotal.value = 0;
		document.form1.total.value = 0;
		document.form1.tax.value = 0;
		document.form1.shipping.value = 0;
	}	
}

function validate(form1) {
	var name = form1.name.value;
	var address = form1.address.value;
	var city = form1.city.value;
	var zip = form1.zip.value;
	var phone = form1.phone.value;
	
	var fieldName = new Array(5);
		fieldName[0] = "Name";
		fieldName[1] = "Address";
		fieldName[2] = "City";
		fieldName[3] = "Zip";
		fieldName[4] = "Phone";		
		
	var fieldValue = new Array(5);
		fieldValue[0] = name;
		fieldValue[1] = address;
		fieldValue[2] = city;
		fieldValue[3] = zip;
		fieldValue[4] = phone;		
	
	for (i=0; i<5; i++) {
		if (fieldValue[i] == "") {
			alert(fieldName[i] + " is a required field. Please try again.");
			//document.form1.fieldName[i].focus();
			return false;			
		}
	}
	
	var state = form1.state.value;
	var quantity_tr = form1.quantity_tr.value;
	
	var dropBox = new Array(2);
		dropBox[0] = "State";
		dropBox[1] = "Quantity";
		
	var dropBoxValue = new Array(2);
		dropBoxValue[0] = state;
		dropBoxValue[1] = quantity_tr;	
	
	for (i=0; i<2; i++) {
		if (dropBoxValue[i] == 0) {
			alert(dropBox[i] + " is a required selection. Please try again.");
			return false;
		}
	}
}
