
function propUpdate(thisE) {
  newProp=thisE.value;
  if (window.prop != newProp) {
    window.propChanged=true;
    window.prop=newProp;
    book= new Book(11);
    if (!book.checkProperty()) return;
    if (!book.checkFuture()) return;
    if (!book.checkForwards()) return;
    setTimeout(
      function tempfunc() {
        if (!book.checkChangeover()) return; 
        if (!book.checkAvailability()) return;
        book.hideError();
      },
      500
    ); 
    //if (!book.checkChangeover()) return;
    //book.hideError();

  }
  else window.propChanged=false;
}

function selectDateAddon(thisID) {
  book= new Book(thisID);
  if (!book.checkProperty()) return;
  if (!book.checkFuture()) return;
  if (!book.checkForwards()) return;
  if (!book.checkChangeover()) return;
  setTimeout(
    function tempfunc() {
      if (!book.checkAvailability()) return;
      book.hideError();
    },
    500
  );
}

//Book Class:
function Book(thisID) {

  //Determine OtherID
  var test=parseInt(thisID)-2;
  if (document.getElementById("input"+test.toString())!=undefined) otherID=test;
  else otherID=parseInt(thisID)+2;

  this.thisE  =document.getElementById("input"+thisID);
  this.otherE =document.getElementById("input"+otherID);
  this.dateMsg= document.getElementById("dateMsg");
  //Get unix dates for current values (NB switch day and month as parse() uses m/d/yyyy)
  var otherV=this.otherE.value.split('/');
  var thisV=this.thisE.value.split('/');
  if (thisID > otherID) {
    this.startDate= Date.parse(otherV[1]+"/"+otherV[0]+"/"+otherV[2]);
    this.endDate=  Date.parse(thisV[1]+"/"+thisV[0]+"/"+thisV[2]);
  }
  else {
    this.startDate= Date.parse(thisV[1]+"/"+thisV[0]+"/"+thisV[2]);
    this.endDate= Date.parse(otherV[1]+"/"+otherV[0]+"/"+otherV[2]);
  }
  
  this.days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

  this.checkFuture = function() {
    this.hideError();
    d= new Date();
    this.today= d.getTime();
    if (this.startDate < this.today || this.endDate < this.today) {
      this.error(1);
      return false;
    }
    return true;
  }

  this.checkForwards = function() {
    this.hideError();
    if (this.otherE.value != "") {
      if (this.startDate > this.endDate) {
        this.error(2);
        return false;
      }
    }
    return true;
  }

  this.checkProperty = function() {
    this.hideError();
    if (window.prop == undefined || window.prop=='false') {
      this.error(6);
      return false;
    }
    else {
      if (window.propChanged) {
        this.getChangeover();
        this.getCalendar();
        window.propChanged=false; //set back to false now that the changeover day is retrieved
      }
      this.getAvailability();
    }
    return true;
  }

  this.checkChangeover = function() {
    this.hideError();
    s= new Date();
    e= new Date();

    s.setTime(this.startDate);
    e.setTime(this.endDate);

    var peak=[5,6,7]; //nb January = 0, December=11
    var startMon=s.getMonth();
    var endMon=e.getMonth();
    
    var pass=false;
    for (x in peak) {
      if (startMon==peak[x] || endMon==peak[x]) pass=true;
    }
    if (pass) {
      var err=false;
      if (!isNaN(this.startDate) && s.getDay()!=window.changeover) {
        this.error(3);
        err=true;
      }
      if (!isNaN(this.endDate) && e.getDay()!=window.changeover) {
        this.error(4);
        err=true;
      }
      if (err) {
        useful=[window.changeover];
        this.error(5,useful);
        return false
      }
    }
    return true;
  }

  this.checkAvailability = function() {
    this.hideError();
    if (window.available=='true') {
      return true;
    }
    else {
      this.error(7,window.available);
      return false;
    }
  }

  this.getAvailability = function() {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) return true;
    if (window.prop != undefined && window.prop!="false") {
      var url="/propertyfrance/lib/ajax/checkAvailability.php?prop="+window.prop+"&startDate="+this.startDate+"&endDate="+this.endDate+"&rand="+Math.random();
      xmlHttp.onreadystatechange=this.getAvailabilityOutput;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
    }
  }

  this.getAvailabilityOutput = function() {
    if (xmlHttp.readyState==4) {
      window.available=xmlHttp.responseText;
    }
  }
  
  this.getCalendar = function() {
    xmlCal=GetXmlHttpObject();
    if (xmlCal==null) return true;
    if (window.prop !=undefined && window.prop!="false") {
      var url="/propertyfrance/lib/ajax/getCalendar.php?prop="+window.prop+"&rand="+Math.random();
      xmlCal.onreadystatechange=this.getCalendarOutput;
      xmlCal.open("GET",url,true);
      xmlCal.send(null);
    }
  }
  
  this.getCalendarOutput = function() {
    if (xmlCal.readyState==4) {
      var div=document.getElementById("availability");
      div.innerHTML=xmlCal.responseText;
    }
  }

  this.getChangeover = function() {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) return true;
    if (window.prop != undefined && window.prop!="false") {
      var url="/propertyfrance/lib/ajax/checkChangeover.php?prop="+window.prop+"&rand="+Math.random();
      xmlHttp.onreadystatechange=this.getChangeoverOutput;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
    }
  }

  this.getChangeoverOutput = function() {
    if (xmlHttp.readyState==4) {
      window.changeover=xmlHttp.responseText;
    }
  }

  this.error = function(type,useful) {
    switch (type) {
      case 1: this.msg= 'The booking dates must both be in the future.  Please check the dates entered'; break;
      case 2: this.msg= 'The arrival date must be before the departure date.  Please check the dates entered'; break;
      case 3: this.msg= 'Please check the arrival date.'; break;
      case 4: this.msg= 'Please check the departure date.'; break;
      case 5: this.msg= 'The changeover day is <strong>'+this.days[useful[0]]+'</strong>.   Unless agreed otherwise, we can only accept standard week bookings in June, July and August.'; break;
      case 6: this.msg= 'Please select a property before entering dates so we can automatically double-check they are available'; break;
      case 7: this.msg= 'These dates are not available.  There are existing bookings '+useful; break;
    }
    this.thisE.style.borderColor="red";
    this.otherE.style.borderColor="red";
    if (this.dateMsg.innerHTML != "&nbsp;") {
      this.dateMsg.innerHTML=this.dateMsg.innerHTML+" "+this.msg;
    }
    else {
      this.dateMsg.innerHTML=this.msg;
    }
    this.dateMsg.style.display="block";

  }

  this.hideError = function() {
    this.thisE.style.borderColor="";
    this.otherE.style.borderColor="";
    this.dateMsg.innerHTML="&nbsp;";
    this.dateMsg.style.display="inline";
  }

}