var isShowCalendar1 = true;
var isShowCalendar2 = true;

function checkDate(num){
	if(num < 10){
		return "0" + num;
	}else{
		return "" + num;
	}
}

function showCalendar(num, name_date){
	var isShowCalc = true;
	var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom;
	var dialog, calendar, name_dial = "d"+num, name_cal="cl"+num, name_btn="show"+num;
	if(num == 1){
		dialog = d1;
		calendar = cl1;
		isShowCalc = isShowCalendar1;
	}else
	if(num == 2){
		dialog = d2;
		calendar = cl2;
		isShowCalc = isShowCalendar2;
	}
	
	if(isShowCalc){
		
		var showBtn = Dom.get(name_btn);
		// Lazy Dialog Creation - Wait to create the Dialog, and setup document click listeners, until the first time the button is clicked.
		if (!dialog) {
			// Hide Calendar if we click anywhere in the document other than the calendar
			Event.on(document, "click", function(e) {
				var el = Event.getTarget(e);
				var dialogEl = dialog.element;
				if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != showBtn && !Dom.isAncestor(showBtn, el)) {
					dialog.hide();
				}
			});
			dialog = new YAHOO.widget.Dialog(name_dial, {
				visible:false,
				context:[name_btn, "tl", "bl"],
				draggable:true,
				close:true
			});
			dialog.setHeader(cal_title);
			dialog.setBody('<div id="'+name_cal+'"></div>');
			dialog.render(document.body);
			dialog.showEvent.subscribe(function() {
				if (YAHOO.env.ua.ie) {
					// Since we're hiding the table using yui-overlay-hidden, we 
					// want to let the dialog know that the content size has changed, when
					// shown
					dialog.fireEvent("changeContent");
				}
			});
		}
		// Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
		if (!calendar) {
			calendar = new YAHOO.widget.CalendarGroup(name_cal, {PAGES:1,iframe:false,hide_blank_weeks:true,LOCALE_WEEKDAYS:"short",START_WEEKDAY:1,navigator:navConfig});
			calendar.cfg.setProperty("MONTHS_LONG",monthes);
			calendar.cfg.setProperty("WEEKDAYS_SHORT", days);
			calendar.render();
			calendar.selectEvent.subscribe(function() {
				if (calendar.getSelectedDates().length > 0) {
					var selDate = calendar.getSelectedDates()[0];
					// Pretty Date Output, using Calendar's Locale values: Friday, 8 February 2008
					//var wStr = calendar.cfg.getProperty("WEEKDAYS_LONG")[selDate.getDay()];
					var dStr = selDate.getDate();
					var mStr = calendar.cfg.getProperty("MONTHS_LONG")[selDate.getMonth()].substring(0, 3);
					var mNum = selDate.getMonth() + 1;
					var yStr = selDate.getFullYear();
					Dom.get(name_date).value = dStr + " " + mStr + " " + yStr;
					if(num == 1)
						Dom.get("departureDate").value = yStr + "-" + checkDate(mNum) + "-" + checkDate(dStr);
					else if(num == 2)
						Dom.get("returnDate").value = yStr + "-" + checkDate(mNum) + "-" + checkDate(dStr);
				} else {
					Dom.get(name_date).value = "";
					if(num == 1)
						Dom.get("departureDate").value = "";
					else if(num == 2)
						Dom.get("returnDate").value = "";
				}
				dialog.hide();
			});
			calendar.renderEvent.subscribe(function() {
				// Tell Dialog it's contents have changed, which allows 
				// container to redraw the underlay (for IE6/Safari2)
				dialog.fireEvent("changeContent");
			});
		}
		var seldate = calendar.getSelectedDates();
		//alert("seldate="+seldate);
		if (seldate.length > 0) {
			// Set the pagedate to show the selected date if it exists
			calendar.cfg.setProperty("pagedate", seldate[0]);
			calendar.render();
		}
		if(num == 1){
			d1 = dialog;
			cl1 = calendar;
		}else
		if(num == 2){
			d2 = dialog;
			cl2 = calendar;
		}
		dialog.show();
	
	}
}