// JavaScript Document

/* bookings admin js */
function currency(){
	var currency = $$('.currency input, input.currency');
	currency.each(function(el){
		el.addEvent('blur',function(e){
			var val = el.value;
			val = val.replace(/[^0-9\.]/g, '');
			val = (val * 1).toFixed(2);
			el.value = val;
		});
	});
}

function choose_property(){
	
}

function calendar_dates(){
	if ($('booking_1')){
		$('booking_1').addEvent('submit',function(e){
			if(!$('startdate').value || !$('enddate').value){
				alert ('Please choose an arrival and departure date');
				var e = new Event(e);
				e.stop();
			}
		})
	}
	var dates = $$('td.calendar_date, td.bookable');
	if (dates.length > 0){
		disable();
		dates.each(function(el){
			el.addEvent('click',function(){
				if(!$('startdate').value){
					// set start date
					disable();
					$('startcell').value=el.getProperty('id');
					$('startdate').value=el.getProperty('rel');
					el.addClass('provisional');
				} else if (!$('enddate').value){
					// set end date
					$('endcell').value=el.getProperty('id');
					if (el.getProperty('id') != $('last_available_date').value){
						alert ('Whoops! You appear to have chosen a booking over somebody else\'s booking. We\'ve taken the liberty of choosing \'' + $($('last_available_date').value).getProperty('rel') + '\' for you.');
						var lastblock = $($('last_available_date').value);
					} else var lastblock = el;
					$('enddate').value=lastblock.getProperty('rel');
					lastblock.addClass('provisional');
					set_date_orders();
					remove_cell_events();
					//$('book_now').removeProperty('disabled');
				} else {
					clearcells();
					el.addClass('provisional');
					$('startcell').value=el.getProperty('id');
					$('endcell').value='';
					$('startdate').value=el.getProperty('rel');
					$('enddate').value = '';
					disable();
					start_all_cell_events();
				}
			});
			el.addEvent('mouseover', cell_start_events);
		});
	}
}

function start_all_cell_events(){
	var dates = $$('td.calendar_date');
	dates.each(function(el){
		el.addEvent('mouseover', cell_start_events);
	});
}

function cell_start_events(){	
	if (!this.hasClass('calendar_booked') || this.hasClass('bookable')){
		this.setStyle('cursor','pointer');
		var start = $('startcell').value;
		var end = $('endcell').value;
		if (start && !end){
			cellstart = $('startcell').value.replace('cell_', '');
			thiscell = this.getProperty('id').replace('cell_', '');
			if (thiscell != cellstart){
				clearcells();
				thiscell = thiscell * 1;
				cellstart = cellstart * 1;
				if (thiscell > cellstart){
					for (a=cellstart; a<=thiscell; a++){
						currcell = 'cell_' + a;
						if ($(currcell)){
							$(currcell).addClass('provisional');
							$('last_available_date').value = currcell;
						}else{
							//$('enddate').value = $('cell_' + (a-1)).getProperty('rel');
							break;
						}
					}
				}else{
					for (a=cellstart; a>=thiscell; a--){
						currcell = 'cell_' + a;
						if ($(currcell)){
							$(currcell).addClass('provisional');
							$('last_available_date').value = currcell;
						}else{
							//$('enddate').value = $('cell_' + (a-1)).getProperty('rel');
							break;
						}
					}
				}
			}
		}
	}
}

function remove_cell_events(){
	var dates = $$('td.calendar_date');
	dates.each(function(el){
		el.removeEvents('mouseover',cell_start_events);
	});
}

function clearcells(){
	var cells = $$('td.calendar_date, td.bookable');
	cells.each(function(el){
		el.removeClass('provisional');
	});
}

function to_date(val){
	var splt = val.split("/");
	return (splt[2] + splt[1] + splt[0]);
}

function set_date_orders(){
	var startdate = $('startdate').value;
	var enddate = $('enddate').value;
	/*var start_expl = startdate.split("/");
	var end_expl = enddate.split("/");
	var start = start_expl[2] + start_expl[1] + start_expl[0];
	var end = end_expl[2] + end_expl[1] + end_expl[0];*/
	var start = to_date(startdate);
	var end = to_date(enddate);
	if (start > end){
		$('startdate').value = enddate;
		$('enddate').value = startdate;
	}
	// var req = new Request.HTML({});
	var url = 'aj.php';
	var data = Object.toQueryString({property: $('property').value, startdate: $('startdate').value, enddate: $('enddate').value}); 
	var myAjax = new Ajax(url, {method: 'post', data: data, update: $('total')}).request();
}

function disable(){
	//$('book_now').setProperty('disabled', 'disabled');
}

window.addEvent('domready',function(){
	currency();
	calendar_dates();
	choose_property();
});

