]> Softwares of Agnibho - dietsurvey.git/blob - dietsurvey/script.js
96a84bea6714b83a1efb64fb86f5f8c1475edaaa
[dietsurvey.git] / dietsurvey / script.js
1 /**********************************************************************
2 * Title: Diet-Survey
3 * Description: Application for calculating nutrient intake
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com
6 **********************************************************************
7 Copyright (c) 2013-2015 Agnibho Mondal
8 All rights reserved
9 **********************************************************************
10 This file is part of Diet-Survey.
11
12 Diet-Survey is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 Diet-Survey is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with Diet-Survey. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
25 var foodlist=[];
26 var customfood=[];
27 var family={};
28 family.members=[];
29 var foods=[];
30 var total={};
31
32
33 $(document).ready(function(){
34 $("#js-check").hide();
35 enum_date();
36 $("#date-month").change(function(e){
37 adjust_day($(e.target).val());
38 });
39
40 $("#final").hide();
41
42 $("#block-wait").modal("show");
43 $.get("ajax.php?initdata=true", function(data, state){
44 $("#block-wait").modal("hide");
45 if(state=="success"){
46 init=JSON.parse(data);
47 foodlist=init.list;
48 customfood=init.custom;
49 for(i=0; i<foodlist.length; i++){
50 $("#food-name").append("<option>"+foodlist[i]+"</option>");
51 }
52 }
53 else{
54 $("#connect-fail").modal("show");
55 }
56 });
57
58 $("#edit-member").on("show.bs.modal", function(){
59 $("#member-name").val("");
60 $("#member-age").val("");
61 $("[name='member-sex']").removeProp("checked");
62 $("#member-female-npnl").prop("checked", "true");
63 $("#member-work-box").hide();
64 $("#female-special").hide();
65 });
66 $("#member-age").change(function(){
67 adjust_member();
68 });
69 $("[name='member-sex']").change(function(){
70 adjust_member();
71 });
72 $("#save-member").click(function(){
73 mem={};
74 mem.name=$("#member-name").val();
75 if(mem.name.length==0){
76 alert("Name can't be empty");
77 return;
78 }
79 mem.age=$("#member-age").val();
80 if(mem.age=="" || isNaN(mem.age) || mem.age<0 || mem.age>100){
81 alert("Please enter correct age");
82 return;
83 }
84 if($("#member-sex-male").is(":checked")){
85 mem.sex="Male";
86 }
87 else if($("#member-sex-female").is(":checked")){
88 mem.sex="Female";
89 }
90 else{
91 alert("Please select sex");
92 return;
93 }
94 if(mem.age>=18){
95 mem.work=$("#member-work").val();
96 if(mem.age<=45 && mem.sex=="Female"){
97 if($("#member-female-npnl").is(":checked")){
98 mem.state="npnl";
99 }
100 if($("#member-female-pregnant").is(":checked")){
101 mem.state="preg";
102 }
103 if($("#member-female-lactating-lt6").is(":checked")){
104 mem.state="lactb6";
105 }
106 if($("#member-female-lactating-gt6").is(":checked")){
107 mem.state="lacto6";
108 }
109 }
110 }
111 family.members.push(mem);
112 show_members();
113 $("#edit-member").modal("hide");
114 });
115 $("#family-members").on("click", ".remove-member", function(e){
116 family.members.splice($(e.target).data("id"), 1);
117 show_members();
118 });
119
120 $("#edit-food").on("show.bs.modal", function(){
121 $("#food-name").val("");
122 $("#food-amount").val("");
123 });
124
125 $("#add-food").click(function(){
126 item={};
127 item.name=$("#food-name").val();
128 item.amount=$("#food-amount").val();
129 if(item.name==""){
130 alert("Please select a food");
131 return;
132 }
133 if(item.amount=="" || isNaN(item.amount) || item.amount<=0){
134 alert("Please enter correct amount");
135 return;
136 }
137 foods.push(item);
138 show_foods();
139 $("#edit-food").modal("hide");
140 });
141 $("#launch-custom").click(function(){
142 $("#edit-food").modal("hide");
143 $("#customize-food").modal("show");
144 });
145 $("#food-list").on("click", ".remove-food", function(e){
146 foods.splice($(e.target).data("id"), 1);
147 show_foods();
148 });
149
150 $("#customize-food").on("show.bs.modal", function(){
151 show_custom_foods();
152 });
153 $("#custom-save").click(function(){
154 save_custom_food();
155 });
156 $("#custom-existing").on("click", ".custom-delete", function(e){
157 del_id=$(e.target).data("id");
158 $("#customize-food :input, #customize-food :button").prop("disabled", "disabled");
159 $.post("ajax.php", {"delete_custom_food":del_id}, function(data, state){
160 if(state=="success" && JSON.parse(data).flag){
161 customfood.splice(del_id, 1);
162 show_custom_foods();
163 }
164 else{
165 alert("Server sync failed");
166 }
167 $("#customize-food :input, #customize-food :button").removeProp("disabled", "disabled");
168 });
169 });
170
171 $("#get-result").click(function(){
172 family.head=$("#family-head").val();
173 family.date=[$("#date-day").val(), $("#date-month").val(), $("#date-year").val()];
174 if(family.head.length==0){
175 alert("Please enter the name of the head of the family");
176 return;
177 }
178 else if(family.members.length==0){
179 alert("Please add family members");
180 }
181 else if(foods.length==0){
182 alert("Please add consumed foods");
183 }
184 retrieve();
185 });
186
187 });
188
189 function retrieve(){
190 compact={};
191 compact.subjects=[];
192 compact.foods=[];
193 local=[];
194 $.extend(true, compact.subjects, family.members);
195 $.extend(true, compact.foods, foods);
196 for(i=0; i<compact.subjects.length; i++){
197 delete compact.subjects[i].name;
198 }
199 for(i=0; i<compact.foods.length; i++){
200 delete compact.foods[i].amount;
201 }
202 $("#block-wait").modal("show");
203 $.post("ajax.php", compact, function(string, status){
204 $("#block-wait").modal("hide");
205 if(status=="success"){
206 data=JSON.parse(string);
207 for(i=0; i<data["rda"].length; i++){
208 family.members[i].rda=data["rda"][i];
209 }
210 for(i=0; i<foods.length; i++){
211 foods[i].value=data["val"][foods[i].name];
212 }
213 calculate();
214 show_result();
215 draw_bar_diagram();
216 }
217 else{
218 $("#connect-fail").modal("show");
219 }
220 });
221 }
222
223 function calculate(){
224 for(i=0; i<foods.length; i++){
225 foods[i].intake={};
226 foods[i].intake.energy=foods[i].value.energy*foods[i].amount/100;
227 foods[i].intake.protein=foods[i].value.protein*foods[i].amount/100;
228 foods[i].intake.iron=foods[i].value.iron*foods[i].amount/100;
229 foods[i].intake.vitA=foods[i].value.vitA*foods[i].amount/100;
230 foods[i].intake.thiamine=foods[i].value.thiamine*foods[i].amount/100;
231 foods[i].intake.riboflavin=foods[i].value.riboflavin*foods[i].amount/100;
232 foods[i].intake.vitC=foods[i].value.vitC*foods[i].amount/100;
233 }
234 total.intake={};
235 total.intake.energy=0
236 total.intake.protein=0
237 total.intake.iron=0
238 total.intake.vitA=0
239 total.intake.thiamine=0
240 total.intake.riboflavin=0
241 total.intake.vitC=0
242 for(i=0; i<foods.length; i++){
243 total.intake.energy=total.intake.energy+foods[i].intake.energy;
244 total.intake.protein=total.intake.protein+foods[i].intake.protein;
245 total.intake.iron=total.intake.iron+foods[i].intake.iron;
246 total.intake.vitA=total.intake.vitA+foods[i].intake.vitA;
247 total.intake.thiamine=total.intake.thiamine+foods[i].intake.thiamine;
248 total.intake.riboflavin=total.intake.riboflavin+foods[i].intake.riboflavin;
249 total.intake.vitC=total.intake.vitC+foods[i].intake.vitC;
250 }
251 total.requirement={};
252 total.requirement.energy=0
253 total.requirement.protein=0
254 total.requirement.iron=0
255 total.requirement.vitA=0
256 total.requirement.thiamine=0
257 total.requirement.riboflavin=0
258 total.requirement.vitC=0
259 for(i=0; i<family.members.length; i++){
260 total.requirement.energy=total.requirement.energy+family.members[i].rda.energy;
261 total.requirement.protein=total.requirement.protein+family.members[i].rda.protein;
262 total.requirement.iron=total.requirement.iron+family.members[i].rda.iron;
263 total.requirement.vitA=total.requirement.vitA+family.members[i].rda.vitA;
264 total.requirement.thiamine=total.requirement.thiamine+family.members[i].rda.thiamine;
265 total.requirement.riboflavin=total.requirement.riboflavin+family.members[i].rda.riboflavin;
266 total.requirement.vitC=total.requirement.vitC+family.members[i].rda.vitC;
267 }
268 total.diff={};
269 total.diff.energy={};
270 total.diff.protein={};
271 total.diff.iron={};
272 total.diff.vitA={};
273 total.diff.thiamine={};
274 total.diff.riboflavin={};
275 total.diff.vitC={};
276 if(total.requirement.energy>total.intake.energy){
277 total.diff.energy.state="-";
278 total.diff.energy.value=total.requirement.energy-total.intake.energy
279 }
280 else{
281 total.diff.energy.state="+";
282 total.diff.energy.value=total.intake.energy-total.requirement.energy
283 }
284 if(total.requirement.protein>total.intake.protein){
285 total.diff.protein.state="-";
286 total.diff.protein.value=total.requirement.protein-total.intake.protein
287 }
288 else{
289 total.diff.protein.state="+";
290 total.diff.protein.value=total.intake.protein-total.requirement.protein
291 }
292 if(total.requirement.iron>total.intake.iron){
293 total.diff.iron.state="-";
294 total.diff.iron.value=total.requirement.iron-total.intake.iron
295 }
296 else{
297 total.diff.iron.state="+";
298 total.diff.iron.value=total.intake.iron-total.requirement.iron
299 }
300 if(total.requirement.vitA>total.intake.vitA){
301 total.diff.vitA.state="-";
302 total.diff.vitA.value=total.requirement.vitA-total.intake.vitA
303 }
304 else{
305 total.diff.vitA.state="+";
306 total.diff.vitA.value=total.intake.vitA-total.requirement.vitA
307 }
308 if(total.requirement.thiamine>total.intake.thiamine){
309 total.diff.thiamine.state="-";
310 total.diff.thiamine.value=total.requirement.thiamine-total.intake.thiamine
311 }
312 else{
313 total.diff.thiamine.state="+";
314 total.diff.thiamine.value=total.intake.thiamine-total.requirement.thiamine
315 }
316 if(total.requirement.riboflavin>total.intake.riboflavin){
317 total.diff.riboflavin.state="-";
318 total.diff.riboflavin.value=total.requirement.riboflavin-total.intake.riboflavin
319 }
320 else{
321 total.diff.riboflavin.state="+";
322 total.diff.riboflavin.value=total.intake.riboflavin-total.requirement.riboflavin
323 }
324 if(total.requirement.vitC>total.intake.vitC){
325 total.diff.vitC.state="-";
326 total.diff.vitC.value=total.requirement.vitC-total.intake.vitC
327 }
328 else{
329 total.diff.vitC.state="+";
330 total.diff.vitC.value=total.intake.vitC-total.requirement.vitC
331 }
332 }
333
334
335 function adjust_member(){
336 age=$("#member-age").val();
337 fem=false;
338 if($("#member-sex-female").is(":checked")){
339 fem=true;
340 }
341 if(age>=18){
342 $("#member-work-box").show();
343 if(fem && age<=45){
344 $("#female-special").show();
345 }
346 else{
347 $("#female-special").hide();
348 }
349 }
350 else{
351 $("#member-work-box").hide();
352 $("#female-special").hide();
353 }
354 }
355
356 function show_members(){
357 $("#family-members").html("");
358 for(i=0; i<family.members.length; i++){
359 $("#family-members").append(display_member(family.members[i], i));
360 }
361 }
362
363 function show_foods(){
364 $("#food-list").html('<tr><th class="col-xs-6">Food Name</th><th class="col-xs-5">Amount (gm or ml)</th><th class="col-xs-1">Remove</th></tr>');
365 for(i=0; i<foods.length; i++){
366 $("#food-list").append(display_food(foods[i], i));
367 }
368 }
369
370 function show_custom_foods(){
371 $("#custom-existing").html("<tr><th>Name</th><th>Energy</th><th>Protein</th><th>Iron</th><th>Vitamin A</th><th>Thiamine</th><th>Riboflavin</th><th>Vitamin C</th><th>Delete</th></tr>");
372 for(i=0; i<customfood.length; i++){
373 $("#custom-existing").append('<tr><td>'+customfood[i].name+'</td><td>'+customfood[i].energy+'</td><td>'+customfood[i].protein+'</td><td>'+customfood[i].iron+'</td><td>'+customfood[i].vitA+'</td><td>'+customfood[i].thiamine+'</td><td>'+customfood[i].riboflavin+'</td><td>'+customfood[i].vitC+'</td><td class="text-center"><a href="#custom-existing" data-id="'+i+'" class="text-danger"><span class="glyphicon glyphicon-remove custom-delete" data-id="'+i+'"></span></a></td></tr>');
374 }
375 }
376 function save_custom_food(){
377 sv={};
378 sv.name=$("#custom-name").val();
379 sv.energy=$("#custom-energy").val();
380 sv.protein=$("#custom-protein").val();
381 sv.iron=$("#custom-iron").val();
382 sv.vitA=$("#custom-vitA").val();
383 sv.thiamine=$("#custom-thiamine").val();
384 sv.riboflavin=$("#custom-riboflavin").val();
385 sv.vitC=$("#custom-vitC").val();
386 if(sv.name==""){
387 alert("Please enter name");
388 return;
389 }
390 else if(!foodlist.indexOf(sv.name)==-1){
391 alert("The food is already present");
392 return;
393 }
394 else if(!(check(sv.energy) && check(sv.protein) && check(sv.iron) && check(sv.vitA) && check(sv.thiamine) && check(sv.riboflavin) && check(sv.vitC))){
395 alert("Please enter proper nutritional values");
396 return;
397 }
398 $("#customize-food :input, #customize-food :button").prop("disabled", "disabled");
399 $.post("ajax.php", sv, function(data, state){
400 if(state=="success" && JSON.parse(data).flag){
401 customfood.push(sv);
402 foodlist.push(sv.name);
403 show_custom_foods();
404 for(i=0; i<foodlist.length; i++){
405 $("#food-name").append("<option>"+foodlist[i]+"</option>");
406 }
407 }
408 else{
409 alert("Server sync failed");
410 }
411 $("#customize-food :input, #customize-food :button").removeProp("disabled");
412 });
413
414 function check(val){
415 if(val=="" || isNaN(val) || val<0){
416 return false;
417 }
418 else{
419 return true;
420 }
421 }
422 }
423
424 function show_result(){
425 $("#initial").hide();
426 $("#final").slideDown();
427
428 $("#show-hof").text(family.head);
429 $("#show-date").text(family.date[0]+"-"+family.date[1]+"-"+family.date[2]);
430
431 $("#show-members").html("<tr><th>Name</th><th>Age</th><th>Sex</th></tr>");
432 $("#show-member-points").html("");
433 for(i=0; i<family.members.length; i++){
434 $("#show-members").append("<tr><td>"+family.members[i].name+"</td><td>"+family.members[i].age+" years</td><td>"+family.members[i].sex+"</td></tr>");
435 if(family.members[i].age>=18){
436 $("#show-member-points").append('<li class="list-group-item">'+family.members[i].name+' is a '+family.members[i].work+' worker</li>');
437 if(family.members[i].age<=45 && family.members[i].sex=="Female"){
438 if(family.members[i].state=="pregnant"){
439 $("#show-member-points").append('<li class="list-group-item">'+family.members[i].name+' is pregnant</li>');
440 }
441 else if(family.members[i].state=="lactb6"){
442 $("#show-member-points").append('<li class="list-group-item">'+family.members[i].name+' is lactating (below 6 months)</li>');
443 }
444 if(family.members[i].state=="lacto6"){
445 $("#show-member-points").append('<li class="list-group-item">'+family.members[i].name+' is lactating (over 6 months)</li>');
446 }
447 }
448 }
449 }
450
451 $("#show-foods").html("<tr><th>Name</th><th>Amount (mg or ml)</th></tr>");
452 for(i=0; i<foods.length; i++){
453 $("#show-foods").append("<tr><td>"+foods[i].name+"</td><td>"+foods[i].amount+"</td></tr>");
454 }
455
456 $("#show-requirement").html("<tr><th>Member</th><th>Energy</th><th>Protein</th><th>Iron</th><th>Vitamin A</th><th>Thiamine</th><th>Riboflavin</th><th>Vitamin C</th></tr>");
457 for(i=0; i<family.members.length; i++){
458 $("#show-requirement").append("<tr><td>"+family.members[i].name+"</td><td>"+family.members[i].rda.energy.toFixed(2)+" kcal</td><td>"+family.members[i].rda.protein.toFixed(2)+" gm</td><td>"+family.members[i].rda.iron.toFixed(2)+" mg</td><td>"+family.members[i].rda.vitA.toFixed(2)+" &mu;g</td><td>"+family.members[i].rda.thiamine.toFixed(2)+" mg</td><td>"+family.members[i].rda.riboflavin.toFixed(2)+" mg</td><td>"+family.members[i].rda.vitC.toFixed(2)+" mg</td></tr>");
459 }
460 $("#show-requirement").append("<tr><th>Total</th><th>"+total.requirement.energy.toFixed(2)+" kcal</th><th>"+total.requirement.protein.toFixed(2)+" gm</th><th>"+total.requirement.iron.toFixed(2)+" mg</th><th>"+total.requirement.vitA.toFixed(2)+" &mu;g</th><th>"+total.requirement.thiamine.toFixed(2)+" mg</th><th>"+total.requirement.riboflavin.toFixed(2)+" mg</th><th>"+total.requirement.vitC.toFixed(2)+" mg</th></tr>");
461
462 $("#show-value").html("<tr><th>Food</th><th>Energy</th><th>Protein</th><th>Iron</th><th>Vitamin A</th><th>Thiamine</th><th>Riboflavin</th><th>Vitamin C</th></tr>");
463 for(i=0; i<foods.length; i++){
464 $("#show-value").append("<tr><td>"+foods[i].name+"</td><td>"+foods[i].intake.energy.toFixed(2)+"</td><td>"+foods[i].intake.protein.toFixed(2)+"</td><td>"+foods[i].intake.iron.toFixed(2)+"</td><td>"+foods[i].intake.vitA.toFixed(2)+"</td><td>"+foods[i].intake.thiamine.toFixed(2)+"</td><td>"+foods[i].intake.riboflavin.toFixed(2)+"</td><td>"+foods[i].intake.vitC.toFixed(2)+"</td></tr>");
465 }
466 $("#show-value").append("<tr><th>Total</th><th>"+total.intake.energy.toFixed(2)+"</th><th>"+total.intake.protein.toFixed(2)+"</th><th>"+total.intake.iron.toFixed(2)+"</th><th>"+total.intake.vitA.toFixed(2)+"</th><th>"+total.intake.thiamine.toFixed(2)+"</th><th>"+total.intake.riboflavin.toFixed(2)+"</th><th>"+total.intake.vitC.toFixed(2)+"</th></tr>");
467
468 $("#show-compare").html("<tr><th>Nutrient</th><th>Requirement</th><th>Intake</th><th>Deficeiency / Excess</th></tr>");
469 $("#show-summary").html("");
470 if(total.diff.energy.state=="+"){
471 $("#show-compare").append("<tr><td>Energy</td><td>"+total.requirement.energy.toFixed(2)+"</td><td>"+total.intake.energy.toFixed(2)+"</td><td>"+total.diff.energy.value.toFixed(2)+" excess</td></tr>");
472 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Energy consumption is excess by "+((total.diff.energy.value*100)/total.requirement.energy).toFixed(2)+"%</li>");
473 }
474 else{
475 $("#show-compare").append("<tr><td>Energy</td><td>"+total.requirement.energy.toFixed(2)+"</td><td>"+total.intake.energy.toFixed(2)+"</td><td>"+total.diff.energy.value.toFixed(2)+" deficient</td></tr>");
476 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Energy consumption is deficient by "+((total.diff.energy.value*100)/total.requirement.energy).toFixed(2)+"%</li>");
477 }
478 if(total.diff.protein.state=="+"){
479 $("#show-compare").append("<tr><td>Protein</td><td>"+total.requirement.protein.toFixed(2)+"</td><td>"+total.intake.protein.toFixed(2)+"</td><td>"+total.diff.protein.value.toFixed(2)+" excess</td></tr>");
480 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Protein consumption is excess by "+((total.diff.protein.value*100)/total.requirement.protein).toFixed(2)+"%</li>");
481 }
482 else{
483 $("#show-compare").append("<tr><td>Protein</td><td>"+total.requirement.protein.toFixed(2)+"</td><td>"+total.intake.protein.toFixed(2)+"</td><td>"+total.diff.protein.value.toFixed(2)+" deficient</td></tr>");
484 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Protein consumption is deficient by "+((total.diff.protein.value*100)/total.requirement.protein).toFixed(2)+"%</li>");
485 }
486 if(total.diff.iron.state=="+"){
487 $("#show-compare").append("<tr><td>Iron</td><td>"+total.requirement.iron.toFixed(2)+"</td><td>"+total.intake.iron.toFixed(2)+"</td><td>"+total.diff.iron.value.toFixed(2)+" excess</td></tr>");
488 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Iron consumption is excess by "+((total.diff.iron.value*100)/total.requirement.iron).toFixed(2)+"%</li>");
489 }
490 else{
491 $("#show-compare").append("<tr><td>Iron</td><td>"+total.requirement.iron.toFixed(2)+"</td><td>"+total.intake.iron.toFixed(2)+"</td><td>"+total.diff.iron.value.toFixed(2)+" deficient</td></tr>");
492 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Iron consumption is deficient by "+((total.diff.iron.value*100)/total.requirement.iron).toFixed(2)+"%</li>");
493 }
494 if(total.diff.vitA.state=="+"){
495 $("#show-compare").append("<tr><td>Vitamin A</td><td>"+total.requirement.vitA.toFixed(2)+"</td><td>"+total.intake.vitA.toFixed(2)+"</td><td>"+total.diff.vitA.value.toFixed(2)+" excess</td></tr>");
496 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Vitamin A consumption is excess by "+((total.diff.vitA.value*100)/total.requirement.vitA).toFixed(2)+"%</li>");
497 }
498 else{
499 $("#show-compare").append("<tr><td>Vitamin A</td><td>"+total.requirement.vitA.toFixed(2)+"</td><td>"+total.intake.vitA.toFixed(2)+"</td><td>"+total.diff.vitA.value.toFixed(2)+" deficient</td></tr>");
500 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Vitamin A consumption is deficient by "+((total.diff.vitA.value*100)/total.requirement.vitA).toFixed(2)+"%</li>");
501 }
502 if(total.diff.thiamine.state=="+"){
503 $("#show-compare").append("<tr><td>Thiamine</td><td>"+total.requirement.thiamine.toFixed(2)+"</td><td>"+total.intake.thiamine.toFixed(2)+"</td><td>"+total.diff.thiamine.value.toFixed(2)+" excess</td></tr>");
504 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Thiamine consumption is excess by "+((total.diff.thiamine.value*100)/total.requirement.thiamine).toFixed(2)+"%</li>");
505 }
506 else{
507 $("#show-compare").append("<tr><td>Thiamine</td><td>"+total.requirement.thiamine.toFixed(2)+"</td><td>"+total.intake.thiamine.toFixed(2)+"</td><td>"+total.diff.thiamine.value.toFixed(2)+" deficient</td></tr>");
508 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Thiamine consumption is deficient by "+((total.diff.thiamine.value*100)/total.requirement.thiamine).toFixed(2)+"%</li>");
509 }
510 if(total.diff.riboflavin.state=="+"){
511 $("#show-compare").append("<tr><td>Riboflavin</td><td>"+total.requirement.riboflavin.toFixed(2)+"</td><td>"+total.intake.riboflavin.toFixed(2)+"</td><td>"+total.diff.riboflavin.value.toFixed(2)+" excess</td></tr>");
512 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Riboflavin consumption is excess by "+((total.diff.riboflavin.value*100)/total.requirement.riboflavin).toFixed(2)+"%</li>");
513 }
514 else{
515 $("#show-compare").append("<tr><td>Riboflavin</td><td>"+total.requirement.riboflavin.toFixed(2)+"</td><td>"+total.intake.riboflavin.toFixed(2)+"</td><td>"+total.diff.riboflavin.value.toFixed(2)+" deficient</td></tr>");
516 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Riboflavin consumption is deficient by "+((total.diff.riboflavin.value*100)/total.requirement.riboflavin).toFixed(2)+"%</li>");
517 }
518 if(total.diff.vitC.state=="+"){
519 $("#show-compare").append("<tr><td>Vitamin C</td><td>"+total.requirement.vitC.toFixed(2)+"</td><td>"+total.intake.vitC.toFixed(2)+"</td><td>"+total.diff.vitC.value.toFixed(2)+" excess</td></tr>");
520 $("#show-summary").append("<li class='list-group-item list-group-item-success'>Vitamin C consumption is excess by "+((total.diff.vitC.value*100)/total.requirement.vitC).toFixed(2)+"%</li>");
521 }
522 else{
523 $("#show-compare").append("<tr><td>Vitamin C</td><td>"+total.requirement.vitC.toFixed(2)+"</td><td>"+total.intake.vitC.toFixed(2)+"</td><td>"+total.diff.vitC.value.toFixed(2)+" deficient</td></tr>");
524 $("#show-summary").append("<li class='list-group-item list-group-item-danger'>Vitamin C consumption is deficient by "+((total.diff.vitC.value*100)/total.requirement.vitC).toFixed(2)+"%</li>");
525 }
526 }
527
528 function enum_date(){
529 for(i=2010; i<=2020; i++){
530 if(i==2014){
531 $("#date-year").append("<option selected>"+i+"</option>");
532 }
533 else{
534 $("#date-year").append("<option>"+i+"</option>");
535 }
536 }
537 for(i=1; i<=12; i++){
538 if(i==10){
539 $("#date-month").append("<option selected>"+i+"</option>");
540 }
541 else{
542 $("#date-month").append("<option>"+i+"</option>");
543 }
544 }
545 adjust_day(10);
546 }
547
548 function adjust_day(month){
549 lim=31;
550 if(month==2){
551 lim=28;
552 }
553 else if(month==4||month==6||month==9||month==11){
554 lim=30;
555 }
556 day=$("#date-day").val();
557 $("#date-day").html("");
558 if(day>lim){
559 day=lim;
560 }
561 for(i=1; i<=lim; i++){
562 if(i==day){
563 $("#date-day").append("<option selected>"+i+"</option>");
564 }
565 else{
566 $("#date-day").append("<option>"+i+"</option>");
567 }
568 }
569 }
570
571 function display_member(mem, i){
572 html='<div class="panel panel-success">'
573 +'<div class="panel-heading">'
574 +'<div class="row">'
575 +'<div class="col-xs-offset-11 col-xs-1"><span data-id="'+i+'" class="point remove-member text-warning"><span data-id="'+i+'" class="glyphicon glyphicon-remove"></span></span></div>'
576 +'</div>'
577 +'</div>'
578 +'<div class="panel-body">'
579 +'<form class="form-horizontal" role="form">'
580 +'<div class="form-group">'
581 +'<label class="col-sm-3 control-label">Name:</label>'
582 +'<div class="col-sm-9">'
583 +'<p class="form-control-static">'+mem.name+'</p>'
584 +'</div>'
585 +'</div>'
586 +'<div class="form-group">'
587 +'<label class="col-sm-3 control-label">Age:</label>'
588 +'<div class="col-sm-9">'
589 +'<p class="form-control-static">'+mem.age+'</p>'
590 +'</div>'
591 +'</div>'
592 +'<div class="form-group">'
593 +'<label class="col-sm-3 control-label">Sex:</label>'
594 +'<div class="col-sm-9">'
595 +'<p class="form-control-static">'+mem.sex+'</p>'
596 +'</div>'
597 +'</div>';
598 if(mem.age>=18){
599 html=html+'<div class="form-group">'
600 +'<label class="col-sm-3 control-label">Work Type:</label>'
601 +'<div class="col-sm-9">'
602 +'<p class="form-control-static">'+mem.work+'</p>'
603 +'</div>'
604 +'</div>'
605 }
606 html=html+'</form>'
607 +'</div>';
608 if(mem.age>=18 && mem.age<=45 && mem.sex=="Female"){
609 if(mem.state=="npnl"){
610 state="";
611 }
612 else if(mem.state=="preg"){
613 state="Pregnant";
614 }
615 else if(mem.state=="lactb6"){
616 state="Lactating, below 6 months";
617 }
618 else if(mem.state=="lacto6"){
619 state="Lactating, over 6 months";
620 }
621 if(state!=""){
622 html=html+'<div class="panel-footer">'
623 +'<div class="text-center">'
624 +'<p class="form-control-static">'+state+'</p>'
625 +'</div>'
626 +'</div>';
627 }
628 }
629 html=html+'</div>';
630 return html;
631 }
632
633 function display_food(item, i){
634 html='<tr><td class="col-xs-6">'+item.name+'</td><td class="col-xs-5">'+item.amount+'</td><td class="col-xs-1 text-center"><span class="point remove-food text-warning" data-id="'+i+'"><span class="glyphicon glyphicon-remove" data-id="'+i+'"></span></span></td></tr>';
635 return html;
636 }
637
638 function draw_bar_diagram(){
639 data={
640 labels: ["Energy", "Protein", "Iron", "Vitamine A", "Thiamine", "Riboflavin", "Vitamin C"],
641 datasets: [
642 {
643 label: "dataset",
644 fillColor: "rgba(151,187,205,0.5)",
645 strokeColor: "rgba(151,187,205,0.8)",
646 highlightFill: "rgba(151,187,205,0.75)",
647 highlightStroke: "rgba(151,187,205,1)",
648 data: [parseFloat(((total.intake.energy*100)/total.requirement.energy).toFixed(2)), parseFloat(((total.intake.protein*100)/total.requirement.protein).toFixed(2)), parseFloat(((total.intake.iron*100)/total.requirement.iron).toFixed(2)), parseFloat(((total.intake.vitA*100)/total.requirement.vitA).toFixed(2)), parseFloat(((total.intake.thiamine*100)/total.requirement.thiamine).toFixed(2)), parseFloat(((total.intake.riboflavin*100)/total.requirement.riboflavin).toFixed(2)), parseFloat(((total.intake.vitC*100)/total.requirement.vitC).toFixed(2))]
649 }
650 ]
651 };
652
653
654 var ctx=$("#bar-diagram").get(0).getContext("2d");
655 var diagram=new Chart(ctx).Bar(data);
656 }