/*****************

  (c) 2006 Q42 B.V.

  The contents of this file, partially or in whole, may not be reproduced
  without prior written permission by Q42 B.V.

*****************/

function PricesAndAvailability()
{
	Spif.DOMEvents.attach(window, "load", this.doLoad, this);
}
PricesAndAvailability.prototype =
{
  doLoad: function() {
    var paaa = document.getElementById("prices-and-availability-accommodations");
    if (!paaa) return;

    var radios = paaa.getElementsByTagName("input");
    for (var i = 0; i < radios.length; i++) {
      Spif.DOMEvents.attach(radios[i], "click", this.reloadPlanboard, this);
    }

    Spif.DOMEvents.attach(document.getElementById("prices-and-availability-number-of-people-select"), "change", this.reloadPlanboard, this);
    Spif.DOMEvents.attach(document.getElementById("prices-and-availability-summary-submit"), "click", this.submitForm, this);
    Spif.DOMEvents.attach(document.getElementById("prices-and-availability-calendar-select"), "click", this.selectCalendar, this);
    Spif.Utils.setTimeoutHandler(this.enablePersonSelection, 100)
  },
  clickCalendar: function(evt) {
  },
  enablePersonSelection: function(evt) {
    document.getElementById("prices-and-availability-number-of-people-select").disabled = false;
  },
  updateAccommodationSelection: function() {
    // This function is also called in the insertSubPage function in accommodation.js, line 135, to
    // make sure that the selected accommodation is highlighted after a refresh of the accommodation subpage.

    // htmlcode waar hieronder naar gerefereerd wordt is te vinden in Accommodations.ascx in de Accommodation folder
    // get product id
    var paaa = document.getElementById("prices-and-availability-accommodations");
    if (paaa) {
      var radios = paaa.getElementsByTagName("input");
      for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked) {
          break;
        }
      }
    }
    var selected_productid = document.getElementById(radios[i].id).value;
    document.getElementById('accommodation-tab-selection').value = "accommodations-" + selected_productid;

    // check if the list of accommodations exists
    if (document.getElementById("accommodation-list-items") != null) {
      var accommodationListItems = document.getElementById("accommodation-list-items").getElementsByTagName("li");
      for (var i = 0; i < accommodationListItems.length; i++) {
        if (accommodationListItems[i].id == selected_productid) // id van li-tag is productid van accommodation, zie Accommodations.asxc
        {
          // deze infodiv van unselected --> selected
          Spif.ClassNameAbstraction.replace(document.getElementById(accommodationListItems[i].id + "-info"), "accommodation-info-unselected", "accommodation-info-selected");
        }
        else {
          // rest van de div's van selected --> unselected
          Spif.ClassNameAbstraction.replace(document.getElementById(accommodationListItems[i].id + "-info"), "accommodation-info-selected", "accommodation-info-unselected");
        }
      }
    }
    // ----------------------------------------------------
  },
  reloadPlanboard: function(evt, el, self, id) {
    var accommodationObj = "";
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask3"), "planboard-mask", "planboard-unmask");
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask5"), "planboard-mask", "planboard-unmask");
    var radios = document.getElementById("prices-and-availability-accommodations").getElementsByTagName("input");
    for (var i = 0; i < radios.length; i++) {
      if (radios[i].checked) {
        accommodationObj = radios[i];
        break;
      }
    }

    // Edited by Rens 02-10-2006 : Removed code and placed it inside updateAccommodationSelection
    // This function is also called in the insertSubPage function in accommodation.js, line 135, to
    // make sure that the selected accommodation is highlighted after a refresh of the accommodation subpage. 
    var personsSelected = "&persons=";

    //Get mimimum number of persons for product
    var minpersonsmatches = accommodationObj.className.match(/\bminpersons-\d+\b/);
    var minpersons = 1;
    if (minpersonsmatches) minpersons = parseInt(minpersonsmatches[0].substring(11));

    // Fill number-of-people selectbox
    var personsmatches = evt.subject.className.match(/\bpersons-\d+\b/);
    if (personsmatches) {
      // when accommodationtab is selected or the accommodation details subpage is shown, reload the acco subpage
      // load the product
      accommodation.loadProduct(evt, el, self, id);
      // End of Edit

      var persons = parseInt(personsmatches[0].substring(8));
      var select = document.getElementById("prices-and-availability-number-of-people-select");
      var selectedValue = select.options[select.selectedIndex].value;
      var selectedNrOfPersons = parseInt(selectedValue);

      // clear selectbox
      select.innerHTML = "";

      //Valid NrOfPersons?
      if (selectedNrOfPersons > persons ||
			    (selectedNrOfPersons > 0 && selectedNrOfPersons < minpersons)) selectedValue = "0";

      // fill selectbox
      for (var i = 0; i <= persons; i++) {
        if (i != 0 && i < minpersons) continue;

        var option = select.appendChild(document.createElement("option"));
        if (i == 0) { option.text = resources.SpecialDecode(resources.messages.choose); }
        else { option.text = i; }

        option.value = i;
        if (option.value == selectedValue)
          option.selected = true;
      }

      personsSelected += selectedValue;

      var productCodeEl = document.getElementById("accommodationProduct-box").getElementsByTagName('input')[0];
      var productCode = document.getElementById(accommodationObj.id + "-code").value;
      productCode = productCode.replace(/[^a-zA-Z0-9]+/g, '-');
      productCodeEl.value = productCodeEl.value.substring(0, productCodeEl.value.lastIndexOf('/') + 1) + productCode;
      productCode = resources.messages.clicktracker.ct_acco_select_product.replace('##name##', document.getElementById("accommodationId-box").getElementsByTagName('input')[1].value) + productCode + '/';
      ClickTracker.trackClick(new Click(productCode));
    }
    else {
      var select = document.getElementById("prices-and-availability-number-of-people-select");
      personsSelected += select.options[select.selectedIndex].value;
      var persons = resources.messages.clicktracker.ct_acco_select_persons.replace('##name##', document.getElementById("accommodationProduct-box").getElementsByTagName('input')[0].value) + select.options[select.selectedIndex].value;
      ClickTracker.trackClick(new Click(persons));
    }

    //Add minimum number of persons to querystring
    personsSelected += "&minpersons=" + minpersons;

    // Reload the planboard (without filling the history)
    this.setMask();
    var urlAddon = "";
    var planboardUrl = document.getElementById("prices-and-availability-planboard-frame").contentWindow.document.location + "";

    if (planboardUrl.indexOf("&date=") != -1) {
      var date = planboardUrl.substring(planboardUrl.indexOf("&date=") + 6);
      if (date.indexOf("&") != -1) date = date.substring(0, date.indexOf("&"));
      urlAddon = "&date=" + date;
    }
    if (planboardUrl.indexOf("&mode=") != -1) {
      var mode = planboardUrl.substring(planboardUrl.indexOf("&mode=") + 6);
      if (mode.indexOf("&") != -1) mode = mode.substring(0, mode.indexOf("&"));
      urlAddon += "&mode=" + mode;
    }
    if (planboardUrl.indexOf("&delRows=") != -1) {
      var delRows = planboardUrl.substring(planboardUrl.indexOf("&delRows=") + 9);
      if (delRows.indexOf("&") != -1)
        delRows = delRows.substring(0, delRows.indexOf("&"));
      urlAddon += "&delRows=" + delRows;
    }
    else if (document.getElementById("prices-and-availability-planboard-frame").contentWindow.curRow > 0) {
      urlAddon += "&delRows=" + document.getElementById("prices-and-availability-planboard-frame").contentWindow.curRow;
    }

    // Bart 26.10 Issue: 5771 encode de id code en converteer het '+' naar %2B
    var url = "/Booking/Planboard.aspx?product-code=" + (escape(document.getElementById(accommodationObj.id + "-code").value)).replace("+", "%2B") + personsSelected + urlAddon;
    var calendarUrl = "/Booking/BookingCalendar.aspx?product-code=" + (escape(document.getElementById(accommodationObj.id + "-code").value)).replace("+", "%2B") + personsSelected + urlAddon;

    //document.getElementById("prices-and-availability-planboard-frame").contentWindow.document.location.replace(url);
    document.getElementById("prices-and-availability-planboard-frame").contentWindow.planboard.reloadMe(url);
    document.getElementById("prices-and-availability-calendar-frame").contentWindow.bookingcalendar.reloadMe(calendarUrl);

    // Hide the summary
    Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary"), "enabled", "disabled");
    Spif.ClassNameAbstraction.replace(document.getElementById("including-box"), "shown", "hidden");
    Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary"), "enabled", "disabled");
  },
  selectDate: function(date) {
    var accommodation = "";
    var radios = document.getElementById("prices-and-availability-accommodations").getElementsByTagName("input");
    var combo = document.getElementById("prices-and-availability-number-of-people-select");
    for (var i = 0; i < radios.length; i++) {
      if (radios[i].checked) {
        //Get mimimum number of persons for product
        var minpersonsmatches = radios[i].className.match(/\bminpersons-\d+\b/);
        var minpersons = "1";
        if (minpersonsmatches) minpersons = minpersonsmatches[0].substring(11);

        //Weenro [20061103] Issue: 5771
        accommodation = (escape(document.getElementById(radios[i].id + "-code").value)).replace("+", "%2B") + "&persons=" + combo.options[combo.selectedIndex].value + "&minpersons=" + minpersons;
        break;
      }
    }

    // Reload the planboard (without filling the history)
    this.setMask();
    document.getElementById("prices-and-availability-planboard-frame").contentWindow.document.location.replace("/Booking/Planboard.aspx?product-code=" + accommodation + "&date=" + date);

    // Hide the summary
    Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary"), "enabled", "disabled");
    Spif.ClassNameAbstraction.replace(document.getElementById("including-box"), "shown", "hidden");
    Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary-error"), "donotshow", "show");

    //calendarContainer = frames['prices-and-availability-calendar-frame'].document.getElementById("calendarContainer");
    //Spif.DOMEvents.attach(calendarContainer, "click", this.clickCalendar,this);
  },
  selectCalendar: function() {
    var el = document.getElementById("prices-and-availability-calendar-box")

    if (el.style.display == "none" || el.style.display == "") {
      el.style.display = "block";
      el.style.visibility = "visible";
    }
    else {
      el.style.display = "none";
      el.style.visibility = "hidden";
    }
    return false;
  },
  hideCalendar: function() {
    var el = document.getElementById("prices-and-availability-calendar-box")

    el.style.display = "none";
    el.style.visibility = "hidden";
  },
  selectCalendarDate: function(date) {
    var personsSelected;
    var personsmatches = document.getElementById("prices-and-availability-number-of-people-select");
    if (personsmatches) {
      personsSelected = personsmatches.options[personsmatches.selectedIndex].value;
    }

    var accommodationObj = "";
    var radios = document.getElementById("prices-and-availability-accommodations").getElementsByTagName("input");
    for (var i = 0; i < radios.length; i++) {
      if (radios[i].checked) {
        accommodationObj = radios[i];
        break;
      }
    }
    document.getElementById("prices-and-availability-calendar-frame").contentWindow.document.location.replace("/Booking/BookingCalendar.aspx?product-code=" + (escape(document.getElementById(accommodationObj.id + "-code").value)).replace("+", "%2B") + "&AutoSelectDate=false&date=" + date + "&persons=" + personsSelected);
  },
  setMask: function() {
    if (document.getElementById("prices-and-availability-calendar-box").style.display == "block") {
      document.getElementById("prices-and-availability-calendar-box").style.display = "none";
    }

    if (document.getElementById("planboard-mask2").className == "planboard-mask") {
      Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask2"), "planboard-mask", "planboard-unmask");
    }
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask"), "planboard-unmask", "planboard-mask");
  },
  setMask2: function() {
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask2"), "planboard-unmask", "planboard-mask");
    Spif.Utils.setTimeoutHandler(function() {
      Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask2"), "planboard-mask", "planboard-unmask");
    }, 2000);
  },
  setMask3: function() {
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask3"), "planboard-unmask", "planboard-mask");
    Spif.Utils.setTimeoutHandler(function() {
      Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask3"), "planboard-mask", "planboard-unmask");
    }, 4000);
  },
  setMask5: function() {
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask5"), "planboard-unmask", "planboard-mask");
  },
  clearMask: function() {
    Spif.ClassNameAbstraction.replace(document.getElementById("planboard-mask"), "planboard-mask", "planboard-unmask");
  },
  showSummary: function() {
    Spif.Utils.setTimeoutHandler(function() {
      Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary"), "disabled", "enabled");
      Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary-error"), "show", "donotshow");
    }, 0);

    //Wait some extra, then jump to reservation button
    setTimeout(function() { pricesAndAvailability.scrollIntoView(); }, 1000);
  },
  scrollIntoView: function() {
    try {
      var obj = document.getElementById('prices-and-availability-summary');
      var curleft;
      var curtop;
      curleft = 0;
      curtop = obj.offsetHeight;
      if (obj.offsetParent) {
        curleft += obj.offsetLeft;
        curtop += obj.offsetTop;
        while (obj = obj.offsetParent) {
          curleft += obj.offsetLeft;
          curtop += obj.offsetTop;
        }
      }

      //Get scroll position
      var ScrollTop = document.body.scrollTop;
      if (ScrollTop == 0) {
        if (window.pageYOffset) {
          ScrollTop = window.pageYOffset;
          if (ScrollTop >= 110) ScrollTop -= 110;
          else ScrollTop = 0;
        }
        else {
          ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }
      }
      curtop -= ScrollTop;

      //Get client height
      var myWidth = 0, myHeight = 0;
      if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }

      if (curtop > myHeight) {
        var el = document.getElementById('prices-and-availability-summary');
        if (el != null) {
          el.scrollIntoView(false);
        }
      }
    }
    catch (e) { }
  },
  submitForm: function(evt) {
    if (Spif.ClassNameAbstraction.contains(document.getElementById("prices-and-availability-summary"), "enabled")) {
      if (typeof (LinkByPost) == 'function') LinkByPost();

      document.getElementById("prices-and-availability-form").submit();
    }
    
    return false;
  },
  reloadCalendar: function(url) {
    document.getElementById("prices-and-availability-calendar-frame").contentWindow.document.location.replace(url);
  }
}
var pricesAndAvailability = new PricesAndAvailability();