// JavaScript Document

var targetDate = new Date();
var arrivalDate = new Date();
var previousMonth;
var reservations;


function initCalendar(){
  document.getElementById("calendarSheet").style.display = "none";
  document.getElementById("calendarLoader").style.display = "inline";
  monthYear = targetDate.getFullYear()+"/"+targetDate.getMonth()
  httpRequest("system/modules/ajaxHandlers.ashx?id=a1&passValue=" + monthYear, "getReservations", false);
}
	
function getReservations(reply) {
	if (reply!="error"){
		reservations = eval('('+ reply +')');
		document.getElementById("calendarLoader").style.display = "none";
		setCalendar()
	} 
	else
	{
		alert("Er is iets fout gegaan bij het laden van de reserveringen.");
		document.getElementById("calendarLoader").style.display = "none";
		setCalendar()
	}
	
}

function roomReserved(date) {
	var kamer1r, kamer2r;
	for(x in reservations.kamer1) {
		if(date.toString()==reservations.kamer1[x].toString()) {
			kamer1r = true;
		}
	}
	for(x in reservations.kamer2) {
		if(date.toString()==reservations.kamer2[x].toString()) {
			kamer2r = true;
		}
	}
	if(kamer1r==true && kamer2r==true) 
		return 3;
	if(kamer2r==true) 
		return 2;
	if(kamer1r==true) 
		return 1;
	return 0;
}

function setCalendar(event) {
  var el, tableEl, rowEl, cellEl, linkEl;
  var tmpDate, tmpDate2;
  var i, j;

  arrivalDate = new Date(Date.parse(targetDate));
  el = document.getElementById("calendarHeader").firstChild;
  el.nodeValue = targetDate.getMonthName() + "\u00a0" + targetDate.getFullYear();

  tmpDate = new Date(Date.parse(targetDate));
  tmpDate.setDate(1);
  while (tmpDate.getDay() != 1) {
    tmpDate.addDays(-1);
  }
  tableEl = document.getElementById("calendar");
  for (i = 2; i <= 7; i++) {
    rowEl = tableEl.rows[i];
    tmpDate2 = new Date(Date.parse(tmpDate));
    tmpDate2.addDays(6);
    if (tmpDate.getMonth()  != targetDate.getMonth() &&
        tmpDate2.getMonth() != targetDate.getMonth())
      rowEl.className = "empty";
    else
      rowEl.className = "";
    for (j = 0; j < rowEl.cells.length; j++) {
	  dateStatus = 0;
      cellEl = rowEl.cells[j];

      if (tmpDate.getMonth() == targetDate.getMonth()) {
		
		//cellEl.onclick = "setTargetDate(event, this); return false;";
        cellEl.date = new Date(Date.parse(tmpDate));
        s = tmpDate.toString().split(" ");
        cellEl.title = tmpDate.getDate().toString() + "-" + eval(tmpDate.getMonth()+1).toString() + "-" + tmpDate.getFullYear().toString();
        cellEl.firstChild.nodeValue = tmpDate.getDate();
        cellEl.style.visibility = "";
		dateStatus = roomReserved(tmpDate.getFullYear().toString() + "-" + eval(tmpDate.getMonth()+1).toString() + "-" +  tmpDate.getDate().toString());
		
		if(dateStatus==3) {	
			cellEl.style.cursor = "not-allowed";
			cellEl.onclick = "";
			if (cellEl.className == "weekend" || cellEl.oldClass == "weekend") {
				cellEl.oldClass = "weekend";
			} else {
				cellEl.oldClass = "dateFull";
			}
			cellEl.className = "dateFull";
		}
		
		if(dateStatus==2 || dateStatus==1) {
			cellEl.onclick = function() {setTargetDate(event, this); return false;}
			cellEl.style.cursor = "pointer";
			if (cellEl.className == "weekend" || cellEl.oldClass == "weekend") {
				cellEl.oldClass = "weekend";
			} else {
				cellEl.oldClass = "dateHalf";
			}
			cellEl.className = "dateHalf";
		}
		
		if(dateStatus==0) {
			cellEl.style.cursor = "pointer";
			if (cellEl.className == "weekend" || cellEl.className == "weekend target" || cellEl.oldClass == "weekend") {
				cellEl.onclick = function() {setTargetDate(event, this); return false;}
				cellEl.className = "weekend";
				cellEl.oldClass = "weekend";
			} else {
				cellEl.onclick = function() {setTargetDate(event, this); return false;}
				cellEl.className = null;
				cellEl.oldClass = null;
			}
		}	
      }
      else
	  {
        cellEl.style.visibility = "hidden";
		cellEl.className = null;
	  }
		
      if (cellEl.oldClass == null)
        cellEl.oldClass = cellEl.className;
		
      if (Date.parse(tmpDate) == Date.parse(targetDate))
	  {
        cellEl.className = cellEl.oldClass + " target";
	  }
      //else
        //cellEl.className = cellEl.oldClass;
		
      tmpDate.addDays(1);
    }
  }
 document.getElementById("calendarSheet").style.display = "block";
 if(document.getElementById("targetDateField")){
	 document.getElementById("targetDateField").value = targetDate.getDate().toString() + "-" + eval(targetDate.getMonth()+1).toString() + "-" + targetDate.getFullYear().toString();
	 numberNightsList = document.getElementById("numberNights");
	 numberNightsList.options.length = 0;
	 nextDate = new Date(Date.parse(targetDate));
	
		 for (i=1;i<20; i++){
			 if(roomReserved(nextDate.getFullYear().toString() + "-" + eval(nextDate.getMonth()+1).toString() + "-" +  nextDate.getDate().toString())!= 3)
			 {
				 numberNightsList.options[numberNightsList.options.length] = new Option(i,i,false, false);
			 }
			 else
			 {
				 break;
			 }
			 nextDate.addDays(1);
		 }
	
	 nightsList = document.getElementById("numberNights");
	 if (nightsList.length != 0) {
		calcDeparture(nightsList.options[nightsList.selectedIndex].value);
	}
 }
}

function calcDeparture(n) {
	departureDate = new Date(Date.parse(arrivalDate));
	departureDate.addDays(eval(n));
	document.getElementById("targetDateLeaveField").value = departureDate.getDate().toString() + "-" + eval(departureDate.getMonth()+1).toString() + "-" + departureDate.getFullYear().toString();
	if(admin){
		calcPeriodAdmin();
	}
}

function addMonths(event, n) {
  targetDate.addMonths(n);
  initCalendar(event);
}

function addYears(event, n) {
  targetDate.addYears(n);
  initCalendar(event);
}

function setTargetDate(event, link) {
  if (link.date != null) {
    targetDate = new Date(Date.parse(link.date));
    setCalendar(event);
	if(admin){
		calcPeriodAdmin();
	}
  }
}

function displayDate(event) {
  alert(formatDate(targetDate));
}

function formatDate() {

  var mm, dd, yyyy;

  mm = String(targetDate.getMonth() + 1);
  while (mm.length < 2)
    mm = "0" + mm;
  dd = String(targetDate.getDate());
  while (dd.length < 2)
    dd = "0" + dd;
  yyyy = String(targetDate.getFullYear());
  while (yyyy.length < 4)
    yyyy = "0" + yyyy;

  return mm + "/" + dd + "/" + yyyy;
}


Date.prototype.monthNames = new Array("januari", "februari", "maart", "april",
  "mei", "juni", "juli", "augustus", "september", "oktober", "november",
  "december");
Date.prototype.savedDate  = null;
Date.prototype.getMonthName = dateGetMonthName;
Date.prototype.getDays      = dateGetDays;
Date.prototype.addDays      = dateAddDays;
Date.prototype.addMonths    = dateAddMonths;
Date.prototype.addYears     = dateAddYears;

function dateGetMonthName() {

  return this.monthNames[this.getMonth()];
}

function dateGetDays() {

  var tmpDate, d, m;

  tmpDate = new Date(Date.parse(this));
  m = tmpDate.getMonth();
  d = 28;
  do {
    d++;
    tmpDate.setDate(d);
  } while (tmpDate.getMonth() == m);

  return d - 1;
}

function dateAddDays(n) {
  this.setDate(this.getDate() + n);
  this.savedDate = this.getDate();
}

function dateAddMonths(n) {
  if (this.savedDate == null)
    this.savedDate = this.getDate();
  this.setDate(1);
  this.setMonth(this.getMonth() + n);
  this.setDate(Math.min(this.savedDate, this.getDays()));
}

function dateAddYears(n) {
  if (this.savedDate == null)
  this.savedDate = this.getDate();
  this.setDate(1);
  this.setFullYear(this.getFullYear() + n);
  this.setDate(Math.min(this.savedDate, this.getDays()));
}
function checkDate(field){
var checkstr = "0123456789-";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "-";
var day = "";
var month = "";
var year = "";
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   alert
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }

   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   //if (DateValue.length == 6) {
      //DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   DateArray = DateValue.split("-")

if (DateArray.length != 3) {
      err = 19;}
   /* year is wrong if year = 0000 */
else{
   year = DateArray[2];
   if (year.length == 2) {
	   year = '20' + year;
   }
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateArray[1];
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   
   day = DateArray[0];
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "1") || (month == "3") || (month == "5") || (month == "7") || (month == "8") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "4") || (month == "6") || (month == "9") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
}
   if (DateValue == ""){
	   err = 0;
	   seperator = "";
	} 
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
	  //DateField.className = "formField"
	  
   }
   /* Error-message if err != 0 */
   else {
	  alert("De ingevulde geboortedatum is onmogelijk!\nWijzig de datum op een correcte manier.");
      DateField.select();
	  DateField.focus();
   }
}
