﻿var ttTopPartContainer="<div id='tooltip' class='Room_Detail_Rollover_Container' style='display:block '>" +
							"<table border='0' cellspacing='0' cellpadding='0'>" +
								"<tr>" +
									"<td style='border-top:2px solid #006666; border-left:2px solid #006666; background-color:#F9F7E5;'><img src='images/spacer.gif' width='1' height='1' /></td>" +
									"<td style='border-top:2px solid #006666; border-right:2px solid #006666; background-color:#F9F7E5;'><img src='images/spacer.gif' width='1' height='1' /></td>" +
									"<td height='10' align='left' valign='top'><img src='images/Shadow_Top_Right.png' width='10' height='10' /></td>" +
								"</tr>" +
								"<tr>" +
									"<td style='border-left:2px solid #006666; border-bottom:2px solid #006666; background-color:#F9F7E5;'><img src='images/spacer.gif' width='1' height='1' /></td>" +
									"<td class='Room_Detail_Rollover'>";
var ttBottomPartContainer=			"</td>" +
									"<td width='10' style='background-image:url(images/Shadow_Right.png); background-repeat:repeat-y; background-position:left; '><img src='images/spacer.gif' width='1' height='1' /></td>" +
								"</tr>" +
								"<tr>" +
									"<td width='10' height='10' align='left' valign='top'><img src='images/Shadow_Bottom_Left.png' width='10' height='10' /></td>" +
									"<td height='10' style='background-image:url(images/Shadow_Bottom.png); background-repeat:repeat-x; background-position: top left; '><img src='images/spacer.gif' width='1' height='1' /></td>" +
									"<td width='10' height='10' align='left' valign='top'><img src='images/Shadow_Bottom_Right.png' width='10' height='10' /></td>" +
								"</tr>" +
							"</table>" +
						"</div>";									

this.imagePreview = function() {	
	var xOffset = 120;
	var yOffset = 12;

	$("a.HotelPreviewPhoto").hover(function(e){
		var anchor="a[name='" + this.name + "']";
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append(ttTopPartContainer +"<img src='"+ $(anchor).attr("image") +"' alt='Photo Preview' />"+ c + ttBottomPartContainer);								 
		
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#tooltip").remove();
    });	
	$("a.HotelPreviewPhoto").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};						
						
this.tooltipPreview = function(){	
		var xOffset = 4;
		var yOffset = 12;		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";
		var ttBody="";
		
		if (this.t.substring(0, 1) == "#") {
			ttBody=$(this.t).html();
		} else {
			ttBody=this.t;
		}

		$("body").append(ttTopPartContainer + ttBody + ttBottomPartContainer);
		
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	
};


function okToProceed() {
	var locName=jQuery.trim($("#Id_Loc").val());
	var idl=$("#id_loc").val();
	var hid=$("#hid").val();
	var hName=jQuery.trim($("#HotelName").val());
	var cidate=$("#CheckInDT").datepicker("getDate");
	var codate=$("#CheckOutDT").datepicker("getDate");	
	var guestNumber = parseInt($("#AdultNumber").val()) + parseInt($("#ChildNumber").val());

	if ((locName.length==0) && (hName.length>0) && (idl.length>0)) {
		$("#id_loc").val("");
	}
	
	if ((idl==null || idl=="")) {
		if ((hName==null || hName=="") && (hid==null || hid=="")) {
			if ($("#hgid").length==0) {
				alert("You must specify at least one of the following fields: 'City', 'Hotel Name'.");
				return false;
			}
		}
	}
	
	hName=hName.toLowerCase();
	
	if (!(hName==null || hName=="")) {
		if (hName=="hotel" || hName=="hotels" || 
		    hName=="apartment" || hName=="apartments" ||
			hName=="room" || hName=="rooms" ||
			hName=="villa" || hName=="villas" ||
			hName=="resort" || hName=="resorts") {
			
			alert("Ambiguous Hotel Name: '" + hName + "'. Please provider another name.");
			return false;
		}
	}
	
	if (cidate==null || codate==null) {
		alert("You must specify a date range before proceeding with the search.");
		return false;
	}

	if (cidate > codate) {
		alert("Check in date must not be greater than check out date.");
		return false;
	}
	//Calculate the difference between two dates, and convert to days
	var one_day=1000*60*60*24
	var numOfDays=Math.ceil((codate.getTime()-cidate.getTime())/(one_day));

	// Check in date and Last night must be between 1 and 30 days
	if (numOfDays<1 || numOfDays>30)  {
		alert("Booking period must be between 1 and 30 days after 'Check In Date' and no later than 360 days from today.");
		return false;
	}

	if (guestNumber>6) {
		alert("The maximum number of guests per booking (i.e. adults plus children) must be six(6).");
		return false;	
	}
		
	return true;	
}

function initSearchForm() {

	$("#Id_Loc").autocomplete(
	"/ws/ws_locsearch.asp",
	{
		max:100,
		width: 400,
		delay:200,
		cacheLength:0,
		autoFill:false
	});

	$('input#Id_Loc').search();
	$('input#Id_Loc').result(function(event, data, formatted) {	if (data) $("#id_loc").val(data[1]);});


	var mindate = new Date();
	var maxdate = new Date();
	mindate.setDate(mindate.getDate());
	maxdate.setDate(maxdate.getDate() + 360); //360 day stay maximum
	
	$("#CheckInDT,#CheckOutDT").datepicker({ 
		dateFormat: "dd/mm/yy",
		duration: "",
		showStatus: true, 
		showOn: "button", 
		buttonImage: "controls/calendar.gif", 
		buttonImageOnly: true,
		minDate: mindate, 
		maxDate: maxdate,		
		onSelect: customRange		
	});	
	
	$("#CheckInDT,#CheckOutDT").change(function() {
		try {
			var testDate=$.datepicker.parseDate('dd/mm/yy', this.value);
		}
		catch (error) {
			this.value="";
			return;
		}
		customRange(null, this);
	});
}

function customRange(dateText, input) {
	var cidate=$("#CheckInDT").datepicker("getDate");
	var codate=$("#CheckOutDT").datepicker("getDate");
	
	var mindate=new Date();
	var maxdate=new Date();
	mindate.setDate(mindate.getDate());
	maxdate.setDate(maxdate.getDate() + 360); //360 day stay maximum
	$("#CheckInDT,#CheckOutDT").datepicker('option', 'minDate', mindate);
	$("#CheckInDT,#CheckOutDT").datepicker('option', 'maxDate', maxdate);
	
	if (input.id == "CheckInDT") {
		if (cidate != null) {
			if (cidate >= maxdate) {
				$("#CheckOutDT").datepicker('setDate', null);
				return;
			}
			else {
				if (codate != null)
					if (cidate < codate) 
						return;
				
				cidate.setDate(cidate.getDate() + 1);
				$("#CheckOutDT").datepicker('setDate', cidate);	
			}
		}
	}

	if (input.id == "CheckOutDT") {
		if (codate != null) {
			if (codate <= mindate) {
				$("#CheckInDT").datepicker('setDate', null);
				return;
			}
			else {
				if (cidate != null)
					if (codate > cidate) 
						return;
				
				codate.setDate(codate.getDate() - 1);
				$("#CheckInDT").datepicker('setDate', codate);	
			}
		}
	}
} 

