//var today_temp = new Date();
//today_temp.setDate(today_temp.getDate() + 15);

var currentTime = new Date();

	
//Calendar2 setup to assign calendar image and textbox to bring up the calendar popup
Calendar.setup({inputField  : "fini", ifFormat: "%Y-%m-%d", button: "triggerx", dateStatusFunc : dateStatus, onSelect: onSelectFunc});
Calendar.setup({inputField  : "fini", ifFormat: "%Y-%m-%d", button: "fini", dateStatusFunc : dateStatus, onSelect: onSelectFunc});
Calendar.setup({inputField  : "ffin", ifFormat: "%Y-%m-%d", button: "triggerx2", dateStatusFunc : dateStatus2, onSelect: onSelectFunc2});
Calendar.setup({inputField  : "ffin", ifFormat: "%Y-%m-%d", button: "ffin", dateStatusFunc : dateStatus2, onSelect: onSelectFunc2});

//DateStatus will restrict only future dates
function dateStatus(date) {
	var min = new Date(currentTime.getFullYear(),currentTime.getMonth(),currentTime.getDate());
	var max = new Date(currentTime.getFullYear()+1,currentTime.getMonth(),currentTime.getDate());
	if (date.getTime() < min.getTime() || date.getTime() > max.getTime())
		return true; // true says "disable"
	else
		return false; // leave other dates enabled
}

//DateStatus will restrict only future dates
function dateStatus2(date) {
	var ad = document.getElementById("fini").value;
	if (ad != ""){
		var nd = ad.split('/');
		var m = 0;
		for (i=0; i<12; i++){
			if (nd[1]==monthname[i]){
				m=i;
				break;
			}
		}
		var min = new Date(('20'+nd[2]).toInt(),m,nd[0].toInt()+1);
	} else {
		var min = new Date(currentTime.getFullYear(),currentTime.getMonth(),currentTime.getDate());
	}
	var max = new Date(currentTime.getFullYear(),currentTime.getMonth(),currentTime.getDate());
	
	if (date.getTime() < min.getTime() || date.getTime() < max.getTime())
		return true; // true says "disable"
	else
		return false; // leave other dates enabled
}


//Select function for the first date will preselect the departure date for 7 days in advance
function onSelectFunc(calendar, date) {
  document.getElementById("fini").value = date;
  if (calendar.dateClicked) {
	aD = new Date(calendar.date.getFullYear(), calendar.date.getMonth(), calendar.date.getDate());
	var temp = calendar.date;
	temp.setDate(temp.getDate() + 1);
	
	dD = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate());
	var y = dD.getFullYear() + '';
	var m = dD.getMonth();
	var d = dD.getDate();
	d = (d < 10)?("0"+d):d;
	m = parseInt(m)+1;
	m = (m < 10)?("0"+m):m;
	document.getElementById("ffin").value = y + "-" + m + "-" + d;
	calendar.callCloseHandler(); // this calls "onClose" (see above)
  }
}

//Select function for the second date
function onSelectFunc2(calendar, date) {
  document.getElementById("ffin").value = date;
  if (calendar.dateClicked) {
	dD = new Date(calendar.date.getFullYear(), calendar.date.getMonth(), calendar.date.getDate());
	calendar.callCloseHandler(); // this calls "onClose" (see above)
  }
}