var now = new Date();
now.setDate(now.getDate() - 1);
var tod = new Date();
var lastArriveDay = new Date( tod.getTime() + (86400000 * 365));
var lastDepartDay = new Date( tod.getTime() + (86400000 * 393));
var dateDirection1 = "arrival";
var dateDirection2 = "departure";
var anchorDirection1 = "arrive";
var anchorDirection2 = "depart";
var adult = "numberofadults";
var dateM = "month";
var dateD = "day";
//var dotNETid = "pbdTemplate__PageTemplate_contentHolder__ctl0_availabilityControl_"
//var dotNETname = "pbdTemplate:_PageTemplate:contentHolder:_ctl0:availabilityControl:"
var aMonth,aDay,dMonth,dDay, noAdult
var currentForm;

function ShowCal(theForm,x) {
	var arrdep = x;
	currentForm=theForm;
	var thisForm=getForm();
	for (i=0;i<document.anchors.length;i++) {
	var tempobj=document.anchors[i];
	var arriveAnchorName, departAnchorName
		if(tempobj.id.toLowerCase().match(anchorDirection1)){
			arriveAnchor = tempobj
			tempobj=document.anchors[i+1];
			departAnchor = tempobj
		}
	}
	if (arrdep == 1) {
	  for (i=0;i<thisForm.elements.length;i++) {
		var tempobj=thisForm.elements[i];
			if(tempobj.id.toLowerCase().match(dateDirection1)){
				if(tempobj.id.toLowerCase().match(dateM)){
					aMonth = tempobj
				}else if(tempobj.id.toLowerCase().match(dateD)){
					aDay = tempobj
				}
			}
		}
		cal11.showCalendar(arriveAnchor,getDateString(aMonth,aDay)); 
		return false;
	} else { 
		for (i=0;i<thisForm.elements.length;i++) {
		var tempobj=thisForm.elements[i];
			if(tempobj.id.toLowerCase().match(dateDirection2)){
				if(tempobj.id.toLowerCase().match(dateM)){
					dMonth = tempobj
				}else if(tempobj.id.toLowerCase().match(dateD)){
					dDay = tempobj
				}
			}
		}
		cal12.showCalendar(departAnchor,getDateString(dMonth,dDay));
		return false;
	}
}

var cal11 = new CalendarPopup();
var firstClick = true;
cal11.addDisabledDates(null,formatDate(now,"yyyy-MM-dd"));
cal11.addDisabledDates(formatDate(lastArriveDay,"yyyy-MM-dd"),null);
cal11.setReturnFunction("setMultipleValuesCheckIn");
function setMultipleValuesCheckIn(y,m,d) {
	var setmonth = m+""+y;
	var thisForm=getForm();
  for (i=0;i<thisForm.elements.length;i++) {
		var tempobj=thisForm.elements[i];
		if(tempobj.id.toLowerCase().match(dateDirection1)){
			if(tempobj.id.toLowerCase().match(dateM)){
				aMonth = tempobj
			}else if(tempobj.id.toLowerCase().match(dateD)){
				aDay = tempobj
			}
		}
	}
  for (var i=0; i<aMonth.length; i++) {
		if (aMonth.options[i].value==setmonth) {
			aMonth.selectedIndex=i;
			}
	}
  for (var i=0; i<aDay.length; i++) {
    if (aDay.options[i].value==d) {
      aDay.selectedIndex=i;
    }
  }
//advance checkout one day
	if (firstClick == true) {
		AdvanceCheckOut(y,m,d);
		firstClick = false;
		//if(document.forms[1].id != "Locate"){
		//	noAdult = document.forms[1].numberOfAdults
		//}else{
			for (i=0;i<thisForm.elements.length;i++) {
				var tempobj=thisForm.elements[i];
				if(tempobj.id.toLowerCase().match(adult)){
					noAdult = tempobj
				}
			}
		//}
		if (noAdult.value == ""){
			noAdult.selectedIndex=1;
		}
	}
//reset check out day if check out less than check in date  
	VerifyCheckOut(y,m,d);
}
var cal12 = new CalendarPopup();
cal12.addDisabledDates(null,formatDate(now,"yyyy-MM-dd"));
cal12.addDisabledDates(formatDate(lastDepartDay,"yyyy-MM-dd"),null);
cal12.setReturnFunction("setMultipleValuesCheckOut");

function setMultipleValuesCheckOut(y,m,d) {
	var setmonth = m+""+y;
	var thisForm=getForm();
  //if(document.forms[1].id != "Locate"){
	//		dMonth = document.forms[1].departureMonth
	//dDay = document.forms[1].departureDay
	//}else{
		for (i=0;i<thisForm.elements.length;i++) {
		var tempobj=thisForm.elements[i];
			if(tempobj.id.toLowerCase().match(dateDirection2)){
				if(tempobj.id.toLowerCase().match(dateM)){
					dMonth = tempobj
				}else if(tempobj.id.toLowerCase().match(dateD)){
					dDay = tempobj
				}
			}
		}
	//}
  for (var i=0; i<dMonth.length; i++) {
		if (dMonth.options[i].value==setmonth) {
			dMonth.selectedIndex=i;
		}
	}
	for (var i=0; i<dDay.length; i++) {
		if (dDay.options[i].value==d) {
			dDay.selectedIndex=i;
		}
	}
}
function getDateString(m_obj,d_obj) {
	var ylen = m_obj.options[m_obj.selectedIndex].value.length;
	var y = m_obj.options[m_obj.selectedIndex].value.substring(ylen-4,6);
	var m = m_obj.options[m_obj.selectedIndex].value.substring(0,ylen-4);
	var d = d_obj.options[d_obj.selectedIndex].value;
	if (y=="" || m=="") { return null; }
	if (d=="") { d=1; }
	return str= y+'-'+m+'-'+d;
}
function getCheckInDate() {
	var thisForm = getForm();
  
	for (i=0;i<thisForm.elements.length;i++) {
	var tempobj=thisForm.elements[i];
		if(tempobj.id.toLowerCase().match(dateDirection1)){
			if(tempobj.id.toLowerCase().match(dateM)){
				aMonth = tempobj
			}else if(tempobj.id.toLowerCase().match(dateD)){
				aDay = tempobj
			}
		}
		if(tempobj.id.toLowerCase().match(adult)){
			noAdult = tempobj
		}
	}
	var d =  aDay.value
	var mon = aMonth.value
	if ( d == "" || mon == "" ) {
		return null;
	}
	else if (mon == "") {
		return null;
	}
  var checkinylen = mon.length;
  m = mon.substring(0,checkinylen-4);
  var checkinmlen = m.length;
  var y = mon.substring(checkinmlen,6);
  m = eval(m);
  y = eval(y);
  d = eval(d);
  var now = new Date();
  var currentDay = now.getDate();
  var currentMonth = now.getMonth()+1;
  var currentYear = now.getFullYear();
//set day to now if date chosen before today
  if (currentMonth == m && currentYear == y && currentDay > d){
		d = currentDay;
		for (var i=0; i<aDay.length; i++) {
			if (aDay.options[i].value==d) {
				aDay.selectedIndex=i;
			}
		}
	}
//set day to 30 for April, June, Sept, and Nov
  if ((m == 4 || m == 6 || m == 9 || m == 11) && (d == 31)){
		d = 30;
		for (var i=0; i<aDay.length; i++) {
			if (aDay.options[i].value==d) {
				aDay.selectedIndex=i;
			}
		}
	}
//set day to 28 for Feb 29 for Leap Year
  if (m == 2 && d > 28){
		d = 28;
		if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {d=29}
		for (var i=0; i<aDay.length; i++) {
			if (aDay.options[i].value==d) {
				aDay.selectedIndex=i;
			}
		}
	}
//reset check out day if check out less than check in date  
	VerifyCheckOut(y,m,d);
//reset check out day on first click					
	if (firstClick == true) {
		AdvanceCheckOut(y,m,d);
		firstClick = false;
		if (noAdult.value == ""){
			noAdult.selectedIndex=1;
		}	
	}
}   

function getCheckOutDate() {
  var thisForm = getForm();
	for (i=0;i<thisForm.elements.length;i++) {
	var tempobj=thisForm.elements[i];
		if(tempobj.id.toLowerCase().match(dateDirection2)){
			if(tempobj.id.toLowerCase().match(dateM)){
				dMonth = tempobj
			}else if(tempobj.id.toLowerCase().match(dateD)){
				dDay = tempobj
			}
		}
		if(tempobj.id.toLowerCase().match(adult)){
			noAdult = tempobj
		}
	}
  var d =  dDay.value
  var mon = dMonth.value
  if ( d == "" || mon == "" ) {
		return null;
	}
  else if (mon == "") {
		return null;
	}

  var checkinylen = mon.length;
  m = mon.substring(0,checkinylen-4);
  var checkinmlen = m.length;
  var y = mon.substring(checkinmlen,6);
  m = eval(m);
  y = eval(y);
  d = eval(d);
	
//set day to 30 for April, June, Sept, and Nov
  if ((m == 4 || m == 6 || m == 9 || m == 11) && (d == 31)){
		d = 30;
		for (var i=0; i<dDay.length; i++) {
			if (dDay.options[i].value==d) {
				dDay.selectedIndex=i;
			}
		}
	}
//set day to 28 for Feb 29 for Leap Year
  if (m == 2 && d > 28){
		d = 28;
		if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {d=29}
		for (var i=0; i<dDay.length; i++) {
			if (dDay.options[i].value==d) {
				dDay.selectedIndex=i;
			}
		}
	}
}   

function VerifyCheckOut(y,m,d){
	var thisForm = getForm();
	//if(calForm.id != "Locate"){
	//	dMonth = document.forms[1].departureMonth
	//	dDay = document.forms[1].departureDay
	//}else{
	for (i=0;i<thisForm.elements.length;i++) {
	var tempobj=thisForm.elements[i];
		if(tempobj.id.toLowerCase().match(dateDirection2)){
			if(tempobj.id.toLowerCase().match(dateM)){					
				dMonth = tempobj
			}else if(tempobj.id.toLowerCase().match(dateD)){
				dDay = tempobj
			}				
		}
	}
	//}
	var d2 =  dDay.value
	var mon2 = dMonth.value
	if (d2 != ""  && mon2 != "") {
		var checkiny2len = mon2.length;
		m2 = mon2.substring(0,checkiny2len-4);
		var checkinm2len = m2.length;
		var y2 = mon2.substring(checkinm2len,6);
		m2 = eval(m2);
		y2 = eval(y2);
		d2 = eval(d2);
		var day1 = new Date(y,m-1,d);
		var day2 = new Date(y2,m2-1,d2);
		if ( day1 >= day2 ){
			AdvanceCheckOut(y,m,d);
		}
	}
}

function AdvanceCheckOut(y,m,d){
	var nextday = new Date(y,m-1,d+1);
	var nextdayD = formatDate(nextday,"d");
	var nextdayY = formatDate(nextday,"yyyy");
	var nextdayM = formatDate(nextday,"M");
	var setnextmonth = nextdayM+""+nextdayY;
	var thisForm = getForm();
	for (i=0;i<thisForm.elements.length;i++) {
	var tempobj=thisForm.elements[i];
		if(tempobj.id.toLowerCase().match(dateDirection2)){
			if(tempobj.id.toLowerCase().match(dateM)){
				dMonth = tempobj
			}else if(tempobj.id.toLowerCase().match(dateD)){
				dDay = tempobj
			}
		}
	}
	for (var i=0; i<dMonth.length; i++) {
		if (dMonth.options[i].value==setnextmonth) {
			dMonth.selectedIndex=i;
		}
	}
	for (var i=0; i<dDay.length; i++) {
		if (dDay.options[i].value==nextdayD) {
			dDay.selectedIndex=i;
		}
	}
}

/*to make sure use does not select dates in the past or that check-out date is not before check-in - jkenney*/
function ValidateDateSearch(thisdoc) {
	var pass=true;
	var thisForm=getForm();
	//if(document.forms[1].id != "Locate"){
	//	aMonth = thisdoc.arrivalMonth.options[thisdoc.arrivalMonth.selectedIndex].value;
	//	aDay = thisdoc.arrivalDay.options[thisdoc.arrivalDay.selectedIndex].value;
	//	dMonth = thisdoc.departureMonth.options[thisdoc.departureMonth.selectedIndex].value;
	//	dDay = thisdoc.departureDay.options[thisdoc.departureDay.selectedIndex].value;
	//}else{
		for (i=0;i<thisForm.elements.length;i++) {
		var tempobj=thisForm.elements[i];
			if(tempobj.id.toLowerCase().match(dateDirection1)){
				if(tempobj.id.toLowerCase().match(dateM)){
					aMonth = tempobj
				}else if(tempobj.id.toLowerCase().match(dateD)){
					aDay = tempobj
				}
			}
			if(tempobj.id.toLowerCase().match(dateDirection2)){
				if(tempobj.id.toLowerCase().match(dateM)){
					dMonth = tempobj
				}else if(tempobj.id.toLowerCase().match(dateD)){
					dDay = tempobj
				}
			}
			if(tempobj.id.toLowerCase().match(adult))
			  noAdult=tempobj;
		}
	//}
	var checkinDay = aDay.value;
	var checkinMonthYr = aMonth.value;
	var checkinylen = checkinMonthYr.length;
	var checkinMonth = checkinMonthYr.substring(0,checkinylen-4);
	var checkinYear = checkinMonthYr.substring(checkinylen-4);
	var checkoutDay = dDay.value;
	var checkoutMonthYr = dMonth.value;
	var checkoutylen = checkoutMonthYr.length;
	var checkoutMonth = checkoutMonthYr.substring(0,checkoutylen-4);
	var checkoutYear = checkoutMonthYr.substring(checkoutylen-4);
	var now = new Date();
	var currentDay = now.getDate();
	var currentMonth = now.getMonth()+1;
	var currentYear = now.getFullYear();
/* debug
	alert("checkinYear: "+checkinYear);
	alert("checkoutYear: "+checkoutYear);
	alert("checkinMonth: "+checkinMonth);
	alert("checkoutMonth: "+checkoutMonth);
	alert("checkinDay: "+checkinDay);
	alert("checkoutDay: "+checkoutDay);
*/

/*debug
	alert("currentDay: "+currentDay);
	alert("currentYear: "+currentYear);
	alert(currentYear+1);
	alert("currentMonth: "+currentMonth);
*/
	if (checkinDay == "" && checkinMonthYr == "" && checkoutDay == "" && checkoutMonthYr == ""){
		pass = true;
 	}
	if((checkinDay == "" && checkinMonthYr != "") || (checkinDay != "" && checkinMonthYr == "")) {
		alert ("Please complete all Check-in date fields."); 
		pass = false;
	}
	if((checkoutDay == "" && checkoutMonthYr != "") || (checkoutDay != "" && checkoutMonthYr == "")) {
		alert ("Please complete all Check-out date fields."); 
		pass = false;
	}
	if((checkinDay != "") && (checkinMonthYr != "") && (checkoutDay != "") && (checkoutMonthYr != "")) {
		checkinDay = eval(checkinDay);
		checkinMonth = eval(checkinMonth);
		checkinYear = eval(checkinYear);
		checkoutDay = eval(checkoutDay);
		checkoutMonth = eval(checkoutMonth);
		checkoutYear = eval(checkoutYear);
		if ((checkinYear == currentYear) && ((checkinMonth < currentMonth) || (checkinMonth <= currentMonth && checkinDay < currentDay))) {
			alert ("Check-in date cannot be before today"); 
			pass = false;
		}
		if ((checkoutMonth < checkinMonth && checkoutYear == checkinYear) || (checkoutMonth == checkinMonth && checkoutDay <= checkinDay && checkoutYear == checkinYear)) {
			alert ("Check-out date must be later than Check-in date");
			pass = false;
		}
		if (((checkoutMonth > checkinMonth && checkoutYear == checkinYear) || (checkoutMonth == checkinMonth && checkoutDay > checkinDay && checkoutYear == checkinYear) || (checkoutMonth <= checkinMonth && checkoutYear > checkinYear)) && (noAdult.options[noAdult.selectedIndex].value == "")) {
			alert ("Please select the number of adults staying.");
			pass = false;
		}
		var day1 = new Date(checkinYear,checkinMonth-1,checkinDay);
		var day2 = new Date(checkoutYear,checkoutMonth-1,checkoutDay);
		//alert(day2-day1);
		//alert(day2);
		
		//if ((day2-day1) > (86400000 * 28)){
		if(Math.ceil((day2.getTime()-day1.getTime())/86400000) > 28){
			alert ("A maximum of 28 days may be confirmed for an online reservation.  If request is for more than 28 days, please call 1-800-466-8356 for assistance.");
			pass = false;
		}
	}
	
	if (pass) {
		//return (thisdoc.submit());
		return true;
	}
	else {
		return (false);
	}
}

function getForm(){
  //currentForm is the form that is passed in from the ShowCal function call,
  //which is usually the point of entry so we will use that form throughout.
  //if currentForm is undefined, then we will grab the first form on the page
  //if the page only contains one form.  otherwise, we grab the second one 
  //usually the 
  var thisForm;
  if(currentForm!=undefined)
    thisForm=currentForm;
  else{
    thisForm=document.forms[0];
    if (document.forms.length>1) thisForm=document.forms[1];
  }
  return thisForm;
}