﻿String.prototype.replaceAt=function(index, char) {
  return this.substr(0, index) + char + this.substr(index+char.length);
}

$(document).ready(function(){
  resizebleMenu();
  $('#my-dropdown').sSelect();
  $("div.selectedTxt").text(selectYourLanguage);
  $('#topgraphic').cycle();

  if($(".lightbox").length >= 1)
  onLoadLightbox();

});
 
function onLoadLightbox(){
  $(".lightbox").lightBox({
        overlayBgColor: '#000000',
	overlayOpacity: 0.7,
	imageLoading: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-ico-loading.gif',
	imageBtnClose: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-btn-close.gif',
	imageBtnPrev: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-btn-prev.gif', 
	imageBtnNext: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-btn-next.gif'
});
}

/* Preço por Noite */
function adjustPricePerNight(newPrice, unitID) {
  var PricePerNight = 0;

  newPrice = newPrice.substring(0,newPrice.length-1);
  newPrice = newPrice.replace(/\./g, ",");
  newPrice = newPrice.replaceAt(newPrice.lastIndexOf(","), ".");
  newPrice = newPrice.replace(/\,/g, "");
  PricePerNight = newPrice/PostedNumberOfDays;
  $("#PriceNight" + unitID).html(PricePerNight.toFixed(2) + "€");
}

/* resets the general search box text to its default value */
function checkIfEmpty( input, defaultValue ) {
 if( ( $('#'+input).val() == "" ) || $('#'+input).val().toLowerCase() == defaultValue.toLowerCase() )
 $('#'+input).val( defaultValue );
}

/* clears the general search box text */
function clearField( input, defaultValue ) {
  if( ( $('#'+input).val() == "" ) || ( $('#'+input).val().toLowerCase() == defaultValue.toLowerCase() ) ) {
    $('#'+input).val("");
  }
}
function createGallery(path, destination) {
  path = path.substring(0,path.lastIndexOf("/"));
  $.ajax({
      url: location.protocol + "//" + location.hostname + "/Files/GetFolderContents.aspx?Path=" + path,
      success: function(data){
        var xml,
            allImgSrc = '';
        if ($.browser.msie && typeof data == "string") {
          xml = new ActiveXObject("Microsoft.XMLDOM");
          xml.async = false;
          xml.loadXML(data);                
        } else {
           xml = data;
        }
        
        $(xml).find("File").each(function() {
          allImgSrc += '<img src="/admin/public/getimage.aspx?Image='+$(this).attr('href')+'&width=500&height535=&colorspace=rgb&format=jpg"/>';
        });
        $('#' + destination).html(allImgSrc).cycle({
          fx: 'fade' ,
          pause: 1
        });
      }
  });
}

function createGalleryList(path, destination) {
  path = path.substring(0,path.lastIndexOf("/"));
  $.ajax({
      url: location.protocol + "//" + location.hostname + "/Files/GetFolderContents.aspx?Path=" + path,
      success: function(data){
        var xml,
            allImgSrc = '';
        if ($.browser.msie && typeof data == "string") {
          xml = new ActiveXObject("Microsoft.XMLDOM");
          xml.async = false;
          xml.loadXML(data);                
        } else {
           xml = data;
        }

        $(xml).find("File").each(function() {
          allImgSrc += '<a href="/admin/public/getimage.aspx?Image='+$(this).attr('href')+'&height=450&colorspace=rgb&format=jpg"><img src="/admin/public/getimage.aspx?Image='+$(this).attr('href')+'&height=80&colorspace=rgb&format=jpg" alt="' + $(this).attr("title") + '"/></a>';
        });
        $('#' + destination).html(allImgSrc);
        $("#" + destination + " a").lightBox({
	overlayBgColor: '#000000',
	overlayOpacity: 0.7,
	imageLoading: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-ico-loading.gif',
	imageBtnClose: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-btn-close.gif',
	imageBtnPrev: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-btn-prev.gif', 
	imageBtnNext: '/Files/Templates/Designs/QtaFurao2010/graficos/lightbox-btn-next.gif',
	txtImage: 'Imagem',
	txtOf: 'de'
        });
      }
  });
}



function ValidateSearch( errorMessage, defaultValue ) {
    if( ( $(".footerNewsletter").val() == '' ) || ( $(".footerNewsletter").val().toLowerCase() == defaultValue.toLowerCase() ) ) {
      alert( errorMessage );
      return false;
    }
}
  //Mainmenu

function resizebleMenu() {
	var totalWidth = $("#mainmenu").width(),
		numItens = $("#mainmenu.pageLevel1 > li").length,
		freeSpace = 0, spacing = 0, totalLiWidth = 0;
	$( "#mainmenu.pageLevel1 > li a").each( function() {
		totalLiWidth += $( this ).width();
	});
	freeSpace = totalWidth - totalLiWidth;
    spacing = freeSpace / numItens / 2;
    $("#mainmenu.pageLevel1 > li a").each(function() {
      $(this).css({
          'padding-left' : spacing + 'px',
          'padding-right' : spacing + 'px',
          'padding-top' : '5px',
          'padding-bottom' : '5px'
      });

    });
}

function SubmitQuickBook() {
  document.getElementById('HotelBookingSearchFormNumberOfDays').value = document.getElementById('NumberOfDays').value;
  document.getElementById('HotelBookingSearchFormCheckinDate').value = document.getElementById('HotelBookingSearchFormDay').value + "-" + document.getElementById('HotelBookingSearchFormMonthYear').value;
  
  if (d.getDate()>(document.getElementById('HotelBookingSearchFormDay').selectedIndex+1) && document.getElementById('HotelBookingSearchFormMonthYear').selectedIndex == 0) {
    alert(errorInvalidDate);
    return false;
  }
  else { return true; }
}

function UpdateNumberOfDays() {
  $("#HotelBookingSearchFormNumberOfDays").val($("#NumberOfDays").val());
}

/* eBokking Step5 - Card Data - Validator*/

  var phoneNumberDelimiters = "",
      validWorldPhoneChars = phoneNumberDelimiters + "",
      optYears = "";

  function loadCreditCardYears() {
    var d = new Date(),
        y = d.getFullYear();
     
    for(i = y; i < (y + 8); i++) {
      optYears += "<option value='" + i + "'>" + i + "</option>";
    }
    $("#cardexp_year").html(optYears);
  }
  
  function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    
    for (i = 0; i < s.length; i++) {   
      var c = s.charAt(i);
      if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
  }
  
  function checkCardNumber(strPhone) {
    var minDigitsInIPhoneNumber = 16;
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    return (!isNaN(s) && s.length >= minDigitsInIPhoneNumber);
  }
  
  function checkSecurityCode(strPhone) {
    var minDigitsInIPhoneNumber = 3;
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    return (!isNaN(s) && s.length >= minDigitsInIPhoneNumber);
  }

  function validateCreditCardDetails() {
    checkedform = true;
    var errorMessage = "";

    if (checkCardNumber(document.getElementById("cardpan").value) == false) {
      document.HotelBooking.cardpan.setAttribute("class", "redBorder");
      document.HotelBooking.cardpan.setAttribute("className", "redBorder");
      errorMessage += invalidCardNumber +' <br />';
      checkedform = false;
    }
    else {
      document.HotelBooking.cardpan.setAttribute("class", "");
      document.HotelBooking.cardpan.setAttribute("className", "");
    }

    if (checkSecurityCode(document.getElementById("cardcode").value) == false) {
      document.HotelBooking.cardcode.setAttribute("class", "redBorder");
      document.HotelBooking.cardcode.setAttribute("className", "redBorder");
      errorMessage += invalidSecurityCode + ' <br />';
      checkedform = false;
    }
    else {
      document.HotelBooking.cardcode.setAttribute("class", "");
      document.HotelBooking.cardcode.setAttribute("className", "");
    }
  
    if (document.getElementById("cardholder").value == "") {
      document.HotelBooking.cardholder.setAttribute("class", "redBorder");
      document.HotelBooking.cardholder.setAttribute("className", "redBorder");
      errorMessage += invalidCardHolderName + ' <br />';
      checkedform = false;
    }
    else {
      document.HotelBooking.cardholder.setAttribute("class", "");
      document.HotelBooking.cardholder.setAttribute("className", "");
    }
  
    var actualDate = new Date(),
        actualYear = actualDate.getFullYear(),
        actualMonth = actualDate.getMonth(),
        selectedYear = parseInt( document.HotelBooking.cardexp_year.value )
    if( selectedYear >= actualYear ) {
      if( selectedYear == actualYear ) {
        if( actualMonth + 1 > parseInt( document.HotelBooking.cardexp_month.value ) ) {
          errorMessage += invalidCardDate + '> <br />';
          incorrectCardDate();
          checkedform = false;
        }
        else
          correctCardDate();
      }
      else
        correctCardDate();
    }
    else {
      errorMessage += invalidCardDate + '> <br />';
      incorrectCardDate();
      checkedform = false;
    }

    if (checkedform) {
      document.HotelBooking.action = "http://" + location.host + "/Gateways/HotelBookingPaymentPage.aspx";
      document.HotelBooking.submit();
    }
    else {
      $("#invalidinputs").html(errorMessage);
    }
    return false;
  }

function incorrectCardDate() {
  document.HotelBooking.cardexp_year.setAttribute("class", "redBorder");
  document.HotelBooking.cardexp_year.setAttribute("className", "redBorder");
  document.HotelBooking.cardexp_month.setAttribute("class", "redBorder");
  document.HotelBooking.cardexp_month.setAttribute("className", "redBorder");
}

function correctCardDate() {
  document.HotelBooking.cardexp_year.setAttribute("class", "");
  document.HotelBooking.cardexp_year.setAttribute("className", "");
  document.HotelBooking.cardexp_month.setAttribute("class", "");
  document.HotelBooking.cardexp_month.setAttribute("className", "");
}


