// Code for calculating total price
function totalPriceForCheckboxes() {
	var totalPrice = 0;
	
    /* iterate through all the elements in the checkbox array */
	for(i=0;i<document.forms[0].elements.length;i++) {
		if(document.forms[0].elements[i].type == 'checkbox' &&
			document.forms[0].elements[i].checked) {
		  var price = parseFloat(document.forms[0].elements[i].value);
		  var quantity = parseInt(document.forms[0].elements[i+1].value);
		  
		  if ((isNaN(quantity)) || (quantity == 0)) {
		  	quantity = 1;
		  }
		  
		  document.forms[0].elements[i+1].value = quantity;
		  
		  totalPrice += price * quantity;
		}
	}
	
	return (totalPrice);
}

function parseCurrency(floatValue) {
	var currencyValue = floatValue+"";
	var prefix="$";
	var wd;
	
	if (currencyValue.charAt(0)==prefix)
		return;

	wd="w";
	var tempnum=currencyValue;
	
	for (i=0;i<tempnum.length;i++) {
		if (tempnum.charAt(i)==".") {
			wd="d";
			break;
		}
	}

	if (wd=="w")
		currencyValue=prefix+tempnum+".00";
	else {
		if (tempnum.charAt(tempnum.length-2)==".") {
			currencyValue=prefix+tempnum+"0";
		}
		else {
			tempnum=Math.round(tempnum*100)/100;
			currencyValue=prefix+tempnum;
		}
	}
	
	return (currencyValue);
}

function updateTotals() {
	var subTotal;
	var shipping;
	var orderTotal;
	
	subTotal = totalPriceForCheckboxes("orderForm");
	//temporary global discount of 25% from 3/28/2005 through 7/31/2005
	//subTotal = subTotal * 0.75;

	shipping = subTotal * .05;
	orderTotal = subTotal + shipping;
	
	document.forms[0].subTotal.value = parseCurrency(subTotal);
	document.forms[0].shipping.value = parseCurrency(shipping);
	document.forms[0].total.value = parseCurrency(orderTotal);
}

// Sets up each of the checkboxes in a form to call totalPriceForCheckout
// when clicked
function setFormEvents () {
	for(i=0; i<document.forms[0].elements.length;i++) {
		var formControl = document.forms[0].elements[i];
		if (formControl.type == "checkbox") {
			formControl.onclick = updateTotals;
		} if (formControl.type == "text") {
			var prevControl = document.forms[0].elements[i-1]
			if (prevControl.type == "checkbox") {
				formControl.onblur = updateTotals;
			}
		}
	}
}

function fixCheckboxes () {
	for(i=0; i<document.forms[0].elements.length;i++) {
		var formControl = document.forms[0].elements[i];
		if (formControl.type == "checkbox") {
			formControl.onclick = updateTotals;
			if (!formControl.checked) {
				if (document.forms[0].elements[i+1].value.length > 0) {
					formControl.checked = true;
				}
			}
		}
	}
}

function fixFormElements () {
	for(i=0; i<document.forms[0].elements.length;i++) {
		var formControl = document.forms[0].elements[i];
		
		if (formControl.type == "checkbox") { 
			if (!formControl.checked) {
				document.forms[0].elements[i+1].value = "";
			} else {
				formControl.checked = false;
			}
		}
	}
	
	return true;
}

function setSubject() {
	document.forms[0].subject.value = document.forms[0].subject.value + document.forms[0].realname.value + ' (' + document.forms[0].school.value + ')';
}

function validateForm() {
	if (document.forms[0].realname.value.length == 0) {
		alert("Please enter your name!");
		return false;
	}
	
	if (document.forms[0].email.value.length == 0) {
		alert("Please enter your email!");
		return false;
	}

	if (document.forms[0].shippingAddress.value.length == 0) {
		alert("Please enter a valid shipping address!");
		return false;
	}

	if (document.forms[0].phone.value.length == 0) {
		alert("Please enter a valid phone number!");
		return false;
	}
	
	return true;
}

function submitForm() {
	if (!validateForm()) {
		return false;
	}
	
	if (!fixFormElements()) {
		return false;
	}
	
	setSubject();
	
	return true;
}
