// common.js -- functions for the Have-a-Bite Cafe


// putInfo -- sets age and gender of user, called by choices form in
//            header.html, with that form as argument

function putInfo (theForm){
	var age, sex;
	switch (theForm.sex.selectedIndex) {
		case 0: 		
			alert("Please select your gender from the pull-down menu.");
			return;
			break;
		case 1:
			top.header.document.tracker.sex.value=sex="F";
			break;
		case 2:
			top.header.document.tracker.sex.value=sex="M";
			break;
		default:
			top.header.document.tracker.sex.value=sex="";
	}	// switch
	
	switch (theForm.age.selectedIndex) {
		case 0:
			alert("Please select your age from the pull-down menu.");
			return;
			break;
		default:
			top.header.document.tracker.age.value = age = theForm.age.selectedIndex;
			putRDAs(getRDAs(age, sex));
			document.location="menuChoice.html";		// open the menu
	}	//switch
		
}	// putInfo


//	goThere -- open a location

function goThere(place){
	document.location.href=place;
}	// goThere



//	getRDAs -- returns a set of recommended daily allowances based on sex and age
//	numbers in the matrices are Calories, Fat, Carbohydrate, Protein, Calcium,
//  Iron, Vitamin A, Vitamin C, Fiber


function getRDAs (age, sex){
	if (sex == "F")
		switch(age){
			case 1:
				return [1300, 100, 100, 16, 800, 100, 400, 40, 6];
			case 2:
				return [1800, 100, 100, 24, 800, 100, 500, 45, 10];
			case 3:
				return [2000, 100, 100, 28, 800, 100, 700, 45, 15];
			case 4:
				return [2200, 100, 100, 46, 1200, 150, 800, 50, 27];
			case 5:
				return [2200, 100, 100, 44, 1200, 150, 800, 60, 27];
			case 6:
				return [2200, 100, 100, 46, 1200, 150, 800, 60, 27];
			case 7:
				return [2200, 100, 100, 50, 800, 150, 800, 60, 27];
			case 8:
				return [1900, 100, 100, 50, 800, 100, 800, 60, 25];
			default:
				alert("There's been a screw-up in the getRDAs function -- missing age.");
				break;
		} // switch
	else if (sex == "M")
		switch(age){
			case 1:
				return [1300, 100, 100, 16, 800, 100, 400, 40, 6];
			case 2:
				return [1800, 100, 100, 24, 800, 100, 500, 45, 10];
			case 3:
				return [2000, 100, 100, 28, 800, 100, 700, 45, 15];
			case 4:
				return [2500, 100, 100, 45, 1200, 120, 1000, 50, 30];
			case 5:
				return [3000, 100, 100, 59, 1200, 120, 1000, 60, 35];
			case 6:
				return [2900, 100, 100, 58, 1200, 100, 1000, 60, 35];
			case 7:
				return [2900, 100, 100, 63, 800, 100, 1000, 60, 35];
			case 8:
				return [2300, 100, 100, 63, 800, 100, 1000, 60, 28];
			default:
				alert("There's been a screw-up in the getRDAs function -- missing age.");
				break;
		} // switch
	else
		alert ("There's been a screw-up in the getRDAs function -- missing sex.");
}	// getRDAs



//	putRDAs -- puts the RDA array into the tracker form in the header. This
//	is a kludge because I don't have time to figure out if there's a way to
//	create a global array variable that's accessible to multiple frames and
//	documents.

function putRDAs(RDAarray){
	top.header.document.tracker.caloriesRDA.value=RDAarray[0];
	top.header.document.tracker.fatRDA.value=RDAarray[1];
	top.header.document.tracker.carbohydrateRDA.value=RDAarray[2];
	top.header.document.tracker.proteinRDA.value=RDAarray[3];
	top.header.document.tracker.calciumRDA.value=RDAarray[4];
	top.header.document.tracker.ironRDA.value=RDAarray[5];
	top.header.document.tracker.vitaminaRDA.value=RDAarray[6];
	top.header.document.tracker.vitamincRDA.value=RDAarray[7];
	top.header.document.tracker.fiberRDA.value=RDAarray[8];
}	// putRDAs




//	addem -- adds selected values to totals
function addem(theForm){
	var addarray = [0,0,0,0,0,0,0,0,0];
	var offset = 11;	// location in form of nutrient running totals
	var temp, nametemp;
	for(var i=0; i<theForm.elements.length; ++i)
		if ((theForm.elements[i].type=="checkbox") && theForm.elements[i].checked){
			temparray = theForm.elements[i].value.split("#");
			for (var j=0; j<9; ++j)
				addarray[j] += Number(temparray[j]);
			nametemp = top.header.document.tracker.foods.value;
			if (nametemp != "")
				nametemp += ", ";
			nametemp += theForm.elements[i].name;
			top.header.document.tracker.foods.value = nametemp;
		}	// if
	for (i=0; i<9; ++i){
		temp = Number(top.header.document.tracker.elements[i+offset].value); // I HATE that!
		temp += addarray[i];
		top.header.document.tracker.elements[i+offset].value = temp;
	}	// for
}	// addem



function listit(theForm){
	addem(theForm);
	goThere("menulister1.html");
}	//	listit



//	truncdivide -- a cumbersome way to do integer division -- ARRGH!
function truncdivide(dividend, divisor){
	return (dividend-dividend%divisor)/divisor;
}	// truncdivide

//	percentify -- returns a percentage

function percentify(dividend, divisor){
	var top=Number(dividend);	// because of loose typing in JavaScript. Ick.
	var bottom=Number(divisor);
	if (bottom == 0) return 0;	// Yes, yes, it's wrong, but it's just to clean up HTML output.
	return truncdivide(100*top, bottom);
}




//	histogram -- creates a histogram of values in an array
function histogram (labels, values, lowerbound, upperbound){
	var i=0, j, maximum=100, barlength=0;
	
	document.writeln('<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">');
	for(i=lowerbound; i<upperbound; i++)
		maximum = (values[i] > maximum) ? values[i] : maximum;	/* determine the maximum value */
			for(i=lowerbound; i<upperbound; i++){
		document.writeln('<TR><TD HEIGHT="15" ALIGN=RIGHT>',labels[i],'&nbsp;',values[i],'\%&nbsp;</TD>');
		barlength = percentify(values[i], maximum); 	/* bar length is a percentage of the max value */
		if (i==lowerbound)
			for (j=0; j<barlength; j++)
				document.writeln('<TD><IMG SRC="bluerect.gif" ALIGN=BOTTOM></TD>');
											/* print a little rectangle for each 1% of max value */
		else
			for (j=0; j<barlength; j++)
				document.writeln('<TD><IMG SRC="redrect.gif" ALIGN=BOTTOM></TD>\n');
											/* print a little rectangle for each 1% of max value */
		document.writeln('</TR>');
		} /* for */
	document.writeln('</TABLE>'); 
	return;
	} /* hist2 */
	


//	spewform -- a debugging function
function spewform(){
	spewwindow=window.open("", "spewwindow");
	for (var i=0; i<top.header.document.tracker.elements.length; ++i)
		spewwindow.document.writeln("<p>",top.header.document.tracker.elements[i].name," ",
					top.header.document.tracker.elements[i].value,"</p>");
}	// spewform
