var availStartDate; 
var startId;
var adults = 0;
var children = 0;
var infants = 0;
var numpets = 0;
var shortBreakAllowed = "false";
var SBproceed = "";
var firstLoad = true;
var globalcaldate = "";



//--------------------------------------------------
//
// Availability chart
//
//--------------------------------------------------

function getAvailabilityCalendar(caldate, propref, style, noOnClick, ownerBooking) {

	// Clear fields
	$("#holiday_price").html("");
	$("#startdatehid").val("");
	$("#todatehid").val("");
	$("#startdate").html("");
	$("#todate").html("");
	
	if(!style) {
		style = "normal";
	}
	
	if(!noOnClick) {
		noOnClick = "true";
	}
	
	if(!ownerBooking) {
		ownerBooking = "false";
	}
	
	// Load up the global
	if(firstLoad) {
		globalcaldate = caldate;
		firstLoad = false;
	}
	
	if(style != "normal") {
		var url="/includes/getSmallAvailabilityCalendar.php";
	}
	else {
		var url="/includes/getAvailabilityCalendar.php";
	}
	
	$.get(url, {caldate: caldate, propref: propref, style: style, click: noOnClick, ownerbooking: ownerBooking}, function(availData) {		
		$("#availability_main").html(availData);		
		// Check to see if its the first load and automatically click if there is a date which is
		// greater than today.  This can only happen if the date is a session variable.
		var today = new Date();
		var calTmp = globalcaldate.split("-");
		var calDay = new Date (calTmp[2],(calTmp[1]-1),calTmp[0]);
		
		if(calDay > today) {
			var id = calDay.getDate()+"-"+(calDay.getMonth())+"-"+calDay.getFullYear();
			clickSeven(id);
			firstLoad = false;
		}
	});
}

function getPrice(startDate, endDate, propRef, people, brandcode) { 
	$.get("/includes/getPrice.php", {startDate: startDate, endDate: endDate, propRef: propRef, people: people, brandcode: brandcode}, function(priceData) {
		if(priceData == "Please Call" || priceData == "Please select a changeover day") {
			$("#holiday_price").html(priceData);
		}
		else {
			$("#holiday_price").html("&pound;" + priceData);
		}
	});
}

function getAdults() {
	if($("#adults").val() != "") {
		return $("#adults").val();
	}
	else {
		return 0;
	}
}

function getChildren() {
	if($("#children").val() != "") {
		return $("#children").val();
	}
	else {
		return 0;
	}
}

function getInfants() {
	if($("#infants").val() != "") {
		return $("#infants").val();
	}
	else {
		return 0;
	}
}

function changeHolLength() {
	var len = $("#holLength").val();
	clearHighlights();
	var one_day=1000*60*60*24;
	var endDate;
	var tempDay;
	var cancel = false;
	
	adults = getAdults();
	children = getChildren();
	infants = getInfants();
	var numpeople = parseInt(adults) + parseInt(children);
	numpeople = ((numpeople == 0) ? 100 : numpeople);
	
	for(var i=0; i<=len; i++) {
		try {
			tempDay = new Date ((availStartDate.getFullYear()),(availStartDate.getMonth()),availStartDate.getDate()+i);
			var id = tempDay.getDate()+"-"+(tempDay.getMonth())+"-"+tempDay.getFullYear();
			endDate = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
		}
		catch(e) {}
				
		try {	
			if(i < len) {
				if($("#" + id).hasClass("avaTd") || $("#" + id).hasClass("cdAvaTd")) {
					if(i == 0) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightStart");
					}
					else if(i == len) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightEnd");
					}
					else {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLight");
					}
				}
				else {
					if($("#" + id).hasClass("notAvaTd")) {
						$("#holiday_price").html("Please Call");
						cancel = true;
						alert('The number of days that you requested is greater than the number of days available.');
						break;
					}
				}
			}
			else {
				break;
			}
		}
		catch(err) {}
	}
	
	try {
		if(!cancel) {
			var thisDayTemp = availStartDate.getDate()+"-"+(availStartDate.getMonth()+1)+"-"+availStartDate.getFullYear();
			var endDayTemp = endDate.getDate()+"-"+(endDate.getMonth()+1)+"-"+endDate.getFullYear();
			tempDayEnd = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());	
			$("#startdate").html((availStartDate.getDate())+"/"+(availStartDate.getMonth()+1)+"/"+availStartDate.getFullYear());
			$("#todate").html((tempDayEnd.getDate())+"/"+(tempDayEnd.getMonth()+1)+"/"+tempDayEnd.getFullYear());		
			$("#startdatehid").val(thisDayTemp);
			$("#todatehid").val(endDayTemp);	
			getPrice(thisDayTemp, endDayTemp, $("#property").val(), numpeople, $("#brandcode").val());
		}
	}
	catch(err) {}
}

function clickSeven(id) {

	var arrTmp = id.split("-");
	var one_day=1000*60*60*24;
	var len = $("#holLength").val();
	var thisDay = new Date (arrTmp[2],arrTmp[1],arrTmp[0]);
	availStartDate = thisDay;
	startId = id;
	var thisDayPlus = new Date (thisDay.getTime() + (one_day*len));
	var endDate;
	var tempDay;
	var tempDayEnd;
	var cancel = false;
	adults = getAdults();
	children = getChildren();
	infants = getInfants();
	var numpeople = parseInt(adults) + parseInt(children);	
	numpeople = ((numpeople == 0) ? 100 : numpeople);
		
	clearHighlights();

	for(var i=0; i<=len; i++) {
		tempDay = new Date ((availStartDate.getFullYear()),(availStartDate.getMonth()),availStartDate.getDate()+i);
		var id = tempDay.getDate()+"-"+(tempDay.getMonth())+"-"+tempDay.getFullYear();
		endDate = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
		
		try {
			if(i < len) {
				if($("#" + id).hasClass("avaTd") || $("#" + id).hasClass("cdAvaTd")) {
					if(i == 0) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightStart");
					}
					else if(i == len) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightEnd");
					}
					else {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLight");
					}
				}
				else {
					if($("#" + id).hasClass("notAvaTd")) {
						$("#holiday_price").html("Please Call");
						cancel = true;
						alert('The number of days that you requested is greater than the number of days available.');
						break;
					}
				}
			}
			else {
				break;
			}
		}
		catch(err) {}
	}
	
	if(!cancel) {
		var thisDayTemp = thisDay.getDate()+"-"+(thisDay.getMonth()+1)+"-"+thisDay.getFullYear();
		var endDayTemp = endDate.getDate()+"-"+(endDate.getMonth()+1)+"-"+endDate.getFullYear();
		tempDayEnd = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
		$("#startdate").html((availStartDate.getDate())+"/"+(availStartDate.getMonth()+1)+"/"+availStartDate.getFullYear());
		$("#todate").html((tempDayEnd.getDate())+"/"+(tempDayEnd.getMonth()+1)+"/"+tempDayEnd.getFullYear());		
		$("#startdatehid").val(thisDayTemp);
		$("#todatehid").val(endDayTemp);
		getPrice(thisDayTemp, endDayTemp,$("#property").val(), numpeople, $("#brandcode").val());
	}
}

function doOwnerBooking(id) {

	var arrTmp = id.split("-");
	var one_day=1000*60*60*24;
	var len = $("#holLength").val();
	var thisDay = new Date (arrTmp[2],arrTmp[1],arrTmp[0]);
	availStartDate = thisDay;
	startId = id;
	var thisDayPlus = new Date (thisDay.getTime() + (one_day*len));
	var endDate;
	var tempDay;
	var tempDayEnd;
	var cancel = false;
		
	clearHighlights();
	
	for(var i=0; i<=len; i++) {
		tempDay = new Date ((availStartDate.getFullYear()),(availStartDate.getMonth()),availStartDate.getDate()+i);
		var id = tempDay.getDate()+"-"+(tempDay.getMonth())+"-"+tempDay.getFullYear();
		endDate = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
		
		try {
			if(i < len) {
				if($("#" + id).hasClass("avaTd") || $("#" + id).hasClass("cdAvaTd")) {
					if(i == 0) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightStart");
					}
					else if(i == len) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightEnd");
					}
					else {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLight");
					}
				}
				else {
					if($("#" + id).hasClass("notAvaTd")) {
						cancel = true;
						alert('The number of days that you requested is greater than the number of days available.');
						break;
					}
				}
			}
			else {
				break;
			}
		}
		catch(err) {}
	}
	
	if(!cancel) {
		var thisDayTemp = thisDay.getDate()+"-"+(thisDay.getMonth()+1)+"-"+thisDay.getFullYear();
		var endDayTemp = endDate.getDate()+"-"+(endDate.getMonth()+1)+"-"+endDate.getFullYear();
		tempDayEnd = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
		$("#startdate").html((availStartDate.getDate())+"/"+(availStartDate.getMonth()+1)+"/"+availStartDate.getFullYear());
		$("#enddate").html((tempDayEnd.getDate())+"/"+(tempDayEnd.getMonth()+1)+"/"+tempDayEnd.getFullYear());		
		$("#fromdate").val(thisDayTemp);
		$("#todate").val(endDayTemp);
	}
}

function changeHolLengthOwner() {
	var len = $("#holLength").val();
	clearHighlights();
	var one_day=1000*60*60*24;
	var endDate;
	var tempDay;
	var cancel = false;
	
	for(var i=0; i<=len; i++) {
		try {
			tempDay = new Date ((availStartDate.getFullYear()),(availStartDate.getMonth()),availStartDate.getDate()+i);
			var id = tempDay.getDate()+"-"+(tempDay.getMonth())+"-"+tempDay.getFullYear();
			endDate = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());
		}
		catch(e) {}
				
		try {	
			if(i < len) {
				if($("#" + id).hasClass("avaTd") || $("#" + id).hasClass("cdAvaTd")) {
					if(i == 0) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightStart");
					}
					else if(i == len) {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLightEnd");
					}
					else {
						$("#" + id).addClass("avaHighLight").addClass("avaHighLight");
					}
				}
				else {
					if($("#" + id).hasClass("notAvaTd")) {
						cancel = true;
						alert('The number of days that you requested is greater than the number of days available.');
						break;
					}
				}
			}
			else {
				break;
			}
		}
		catch(err) {}
	}
	
	try {
		if(!cancel) {
			var thisDayTemp = availStartDate.getDate()+"-"+(availStartDate.getMonth()+1)+"-"+availStartDate.getFullYear();
			var endDayTemp = endDate.getDate()+"-"+(endDate.getMonth()+1)+"-"+endDate.getFullYear();
			tempDayEnd = new Date(tempDay.getFullYear(),tempDay.getMonth(),tempDay.getDate());	
			$("#startdate").html((availStartDate.getDate())+"/"+(availStartDate.getMonth()+1)+"/"+availStartDate.getFullYear());
			$("#enddate").html((tempDayEnd.getDate())+"/"+(tempDayEnd.getMonth()+1)+"/"+tempDayEnd.getFullYear());		
			$("#fromdate").val(thisDayTemp);
			$("#todate").val(endDayTemp);
		}
	}
	catch(err) {alert(err);}
}

function clearHighlights() {
	$("td").each(function() {
		$(this).removeClass("avaHighLight").removeClass("avaHighLightStart");
	});
}

function checkParty() {
	var adults = $("#adults").val();
	var children = $("#children").val();
	var maxpeople = $("#maxpeople").val();
	var people = 0;	
	try {	
		people = parseInt(adults) + parseInt(children);		
		if(adults == 0) {
			return 3;
		}
		
		if(people > 0) {
			if(maxpeople >= people) {
				return 1;
			}
			else {
				return 0;
			}
		}
		else {
			return 2;
		}
	}
	catch(e) {
		return 0;
	}
}

function checkAvailability(day) {
	if(day != "") {
		// Set available variable
		var available = false;
		
		// Clear out old content
		$("#availabledates").html("<p>Loading availability...</p>").fadeIn(500);
		
		// Get the available calendars on the page
		var availabilityObj = $(".availabilitytable tbody tr td." + day + ":not(.notAvaTd)");
		availabilityObj.each(function() {
			var thisId = $(this).attr("id");
			var arrTmp = thisId.split("-");
			var one_day=1000*60*60*24;
			var len = $("#holLength").val();
			var thisDay = new Date (arrTmp[2],arrTmp[1],arrTmp[0]);
			var thisDayPlus = new Date (thisDay.getTime() + (one_day*len));		
			var startid = thisDay.getDate()+"-"+(thisDay.getMonth()+1)+"-"+thisDay.getFullYear();
			var endid = thisDayPlus.getDate()+"-"+(thisDayPlus.getMonth()+1)+"-"+thisDayPlus.getFullYear();
			var propref = $("#property").val();
			$.ajax({
					url: "/includes/custom/shortBreakCheck.php",
					type: "GET",
					cache: false,
					async: false,
					data: {startdate: startid, todate: endid, propref: propref},
					dataType: "text",
					success: function(msg){
						if(msg == "") {
							if(!available) { 
								$("#availabledates").html("");
							}
							$("#availabledates").append("<a href=\"#\" onclick=\"clickSeven('"+thisId+"'); return false;\">Starting "+thisDay.getDate()+"/"+(thisDay.getMonth()+1)+"/"+thisDay.getFullYear()+"</a><br />");
							available = true;
						}	
					}
				});
			$("#availabledates").fadeOut(500, function() {
				if(available) {
					$("#availabledates").fadeIn(500);
				}
			});
			
		});
	}
}

function CheckAvailForm() {
	if($("#startdatehid").val() == "") {
		alert("Please click on the calendar to select your holiday dates.");
		return false;
	}
	
	try {
		var propref = $("#property").val();
		var sbCheck = "";
		bodyContent = $.ajax({
				url: "/includes/custom/shortBreakCheck.php",
				type: "GET",
				cache: false,
				async: false,
				data: {startdate: $("#startdatehid").val(), todate: $("#todatehid").val(), propref: propref},
				dataType: "text",
				success: function(msg){
					sbCheck = msg;
				}
			}).responseText;
			
		if(sbCheck != "") {
			alert(sbCheck);
			return false;
		}
			
	}
	catch(e) {
		alert(e + " An error occurred taking your booking, please call us and we will be happy to help.");
		return false;
	}
	
	if($("#holiday_price").html() == "Please Call" || $("#holiday_price").html() == "Please select a changeover day" || $("#holiday_price").html() == "" || $("#holiday_price").html() == "Unavailable") {
		alert("Sorry, the property is not available for those dates.");
		return false;
	}
	
	var people = checkParty();
	
	if(people == 0) {
		alert("The number in your party exceeds the amount allowed on the property.");
		return false;
	}
	else if(people == 2) {
		alert("Please select how many people are coming to the property.");
		return false;
	}
	else if(people == 3) {
		alert("Please select how many adults are coming to the property.");
		return false;
	}
}
