
var elz = new Array("tCal","tCar","tPro","tFat","tSat","tSug","tFib","cCal","cCar","cPro","cFat","cSat","cSug","cFib","pCal","pCar","pPro","pFat","pSat","pSug","pFib");

function ClearAll() {
	var tc = document.zForm.count.value + 1;
	for (i=1;i<tc;i++) {
		//iv = zro(i,3);
		var frd = eval("document.zForm.m" + i);
		if (frd) {
			frd.checked = false;
		}
	}
	ReCalc();
}
function ClickIt(a) {
	t = eval("document.zForm.m" + a);
	//r = document.getElementById(("r"+a));
	if ($('#m'+a).is(':checked')) {
		$('#m'+a).removeAttr('checked');
	} else {
		$('#m'+a).attr('checked','checked');
	}
	ReCalc();
}
function Qty(a,b) {
	//var bq = eval("document.zForm.q" + b);
	//var bd = document.getElementById(("d" + b));
	var qval = $('#q'+b).val();
	if (a > 0) {
		$('#q'+b).val(Math.round(parseFloat(qval) + 1));
	} else {
		if (parseFloat(qval) > 1) {
			$('#q'+b).val(Math.round(parseFloat(qval) - 1));
		} else {
			$('#d'+b).hide();
			$('#q'+b).val('');
			$('#m'+b).removeAttr('checked');
		}
	}
	ReCalc();
}
function zro(num,count) {
	//var zer = num + '';
	//while(zer.length < count) {
	//	zer = "0" + zer;
	//}
	return num;
}
function SendForm() {
	document.zForm.submit();
}
function GetData() { 
	window.location = "/diet/view/" + document.zForm.month.value + document.zForm.day.value + document.zForm.year.value;
}
function ReCalc() {
	var b = new Array();
	var c = new Array();
	var t = new Array(4,4,9,9,4,4);
	var tab = document.getElementById("cntr");
	var tbr = tab.getElementsByTagName("tr");
	for (f=0;f<elz.length;f++) { c[f] = 0; }
	for (i=0;i<tbr.length;i++) {
		var zi = tbr[i].id.substring(1);
		var obt = document.getElementById("diet");
		if ($('#m'+zi).length) {
			if ($('#m'+zi).is(':checked')) {
				tbr[i].style.backgroundColor = "#FFFFFF";
				//var a = new Array(6);
				var chkbox = $('#m'+zi).val();
				var a = chkbox.split(",");
				$('#d'+zi).show();
				var inpval = $('#q'+zi).val();
				if (inpval == '') {
					$('#q'+zi).val(1);
					inpval = 1;
				}
				for (f=0;f<14;f++) {
					if (f<7) {
						c[f] = parseFloat(c[f]) + parseFloat((a[f])*(inpval));
					} else if (f>7) {
						c[f] = parseFloat(c[(f-7)]) * parseFloat(t[(f-8)]);
					}
				}
				c[7] = parseFloat(c[8]) + parseFloat(c[9]) + parseFloat(c[10]);
				for (f=15;f<18;f++) {
					c[f] = parseFloat(c[(f-7)]) / parseFloat(c[7]) * 100;
				}
				c[18] = parseFloat(c[4]) / parseFloat(c[3]) * 100;
				c[19] = parseFloat(c[5]) / parseFloat(c[1]) * 100;
			} else {
				tbr[i].style.background = "none";
				$('#d'+zi).hide();
				$('#q'+zi).val('');
			}
		}
	}
	for (f=0;f<elz.length;f++) {
		if (!isNaN(c[f])) {
			$('#'+elz[f]).val(Math.round(c[f]));
		} else {
			$('#'+elz[f]).val(0);
		}
	}
	$('#pCal').val('100%');
}

function addItem() {
	var collect = { 
		NewItem: $('#NewItem').val(),
		NewPortion: $('#NewPortion').val(),
		NewDescription: $('#NewDescription').val(), 
		NewCategory: $('#NewCategory').val(), 
		NewCalories: $('#NewCalories').val(), 
		NewCarbs: $('#NewCarbs').val(), 
		NewProtein: $('#NewProtein').val(), 
		NewFat: $('#NewFat').val(), 
		NewSatFat: $('#NewSatFat').val(), 
		NewSugar: $('#NewSugar').val(), 
		NewFiber: $('#NewFiber').val() 
	};
	$.post("/inc/add-item.php", collect, function(data){
		if(data.length > 0) {
			$('#NewMessage').html(data);
		}
	});
}
function saveDiet() {
	var elz = Array("tCal","tCar","tPro","tFat","tSat","tSug","tFib","cCal","cCar","cPro","cFat","cSat","cSug","cFib","pCal","pCar","pPro","pFat","pSat","pSug","pFib");
	var calz = "";
	$.each(elz, function(index) {
		calz += $('#'+elz[index]).val() + ","
	});
	var items = "";
	$('#zForm').find('#[name^="m"]').each(function(index) {
		if ($(this).is(':checked')) {
			var num = $(this).attr('id').substring(1);
			items +=  $(this).attr('id') + '=' + $('#q'+num).val() + ",";
		}
	});
	var theDate = $('#thisday').val();
	var thisDiet = {
		totals: calz,
		checkd: items,
		thisDate: theDate
	};
	$.post("/account/save_diet", thisDiet, function(data){
		if(data.length >0) {
			$('#dayMsg').html(data);
		}
	});
}
function saveNotes() {
	var collect = { Notes: $('#Notes').val(), thisday: $('#thisday').val() };
	$.post("/inc/save-note.php", collect, function(data){
		if(data.length > 0) {
			$('#NotesMessage').html(data);
		}
	});
}
function deleteItem(num) {
	var queryString = { del: num };
	$.post("/inc/delete-item.php", queryString, function(data){
		if(data.length >0) {
			$('#itemMessage').html(data);
		}
	});
}

$(document).ready(function(){
	//SETUP FLEXIBLE TOGGLING
	if ($(".subnav").length) {
		$(".subnav").find("a").click(function() {
			if ($(this).attr('title')) {
				$(this).parents(".subnav").find("a").removeClass("tabOn");
				$(this).addClass("tabOn");
				obj = $(this).attr('title');
				$("#"+obj).css('display','block');
				sibClass = $("#"+obj).attr('class');
				$("#"+obj).siblings().each(function() {
					if (this.id != "" && this.id != "NULL" && this.className == sibClass) {
						this.style.display = "none";
					}
				});
			}
		});
	}
	if ($('#cntr').length) {
		ReCalc();
		//SetCalendar();
		//$('#Category').click(function() { ShowCategory() });
		$('#cntr').find('div[id^="x"]').each(function(index) {
			this.bOverlay = false;
			this.setTimeShow;
			this.setTimeHide;
			this.show = function() {
				$('#z'+this.id.substring(1)).removeClass('offUp').addClass('onDown');
				$('#r'+this.id.substring(1)).css('background-color','#FFF');
				$('#r'+this.id.substring(1)).find('td').css('border-bottom-width','0px');
				this.bOverlay = true;
			}
			this.hide = function() {
				$('#z'+this.id.substring(1)).removeClass('onDown').addClass('offUp');
				$('#r'+this.id.substring(1)).css('background-color','transparent');
				$('#r'+this.id.substring(1)).find('td').css('border-bottom-width','1px');
				this.bOverlay = false;
			}
			this.startShow = function() {
				if(this.bOverlay) {
					clearTimeout(this.setTimeHide);
				} else {
					var id = this.id;
					this.setTimeShow = setTimeout("document.getElementById('"+id+"').show(this)",500);
				}
			}
			this.startHide = function() {
				if(this.bOverlay) {
					var id = this.id;
					this.setTimeHide = setTimeout("document.getElementById('"+id+"').hide(this)",400);
				} else {
					clearTimeout(this.setTimeShow);
				}
			}
			this.onclick = function() {
				var num = this.id.substring(1);
				ClickIt(num);
			}
			
			this.onmouseover = this.startShow;
			this.onmouseout = this.startHide;
		});
		$('#cntr').find('td[id^="z"]').each(function(index) {
			this.onmouseover = function() {
				var id = "x" + this.id.substring(1);
				document.getElementById(id).startShow();
			}
			this.onmouseout = function() { 
				var id = "x" + this.id.substring(1);
				document.getElementById(id).startHide();
			}
		});
	}
	if ($('.dropdown').length) {
		$('.dropdown').each(function() {
			var id = $(this).attr('id');
			$('.'+id).mouseover(function() { $(this).css('display','block'); });
			$('.'+id).mouseout(function() { $(this).css('display','none'); });
			$('.'+id).find('li').click(function() { $('#'+id).val($(this).html()); $('.'+id).css('display','none'); });
			$(this).mouseover(function() { 
				var pos = $(this).position();
				$('.'+id).css('left',pos.left-25).css('top',pos.top);
				$('.'+id).css('display','block');
			});
			$(this).mouseout(function() { $('.'+id).css('display','none'); });
		});
	}

	//DATEPICKER
	if ($(".pickdate").length) {
		$('head').append('<link rel="stylesheet" type="text/css" href="/inc/jquery-ui-1.8.7.custom.css" />');
		$('head').append('<script type="text/javascript" src="/inc/jquery-ui-1.8.7.custom.min.js"></script>');
		if ($.ui){
			$(".pickdate").datepicker({
				showOn: "button",
				buttonImage: "/img/calendar.gif",
				buttonImageOnly: true,
				buttonText: "Change Date",
				altField: ('#thisday'),
				dateFormat: 'yy-mm-dd',
				onSelect:function(dateText,inst) { window.location = "/diet/view/" + $('#thisday').val(); }
			});
		}
	}
});
