PNA.fi koodi

ruoka.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. }
  89. var NO_WARNING_SETTING = "pna-no-warning";
  90. function shouldShowWarning() {
  91. if (window.localStorage) {
  92. return !localStorage[NO_WARNING_SETTING];
  93. } else {
  94. return document.cookie.indexOf(NO_WARNING_SETTING) == -1;
  95. }
  96. }
  97. function noMoreWarning() {
  98. if (window.localStorage) {
  99. localStorage[NO_WARNING_SETTING] = "true";
  100. } else {
  101. document.cookie += NO_WARNING_SETTING + "=true; expires=Tue, 19 Jan 2038 03:14:07 GMT";
  102. }
  103. }
  104. function show_warning() {
  105. var notice = document.getElementById("notice");
  106. if (shouldShowWarning()) {
  107. var input = document.createElement("input");
  108. input.type="submit";
  109. input.value="Älä näytä tätä enää";
  110. input.onclick = function() {
  111. notice.parentNode.removeChild(notice);
  112. noMoreWarning();
  113. return false;
  114. }
  115. notice.appendChild(input)
  116. } else {
  117. notice.parentNode.removeChild(notice);
  118. }
  119. }