var hml_years = new Array();
var accom_dates = new Array();
var current_month_selected;
var current_year_selected;
var low_season_list = "Y";
var includesBreakage = true;

function hml_year (year, low, mid, high, old_low, old_mid, old_high) {
	this.year = year;
	this.low = low;
	this.mid = mid;
	this.high = high;
	this.old_low = old_low;
	this.old_mid = old_mid;
	this.old_high = old_high;
}

function accom_date (id, res_date, price, old_price, promotion, rental_type_id) {
	this.id = id;
	this.res_date = res_date;
	this.price = price;
	this.old_price = old_price;
	this.promotion = promotion;		
	this.rental_type_id = rental_type_id
}

function hml_switch_year (p_year, channel_id) {			
	hml_year_selected = p_year;

	// sets up hml title
	if(document.getElementById("year_name_text")){
		document.getElementById("year_name_text").innerHTML = p_year + "&nbsp;";
	}
	
	// sets up hml year links
	var year_link_html = "";
	var year_position = -1;
	for (var i = 0; i < hml_years.length; i++) {
		if (hml_years[i].year == p_year) {
			year_link_html += hml_years[i].year;
			year_position = i;
		}
		else {
			year_link_html += "<a href='JavaScript:hml_switch_year(" + hml_years[i].year + ", " + channel_id + ");'>" + hml_years[i].year + "</a>";
		}
		if (i < hml_years.length - 1) {
			year_link_html += "&nbsp;|&nbsp;";
		}
	}
	year_link_html = lblHMLPricesFor + "&nbsp;" + year_link_html;
	document.getElementById("hml_year_links").innerHTML = year_link_html;
	
	// sets up hml season blocks
	if (low_season_list != "") {
		document.getElementById("hml_low_prices").style.display = "none";
		document.getElementById("hml_low_fully_booked").style.display = "none";
	}
	if (mid_season_list != "") {
		document.getElementById("hml_mid_prices").style.display = "none";
		document.getElementById("hml_mid_fully_booked").style.display = "none";
	}
	if (high_season_list != "") {
		document.getElementById("hml_high_prices").style.display = "none";
		document.getElementById("hml_high_fully_booked").style.display = "none";
	}
	
	if (year_position == -1) {
		if (low_season_list != "") {	
			document.getElementById("hml_low_fully_booked").style.display = "block";
		}
		if (mid_season_list != "") {
			document.getElementById("hml_mid_fully_booked").style.display = "block";
		}
		if (high_season_list != "") {
			document.getElementById("hml_high_fully_booked").style.display = "block";
		}
	}
	else {
		if (low_season_list != "") {
			if (hml_years[year_position].low == "" || hml_years[year_position].low < 50) {
				document.getElementById("hml_low_fully_booked").style.display = "block";
			}
			else {
				document.getElementById("hml_low_price").innerHTML = hml_years[year_position].low;
				if (hml_years[year_position].old_low != "" && hml_years[year_position].old_low > hml_years[year_position].low) {
					if(channel_id == 29){
						document.getElementById("hml_low_old_price").innerHTML = 'was <span style="text-decoration: line-through">'+hml_years[year_position].old_low+'</span>';
					} else {
						document.getElementById("hml_low_old_price").innerHTML = hml_years[year_position].old_low;
						document.getElementById("hml_low_price").innerHTML = "&nbsp;" + document.getElementById("hml_low_price").innerHTML;
					}
				}
				else {
					document.getElementById("hml_low_old_price").innerHTML = "";
				}
				document.getElementById("hml_low_prices").style.display = "block";					
			}	
		}
		if (mid_season_list != "") {
			if (hml_years[year_position].mid == "" || hml_years[year_position].mid < 50) {
				document.getElementById("hml_mid_fully_booked").style.display = "block";
			}
			else {
				document.getElementById("hml_mid_price").innerHTML = hml_years[year_position].mid;
				if (hml_years[year_position].old_mid != "" && hml_years[year_position].old_mid > hml_years[year_position].mid) {
					if(channel_id == 29){
						document.getElementById("hml_mid_old_price").innerHTML = 'was <span style="text-decoration: line-through">'+hml_years[year_position].old_mid+'</span>';
					} else {
						document.getElementById("hml_mid_old_price").innerHTML = hml_years[year_position].old_mid;
						document.getElementById("hml_mid_price").innerHTML = "&nbsp;" + document.getElementById("hml_mid_price").innerHTML;
					}
				}
				else {
					document.getElementById("hml_mid_old_price").innerHTML = "";
				}
				document.getElementById("hml_mid_prices").style.display = "block";					
			}
		}
		if (high_season_list != "") {
			if (hml_years[year_position].high == "" || hml_years[year_position].high < 50) {
				document.getElementById("hml_high_fully_booked").style.display = "block";
			}
			else {
				document.getElementById("hml_high_price").innerHTML = hml_years[year_position].high;
				if (hml_years[year_position].old_high != "" && hml_years[year_position].old_high > hml_years[year_position].high) {
					if(channel_id == 29){
						document.getElementById("hml_high_old_price").innerHTML = 'was <span style="text-decoration: line-through">'+hml_years[year_position].old_high+'</span>';
					} else {
						document.getElementById("hml_high_old_price").innerHTML = hml_years[year_position].old_high;
						document.getElementById("hml_high_price").innerHTML = "&nbsp;" + document.getElementById("hml_high_price").innerHTML;
					}
				}
				else {
					document.getElementById("hml_high_old_price").innerHTML = "";
				}
				document.getElementById("hml_high_prices").style.display = "block";					
			}
		}
	}			
}
var alreadyShowPricing = false;
function setup_pricing (p_year) {
	if (!alreadyShowPricing) {
		updateForm();
		$('#bookWeekDiv').appendTo("body");
	}
	alreadyShowPricing = true;
}

function number_format (x) {
	var num = parseFloat(x);
	return (x < 10) ? "0" + x : x;
}

function addPromo(id, webShortDescription) {
	for (var i=0;i<promos.length;i++) {
		if (promos[i].id == id) {
			return false;
		}
	}
	promos[promos.length] = this;
	this.id = id;
	this.webShortDescription = webShortDescription;
}

function addAvail(dt, nights, paxa, paxc, paxi, searchAirport, aowas, aois, wfwas, wfis, airport, aopromoids, wfpromoids, cacheId, arrairid,depairid,minresdate,maxresdate) {
	var pax = paxa+","+paxc+","+paxi;
	var myDataSearch = availData[nights + "_" + pax + "_" + searchAirport];
	if (!myDataSearch) {
		myDataSearch = new Array();
		availData[nights + "_" + pax + "_" + searchAirport] = myDataSearch;
	}

	this.resDate = new Date(dt);
	this.y = this.resDate.getFullYear();
	this.m = this.resDate.getMonth()+1;
	this.d = this.resDate.getDate();

	var myDataY = myDataSearch["y"+this.y];
	if (!myDataY) {
		myDataY = new Object();
		myDataY.maxAvailDate = this.resDate;
		myDataY.minAvailDate = this.resDate;
		myDataY.minResDate = minresdate;
		myDataY.maxResDate = maxresdate;
		myDataY.dates = new Array();
		myDataY.year = this.y;
		myDataY.paint = 0;
		myDataY.processed = false;
		myDataY.n = 0;
		myDataSearch["y"+this.y] = myDataY;
	}
	
	var alreadyLoaded = myDataY.dates[this.y+"/"+this.m+"/"+this.d];
	if (alreadyLoaded) {
		availData[alreadyLoaded.n] = this;
		this.n = alreadyLoaded.n;
	} else {
		if (aowas != null) {
			myDataY.n++;
		}	
		availData[nResItems] = this;
		this.n = nResItems;
		nResItems++;
	}
	
	this.nights = nights;
	this.pax = pax;
	this.searchAirport = searchAirport;
	this.aowas = aowas;
	this.aois = aois;
	this.wfwas = wfwas; 
	this.wfis = wfis; 
	this.airport =airport;
	this.available = true;
	this.lastAvail = 0;

	this.aopromoids = aopromoids;
	this.wfpromoids = wfpromoids;
	
	this.aopromos = new Array();
	this.wfpromos = new Array();	
	for (var i=0;i<promos.length;i++) {
		if (aopromoids) {
			for (var j=0;j<aopromoids.length;j++) {
				if (promos[i].id == aopromoids[j]) {
					this.aopromos[this.aopromos.length] = promos[i];
				}
			}
		}
		if (wfpromoids) {
			for (var j=0;j<wfpromoids.length;j++) {
				if (promos[i].id == wfpromoids[j]) {
					this.wfpromos[this.wfpromos.length] = promos[i];
				}
			}			
		}
	}
	this.paxa = paxa;
	this.paxc = paxc;
	this.paxi = paxi;
	this.cacheId = cacheId;
	this.arrairid = arrairid;
	this.depairid = depairid;
	if (aowas != null) {
		myDataY.dates[this.y+"/"+this.m+"/"+this.d] = this;
		if (this.resDate.getTime() < myDataY.minAvailDate.getTime()) {
			myDataY.minAvailDate =this.resDate
		}	
		if (this.resDate.getTime() > myDataY.maxAvailDate.getTime()) {
			myDataY.maxAvailDate =this.resDate
		}	
	}	
}


function processYearData(data) {
	if (!data.processed) {
		var cycleDate = new Date(data.year, 0, 1);
		var lastAvail = -1;
		for (i=0; i<=366; i++) {
			if (cycleDate.getFullYear() == data.year) {
				var resDate = data.dates[data.year + "/" + (cycleDate.getMonth() + 1) + "/" + cycleDate.getDate()];
				if (resDate) {
					lastAvail = 0;
				} else {
					var resCell = new Object();
					if (lastAvail == -1) {
						resCell.lastAvail = -1;
					} else {	
						lastAvail++;
						resCell.lastAvail = lastAvail;
					}	
					resCell.available = false;
					data.dates[data.year + "/" + (cycleDate.getMonth() + 1) + "/" + cycleDate.getDate()] = resCell;
				}
				cycleDate.setDate(cycleDate.getDate()+1);
			}
		}
		data.processed = true;
	}
}

function showDate(d, full) {
	var result;
	
	if (full) {	
		if (d.getDate() < 10) {
			result = "0" + d.getDate();
		} else {
			result = "" + d.getDate();
		}		
		var year = d.getFullYear() + "";
		if (d.getMonth() < 9) {
			result += "/0" + (d.getMonth()+1)+ "/" + year.substring(2,4);
		} else {
			result += "/" + (d.getMonth()+1)+ "/" + year.substring(2,4);
		}
		return result;
	} else {
		if (d.getDate() == 1 || d.getDate() == 21 || d.getDate() == 31) {
			result = d.getDate() + "st";
		} else if (d.getDate() == 2  || d.getDate() == 22) {
			result = d.getDate() + "nd";
		} else if (d.getDate() == 3 ||  d.getDate() == 23) {
			result = d.getDate() + "rd";
		} else {
			result = d.getDate() + "th";
		}
		if (d.getDate() == 1) {
			result += " " + cmonths[d.getMonth()];
		}
		return result;
	}
}
	
function airportName(name) {
	if (name.length > 11) {
		return name.substring(0,9) + "..";
	} else {
		return name;
	}
}

function displayCalendarGrid2(data) {
	var newPos = 0;
	var currentYear = $('#currentYear').val();
	$("#contentDivWait").hide();
	if (firstLoad) { 
		firstLoad = false;
		depmonthObj = document.getElementById("depmonth"); 
		if (data.minAvailDate.getMonth()+1 > currentMonth) {
			for (i=0; i < depmonthObj.options.length; i++) {
				if (depmonthObj.options[i].value.replace("_", "/") == (data.minAvailDate.getMonth()+1) + "/" + data.minAvailDate.getFullYear()) {
					depmonthObj.selectedIndex = i;
					break;
				}
			}
			currentMonth = data.minAvailDate.getMonth()+1;
		}
	}	

	var minMonth = data.minAvailDate.getMonth();
	var maxMonth = data.maxAvailDate.getMonth();
	minMonth=0;
	maxMonth=11;

	var monthObj = document.getElementById("depmonth");
	var selectedMonth = monthObj.options[monthObj.selectedIndex].value.replace("_", "/").split("/")[0];
	newPos = 0 - (750 * ((selectedMonth-1) - minMonth));
	if (data.paint != currentPaint) {
		currentPaint++;
		data.paint = currentPaint;
		
		if (data.n == 0) {
			if($('#noAvailMsgSpan')) {
				$('#noAvailMsgSpan').html('There is no availability for '+currentYear+' for '+currentDuration+' nights, please choose another year or amend the duration.').slideDown("fast");
			}
			else {
				$('#noAvailMsg').html('There is no availability for '+currentYear+' for '+currentDuration+' nights, please choose another year or amend the duration.').slideDown("fast");
			}
		} else {
			$('#noAvailMsg').slideUp();
		}
		
		var content = "<div style=\"width:740px; overflow-x:hidden;\" cellpadding=\"0\" cellspacing=\"0\"><div id=\"prices\" style=\"position:relative; width:"+((((maxMonth-minMonth)+1)*750)-10)+"px\"><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
	
		content+="</tr><tr>";
		for (var i=minMonth; i<=maxMonth; i++) {
			content+="<td valign=\"top\" width=\"740px\">"+displayCalendarGridHTML(data,i, 0); + "</td>";
			if (i < maxMonth) {
				content+="<td width=\"10px\"></td>";	
			}
		}
		content +="</tr></table></div></div>";
		document.getElementById("villaPricesDiv").innerHTML = content;
	}	
	
	var prices = $("#prices");
	
	if (changeAirportMode) {
		document.getElementById("prices").style.left = newPos+"px";

		var dateData = data.dates[availData[currentSelectedWeek].resDate.getFullYear() + "/" + (availData[currentSelectedWeek].resDate.getMonth()+1) + "/" + availData[currentSelectedWeek].resDate.getDate()];
		if (dateData) {		
			$("#bookWeekDivDetailsWFWrapper").show();
			$("#bookWeekDivDetailsWFbutton").show();
			$("#bookWeekDivDetailsWFPleaseWait").hide();	
			bookWeek(dateData.n);
			changeAirportMode = false;
		}
	} else {
		prices.animate({left: newPos}, 600);
	}	
	managePriceLinks(depmonthObj);	
}

function highlightWeek(obj, hover) {
	var count = 0;
	while (count < currentDuration) {
		$(obj).toggleClass("availHover", hover);
		
		if (obj.nextSibling == null) { // End of the week
			if (obj.parentNode.nextSibling && obj.parentNode.nextSibling.firstChild) {
				obj = obj.parentNode.nextSibling.firstChild;
			}
		} else {		
			obj = obj.nextSibling;
		}
		count++;
	}
}

function displayCalendarGridHTML(data, monthOffSet, showFlights) {
	processYearData(data);
	var currentYear = $('#currentYear').val();
	var iDates = new Date(currentYear, monthOffSet, 1);
	
	var html = '<table width="100%" id="calGrid"><tr class="availDays"><td width="14%" align="center">Sunday</td><td width="14%" align="center">Monday</td><td width="14%" align="center">Tuesday</td><td width="14%" align="center">Wednesday</td><td width="14%" align="center">Thursday</td><td width="14%" align="center">Friday</td><td width="14%" align="center">Saturday</td></tr><tr>';
	var i=0;
	
	var toDate = 42;
	for (i=0; i < iDates.getDay(); i++) {
		html+="<td class=\"availCell2\">&nbsp;</td>";
		toDate--;
	}

	var currentDate = new Date();
	var showMonth = iDates.getMonth();
	var rows = 0;
	var dateData = null;
	var dateDataLastAv = null;
	
	for (i=0; i < toDate; i++) {	
		
		dateData = data.dates[iDates.getFullYear() + "/" + (iDates.getMonth()+1) + "/" + iDates.getDate()];
				
		if (!dateData) {			
			if (dateDataLastAv && dateDataLastAv.available) {
				var tmpDate = new Date(dateDataLastAv.resDate);
				tmpDate.setDate(tmpDate.getDate() + parseInt(currentDuration));
			}			
			if (dateDataLastAv && dateDataLastAv.available && tmpDate > iDates) {
				html+="<td title=\"Not a changeover day\" class=\"availCell3\" align=\"center\">"+ showDate(iDates) +"<br/></td>";
			} else {
				html+="<td class=\"availCell2 unavail\">&nbsp;<br/></td>";
			}
			
		} else if (!dateData.available) {
			if (dateData.lastAvail == -1 || (dateData.lastAvail >= currentDuration && iDates.getTime() > data.maxAvailDate.getTime())) {	// start/end contract
				if (data.minResDate != null && data.maxResDate != null && iDates.getTime() >= data.minResDate.getTime() && iDates.getTime() < data.maxResDate.getTime()) {
					if (monthOffSet == 0) {
						html+="<td class=\"availCell2 unavail\">"+ showDate(iDates) +"<br/></td>";
					} else {
						html+="<td title=\"Booked\" class=\"bookedCell\">"+ showDate(iDates) +"<div class=\"bookedCellTxt\">Booked</div></td>";
					}
				} else {				
					html+="<td class=\"availCell2 unavail\">"+ showDate(iDates) +"<br/></td>";
				}
			} else if (dateData.lastAvail < currentDuration) { // unavailable for duration after changeover
				html+="<td title=\"Not a changeover day\" class=\"availCell3\" align=\"center\">"+ showDate(iDates) +"<br/></td>";
			} else {
				html+="<td title=\"Booked\" class=\"bookedCell\">"+ showDate(iDates) +"<div class=\"bookedCellTxt\">Booked</div></td>";
			}
			
		} else {
			html+="<td class=\"availCell";
			if (dateData.aopromos.length > 0 || dateData.wfpromos.length > 0) {
				html+=" offersAvail";
			}
			html+="\" title=\"Book now\" align=\"center\" onmouseover=\"highlightWeek(this, true);\" onmouseout=\"highlightWeek(this, false);\" onclick=\"bookWeek("+dateData.n+");\"><b>"+ showDate(iDates) +"<div>";
			
			if (dateData.aowas != dateData.aois) {
				var priceWas = getCurrencySymbol(dateData.aowas);
				html+="<span class=\"oldAccomPrice\">"+priceWas+"</span><br />";
			} else {
				html+="<div class=\"availCellSpacer\"></div>";	
			}
		
			html+="<span class=\"newAccomPrice\">"+getCurrencySymbol(dateData.aois)+"</span>";
			if (typeof(sitePrefix) != 'undefined' && sitePrefix == 'v4u') {
				html+="<img src=\"/images/v4u/buttons/arrow_pink.png\" align=\"absmiddle\" style=\"margin-left:5px;\" />";
			} else if (typeof(sitePrefix) != 'undefined' && sitePrefix == 'ivc') {
				html+=" <img src=\"/images/ivc/buttons/green-arrows.png\" />"
			} else {
				html+="<img src=\"/images/buttons/button-arrow.gif\" align=\"absmiddle\" style=\"margin-left:5px;\" />";
			}
			html+="</td>";	
			
			dateDataLastAv = dateData;
		}
		
		if (iDates.getDay() == 6) {
			html+="</tr><tr>";
		}

		iDates.setDate(iDates.getDate()+1);
		
	}
	html+="</tr></table>";	
	document.getElementById("villaPricesFloatDiv").style.display = 'block';
	return html;
}


function getMyData(nights, paxa, paxc, paxi, airport, year) {
	var yearData = availData[nights + "_" + paxa + "," + paxc + "," + paxi + "_" + airport];
	if (!yearData) {
		return null;
	} else {
		var actualData = yearData["y"+year];
		if (!actualData) {
			return null;
		}
		return actualData;
	}	
}

var bookingSearchCounter = 0;
function ajaxFlightLookup(accom_id,resort_id,country_id, usePrimeForm, currency) {
	var bookWeekDivDetailsAjaxFlightsRef = $("#bookWeekDivDetailsAjaxFlights");
	var bookWeekDivDetailsAjaxPromoRef = $("#bookWeekDivDetailsAjaxFlightsPromo");
	var bookWeekDivDetailsWFbuttonRef = "#bookWeekDivDetailsWFbutton";
		
	if (usePrimeForm) {
		$('#bstart').val($('#primeDepDate').val());
		$('#frmAvailduration').val($('#primeDuration').val());
		
		$('#frmAvailpaxa').val($('#primePaxa').val());
		$('#frmAvailpaxc').val($('#primePaxc').val());
		$('#frmAvailpaxi').val($('#primePaxi').val());
		bookWeekDivDetailsAjaxFlightsRef =  $("#primeWithFlightsPriceArea");
		bookWeekDivDetailsAjaxPromoRef = $("#primeWithFlightsPromoArea");
		bookWeekDivDetailsWFbuttonRef = "#flight_quote_prime_button";
		
	}

	$('#bookWeekDivDetailsAjaxFlightsBtn').hide();
	bookWeekDivDetailsAjaxFlightsRef.show().html('<div style="text-align:center;font-weight:bold;">Please wait<br/><img alt="Please wait" src="/images/booking/pleaseWait.gif" style="margin-top:5px;"/></div>');
	
	var flightUrl = "/holiday/lookupFlights.cfm?villa_id="+accom_id+"&dep_air_id=01&arr_air_id=&dep_date="+$('#bstart').val()+"&duration="+$('#frmAvailduration').val()+"&adults="+$('#frmAvailpaxa').val()+"&children="+$('#frmAvailpaxc').val()+"&infants="+$('#frmAvailpaxi').val()+"&resort_id="+resort_id+"&setcountry="+country_id+"&bookingSearchCounter="+bookingSearchCounter;

	$.ajax({
		url: flightUrl,
		cache: false,
		success: function(data) {
			var bookingSearchCounterResponse = $(data).find("booking_search_counter").text();
			
			var err = $(data).find("error");
			if ($(err).text() != 'None') {
				bookWeekDivDetailsAjaxFlightsRef.html($(err[0]).text());
			} else if (bookingSearchCounterResponse == bookingSearchCounter) {
				package_price = $(data).find("package").text();
				dep_air_id = $(data).find("f_dep_air_id").text();
				dep_air_name = $(data).find("f_dep_air_name").text();				
				arr_air_id = $(data).find("f_arr_air_id").text();
				adults = parseFloat($('#frmAvailpaxa').val());
				children = parseFloat($('#frmAvailpaxc').val());
				xml_id = $(data).find("xml_id").text();
				xml_no = $(data).find("xml_no").text();										
				promostring = $(data).find("promostring").text();

				if (usePrimeForm) {
					//$("#primeTransportCacheId").val();
					$("#primeArrAirId").val(arr_air_id);
					$("#primeDepAirId").val(dep_air_id);
					$("#primeSearchPackagePrice").val(package_price);
				}

				bookPopulate(package_price,false,adults,children,$('#frmAvailpaxi').val(),$('#bstart').val(),$('#frmAvailduration').val(),accom_id,dep_air_id,arr_air_id,0,xml_id,xml_no);

				if (usePrimeForm) {
					bookWeekDivDetailsAjaxFlightsRef.html('<span class="popupBookPrice">'+formatMoney(package_price, currency,  false, false)+'&nbsp;&nbsp;=&nbsp;&nbsp;'+
					formatMoney(Math.ceil(package_price/(adults+children)), currency,  false, false)+'pp</span>(from '+dep_air_name+')<br/>');
					bookWeekDivDetailsAjaxPromoRef.html('<span style="">'+promostring+'</span>');
					$(bookWeekDivDetailsWFbuttonRef).show();					
				} else {
					bookWeekDivDetailsAjaxFlightsRef.html('<span class="popupBookPrice">'+formatMoney(package_price, currency,  false, false)+'&nbsp;&nbsp;=&nbsp;&nbsp;'+
					formatMoney(Math.ceil(package_price/(adults+children)), currency,  false, false)+'pp</span><br/>from '+dep_air_name+'<br/>');
					bookWeekDivDetailsAjaxPromoRef.html('<span style="">'+promostring+'</span>');
					$(bookWeekDivDetailsWFbuttonRef).show();
				}
			}
		},
		error: function() { 
			bookWeekDivDetailsAjaxFlightsRef.html("There was a problem finding your flight");
		}
	});
}


function laterPrices() {
		var selectMonth = document.getElementById("depmonth");
		if (selectMonth.selectedIndex < selectMonth.options.length-1) {
			selectMonth.selectedIndex+=1;
			moveToMonth(selectMonth);
		}
}


var earlierPricesShow = false;
var laterPricesShow = true;

function earlierPrices(toTop) {
		var selectMonth = document.getElementById("depmonth");
		if (selectMonth.selectedIndex >= 1) {
			selectMonth.selectedIndex-=1;
			moveToMonth(selectMonth);
		}
}

function managePriceLinks(obj) {
	if ( obj.selectedIndex == 0) {
		$("#earlierPrice").fadeOut(500).parent().css('cursor', 'default');
	} else {
		$("#earlierPrice").fadeIn(500).parent().css('cursor', 'pointer');
	}
	if ( obj.selectedIndex == obj.options.length-1) {
		$("#laterPrice").fadeOut(500).parent().css('cursor', 'default');
	} else {
		$("#laterPrice").fadeIn(500).parent().css('cursor', 'pointer');
	}
}
function moveToMonth(obj) {
		var monthYear = obj.options[obj.selectedIndex].value.replace("_", "/").split("/");
		managePriceLinks(obj);
		currentMonth = parseInt(monthYear[0]);
		$('#currentYear').val(parseInt(monthYear[1]));
		updateForm();
}


function showPleaseWaitAccom(show) {
	var contentDivWait= $("#contentDivWait");
	if (show) {
		document.getElementById("villaPricesFloatDiv").style.display = 'none';
		var toppx = ($("#noAvailMsg").is(":visible")) ? '180px' : '130px';
		contentDivWait.css('top',toppx).fadeIn(500);
		
	} else {
		contentDivWait.fadeOut(200);
	}	
}		


function displayContent2(data) {
	var duration = $('#frmAvailduration').val();
	var currentYear = $('#currentYear').val();

	if (data.durations.length > 0) {//populate duration list
		durationObj = document.getElementById("frmAvailduration");
		durationObj.options.length = 0;
		for (var i=0; i<data.durations.length; i++) {
			postfix = (data.durations[i] == 1) ? " night" : " nights";
			durationObj.options[i] = new Option(data.durations[i]+postfix, data.durations[i]);
			if (data.durations[i] == duration) {
				durationObj.options.selectedIndex = i;
			}
		}
	}

	if (parseInt(data.reqNo) == ajaxAvailRequests) {

		$.each(data.promos, function(key, promo) {
			new addPromo(promo.id, promo.webShortDescription);			
		});

		if (data.minDate != null && data.maxDate != null) {
			minResDate = new Date(data.minDate);
			maxResDate = new Date(data.maxDate);
		} else {
			minResDate = maxResDate = data.minDate;
		}

		$.each(data.availability, function(key, avail) {
			new addAvail(avail.depDate, data.duration, data.adults, data.children, data.infants, data.airport, avail.aoRrp, avail.aoSale, avail.atRrp, avail.atSale, avail.obDepAirName, avail.aoPromoIds, avail.atPromoIds, avail.cacheId, avail.obArrAirId, avail.obDepAirId,minResDate,maxResDate);
		});

		if (data.availability.length == 0) {
			var paxa = parseFloat($('#frmAvailpaxa').val());
			var paxc = parseFloat($('#frmAvailpaxc').val());
			var paxi = parseFloat($('#frmAvailpaxi').val());
			var airport = $('#frmAvailairport').val();
			
			currentDt = "January 1, " + currentYear;
			new addAvail(currentDt, duration, paxa,paxc,paxi, airport, null,null, null, null, null, null, null, null, null, null, null, null);
		}

		var d = new Date();
		var nextYear = "1/" + (d.getFullYear()+1);
		if (data.availability.length == 0 && ajaxAvailRequests == 1 && $("#depmonth option[value='"+nextYear+"']").length != 0) {
			// This year, no availability so search following year
			$("#depmonth").val(nextYear).attr('selected',true);
			moveToMonth(document.getElementById('depmonth'));
		} else {
			if (!bookWeekMonth || bookWeekMonth == -1) {
				updateForm();
			}	
				pricesHidden = false;					
				$("#villaPricesDiv").stop(true).css('opacity',1).show();
				$('#keyDiv').slideDown();
			
		}
	}
}

function showPromo(n, accomOnly) {
	var obj = null;
	if (accomOnly) {
		obj = document.getElementById("promoRowA"+n);
	} else {
		obj = document.getElementById("promoRow"+n);
	}
	obj.style.display = 'block';
}


function closeBookWeek() {
	$("#bookWeekDiv,#bookWeekDivDetailsWFAirport").hide();
	closeFullOverlay();
}


function portInfo(id) {
	for (var i=0;i<airs.length;i++) {	
		if (id == airs[i].id) {
			return airs[i];
		}
	}
	return null;
}


function isSameAirport(aAir, bAir) {
	if (aAir.length == 3) {
		if (aAir == bAir) {
			return true;
		}
	} else {
		for (var i=0;i<airs.length;i++) {
			if (airs[i].id == bAir && parseFloat(airs[i].group_id) == parseFloat(aAir)) {
				return true;
			}
		}
	}
	return false;
}


function hidePromo(n, accomOnly) {
	var obj = null;
	if (accomOnly) {
		obj = document.getElementById("promoRowA"+n);
	} else {
		obj = document.getElementById("promoRow"+n);
	}
	obj.style.display = 'none';
}

function bookPopulate(searchPackagePrice,accomOnly,adults,children,infants,startd,nights,accomid,depairid,arrairid,cacheid,xmlId,xmlNo) {
	
	document.getElementById("bsearchPackagePrice").value=searchPackagePrice;
	document.getElementById("baccomOnly").value=accomOnly;
	document.getElementById("badults").value=adults;
	document.getElementById("bchildren").value=children;
	document.getElementById("binfants").value=infants;
	
	document.getElementById("bstart").value=startd;
	document.getElementById("bnights").value=nights;
	document.getElementById("baccomid").value=accomid;
	document.getElementById("bdepairid").value=depairid;
	document.getElementById("barrairid").value=arrairid;
	document.getElementById("bcacheid").value=cacheid;
	document.getElementById("bxmlid").value=xmlId;
	document.getElementById("bxmlno").value=xmlNo;						
}

function showAirport() {
	$('#bookWeekDivDetailsWFAirport').show();
	$("#frmChangeAirport").css({ visibility:"visible" });	/* IE6 fix */
}


function changeAirport(obj) {
	var airport = obj.options[obj.selectedIndex].value;
	changeAirportMode = true;
	$("#bookWeekDivDetailsWFWrapper").hide();
	$("#bookWeekDivDetailsWFbutton").hide();
	$("#bookWeekDivDetailsWFPleaseWait").show();
	document.getElementById("frmAvailairport").value = airport;
	updateForm();
	document.getElementById("bookWeekDivDetailsWFAirport").style.display = 'none';
}

function airport(id,name,group_id,type_id) {
	this.id = id;
	this.name = name;
	this.group_id = group_id;
	this.type_id = type_id;
}


var bookingWeek = false;
function bookWeek(n) {
	if (!bookingWeek) {
		bookingWeek = true;	
		currentSelectedWeek = n;		
		bookWeek2(n);	
	}
}
function bookWeek2(n, channel) {
	
	bookingSearchCounter++;
	
	bookingWeek = false;	
	var obj = document.getElementById("bookWeekDiv");
	currentSelectedWeek = n;

	bookPopulate(availData[n].wfis, false, availData[n].paxa, availData[n].paxc, availData[n].paxi, showDate(availData[n].resDate, true), availData[n].nights, accomId, availData[n].depairid, availData[n].arrairid, availData[n].cacheId,0,0);
	
	var content = "Departing on " + showDate(availData[n].resDate, true) + " for " + availData[n].nights + " nights for " ;
	if (availData[n].paxa == 1) {
		content+= availData[n].paxa + " adult";
	} else 	{
		content+= availData[n].paxa + " adults";
	}
	if (availData[n].paxc > 0) {
		if (availData[n].paxc == 1) {
			content+= ", " + availData[n].paxc + " child";
		} else 	{
			content+= ", " + availData[n].paxc + " children";
		}
	}
	if (availData[n].paxi > 0) {
		if (availData[n].paxi == 1) {
			content+= ", " + availData[n].paxi + " infant";
		} else 	{
			content+= ", " + availData[n].paxi + " infants";
		}	
	}			
	if (includesBreakage) {
		content+= ", including breakage charge.";
	}
	$("#bookWeekDivDetails").html(content);
	
	content = '<span class="popupBookPrice">';
	if (availData[n].aowas > availData[n].aois) {
		content += '<span class="popupPricingWas">' + getCurrencySymbol(availData[n].aowas) + '</span>&nbsp;&nbsp;';
	}

	content += getCurrencySymbol(availData[n].aois) + "</span>";
	
	if (availData[n].aopromos.length > 0) {
		
		content +='<br/><span class="booktoday">Book today for:</span><br />';
		
		for (var p=0;p<availData[n].aopromos.length;p++) {
			
			if (availData[n].aopromos[p].webShortDescription.indexOf("<li") != -1) {
				if (availData[n].aopromos[p].webShortDescription.indexOf("{PERCENTAGE}") > -1)
					content += availData[n].aopromos[p].webShortDescription.replace(/\{PERCENTAGE\}/i, Math.floor(((availData[n].aowas - availData[n].aois) / availData[n].aowas) * 100)+"%");
				else if (availData[n].aopromos[p].webShortDescription.indexOf("{AMOUNT}") > -1)
					content += availData[n].aopromos[p].webShortDescription.replace(/\{AMOUNT\}/i, getCurrencySymbol(Math.floor(availData[n].aowas - availData[n].aois)));
				else
					content += availData[n].aopromos[p].webShortDescription
			}
			else {
				content += availData[n].aopromos[p].webShortDescription + "<br/>";
			}
		}
	}
	
	$('#bookWeekDivDetailsAO').html(content);
	
	content = '<span class="popupBookPrice">';
	if (availData[n].wfis != "" && availData[n].wfis > 0) {
		if (availData[n].wfwas > availData[n].wfis) {
			content += '<span class="popupPricingWas">' + getCurrencySymbol(availData[n].wfwas) + '</span>&nbsp;&nbsp;';
		}
		content +=	getCurrencySymbol(availData[n].wfis) + "";
		if (availData[n].paxa + availData[n].paxc > 1) {
			content+= "&nbsp;&nbsp;=&nbsp;&nbsp;" + getCurrencySymbol(Math.ceil(availData[n].wfis/(availData[n].paxa + availData[n].paxc))) + "pp</span>";
		}
		
		var port = portInfo(availData[n].depairid);

		if (port.type_id == 1) {
			$('#transportTypeLabel').html(lblFlights);
		} else {
			$('#transportTypeLabel').html(lblCrossings);
		}				
		content += "<br/>from " + port.name;
		
		if (changeAirportMode && !isSameAirport($('#frmChangeAirport').val(), availData[n].depairid)) {
			content += " (unavailable from "+$('#frmChangeAirport option:selected').text()+")";
		}

		$('#frmChangeAirport').val(availData[n].depairid);
		
		content +="&nbsp;&nbsp;<a href=\"javascript:showAirport();\">Change airport</a> ";
		$('#bookWeekDivDetailsWF1').html(content).show();
		$('#bookWeekDivDetailsWFbutton').show();
		
		content = "";
		if (availData[n].wfpromos.length > 0) {
			
			content +='<br/><span class="booktoday">Book today for:</span><br />';
		
			for (var p=0;p<availData[n].wfpromos.length;p++) {
				if (availData[n].wfpromos[p].webShortDescription.indexOf("{PERCENTAGE}") > -1)
					content += availData[n].wfpromos[p].webShortDescription.replace(/\{PERCENTAGE\}/i, Math.floor(((availData[n].wfwas - availData[n].wfis) / availData[n].wfwas) * 100)+"%");
				else if (availData[n].wfpromos[p].webShortDescription.indexOf("{AMOUNT}") > -1)
					content += availData[n].wfpromos[p].webShortDescription.replace(/\{AMOUNT\}/i, getCurrencySymbol(Math.floor(availData[n].wfwas - availData[n].wfis)));
				else
					content += availData[n].wfpromos[p].webShortDescription
			}
		}
		$('#bookWeekDivDetailsWF1.#bookWeekDivDetailsWFbutton').show();
		$('#bookWeekDivDetailsAjaxFlights,#bookWeekDivDetailsAjaxFlightsBtn').hide();
	} else {
		// No flights
		content += "</span>";
		$('#bookWeekDivDetailsWFbutton,#bookWeekDivDetailsAjaxFlights,#bookWeekDivDetailsWF1').hide();
		$('#bookWeekDivDetailsAjaxFlightsBtn').show();		
		$("#bookWeekDivDetailsAjaxFlightsPromo").html("");
	}	
	$('#bookWeekDivDetailsWF2').html(content);
	$('#bookWeekDiv').stop().css('opacity',1).show();

	var dimens = fullPageOverlaySetup("", closeBookWeek);
	obj.style.top = ((dimens.height - obj.offsetHeight)/2) + dimens.scrollTop + "px";
	obj.style.left = ((dimens.width - obj.offsetWidth)/2) + "px";	
}
