var firstTime=true;

function currency(num) {  // by D. Goodman, modified
  str = "" + Math.round(num * 100)
  decpoint = str.length - 2
  return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length)
}


function validate(detailsForm) {
	errors = "";
	f = document.getElementById("cateringForm");
	kitchener = (f.Deliver_To_Street) ? true : false;
	if ( f.Phone.value.length < ((kitchener) ? 7 : 3) ) { //allow only extension number in Wloo
		errors += "Please supply a valid phone number.\n";
	}
	if (f.realname.value.length < 2) {
		errors += "Please supply the name of the person to contact about this order.\n";
	}
	if (kitchener) {
		if ( (f.Event_Date.value.length < 3) || (f.Delivery_or_PickUp_Time.value.length < 1) ) {
			errors += "Please enter the date and time of your event (or pick-up time).\n";
		}	
		if (f.PickUp_or_Delivery[0].checked==false && f.PickUp_or_Delivery[1].checked==false) {
			errors += "Please select pick-up or delivery.\n";
		}
		if (f.PickUp_or_Delivery[0].checked) {
			if (f.Deliver_To_Street.value.length < 3 || f.Deliver_To_City.value.length < 2) {
				errors += "Please complete the delivery address.\n";
			}
			if (f.Deliver_To_Attention.value.length < 3 && f.Deliver_To_Company.value.length < 3) {
				errors += "Please supply a company or person's name to deliver to the attention of.\n";
			}
		} else { //pick up
			if (f.Bill_To_Street.value.length < 3 || f.Bill_To_City.value.length < 2) {
				errors += "Please complete the billing address.\n";
			}
			if (f.Bill_To_Attention.value.length < 3 && f.Bill_To_Company.value.length < 3) {
				errors += "Please supply a company or person's name to bill to the attention of.\n";
			}
		}
	} else { // waterloo only
		if (f.Event_Date.value.length < 3) {
			errors += "Please enter the date and time of your event.\n";
		}
		if (f.Deliver_To_Room_or_Dept.value.length < 1) {
			errors += "Please enter the room number or department where you would like the order delivered to.\n";
		}
	}
	if (detailsForm.Total.value == "" || detailsForm.Total.value == 0) {
		errors += "You need to order at least one item.";
	}

	if (errors != "") {
		location.hash = "header"; //scroll to top
		alert(errors);
		return false;
	} else {	
		f.Items_Ordered.value = "\n  ";
		for (i=0; i<detailsForm.elements.length; i++) { 
			elem=detailsForm.elements[i];
			if (elem.name.indexOf("Price")>0) {
				price = elem.value;
				i++; 
				quan = detailsForm.elements[i].value; 
				if (quan != "") { 
					f.Items_Ordered.value += detailsForm.elements[i].name	+ ": " + quan + " @ $" + price + "\n  ";
				}
			} // end if
		} // end for
		if(kitchener) f.Delivery.value = "$" + detailsForm.Delivery.value;
		f.Subtotal.value = "$" + detailsForm.Subtotal.value;
		f.Taxes.value = "$" + detailsForm.Taxes.value;
		f.Total.value = "$" + detailsForm.Total.value;
		f.submit();
	}
}


function insertDate() {
	f = document.getElementById('cateringForm');
	months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	today=new Date();
	f.Order_Date.value=today.getDate()+"-"+months[today.getMonth()]+"-"+today.getFullYear();
	f.Order_Date.readOnly = true;
}

function calcTotals() {
	var i, elem, price, quan, del, tax;
	var water = 0, subt = 0, f = document.getElementById('orderDetails');
	for (i=0; i<f.elements.length; i++) {
		elem=f.elements[i];
		if (elem.name.indexOf("Price")>0) {
			price = parseFloat(elem.value);
			i++; //we have to skip the next field anyway
			quan = parseFloat(f.elements[i].value); // quan is next field after price
			if (isNaN(quan) || quan < 0) { //in case fld is empty, negative or a string
				quan = 0;
				f.elements[i].value = "";
			}
			if (elem.name == "WaterPrice") { water = quan * price; } //water not taxable
			else { subt += (quan * price); }
		} // end if
	} // end for
	if(f.Delivery) {
		del = parseFloat(f.Delivery.value);
		if (isNaN(del) || del < 0) del = 0;
		subt += del;
	}
	f.Subtotal.value = currency(subt + water);
	tax = subt * 0.14; //PST 8%, GST 6%
	f.Taxes.value = currency(tax);
	f.Total.value = currency(subt + water + tax);
}	

function skip(inputObj) { // not being used due to name changes
	f = document.getElementById('orderDetails');
	inputName = inputObj.name;
	nextInput = inputName.substring(0,(inputName.length-5));
	f.elements[nextInput].focus();

}