var prices=[];
//sandwiches
prices['PBJ']=2.50;
prices['nutellaBanana']=2.50;
prices['turkey']=3.00;
prices['chicken']=3.00;
prices['hamCheese']=3.00;
prices['tuna']=3.00;
prices['hummus']=2.50;
prices['BLT']=3.00;
//drinks
prices['appleJuice']=1.00;
prices['water']=1.00;
prices['juiceBox']=.50;
prices['wholeMilk']=1.00;
prices['soyMilk']=1.50;
prices['chocolateMilk']=1.50;
prices['orangeJuice']=1.00;
//desserts
prices['chocolateChip']=.50;
prices['oatmealRaisin']=.50;
prices['raisins']=1.00;
prices['jello']=1.00;
prices['appleSauce']=.50;
prices['vanillaPudding']=.50;
prices['chocolatePudding']=.50;
prices['fruitSnacks']=.75;
//fruit
prices['banana']=.50;
prices['apple']=1.00;
prices['orange']=1.00;
prices['cucumbers']=.50;
prices['carrot']=.50;
prices['celery']=.50;
//chips
prices['sunChips']=.75;
prices['potatoChips']=.50;
prices['pretzels']=.50;
prices['veggieChips']=.75;
//misc
prices['stringCheese']=.50;
prices['dunkems']=.50;
prices['cheeseCrackers']=.50;

var extras = new Array();
extras.push('chocolateChip');
extras.push('oatmealRaisin');
extras.push('raisins');
extras.push('jello');
extras.push('appleSauce');
extras.push('vanillaPudding');
extras.push('chocolatePudding');
extras.push('fruitSnacks');
//fruit
extras.push('banana');
extras.push('apple');
extras.push('orange');
extras.push('cucumbers');
extras.push('carrot');
extras.push('celery');
//chips
extras.push('sunChips');
extras.push('potatoChips');
extras.push('pretzels');
extras.push('veggieChips');
//misc
extras.push('stringCheese');
extras.push('dunkems');
extras.push('cheeseCrackers');

var sandwichSelection;
var drinkSelection;
var otherSelections = new Array();
var currentPrice;



function calcPrice(element) {
	changeSelection(element);
	currentPrice = 0;
	addIfDefined(sandwichSelection);
	addIfDefined(drinkSelection);
	for (var i = 0; i < otherSelections.length; i++) {
		addIfDefined(otherSelections[i]);
	}
	document.getElementById('total').value = formatCurrency(currentPrice);
}

function changeSelection(element) {
	if (element.name == 'sandwich') {
		sandwichSelection = element.value;
	} else if (element.name == 'drink') {
		drinkSelection = element.value;
	} else {		
		if (element.checked) {
			addSelection(element.name);
		} else {
			removeSelection(element.name);
		}
	}
}

function removeSelection(name) {
 	var idx = otherSelections.indexOf(name);
 	otherSelections[idx] = '';
}

function addSelection(name) {
	otherSelections.push(name);
}

function addIfDefined(selection) {
	if (prices[selection] > 0) {
		currentPrice += prices[selection];
	}
}

function formatCurrency(amt) {
	var strAmt = new String(amt);
	if(strAmt.indexOf('.') < 0) { strAmt += '.00'; }
	if(strAmt.indexOf('.') == (strAmt.length - 2)) { strAmt += '0'; }
	strAmt = '$' + strAmt;
	return strAmt; 
}

function writePrice(val) {
	document.write(' (' + formatCurrency(prices[val]) + ')');	
}

function validateSubmission() {
	var theForm = document.forms[0];
	var errorMsg = "";
	var errorCount = 0;
	if (typeof theForm['parentsName'].value == 'undefined' || theForm['parentsName'].value == "") {
		errorMsg += "Parent's Name";
		errorCount++;
		theForm['parentsName'].focus();
	}
	if (typeof theForm['childsName'].value == 'undefined' || theForm['childsName'].value == "") {
		if (errorCount > 0) {
			errorMsg += ", ";
		}
		errorMsg += "Child's Name";
		errorCount++;
		if (errorCount == 1) {
			theForm['childsName'].focus();
		}
	}
	if (typeof theForm['phone'].value == 'undefined' || theForm['phone'].value == "") {
		if (errorCount > 0) {
			errorMsg += ", ";
		}
		errorMsg += "Phone";
		errorCount++;
		if (errorCount == 1) {
			theForm['phone'].focus();
		}
	}
	if (typeof theForm['email'].value == 'undefined' || theForm['email'].value == "") {
		if (errorCount > 0) {
			errorMsg += ", ";
		}
		errorMsg += "Email";
		errorCount++;
		if (errorCount == 1) {
			theForm['email'].focus();
		}
	}
	if (!theForm['Mon'].checked && !theForm['Tue'].checked && !theForm['Wed'].checked &&
	    !theForm['Thu'].checked && !theForm['Fri'].checked) {
		if (errorCount > 0) {
			errorMsg += ", ";
		}
		errorMsg += "Day(s)";
		errorCount++;
	}
	if (!atLeastOneSelected(theForm, 'sandwich')) {
		if (errorCount > 0) {
			errorMsg += ", ";
		}
		errorMsg += "Sandwich";
		if (errorCount == 1) {
			theForm['sandwich'].focus();
		}
	}	
	if (!atLeastOneSelected(theForm, 'bread')) {
		if (errorCount > 0) {
			errorMsg += ", ";
		}
		errorMsg += "Bread";
		errorCount++;
		if (errorCount == 1) {
			theForm['bread'].focus();
		}
	}
	if (errorCount > 0) {
		alert("The following fields are required: " + errorMsg);
		return false;
	}
	writeConfirmationNum(theForm);
	summarizeFields(theForm);	
}

function summarizeFields(theForm) {
	var food = getCheckedValue(theForm, 'sandwich') + ' on ' + getCheckedValue(theForm, 'bread') + ' with ' +  getCheckedValue(theForm, 'drink');
	theForm['1food'].value = food;
	theForm['2extras'].value = getExtras(theForm);
	theForm['3parentInfo'].value = getParentInfo(theForm);
	theForm['4childInfo'].value = getChildInfo(theForm);
	theForm['5pickupInfo'].value = getPickupInfo(theForm);
}

function atLeastOneSelected(theForm, fieldName) {
	for (var i=0;i < theForm[fieldName].length;i++) {
		if (theForm[fieldName][i].checked) {
			return true;
		}
	}
	return false;
}

function getCheckedValue(theForm, fieldName) {
	for (var i=0;i < theForm[fieldName].length;i++) {
		if (theForm[fieldName][i].checked) {
			return theForm[fieldName][i].value;
		}
	}
	return "";
}

function getExtras(theForm) {
	var strExtras = "";
	for (var i=0;i < extras.length;i++) {		
		if (theForm[extras[i]].checked) {
			strExtras += extras[i] + ",";
		}
	}
	return strExtras;
}

function getParentInfo(theForm) {
	return theForm['parentsName'].value + "," + theForm['phone'].value;
}

function getChildInfo(theForm) {
	var info = theForm['childsName'].value;
	if (theForm['school'].value.length > 0) {
		info += "," + theForm['school'].value;
	}
	if (theForm['childsBday'].value.length > 0) {
		info += "," + theForm['childsBday'].value;
	}
	if (theForm['allergies'].value.length > 0) {
		info += ", allergic to " + theForm['allergies'].value;
	}
	return info;
}

function getPickupInfo(theForm) {
	var info = "";
	if (theForm['Mon'].checked) {
		info += "Mon, ";
	}
	if (theForm['Tue'].checked) {
		info += "Tue, ";
	}
	if (theForm['Wed'].checked) {
		info += "Wed, ";
	}
	if (theForm['Thu'].checked) {
		info += "Thu, ";
	}
	if (theForm['Fri'].checked) {
		info += "Fri, ";
	}
	info += theForm['pickUpTime'].value + "AM";
	if (theForm['comments'].value.length > 0) {
		info += ", " + theForm['comments'].value;
	}
	info += ", " + theForm['total'].value;
	info += ", #" + theForm['confirmationNum'].value;
 	return info;
}

function writeConfirmationNum(theForm) {
	var randomNumber = Math.floor(Math.random()*100001)
	theForm['confirmationNum'].value = randomNumber;
	theForm['redirect'].value = "lunchProgramThankYou.html?confNum=" + randomNumber; 
}
