// JavaScript Document
var theValues = new Array();
theValues["quantity1"] = 0;
theValues["quantity2"] = 0;
theValues["quantity3"] = 0;
theValues["extprice1"] = 0;
theValues["extprice2"] = 0;
theValues["extprice3"] = 0;
theValues["subtotal"] = 0;
theValues["shipping"] = 0;
theValues["grandtotal"] = 0;

function calcSubTotal() {
	if(document.orderform.check1.checked == false) 
		theValues["extprice1"] = 0;
	if(document.orderform.check2.checked == false) 
		theValues["extprice2"] = 0;
	if(document.orderform.check3.checked == false) 
		theValues["extprice3"] = 0;
	
	theValues["subtotal"] = theValues["extprice1"] + theValues["extprice2"] + theValues["extprice3"];
	showSubTotal();
}

function calcGrandTotal() {
	theValues["grandtotal"] = theValues["subtotal"] + theValues["shipping"];
	showGrandTotal();
}

function calcExtPrice (level) {
	switch(level) {
		case 1:
			if((document.orderform.check1.checked == false) && (document.orderform.quant1.value != 0)) {
				alert ("If you want this item, please check the items's checkbox.");
				document.orderform.quant1.value = 0;
				theValues["quantity1"] = parseInt(document.orderform.quant1.value);
				}
			else {
				if((document.orderform.quant1.value == "") || (document.orderform.quant1.value < 0)) {
					alert ("Please enter a positive number of items");
					document.orderform.quant1.value = 0;
					theValues["quantity1"] = parseInt(document.orderform.quant1.value);
				}
				else {
					theValues["quantity1"] = parseInt(document.orderform.quant1.value);
					theValues["extprice1"] = 20.95 * theValues["quantity1"];
				}
			}
		break;
		case 2:
			if((document.orderform.check2.checked == false) && (document.orderform.quant2.value != 0)) {
				alert ("If you want this item, please check the items's checkbox.");
				document.orderform.quant2.value = 0;
				theValues["quantity2"] = parseInt(document.orderform.quant2.value);
				}
			else {
				if((document.orderform.quant2.value == "") || (document.orderform.quant2.value < 0)) {
					alert ("Please enter a positive number of items");
					document.orderform.quant2.value = 0;
					theValues["quantity2"] = parseInt(document.orderform.quant2.value);
				}
				else {
					theValues["quantity2"] = parseInt(document.orderform.quant2.value);
					theValues["extprice2"] = 32.95 * theValues["quantity2"];
				}
			}
		break;
		case 3:
			if((document.orderform.check3.checked == false) && (document.orderform.quant3.value != 0)) {
				alert ("If you want this item, please check the items's checkbox.");
				document.orderform.quant3.value = 0;
			    theValues["quantity3"] = parseInt(document.orderform.quant3.value);
				}
			else {
				if((document.orderform.quant3.value == "") || (document.orderform.quant3.value < 0)) {
					alert ("Please enter a positive number of items");
					document.orderform.quant3.value = 0;
					theValues["quantity3"] = parseInt(document.orderform.quant3.value);
				}
				else {
					theValues["quantity3"] = parseInt(document.orderform.quant3.value);
					theValues["extprice3"] = 17.59 * theValues["quantity3"];
				}
			}
		break;
	} // end switch
	showExtPrice();
	calcSubTotal();
	calcshipping();
	calcGrandTotal();
	showInfo();
}

function calcShipping() {
	var x = theValues["quantity1"];
	var y = theValues["quantity2"];
	var z = theValues["quantity3"];
	
	var total = (x + y + z);
	
	if( total == 1) {
		theValues["shipping"] = 5.00;
	}
	else if(total == 0) {
		theValues["shipping"] = 0;
	}
	else {
		theValues["shipping"] = 5.00 + (3.00 * (total - 1.00));
	}
	
	showShipping();	
}

function showInfo() {
	showExtPrice();
	showSubTotal();
	showShipping();
	showGrandTotal();
}

function showShipping() {
	document.orderform.shipping.value = theValues["shipping"];
	calcGrandTotal();
}

function showExtPrice() {
		if(document.orderform.check1.checked == true) {
			document.orderform.extprice1.value = Math.round(theValues["extprice1"] * 100)/ 100;
		}
		else {
			document.orderform.extprice1.value = 0;
		}
		
		if(document.orderform.check2.checked == true) {
			document.orderform.extprice2.value = Math.round(theValues["extprice2"] * 100) / 100;
		}
		else {
			document.orderform.extprice2.value = 0;
		}
		
		if(document.orderform.check3.checked == true) {
			document.orderform.extprice3.value = Math.round(theValues["extprice3"] * 100) / 100;
		}
		else {
			document.orderform.extprice3.value = 0;
		}
		calcSubTotal();
}

function showSubTotal() {		
		document.orderform.subtotal.value = Math.round(theValues["subtotal"] * 100) / 100;
		calcShipping();
}

function showGrandTotal() {
		document.orderform.orderTotal.value = Math.round(theValues["grandtotal"] * 100) / 100;
}

function getSelect(num) {
	switch(num) {
		case 1:
			document.orderform.quant1.select();
			break;
		case 2:
			document.orderform.quant2.select();
			break;
		case 3:
			document.orderform.quant3.select();
			break;
	}
}
	


