// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function check_number_of_rooms(select_class, max_rooms_selected) {
  var selects = $$('select.'+select_class);
  var count = 0;
  selects.each(function(e) {			
		count += parseInt(e.value);
	}.bind(this));
	if(count > max_rooms_selected){
	  alert('You can select max ' + max_rooms_selected + ' rooms.');
	  return false;
	}else if(count <= 0){
	  alert('You must choose some room.');
	  return false;
	}
	return true;
}

var TotalPriceHelper = Class.create();

TotalPriceHelper.prototype = {
			
		initialize: function(room_options, room_rate_options, room_select, occupancy_div, total_place, adult_price, child_price) {
		  this.room_options = room_options;
		  this.rate_options = room_rate_options;
		  this.rooms_select = room_select;
		  this.occupancy_div = occupancy_div;
		  this.conversion = {};
		  this.total_place = total_place;
		  this.total = 0;
		  this.room_rate_code = room_options.code + room_options.special_request_code + "_" + room_rate_options.code;
		  this.stripped_room_rate_code = room_options.code + room_options.special_request_code + "_" + room_rate_options.stripped_chars_code;
		  $(this.total_place).update('&euro;'+this.total);
		  
		  //this.roomOccupancyTemplate = new Template('<label class="room-name">#{room_name}&nbsp;#{r_number}:&nbsp;</label>&nbsp;adults:#{select_adults}&nbsp;children:#{select_children}<br />');
		  this.roomOccupancyHeader = new Template('<a class="addroom" href="javascript:void(0);" onclick="helper_' + this.stripped_room_rate_code + '.incrementRoomlist()"><span class="alt">Add room</span></a>');
		  this.roomOccupancyTemplate = new Template('<p><label class="room-name">Room&nbsp;#{r_number}:&nbsp;</label>&nbsp;adults:#{select_adults}&nbsp;<span style="display: none">children:#{select_children}</span></p>');
		  this.selectTemplate = new Template('<select name="#{name}" class="#{css}" id="#{id}" style="width:35px;margin-left:3px">#{select_options_tag}</select>');
		  this.optionTemplate = new Template('<option value="#{value}">#{value}</option>');
		  		  
		  $(this.rooms_select).onchange = function() {
				this.changeRooms($(this.rooms_select));
			}.bindAsEventListener(this);
			$(this.rooms_select).selectedIndex = 0;			
		},

		incrementRoomlist: function() {
			
			sel = $(this.rooms_select);

			if(sel.selectedIndex < (sel.options.length - 1)) {
				sel.options[sel.selectedIndex+1].selected = '1';
			}
			
			this.changeRooms(sel);
		},
		
		changeRooms: function(sel) {
		  var template = '';
		  for(n=0; n < parseInt(sel.value); n++){
		     if(n == 0){
		      template += this.getRoomOccupancyHeader(0) + this.getRoomOccupancyTemplate(n+1);
		    }else{
		      template += this.getRoomOccupancyTemplate(n+1);
		    }	
	      }
	      $(this.occupancy_div).update(template);
      
  		var selects = $$('div#' + this.occupancy_div + ' select');
			selects.each(function(s) {
				s.onchange = function(e) {					
					this.recalculate(s);
				}.bindAsEventListener(this);
				// end of onchange function for select
			}.bind(this));
			this.recalculate();
		},
		
		getRoomOccupancyHeader: function(_number) {
		  this.conversion = {room_name: this.rate_options.name};
		  var template = this.roomOccupancyHeader.evaluate(this.conversion);
		  return template;
		},
		
		getRoomOccupancyTemplate: function(_number) {
		  var room_prefix = 'room_types[' + this.room_rate_code + '][rooms_occupancy][' + _number + ']';
		  var selectChildName = room_prefix + '[children]';
		  var selectAdultsName = room_prefix + '[adults]';
		  var selectChildId = 'select_' + this.room_rate_code + '_child';
		  var selectAdultsId = 'select_' + this.room_rate_code + '_adults';
		  
		  var selectChildren = this.getSelectTemplate(selectChildName, 'select-child', selectChildId, this.room_options.min_children, this.room_options.children);
		  var selectAdults = this.getSelectTemplate(selectAdultsName, 'select-adults', selectAdultsId, this.room_options.min_adults, this.room_options.adults);
		  
		  this.conversion = {room_name: this.rate_options.name, r_number: _number, select_adults: selectAdults, select_children: selectChildren};
		  var template = this.roomOccupancyTemplate.evaluate(this.conversion);
		  return template;
		},
		
		getSelectTemplate: function(_name, _class, _id, _from, _to){
		  var select_options = '';
		  for(j=_from; j <= _to; j++){
		    this.conversion = {value: j};
		    select_options += this.optionTemplate.evaluate(this.conversion);
	    }
	    var select_tag = '';
	    this.conversion = {name: _name, css: _class, id: _id, select_options_tag: select_options};
	    select_tag = this.selectTemplate.evaluate(this.conversion);
	    return select_tag;
		},
		
		recalculate: function(sel) {
		  var adults = this.getAdultsCount();
		  var child = this.getChildCount();
		  var rooms = this.getRoomsCount();
		  diff_adults = adults-rooms;
		  this.total = (rooms*this.rate_options.base_price) + (diff_adults*this.rate_options.extra_adult_price) + (child*this.rate_options.extra_child_price);
		  if(this.checkOccupancy(sel)){
		    $(this.total_place).update('&euro;'+this.total);
	    }else{
	      alert('Max Occupancy for this room is '+this.room_options.adults);
	    }
		},
		
		getChildCount: function() {
		  var selects_child = $$('div#' + this.occupancy_div + ' select.select-child');
		  var count_child = 0;
		  selects_child.each(function(e) {
					count_child += parseInt(e.value);				 
			});
			return count_child;
		},
		
		getAdultsCount: function() {
		  var selects_adults = $$('div#' + this.occupancy_div + ' select.select-adults');
		  var count_adults = 0;
		  selects_adults.each(function(e) {
					count_adults += parseInt(e.value);				 
			});
			return count_adults;
		},
		
		getRoomsCount: function() {
		  rooms_count = $('room_type_'+this.room_rate_code+'_rooms').value;
		  return rooms_count;
		},
		
		checkOccupancy: function(sel){
		  if(sel){
  		  if(sel.name.endsWith('[children]')){
  		    var child_select = sel;
  		    var adults_select = $$('select[name="'+sel.name.gsub('children','adults')+'"]')[0];
  		  }else{
  		    var adults_select = sel;
  		    var child_select = $$('select[name="'+sel.name.gsub('adults','children')+'"]')[0];
  		  }
  		  sum = parseInt(adults_select.value) + parseInt(child_select.value);
  		  result = (this.room_options.adults >= sum)
  		  if(!result){
  		    child_select.selectedIndex = 0;
  		  }
  		  return result;
		  }else{
		    return true;
		  }
		}
}


