var arrivalDate = readCookie('arrivalDate_calendar'+''+readCookie('time'));
var departureDate = null;
var old_id = readCookie('old_id_calendar'+''+readCookie('time'));
var id = null;
var old_color = readCookie('old_color_calendar'+''+readCookie('time'));
var old_color1 = null;
var d= new Date();
function getDates(date,ids)
{
	
	id = ids;
	if(!arrivalDate)
	{
		arrivalDate = date;
		old_id = id;
		old_color = document.getElementById(id).bgColor;
		document.getElementById(id).style.background= '#FF7979';
		document.getElementById(id).style.textDecoration= 'blink';
		document.getElementById(id).title= 'Click again to cancel';
		document.getElementById('arrivalDate').innerHTML = '<p align="center">Arrival Date: '+date+'<br><small>To cancel this date click on it.</small></p>';
		createCookie('time',d.getHours+''+d.getMinutes+''+d.getSeconds(),1);
		createCookie('arrivalDate_calendar'+''+readCookie('time'),arrivalDate,1);
		createCookie('ids_calendar'+''+readCookie('time'),ids,1);
		createCookie('old_id_calendar'+''+readCookie('time'),old_id,1);
		createCookie('old_color_calendar'+''+readCookie('time'),old_color,1);
		
		
	}else
	{
		
		if(id<old_id)
		{
			alert("Please select a correct Departure Date");
		}else if(id==old_id)
		{
			document.getElementById(id).style.background= old_color;
			document.getElementById(id).style.textDecoration= 'none';
			document.getElementById('arrivalDate').innerHTML = '';
			document.getElementById(id).style.cursor= 'pointer';
			document.getElementById(id).title= '';
			arrivalDate = null;
		}
		else
		{
			id = ids;
			departureDate = date;
			old_color1 = document.getElementById(id).bgColor;
			document.getElementById(id).style.background= '#FF7979';
			document.getElementById(id).style.textDecoration= 'blink';
			arrivalDate = readCookie('arrivalDate_calendar'+''+readCookie('time'));
			MOOdalBox.open('index2.php?option=com_calendar&view=booking&a='+arrivalDate+'&d='+departureDate,'Add Booking','500 500');
			
			eraseCookie('arrivalDate_calendar'+''+readCookie('time'));
		}
	}
	
	
	
}
function editdates(id)
{
	if(arrivalDate)
	{
		alert("You cannot select a booked date as departure date.");
	}else
	{
		window.location.href = "index.php?option=com_calendar&view=booking&id="+id;
	}
}
function cleardates()
{
	//empty arrival
	if(document.getElementById(old_id))
	{
		document.getElementById(old_id).style.background= old_color;
		document.getElementById(old_id).style.textDecoration= 'none';
		document.getElementById('arrivalDate').innerHTML = '';
		document.getElementById(old_id).style.cursor= 'pointer';
		document.getElementById(old_id).title= '';
		arrivalDate = null;
	}
	document.getElementById(id).style.background= old_color1;
	document.getElementById(id).style.textDecoration= 'none';
	document.getElementById(id).style.cursor= 'pointer';
	document.getElementById(id).title= '';
	document.getElementById('arrivalDate').innerHTML = '';
	departureDate = null;
	eraseCookie('arrivalDate_calendar'+''+readCookie('time'));
	eraseCookie('old_id_calendar'+''+readCookie('time'));
	eraseCookie('old_color_calendar'+''+readCookie('time'));
	arrivalDate = null;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function loadContent()
{
	if(arrivalDate)
	{
		if(document.getElementById(old_id))
		{
			document.getElementById(old_id).style.background= '#FF7979';
			document.getElementById(old_id).style.textDecoration= 'blink';
			document.getElementById(old_id).title= 'Click again to cancel';
		}
	document.getElementById('arrivalDate').innerHTML = '<p align="center">Arrival Date: '+arrivalDate+'<br><small>To cancel this date click on it.</small></p>';
	}
}
