var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var leapMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var activeDp = null;
var mousePressed = false;
var calenderBuilding = false;

document.onmouseup = documentMouseup;

function documentMouseup(){ if (mousePressed || calenderBuiling) { mousePressed = false; return false;}; tryBlur();}
function DatePicker(){this.initialize = initialize_object;this.canBlur = true;function initialize_object(){if (this.canvas != null){this.canvasElement = getElement(this.canvas);}if (this.anchor != null){this.anchorElement = getElement(this.anchor);this.anchorElement.onclick = DatePicker_AnchorEvents;this.anchorElement.onmouseover = DatePicker_AnchorEvents;this.anchorElement.parent = this;}}function DatePicker_AnchorEvents(){if (activeDp != this.parent)doShow(this.parent);}}
function getElement(id){var result = document.getElementById(id);return result == null ? document.all[id] : result;}
function setDate(dp, day){var date = dp.format;if (day == 0){dp.year = currYear;dp.month = currMonth;day = currDate;} if (date.indexOf('yyyy') >= 0) date = date.replace('yyyy',dp.year.toString());else date = date.replace('yy',dp.year.toString().substring(2,4));if (date.indexOf('mmm') >= 0) date = date.replace('mmm', months[dp.month-1].substring(0,3));else if (date.indexOf('mm') >= 0){ var monthString =  dp.month.toString();monthString = monthString.length == 1 ? '0' + monthString : monthString; date = date.replace('mm', monthString);}else date = date.replace('m', dp.month.toString());	if (date.indexOf('dd') >= 0){var dateString = day.toString();dateString = dateString.length == 1 ? '0' + dateString : dateString;date = date.replace('dd',dateString); }else date = date.replace('d',day);getElement(dp.id).value = date;dp.canBlur = true;doHide(dp);}
function tryFocus(dp){ dp.canBlur = false; }
function tryBlur(){if (activeDp != null) activeDp.canBlur = true;setTimeout("doHide();",50);}
function movePrev(isMonth){var dp = activeDp;dp.canBlur = false;if (isMonth){if (dp.month > 1){dp.month --;}else{dp.month = 12;dp.year --;}}else dp.year--;buildCalendar(dp);}
function moveNext(isMonth){var dp = activeDp;dp.canBlur = false;if (isMonth){if (dp.month < 12){dp.month ++;}else{dp.month = 1;dp.year ++;}}else dp.year++;buildCalendar(dp);}
function doShow(dp){if (activeDp == dp){activeDp.canBlur = true;doHide();}else{if (activeDp != null){activeDp.canBlur = true;doHide();}if (dp.overlayControls != null){var cons = dp.overlayControls.split(",");for(i=0; i < cons.length; i++)	getElement(cons[i]).style.visibility = "hidden";}dp.canBlur = false;dp.canvasElement.style.visibility = "visible";document.getElementById('nb4MainForm').numberOfRooms.style.visibility='hidden';document.getElementById('nb4MainForm').adults.style.visibility='hidden';dp.canvasElement.style.zIndex = 1;activeDp = dp;buildCalendar(dp)}}
function doHide(){if (activeDp != null && activeDp.canBlur) {activeDp.canvasElement.style.visibility = "hidden";document.getElementById('nb4MainForm').numberOfRooms.style.visibility='visible';document.getElementById('nb4MainForm').adults.style.visibility='visible';activeDp.canvasElement.style.zIndex = -1;if (activeDp.overlayControls != null){var cons = activeDp.overlayControls.split(",");for(i=0; i < cons.length; i++)getElement(cons[i]).style.visibility = "visible";}activeDp = null;}}
function isDateInRange(from, to, current){ var result = false; if (from == null && to == null) result = true; else if ((from != null && current < from) || (to != null && current > to)) result = false; else result = true; return result;}
function getDayNameCell(dayIndex){var dayClass = "dayNames"; if (!isWorkingDay(dayIndex)) dayClass = "nwDayNames"; return "<td align='center' class='" + dayClass + "' width=21>" + daysOfWeek[dayIndex].substring(0,2) + "</td>";}
function isWorkingDay(dayIndex){return nonWorkingDays.indexOf(dayIndex) < 0;}
function getDayClass(currentClass, dayIndex){var dateClass = currentClass; if (!isWorkingDay(dayIndex)) dateClass = "nwday";  return dateClass;}
function moveAuto(direction){var delay = 50; switch(direction){ case 'yearPrev': movePrev(false); break; case 'yearNext': moveNext(false); break; case 'monthPrev': delay = 300; movePrev(true); break; case 'monthNext': delay = 300; moveNext(true); break;} if (mousePressed) setTimeout('moveAuto("' + direction + '")',delay);}
function startAuto(direction){ mousePressed = true; setTimeout('moveAuto("' + direction + '")',300);}
function stopAuto(){mousePressed = false;}
function buildCalendar(dp){
  calenderBuiling = true;
  var m = dp.month - 1;var isLeap = true; if (dp.year % 4 != 0) isLeap = false; else if (dp.year % 400 == 0) isLeap = true; else if (dp.year % 100 == 0) isLeap = false;
  var days = monthDays[m];
  if (isLeap) days = leapMonthDays[m];
  var dDate = new Date();
  dDate.setDate(1);
  dDate.setMonth(m);
  dDate.setFullYear(dp.year);
  var firstDay = dDate.getDay();
  var calCode = "<table border=0 cellspacing=0 cellpadding=2 width='100%' height='100%' class='calbg' onclick='tryFocus(dp" + dp.id + ");' onfocus='tryFocus(dp" + dp.id + ");'>"
  calCode += "<tr><td align='center'><a href='javascript:;' class='linknormal' onmousedown='startAuto(\"yearPrev\");' onfocus='tryFocus(dp" + dp.id + ");'>&lt;</a></td><td colspan=5 align='center' class='year'><b>"+ dp.year +"</b></td><td align='center'><a href='javascript:;' class='linknormal' onmousedown='startAuto(\"yearNext\");' onfocus='tryFocus(dp" + dp.id + ");'>&gt;</a></td></tr>";
  calCode += "<tr><td align='center'><a href='javascript:;' class='linknormal' onmousedown='startAuto(\"monthPrev\");' onfocus='tryFocus(dp" + dp.id + ");'>&lt;</a></td><td colspan=5 align='center' class='month'>"+ months[m] +"</td><td align='center'><a href='javascript:;' class='linknormal' onfocus='tryFocus(dp" + dp.id + ");' onmousedown='startAuto(\"monthNext\");'>&gt;</a></td></tr>";
  calCode += "<tr>";
  for (i = 0; i < 7; i++) // we have only 7 days to print
    if (i + weekStartDay < 7) // print first day names
    	calCode += getDayNameCell(i + weekStartDay)
    else
	calCode += getDayNameCell(i + weekStartDay-7); // print last day names

  calCode += "</tr>";
  colIndex = 0;

  var dayIndex = 0;
  var loopDate = 0;
  var displayDate = new Date();
  displayDate.setMonth(m);
  displayDate.setFullYear(dp.year);
  displayDate.setDate(1);

  firstDay -= weekStartDay;
  if (firstDay < 0) 
    firstDay = 7 - weekStartDay;
  for (i = -firstDay + 1; i < 36 - firstDay ; i++)
  {
    loopDate = 0;	

    if (colIndex >6)
    {
      colIndex = 0;
      calCode += "</tr><tr>";
    }
    dayIndex = colIndex + weekStartDay;
    if (colIndex + weekStartDay > 6)
	dayIndex = 7 - dayIndex;
    colIndex++;
    

    if (days - i >= 35 && i < 0){
      calCode += "<td align='right' class='" + getDayClass("day",dayIndex) + "'>";
      loopDate  = 35 + i;
    }
    else if (i < 1 || i > days){
      calCode += "<td>";
      loopDate = 0;	
    }
    else if (i == currDate && dp.month == currMonth && dp.year == currYear){ 
      calCode += "<td align='right' class='" + getDayClass("cday",dayIndex) + "'>";
      loopDate  = i;	

    }
    else if (i > 0) {
      calCode += "<td align='right' class='" + getDayClass("day",dayIndex) + "'>";
      loopDate  = i;
    }

    if (loopDate > 0)
      displayDate.setDate(loopDate);

    if (loopDate <= 0)
	calCode += "&nbsp;";
    else if (isDateInRange(dp.fromDate, dp.toDate, displayDate))
	calCode += "<a href='javascript:;' class='linkbold' onfocus='tryFocus(dp" + dp.id + ");' onclick='setDate(dp" + dp.id + "," + loopDate.toString() + ");ancEndDate.disabled=false;'>"+ loopDate.toString() +"</a>"
    else
	calCode += loopDate.toString();

    calCode += "</td>";
  }
  calCode += "</tr>";
  calCode += "<tr><td align='left' colspan=7 class='today'>";
  var currentDate = new Date(currYear.toString() + "/" + months[currMonth-1] + "/" + currDate.toString());
  if (isDateInRange(dp.fromDate, dp.toDate, currentDate))
  	calCode += "<a href='javascript:;' onfocus='tryFocus(dp" + dp.id + ");' onclick='setDate(dp" + dp.id + ",0);'>" + todayCaption + ": " + months[currMonth-1] + " " + currDate.toString() + ", " + currYear.toString() + "</a>";
  else
  	calCode += todayCaption + ": " + months[currMonth-1] + " " + currDate.toString() + ", " + currYear.toString();

  calCode += "</td></tr>";
  dp.canvasElement.innerHTML = calCode;
  calenderBuiling = false;
}
