function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			/*inputs[i].onfocus = function () {
				if (this.value=="") {
					this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
					this.className="required-input";
				}
			}*/
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				this.className="normal-input";
			}
		}
	}
	// repeat the same tests as above for textareas
	var selects = document.getElementsByTagName("textarea");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			/*selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				this.className="required-input";
			}*/
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				this.className="normal-input";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			/*selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				this.className="required-input";
			}*/
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				this.className="normal-input";
			}
		}
	}
}

function fncCopyInfo() {
	if (document.signupForm.copyInfo.checked==true) {
		document.signupForm.Recipient_First_Name.value=document.signupForm.First_Name.value;
		document.signupForm.Recipient_Last_Name.value=document.signupForm.Last_Name.value;
		document.signupForm.Recipient_Address.value=document.signupForm.Address.value;
		document.signupForm.Recipient_City.value=document.signupForm.City.value;
		document.signupForm.Recipient_State.value=document.signupForm.State.value;
		document.signupForm.Recipient_ZIP.value=document.signupForm.ZIP.value;
		document.signupForm.Recipient_Phone.value=document.signupForm.Phone.value;
	}else{
		document.signupForm.Recipient_First_Name.value="";
		document.signupForm.Recipient_Last_Name.value="";
		document.signupForm.Recipient_Address.value="";
		document.signupForm.Recipient_City.value="";
		document.signupForm.Recipient_State.value="";
		document.signupForm.Recipient_ZIP.value="";
		document.signupForm.Recipient_Phone.value="";
	}
}

function fncDisplayVeg() {
	if (document.orderForm.Entree.selectedIndex==0) {
		document.getElementById("vegDisplay").innerHTML="";
	}else{
		var selIndex=document.orderForm.Entree.selectedIndex;
		var veg=document.orderForm.Entree[selIndex].getAttribute("datapoint");
		if (veg!="") {
			document.getElementById("vegDisplay").innerHTML="<b>Included vegetable:</b> "+veg+"<br />";
		}else{
			document.getElementById("vegDisplay").innerHTML="";
		}
	}
}

function fncTypeOfMeal(typeOfMeal) {
	if (typeOfMeal==1) {
		document.orderForm.Dinner_Roll.disabled=false;		
		document.orderForm.Dessert.disabled=false;
		document.orderForm.Milk.disabled=false;
	}else{
		document.orderForm.Dinner_Roll.disabled=true;		
		document.orderForm.Dessert.disabled=true;
		document.orderForm.Milk.disabled=true;
	}
}

function fncUpdateItem(selector,qty,theItem) {
	if(qty==0){
		if (confirm("Are you sure you want to delete this meal from your order?")){
			document.orderForm.update_mdl.value="delete_meal.mdl";
			document.orderForm.itemNumber.value=theItem;
			document.orderForm.submit();
		}else{
			var theValueField="document.orderForm.OrigQty_"+theItem+".value";
			selector.selectedIndex=eval(theValueField);
		}
	}else{
		document.orderForm.update_mdl.value="update_order.mdl";
		document.orderForm.itemNumber.value=theItem;
		document.orderForm.newQty.value=qty;
		document.orderForm.submit();
	}
}

//validation section
function fncValidate() {
	if(document.signupForm.First_Name.value=="") {
		document.signupForm.First_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.First_Name.className="required-input";
		document.signupForm.First_Name.focus();
		return false;
	}
	if(document.signupForm.Last_Name.value=="") {
		document.signupForm.Last_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.Last_Name.className="required-input";
		document.signupForm.Last_Name.focus();
		return false;
	}
	var rePattern=/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/
	if(document.signupForm.userid.value.search(rePattern)==-1) {
		document.signupForm.userid.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.userid.className="required-input";
		document.signupForm.userid.focus();
		return false;
	}
	if(document.signupForm.Address.value=="") {
		document.signupForm.Address.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.Address.className="required-input";
		document.signupForm.Address.focus();
		return false;
	}
	if(document.signupForm.City.value=="") {
		document.signupForm.City.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.City.className="required-input";
		document.signupForm.City.focus();
		return false;
	}
	if(document.signupForm.State.value=="") {
		document.signupForm.State.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.State.className="required-input";
		document.signupForm.State.focus();
		return false;
	}
	var rePattern=/^(\d{5}-\d{4})|(\d{5})$/
	if(document.signupForm.ZIP.value.search(rePattern)==-1||document.signupForm.ZIP.value.length<5) {
		document.signupForm.ZIP.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.ZIP.className="required-input";
		document.signupForm.ZIP.focus();
		return false;
	}
	var rePattern=/^[0-9\-\.\(\)\ ]+$/
	if(document.signupForm.Phone.value.search(rePattern)==-1||document.signupForm.Phone.value.length<7) {
		document.signupForm.Phone.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.signupForm.Phone.className="required-input";
		document.signupForm.Phone.focus();
		return false;
	}
	
	if (document.signupForm.Recipient_First_Name.value!="") { /* modified per Bill H 12-14-09 */
		/*if(document.signupForm.Recipient_First_Name.value=="") {
			document.signupForm.Recipient_First_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_First_Name.className="required-input";
			document.signupForm.Recipient_First_Name.focus();
			return false;
		}*/
		if(document.signupForm.Recipient_Last_Name.value=="") {
			document.signupForm.Recipient_Last_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_Last_Name.className="required-input";
			document.signupForm.Recipient_Last_Name.focus();
			return false;
		}
		if(document.signupForm.Recipient_Address.value=="") {
			document.signupForm.Recipient_Address.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_Address.className="required-input";
			document.signupForm.Recipient_Address.focus();
			return false;
		}
		if(document.signupForm.Recipient_City.value=="") {
			document.signupForm.Recipient_City.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_City.className="required-input";
			document.signupForm.Recipient_City.focus();
			return false;
		}
		if(document.signupForm.Recipient_State.value=="") {
			document.signupForm.Recipient_State.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_State.className="required-input";
			document.signupForm.Recipient_State.focus();
			return false;
		}
		var rePattern=/^(\d{5}-\d{4})|(\d{5})$/
		if(document.signupForm.Recipient_ZIP.value==""||document.signupForm.Recipient_ZIP.value.length<5) {
			document.signupForm.Recipient_ZIP.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_ZIP.className="required-input";
			document.signupForm.Recipient_ZIP.focus();
			return false;
		}
		var rePattern=/^[0-9\-\.\(\)\ ]+$/
		if(document.signupForm.Recipient_Phone.value.search(rePattern)==-1||document.signupForm.Recipient_Phone.value.length<7) {
			document.signupForm.Recipient_Phone.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.signupForm.Recipient_Phone.className="required-input";
			document.signupForm.Recipient_Phone.focus();
			return false;
		}
	}
	
	return true;
}

function fncValidateAddRecipient() {
	if(document.addRecipientForm.Recipient_First_Name.value=="") {
		document.addRecipientForm.Recipient_First_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_First_Name.className="required-input";
		document.addRecipientForm.Recipient_First_Name.focus();
		return false;
	}
	if(document.addRecipientForm.Recipient_Last_Name.value=="") {
		document.addRecipientForm.Recipient_Last_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_Last_Name.className="required-input";
		document.addRecipientForm.Recipient_Last_Name.focus();
		return false;
	}
	if(document.addRecipientForm.Recipient_Address.value=="") {
		document.addRecipientForm.Recipient_Address.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_Address.className="required-input";
		document.addRecipientForm.Recipient_Address.focus();
		return false;
	}
	if(document.addRecipientForm.Recipient_City.value=="") {
		document.addRecipientForm.Recipient_City.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_City.className="required-input";
		document.addRecipientForm.Recipient_City.focus();
		return false;
	}
	if(document.addRecipientForm.Recipient_State.value=="") {
		document.addRecipientForm.Recipient_State.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_State.className="required-input";
		document.addRecipientForm.Recipient_State.focus();
		return false;
	}
	var rePattern=/^(\d{5}-\d{4})|(\d{5})$/
	if(document.addRecipientForm.Recipient_ZIP.value.search(rePattern)==-1||document.addRecipientForm.Recipient_ZIP.value.length<5) {
		document.addRecipientForm.Recipient_ZIP.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_ZIP.className="required-input";
		document.addRecipientForm.Recipient_ZIP.focus();
		return false;
	}
	var rePattern=/^[0-9\-\.\(\)\ ]+$/
	if(document.addRecipientForm.Recipient_Phone.value.search(rePattern)==-1||document.addRecipientForm.Recipient_Phone.value.length<7) {
		document.addRecipientForm.Recipient_Phone.parentNode.getElementsByTagName("span")[0].style.display = "inline";
		document.addRecipientForm.Recipient_Phone.className="required-input";
		document.addRecipientForm.Recipient_Phone.focus();
		return false;
	}
	return true;
}

function fncValidateOrder() {
	if(document.orderForm.nextAction.value=="") {
		if(document.orderForm.Recipient.selectedIndex==0) {
			document.orderForm.Recipient.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.orderForm.Recipient.className="required-input";
			document.orderForm.Recipient.focus();
			return false;
		}
		document.orderForm.nextAction.value="add";
		document.orderForm.mealNumber.value=0;
		return true;
	}else if(document.orderForm.nextAction.value=="add") {
		if(document.orderForm.Type_of_Meal[0].checked==false&&document.orderForm.Type_of_Meal[1].checked==false) {
			document.orderForm.Type_of_Meal[0].parentNode.getElementsByTagName("span")[0].style.display = "inline";
			return false;
		}		
		if(document.orderForm.Entree.selectedIndex==0) {
			document.orderForm.Entree.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.orderForm.Entree.className="required-input";
			document.orderForm.Entree.focus();
			return false;
		}
		if (document.orderForm.Type_of_Meal[0].checked) {
			if(document.orderForm.Dinner_Roll.selectedIndex==0) {
				document.orderForm.Dinner_Roll.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				document.orderForm.Dinner_Roll.className="required-input";
				document.orderForm.Dinner_Roll.focus();
				return false;
			}
			if(document.orderForm.Dessert.selectedIndex==0) {
				document.orderForm.Dessert.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				document.orderForm.Dessert.className="required-input";
				document.orderForm.Dessert.focus();
				return false;
			}
			if(document.orderForm.Milk.selectedIndex==0) {
				document.orderForm.Milk.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				document.orderForm.Milk.className="required-input";
				document.orderForm.Milk.focus();
				return false;
			}
		}
	}else if(document.orderForm.nextAction.value=="done") {
		if(document.orderForm.CC_Holder_Name.value=="") {
			document.orderForm.CC_Holder_Name.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.orderForm.CC_Holder_Name.className="required-input";
			document.orderForm.CC_Holder_Name.focus();
			return false;
		}
		if(document.orderForm.CC_Number.value==""||16<document.orderForm.CC_Number.value.length||8>document.orderForm.CC_Number.value.length||isNaN(document.orderForm.CC_Number.value)){
			document.orderForm.CC_Number.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.orderForm.CC_Number.className="required-input";
			document.orderForm.CC_Number.focus();
			return false;
		}
		if(document.orderForm.CC_Expire_Month.selectedIndex==0) {
			document.orderForm.CC_Expire_Month.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.orderForm.CC_Expire_Month.className="required-input";
			document.orderForm.CC_Expire_Month.focus();
			return false;
		}
		if(document.orderForm.CC_Expire_Year.selectedIndex==0) {
			document.orderForm.CC_Expire_Year.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			document.orderForm.CC_Expire_Year.className="required-input";
			document.orderForm.CC_Expire_Year.focus();
			return false;
		}
	}
}
