minStayDays = 7; // what is the minimum day stay
document.onkeyup = checkEscape;
function checkEscape(e) // check if the escape key is pressed, if true then hide the calendar;
{
	var target =(e && e.target) || (event && event.srcElement);
	var key =( e && e.keyCode ) || (event && event.keyCode);

	if( key == 27 )
		hideCalendar();
}

function showHideCalendar(clicked)
{
	clickedDate=clicked;
	document.getElementById('cal').style.display='none';
	document.getElementById('cal').style.display='block';
	positionBase(); //position the base div in relation with the coordinates of the mouse cursor	
}
function hideCalendar()
{
	document.getElementById('cal').style.display='none';
}


function calendar()
{
	createBase(); // create the containing div
	manageDates(); // generate the staring and the ending date
	createTable(); // create the calendar table
	createSelect(); // creates the select boxes for choosing the months and years
	createPage(); // generate the boxes with the dates, mark the past dates
}

function createBase()
{
	//check if there is a created element, if 1 then return 1
	if(document.getElementById('cal').childNodes.length>0)
		return true;
	var base=document.createElement('div');
	base.setAttribute('id', 'base');
	document.getElementById('cal').appendChild(base);
}	

function manageDates()
{
	currentDate = new Date();
	try // try/catch statement for setting the start date 
	{
		if(sDate.length>0)
		{
			startDate=new Date(sDate[2], sDate[1], sDate[0]);
		}
	}
	catch(e)
	{
		startDate=new Date(2006,1,1);
	}
	try // try/catch statement for setting the end date
	{
		if(eDate.length>0)
		{
			for (i=0; i<months.length; i++)
			{
				if(months[i]==eDate[1])
				{
					var mon=i;
					break;
				}
			}
			endDate=new Date(eDate[2], mon, eDate[0]);
		}
	}
	catch(e)
	{
		//var cDate=new Date();
		endDate=new Date(currentDate.getFullYear()+2,currentDate.getMonth(),currentDate.getDay());
	}
	return true;
}

function createTable()
{
	var base=document.getElementById('base');
	var table=document.createElement('div');
	table.setAttribute('id', 'calendarTable');
	base.appendChild(table);
	return true;
}

function createSelect()
{
	var select;
	select='<select name="month" id="month" onChange="javascript: createPage(document.getElementById(\'year\').value, this.value)">';
	for(i=0; i<fullMonths.length; i++)
	{	
		if(i==currentDate.getMonth())
			select+='<option value="'+i+'" selected="selected">'+fullMonths[i]+'</option>';		
		else	
			select+='<option value="'+i+'">'+fullMonths[i]+'</option>';		
	}	
	select+='</select>';	
	
	select+='<select name="year" id="year" onChange="javascript: createPage(this.value, document.getElementById(\'month\').value)">';
	for(i=startDate.getFullYear(); i<=endDate.getFullYear(); i++)
	{
		if(i==currentDate.getFullYear())
			select+='<option value="'+i+'" selected="selected">'+i+'</option>';		
		else	
			select+='<option value="'+i+'">'+i+'</option>';		
	}	
	select+='</select>';
	
	select+='<div id="calendarDates"></div>';	
	document.getElementById('base').innerHTML=select;
}

function createPage(year,month)
{
	//if year and month are not set, set them with the current year and month
	if(!year || !month)
	{
		year=currentDate.getFullYear();
		month=currentDate.getMonth();
	}	
	//how days are in the given month
	var days=32-(new Date(year, month, 32).getDate());
	//start generating the output var
	var cPage='<table ><tr>';
	for(i=0; i<shortDays.length; i++)
	{
		cPage+='<td>'+shortDays[i]+'</td>';
	}
	cPage+='</tr>';
	// we have to know on what day of the week the month starts
	firstDay=new Date(year,month, 1).getDay();
	var b=0;
	for(i=1; i<=days; i++)
	{
		var checkHoliday=new Date(year,month, i).getDay();
		if(((i<currentDate.getDate() && month<=currentDate.getMonth()) || month<currentDate.getMonth()) && year<=currentDate.getFullYear())
		{
			
			if(i==1)
			{	
				cPage+='<tr>';
				for(c=1; c<firstDay; c++)
				{	
					cPage+='<td></td>';
					b++;
				}
			}				
			else if(b%7==0)
				cPage+='</tr><tr>'
			
			if( checkHoliday==6 || checkHoliday==0 )
				cPage+='<td class="passed_holiday">'+i+'</td>';	
			else
				cPage+='<td class="passed">'+i+'</td>';

			b++;	
			continue;	
		}
		if(i==1)
		{	
			cPage+='<tr>';
			for(c=1; c<firstDay; c++)
			{	
				cPage+='<td></td>';
				b++;
			}
		}
		else if(b%7==0)
			cPage+='</tr><tr>';
		else if(i==days)	
		{		
			
			if( checkHoliday==6 || checkHoliday==0 )
				cPage+='<td onClick="changeDates(\''+i+'\', \''+((month*1)+1)+'\', \''+year+'\');"><a class="holiday" href="#">'+i+'</a></td>';	
			else
				cPage+='<td onClick="changeDates(\''+i+'\', \''+((month*1)+1)+'\', \''+year+'\');"><a href="#">'+i+'</a></td>';	
			
			cPage+='</tr></table>';
			break;
		}
		if(i==currentDate.getDate() && month==currentDate.getMonth() && year==currentDate.getFullYear())
		{
			if( checkHoliday==6 || checkHoliday==0 )
				cPage+='<td onClick="changeDates(\''+i+'\', \''+((month*1)+1)+'\', \''+year+'\');"><b><a class="holiday" href="#">'+i+'</a></b></td>';
			else
				cPage+='<td onClick="changeDates(\''+i+'\', \''+((month*1)+1)+'\', \''+year+'\');"><b><a href="#">'+i+'</a></b></td>';	
		}	
		else
		{
			if( checkHoliday==6 || checkHoliday==0 )
				cPage+='<td onClick="changeDates(\''+i+'\', \''+((month*1)+1)+'\', \''+year+'\');"><a class="holiday" href="#">'+i+'</a></td>';	
			else
				cPage+='<td onClick="changeDates(\''+i+'\', \''+((month*1)+1)+'\', \''+year+'\');"><a href="#">'+i+'</a></td>';	
		}	
		b++;
	}
	document.getElementById('calendarDates').innerHTML=cPage;
	return true;
}

function changeDates(day,month,year)
{
	if( day<10 )
		day='0'+day;
	if( month<10 )	
		month='0'+month;
		
	if(clickedDate == 'from')
	{
		var newDate=day+'-'+month+'-'+year;
		var DateTo=new Date(year, (month-1), ((day*1)+minStayDays));
		
		if( DateTo.getDate()<10 )
			var newDayTo='0'+DateTo.getDate();
		else
			var newDayTo=DateTo.getDate();
		if( (DateTo.getMonth()+1)<10 )
			var newMonthTo='0'+(DateTo.getMonth()+1);
		else
			var newMonthTo=DateTo.getMonth()+1;
			
		var newDateTo=newDayTo+'-'+newMonthTo+'-'+DateTo.getFullYear();
		document.getElementById('dateFrom').value=newDate;
	}
	else
	{
		var compareDateTo=new Date( year, (month-1), day );
		var currentDateFrom=document.getElementById('dateFrom').value;
		currentDateFrom=currentDateFrom.split(/-/);
		var compareDateFrom=new Date( currentDateFrom[2], (currentDateFrom[1]-1), currentDateFrom[0] );
		
		if( compareDateTo<compareDateFrom )
			var newDateTo=document.getElementById('dateTo').value;
		else	
			var newDateTo=day+'-'+month+'-'+year;
	}

	document.getElementById('dateTo').value=newDateTo;
	hideCalendar();
}

function positionBase()
{
	if( clickedDate=='from' )
		position=findPos( document.getElementById('dateFrom') );
	else
		position=findPos( document.getElementById('dateTo') );
	document.getElementById('base').style.left=position[0]+105+'px';
	document.getElementById('base').style.top=position[1]+'px'; // was +32
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}