var booking_now = false;
var total_days;
var start_date;
var end_date;
var date_details;
function handle_booking(cell){
	var b = document.getElementById('book_step3');
	if(b.style.backgroundColor == '#dff7ff' || b.style.backgroundColor =='rgb(223, 247, 255)'){
		alert('You have already selected dates.\n\n To reset the dates, Please Start again from Step 1');
	}
				
	cellstate = cell.className;
	if(booking_now != "fin"){
	if(cellstate == 'unavail'){
		alert("Sorry, this date is not available!");
		return;
		}
		if(cellstate == 'avail' || cellstate == 'unavailnight'){
			if(!booking_now){
				// check that arrival is allowed on this date
				if(date_details.data[cell.axis].closed_to_arrival == 1){
					alert('Sorry, you can stay during, but not arrive on this day. \nIf you wish to stay on this day, you must arrive on an earlier date.');
					return;
					}
				if(cellstate == 'avail'){
					cell.className='book_start';
					} else {
					cell.className='unavailnight_chk';
					}
				start_date = Number(cell.id);
				var b = document.getElementById('book_step1');
				b.style.backgroundColor = 'DFF7FF';
				var b = document.getElementById('book_step1_content');
				b.innerHTML = "<p>Click here to start again.<p>";
				var b = document.getElementById('book_step2');
				b.style.backgroundColor = '#DFF7FF';
				var b = document.getElementById('book_step2_content');
				b.style.display = 'block';
				booking_now = true;
				} else {
				end_date = Number(cell.id);
				if(test_dates()){
					// reset the classes of start_date and end_date cells (in case they were flipped by user clicking end date first)
					document.getElementById(start_date).className='book_start';
					if(document.getElementById(end_date).className == 'avail'){
						document.getElementById(end_date).className='book_end';
						} else {
						document.getElementById(end_date).className='unavailnight_chk';
						}
					var total_price = new Number();
					var basic_price = Math.round(fill_dates());	
					
					var b = document.getElementById('book_step3');
					b.style.backgroundColor = '#DFF7FF';
					var b = document.getElementById('book_step3_content');
					b.style.display = 'block';
					booking_now = 'fin';
					if(basic_price == 0){
						document.getElementById('total_price').value = 0;
						document.getElementById('total_nights_price').value = 0;
						document.getElementById('total_days_price_display').innerHTML = '';
						document.getElementById('total_price_display').innerHTML = 'P.O.A';
						} else {
						basic_price = basic_price.toFixed(2);
						var exit_cleaning_price = document.getElementById('exit_clean_price').value;
						if(exit_cleaning_price > 0){
							total_price = (Number(basic_price) + Number(exit_cleaning_price)).toFixed(2);
							} 
						document.getElementById('nt_price').innerHTML = '';
						document.getElementById('total_price').value = total_price;
						document.getElementById('total_nights_price').value = basic_price;
						document.getElementById('total_days_price_display').innerHTML = '$'+basic_price;
						document.getElementById('total_price_display').innerHTML = '$'+total_price;
						}
					} else {
					end_date = false;
					}
				}
			}
		}
	}

function test_dates(){
	// first check that end_date is greater than start_date
	// if not, restart the booking sequence at end_date
	if(end_date < start_date){
		
		var e = document.getElementById(end_date);
		if(date_details.data[e.axis].closed_to_arrival == 1){
			alert('Sorry, you can stay during, but not arrive on that day. \nIf you wish to stay on that day, you must arrive at an earlier date.');
			return false;
			}
		var flip = flip_dates();
		}
	if(document.getElementById(start_date).className == 'unavailnight_chk'){
		alert('Sorry, some of the dates you have selected are not available.');
		return false;
		}
	if(document.getElementById(start_date).className == 'unavailnight'){
		var flip = flip_dates();
		alert('Sorry, some of the dates you have selected are not available.');
		return false;
		}

	for(var i=(start_date+1); i < end_date; i++){
		var c=document.getElementById(i);
		if(c.className == 'unavail' || c.className == 'unavailnight'){
			if(flip){ // if dates were flipped, flip back before returning
				flip_dates();
				}
			alert('Sorry, some of the dates you have selected are not available.');
			return false;
			}
		}
	// check that minumum days were booked
	// get start_date cell
	var s = document.getElementById(start_date);
	var min_days = date_details.data[s.axis].minimum_stay;
	var num_days = i-start_date;
	if(num_days < min_days){
		alert('Sorry, you must book at least '+min_days+' nights.');
		if(flip){ // if dates were flipped, flip back before returning
			flip_dates();
			}
		return false;
		}
	return true;
	}

function fill_dates(){
	// as we fill the dates, we will also calculate the rates.... WOW!
	// to do this we will keep track of 2 vars - daily rates and weekly rates (in case booking is 7 days or over)
	// NEW: 26 / 6 / 08 - if we hit a zero dollar value in any day of the selection, the booking becomes P.O.A (Price On Application)
	var POA = false;
	var daily_rates_total = new Number;
	var weekly_rates_total = new Number;
	//
	var maindiv = document.getElementById('booking');
	var allcells = maindiv.getElementsByTagName('td');
	for(var i=(start_date); i < end_date; i++){
		var c=document.getElementById(i);
		if(i>start_date){
			c.className = 'book';
			}
		if(c.abbr == 'Fr' || c.abbr == 'Sa'){
			//alert(c.abbr+' weekend! yay!');
			var dtmp = Number(date_details.data[c.axis].weekend_rate);
			daily_rates_total += dtmp;
			} else {
			var dtmp = Number(date_details.data[c.axis].nightly_rate);
			daily_rates_total += dtmp;
			}
		var wtmp = Number(date_details.data[c.axis].weekly_rate/7);
		weekly_rates_total += wtmp;
		if(dtmp == 0 && wtmp == 0){
			POA = true;
			}
		}
	total_days = i-start_date;
	document.getElementById('total_days_display').innerHTML = total_days;
	//alert('Weekly Rate Total: '+weekly_rates_total+'\nDaily Rate Total: '+daily_rates_total);
	if(POA){
		return 0;
		}
	if(total_days >= 7){
		return weekly_rates_total;
		} else {
		return daily_rates_total;
		}
	}
function flip_dates(){
	// reverse the position of start_date and end_date
	var flip = start_date;
	start_date = end_date;
	end_date = flip;
	return true;
	}
function cancel_booking(){
	if(!end_date){
		document.getElementById(start_date).className=(document.getElementById(start_date).className=='unavailnight_chk')?"unavailnight":"avail";
		} else {
			for(var i=(start_date); i <= end_date; i++){
			var c=document.getElementById(i);
			c.className = (c.className=='unavailnight_chk')?"unavailnight":"avail";
			}
		}
	start_date = false;
	end_date = false;
	booking_now = false;
	var b = document.getElementById('book_step1');
	b.style.backgroundColor = '#DFF7FF';
	var b = document.getElementById('book_step1_content');
	b.innerHTML = "<p>First choose a starting date...<p>";
	var b = document.getElementById('book_step2');
	b.style.backgroundColor = '#f4f4f4';
	var b = document.getElementById('book_step2_content');
	b.style.display = 'none';
	var b = document.getElementById('book_step3');
	b.style.backgroundColor = '#f4f4f4';
	var b = document.getElementById('book_step3_content');
	b.style.display = 'none';
	var b = document.getElementById('book_step4');
	b.style.backgroundColor = '#f4f4f4';
	var b = document.getElementById('book_step4_content');
	b.style.display = 'none';
	document.getElementById('num_adults').value = '';
	document.getElementById('num_children').value = '';	
}

function make_booking(){
	//alert();
	var f = document.getElementById('make_booking_form');
	var m_y = document.getElementById(start_date).parentNode.parentNode.parentNode.id;
	var m_y = m_y.split(" ");
	var sym = m_y[1] + '' + m_y[2];
	var sday = padLeft(document.getElementById(start_date).childNodes[0].nodeValue,'0',2);
	document.getElementById('start_date').value = sym+''+sday;
	document.getElementById('total_days').value = total_days;
	f.submit();

	}
	
function padLeft(val, ch, num) {
            var re = new RegExp(".{" + num + "}$");
            var pad = "";
            if (!ch) ch = " ";
            do  {
                pad += ch;
            }while(pad.length < num);
            return re.exec(pad + val);
        }
function popmove(){
	popmove.prototype._targetDiv;
	popmove.prototype._parentDiv;
	popmove.prototype._IE = document.all?true:false;
	}
popmove.prototype.init = function(t,p){
	var me = this;
	this._targetDiv = t;
	this._parentDiv = p;
	if(this._IE){
		this._parentDiv.attachEvent("onmousemove",function(e){me.movepopup(e)});
		this._parentDiv.attachEvent("onmouseover",function(){me.showpopup()});
		this._parentDiv.attachEvent("onmouseout",function(){me.hidepopup()});
		} else {
		this._parentDiv.addEventListener("mousemove",function(e){me.movepopup(e)},false);
		this._parentDiv.addEventListener("mouseover",function(){me.showpopup()},false);
		this._parentDiv.addEventListener("mouseout",function(){me.hidepopup()},false);
		}
	}
popmove.prototype.showpopup = function(){
	// get date details
	var m_y = this._parentDiv.parentNode.parentNode.parentNode.id;
	var m_y = m_y.split(" ");
	var dd = '<h1>' + m_y[0] + ' ' + this._parentDiv.childNodes[0].nodeValue + ', ' +  m_y[1] + '</h1>';
	if(date_details.data[this._parentDiv.axis].closed_to_arrival != 0){
		dd += '<i>No arrivals on this date</i><br />';
		}
	if(date_details.data[this._parentDiv.axis].minimum_stay != 0){
		dd += '<b>Minimum Stay:</b> ' + date_details.data[this._parentDiv.axis].minimum_stay + ' nights <br />';
		}
	// we need to look at all rates for this day, if any are 0, it will be displayed as POA
	var wrt = date_details.data[this._parentDiv.axis].weekly_rate;
	var nrt = date_details.data[this._parentDiv.axis].nightly_rate;
	var ert = date_details.data[this._parentDiv.axis].weekend_rate;
	if(wrt == 0 || nrt == 0 || ert == 0){
			var nt_price_hidden = document.getElementById('nt_price_hidden').value;
			if(nt_price_hidden == ''){
				dd += '<b>Rates:</b> P.O.A<br />';
			}
			else{
				dd += '<strong>'+nt_price_hidden+'</strong><br />';
			}
			} else {
			dd += '<b>Weekly Rate:</b> $' + date_details.data[this._parentDiv.axis].weekly_rate + '<br />';
			dd += '<b>Nightly Rate:</b> $' + date_details.data[this._parentDiv.axis].nightly_rate + '<br />';
			dd += '<b>Weekend Rate:</b> $' + date_details.data[this._parentDiv.axis].weekend_rate + '<br />';
			}
	if(date_details.data[this._parentDiv.axis].bond > 0){
		dd += '<b>Security Bond:</b> $' + date_details.data[this._parentDiv.axis].bond + '<br />';
		}
	if(date_details.data[this._parentDiv.axis].linen_hire != 0){
		dd += '<b>Linen Hire:</b> $' + date_details.data[this._parentDiv.axis].linen_hire + '<br />';
		}
	if(date_details.data[this._parentDiv.axis].extra_person != 0){
		dd += 'Note: pricing based on up to ' + date_details.data[this._parentDiv.axis].extra_person_thresh + ' people staying. Extra people at $' + date_details.data[this._parentDiv.axis].extra_person + ' per head.<br />';
		}
	if(date_details.data[this._parentDiv.axis].events != ''){
		dd += '<p class=\"events\">' + date_details.data[this._parentDiv.axis].events + '</p>';
		}
	this._targetDiv.innerHTML = dd;

	this._targetDiv.style.visibility = 'visible';
	}
popmove.prototype.hidepopup = function(){
	this._targetDiv.style.visibility = 'hidden';
	}
	

popmove.prototype.movepopup = function(e){
// If NS -- that is, !IE -- then set up for mouse capture
if (!this._IE) document.captureEvents(Event.MOUSEMOVE)
if (this._IE) { // grab the x-y pos.s if browser is IE
	//alert(document.documentElement.scrollTop);
    var x = event.clientX + document.documentElement.scrollLeft;
	x += 10;
    var y = event.clientY + document.documentElement.scrollTop;
	 // alert(document.body.scrollTop);
	  } else {  // grab the x-y pos.s if browser is NS
		var x = e.pageX
		var y = e.pageY
	  }  
	if(this._targetDiv.currentStyle){ // ie
		//var height = parseInt(this._targetDiv.currentStyle.height);
		var height = 155; // because ie is retarded and returns 'auto' for currentStyle.height.
		var width = parseInt(this._targetDiv.currentStyle.width);
		if((x + width + 30) > document.body.offsetWidth){
			x =	document.body.offsetWidth - width - 30;
			}

		} else {
		var height = parseInt(getComputedStyle(this._targetDiv,'').height);
		var width = parseInt(getComputedStyle(this._targetDiv,'').width);
		if((x + width + 30) > window.innerWidth){
			x =	window.innerWidth - width - 30;
			}
		}

	y -= (height+13);
	this._targetDiv.style.left = x+'px';
	this._targetDiv.style.top = y+'px';
	}

	
	
	
function test_for_num_ppl(){
		var noOfDays = document.getElementById('total_days_display').innerHTML;
		var a = document.getElementById('num_adults').value;
		var c = document.getElementById('num_children').value;
		var max_people = document.getElementById('sleeps').value;
		var extra_person_thresh = document.getElementById('extra_person_thresh').value;
		var extra_person_charge = document.getElementById('extra_person_charge').value;
		if(Number(a) + Number(c) > max_people){
			var b = document.getElementById('book_step4');
			b.style.backgroundColor = '#f4f4f4';
			var b = document.getElementById('book_step4_content');
			b.style.display = 'none';
			alert('Sorry, no more than ' + max_people + ' people can stay.');
			return;
			}
		var total_price = Number(document.getElementById('total_nights_price').value);
		var exit_clean_price = Number(document.getElementById('exit_clean_price').value);
		var extra_people_display = document.getElementById('extra_people_display');
		if(Number(a) > 0){
		if(total_price > 0){
			// first calculate extra person charge if over threshhold
			if(extra_person_thresh > 0 && (Number(a) + Number(c)) > extra_person_thresh){
				var extra_people = ((Number(a) + Number(c))-extra_person_thresh);
				var extra_people_total_charge = extra_people * extra_person_charge * Number(noOfDays) ;
				total_price += exit_clean_price;
				total_price += extra_people_total_charge;
				document.getElementById('total_price_display').innerHTML = '$'+total_price.toFixed(2);		
				document.getElementById('total_extra_people_display').innerHTML = extra_people;
				document.getElementById('total_extra_people_price_display').innerHTML = '$'+ (Number(extra_people_total_charge)).toFixed(2);
				document.getElementById('total_price').value = total_price.toFixed(2);
				extra_people_display.style.display = 'block';
				} else {
				total_price += exit_clean_price;
				document.getElementById('total_price_display').innerHTML = '$'+ Number(total_price).toFixed(2);
				document.getElementById('total_price').value = Number(total_price).toFixed(2);
				extra_people_display.style.display = 'none';
				}
		}
			// now enable step 4
			var b = document.getElementById('book_step4');
			b.style.backgroundColor = '#DFF7FF';
			var b = document.getElementById('book_step4_content');
			b.style.display = 'block';
			} else {
			total_price += exit_clean_price;
			document.getElementById('total_price_display').innerHTML = '$'+ Number(total_price).toFixed(2);
			document.getElementById('total_price').value = Number(total_price).toFixed(2);
			extra_people_display.style.display = 'none';
			var b = document.getElementById('book_step4');
			b.style.backgroundColor = '#f4f4f4';
			var b = document.getElementById('book_step4_content');
			b.style.display = 'none';
			}
		}
