/** 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 = "cal1Container"; // the div where the calendar goes
var calendarContainer2 = "cal2Container"; // 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() {
	var cookieLang = getCookie('lang');
	if(cookieLang == "es"){
		var strCheckIn = "Llegada";
	}else{
		var strCheckIn = "Check-in Date";
	}
	
	document.getElementById(calendarContainer2).style.display = "none";
	YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1",calendarContainer1,{ title:strCheckIn,mindate: minDate, close:true });
	
	if(cookieLang == "es"){
		YAHOO.example.calendar.cal1.cfg.setProperty("MONTHS_LONG", ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"]);
		YAHOO.example.calendar.cal1.cfg.setProperty("WEEKDAYS_SHORT", ["do", "lu", "ma", "mi", "ju", "vi", "sá"]);
	}
	
	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() {
	var cookieLang = getCookie('lang');
	if(cookieLang == "es"){
		var strCheckOut = "Salida";
	}else{
		var strCheckOut = "Check-out Date";
	}
	document.getElementById(calendarContainer1).style.display = "none";
	YAHOO.example.calendar.cal2 = new YAHOO.widget.Calendar("cal2",calendarContainer2,{ title:strCheckOut,mindate: minDate, close:true });
	
	if(cookieLang == "es"){
		YAHOO.example.calendar.cal2.cfg.setProperty("MONTHS_LONG", ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"]);
		YAHOO.example.calendar.cal2.cfg.setProperty("WEEKDAYS_SHORT", ["do", "lu", "ma", "mi", "ju", "vi", "sá"]);
	}
	
	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);
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

YAHOO.util.Event.addListener("datePickerTrigger1", "click", init);
YAHOO.util.Event.addListener("datePickerTrigger2", "click", init2);

YAHOO.util.Event.addListener(firstTextBox, "click", init);
YAHOO.util.Event.addListener(secondTextBox, "click", init2);

