/*** Sets the values which the availability calendar shifts by when scrolled through (seen on selectCategoryHotel.do). ***/
function initRoomAvailability(){
  document.getElementById('availability').style.clip = "rect(0,0,30px,586px)";
  document.getElementById('availability').style.height = "60px";
  document.getElementById('availability').style.overflow = "hidden";
}

function changeAvailabilityMonth(direction){
  var calPosition=document.getElementById('availability_months').style.top;
  var calPosition=calPosition.replace('px','')*1;
  var shiftValue=76;
  if (direction=='forward'){
    document.getElementById('availability_months').style.top=(calPosition-shiftValue)+'px';
  } else {
    document.getElementById('availability_months').style.top=(calPosition+shiftValue)+'px';
  }
}
/*** Ends  ***/


formReference = document.forms.hotels_search;

/*** Sets the correct number of days in the pulldown menu when a month is selected  ***/
function selectMonth(dayField,monthField){        
  var selectedMonth = monthField.options[monthField.selectedIndex].value;
  var selectedDay = dayField.options[dayField.selectedIndex].value;
  var selectedYear;
  var today = new Date();
  var thisYear=today.getYear();
  if(thisYear<2000)thisYear+=1900;
  var febDate=new Date(thisYear,(selectedMonth-1),selectedDay);
  if (febDate<today){selectedYear = thisYear+1;}
  else{selectedYear = thisYear;}

  var days = getNumDays(selectedMonth,selectedYear);
  dayField.length=days;        
  
  for(var i=1;i<=days;i++){
      dayField.options[i-1] = null;
      dayField.options[i-1] = new Option(i,i);}

  if (selectedDay <= dayField.options.length){
  	dayField.selectedIndex=selectedDay-1;}
}           

function getNumDays(M,Y){
  if(M==2){
    var febDate=new Date(Date.UTC(Y,1,29)); 
    return (febDate.getMonth()==1)?29:28; } 
  else return (M==9||M==4||M==6||M==11)?30:31;
}

/*** Updates form objects: lmnCheckOutDay, lmnCheckOutDayDisplay, lmnCheckOutMonthDisplay and formReference. */	 
function updateCheckOut(value) {		
  var months_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  
  // save number of nights
  no_nights = parseInt(value);
  
  // check that it is a valid number
  if(isNaN(no_nights)||no_nights<=0) {
    //alert("Please enter a valid number of nights"); 
  }
  else {
    // construct the check in date
    // for this we need to decide if the month selected is before current month, if so
    // user is booking for next year
    d = new Date();
    y = d.getFullYear();
    m1 = d.getMonth();			
    m2 = formReference.lmnCheckInMonth.value-1; // month is not 0 based in calendar!!
    if(m2<m1) { y = y+1; }

    // now create the proper check in date
    real_checkin_s = formReference.lmnCheckInDay.value+" "+months_array[parseInt(formReference.lmnCheckInMonth.value)-1]+" "+y;
    real_checkin_d = new Date(real_checkin_s);
    dayOfCheckInMonth = real_checkin_d.getDate();
    dayOfCheckOutMonth = dayOfCheckInMonth + no_nights;
    real_checkout_d = real_checkin_d;
    real_checkout_d.setDate(dayOfCheckOutMonth);

    // get day and month for check out
    check_out_day = real_checkout_d.getDate();
    check_out_month = nnMonthsArray[real_checkout_d.getMonth()];

    // show user
    formReference.lmnCheckOutDay.value=check_out_day;
    formReference.lmnCheckOutMonth.value=(real_checkout_d.getMonth()+1);
    formReference.lmnCheckOutDisplay.value=check_out_day+' '+check_out_month;
  }
}

/* link the radio buttons for the search categories in IE */
lm.forEach(lm.getElementsBy({tagName:'input'}),function(item)
{
	if(item.type=='radio' && item.checked==false && item.name=='' && item.parentNode.tagName=='A') 
	{
		lm.addEvent({e:item, eT:'click', fn:function(){ window.location = item.parentNode.href}})
	}
})


/* change child number dropdown range depending on number of adults 
lm.forEach(lm.getElementsBy({tagName:'select',parentNode:'roomAllocationContainer',filter:function(item){return item.id.match(/(adults|children)[0-9]/)}}),
			function(item){
				lm.addEvent({
					e:item,
					eT:'change',
					fn:function(item){
						if(item.target)	var targetId = item.target
						else var targetId = item.srcElement					 		
						changeDropDownRange(targetId)
					}
				})
			}
)
*/


hotelRoomAllocationDropDown = {
	selectOnChange:function(){
			lm.forEach(lm.getElementsBy({tagName:'select',parentNode:'roomAllocationContainer',filter:function(item){return item.id.match(/(adults|children)[0-9]/)}}),
			function(item){
				lm.addEvent({
					e:item,
					eT:'change',
					fn:function(item){
						if(item.target)	var targetId = item.target
						else var targetId = item.srcElement	
						if(targetId.id.match(/adults/)){
							adultNum = targetId.value;
							childrenNode = document.getElementById(targetId.id.replace('adults','children'));
							optionStart = 0;
						}else{
							adultNum = lm.e(targetId).value;
							childrenNode = document.getElementById(targetId.id.replace('children','adults'));	
							optionStart = 1;
						}
						var initialValue = childrenNode.value
						childrenNumChange = 5 - adultNum;
						if(initialValue > childrenNumChange){
							initialValue == childrenNumChange
						}
						lm.forEach(lm.getElementsBy({parentNode:childrenNode, tagName:'option'}), function(item){
							childrenNode.removeChild(item)
						})
						for(i=optionStart;i<childrenNumChange;i++){
							var newOption = lm.ce('option')
							newOption.value = i
							newOption.innerHTML = i
							if(i==initialValue) newOption.selected = 'selected'
							childrenNode.appendChild(newOption)
						}
					}
				})
			}
		)
	},
	init: function(){
		lm.forEach(lm.getElementsBy({tagName:'select',parentNode:'roomAllocationContainer',filter:function(item){return item.id.match(/(adults|children)[0-9]/)}}), function(item){
			if(item.id.match(/adults/)){
				adultNum = item.value;
				childrenNode = document.getElementById(item.id.replace('adults','children'));
				optionStart = 0;
			}else{
				adultNum = lm.e(item).value;
				childrenNode = document.getElementById(item.id.replace('children','adults'));	
				optionStart = 1;
			}
			var initialValue = childrenNode.value
			childrenNumChange = 5 - adultNum;
			if(initialValue > childrenNumChange){
				initialValue == childrenNumChange
			}
			lm.forEach(lm.getElementsBy({parentNode:childrenNode, tagName:'option'}), function(item){
				childrenNode.removeChild(item)
			})
			for(i=optionStart;i<childrenNumChange;i++){
				var newOption = lm.ce('option')
				newOption.value = i
				newOption.innerHTML = i
				if(i==initialValue) newOption.selected = 'selected'
				childrenNode.appendChild(newOption)
			}
	})
		this.selectOnChange()
	}
}
hotelRoomAllocationDropDown.init()
