document.write('<table id="fc" style="position: absolute; display: none;" cellpadding="1">');
document.write("<tr class='header'><td class='button' onclick='XCal.prevMonth();'>&lt;</td><td colspan=5 class='yearBox' id='yearBox'></td><td class='button' onclick='XCal.nextMonth();'>&gt;</td></tr>");
document.write("<tr class='weekBar'><th>M</th><th>D</th><th>W</th><th>D</th><th>V</th><th>Z</th><th>Z</th></tr>");
for(var kk = 1; kk <= 6; kk++) {
   document.write('<tr class="dateBar">');
   for(var tt = 1; tt <= 7; tt++) {
      num = 7 * (kk - 1) - (-tt);
      document.write('<td id="xcal_' + num + '"">&nbsp;</td>');
   }
   document.write('</tr>');
}

document.write("</table>");

var XCal  = XCal ? XCal : function() {
   var private = {
      monthNames : new Array('JAN','FEB','MAA','APR','MEI','JUN','JUL','AUG','SEP','OKT','NOV','DEC'),
      normMonthLength : new Array(31,28,31,30,31,30,31,31,30,31,30,31),
      leapMonthLength : new Array(31,29,31,30,31,30,31,31,30,31,30,31),
      jsDate : new Date(),
      nDate : new Date(), 
      opacity : 0,
      fadeId : null,
      calObj : null,
      curObj : null,

      offsetLeft: function(obj) {
         var curleft = 0;
         if (obj.offsetParent) {
            while (obj.offsetParent) {
               curleft += obj.offsetLeft
               obj = obj.offsetParent;
            }
         }
         else if (obj.x) {
            curleft += obj.x;
         }

         return curleft;
      },

      offsetTop: function(obj) {
         var curtop = 0;
      
         if (obj.offsetParent) {
            while (obj.offsetParent) {
               curtop += obj.offsetTop
               obj = obj.offsetParent;
            }
         }
         else if (obj.y) {
            curtop += obj.y;
         }
      
         return curtop;
         }
      };

   var public = {
      init: function() {
         private.calObj = document.getElementById('fc');
         document.body.appendChild(private.calObj);
      },

      show: function(cObj) {
         // Initialize
         
         var cDate = cObj.value;

         private.curDate = new Date();
         //private.curDate.setFullYear(year, month, day);

         //var cStamp = cObj.xCal ? cObj.xCal : cObj.value;
         //private.curDate = isNaN(cStamp) ? new Date(oStamp * 1000) : new Date(cStamp * 1000);
         private.jsDate  = new Date(private.curDate.getTime());
         private.curObj  = cObj;
         public.update();

         // Style
         private.calObj.style.top  = private.offsetTop(cObj)  + 'px';
         private.calObj.style.left = private.offsetLeft(cObj) + 22 + 'px';
         if (public.getViewportWidth() < 1100) {
            private.calObj.style.left = private.offsetLeft(cObj) - 200 + 'px';
         }  

         private.calObj.style.zIndex = '1000';
         private.calObj.style.display = 'block';

         // Show (fade-in)
         clearInterval(private.fadeId);
         private.fadeId = setInterval("XCal.fade(1)", 15);
      },

      fade: function(io) {
         private.opacity = io ? private.opacity + 0.05 : private.opacity - 0.05;
         private.opacity = (private.opacity > 1) ? 1 : private.opacity;
         private.opacity = (private.opacity < 0) ? 0 : private.opacity;

         if (navigator.appName == "Microsoft Internet Explorer") {
            private.calObj.style.filter  = "alpha(opacity=" + parseInt(100 * private.opacity) + ")";
         }
         else {
            private.calObj.style.opacity = private.opacity;
         }

         if (!io && private.opacity == 0) {
            private.calObj.style.display = 'none';
         }

         if (private.opacity == 1 || private.opacity == 0) {
            clearInterval(private.fadeId);
         }
      },

      update: function() {
         // Current selected date
         var curDate     = private.curDate.getDate();
         var curMonth    = private.curDate.getMonth();
         var curYear     = private.curDate.getFullYear();

         // Current date
         var nowDate     = private.nDate.getDate();
         var nowMonth    = private.nDate.getMonth();
         var nowYear     = private.nDate.getFullYear();

         // Current calendar
         var tmpDate = new Date(private.jsDate.getTime());
         tmpDate.setDate(1);
         var calMonthStart   = tmpDate.getDay();
         var calMonthStart   = calMonthStart ? calMonthStart : 7;

         var calYear         = private.jsDate.getFullYear();
         var calMonth        = private.jsDate.getMonth();
         var calMonthLength  = public.isLeapYear(calYear) ? private.normMonthLength[calMonth] : private.leapMonthLength[calMonth];

         for (var i = 1; i <= 42; i++) {
            var cObj = document.getElementById('xcal_' + i);

            // Outside current month
            if (i < calMonthStart || i + 1 > calMonthStart + calMonthLength) {
               cObj.className = 'empty';
               cObj.innerHTML = '';
               cObj.onclick = null;
               continue;
            }

            // Before current day
            if ((i - calMonthStart + 1 < nowDate && nowMonth == calMonth && nowYear == calYear) || nowMonth > calMonth && nowYear == calYear) {
               cObj.className = 'obsolete';
               cObj.innerHTML = i - calMonthStart + 1;
               cObj.onclick = null;
               continue;
            }

            cObj.onclick = XCal.onClick;
            cObj.innerHTML = i - calMonthStart + 1;

            // Current day
            if (i - calMonthStart + 1 == curDate && curMonth == calMonth && curYear == calYear) {
               cObj.className = 'current';
               continue;
            }

            cObj.className = 'date';
         }

         document.getElementById('yearBox').innerHTML = private.monthNames[calMonth] + ' ' + calYear;
      },

      hide: function() {
         clearInterval(private.fadeId);
         private.fadeId = setInterval("XCal.fade(0)", 15);
      },

      docClick: function (e) {
         e ? evt = e : evt = event;
         cObj = evt.target ? evt.target : evt.srcElement;

         while (cObj) {
            if (cObj == private.calObj) {
               return;
            }

            cObj = cObj.parentNode;
         }

         public.hide();
      },

      onClick: function(e) {
         e ? evt = e : evt = event;
         cObj = evt.target ? evt.target : evt.srcElement;

         private.jsDate.setDate(parseInt(cObj.innerHTML));
         private.curObj.xCal = private.jsDate.getTime() / 1000;

         var cDate  = "0" + private.jsDate.getDate();
         var cMonth = "0" + (private.jsDate.getMonth() + 1);
         var cYear  = private.jsDate.getFullYear();

		 private.curObj.value = cDate.substr((cDate.length-2),2) + '/' + cMonth.substr((cMonth.length-2),2) + '/' + cYear;         public.hide();
      },

      isLeapYear: function(year) {
         return (year % 4) == 0;
      },

      prevMonth: function() {
         if (private.jsDate.getMonth() == private.nDate.getMonth() && private.jsDate.getFullYear() == private.nDate.getFullYear()) {
            XMessage.show("Een reservering maken in het verleden is helaas niet mogelijk.<br />A reservation in the past is not possible.");
            return;
         }

         private.jsDate.setMonth(private.jsDate.getMonth() - 1);
         public.update();
      },

      nextMonth: function() {
         private.jsDate.setMonth(private.jsDate.getMonth() + 1);
         public.update();
      },
      
      getViewportWidth: function() {
         if (typeof window.innerWidth != 'undefined') {
            return window.innerWidth;
         }
         
         if (typeof document.documentElement != 'undefined'
          && typeof document.documentElement.clientWidth != 'undefined') {
            return document.documentElement.clientWidth;
          }
      }

   };

   return public;
}();
addListener(window, 'load', XCal.init);
addListener(window, 'click', XCal.docClick);

function showHoteliers() {
   //pageTracker._link("http://www.hoteliers.com/wlpEngine.php?ID=654&lang=" + bookLang);
   frames["bookFrame"].location.href = "http://www.hoteliers.com/wlpEngine.php?ID=654&lang=en";

   document.getElementById("contentBox").style.display = "none";
   document.getElementById("frameBox").style.display = "block";
   return false;
}

function showHoteliersResult(form) {
   //pageTracker._trackPageview("/boeken.html");

   var aDateSet = false;
   var dDateSet = false;
   var regDate = /^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/]20\d{2}$/;

   // Prepare arrival date
   var aDate = document.getElementById("d_arrival").value;
   if (regDate.test(aDate)) {
      var aDay   = trimNumber(aDate.substring(0, 2));
      var aMonth = trimNumber(aDate.substring(3, 5));
      var aYear  = aDate.substring(7);

      aDate = new Date();
      aDate.setFullYear(aYear, aMonth, aDay);

      document.getElementById("dd").value  = aDay;
      document.getElementById("dm").value  = aMonth;
      document.getElementById("AEE").value = aYear;
      aDateSet = true;
   }

   // Prepare departure date
   var dDate = document.getElementById("d_departure").value;
   if (regDate.test(dDate)) {
      var dDay   = trimNumber(dDate.substring(0, 2));
      var dMonth = trimNumber(dDate.substring(3, 5));
      var dYear  = dDate.substring(7);
      
      dDate = new Date();
      dDate.setFullYear(dYear, dMonth, dDay);

      document.getElementById("ad").value  = dDay;
      document.getElementById("am").value  = dMonth;
      document.getElementById("ASS").value = dYear;
      dDateSet = true;
   }

   if (!dDateSet || !dDateSet) {
      XMessage.show("De datum is niet in het juiste formaat (dd/mm/jjjj).<br />The dates are not in the correct format (dd/mm/yyyy).");
      return;
   }

   // Aantal dagen...
   var tNights = Math.round((aDate.getTime() - dDate.getTime()) / 1000 / 86400);
   document.getElementById("NN").value = tNights;

   // Show result
   form.submit();
   document.getElementById("contentBox").style.display = "none";
   document.getElementById("frameBox").style.display = "block";
} 

function trimNumber(s) {
   return s.replace(/^0+/, '');
}
