//////////////////////////////////////////////////////////////////////////////////////
function popupwindow(which,width,height)
  {
	if(!width){
		width=400;
		height=460;
	}
	if(popup!=null)
		{
			popup.close();
		}
	popup=window.open(which,'popup_window','toolbar=no,location=no,directories=no,status=no,scrollbars,resizable=yes,copyhistory=no,width='+ width+ ',height='+height);
  }

///////////////////////////////////////////////////////////////////////////////////////////
function inday_last_day_of_depature_day()
{
  display_error="11months";	
  document.popupform.number_of_nights.selectedIndex=14;
  document.popupform.rqcheck_out_month.selectedIndex = document.popupform.rqcheck_rqcheck_in_month.selectedIndex;
  document.popupform.rqcheck_out_day.selectedIndex = 30;
  outmonth_lastday();
  display_day();
}
///////////////////////////////////////////////////////////////////////////////////////////

function changedepartureday_movenights_old_notused()
{
	var rqcheck_in_day_index=document.popupform.rqcheck_rqcheck_in_day.selectedIndex;
	var rqcheck_out_day_index =document.popupform.rqcheck_out_day.selectedIndex;
	var rqcheck_in_month_value = document.popupform.rqcheck_in_month.options[document.popupform.rqcheck_in_month.options.selectedIndex].value;
	var rqcheck_out_month_value = document.popupform.rqcheck_out_month.options[document.popupform.rqcheck_out_month.options.selectedIndex].value;

	var in_date_object=date_string_from_rqcheck_in_year_month_date(rqcheck_in_month_value,(rqcheck_in_day_index+1));
	var out_date_object=date_string_from_rqcheck_out_year_month_date(rqcheck_out_month_value,(rqcheck_out_day_index+1));
	
	
	var no_nights=0;
	display_error="";
	if(document.popupform.rqcheck_out_month.selectedIndex==document.popupform.rqcheck_in_month.selectedIndex){
		no_nights=rqcheck_out_day_index - rqcheck_in_day_index
		if(out_date_object > in_date_object)
		document.popupform.number_of_nights.selectedIndex=rqcheck_out_day_index - rqcheck_in_day_index - 1;
	}else{
		document.popupform.number_of_nights.selectedIndex=(ret_lastday()-1)-rqcheck_in_day_index+rqcheck_out_day_index;
		no_nights=ret_lastday() - rqcheck_in_day_index + rqcheck_out_day_index;
	}
	if((no_nights>14) || (out_date_object < in_date_object) ||  (no_nights<=0) || (document.popupform.rqcheck_out_month.selectedIndex > document.popupform.rqcheck_in_month.selectedIndex+1))
	{
		document.popupform.number_of_nights.selectedIndex=14;
		if(out_date_object < in_date_object)
			display_error="trying_to_leave_before_arriving";
		else
		{
			if(dates_are_equal(out_date_object,in_date_object))
			display_error="less_than_min_stay";
			else
			display_error="more_than_14_day_stay";
		}	
	
	}

}

////////////////////////////////////////////////////////////////////////////////////////////
function changedepartureday_movenights()
{
	var rqcheck_in_day_value=document.popupform.rqcheck_in_day.selectedIndex+1;
	var rqcheck_out_day_value =document.popupform.rqcheck_out_day.selectedIndex+1;
	var rqcheck_in_month_value = document.popupform.rqcheck_in_month.options[document.popupform.rqcheck_in_month.options.selectedIndex].value;
	var rqcheck_out_month_value = document.popupform.rqcheck_out_month.options[document.popupform.rqcheck_out_month.options.selectedIndex].value;

	var in_date_object=date_string_from_rqcheck_in_year_month_date(rqcheck_in_month_value,rqcheck_in_day_value);
	var out_date_object=date_string_from_rqcheck_out_year_month_date(rqcheck_out_month_value,rqcheck_out_day_value);
	
	
	var night_count=days_elapsed(in_date_object,out_date_object);
	display_error="";
	if((night_count > 0) && (night_count <= 14))
	{
		document.popupform.number_of_nights.selectedIndex=night_count - 1;
	}
	else
	{
		document.popupform.number_of_nights.selectedIndex=14;
		if(out_date_object < in_date_object)
			display_error="trying_to_leave_before_arriving";
		else
		{
			if(dates_are_equal(out_date_object,in_date_object))
			display_error="less_than_min_stay";
			else
			display_error="more_than_14_day_stay";
		}	
	
	}

}

///////////////////////////////////////////////////////////////////////////////////
function display_day(flag)
{
	
	var day_gif_prefix = "days_3_";
	var flag=flag;
	
	if((flag) && ((document.popupform.rqcheck_in_month.length == (document.popupform.rqcheck_in_month.selectedIndex+1)) && ((ret_lastday() - (document.popupform.number_of_nights.selectedIndex+1)) < (document.popupform.rqcheck_in_day.selectedIndex+1))))
	{
		inday_last_day_of_depature_day();

		return;
	}else{

		if(document.popupform.number_of_nights.selectedIndex!=14)
		{
			if(minstay > ((document.popupform.number_of_nights.selectedIndex)+1))
			{
				if(!flag)
				{
					document.popupform.number_of_nights.selectedIndex=14;
					display_error="less_than_min_stay";
					
				}
				else 
					moveDeparture();
			}else{
				var inDay = document.popupform.rqcheck_in_day.selectedIndex;
				var inMonth = document.popupform.rqcheck_in_month.selectedIndex;
				var lastDay = ret_lastday();
				display_error="";
				
				if(document.popupform.rqcheck_out_month.selectedIndex <=  document.popupform.rqcheck_in_month.selectedIndex)
					document.popupform.rqcheck_out_month.selectedIndex =document.popupform.rqcheck_in_month.selectedIndex;

				if((document.popupform.rqcheck_in_day.selectedIndex + (document.popupform.number_of_nights.selectedIndex)+1) < lastDay)
					document.popupform.rqcheck_out_day.selectedIndex =document.popupform.rqcheck_in_day.selectedIndex + (document.popupform.number_of_nights.selectedIndex)+1;

				if (inDay >= (lastDay - ((document.popupform.number_of_nights.selectedIndex)+1))) // if the in date is the	last day of the month
		    		{
		    		
					if(document.popupform.rqcheck_out_month.selectedIndex <=  document.popupform.rqcheck_in_month.selectedIndex)
						document.popupform.rqcheck_out_month.selectedIndex = inMonth+1;

					if((lastDay-inDay)< ((document.popupform.number_of_nights.selectedIndex)+1))
						document.popupform.rqcheck_out_day.selectedIndex =((document.popupform.number_of_nights.selectedIndex)+1)+inDay - lastDay;
					else
						document.popupform.rqcheck_out_day.selectedIndex = 0;
		    		}
		    		else
		    		{
		    			//Introduced to make the departure month same as arrival month if the total of inday + nights is lower than the total days of month
		    			if(document.popupform.rqcheck_out_month.selectedIndex >  document.popupform.rqcheck_in_month.selectedIndex)
						document.popupform.rqcheck_out_month.selectedIndex = inMonth;
				}		
			}
		}

		var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var weekday_array=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
		
		var inmonth_value = document.popupform.rqcheck_in_month.options[document.popupform.rqcheck_in_month.options.selectedIndex].value;
		var inday_index= (document.popupform.rqcheck_in_day.selectedIndex)+1;
		var month_day_rqcheck_in_year_in=montharray[(eval(inmonth_value.substring(4,6)))-1]+" "+inday_index+", "+inmonth_value.substring(0,4);
		var mydate_in=new Date(month_day_rqcheck_in_year_in);
		document.popupform.rqcheck_in_day_of_week.value=' ' + weekday_array[mydate_in.getDay()];

		rqcheck_in_month = mydate_in.getMonth() + 1;
		in_date =  mydate_in.getDate();
		month_pad = (rqcheck_in_month<10)?"0":"";
		date_pad = (in_date<10)?"0":"";
		
		
		
		highlight_start = mydate_in.getFullYear().toString() + month_pad + rqcheck_in_month.toString() + date_pad + in_date.toString();

		var outmonth_value = document.popupform.rqcheck_out_month.options[document.popupform.rqcheck_out_month.selectedIndex].value;
		var outday_index= (document.popupform.rqcheck_out_day.selectedIndex)+1;
		var month_day_rqcheck_out_year_out=montharray[(eval(outmonth_value.substring(4,6)))-1]+" "+outday_index+", "+outmonth_value.substring(0,4);
		var mydate_out=new Date(month_day_rqcheck_out_year_out);
		document.popupform.rqcheck_out_day_of_week.value=' ' + weekday_array[mydate_out.getDay()];
		
		rqcheck_out_month = mydate_out.getMonth() + 1;
		out_date =  mydate_out.getDate();
		month_pad = (rqcheck_out_month<10)?"0":"";
		date_pad = (out_date<10)?"0":"";
	
		highlight_end = mydate_out.getFullYear().toString() + month_pad + rqcheck_out_month.toString() + date_pad + out_date.toString();
		
		highlight_calendar(highlight_start, highlight_end);
	}	
}

//////////////////////////////////////////////////////////////////////////////////////////
var calendar_iframe_is_loaded=false;
var calendar_iframe=null;
//////////////////////////////////////////////////////////////////////////////////////////

function highlight_calendar(highlight_start, highlight_end)
{
	defer=false;
	//return;
	
	if(!calendar_iframe_is_loaded)
	{
		do_later="highlight_calendar(" + highlight_start + "," + highlight_end + ")";
		setTimeout(do_later, 1000);
	}
	else 
	{
		//alert(highlight_start + ' and ' + highlight_end );
		calendar_iframe.highlight_date_range(highlight_start, highlight_end);
	}
}


//////////////////////////////////////////////////////////////////////////////////////////
function isleapyear (year)
{
	if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0))
	{return (29);} else{ return (28); }
}

///////////////////////////////////////////////////////////////////////////////////////////

function ret_lastday()
{
	var inDay = document.popupform.rqcheck_in_day.selectedIndex;
	var inMonth = document.popupform.rqcheck_in_month.options[document.popupform.rqcheck_in_month.selectedIndex].value;
	var inMonthTranslated =    eval(inMonth.substring(4,6));
        var year=eval(inMonth.substring(0,4));
	var lastDay = 31;

	switch (inMonthTranslated)
	{
		case 9:
		case 4:
		case 6:
		case 11:
			lastDay = 30
			break;
		case 2:
			lastDay =isleapyear(year);
			break;
	}
	if((lastDay >0) && (lastDay <= document.popupform.rqcheck_in_day.selectedIndex))
		document.popupform.rqcheck_in_day.selectedIndex = (lastDay - 1);

	return lastDay;

}
//////////////////////////////////////////////////////////////////////////////////

function pop(n,page)
{
	calendar = open(page , 'calendar', 'toolbar=no,location=no,directories=no,status=yes,scrollbars=auto,menubar=no,resizable=no,width=290,height=230')
}
//////////////////////////////////////////////////////////////////////////////////
function moveDeparture()
{
	var lastDay=ret_lastday()
	display_error="";
	var night_count =document.popupform.number_of_nights.selectedIndex+1;
	if((night_count <= minstay) || (document.popupform.number_of_nights.selectedIndex==14) )
	{
		//if(night_count <= minstay)
		document.popupform.number_of_nights.selectedIndex=minstay-1;
	}
	var inDay = document.popupform.rqcheck_in_day.selectedIndex;
	var inMonth = document.popupform.rqcheck_in_month.selectedIndex;
	if((document.popupform.rqcheck_in_month.length == (document.popupform.rqcheck_in_month.selectedIndex+1)) && ((lastDay - minstay) < (document.popupform.rqcheck_in_day.selectedIndex+1)))
	{
		  inday_last_day_of_depature_day();
		  return;
	}
	display_error="";
	document.popupform.rqcheck_out_month.selectedIndex = document.popupform.rqcheck_in_month.selectedIndex - offsetMonth;

	if((document.popupform.rqcheck_in_day.selectedIndex + minstay) < lastDay)
		document.popupform.rqcheck_out_day.selectedIndex = document.popupform.rqcheck_in_day.selectedIndex + minstay;

	if (inDay >= (lastDay - minstay)) // if the in date is the last day of the month
	{
		document.popupform.rqcheck_out_month.selectedIndex = inMonth+ initMonth;
		if((lastDay - inDay ) < minstay)
			document.popupform.rqcheck_out_day.selectedIndex = minstay + inDay-lastDay;
		else
			document.popupform.rqcheck_out_day.selectedIndex = 0;
		return;
	}

	if(inDay >= document.popupform.rqcheck_out_day.selectedIndex)
	{
		document.popupform.rqcheck_out_day.selectedIndex = (inDay + minstay);
		return;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
function outmonth_lastday(){
        var outDay = document.popupform.rqcheck_out_day.selectedIndex +1 ;
        var outMonth = document.popupform.rqcheck_out_month.options[document.popupform.rqcheck_out_month.selectedIndex].value;
        var outMonthTranslated = eval(outMonth.substring(4,6));
        var year=eval(outMonth.substring(0,4));
        var lastDay = 31;

        switch (outMonthTranslated)
        {
			case 2:
				lastDay =isleapyear(year);
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				lastDay = 30
			break;
        }
         if(outDay  > lastDay)
         document.popupform.rqcheck_out_day.selectedIndex = lastDay - 1;
         else return;
}
//////////////////////////////////////////////////////////////////////////////////
function date_string_from_year_month_date(fn_year_month,fn_date)
{
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var month_day_year_value=montharray[(eval(fn_year_month.substring(4,6)))-1]+" "+fn_date+", "+fn_year_month.substring(0,4);
	return new Date(month_day_year_value);
}

//////////////////////////////////////////////////////////////////////////////////
function fshr_js_validate_date_selection()
{

	var rqcheck_in_month_value = document.popupform.rqcheck_in_month.options[document.popupform.rqcheck_in_month.options.selectedIndex].value;
	var rqcheck_in_day_value= (document.popupform.rqcheck_in_day.selectedIndex)+1;
	var rqcheck_out_month_value = document.popupform.rqcheck_out_month.options[document.popupform.rqcheck_out_month.options.selectedIndex].value;
	var rqcheck_out_day_value= (document.popupform.rqcheck_out_day.selectedIndex)+1;
	var todays_date = today_date_object;
	var arrival_date = date_string_from_rqcheck_in_year_month_date(rqcheck_in_month_value,rqcheck_in_day_value);
	var departure_date = date_string_from_rqcheck_out_year_month_date(rqcheck_out_month_value,rqcheck_out_day_value);

	
	if(todays_date > arrival_date)
		return "trying_to_arrive_in_the_past";

	if(arrival_date > departure_date )
		return "trying_to_leave_before_arriving";

	
	if(dates_are_equal(arrival_date,todays_date))
		return "trying_to_arrive_today";


	if(dates_are_equal(arrival_date,departure_date))
		return "less_than_min_stay";
}


//////////////////////////////////////////////////////////////////////////////////
function dates_are_equal(date_object_1,date_object_2)
{
	if((date_object_1.getYear() == date_object_2.getYear()) && (date_object_1.getDate() == date_object_2.getDate()) && (date_object_1.getMonth() == date_object_2.getMonth()))
		return true;
}
///////////////////////////////////////////////////////////////////////
function days_elapsed(start_date,end_date)
{
	var difference = Date.UTC(end_date.getYear(),end_date.getMonth(),end_date.getDate(),0,0,0) - Date.UTC(start_date.getYear(),start_date.getMonth(),start_date.getDate(),0,0,0);
	return difference/1000/60/60/24;
}

//////////////////////////////////////////////////////////////////////////
function retain_dates_and_link(link_url,no_dates){
if(no_dates)
	var redirect_url=link_url;
else
	{
	 if(link_url.indexOf("?")>0)
		var start_with="&"
	 else
		var start_with="?"


	var rqcheck_in_day_value=document.popupform.rqcheck_in_day.selectedIndex+1;
	var rqcheck_out_day_value =document.popupform.rqcheck_out_day.selectedIndex+1;
	var rqcheck_in_month_value = document.popupform.rqcheck_in_month.options[document.popupform.rqcheck_in_month.options.selectedIndex].value;
	var rqcheck_out_month_value = document.popupform.rqcheck_out_month.options[document.popupform.rqcheck_out_month.options.selectedIndex].value;
	var night_count =	document.popupform.number_of_nights.selectedIndex+1;
	var occupancy_count = document.popupform.OCCUPANCY.selectedIndex+1;

	var redirect_url= link_url + start_with + "&rqcheck_out_day=" + rqcheck_out_day_value + "&rqcheck_out_month=" + rqcheck_out_month_value + "&rqcheck_in_month=" + rqcheck_in_month_value + "&rqcheck_in_day=" + rqcheck_in_day_value + "&number_of_nights=" + night_count + "&OCCUPANCY=" + occupancy_count + "&force_v2=true" ;

	}

document.location= redirect_url;
}
//////////////////////////////////////////////////////////////////////////
