PNA.fi koodi

ruoka.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. function is_in_list(list, num) {
  2. for (i = 0; i < list.length; i++) {
  3. if (list[i] == num)
  4. return true;
  5. }
  6. return false;
  7. }
  8. function any_allergies_set()
  9. {
  10. for (a = 0; a < allergies.length; a++) {
  11. var e = document.getElementById("allergy_" + allergies[a]);
  12. if (e.checked)
  13. return true;
  14. }
  15. return false;
  16. }
  17. function unhighlight() {
  18. for (f = 0; f < food_count; f++) {
  19. var e = document.getElementById("f"+f);
  20. e.style.background = "#ffffff";
  21. }
  22. }
  23. function fix_hrefs() {
  24. // change all links to submit-buttons
  25. for (f = 0; f < document.links.length; f++) {
  26. var href = document.links[f].href;
  27. var i = href.lastIndexOf('/') + 1;
  28. if (href[i] >= '1' && href[i] <= '7' && href[i+1] == '.') {
  29. document.links[f].onclick = function() {
  30. document.forms[0].action = this.href;
  31. document.forms[0].submit();
  32. return false;
  33. }
  34. }
  35. }
  36. }
  37. function highlight() {
  38. fix_hrefs();
  39. if (!any_allergies_set()) {
  40. unhighlight();
  41. return;
  42. }
  43. for (f = 0; f < food_count; f++) {
  44. var eatable = 1;
  45. for (a = 0; a < allergies.length; a++) {
  46. var e = document.getElementById("allergy_" + allergies[a]);
  47. if (e.checked) {
  48. if (is_in_list(eatable_foods[allergies[a]], f)) {
  49. // still (maybe) eatable
  50. } else if (is_in_list(maybe_eatable_foods[allergies[a]], f)) {
  51. // just maybe eatable
  52. eatable = 0;
  53. } else {
  54. eatable = -1;
  55. }
  56. }
  57. }
  58. var e = document.getElementById("f"+f);
  59. if (eatable < 0) {
  60. e.style.background = "#ffffff";
  61. //e.style.display = "none";
  62. } else {
  63. //e.style.display = "list-item";
  64. e.style.background = eatable > 0 ? "#ffff00" : "#cccccc";
  65. }
  66. }
  67. }
  68. function url_param(name)
  69. {
  70. // From http://www.netlobo.com/url_query_string_javascript.html
  71. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  72. var regexS = "[\\?&]"+name+"=([^&#]*)";
  73. var regex = new RegExp( regexS );
  74. var results = regex.exec( window.location.href );
  75. if( results == null )
  76. return "";
  77. else
  78. return results[1];
  79. }
  80. function set_allergies()
  81. {
  82. for (a = 0; a < allergies.length; a++) {
  83. var e = document.getElementById("allergy_" + allergies[a]);
  84. if (url_param("allergy_" + allergies[a]) != "")
  85. e.checked = true;
  86. }
  87. highlight();
  88. }