var hml_years = new Array();
var accom_dates = new Array();
var current_month_selected;
var current_year_selected;

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) {			
	hml_year_selected = p_year;

	// sets up hml title
	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 + ");'>" + 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
	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) {
		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 (hml_years[year_position].low == "") {
			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 != "") {
				document.getElementById("hml_low_old_price").innerHTML = hml_years[year_position].old_low + "&nbsp;";
				document.getElementById("hml_low_spacer").innerHTML = "&nbsp;&nbsp;";
			}
			else {
				document.getElementById("hml_low_old_price").innerHTML = "";
				document.getElementById("hml_low_spacer").innerHTML = "";
			}
			document.getElementById("hml_low_prices").style.display = "block";					
		}				
		if (mid_season_list != "") {
			if (hml_years[year_position].mid == "") {
				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 != "") {
					document.getElementById("hml_mid_old_price").innerHTML = hml_years[year_position].old_mid + "&nbsp;";
					document.getElementById("hml_mid_spacer").innerHTML = "&nbsp;&nbsp;";
				}
				else {
					document.getElementById("hml_mid_old_price").innerHTML = "";
					document.getElementById("hml_mid_spacer").innerHTML = "";
				}
				document.getElementById("hml_mid_prices").style.display = "block";					
			}
		}
		if (high_season_list != "") {
			if (hml_years[year_position].high == "") {
				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 != "") {
					document.getElementById("hml_high_old_price").innerHTML = hml_years[year_position].old_high + "&nbsp;";
					document.getElementById("hml_high_spacer").innerHTML = "&nbsp;&nbsp;";
				}
				else {
					document.getElementById("hml_high_old_price").innerHTML = "";
					document.getElementById("hml_high_spacer").innerHTML = "";
				}
				document.getElementById("hml_high_prices").style.display = "block";					
			}
		}
	}			
}

function setup_pricing (p_year) {
	var first_month = -1;
	for (var i = 0; i < accom_dates.length; i++) {
		if (first_month == -1 && accom_dates[i].res_date.getFullYear() == p_year) {
			first_month = accom_dates[i].res_date.getMonth();
		}
	}
	display_three_months(first_month, p_year);
}

// displays the complete grid
function display_three_months (month, year) {
	
	current_month_selected = month;
	current_year_selected = year;
	
	if (month < accom_dates[0].res_date.getMonth() && year == accom_dates[0].res_date.getFullYear()) {
		month = accom_dates[0].res_date.getMonth();
	}

	//- finds the details for the three columns
	var tmp_month_A = month;
	var tmp_year_A = year;
	var tmp_month_B = (month + 1) % 12;
	var tmp_year_B = (tmp_month_B < month) ? year + 1 : year;
	var tmp_month_C = (month + 2) % 12;
	var tmp_year_C = (tmp_month_C < month) ? year + 1 : year;
	
	var tmp_data_A = new Array();
	var tmp_data_B = new Array();
	var tmp_data_C = new Array();
	
	// finds the data for each month
	for (var i = 0; i < accom_dates.length; i++) {
		if (accom_dates[i].res_date.getMonth() == tmp_month_A && accom_dates[i].res_date.getFullYear() == tmp_year_A) {
			tmp_data_A[tmp_data_A.length] = accom_dates[i];
		}
		else if (accom_dates[i].res_date.getMonth() == tmp_month_B && accom_dates[i].res_date.getFullYear() == tmp_year_B) {
			tmp_data_B[tmp_data_B.length] = accom_dates[i];
		}
		else if (accom_dates[i].res_date.getMonth() == tmp_month_C && accom_dates[i].res_date.getFullYear() == tmp_year_C) {
			tmp_data_C[tmp_data_C.length] = accom_dates[i];
		}						
	}
	
	// finds the max rows to display
	var max_rows = tmp_data_A.length;
	if (max_rows < tmp_data_B.length) {
		max_rows = tmp_data_B.length;
	}
	if (max_rows < tmp_data_C.length) {
		max_rows = tmp_data_C.length;
	}
	
	// displays the three months
	display_month(tmp_month_A,tmp_year_A,1,tmp_data_A,max_rows);
	display_month(tmp_month_B,tmp_year_B,2,tmp_data_B,max_rows);
	display_month(tmp_month_C,tmp_year_C,3,tmp_data_C,max_rows);
	
	if(tmp_year_A == tmp_year_B && tmp_year_A == tmp_year_C){
		switch_year(tmp_year_A);
	}
	
	// checks if any of the months have pricing
	var price_found = false;
	for (var i = 0; i < tmp_data_A.length; i++) {
		if (tmp_data_A[i].price != "") {
			price_found = true;
			break;
		}
	}
	for (var i = 0; i < tmp_data_B.length; i++) {
		if (!price_found && tmp_data_B[i].price != "") {
			price_found = true;
			break;
		}
	}
	for (var i = 0; i < tmp_data_C.length; i++) {
		if (!price_found && tmp_data_C[i].price != "") {
			price_found = true;
			break;
		}
	}
	if (price_found) {
		document.getElementById("nextAvailableDates").style.display = "none";
	}
	else {
		var next_av_month = "";
		var next_av_year = "";
		for (var i = 0; i < accom_dates.length; i++) {
			if ((accom_dates[i].res_date > new Date(tmp_year_C,tmp_month_C,1)) && accom_dates[i].price != "") {
				next_av_month = accom_dates[i].res_date.getMonth();
				next_av_year = accom_dates[i].res_date.getFullYear();
				break;
			}
		}
		document.getElementById("nextAvailableDates").onclick = function () {display_three_months(next_av_month,next_av_year);}
		document.getElementById("nextAvailableDates").style.display = "block";					
	}					
}

// displays a single month
function display_month (month, year, position, data, max_rows) {

	// displays the month heading
	var month_heading_html = month_name(month) + " " + year;
	var arrow_html = "";
	if (position == 1) {
		if ((new Date(accom_dates[0].res_date.getFullYear(),accom_dates[0].res_date.getMonth(),1)) <  (new Date(year,month,1))) {
			var tmp_previous_month = (month - 1) % 12;
			if (tmp_previous_month == -1) {
				tmp_previous_month = 11;
			}
			var tmp_previous_year = (tmp_previous_month > month) ? year - 1 : year;
			arrow_html = "<img src='" + lblWebsiteUrl + "images/arrows/blue_left_on_grey.jpg' style='cursor:pointer;' align='absmiddle' onClick='display_three_months(" + tmp_previous_month + "," + tmp_previous_year + ");'>";
		}
		month_heading_html = "<div style='float:left;width:30px;margin-left:10px;'>" + arrow_html + "</div><div style='float:left;'>" + month_heading_html + "</div>";
	}
	else if (position == 3) {
		if ((new Date(accom_dates[accom_dates.length-1].res_date.getFullYear(),accom_dates[accom_dates.length-1].res_date.getMonth(),1)) >  (new Date(year,month,1))) {
			var tmp_next_month = (month - 1) % 12;						
			if (tmp_next_month == -1) {
				tmp_next_month = 11;
			}
			else if (tmp_next_month == -2) {
				tmp_next_month = 10;
			}
			var tmp_next_year = year;
			if (tmp_next_month > 9 && month < 2) {
				tmp_next_year = tmp_next_year - 1;
			}
			arrow_html = "<img src='" + lblWebsiteUrl + "images/arrows/blue_right_on_grey.jpg' style='cursor:pointer;' align='absmiddle' onClick='display_three_months(" + tmp_next_month + "," + tmp_next_year + ");'>";
		}
		month_heading_html = "<div style='float:right;width:30px;margin-right:10px;'>" + arrow_html + "</div><div style='float:right;'>" + month_heading_html + "</div>";
	}
	document.getElementById("month_" + position).innerHTML = month_heading_html;
	
	// sets up the conditions to display the cell
	var border_type = 2;
	if (position == 1) {
		border_type = 1;
	}
	var cell_html = "";
	for (var i = 0; i < data.length; i++) {
		cell_html += create_cell(data[i],border_type);						
	}
	for (var i = data.length; i < max_rows; i++) {
		cell_html += create_cell(null,border_type);
	}
	document.getElementById("month_cells_" + position).innerHTML = cell_html;

}

// creates a date cell
function create_cell (cell, border_type) {				
	if (border_type == 1) {
		var border_html = "border-left:2px solid #CCCCCC;border-bottom:2px solid #CCCCCC;border-right:2px solid #CCCCCC;";
	} 
	else {
		var border_html = "border-bottom:2px solid #CCCCCC;border-right:2px solid #CCCCCC;";
	}
	var cell_html = "";
	
	if (cell == null) {
		cell_html += "<div class='pricing_cell' style='" + border_html + "'>";
	}
	else {
		var cell_bg_class = "";
		if (accom_res_user) {
			cell_bg_class = "rental_type_id" + cell.rental_type_id;
		}
		cell_html += "<div class='pricing_cell " + cell_bg_class + "' style='" + border_html + "'>";
		cell_html += "<div>";
		cell_html += "<div style='float:left;width:80px;padding-top:5px;'>" + day_name(cell.res_date.getDay()) + " " + cell.res_date.getDate() + "</div>";
		if (cell.price != "") {
			var check_box = "";
			if (document.frmPrice.priceChoiceID.value == cell.id || document.frmPrice.priceChoiceID2.value == cell.id) {
				check_box = "checked";
			}
			cell_html += "<div style='float:left;'><input type='checkbox' id='" + cell.id + "' name='" + cell.id + "' onClick='vilGetDuration(\"" +pricing_region_id+"\",new Date(" + cell.res_date.getFullYear() + "," + cell.res_date.getMonth() + "," + cell.res_date.getDate() + "));pick_cell_checkbox(\"" + cell.id + "\",this);' " + check_box + " /></div>";
		}
		cell_html += "</div>";
		
		if (cell.price != "") {
			cell_html += "<div style='clear:both;'>";
			
			cell_html += "<div style='float:left;width:70px;font-weight:bold;'>";
			cell_html += ((cell.old_price != "") ? "<span style='text-decoration:line-through;color:#666666;'>" + cell.old_price + "</span>" : "&nbsp;") ;
			cell_html += "</div>";
			
			cell_html += "<div class='";
			if (s_accom_type == "BOJ") {
				cell_html += "bojFont"
			}
			else {
				cell_html += "stdFont";
			}
			cell_html += "' style='float:left;font-weight:bold;'>" + cell.price + "</div>";
			cell_html += "</div>";
			
			cell_html += "<div style='clear:both;'>" + ((cell.promotion != "") ? lbloffersavailable : "") +"</div>";
		}	
	}
	
	cell_html += "</div>";
	
	return cell_html;
}

function month_selected (month_year) {
	display_three_months(parseFloat(month_year.substr(4,2))-1, parseFloat(month_year.substr(0,4)));
}

function pick_cell_checkbox (id, checkbox) {
	var id_parts = id.split("_");
	var tmp_day = id_parts[0];
	var tmp_id = id_parts[1];
	if (!checkbox.checked) {
		if (document.frmPrice.priceChoiceID.value == id) {
			document.frmPrice.priceChoiceID.value = document.frmPrice.priceChoiceID2.value;
			document.frmPrice.priceChoiceID2.value= 0;
			set_nights_dropdown(7);
		}
		if (document.frmPrice.priceChoiceID2.value == id) {
			document.frmPrice.priceChoiceID2.value = 0;
			document.frmPrice.duration.selectedIndex = 0;
			set_nights_dropdown(7);
		}
	}
	else {
		
		var choice_a = document.frmPrice.priceChoiceID.value;
		var choice_b = document.frmPrice.priceChoiceID2.value;
		var choice_parts_a = choice_a.split("_");
		var choice_parts_b = choice_b.split("_");
		
		if (choice_a == 0) {
			document.frmPrice.priceChoiceID.value = id;
			set_nights_dropdown(7);
		}
		else if (choice_parts_a[0] == tmp_day && (parseFloat(choice_parts_a[1]) - 1) == tmp_id) {
			if(document.getElementById(choice_b)){
				document.getElementById(choice_b).checked= false;
			}
			document.frmPrice.priceChoiceID2.value = choice_a;
			document.frmPrice.priceChoiceID.value = id;
			set_nights_dropdown(14);
		}
		else if (choice_parts_a[0] == tmp_day && (parseFloat(choice_parts_a[1]) + 1) == tmp_id) {
			document.frmPrice.priceChoiceID2.value = id;
			set_nights_dropdown(14);
		}	
		else if (choice_parts_b[0] == tmp_day && (parseFloat(choice_parts_b[1]) + 1) == tmp_id) {
			if(document.getElementById(choice_a)){
				document.getElementById(choice_a).checked= false;
			}
			document.frmPrice.priceChoiceID.value = choice_b;
			document.frmPrice.priceChoiceID2.value = id;
			set_nights_dropdown(14);
		}	
		else {
			if(document.getElementById(document.frmPrice.priceChoiceID.value)){
				document.getElementById(choice_a).checked= false;
			}
			if(document.getElementById(choice_b)){
				document.getElementById(choice_b).checked= false;
			}
			document.frmPrice.priceChoiceID.value = id;
			document.frmPrice.priceChoiceID2.value = 0;
			set_nights_dropdown(7);
		}				
	}
}

function set_nights_dropdown (value) {
	for (var i = 0; i < document.frmPrice.duration.options.length; i++) {
		if (document.frmPrice.duration.options[i].value == value) {
			document.frmPrice.duration.options.selectedIndex = i;
			break;
		}
	}
}

function number_format (x) {
	var num = parseFloat(x);
	return (x < 10) ? "0" + x : x;
}

function setup_accom_for_pricing () {
	showTab("villa_price");
	display_three_months(parseFloat(document.frmPrice.selected_month.value),parseFloat(document.frmPrice.selected_year.value));
}

function produceQuote () {
	var cell = "";
	for (var i = 0; i < accom_dates.length; i++) {
		if (accom_dates[i].id == document.frmPrice.priceChoiceID.value) {
			cell = accom_dates[i];
			break;
		}
	}
	if (cell == "") {
		alert(lblnotselecteddate);
		return false;
	}
	var max_occ = pricing_max_occupany;
	var pax = parseFloat(document.frmPrice.paxa.value) + parseFloat(document.frmPrice.paxc.value);
	if (parseFloat(document.frmPrice.paxi.value) > 1) {
		pax = pax + (parseFloat(document.frmPrice.paxi.value) - 1);
	}
	if (pax > max_occ) {
		alert(lblselectedtoomanypassengers + " " + max_occ);
		return false;
	}
	
	var start_date = "{ts '" + cell.res_date.getFullYear() + "-" + number_format((cell.res_date.getMonth() + 1)) + "-" + number_format(cell.res_date.getDate()) + " 00:00:00'}";
	var duration_list = document.frmPrice.duration[document.frmPrice.duration.selectedIndex].value;
	var adults = document.frmPrice.paxa[document.frmPrice.paxa.selectedIndex].value;
	var children = document.frmPrice.paxc[document.frmPrice.paxc.selectedIndex].value;
	var infants = document.frmPrice.paxi[document.frmPrice.paxi.selectedIndex].value;
	var villa_id = pricing_villa_id;
	var country_id = pricing_country_id;
	var region_id = pricing_region_id;
	var airport_id = document.frmPrice.airport[document.frmPrice.airport.selectedIndex].value;
	var isFerryVilla = document.frmPrice.isFerryVilla.value;
	
	document.getElementById("quoteIFrame").src = "/accom_page/quote.cfm?start_date=" + start_date + "&duration_list=" + duration_list + "&adults=" + adults + "&children=" + children + "&infants=" + infants + "&villa_id=" + villa_id + "&country_id=" + country_id + "&airport_id=" + airport_id + "&isFerryVilla=" + isFerryVilla + "&region_id=" + region_id + "&resort_id=" + pricing_resort_id + pricing_cookieValue;
	document.getElementById("quoteIFrame").style.display = 'block';
	document.getElementById("produceQuoteButton").style.display = 'none';
	document.getElementById("updateQuoteButton").style.display = 'block';					
}

// pass a region id and date and it'll populate the duration field
function vilGetDuration(ir, idt) {
	
	if(ir.length == 1){
		ir = '0'+ir;	
	}
	var r,s,e,d;
	var selDur = document.frmPrice.duration;
	var curVal = selDur.value;
	selDur.options.length = 0;
	
	var found = false;
	for(var i=0;i<dur_data.length;i++) {
		r = dur_data[i].substr(0,2);
		s = new Date(parseInt("20" + dur_data[i].substr(6,2)),dur_data[i].substr(4,2)-1,dur_data[i].substr(2,2));
		e = new Date(parseInt("20" + dur_data[i].substr(12,2)),dur_data[i].substr(10,2)-1,dur_data[i].substr(8,2));
		
		if (ir == r && s <= idt && e > idt && dur_data[i].length > 14) {
			d = dur_data[i].substr(14);
			for (var j=0;j<d.length;j+=2) {
				var n = (d.substr(j,2) < 10) ? d.substr(j,2).substr(1) : d.substr(j,2);
				selDur.options[selDur.options.length] = new Option(n + " "  + convertSpecialCharacters(nights_text).toLowerCase(), n);
				if (n == curVal) selDur.selectedIndex = selDur.options.length - 1;
			}
			found = true;
			break;
		}
	}
	
	if (!found || selDur.options.length == 0) {
		selDur.options[0] = new Option("7 "  + convertSpecialCharacters(nights_text).toLowerCase(), 7);
		selDur.options[1] = new Option("14 " + convertSpecialCharacters(nights_text).toLowerCase(), 14);
		selDur.options[2] = new Option("21 " + convertSpecialCharacters(nights_text).toLowerCase(), 21);
		selDur.value = curVal;
	}
}
