function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie(name, value, expires, path, domain, secure) 
{
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function strToPriceDate(dateStr)
{
	tmpY = dateStr.substring(0, 4);
	tmpM = dateStr.substring(4, 6);
	tmpD = dateStr.substring(6, 8);

	resDate = tmpD+'.'+tmpM+'.'+tmpY;

	return resDate;
}


var PricesTableClass = Class.create();
PricesTableClass.prototype = {
	initialize: function(ColsCount, RowsCount, ColSelectColor, RowSelectColor, CrossingColor, ColUnselectColor, RowUnselectColor) {
		this.setColsCount(ColsCount);
		this.setRowsCount(RowsCount);
		this.ColSelectColor = ColSelectColor;
		this.RowSelectColor = RowSelectColor;
		this.CrossingColor = CrossingColor;
		this.ColUnselectColor = ColUnselectColor;
		this.RowUnselectColor = RowUnselectColor;
		this.Discounts = new Array();
	},
	ColSelectColor: '#000000',
	RowSelectColor: '#000000',
	CrossingColor: '#000000',
	ColUnselectColor: '#000000',
	RowUnselectColor: '#000000',
	ColsCount: 0,
	setColsCount: function (count) {
		count = parseInt(count);
		this.ColsCount = count;
	},
	selectCol: function (id) {
		
	},
	RowsCount: 0,
	setRowsCount: function (count) {
		count = parseInt(count);
		this.RowsCount = count;
	},
	selectRow: function (id) {
		
	},
	RoomType: 0,
	setRoomType: function(type) {
		this.RoomType = type;
		//getFreeRoomsCount();
		//UpdateBookingLink();
	},
	StartDate: 0,
	setStartDate: function (date) {
		this.StartDate = date;
		$('StartDate').value = this.StartDate;
		getFreeRoomsCount();
		//GetPriceTable();
		UpdateBookingLink();
	},
	EndDate: 0,
	setEndDate: function (date) {
		this.EndDate = date;
		$('EndDate').value = this.EndDate;
		getFreeRoomsCount();
		//GetPriceTable();
		UpdateBookingLink();
	},
	Discounts: 0,
	updateDiscounts: function (value) {
		this.Discounts = parseFloat(value);
		this.updateTable();
	},
	Currency: 'UAH',
	setCurrency: function (value) {
		this.Currency = value;
		this.updateTable();
	},
    SortType: 'price',
	setSortType: function (value)
	{
		this.SortType = value;
		this.updateTable();
	},
	updateTable: function () {
		
		if (this.StartDate == 0 || this.EndDate == 0)
		{
            //return false;

			tmpStartDate = getCookie('bookitStartDate');
			tmpEndDate = getCookie('bookitDueDate');
			if (tmpStartDate) this.StartDate = strToPriceDate(tmpStartDate);
			if (tmpEndDate) this.EndDate = strToPriceDate(tmpEndDate);
		}
		
		//if (tmpStartDate) this.setStartDate(strToPriceDate(tmpStartDate));
		//if (tmpEndDate) this.setEndDate(strToPriceDate(tmpEndDate));

		if (this.StartDate == 0 || this.EndDate == 0) {
			var params = "f=get_pricestable2";
		} else {
			var params = "f=get_pricestable2";
		}
		params += "&object=" + Hotel;
//		params += "&object=" + Hotel;		
		params += "&currency=" + this.Currency;
		params += "&discount=" + this.Discounts;
		params += "&startDate=" + this.StartDate;
		params += "&endDate=" + this.EndDate;
		params += "&city=" + City;
//		params += "&city=" + City;
		params += "&sortType=" + this.SortType;
		params += "&isAgent=" + isAgent;
//		params += "&isAgent=" + isAgent;
		update_prepay_percent(this.StartDate, this.EndDate, Hotel);
		var ajax = new Ajax.Updater({success: "PricesTableContainer"}, '/ajax_functions/nzb_places.php', 
			{
				method: 'post', 
				parameters: params,
				asynchronous: true
			}
		);
	}
}
function getFreeRoomsCount() {
	if (PricesTable.StartDate != 0 && PricesTable.EndDate != 0 && PricesTable.RoomType != 0) {
		var params = "f=get_freeroomscount";
		params += "&hotel=" + Hotel;
		params += "&start=" + PricesTable.StartDate;
		params += "&end=" + PricesTable.EndDate;
		params += "&type=" + PricesTable.RoomType;
		var ajax = new Ajax.Request('/ajax_functions/nzb_places.php', 
			{
				method: 'post', 
				parameters: params,
				asynchronous: true,
				onComplete: function (transport) {
					var tmp = parseInt(transport.responseText);
					if (tmp > 3) {
						$('freeRoomsCount').innerHTML = "3+";
					} else {
						$('freeRoomsCount').innerHTML = "" + tmp;
					}
					/*
					if (tmp > 0) {
						$('BookingLink').show();
						$('BookingLinkDisabled').hide();
					} else {
						$('BookingLink').hide();
						$('BookingLinkDisabled').show();
						$('BookingLinkDisabled').title = 'Нет свободных номеров, попробуйте другой тип номера или период отдыха';
						
					}
					*/
				}
			}
		);
		$('freeRoomsCountTitle').show();
	} else {
		$('freeRoomsCountTitle').hide();
	}
}

function setRoomType(type) {
	PricesTable.setRoomType(type);
}

function setCurrency(value) {
	PricesTable.setCurrency(value);
}
function updateDiscounts(el) {
	var value = 0;
	if (el)
	{
		if (el.id != "BookITDiscount") {
			if (el.checked) {
				value = parseFloat(el.value);
			} else {
				value = 0;
			}
			for (var i = 1; i <= CountOfDiscountCards; i++) {
				if (el.id != 'NezabaromCardDiscount_' + i) {
					$('NezabaromCardDiscount_' + i).checked = false;
				}
			}
			if ($('BookITDiscount').checked) {
				value += parseFloat($('BookITDiscount').value);
			} 
		} else {
			for (var i = 1; i <= CountOfDiscountCards; i++) {
				if ($('NezabaromCardDiscount_' + i).checked) {
					value = parseFloat($('NezabaromCardDiscount_' + i).value);
				}
			}
			if (el.checked) {
				value += parseFloat(el.value);
			} 
		}
	}
	PricesTable.updateDiscounts(value);
}

function getSoonEntranceUrl() {
	var params = "";
	if (PricesTable.StartDate != 0) {
		params += "&srt=" + PricesTable.StartDate;
	}
	if (PricesTable.EndDate != 0) {
		params += "&end=" + PricesTable.EndDate;
	}
	params += "&bookitdate"
	return params;
}

function GoToBookITConfigurator(objectId, cityId)
{
	var params = "";
	var d = document;
	document.location.href = 'http://www.bookit.com.ua/configurator/?start=' + (PricesTable.StartDate != 0 ? PricesTable.StartDate : '') + '&end=' + (PricesTable.EndDate != 0 ? PricesTable.EndDate : '') + '&object=' + objectId + '&city=' + cityId + '&roomtype_b=' + (PricesTable.RoomType != 0 ? PricesTable.RoomType : '');
}

function UpdateBookingLink()
{
	try {
		$('BookingLink').href = 'http://www.bookit.com.ua/configurator/?start=' + (PricesTable.StartDate != 0 ? PricesTable.StartDate : '') + '&end=' + (PricesTable.EndDate != 0 ? PricesTable.EndDate : '') + '&object=' + Hotel + '&city=' + City + '&roomtype_b=' + (PricesTable.RoomType != 0 ? PricesTable.RoomType : '');
	} catch(err) {
		
	}
}

function GetPriceTable()
{
    if (PricesTable.StartDate != 0 && PricesTable.EndDate != 0) {
        PricesTable.updateTable();
    }
}

function getBookingHref(hotelId, cityId, roomtype, start, end)
{
	document.location.href = '/configurator/?&object='+hotelId+'&city='+cityId+'&roomtype='+roomtype+'&start='+start+'&end='+end+'&roomsCount=1';
}

function getBookingHrefTO(hotelId, cityId, roomtype, start, end)
{
	document.location.href = '/configurator/?&object='+hotelId+'&city='+cityId+'&roomtype='+roomtype+'&start='+start+'&end='+end+'&roomsCount=1&isSaleTO=1';
}

