YAHOO.namespace ("bookexam.calendar");
var isIE = (window.ActiveXObject);
var bgIframe;
var firstDateContainer;
var secondDateContainer;
var thirdDateContainer;
var opening	= false;

function init() {
	
	this.firstDate		= document.getElementById ('eyeexam_booking_1st_date');
	this.firstDateBtn	= document.getElementById ('eyeexam_1st_date_btn');
	
	this.secondDate		= document.getElementById ('eyeexam_booking_2nd_date');
	this.secondDateBtn	= document.getElementById ('eyeexam_2nd_date_btn');
	
	this.thirdDate		= document.getElementById ('eyeexam_booking_3rd_date');
	this.thirdDateBtn	= document.getElementById ('eyeexam_3rd_date_btn');
	
	if (isIE)
	{
		bgIframe				= document.createElement ('iframe');
		bgIframe.className		= 'hoverIframe';
		bgIframe.frameBorder	= '0';
		bgIframe.scrolling		= 'no';
		document.body.appendChild (bgIframe);
	}
	
	firstDateContainer	= document.createElement ('div');
	firstDateContainer.setAttribute ('id',		'cal_1stdate');
	firstDateContainer.className	= 'hoverCalendar';
	document.body.appendChild (firstDateContainer);
	
	secondDateContainer	= document.createElement ('div');
	secondDateContainer.setAttribute ('id',		'cal_2nddate');
	secondDateContainer.className	= 'hoverCalendar';
	document.body.appendChild (secondDateContainer);
	
	thirdDateContainer	= document.createElement ('div');
	thirdDateContainer.setAttribute ('id',		'cal_3rddate');
	thirdDateContainer.className	= 'hoverCalendar';
	document.body.appendChild (thirdDateContainer);
	
	var myDate=new Date();
	myDate.setFullYear(document.getElementById ('open_date_year').value,document.getElementById ('open_date_month').value,document.getElementById ('open_date_day').value);
	
	YAHOO.bookexam.calendar.firstdate			= new YAHOO.widget.Calendar("YAHOO.bookexam.calendar.firstdate","cal_1stdate");
	YAHOO.bookexam.calendar.firstdate.minDate	= YAHOO.widget.DateMath.add(myDate, YAHOO.widget.DateMath.DAY, 1);
	YAHOO.bookexam.calendar.firstdate.onSelect	= setFirstDate;
	YAHOO.bookexam.calendar.firstdate.render();
	
	YAHOO.bookexam.calendar.seconddate			= new YAHOO.widget.Calendar("YAHOO.bookexam.calendar.seconddate","cal_2nddate");
	YAHOO.bookexam.calendar.seconddate.minDate	= YAHOO.widget.DateMath.add(myDate, YAHOO.widget.DateMath.DAY, 1);
	YAHOO.bookexam.calendar.seconddate.onSelect	= setSecondDate;
	YAHOO.bookexam.calendar.seconddate.render();
	
	YAHOO.bookexam.calendar.thirddate			= new YAHOO.widget.Calendar("YAHOO.bookexam.calendar.thirddate","cal_3rddate");
	YAHOO.bookexam.calendar.thirddate.minDate	= YAHOO.widget.DateMath.add(myDate, YAHOO.widget.DateMath.DAY, 1);
	YAHOO.bookexam.calendar.thirddate.onSelect	= setThirdDate;
	YAHOO.bookexam.calendar.thirddate.render();
	
	Event.observe (document, 'click', hideCalendars, false);
	
}

function showFirstCalendar () {
	
	opening	= true;
	
	YAHOO.bookexam.calendar.firstdate.oDomContainer.style.display='block';
	YAHOO.bookexam.calendar.seconddate.oDomContainer.style.display='none';
	YAHOO.bookexam.calendar.thirddate.oDomContainer.style.display='none';
	
	var pos = YAHOO.util.Dom.getXY(firstDateBtn);
	YAHOO.util.Dom.setXY(YAHOO.bookexam.calendar.firstdate.oDomContainer, [pos[0],pos[1]+firstDateBtn.offsetHeight+1]);
	
	if (isIE)
	{
		bgIframe.style.left		= pos[0] + 'px';
		bgIframe.style.top		= (pos[1]+firstDateBtn.offsetHeight+1) + 'px';
		bgIframe.style.width	= firstDateContainer.offsetWidth  + 'px';
		bgIframe.style.height	= firstDateContainer.offsetHeight + 'px';
		bgIframe.style.display	= 'block';
	}
	
}

function showSecondCalendar () {
	
	opening	= true;
	
	YAHOO.bookexam.calendar.firstdate.oDomContainer.style.display='none';
	YAHOO.bookexam.calendar.seconddate.oDomContainer.style.display='block';
	YAHOO.bookexam.calendar.thirddate.oDomContainer.style.display='none';
	
	var pos = YAHOO.util.Dom.getXY(secondDateBtn);
	YAHOO.util.Dom.setXY(YAHOO.bookexam.calendar.seconddate.oDomContainer, [pos[0],pos[1]+secondDateBtn.offsetHeight+1]);
	
	if (isIE)
	{
		bgIframe.style.left		= pos[0] + 'px';
		bgIframe.style.top		= (pos[1]+secondDateBtn.offsetHeight+1) + 'px';
		bgIframe.style.width	= secondDateContainer.offsetWidth  + 'px';
		bgIframe.style.height	= secondDateContainer.offsetHeight + 'px';
		bgIframe.style.display	= 'block';
	}
	
}

function showThirdCalendar () {
	
	opening	= true;
	
	YAHOO.bookexam.calendar.firstdate.oDomContainer.style.display='none';
	YAHOO.bookexam.calendar.seconddate.oDomContainer.style.display='none';
	YAHOO.bookexam.calendar.thirddate.oDomContainer.style.display='block';
	
	var pos = YAHOO.util.Dom.getXY(thirdDateBtn);
	YAHOO.util.Dom.setXY(YAHOO.bookexam.calendar.thirddate.oDomContainer, [pos[0],pos[1]+thirdDateBtn.offsetHeight+1]);
	
	if (isIE)
	{
		bgIframe.style.left		= pos[0] + 'px';
		bgIframe.style.top		= (pos[1]+thirdDateBtn.offsetHeight+1) + 'px';
		bgIframe.style.width	= thirdDateContainer.offsetWidth  + 'px';
		bgIframe.style.height	= thirdDateContainer.offsetHeight + 'px';
		bgIframe.style.display	= 'block';
	}
	
}

function hideCalendars ()
{
	
	if (opening	== false)
	{
		
		YAHOO.bookexam.calendar.firstdate.oDomContainer.style.display='none';
		YAHOO.bookexam.calendar.seconddate.oDomContainer.style.display='none';
		YAHOO.bookexam.calendar.thirddate.oDomContainer.style.display='none';
		
		if (isIE)
		{
			bgIframe.style.display	= 'none';
		}
		
	}
	else
	{
		
		opening	= false;
		
	}
	
}

function setFirstDate ()
{
	
	var date1			= YAHOO.bookexam.calendar.firstdate.getSelectedDates()[0];
	firstDate.value		= formDate (date1);
	YAHOO.bookexam.calendar.firstdate.oDomContainer.style.display='none';
	
	if (isIE)
	{
		bgIframe.style.display	= 'none';
	}
	
}

function setSecondDate ()
{
	
	var date2			= YAHOO.bookexam.calendar.seconddate.getSelectedDates()[0];
	secondDate.value	= formDate (date2);
	YAHOO.bookexam.calendar.seconddate.oDomContainer.style.display='none';
	
	if (isIE)
	{
		bgIframe.style.display	= 'none';
	}
	
}

function setThirdDate ()
{
	
	var date3			= YAHOO.bookexam.calendar.thirddate.getSelectedDates()[0];
	thirdDate.value		= formDate (date3);
	YAHOO.bookexam.calendar.thirddate.oDomContainer.style.display='none';
	
	if (isIE)
	{
		bgIframe.style.display	= 'none';
	}
	
}

function formDate (date)
{
	
	var day		= date.getDate ();
	
	if (day < 10)
	{
		day		= '0' + day;
	}
	
	var month	= date.getMonth () + 1;
	
	if (month < 10)
	{
		month	= '0' + month;
	}
	
	return day + '/' + month + '/' + date.getFullYear ();
	
}

YAHOO.util.Event.addListener(window, "load", init);