/** THIS VERSION IS SETUP TO USE 2 SEPERATE DIV CONTAINERS FOR BETTER POSITIONING **/


YAHOO.namespace("example.calendar");

/** SET UP HERE **/
var firstTextBox = "txt_outBound" // the ID of the first text field connected to the calendar (e.g. arrival)
var secondTextBox = "txt_inBound"
var calendarContainer1 = "containerDatePicker1"; // the div where the calendar goes
var calendarContainer2 = "containerDatePicker2"; // the div where the calendar goes

/** SET MIN DATE TO CURRENT DATE (MM/DD/YYYY) **/
var curDate=new Date()
var curYear=curDate.getFullYear()
var curDay=curDate.getDay()
var curMonth=curDate.getMonth()+1
var curDayMonth=curDate.getDate()
var minDate=(curMonth+"/"+curDayMonth+"/"+curYear);
var temp;

function handleCheckIn(type,args,obj) {
	// if user picks txtDate1, txtDate2 and then changes txtDate1, warp txtDate2 to maintain interval
	var utcInterval = 0; // # of milliseconds between dates
	var txtDate1 = document.getElementById(firstTextBox);
	var txtDate2 = document.getElementById(secondTextBox);
	
	var dates = args[0]; 
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];
	
	txtDate1.value = month + "/" + day + "/" + year;
	txtDate1.select();
	
	if (!temp) temp = txtDate1.value;
	if ((txtDate1.value != "mm/dd/yyyy") && (txtDate2.value != "mm/dd/yyyy")) {
		var utc1 = Date.parse(txtDate1.value);
		var utcTemp = Date.parse(temp);
		var utc2 = Date.parse(txtDate2.value);
		utcInterval = utc2 - utcTemp;
		var dt2 = new Date(Date.parse(txtDate1.value) + utcInterval);
		txtDate2.value = dt2.getMonth() + 1 + "/" + dt2.getDate() + "/" + dt2.getFullYear();
	}
	temp = txtDate1.value;
	obj.hide();
}

function handleCheckOut(type,args,obj) {
	var dates = args[0]; 
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];
	
	var txtDate2 = document.getElementById(secondTextBox);
	txtDate2.value = month + "/" + day + "/" + year;
	txtDate2.select();
	obj.hide();
}	

function updateCal() {
	var txtDate1 = document.getElementById(firstTextBox);
	var txtDate2 = document.getElementById(secondTextBox);
	
	if (txtDate1) {
		if (txtDate1.value != "mm/dd/yyyy") {
			if (YAHOO.example.calendar.cal1) {
				var firstDate = YAHOO.example.calendar.cal1.getSelectedDates()[0];
				if (firstDate) {
					YAHOO.example.calendar.cal2.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
				}
			}
		}
	} else {
		if (txtDate2) {
			if (txtDate2.value != "mm/dd/yyyy") YAHOO.example.calendar.cal2.select(txtDate2.value);txtDate2.select();
		}
	}
}

function init() {
	document.getElementById(calendarContainer2).style.display = "none";
	YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1",calendarContainer1,{ title:"Check-in Date",mindate: minDate, close:true });
	YAHOO.example.calendar.cal1.selectEvent.subscribe(handleCheckIn, YAHOO.example.calendar.cal1, true);
	
	var txtDate1 = document.getElementById(firstTextBox);
	if (txtDate1.value != "mm/dd/yyyy") {
		var firstDate = new Date(Date.parse(txtDate1.value));
		if (firstDate) YAHOO.example.calendar.cal1.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
		YAHOO.example.calendar.cal1.select(txtDate1.value);txtDate1.select();
	}
	YAHOO.example.calendar.cal1.render();
	document.getElementById(calendarContainer1).style.display = "";
	
	YAHOO.util.Event.addListener("datePickerTrigger1", "click", YAHOO.example.calendar.cal1.show, YAHOO.example.calendar.cal1, true);
	YAHOO.util.Event.addListener(firstTextBox, "click", YAHOO.example.calendar.cal1.show, YAHOO.example.calendar.cal1, true);
}

function init2() {
	document.getElementById(calendarContainer1).style.display = "none";
	YAHOO.example.calendar.cal2 = new YAHOO.widget.Calendar("cal2",calendarContainer2,{ title:"Check-out Date",mindate: minDate, close:true });
	updateCal();
	
	YAHOO.example.calendar.cal2.selectEvent.subscribe(handleCheckOut, YAHOO.example.calendar.cal2, true);
	
	var txtDate2 = document.getElementById(secondTextBox);
	if (txtDate2.value != "mm/dd/yyyy") {
		var secondDate = new Date(Date.parse(txtDate2.value));
		if (secondDate) {
			if (YAHOO.example.calendar.cal1) YAHOO.example.calendar.cal1.cfg.setProperty("pagedate", (secondDate.getMonth()+1) + "/" + secondDate.getFullYear());
		}
		YAHOO.example.calendar.cal2.select(txtDate2.value);txtDate2.select()
	}
	document.getElementById(calendarContainer2).style.display = "block";
	YAHOO.example.calendar.cal2.render();
	
	YAHOO.util.Event.addListener("datePickerTrigger2", "click", YAHOO.example.calendar.cal2.show, YAHOO.example.calendar.cal2, true);
	YAHOO.util.Event.addListener(secondTextBox, "click", YAHOO.example.calendar.cal2.show, YAHOO.example.calendar.cal2, true);
}

YAHOO.util.Event.addListener("checkInDatePickerTrigger", "click", init);
YAHOO.util.Event.addListener("checkOutDatePickerTrigger", "click", init2);
YAHOO.util.Event.addListener(firstTextBox, "click", init);
YAHOO.util.Event.addListener(secondTextBox, "click", init2);

