﻿function selectAllShortlistItems(isChecked) {

  var getAllCheckboxes = document.getElementsByTagName("input");
  var chkAll1 = document.getElementById("chkAll");
  var chkAll2 = document.getElementById("chkAll2");

  for (var i = 0; i < getAllCheckboxes.length; i++) {
    if (getAllCheckboxes[i].type == "checkbox" && getAllCheckboxes[i].id.search("chkSelection") != -1) {
      if (isChecked) {
        getAllCheckboxes[i].checked = true;
        chkAll1.checked = true;
        chkAll2.checked = true;
      }
      else {
        getAllCheckboxes[i].checked = false;
        chkAll1.checked = false;
        chkAll2.checked = false;
      }
    }
  }
  return false;
}

function EmailPopup() {
  var accList = selectedAaccommodations();
  var queryString = accList.join("-");
  var email1 = document.getElementById("EmailPopup1");
  var email2 = document.getElementById("EmailPopup2");
  var pCode = document.getElementById(hdnProdCodeClientID).value;

  if (email1 != null && email2 != null) {
      email1.href = "../../WebPages/MyFavourites/EmailFavourites.aspx?pCode=" + pCode + "&code=" + queryString;
      email2.href = "../../WebPages/MyFavourites/EmailFavourites.aspx?pCode=" + pCode + "&code=" + queryString;
    return true;
  }
}

function PrintPopup() {

  var accList = selectedAaccommodations();
  var queryString = accList.join("-");
  var pCode = document.getElementById(hdnProdCodeClientID).value;
  var prodCode = pCode != null ? pCode : "";
  if (accList.length > 0) {

      window.open("../../WebPages/MyFavourites/PrintMyFavourites.aspx?pCode=" + pCode + "&code=" + queryString);
  }

  return false;
}

function removeItems() {
  var accList = removeSelectedAccList();
  var queryString = accList.join(",");
  var hdnAccList = document.getElementById(hdnAccommodationListClientID);

  //Store count in cookie
  updateCountInCookie(accList.length);

  if (accList.length > 0) {
    hdnAccList.value = queryString;
  }
  else {
    return false;
  }
}

function updateCountInCookie(FavCount) {
  var count = 0;
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf("MyFavCount=");
    if (c_start != -1) {
      c_start = c_start + 11;
      c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) c_end = document.cookie.length;
      count = unescape(document.cookie.substring(c_start, c_end));
    }
  }
  if (isNaN(count)) { count = 0; }

  var expdate = new Date();
  count = count - FavCount;
  expdate.setTime(expdate.getTime() + 1 * 24 * 60 * 60 * 1000); //Set expiry to 1 day
  document.cookie = 'MyFavCount=' + escape(count) + '; expires=' + expdate.toGMTString() + ";path=/";
}

function selectedAaccommodations() {
  var getAllCheckboxes = document.getElementsByTagName("input");
  var accList = new Array();
  var count = 0;

  for (var i = 0; i < getAllCheckboxes.length; i++) {
    if (getAllCheckboxes[i].type == "checkbox" && getAllCheckboxes[i].id.search("chkSelection") != -1) {
      if (getAllCheckboxes[i].checked) {
        ++count;
        accList[count - 1] = getAllCheckboxes[i].title;
      }
    }
  }
  return accList;
}

function removeSelectedAccList() {
  var getAllCheckboxes = document.getElementsByTagName("input");
  var accList = new Array();
  var count = 0;

  for (var i = 0; i < getAllCheckboxes.length; i++) {
    if (getAllCheckboxes[i].type == "checkbox" && getAllCheckboxes[i].id.search("chkSelection") != -1) {
      if (getAllCheckboxes[i].checked) {
        ++count;

        // title= accommodation code, name = startdate, value = duration
        accList[count - 1] = getAllCheckboxes[i].title + '@' + getAllCheckboxes[i].name + '@' + getAllCheckboxes[i].value;
      }
    }
  }
  return accList;
}

function compareAaccommodationList(id) {

  var getAllCheckboxes = document.getElementsByTagName("input");
  var link = document.getElementById(id);
  var accList = new Array();
  var count = 0;

  for (var i = 0; i < getAllCheckboxes.length; i++) {
    if (getAllCheckboxes[i].type == "checkbox" && getAllCheckboxes[i].id.search("chkSelection") != -1 && getAllCheckboxes[i].tabIndex == link.title) {
      if (getAllCheckboxes[i].checked) {
        ++count;
        accList[count - 1] = getAllCheckboxes[i].title;
      }
    }
  }
  return accList;
}

function comparePopupLink(id) {
  var accList = compareAaccommodationList(id);
  var queryString = accList.join("-");
  var link = document.getElementById(id);
  if (link != null) {
      link.href = "../../WebPages/MyFavourites/CompareMyFavourites.aspx?pCode=" + link.title + "&accCodes=" + queryString;
  }
}

function highlightSelectedCheckBox(accCode, checked) {
  var id = "cell" + accCode;
  if (checked) {
    document.getElementById(id).style.background = "#EEEEEE";
  }
  else {
    document.getElementById(id).style.background = "white";
  }
}