/*
	Quick Booking js functions
*/

// Promotional code show/hide
function showHidePromoBox () {
	
	if($("div#page div.promocode .promo_entry").css("display") == "none"){
		$("div#page div.promocode .promo_entry").show();
		$("div#page div.promocode a#showpromo").hide();
	}else{
		$("div#page div.promocode .promo_entry").slideUp(100);
	}
	
}

// Open click to talk popup
function openClicktoTalk() {
  window.open("clicktotalk.html","win", "height=498, width=439, toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no");
}

$(function() {
	
	// Insert hidden data field for reservation code into forms
	var reservation_code = '<input type="hidden" name="source" value="HWS">';
	$('form#qb_search').prepend(reservation_code);
	$('form#qb_existing').prepend(reservation_code);
	$('form#qb_booking').prepend(reservation_code);
	
	// set current date to arrival and departure within quick booking
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	if(month < 10) month = "0" + month;
	var day = currentTime.getDate();
	if(day < 10) day = "0" + day;
	var year = currentTime.getFullYear();
	
	$("input#search_from_date").attr("value", day + "/" + month + "/" + year);
	$("input#search_from_date-br").attr("value", day + "/" + month + "/" + year);
	
	var nextDate = new Date(currentTime.valueOf()+(24*60*60*1000)); // 24 hours ahead.
	var next_month = nextDate.getMonth() + 1;
	if(next_month < 10) next_month = "0" + next_month;
	var next_day = nextDate.getDate();
	if(next_day < 10) next_day = "0" + next_day;
	var next_year = nextDate.getFullYear();
	
	$("input#search_to_date").attr("value", next_day + "/" + next_month + "/" + next_year);
	$("input#search_to_date-br").attr("value", next_day + "/" + next_month + "/" + next_year);
	
	// Show promotion code selector
	$("div.promocode a").attr( "href", "javascript:showHidePromoBox()" );
	
	// Set promotional code type
	$("select#promo_select").change(function() {
		$("input#promo_input").attr( "name", $("select#promo_select").attr("value") );
	});
	
	// Trigger click to talk popup
	$("div#page div#clicktotalk a").attr( "href", "javascript:openClicktoTalk()" );
	
	// Initialize loading animation
	$("input#check-availability").click(function() {
	  if (formHelpers.validate()) {
	    //$("div.row", $("form#reservation")).hide();
  		//$("div.loading-booking", $("form#reservation")).show(); 
  		setTimeout(function() { 
  		  $("div.loading-booking img").attr("src", $("div.loading-booking img").attr("src"));
  		}, 100);
  		return true;
	  } else {
	    return false;
	  }
	});
	// Initialize loading animation
	$("input#booking-availability").click(function() {
	  if (formHelpers.validate()) {
	    //$("div.row", $("form#qb_booking")).hide();
  		//$("div#loader_booking").show(); 
  		setTimeout(function() { $('div#loader_booking img').attr("src", $('div#loader_booking img').attr("src")); }, 30);
  		return true;
	  } else {
	    return false;
	  }
	});
	// Initialize loading animation
	$("input#search-availability").click(function() {
	  if (formHelpers.validate()) {
	    //$("div.row", $("form#qb_search")).hide();
  		//$("div#loader_search").show(); 
		setTimeout(function() { $('div#loader_search img').attr("src", $('div#loader_search img').attr("src")); }, 30);
  		return true;
	  } else {
	    return false;
	  }
	});
	// Initialize loading animation
	$("input#find-existing").click(function() {
	  if (formHelpers.validate()) {
	    //$("div.row", $("form#qb_existing")).hide();
  		//$("div#loader_existing").show(); 
  		setTimeout(function() { 
  		  $("div#loader_existing img").attr("src", $("div#loader_existing img").attr("src"));
  		}, 100);
  		return true;
	  } else {
	    return false;
	  }
	});
	
	// Tab functionality
	$('div#quickbook ul#quickbook_nav li a').click(function(){
		var tabClass = $(this).attr('class');
		$('div#quickbook ul#quickbook_nav li.on').removeClass('on').addClass('off');
		$(this).parent().removeClass('off').addClass('on');
		$('div#quickbook').attr('class', tabClass);
		$('div#quickbook form').hide();
		$('form#'+tabClass).show();
		return false;
	});
	
	//Best Rate
	$('select#hotel_name_search').change(function(){
		$("form#qb_search").attr('action', 'https://www.mirvachotels.com/booking/calendar/choose_room');
	});
	$('select#hotel_region').change(function(){
		$("form#qb_search").attr('action', 'https://www.mirvachotels.com/booking/calendar/choose_hotel');
		if($('select#hotel_region option:selected').hasClass('state')){
			$('select#hotel_region').attr('name', 'state');
		}else{
			$('select#hotel_region').attr('name', 'region');
		}
	});

});

window.onunload = function(){};

$(document).ready(function() {
	$('form#qb_booking div#loader_booking').hide();
	$('form#qb_booking div.row').show();
});