
var theRows = "";
theUnits = new Array();
counter = 0;
target = 0;


function addHeaderRow() {
var theForm = document.frmTableHeadGen;
 target = theForm.txtTarUnits.value;
theRows = "<tr><th>Day " + theForm.txtDay.value + " in Week</th><th>Target</th><th>" + theForm.txtTarUnits.value + "</th></tr>\n";
theRows = theRows + "<tr><th>Food</th><th>Points</th><th>Count</th></tr>\n";

theForm.style.display ="none"
document.frmTableGen.style.display ="block"
document.getElementById("theTable").innerHTML = "<table align=\"center\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">" + theRows + "\n</table>";

}


function addRow(){
	
var theForm = document.frmTableGen;
var runningUnits;

if (theForm.ap.checked == true) { //should reverse if
	runningUnits =  parseFloat(theForm.txtUnits.value);
} else {
		runningUnits = - parseFloat(theForm.txtUnits.value);
		theUnits[counter] = runningUnits;
		counter++;
}
var descUnits =runningUnits+parseFloat(target);
target = descUnits;

theRows = theRows + "<tr><td>" +  theForm.txtFood.value + "</td><td>" + runningUnits + "</td><td>" + descUnits + "</td></tr>\n";

document.getElementById("theTable").innerHTML = "<table align=\"center\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">" + theRows + "\n</table>";
}


function calcRow(){
var unitTotal=0;
for(i=0;i<theUnits.length;i++){
unitTotal+=parseFloat(theUnits[i]);
}
theRows = theRows + "<tr><td align=\"right\"><strong>Total:</strong></td><td>" + -unitTotal + "</td><td>&nbsp;</td></tr>";
document.getElementById("theTable").innerHTML = "<table align=\"center\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">" + theRows + "\n</table>";
document.frmTableGen.style.display ="none";
document.frmTableFootGen.style.display ="block";

}

function addfooterRow() { 
var theForm = document.frmTableFootGen;
 target = theForm.txtNotes.value;
theRows = theRows +"<tr><th colspan=\"3\">" + theForm.txtNotes.value + " in Week</th></tr>\n";

document.getElementById("theTable").innerHTML = "<p class=\"alert\"><b>All Done! scroll down for the HTML code</b></p><table align=\"center\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">" + theRows + "\n</table>";

theForm.style.display ="none"

//give the code
document.frmTableCode.style.display="block";
document.frmTableCode.txtTheCode.value = "<table align=\"center\" border=\"1\" cellpadding=\"3\" cellspacing=\"0\">" + theRows + "\n</table><p align=\"center\"><small>Table provided by<a href=\"http://ronisweigh.com/2008/07/daily-food-journal-generator_v3.html\">Roni's Food Journal Generator</a>.</small></p>";


}



function selectCode(id){
id.focus();
id.select();
}
