// JavaScript Document

function showDatepicker(id) {
  $('#' + id).val($('#' + id + '_dayDropDown').val() + '/' + $('#' + id + '_monthDropDown').val() + '/' + $('#' + id + '_yearDropDown').val());
  $('#' + id).datepicker('show');
}

function setDate(text, control) {
  var dt = text.split('/');
  $('#' + control.id + '_dayDropDown').val(dt[0]);
  $('#' + control.id + '_monthDropDown').val(dt[1]-1);
  $('#' + control.id + '_yearDropDown').val(dt[2]);
}

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield, addNum){
	var today=new Date();
	today.setDate(today.getDate() + addNum);
	
	var dayfieldObj=document.getElementById(dayfield);
	var monthfield=document.getElementById(monthfield);
	var yearfield=document.getElementById(yearfield);
	
	for (var i=0; i<31; i++) {
		dayfieldObj.options[i]=new Option(i+1, i+1);
	}
	for (var m=0; m<12; m++) {
		monthfield.options[m]=new Option(monthtext[m], m);
		monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], today.getMonth(), true, true); //select today's month
	}
	var thisyear=today.getFullYear();
	for (var y=0; y<2; y++) {
		yearfield.options[y]=new Option(thisyear, thisyear);
		thisyear+=1;
	}
	dayfieldObj.selectedIndex = (today.getDate()-1);
	yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true); //select today's year
}

function launchBE()
{
	
	//puts the date fields together and submits form
	var dateOne = new Date(document.resForm.yearDropDown.value, document.resForm.monthDropDown.value, document.resForm.dayDropDown.value);
	document.resForm.checkInDate.value = (dateOne.getFullYear() + "-" + (dateOne.getMonth()+1) + '-' + dateOne.getDate());
	if(document.resForm.nights) {
		var dateTwo = dateOne;
		var numNights = parseInt(document.resForm.nights.value);
		dateTwo.setDate(dateTwo.getDate()+numNights);
		document.resForm.checkOutDate.value = (dateTwo.getFullYear() + '-' + (dateTwo.getMonth()+1) + '-' + dateTwo.getDate());
	} else {
		var dateOne = new Date(document.resForm.yearDropDown2.value, document.resForm.monthDropDown2.value, document.resForm.dayDropDown2.value);
		document.resForm.checkOutDate.value = (dateOne.getFullYear() + "-" + (dateOne.getMonth()+1) + '-' + dateOne.getDate());
	}
}
