
var bSD=0;				// 0= date selected is FROM ; 1=date selected is TO
var oldMonthID=0;		// for row calendar line selection effect

// view mode variables
var tmDate1=0;
var tmDate2=0;
var Obj1=0;
var Obj2=0;
var BkgCol1=0;
var BkgCol2=0;

include("/class/js/formvalidation.js");
include("/class/js/tools.js");

// --------------------------------------------------------------------------------------
//	live date selection in calendar for editing
// --------------------------------------------------------------------------------------

function onSD_edit(obj,day,month,year)
{
  if(bSD==0)
  {
		y=document.getElementById("f_d_RatesDateFromY");
		m=document.getElementById("f_d_RatesDateFromM");
		d=document.getElementById("f_d_RatesDateFromD");
  }
  else
  {
		y=document.getElementById("f_d_RatesDateToY");
		m=document.getElementById("f_d_RatesDateToM");
		d=document.getElementById("f_d_RatesDateToD");
  }
	y.value=year;
	m.value=month;
	EnsureDates(bSD,"f_d_RatesDateFromD","f_d_RatesDateFromM","f_d_RatesDateFromY","f_d_RatesDateToD","f_d_RatesDateToM","f_d_RatesDateToY");
	d.value=day;
	bSD++;
	if(bSD==2)
	  bSD=0;
}

// --------------------------------------------------------------------------------------
//  live date selection in calendar for booking
// --------------------------------------------------------------------------------------

function onSD_view(objName,tmDate,idCal)
{
	if(Obj1==0)
	{
		tmDate1=tmDate;
		Obj1=document.getElementById(objName);
		BkgCol1=Obj1.style.backgroundColor;
		Obj1.style.backgroundColor="pink";
		if(Obj2!=0)
		{
			Obj2=0;
		}
	}
	else
	{
		tmDate2=tmDate;
		Obj2=document.getElementById(objName);
		if(tmDate2<tmDate1)
		{
			if(_checkDatesAreAvailable(idCal,tmDate2,tmDate1))
			{
				_fillDates(idCal,tmDate2,tmDate1,"pink");
				OpenBookingRequest(tmDate2,tmDate1,idCal);
			}
			else
			{
				Obj1.style.backgroundColor=BkgCol1;
				alert("Sorry, cannot process this request. Please ensure you select a date range which is made only of available dates");
			}
		}
		else
		{
			if(_checkDatesAreAvailable(idCal,tmDate1,tmDate2))
			{
				_fillDates(idCal,tmDate1,tmDate2,"pink");
				OpenBookingRequest(tmDate1,tmDate2,idCal);
			}
			else
			{
				Obj1.style.backgroundColor=BkgCol1;
				alert("Sorry, cannot process this request. Please ensure you select a date range which is made only of available dates");
			}
		}
		Obj1=0;
	}
	
}

// check that all dates between from and to are available (id of object start with "da" )
function _checkDatesAreAvailable(propid,tsFrom,tsTo)
{
	DateFrom=_strDate(tsFrom);
	DateTo=_strDate(tsTo);
	
	objFrom=document.getElementById(propid+"_da"+DateFrom);
	if(objFrom==null)
		return false;	// must start from available date !!
	
	objTo=document.getElementById(propid+"_da"+DateTo);	// preferrably goes to an available date, but changeover days are also accepted so we can accept booked/softbooked target day
	if(objTo==null)
		objTo=document.getElementById(propid+"_db"+DateTo);
	if(objTo==null)
		objTo=document.getElementById(propid+"_ds"+DateTo);
	if(objTo==null)
		return false;
	tmp=objFrom;
	ts=tsFrom;
	while((tmp!=objTo) && (tmp!=null))
	{
		ts=ts+86400;
		DateTmp=_strDate(ts);
		if(DateTmp==DateTo)
			return true;	// it s just OK if the last day is not fully available as it s the night before (into that day) that counts
		tmp=document.getElementById(propid+"_da"+DateTmp);
		if(tmp==null)
			return false;
	}
	return true;
}

// fill dates between from and to with color
function _fillDates(propid,tsFrom,tsTo,color)
{
	DateFrom=_strDate(tsFrom);
	DateTo=_strDate(tsTo);
	
	objFrom=document.getElementById(propid+"_da"+DateFrom);
	if(objFrom==null)
		return false;
	objTo=document.getElementById(propid+"_da"+DateTo);
	if(objTo==null)
		objTo=document.getElementById(propid+"_db"+DateTo);
	if(objTo==null)
		objTo=document.getElementById(propid+"_ds"+DateTo);
	if(objTo==null)
		return false;
	tmp=objFrom;
	ts=tsFrom;
	while((tmp!=objTo) && (tmp!=null))
	{
		ts=ts+86400;
		DateTmp=_strDate(ts);
		tmp.style.backgroundColor=color;
		tmp=document.getElementById(propid+"_da"+DateTmp);
		if(tmp==null)
			return true;	// accept that last day could be not available (but night is), and do not paint last day pink in this case...
	}
	tmp.style.backgroundColor=color;
	return true;
}

// --------------------------------------------------------------------------------------
// Booking form management
// --------------------------------------------------------------------------------------
function CancelBookingRequest()
{
	obj=document.getElementById("calBookingForm");
	obj.style.visibility="hidden";
	From=document.getElementById("htsFrom");
	To=document.getElementById("htsTo");
	if(_checkDatesAreAvailable(From.value,To.value))
	{
		_fillDates(From.value,To.value,"#80FF80");
	}
	return true;
}

function OpenBookingRequest(tsFrom,tsTo,id)
{
	obj=document.getElementById("calBookingForm");
	obj.style.visibility="visible";
	
	From=document.getElementById("editFrom");
	To=document.getElementById("editTo");
	
	// store prop id
	prop=document.getElementById("propid");
	prop.value=id;
	
	// fill in values in edits of the booking form
	var myDate;
	var txtDate;
	
	myDate= new Date();
	myDate.setTime(tsFrom*1000);
	txtDate= pad(myDate.getDate(),2) + "/" + pad((1+myDate.getMonth()),2) + "/" + myDate.getFullYear();
	txtDate= myDate.getFullYear() + "-" + pad((1+myDate.getMonth()),2) + "-" + pad(myDate.getDate(),2);
	From.value=txtDate;
	
	myDate.setTime(tsTo*1000);
	txtDate= pad(myDate.getDate(),2) + "/" + pad((1+myDate.getMonth()),2) + "/" + myDate.getFullYear();
	txtDate= myDate.getFullYear() + "-" + pad((1+myDate.getMonth()),2) + "-" + pad(myDate.getDate(),2);
	To.value=txtDate;
	
	var nbDays=(tsTo-tsFrom)/86400;
	nbD=document.getElementById("editNbDays");
	nbD.value=nbDays;
	
	// keep dates from and to in memory
	From=document.getElementById("htsFrom");
	To=document.getElementById("htsTo");
	From.value=tsFrom;
	To.value=tsTo;
	return true;
}

function SendBookingRequest()
{
	dateFromEdit=document.getElementById("editFrom");
	dateToEdit=document.getElementById("editTo");
	nbDays=document.getElementById("editNbDays");
	nameGuest=document.getElementById("editName");
	nbGuests=document.getElementById("cbGroup");
	email=document.getElementById("editEmail");
	phone=document.getElementById("editPhone");
	lng=document.getElementById("cbGstLng");
	comment=document.getElementById("editComment");
	prop=document.getElementById("propid");
	
	// reformat dates from and to YYYY-MM-DD
	dateFrom=dateFromEdit.value;
	dateTo=dateToEdit.value;
//	dateFrom=dateFromTmp.substring(6,10)+"-"+dateFromTmp.substring(3,5)+"-"+dateFromTmp.substring(0,2);
//	dateTo=dateToTmp.substring(6,10)+"-"+dateToTmp.substring(3,5)+"-"+dateToTmp.substring(0,2);
	
	if (ValidateMinChar(nameGuest,3))
		if(ValidateEmail(email))
			if (ValidateMinChar(phone,6))
				if (ValidateMinChar(comment,3))
	{
	// OK, form is validated, now send email
		obj=document.getElementById("calBookingForm");
		obj.style.visibility="hidden";
		
	// ajax call here
	    var xhr; 
	    try {  xhr = new ActiveXObject("Msxml2.XMLHTTP");   }
	    catch (e) 
	    {
	        try {   xhr = new ActiveXObject("Microsoft.XMLHTTP");    }
	        catch (e2) 
	        {
	          try {  xhr = new XMLHttpRequest();     }
	          catch (e3) {  xhr = false;   }
	        }
	    }
		
		xhr.open("POST", "ajax-post-sendenquiry.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
		xhr.send("propid="+prop.value+"&name="+nameGuest.value+"&email="+email.value+"&datefrom="+dateFrom+"&dateto="+dateTo+"&lng="+lng.value+"&phone="+phone.value+"&nbdays="+nbDays.value+"&nbguestsA="+nbGuests.value+"&comment="+comment.value);
		ShowAcknowledgeBooking();
		return true;
	}
	return false;
}

function ShowAcknowledgeBooking()
{
	Form=document.getElementById("AcknowledgeBooking");
	if(Form!=null)
		Form.style.visibility = "visible"; 
	tTimer=setTimeout("HideAcknowledgeBooking()",4000);
}

function HideAcknowledgeBooking()
{
	Form=document.getElementById("AcknowledgeBooking");
	if(Form!=null)
		Form.style.visibility = "hidden"; 
}


// --------------------------------------------------------------------------------------
// SHOW / HIDE Booking text
// --------------------------------------------------------------------------------------

function onBookingTxt()
{
	obj=document.getElementById("calBookingTxt");
	if(obj!=null)
	{
		obj.style.visibility="visible";
		setTimeout(HideBookingTxt,5000); // call doMove in 5 sec
	}
}

function HideBookingTxt()
{
	_HideForm("calBookingTxt");
}

// --------------------------------------------------------------------------------------
// calendar Mouse Overs
// --------------------------------------------------------------------------------------

function onMOM(month_id)
{
//  return;
  if(oldMonthID!=0)
  {
	oldMonthID.style.borderBottom="none";
	oldMonthID.style.borderTop="none";
	oldMonthID.style.color="#000000";
  }
  month_id.style.borderBottom="thin dotted #FF0000";
  month_id.style.borderTop="thin dotted #FF0000";
  month_id.style.color="#FF0000";
  oldMonthID=month_id;
}

function onMOM_beta(month_id)
{
  month_id.style.borderBottom="thin dotted #FF0000";
  month_id.style.borderTop="thin dotted #FF0000";
}

function onMOD(rate)
{
  edit=document.getElementById("pindic");
  edit.value=rate;
}

function removeOption(id,olddate)
  {
    id.remove(olddate);
  }
function insertOption(id,newdate)
  {
  id.options[newdate-1]=new Option(newdate, newdate, false, false)
  }

// ensure that dates given are valid calendar dates
function _EnsureDates(d,m,y)
{
  var vM = m.options[m.selectedIndex].value - 0;
  var vY = y.options[y.selectedIndex].value - 0;
  
  valD=d.value;
  d.options.length=28;
  switch(vM)
  {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
     insertOption(d,"29");
     insertOption(d,"30");
     insertOption(d,"31");
	 break;
	 
    case 4:
    case 6:
    case 9:
    case 11:
     insertOption(d,"29");
     insertOption(d,"30");
	 break;
	 
    case 2:
	{
	  switch(vY)
	  {
	    case 2008:
	    case 2012:
	    case 2016:
	    case 2020:
          insertOption(d,"29");
		  break;
	  }
	}
	 break;
  }
  d.value=valD;
}

  
function EnsureDates(bSD,objDateFromD,objDateFromM,objDateFromY,objDateToD,objDateToM,objDateToY)
{
//  if(bSD==0)
  {
	  dF=document.getElementById(objDateFromD);
	  mF=document.getElementById(objDateFromM);
	  yF=document.getElementById(objDateFromY);
	  _EnsureDates(dF,mF,yF);
//	alert("From month "+mF.options[mF.selectedIndex].value);
  }
//  else
  {
	  dT=document.getElementById(objDateToD);
	  mT=document.getElementById(objDateToM);
	  yT=document.getElementById(objDateToY);
	  _EnsureDates(dT,mT,yT);
//	alert("To month "+mT.options[mT.selectedIndex].value);
  }
}

function SelMon(id)
{
  bSD=1;
  if(id==document.getElementById("f_d_RatesDateFromM"))
  {
    to=document.getElementById("f_d_RatesDateToM");
	var valF = id.options[id.selectedIndex].value - 0;
	var valT = to.options[to.selectedIndex].value - 0;
	if (valT < valF)
	{
      to.value=valF;
	}
	bSD=0;
  }
  EnsureDates(bSD,"f_d_RatesDateFromD","f_d_RatesDateFromM","f_d_RatesDateFromY","f_d_RatesDateToD","f_d_RatesDateToM","f_d_RatesDateToY");
}

function SelYear(id)
{
  bSD=1;
  if(id==document.getElementById("f_d_RatesDateFromY"))
  {  
    to=document.getElementById("f_d_RatesDateToY");
    to.value=id.options[id.selectedIndex].value;
    bSD=0;
  }
  EnsureDates(bSD,"f_d_RatesDateFromD","f_d_RatesDateFromM","f_d_RatesDateFromY","f_d_RatesDateToD","f_d_RatesDateToM","f_d_RatesDateToY");
}

function selDay()
{
}

function onChangeSelCalType(obj){
}

