/*
			Vacansee(tm) calendar script
			copyright (c) Ozstays.com
			coded by Chris Sharkey

		*/
    
    var nowDate  = new Date();
    var nowDay   = nowDate.getDate();
    var nowMonth = nowDate.getMonth()+1;
    var nowYear  = nowDate.getFullYear();
    //alert('Now:'+nowYear+'-'+nowMonth+'-'+nowDay);
    
		var click=0;
		var startDay=0;
		var startMonth=0;
		var startYear=0;
		var endDay=0;
		var msg = "Click the Calendar";
		var b;
		var d;
		var tmp;
		var error=0;
		var enabled=0;
		var blength=0;

		var state;


		function stzLeapYear(year) {
		  if (year % 4 == 0) {// basic rule
		    return true; // is leap year
		  }
		  return false; // is not leap year
		}

    function stzIsPast(year, month, day) {
    
      if (year < nowYear)
      {
        return 1;
      }
      else if (year == nowYear)
      {
        if (month < nowMonth)
        {
          return 1;
        }
        else if (month == nowMonth)
        {
          if (day < nowDay)
          {
            return 1;
          }
        }
      }
      
      return 0;
    }

    function stzNextDate(year, month, day)
    {
      var nextDay = day+1;
      var nextMonth = month;
      var nextYear = year;
      if (nextDay > stzGetDays(nextMonth, nextYear))
      {
	nextDay = 1;
        nextMonth++;
        
        if (nextMonth > 12)
        {
          nextMonth = 1;
          nextYear++;
        }
      }
      
      var nextDate = new Array(3);
      nextDate['d'] = nextDay;
      nextDate['m'] = nextMonth;
      nextDate['y'] = nextYear;
      
      return nextDate;
    }
    
		function stzGetDays(month, year) {
			month--;
		  // create array to hold number of days in each month
		  var ar = new Array(12);
		  ar[0] = 31; // January
		  ar[1] = (stzLeapYear(year)) ? 29 : 28; // February
		  ar[2] = 31; // March
		  ar[3] = 30; // April
		  ar[4] = 31; // May
		  ar[5] = 30; // June
		  ar[6] = 31; // July
		  ar[7] = 31; // August
		  ar[8] = 30; // September
		  ar[9] = 31; // October
		  ar[10] = 30; // November
		  ar[11] = 31; // December
		
		  // return number of days in the specified month (parameter)
		  return ar[month];
		}

		function mycount(startDay,startMonth,startYear,endDay,month,year) {
			var try2=0;
			var loops=0;
			var d1=startDay;
			var m1=startMonth;
			var y1=startYear;
			var unrel=0;
			var try2=0;

			while (((d1!=endDay)||(m1!=month)||(y1!=year))&&(unrel<1000)) {

				if (try2==0) {
					try2=1;
				}
				else {
					d1++;
				}
				if (d1>stzGetDays(m1,y1)) {
					m1++;
					d1=1;
					if (m1>12) {
						y1++;
						m1=1;
					}
				}
				unrel++;
				loops++;
			}
			return loops;
		}

		function dateOut(day,month,year) {
		
		  month--;
		
		  var ar = new Array(12);
		  ar[0] = "January"
		  ar[1] = "February"
		  ar[2] = "March"
		  ar[3] = "April"
		  ar[4] = "May"
		  ar[5] = "June"
		  ar[6] = "July"
		  ar[7] = "August"
		  ar[8] = "September"
		  ar[9] = "October"
		  ar[10] = "November"
		  ar[11] = "December"
		
		  return ar[month] + " " + day + ", " + year
		}

		function stzD(day,month,year,dates) {
		// initialise max and min nights to the boundaries
		var min_nights = 1;
                var max_nights = 59;
		if (stzIsPast(year, month, day))
	      	{
		        alert('Please select a date in the future.');
	     	}
    		else
      		{
        		if (click==0) {
				  startDay=day;
				  startMonth=month;
				  startYear=year;
        			  dates.sd.value=dateOut(day,month,year);

				  click=1; //make it so next time it is the end day

				  //if its netscape give them an extra alert
  //				if (document.layers||window.sidebar) {
					  alert("Now click the departure date");
  //				}
				  //make the start day look like its 'locked in'
					  dates.sd.style.background="lightyellow";

				  //remove the red 'prompt' from the start date
					  dates.sd.style.borderColor=dates.ed.style.borderColor;

				  //put the red 'prompt' on the end date
					  dates.ed.style.borderColor="red";

				  //Let them know they need to enter the departure day
				  dates.ed.value="Click the departure day";

			  }
			  else {
				  endDay=day;

				  var bcount=mycount(startDay,startMonth,startYear,endDay,month,year);
				  if ((startDay==endDay)&&(startMonth==month)&&(startYear==year)) {
					  click=0;
					  startDay=0;
					  endDay=0;
					  dates.sd.value=msg;
					  dates.ed.value='';

					  //remove the red 'prompt' from the start date
					  dates.ed.style.borderColor=dates.sd.style.borderColor;

					  //put the red 'prompt' on the start date
					  dates.sd.style.borderColor="red";

					  //restore background colour
					  dates.sd.style.background="#ffffff";

					  alert("You cannot leave and arrive on the same day. Please select the dates for your booking again");
				  }


				  //check if end day is after start day
				  else if ((year<startYear)||((month<startMonth)&&(year==startYear))||((endDay<startDay)&&(month==startMonth)&&(year==startYear))) {
					  click=0;
					  startDay=0;
					  endDay=0;
					  dates.sd.value=msg;
					  dates.ed.value='';

					  //remove the red 'prompt' from the start date
					  dates.ed.style.borderColor=dates.sd.style.borderColor;

					  //put the red 'prompt' on the start date
					  dates.sd.style.borderColor="red";

					  //restore background colour
					  dates.sd.style.background="#ffffff";

					  alert("You have chosen a departure day before the arrival day! The status has been reset so you can try again.");
				  }

/******************
				  //check if ths booking is greater than the max length (this comes from php)
				  else if (bcount>MAX_BOOKING_LENGTH) {
					  click=0;
					  startDay=0;
					  endDay=0;
					  dates.sd.value=msg;
					  dates.ed.value='';

					  //remove the red 'prompt' from the start date
					  dates.ed.style.borderColor=dates.sd.style.borderColor;

					  //put the red 'prompt' on the start date
					  dates.sd.style.borderColor="red";

					  //restore background colour
					  dates.sd.style.background="#ffffff";

					  alert("Sorry the booking you have entered is too LONG, this property's maximum booking length is "+MAX_BOOKING_LENGTH+" nights. \nPlease try again.");

				  }
				  else if (bcount<=MIN_BOOKING_LENGTH) {
					  click=0;
					  startDay=0;
					  endDay=0;
					  dates.sd.value=msg;
					  dates.ed.value='';

					  //remove the red 'prompt' from the start date
					  dates.ed.style.borderColor=dates.sd.style.borderColor;

					  //put the red 'prompt' on the start date
					  dates.sd.style.borderColor="red";

					  //restore background colour
					  dates.sd.style.background="#ffffff";

					  alert("Sorry the booking you have entered is too SHORT, this property's minimum booking length is "+MIN_BOOKING_LENGTH+" nights. \nPlease try again.");

				  }
******************************/
				  else {


					  //check to see if dates are valid

					  dates.ed.value=dateOut(day,month,year);
					  //if (document.layers||window.sidebar) {
						//alert('Backdoor');
                                                //proceed_to_booking_page(startDay, startMonth, startYear, day, month, year, property_id);
                                                  //document.location="/calendar-enquiry?arrive_d="+startDay+"&arrive_m="+startMonth+"&arrive_y="+startYear+"&depart_d="+day+"&depart_m="+month+"&depart_y="+year+"&property_id="+property_id+"&dates-clicked=t";
					  //}		

					  loopD = startDay;
					  loopM = startMonth;
					  loopY = startYear;
					  bookingConflict = 0;
					  alertStr = '';
					  previousCellClass = '';
					  for (i = 0; i < bcount-1; i++)
					  {
					    checkCellName = 'b'+loopD+'_'+loopM+'_'+loopY;
					    checkCellClass = document.getElementById(checkCellName).className;
					    if (checkCellClass == 'overlib_link b')
					    {
					      bookingConflict = 1;
					    }
					    previousCellClass = checkCellClass;	// set previous class to current

					    alertStr += checkCellName+'='+checkCellClass+'\n';

					    // get the highest minimum number of nights stay from all the dates
					    nightsCellName = 'nights'+loopD+'_'+loopM+'_'+loopY;
					    nightsCellClass = document.getElementById(nightsCellName).className;	
					    nightsArray = nightsCellClass.split("_");
					    if(nightsArray[1] > min_nights){
						min_nights = nightsArray[1];
						max_nights = nightsArray[3];
					    }
										   
 
					    // Next date
					    nextDay = stzNextDate(loopY, loopM, loopD);
					    loopD = nextDay['d'];
					    loopM = nextDay['m'];
					    loopY = nextDay['y'];
					  }
					  //alert(alertStr);
					  bcount--;
					  if (bookingConflict == 1)
					  {
					    alert('Sorry, the dates you have selected conflict with another booking.');
					    error = 1;
					  } else if (bcount>max_nights) {
						  error = 1;
	                                          click=0;
        	                                  startDay=0;
                	                          endDay=0;
                        	                  dates.sd.value=msg;
	                                          dates.ed.value='';

        	                                  //remove the red 'prompt' from the start date
                	                          dates.ed.style.borderColor=dates.sd.style.borderColor;

                        	                  //put the red 'prompt' on the start date
                                	          dates.sd.style.borderColor="red";

                                        	  //restore background colour
	                                          dates.sd.style.background="#ffffff";

        	                                  alert("Sorry the booking you have entered is too LONG, this property's maximum booking length is "+max_nights+" nights. \nPlease try again.");
	
        	                          } else if (bcount<min_nights) {
					  error = 1;
                                          click=0;
                                          startDay=0;
                                          endDay=0;
                                          dates.sd.value=msg;
                                          dates.ed.value='';

                                          //remove the red 'prompt' from the start date
                                          dates.ed.style.borderColor=dates.sd.style.borderColor;

                                          //put the red 'prompt' on the start date
                                          dates.sd.style.borderColor="red";

                                          //restore background colour
                                          dates.sd.style.background="#ffffff";

                                          alert("Sorry the booking you have entered is too SHORT, this property's minimum booking length for this time is "+min_nights+" nights. \nPlease try again.");
                                  }

	

					  var d1=startDay;
					  var m1=startMonth;
					  var y1=startYear;
					  var unrel=0;
					  var try2=0;
					  var tcount=-1;
					  var bcount=-1;
					  var top=new Array();
					  var bottom=new Array();

					  //make the end day look like its 'locked in'
					  dates.ed.style.background="lightyellow";

					  //take the red 'prompt' away from the end date
					  dates.ed.style.borderColor=dates.sd.style.borderColor;

					  while (((d1!=endDay)||(m1!=month)||(y1!=year))&&(unrel<1000)) {
						  if (try2==0) {
							  try2=1;
						  }
						  else {
							  d1++;
						  }
						  if (d1>stzGetDays(m1,y1)) {
							  m1++;
							  d1=1;
							  if (m1>12) {
								  y1++;
								  m1=1;
							  }
						  }

						  unrel++;

					  }

					  if (!error) {

						  if (confirm(dates.sd.value+" until "+dates.ed.value+" Correct?")) {
                                                          //document.location="/calendar-enquiry?arrive_d="+startDay+"&arrive_m="+startMonth+"&arrive_y="+startYear+"&depart_d="+day+"&depart_m="+month+"&depart_y="+year+"&property_id="+property_id+"&dates-clicked=t";
							 proceed_to_booking_page(startDay, startMonth, startYear, day, month, year, property_id);
						  }
						  else {
							  error=1;
						  }
					  }

					  if (error==1) {
						  error=0;
						  click=0;
						  startDay=0;
						  endDay=0;
						  dates.sd.value=msg;
						  dates.ed.value='';

						  //now we restore the colours for this clump

						  dates.sd.style.background="#ffffff";
						  dates.ed.style.background="#ffffff";

						  //put the red 'prompt' back on the start day
						  dates.sd.style.borderColor="red";

					  }

				  }
			  }
      }
}

function proceed_to_booking_page(arrd, arrm, arry, depd, depm, depy, pid)
{
  url = bookingURL+'?arrive_d='+arrd+'&arrive_m='+arrm+'&arrive_y='+arry+'&depart_d='+depd+'&depart_m='+depm+'&depart_y='+depy+'&property_id='+pid+'&dates-clicked=t';
  //alert(url);
  document.location = url;
}

