//this should be set from jsp
var numAvailableMonths = 11;

//partnerId javascript, used in common lmn code.
//var partnerId = 0;
if (document.cookie) {
  cookies = document.cookie.split(';');
  for (var i=0;i<cookies.length;i++){cookie = cookies[i].split('=');if (cookie[0].indexOf('partnerId')>=0) var partnerId=cookie[1]; }
}
if (!partnerId) {
  queryStr = document.location.href.substr(document.location.href.indexOf('?')+1);
  if (queryStr) {
    var tempStr = "";
    for (i=0;i<queryStr.length;i++) {if (queryStr.charAt(i) == '%') {if (queryStr.charAt(i+1) == '2') tempStr+='&';} else tempStr+=queryStr.charAt(i);}
    queryStr = tempStr;pairs = queryStr.split('&');
    for (var i=0;i<pairs.length;i++){pair = pairs[i].split('=');if (pair[0] == 'partnerId') var partnerId = pair[1];}
  }
}
if (!partnerId) var partnerId = "0";


// The form index under forms array of the search form
var formReferenceIndex;
// The search form object
var formReference;
var endDate = new Date();
var month;

   /**
    * Set values to formReferenceIndex, formReference, endDate and month
    */
   function init(){      
    	//initialises everything *after* the page has finished loading    	

		//lmnLengthOfStay is used as the field to look for as it is a 
		//mandatory field of the search form
		var currentFormIndex;		
		formReferenceIndex = 0;	
		for (currentFormIndex = 0; currentFormIndex < document.forms.length; currentFormIndex++){
			if  (document.forms[currentFormIndex].lmnLengthOfStay){				
				formReferenceIndex = currentFormIndex;				
 			}
		}        
		formReference = document.forms[formReferenceIndex];	
    	
        if(formReference.lmnLengthOfStay){
            formReference.lmnCheckOutDay.disable = true;
            formReference.lmnCheckOutMonth.disable = true;
            
            month = endDate.getMonth() + numAvailableMonths;
            endDate.setMonth(month);        
            if (month == 0) month = 12;
            endDate = endDate.getDate()+'/'+month+'/'+endDate.getFullYear();    
        }
    	 
		
    }

	function initSearchPage(){
		init();
        if(formReference.lmnLengthOfStay){
            selectMonth(formReference.lmnCheckInDay,formReference.lmnCheckInMonth);
            startUpCalendar(theDate,endDate,formReferenceIndex,'lmnCheckInDay','lmnCheckInMonth','lmnCheckOutDay','lmnCheckOutDay',false);  
            //  if (!formReference.lmnLengthOfStay.value){formReference.lmnLengthOfStay.value='1';}
            updateCheckOut(formReference.lmnLengthOfStay.options[formReference.lmnLengthOfStay.selectedIndex].value);
            showRoomOptions();
        }		
	}

	function initSearchLuxuryUniqueBoutiquePage(){
		init();
		selectMonth(formReference.lmnCheckInDay,formReference.lmnCheckInMonth);
		startUpCalendar(theDate,endDate,formReferenceIndex,'lmnCheckInDay','lmnCheckInMonth','lmnCheckOutDay','lmnCheckOutDay',false);  
		//	if (!formReference.lmnLengthOfStay.value){formReference.lmnLengthOfStay.value='1';}
		updateCheckOut(formReference.lmnLengthOfStay.options[formReference.lmnLengthOfStay.selectedIndex].value);
	}
	
	function initCategorySearchPage(){
	
	//initialises everything *after* the page has finished loading    	
    
		var currentFormIndex;		
		formReferenceIndex = 0;	
		for (currentFormIndex = 0; currentFormIndex < document.forms.length; currentFormIndex++){
			if  (document.forms[currentFormIndex].lmnLengthOfStay){				
				formReferenceIndex = currentFormIndex;				
 			}
		}
		formReference = document.forms[formReferenceIndex];	
    	showCategoryRoomOptions();	
  		
		updateCategoryCheckOutOnInit(formReference.lmnLengthOfStay.options[formReference.lmnLengthOfStay.selectedIndex].value, formReference);
	}
	
	
	function initResultsPage(){
		init();
		startUpCalendar(theDate,endDate,formReferenceIndex,'lmnCheckInDay','lmnCheckInMonth','lmnCheckOutDay','lmnCheckOutDay',false);  
		//	if (!formReference.lmnLengthOfStay.value){formReference.lmnLengthOfStay.value='1';}
		updateCheckOut(formReference.lmnLengthOfStay.options[formReference.lmnLengthOfStay.selectedIndex].value);
		showRoomOptions();
	}

function updateCategoryCheckOutOnInit(value, formReference) {
    var months_array = new Array(formReference.january.value,
                                 formReference.february.value,
                                 formReference.march.value,
                                 formReference.april.value,
                                 formReference.may.value,
                                 formReference.june.value,
                                 formReference.july.value,
                                 formReference.august.value,
                                 formReference.september.value,
                                 formReference.october.value,
                                 formReference.november.value,
                                 formReference.december.value);
    updateCategoryCheckOut(value, months_array, formReference);
}

function updateCategoryCheckOut(value, monthsArray, formReference) {        

	//constructs checkout dates based on checkin dates and number of nights selected        
	
	// save number of nights
	no_nights = parseInt(value);
	
	// 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();
	
	dateString = formReference.checkIn.value;
	var dateArray = dateString.split("-")
	
	m2 = dateArray[1]-1;
	
	
	if(m2<m1) { y = y+1; }
	
	// now create the proper check in date
	//real_checkin_s = dateArray[0]+" "+months_array[parseInt(m2)]+" "+y;
	
	real_checkin_s = y+"/"+(parseInt(m2)+1)+"/"+dateArray[0];
	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 = monthsArray[real_checkout_d.getMonth()];
	// show user
	formReference.lmnCheckOutDisplay.value=check_out_day+" "+check_out_month;
}


		
//dynamic room select code
function showRoomOptions(){
	dA=(document.all)?document.all:(document.layers)?0:document.getElementsByTagName('*');
	if(dA){
		var ddRooms=dA.lmnRooms;// No. rooms dropdown name
		var ddAges=new Array(dA.lmnChildrenRoom1,dA.lmnChildrenRoom2,dA.lmnChildrenRoom3);// No.children dropdown names
		var numRooms=ddRooms.options[ddRooms.selectedIndex].value;
		for(i=0;i<3;i++){
			dA["room"+i].style.display=(numRooms>i)?"block":"none";
			for(j=0;j<4;j++){
				var numChildren=ddAges[i].options[ddAges[i].selectedIndex].value;
				dA["lmnRoom"+(i+1)+"ChildAge"+(j+1)].style.visibility=(numChildren>j)?"visible":"hidden";
				dA["lblChildrensAges"+i].style.visibility=(numChildren>0)?"visible":"hidden";
			}
		}
	}
}

 function showCategoryRoomOptions(){
        dA=(document.all)?document.all:(document.layers)?0:document.getElementsByTagName('*');
        if(dA){
            var ddRooms=dA.lmnRooms;// No. rooms dropdown name             
            var numRooms=ddRooms.options[ddRooms.selectedIndex].value;
            for(i=0;i<3;i++){
                dA["room"+i].style.display=(numRooms>i)?"block":"none";
                numChildrenFormElement = dA["roomOccupancies["+i+"].childOccupancy"];
                numChildren=numChildrenFormElement.options[numChildrenFormElement.selectedIndex].value;
                for(j=0;j<4;j++){
                	childAgeFormElement = dA["childAge"+i+"-"+j];                
                    childAgeFormElement.style.visibility=(numChildren>j)?"visible":"hidden";
                    dA["lblChildrensAges"+i].style.visibility=(numChildren>0)?"visible":"hidden";
                }
            }
        }
    }
	
//star rating selection functions		
function setStarRating(starValue){
	if (starValue=='lmnAnyStar'){resetStars();formReference.lmnAnyStar.value="on";}
	else if (starValue=='lmnStar1'){resetStars();formReference.lmnStar1.value="on";}
	else if (starValue=='lmnStar2'){resetStars();formReference.lmnStar2.value="on";}
	else if (starValue=='lmnStar3'){resetStars();formReference.lmnStar3.value="on";}
	else if (starValue=='lmnStar4'){resetStars();formReference.lmnStar4.value="on";}
	else if (starValue=='lmnStar5'){resetStars();formReference.lmnStar5.value="on";}
}
		
function resetStars(){
	formReference.lmnAnyStar.value="off";
	formReference.lmnStar1.value="off";
	formReference.lmnStar2.value="off";
	formReference.lmnStar3.value="off";
	formReference.lmnStar4.value="off";
	formReference.lmnStar5.value="off";
}

//set lmnLocation in the search form 
function setSearchLocation(location) {
	formReference.lmnLocation.value = location
}

// set page in the paging form	
function setPage(page) {
	var formIndex;
	for (formIndex = 0; formIndex < document.forms.length; formIndex++){
		var formAction = document.forms[formIndex].action;
		var isPaging = formAction.match(/paging/g);		
		if ( isPaging ){
 			document.forms[formIndex].page.value = page;	
 		}
	}
}
// set page in the paging form for customer reviews	
function setCRPage(page) {
	document.customerReviewsPagination.selectedPage.value = page;	
}

/* Help layers */
function showHelp(){HF(1);}
function hideHelp(){HF(0);}
function HF(b){
	if(!document.all)return;
	var HL=document.all.helpLayer;
		if(IE){HL.style.filter="blendTrans(duration=0.2)";
		HL.filters.blendTrans.apply();}
	HL.style.visibility=(b)?'visible':'hidden';
		if(IE)HL.filters.blendTrans.play();
}

/* Info PopUp */
function popup(page){window.open(page);}

function openPopupWindow(url,name){
newwin=window.open(url,name,'width=510,height=410,scrollbars=yes,resizable=no,');   
newwin.focus();
}

function RenderPopup(windowsource, windowname, width,
	height, scrollbars, directories, location,
	menubar, status, toolbar, resizable) {
	var windowfeatures = 'width=' + width +
				',height=' + height + ',directories=' + directories +
				',location=' + location + ',menubar=' + menubar +
				',scrollbars=' + scrollbars + ',status=' + status +
				',toolbar=' + toolbar + ',resizable=' + resizable;

	window.open(windowsource, windowname, windowfeatures);
}

/*function to display the returning/new customer html*/

function isReturningCustomer(status){
	signup = document.getElementById("signupToNewsletterLayer");
    signupCheckbox = document.getElementById("signupToNewsletterCheckbox");
    password = document.getElementById("passwordConfirmationLayer");        
    if (status == "false"){
        signup.style.display='inline';
		if(signupCheckbox !=null)
			 signupCheckbox.style.display='inline';
        password.style.display='inline';
    } else{
        signup.style.display='none';
		if(signupCheckbox !=null)
			 signupCheckbox.style.display='none';
        password.style.display='none';
   }  
}

/*function to display more room types*/

function showMoreRoomTypes(hotelIndex, roomIndex, hiddenRoomTypeStartIndex, totalRoomTypes) {
  var showMoreButtonElement = document.getElementById("showMoreRooms"+hotelIndex+roomIndex);
  var showLessButtonElement = document.getElementById("showLessRooms"+hotelIndex+roomIndex);
  showMoreButtonElement.style.display = "none";
  showLessButtonElement.style.display = "";
  for(var i=hiddenRoomTypeStartIndex;i<totalRoomTypes;i++){
	var roomTypeElement = document.getElementById("moreRooms"+hotelIndex+roomIndex+i);	
	roomTypeElement.style.display = "";
	var priceRowElement = document.getElementById("morePriceRow"+hotelIndex+roomIndex+i);	
	if(priceRowElement){
		priceRowElement.style.display = "";
    }
  } 	
}

/*function to hide room types*/
function showLessRoomTypes(hotelIndex, roomIndex, hiddenRoomTypeStartIndex, totalRoomTypes) {
  var showMoreButtonElement = document.getElementById("showMoreRooms"+hotelIndex+roomIndex);
  var showLessButtonElement = document.getElementById("showLessRooms"+hotelIndex+roomIndex);
  showMoreButtonElement.style.display = "";
  showLessButtonElement.style.display = "none";
  for(var i=hiddenRoomTypeStartIndex;i<totalRoomTypes;i++){
	var roomTypeElement = document.getElementById("moreRooms"+hotelIndex+roomIndex+i);	
	roomTypeElement.style.display = "none";
	var priceRowElement = document.getElementById("morePriceRow"+hotelIndex+roomIndex+i);	
	if(priceRowElement){
		priceRowElement.style.display = "none";
    }
  } 	
}

/* submits the form  */
function submit(form){
    document.getElementById(form).submit();
}
