YAHOO.util.Event.onDOMReady(function () {
	var oCalendarMenu = new Array();
	var oButton = new Array();
	var strButtonID, strButtonContainer, strCalendarMenuOverlayID, strCalendarMenuID;
	var strButtonCalendar, strDayID, strMonthID, strYearID;
	var strPrefix;
	
	for (var intColumn=0; intColumn<2;intColumn++) {
		if (intColumn==0)
			strPrefix = "start";
		else
			strPrefix = "end";
		
		oCalendarMenu[intColumn] = new Array();
		oButton[intColumn] = new Array();
	
		for (var intService=0;intService<6;intService++) {
			
			strButtonID = "pickerentry" + intService + strPrefix + "date";
			strButtonContainer = "entry" + intService + strPrefix + "date";
			strCalendarMenuOverlayID = "menuentry" + intService + strPrefix + "date";
			strCalendarMenuID = "containerentry" + intService + strPrefix + "date";
			strButtonCalendar = "buttonentry" + intService + strPrefix + "date";
			strDayID = "entryday" + strPrefix + intService + "";
			strMonthID = "entrymonth" + strPrefix + intService + "";
			strYearID = "entryyear" + strPrefix + intService + "";

			// Create an Overlay instance to house the Calendar instance
			oCalendarMenu[intColumn][intService] = new YAHOO.widget.Overlay(strCalendarMenuOverlayID, { visible: false });

			// Create a Button instance of type "menu"

			oButton[intColumn][intService] = new YAHOO.widget.Button({ 
												type: "menu", 
												id: strButtonID, 
												label: "Choose A Date", 
												menu: oCalendarMenu[intColumn][intService], 
												container: strButtonContainer });
			
			var arrAppendVar = new Array();
			arrAppendVar[0] = intColumn;
			arrAppendVar[1] = intService;
			arrAppendVar[2] = strCalendarMenuID;
			
			var arrClickVar = new Array();
			arrClickVar[0] = intColumn;
			arrClickVar[1] = intService;
			arrClickVar[2] = strButtonCalendar; 
			arrClickVar[3] = strDayID;
			arrClickVar[4] = strMonthID;
			arrClickVar[5] = strYearID;
			
			oButton[intColumn][intService].on("appendTo", onAppendTo, arrAppendVar);
			oButton[intColumn][intService].on("click", onButtonClick, arrClickVar);
		}
	}
	
	
	function onAppendTo(pa_sType, pa_aArgs) {

		/*
			 Create an empty body element for the Overlay instance in order 
			 to reserve space to render the Calendar instance into.
		*/
		var intColumnIndex, intServiceIndex, strCalendarMenuID;
		intColumnIndex = pa_aArgs[0];
		intServiceIndex = pa_aArgs[1];
		strCalendarMenuID = pa_aArgs[2]; 
		
		oCalendarMenu[intColumnIndex][intServiceIndex].setBody("&#32;");

		oCalendarMenu[intColumnIndex][intServiceIndex].body.id = strCalendarMenuID;
		// Render the Overlay instance into the Button's parent element

		oCalendarMenu[intColumnIndex][intServiceIndex].render(this.get("container"));

	}

	/*
		Add a "click" event listener that will render the Overlay, and 
		instantiate the Calendar the first time the Button instance is 
		clicked.
	*/
	
	function onButtonClick(pc_sType, pc_aArgs) {
		
		/*
			 Create a Calendar instance and render it into the body 
			 element of the Overlay.
		*/
		var intColumnIndex, intServiceIndex, strButtonCalendarID, strDaySelectID, strMonthSelectID, strYearSelectID;
		
		intColumnIndex = pc_aArgs[0];
		intServiceIndex = pc_aArgs[1];
		strButtonCalendarID = pc_aArgs[2]; 
		strDaySelectID = pc_aArgs[3];
		strMonthSelectID = pc_aArgs[4];
		strYearSelectID = pc_aArgs[5];		
		
		var oCalendar = new YAHOO.widget.Calendar(strButtonCalendarID, oCalendarMenu[intColumnIndex][intServiceIndex].body.id, {navigator:true});

		oCalendar.render();


		/* 
			Subscribe to the Calendar instance's "changePage" event to 
			keep the Overlay visible when either the previous or next page
			controls are clicked.
		*/

		oCalendar.changePageEvent.subscribe(function () {
			
			window.setTimeout(function () {

				oCalendarMenu[intColumnIndex][intServiceIndex].show();
			
			}, 0);
		
		});


		/*
			Subscribe to the Calendar instance's "select" event to 
			update the month, day, year form fields when the user
			selects a date.
		*/

		oCalendar.selectEvent.subscribe(function (p_sType, p_aArgs) {

			var aDate;

			if (p_aArgs) {
			aDate = p_aArgs[0][0];        
			var year = aDate[0], month = aDate[1], day = aDate[2];
			var selMonth = document.getElementById(strMonthSelectID);
			var selDay = document.getElementById(strDaySelectID);
			var selYear = document.getElementById(strYearSelectID);
			selMonth.selectedIndex = month;
			selDay.selectedIndex = day;
			for (var y=0;y<selYear.options.length;y++) {
				if (selYear.options[y].text == year) {
					selYear.selectedIndex = y;
					break;
				}
			}
			}
			oCalendarMenu[intColumnIndex][intServiceIndex].hide();
		
		});
		

		/*
			 Unsubscribe from the "click" event so that this code is 
			 only executed once
		*/

		this.unsubscribe("click", onButtonClick);
	
	}
});
