var CalendarBookings = {};

CalendarBookings.showCreateBooking = function(cCalendarDate, state) {
	Properties.reserveReference({
		showLoading: function() {
			Dialog.info('Generating Booking Reference...<br /><br /><img src=\'/images/loading.gif\' alt=\'\' />', {
				className: 'cityred',
				width: 300,
				hideEffect: Element.hide
			});
		},
		onSuccess: function(reference) {
			Dialog.closeInfo();
			CalendarBookings.showEditBooking(cCalendarDate, {
				state: state,
				reference: reference
			});
		},
		onFailure: function(message) {
			Dialog.closeInfo();
			Dialog.alert(message, {
				className: 'cityred',
				width: 300
			});
		}
	});
}

CalendarBookings.showEditBooking = function(cCalendarDate, options) {
	try {
	var bookinginfo = cCalendarDate.getBookingInfo();
	
	if (options == undefined) {
		options = {};
	}
	
	if (options.state == undefined) {
		options.state = cCalendarDate.getState();
	}
	
	// Reset Booking Form
	$('editbooking_name').value = '';
	$('editbooking_email').value = '';
	$('editbooking_phone1_code').value = '';
	$('editbooking_phone1').value = '';
	
	if (cCalendarDate.calendar.property['_fieldvalues'].max_price == undefined) {
		pricepernight = 0;
	} else {
		var pricepernight = cCalendarDate.calendar.property['_fieldvalues'].max_price - 0;
	}
	
	$('editbooking_booking_price').value = pricepernight.toFixed(2);
	$('editbooking_property_pricepernight').value = pricepernight;
	$('editbooking_pricepernight').value = pricepernight;
	$('editbooking_discount').innerHTML = '';
	
	$('editbooking_user_language').selectedIndex = 0;
	$('editbooking_street1').value = '';
	$('editbooking_street2').value = '';
	$('editbooking_city').value = '';
	$('editbooking_county').value = '';
	$('editbooking_postalcode').value = '';
	$('editbooking_country').value = '';
	
	$('editbooking_comments').value = '';	
	
	if (options.reference == undefined) {
		// Edit Booking
		var window_title = 'Edit Booking';
		options.reference = cCalendarDate.getBookingReference();
		
		$('editbooking_reference').innerHTML = cCalendarDate.getBookingReference();
		
		// Find Date From
		var cDate = new Date();
		cDate.setDate(1);
		cDate.setFullYear(cCalendarDate.getYear());
		cDate.setMonth(cCalendarDate.getMonth() - 1);
		cDate.setDate(cCalendarDate.getDay());
		do {
			cDate.setDate(cDate.getDate() - 1);
			var dateID = cDate.getFullYear() + '-' + (cDate.getMonth() + 1) + '-' + cDate.getDate();
			if (cCalendarDate.calendar.dates[dateID] == undefined) {
				break;
			} else if (cCalendarDate.calendar.dates[dateID].getBookingReference() != cCalendarDate.getBookingReference()) {
				break;
			}
		} while (1);
		cDate.setDate(cDate.getDate() + 1);
		bookinginfo.date_from = cDate.getFullYear() + '-' + (cDate.getMonth() + 1) + '-' + cDate.getDate();
		
		$('editbooking_name').value = bookinginfo.name;
		$('editbooking_email').value = bookinginfo.email;
		$('editbooking_phone1_code').value = bookinginfo.phone1_code;
		$('editbooking_phone1').value = bookinginfo.phone1;
		
		$('editbooking_user_language').selectedIndex = 0;
		for (var i = 0; i < $('editbooking_user_language').options.length; i++) {
			if ($('editbooking_user_language').options[i].value == bookinginfo.user_language) {
				$('editbooking_user_language').selectedIndex = i;
			}
		}
		
		$('editbooking_street1').value = bookinginfo.street1;
		$('editbooking_street2').value = bookinginfo.street2;
		$('editbooking_city').value = bookinginfo.city;
		$('editbooking_county').value = bookinginfo.county;
		$('editbooking_postalcode').value = bookinginfo.postalcode;
		$('editbooking_country').value = bookinginfo.country;
		
		$('editbooking_comments').value = bookinginfo.comments;
		
		if (cCalendarDate.calendar.property['_fieldvalues'].max_price != undefined) {
			var pricepernight = cCalendarDate.calendar.property['_fieldvalues'].max_price - 0;
		} else {
			pricepernight = 0;
		}
		
		$('editbooking_property_pricepernight').value = pricepernight;
		$('editbooking_pricepernight').value = bookinginfo.booking_price / bookinginfo.booking_nights;
		$('editbooking_booking_price').value = (bookinginfo.booking_price - 0).toFixed(2);
		if (pricepernight > 0 && bookinginfo.booking_price / bookinginfo.booking_nights != pricepernight) {
			var discount = 100 * ((bookinginfo.booking_price / bookinginfo.booking_nights) / pricepernight);
			$('editbooking_discount').innerHTML = '(' + discount.ceil() + '%)';
		} else {
			$('editbooking_discount').innerHTML = '';
		}
	} else {
		// Create Booking
		var window_title = 'Create Booking';
		bookinginfo.date_from = cCalendarDate.getYear() + '-' + cCalendarDate.getMonth() + '-' + cCalendarDate.getDay();
		
		var bookinginfo = cCalendarDate.getBookingInfo();
			
		bookinginfo.property_reference = cCalendarDate.calendar.property['_fieldvalues'].reference;
		bookinginfo.date_from = cCalendarDate.getYear() + '-' + cCalendarDate.getMonth() + '-' + cCalendarDate.getDay();
		
		$('editbooking_reference').innerHTML = options.reference;
	}
	
	// Calculate Date From
	var cDate = new Date();
	var date_from = bookinginfo.date_from.split('-');
	cDate.setDate(1);
	cDate.setFullYear(date_from[0]);
	cDate.setMonth(date_from[1] - 1);
	cDate.setDate(date_from[2]);
	$('editbooking_date_from').innerHTML = CalendarWeekNames[cCalendarDate.calendar.getWeekday(cDate.getFullYear(), cDate.getMonth() + 1, cDate.getDate())].short + ' '
						                 + cDate.getDate() + cDate.getDate().getSuffix() + ' '
						                 + CalendarMonthNames[cDate.getMonth()].full + ' '
						                 + cDate.getFullYear();
	
	// Number of Nights
	var cDate = new Date();
	cDate.setDate(1);
	if (options.reference == undefined) {
		cDate.setFullYear(cCalendarDate.getYear());
		cDate.setMonth(cCalendarDate.getMonth() - 1);
		cDate.setDate(cCalendarDate.getDay());
	} else {
		var date_from = bookinginfo.date_from.split('-');
		cDate.setFullYear(date_from[0]);
		cDate.setMonth(date_from[1] - 1);
		cDate.setDate(date_from[2]);
	}
	
	$('editbooking_booking_nights').options.length = 0;
	for (var night = 1; night <= 200; night++) {
		cDate.setDate(cDate.getDate() + 1);
		var optionText = CalendarWeekNames[cCalendarDate.calendar.getWeekday(cDate.getFullYear(), cDate.getMonth() + 1, cDate.getDate())].short + ' '
					   + cDate.getDate() + cDate.getDate().getSuffix() + ' '
					   + CalendarMonthNames[cDate.getMonth()].full + ' '
					   + cDate.getFullYear();
		$('editbooking_booking_nights').options[$('editbooking_booking_nights').options.length] = new Option(optionText, night);
		
		var dateID = cDate.getFullYear() + '-' + (cDate.getMonth() + 1) + '-' + cDate.getDate();
		if (cCalendarDate.getBookingReference() && (cCalendarDate.getBookingInfo().date_to == dateID)) { // (cCalendarDate.calendar.dates[dateID].getYear() + '-' + cCalendarDate.calendar.dates[dateID].getMonth() + '-' + cCalendarDate.calendar.dates[dateID].getDay()))) {
			$('editbooking_booking_nights').options[$('editbooking_booking_nights').options.length - 1].selected = true;
		}
		if (cCalendarDate.calendar.dates[dateID] == undefined) {
			break;
		} else if (cCalendarDate.calendar.dates[dateID].getState() != 1 && cCalendarDate.calendar.dates[dateID].getState() != cCalendarDate.getState()) {
			break;
		} else if (cCalendarDate.calendar.dates[dateID].getBookingReference() && (cCalendarDate.getBookingReference() != cCalendarDate.calendar.dates[dateID].getBookingReference())) {
			break;
		}
	}
	
	$('editbooking_form').win = Dialog.confirm('<div id="planner_editbooking"></div>', {
		className: 'cityred',
		width: 600,
		height: 392,
		title: window_title,
		okLabel: 'Save',
		cancelLabel: 'Cancel',
		onBeforeShow: function(win) {
			$('editbooking_booking_nights').style.visibility = '';
			$('planner_editbooking').appendChild($('editbooking_form_content'));
			$('editbooking_form_content').show();
		},
		onOk: function(win) {
			CalendarBookings.saveBooking(cCalendarDate, options.state);
			return true;
		},
		onDestroy: function(win) {
			$('editbooking_form_content').hide();
			$(document.body).appendChild($('editbooking_form_content'));
		}
	});
	
	} catch (e) {
		alert (e);
	}
}

CalendarBookings.saveBooking = function(cCalendarDate, state) {
	var bookinginfo = cCalendarDate.getBookingInfo();
	var bookingstate = (state == undefined) ? cCalendarDate.getState() : state;
	var date_from = bookinginfo.date_from.split('-');
	
	bookinginfo.booking_reference = $('editbooking_reference').innerHTML;
	bookinginfo.name = $F('editbooking_name');
	bookinginfo.email = $F('editbooking_email');
	bookinginfo.phone1_code = $F('editbooking_phone1_code');
	bookinginfo.phone1 = $F('editbooking_phone1');
	bookinginfo.booking_price = $F('editbooking_booking_price');
	bookinginfo.booking_nights = $F('editbooking_booking_nights');
	
	bookinginfo.user_language = $F('editbooking_user_language');
	bookinginfo.street1 = $F('editbooking_street1');
	bookinginfo.street2 = $F('editbooking_street2');
	bookinginfo.city = $F('editbooking_city')
	bookinginfo.county = $F('editbooking_county')
	bookinginfo.postalcode = $F('editbooking_postalcode')
	bookinginfo.country = $F('editbooking_country')
	
	bookinginfo.comments = $F('editbooking_comments')
	
	var cDate = new Date();
	cDate.setDate(1);
	cDate.setFullYear(date_from[0]);
	cDate.setMonth(date_from[1] - 1);
	cDate.setDate((date_from[2] - 0) + (bookinginfo.booking_nights - 0));
	bookinginfo.date_to = cDate.getFullYear() + '-' + (cDate.getMonth() + 1) + '-' + cDate.getDate();
	
	cDate.setDate(1);
	cDate.setFullYear(date_from[0]);
	cDate.setMonth(date_from[1] - 1);
	cDate.setDate(date_from[2]);
	var night = 0;
	do {
		var key = cDate.getFullYear() + '-' + (cDate.getMonth() + 1) + '-' + cDate.getDate();
		if (cCalendarDate.calendar.dates[key] == undefined) {
			break;
		}
		cDateCal = cCalendarDate.calendar.dates[key];
		if (night < bookinginfo.booking_nights) {
			cDateCal.setBookingInfo(bookinginfo);
			cDateCal.setState(bookingstate);
		} else if (cDateCal.getBookingReference() == bookinginfo.booking_reference) {
			cDateCal.setBookingInfo({
				date: cDateCal.getBookingInfo().date,
				state: 1
			});
			cDateCal.setState(1);
		} else {
			break;
		}
		night++;
		cDate.setDate(cDate.getDate() + 1);
	} while(1);
}