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 = "";

function countJson(obj) {
	var prop;
	var count = 0;
	for (prop in obj) {
		count++;
	}
	return count;
}

function getPrice(startDate, endDate, propRef, people, brandcode, numberofpets) {
	jQuery("#booking-details-form .property-box-content").append("<div class='pricing-error'><span>Finding your holiday price, please wait...</span></div>");
	jQuery.getJSON("/includes/getPrice.php", {startDate: startDate, endDate: endDate, propRef: propRef, people: people, brandcode: brandcode, numpets: numberofpets, response: 'json'}, function(results) {
		if(countJson(results) > 0) {
			if(results.status == "ok") {
				jQuery(".pricing-error").remove();
				jQuery("#basic_price").html("&pound;" + results.price);
				if(parseInt(results.petfee) > 0) {
					jQuery("#extras").html("&pound;" + results.bookingfee + " + " + "&pound;" + results.petfee + " (Pet Charge)");
				}
				else {
					jQuery("#extras").html("&pound;" + results.bookingfee);
				}
				jQuery("#total_price").html("&pound;" + results.totalprice);
			}
			else {
				if(results.message.length > 100) {
					jQuery("#booking-details-form .property-box-content").append("<div class='pricing-error'><span style='top: 20px;'>" + results.message + "</span></div>");
				}
				else {
					jQuery("#booking-details-form .property-box-content").append("<div class='pricing-error'><span>" + results.message + "</span></div>");
				}
			}
		}
		else {
			jQuery("#booking-details-form .property-box-content").append("<div class='pricing-error'><span>Sorry, we couldn't find a price for the period you selected.  Please call us a price.</span></div>");
		}
	});
}

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;
	}
}
	
jQuery("#holLength,#adults,#children,#infants,#numpets").change(function() {
	changeHolLength();
});

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(), $("#numpets").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());
		jQuery("#startdate").html((availStartDate.getDate())+"/"+(availStartDate.getMonth()+1)+"/"+availStartDate.getFullYear());
		jQuery("#todate").html((tempDayEnd.getDate())+"/"+(tempDayEnd.getMonth()+1)+"/"+tempDayEnd.getFullYear());		
		jQuery("#startdatehid").val(thisDayTemp);
		jQuery("#todatehid").val(endDayTemp);
		getPrice(thisDayTemp, endDayTemp,$("#property").val(), numpeople, $("#brandcode").val(), $("#numpets").val());
	}
}

function clearHighlights() {
	jQuery("td").each(function() {
		jQuery(this).removeClass("avaHighLight").removeClass("avaHighLightStart");
	});
}

function checkParty() {
	var adults = jQuery("#adults").val();
	var children = jQuery("#children").val();
	var maxpeople = jQuery("#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;
	}
}

jQuery(document).ready(function() {
	
	/**
	* Availability buttons
	*/
	jQuery("#next-cal-months a,#prev-cal-months a").live("click", function() {
		var link = jQuery(this).attr("href");
		if(link.length > 2) {
			jQuery("#avail-cal-container").append("<div class='avail-loading'></div>");
			jQuery("#avail-cal-container").load(link + " #avail-cal-contents", function() {
				jQuery(".avail-loading").fadeOut(200).remove();
				if(jQuery("#startdatehid").val() != "") {
					var arrTmp = jQuery("#startdatehid").val().split("-");
					var tempId = arrTmp[0] + "-" + (parseInt(arrTmp[1])-1) + "-" + arrTmp[2];
					if(jQuery("#" + tempId).size() > 0) {
						clickSeven(tempId);
					}
				}
			});
		}
		return false;
	});

	jQuery("#adults option:first,#children option:first,#infants option:first,#numpets option:first").attr('selected', 'selected');
	jQuery("#holiday_price,#startdate,#todate").html(""); 
	jQuery("#startdatehid,#todatehid").val("");	
	jQuery("#availform .book-button").click(function() {
		if($("#startdatehid").val() == "") {
			alert("Please click on the calendar to select your holiday 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;
		}
		
		try {
		
			var len = jQuery("#holLength").val();
			var startDate = jQuery("#startdatehid").val();
			var endDate = jQuery("#todatehid").val();
			var propRef = jQuery("#property").val();
			var numpeople = parseInt(adults) + parseInt(children);
			numpeople = ((numpeople == 0) ? 100 : numpeople);
			var brandcode = jQuery("#brandcode").val();
			var numberofpets = jQuery("#numpets").val();
		
			jQuery.getJSON("/includes/getPrice.php", {startDate: startDate, endDate: endDate, propRef: propRef, people: people, brandcode: brandcode, numpets: numberofpets, response: 'json'}, function(results) {
				if(countJson(results) > 0) {
					if(results.status == "ok") {
						jQuery("#holiday_price").html("&pound;" + results.message);
						jQuery("#availform").submit();
					}
					else {
						jQuery("#holiday_price").html(results.message);
						alert(results.message);
						return false;
					}
				}
				else {
					jQuery("#holiday_price").html("Error getting price");
					return false;
				}
			});
			return false;
		}
		catch(e) {
			return false;
		}
	});
});

