PageRenderTime 55ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/server/public/common/v_003/javascripts/application.js

https://github.com/Domo/vz.designer
JavaScript | 356 lines | 296 code | 54 blank | 6 comment | 54 complexity | 7ed06cc8f7783164030a2f00c4641c20 MD5 | raw file
  1. // Place your application-specific JavaScript functions and classes here
  2. // This file is automatically included by javascript_include_tag :defaults
  3. // Try to be compatible with other browsers
  4. // Only use firebug logging when available
  5. if (typeof console == 'undefined') {
  6. console = new Object;
  7. console.trace = function() {};
  8. console.log = function() {};
  9. console.debug = function() {};
  10. console.info = function() {};
  11. console.warn = function() {};
  12. console.error = function() {};
  13. console.time = function() {};
  14. console.timeEnd = function() {};
  15. console.count = function() {};
  16. }
  17. function toggle_room(id) {
  18. Element.toggle(id);
  19. Element.toggleClassName( "room_" + id, 'active');
  20. return false;
  21. }
  22. function disableButton(element) {
  23. $(element).disabled = true;
  24. $(element).addClassName('disabled');
  25. }
  26. function card_cvv(select_card, creditcard_verification_value) {
  27. select = $(select_card)
  28. if(select.selectedIndex == 1){
  29. $("cvv_text").hide()
  30. } else {
  31. $("cvv_text").show();
  32. }
  33. }
  34. var ME = Class.create();
  35. ME.toggle = function(element){
  36. new Effect.toggle(element,'slide', { duration: 0.3 });
  37. return false;
  38. }
  39. var ReservationsManager = Class.create();
  40. ReservationsManager.prototype = {
  41. room_type_settings: $H({}),
  42. conditions_settings: $H({}),
  43. basic_template: null,
  44. adults_option_tags_template: null,
  45. children_option_tags_template: null,
  46. roomTypePrice: 0,
  47. totalPrice: 0,
  48. initialize: function(_room_type_settings, _conditions_settings,
  49. _basic_template, _adults_option_tags_template, _children_option_tags_template,
  50. _template_min_number_of_rooms_error_message, _template_min_number_of_people_error_message,
  51. _template_max_number_of_people_error_message) {
  52. this.room_type_settings = $H(_room_type_settings);
  53. this.conditions_settings = $H(_conditions_settings);
  54. this.basic_template = new Template(_basic_template);
  55. this.adults_option_tags_template = new Template(_adults_option_tags_template);
  56. this.children_option_tags_template = new Template(_children_option_tags_template);
  57. this.template_min_number_of_rooms_error_message = new Template(_template_min_number_of_rooms_error_message);
  58. this.template_min_number_of_people_error_message = new Template(_template_min_number_of_people_error_message);
  59. this.template_max_number_of_people_error_message = new Template(_template_max_number_of_people_error_message);
  60. },
  61. numberOfRoomsChanged: function(_id, _container_name, table_id, select_tag) {
  62. var room_definitions = $$('#' + table_id + ' tr.room_definition');
  63. var actual_count = room_definitions.length;
  64. var requested_count = $(select_tag).options[$(select_tag).selectedIndex].value;
  65. diff = requested_count - actual_count;
  66. if (diff == 0) { return; }
  67. if (diff > 0) {
  68. min_adults = this.room_type_settings.get(_id)['min_adults'] || 0;
  69. adults = this.room_type_settings.get(_id)['adults'] || 0;
  70. children = this.room_type_settings.get(_id)['children'] || 0;
  71. max_children = this.room_type_settings.get(_id)['max_children'] || 0;
  72. text = ''
  73. text = template_text = this.defineRooms(_id, _container_name, min_adults, adults, children, max_children, diff, actual_count, table_id, select_tag);
  74. new Insertion.Bottom($$('#' + table_id + ' tbody')[0], text);
  75. } else {
  76. for(i=1;i<=Math.abs(diff);i++) {
  77. room_definitions[actual_count - i].remove();
  78. }
  79. }
  80. this.recalculateRoomTypePrice(_id, _container_name);
  81. var errors = $A([]);
  82. errors = this.validateMinNumberOfRooms(requested_count, this.conditions_settings.get(_id)['min_rooms']);
  83. errors = errors.concat(this.validateMinNumberOfPeople(_id, _container_name));
  84. this.publishConditionsErrors(_id, errors, requested_count);
  85. },
  86. pluralizeLabel: function(_id, _count, _singular) {
  87. _id = $(_id);
  88. _id.innerHTML = pluralize_without_count({count:_count,singular:_singular});
  89. },
  90. numberOfPeopleChanged: function(_id, _container_name, _table_id, _select_tag, _i, _singular) {
  91. var errors = $A([]);
  92. var requested_count = $(_select_tag).options[$(_select_tag).selectedIndex].value;
  93. id = _container_name+'_'+_id+'_'+_singular+'_'+_i.toString();
  94. count = $(id).options[$(id).selectedIndex].value;
  95. //this.pluralizeLabel(id+'_label', count, _singular);
  96. this.recalculateRoomTypePrice(_id, _container_name);
  97. errors = this.validateMinNumberOfRooms(requested_count, this.conditions_settings.get(_id)['min_rooms']);
  98. errors = errors.concat(this.validateMinNumberOfPeople(_id, _container_name));
  99. this.publishConditionsErrors(_id, errors, requested_count);
  100. },
  101. publishConditionsErrors: function(_id, _errors, _rooms) {
  102. var conditionsContainer = $('conditions_' + _id.toString());
  103. var html = '';
  104. if (_errors.length > 0) {
  105. html = _errors.inject('', function(str, error) {
  106. str += '<li>' + error + '</li>';
  107. return str;
  108. });
  109. } else {
  110. html = '<div style="color:#0d0;">You have selected ' + pluralize({count: _rooms, singular: "room"}) + '.</div>';
  111. }
  112. if (conditionsContainer != null) {
  113. conditionsContainer.innerHTML = html;
  114. }
  115. },
  116. validateMinNumberOfRooms: function(_rooms_selected, _min_rooms_required) {
  117. if (_min_rooms_required == NaN || _min_rooms_required == null) _min_rooms_required = 0;
  118. if (_rooms_selected < _min_rooms_required) {
  119. return $A([this.template_min_number_of_rooms_error_message.evaluate({min_rooms_required: pluralize({count: _min_rooms_required, singular: 'room'})})])
  120. }
  121. return $A([]);
  122. },
  123. validateMinNumberOfPeople: function(_id, _container_name) {
  124. _min_people_required = this.conditions_settings.get(_id)['min_people'];
  125. _max_people_required = this.conditions_settings.get(_id)['max_people'];
  126. if (_min_people_required == NaN || _min_people_required == null) _min_people_required = 0;
  127. if (_max_people_required == NaN || _max_people_required == null) _max_people_required = 0;
  128. var adults = $A([]); var children = $A([]);
  129. adults_nodes = $$('select.' + _container_name + '_' + _id + '_adults');
  130. adults = adults_nodes.inject([], function(acc, adult_select) {
  131. acc.push(parseInt(adult_select.options[adult_select.selectedIndex].value));
  132. return acc;
  133. });
  134. children_nodes = $$('select.' + _container_name + '_' + _id + '_children');
  135. children = children_nodes.inject([], function(acc, children_select) {
  136. acc.push(parseInt(children_select.options[children_select.selectedIndex].value));
  137. return acc;
  138. });
  139. var errors = $A([]);
  140. for(i=0;i<adults.length;i++) {
  141. if ((adults[i] + children[i]) < _min_people_required && _min_people_required != 0) {
  142. errors.push(this.template_min_number_of_people_error_message.evaluate({i: i + 1, min_people_required: pluralize({count:_min_people_required,singular:'person'})}));
  143. }
  144. if ((adults[i] + children[i]) > _max_people_required && _max_people_required != 0) {
  145. errors.push(this.template_max_number_of_people_error_message.evaluate({i: i + 1, max_people_required: pluralize({count:max_people_required,singular:'person'})}));
  146. }
  147. }
  148. return errors;
  149. },
  150. defineRooms: function(_id, _container_name, _min_adults, _adults, _children, _max_children, _rooms, _from_number, _table_id, _select_tag) {
  151. var data = $H({
  152. adults_option_tags: this.renderAdultsOptions(_min_adults, _adults),
  153. children_option_tags: this.renderChildrenOptions(_children, _max_children),
  154. id: _id,
  155. container_name: _container_name,
  156. i: 0,
  157. table_id: _table_id,
  158. select_tag: _select_tag
  159. });
  160. text = ''
  161. for (i=0;i<diff;i++) {
  162. data.set('i', _from_number + i + 1);
  163. text += this.basic_template.evaluate(data);
  164. }
  165. return text;
  166. },
  167. renderAdultsOptions: function(min_adults, adults) {
  168. adults_options = '';
  169. for(j=min_adults;j<=adults;j++) {
  170. adults_options += this.adults_option_tags_template.evaluate({i: j});
  171. }
  172. return adults_options;
  173. },
  174. renderChildrenOptions: function(children, max_children) {
  175. children_options = '';
  176. for(j=children;j<=max_children;j++) {
  177. children_options += this.children_option_tags_template.evaluate({i: j});
  178. }
  179. return children_options;
  180. },
  181. recalculateTotalPrice: function() {
  182. this.totalPrice = this.recalculateTotalStandardRoomTypePrice() + this.recalculateTotalRatePlanRoomTypePrice();
  183. //$('total_price').innerHTML = this.totalPrice.toString();
  184. },
  185. recalculateTotalStandardRoomTypePrice: function() {
  186. var prices = $$('span.room_type_price');
  187. var totalPrice = 0;
  188. totalPrice = prices.inject(0, function(totalPrice, price){
  189. price_int = parseInt(price.innerHTML);
  190. price_int = (price_int == NaN) ? 0 : price_int;
  191. totalPrice += price_int;
  192. return totalPrice;
  193. }.bind(this));
  194. return totalPrice;
  195. },
  196. recalculateTotalRatePlanRoomTypePrice: function() {
  197. var prices = $$('span.rate_plan_room_type_price');
  198. var totalPrice = 0;
  199. totalPrice = prices.inject(0, function(totalPrice, price){
  200. price_int = parseInt(price.innerHTML);
  201. price_int = (price_int == NaN) ? 0 : price_int;
  202. totalPrice += price_int;
  203. return totalPrice;
  204. }.bind(this));
  205. return totalPrice;
  206. },
  207. recalculateRoomTypePrice: function(result_id, _container_name) {
  208. var adults = this.getAdultsNumbers(result_id, _container_name);
  209. var children = this.getChildrenNumbers(result_id, _container_name);
  210. var price = this.getRoomTypePrice(result_id, adults, children);
  211. var priceTag = $(_container_name + '_' + result_id + '_price');
  212. if (priceTag) {
  213. priceTag.innerHTML = price.toString();
  214. }
  215. this.recalculateTotalPrice();
  216. },
  217. getAdultsNumbers: function(result_id, _container_name) {
  218. var adultsSelects = $$('select.' + _container_name + '_' + result_id + '_adults');
  219. var numbers = $A();
  220. numbers= adultsSelects.inject([], function(arr, aSelect) {
  221. arr.push(this.getNumberOfAdults(aSelect));
  222. return arr;
  223. }.bind(this));
  224. return numbers;
  225. },
  226. getNumberOfAdults: function(select) {
  227. var adults = parseInt(select.options[select.selectedIndex].value);
  228. return ((adults == NaN) ? 0 : adults);
  229. },
  230. getChildrenNumbers: function(result_id, _container_name) {
  231. var childrenSelects = $$('select.' + _container_name + '_' + result_id + '_children');
  232. var numbers = $A();
  233. numbers = childrenSelects.inject([], function(arr, cSelect) {
  234. arr.push(this.getNumberOfChildren(cSelect));
  235. return arr;
  236. }.bind(this));
  237. return numbers;
  238. },
  239. getNumberOfChildren: function(select) {
  240. var children = parseInt(select.options[select.selectedIndex].value);
  241. return ((children == NaN) ? 0 : children);
  242. },
  243. getRoomTypePrice: function(result_id, adults, children) {
  244. this.roomTypePrice = 0;
  245. this.roomTypePrice = this.room_type_settings.get(result_id)['sell_rate'] * adults.size();
  246. this.roomTypePrice += adults.inject(0, function(acc, number_of_adults) {
  247. acc += this.getExtraPriceForAdults(result_id, number_of_adults);
  248. return acc;
  249. }.bind(this));
  250. this.roomTypePrice += children.inject(0, function(acc, number_of_children) {
  251. acc += this.getExtraPriceForChildren(result_id, number_of_children);
  252. return acc;
  253. }.bind(this));
  254. return this.roomTypePrice;
  255. },
  256. getExtraPriceForAdults: function(result_id, number_of_adults) {
  257. var min_adults = this.room_type_settings.get(result_id)['min_adults'];
  258. var extra_adult_price = this.room_type_settings.get(result_id)['extra_adult'];
  259. var diff = min_adults - number_of_adults;
  260. if (diff < 0) {
  261. return Math.abs(diff) * extra_adult_price;
  262. }
  263. return 0;
  264. },
  265. getExtraPriceForChildren: function(result_id, number_of_children) {
  266. var children = this.room_type_settings.get(result_id)['children'];
  267. var extra_child_price = this.room_type_settings.get(result_id)['extra_child'];
  268. var diff = children - number_of_children;
  269. if (diff < 0) {
  270. return Math.abs(diff) * extra_child_price;
  271. }
  272. return 0;
  273. },
  274. checkForCheckout: function() {
  275. var rooms = $$('.room_definition');
  276. if (rooms.size() > 0) {
  277. return true;
  278. }
  279. alert('Sorry, you can\'t checkout. No room selected.');
  280. return false;
  281. }
  282. }
  283. function updatePrice() {
  284. elements = document.getElementsByClassName("ticketselector");
  285. total = 0.0;
  286. for (i=0;i<elements.length;i++) {
  287. price = elements[i].title;
  288. element = elements[i];
  289. options = element.options;
  290. counter = options[element.selectedIndex].value;
  291. total = total + counter * element.title;
  292. }
  293. $("price").innerHTML = " &euro;" + round(total,2);
  294. }
  295. function round(x, n) {
  296. if (n < 1 || n > 14) return false;
  297. var e = Math.pow(10, n);
  298. var k = (Math.round(x * e) / e).toString();
  299. if (k.indexOf(".") == -1) k += ".";
  300. k += e.toString().substring(1);
  301. return k.substring(0, k.indexOf(".") + n+1);
  302. }