function valueChanged() {
   var year,month,day;

   if (this.name == "depart_date"){
       departDate = new Date(document.forms[0].depart_date.value);
	   
	   month = departDate.getMonth()+1;
	   day = departDate.getDate();
	   year = departDate.getFullYear();
	   
   	   document.forms[0].date_depart_mm.value = month;	   
	   document.forms[0].date_depart_dd.value = day;	
   	   document.forms[0].date_depart_yy.value = year;	      

   }else if (this.name == "return_date"){
       returnDate = new Date(document.forms[0].return_date.value);
	   
	   month = returnDate.getMonth()+1;
	   day = returnDate.getDate();
	   year = returnDate.getFullYear();
	   
	   document.forms[0].date_return_mm.value = month;	   
	   document.forms[0].date_return_dd.value = day;	
   	   document.forms[0].date_return_yy.value = year;	      
   }
}

function updateDateFields(){
	document.forms[0].depart_date.value = document.forms[0].date_depart_mm.options[document.forms[0].date_depart_mm.selectedIndex].value+
										  "/"+document.forms[0].date_depart_dd.options[document.forms[0].date_depart_dd.selectedIndex].value+
									   	  "/"+document.forms[0].date_depart_yy.options[document.forms[0].date_depart_yy.selectedIndex].value;
	
	document.forms[0].return_date.value = document.forms[0].date_return_mm.options[document.forms[0].date_return_mm.selectedIndex].value+
										  "/"+document.forms[0].date_return_dd.options[document.forms[0].date_return_dd.selectedIndex].value+
									   	  "/"+document.forms[0].date_return_yy.options[document.forms[0].date_return_yy.selectedIndex].value;				  
}

function cal_popup2 (str_path,str_datetime) {
	if (document.forms[0].return_date.value == '' || isNaN(document.forms[0].return_date.value)) {
		document.forms[0].return_date.value = document.forms[0].depart_date.value;
	}
	
	this.dt_current = this.prs_tsmp(str_datetime ? str_datetime : this.target.value);
	if (!this.dt_current) return;

	var obj_calwindow = window.open(
		str_path+'calendar.html?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
		'Calendar', 'width=200,height='+(this.time_comp ? 215 : 190)+
		',status=no,resizable=yes,top=150,left=350,dependent=yes,alwaysRaised=yes');
	obj_calwindow.opener = window;
	obj_calwindow.focus();
}

function initCalendarPopup() {
   if (calDepart == null)
	calDepart = new calendar2(document.forms[0].elements['depart_date'], new point(0,0));
	
	document.forms[0].elements['depart_date'].valueChanged = valueChanged;

   if (calReturn == null)
	calReturn = new calendar2(document.forms[0].elements['return_date'], new point(0,0));

	document.forms[0].elements['return_date'].valueChanged = valueChanged;
}

function point(top, left) {
	  this.top = top;
	  this.left = left;
}
	
function resortFlight(){
var resortFlightTD = document.getElementById("resortFlightTD");
	if(document.forms[0].vacation_type[0].checked) {
		resortFlightTD.style.display="";
	}else{
		resortFlightTD.style.display='none';
	}
}	

function sendToCBE(){
	if (validateForm()){
		setHidden();
		document.forms[0].target="_blank";
		document.forms[0].action="http://partners.kcdataservices.com/amr/custom/buildMyOwn.jsp";
		document.forms[0].submit();
	}	
}

function setHidden(){
	document.forms[0].commingFrom.value  = "quicksearch";
	if(document.forms[0].vacation_type[0].checked){
		document.forms[0].Gateway.value = document.forms[0].selectedGateway.options[document.forms[0].selectedGateway.selectedIndex].value;
	}else{
		document.forms[0].Gateway.value = 'PHL'	
	}
	document.forms[0].Destination.value = document.forms[0].selectedHotel.options[document.forms[0].selectedHotel.selectedIndex].value;
	document.forms[0].Depart.value = document.forms[0].date_depart_yy.options[document.forms[0].date_depart_yy.selectedIndex].value+
									 "-"+document.forms[0].date_depart_mm.options[document.forms[0].date_depart_mm.selectedIndex].value+
									 "-"+document.forms[0].date_depart_dd.options[document.forms[0].date_depart_dd.selectedIndex].value;
	document.forms[0].Return.value = document.forms[0].date_return_yy.options[document.forms[0].date_return_yy.selectedIndex].value+
									 "-"+document.forms[0].date_return_mm.options[document.forms[0].date_return_mm.selectedIndex].value+
									 "-"+document.forms[0].date_return_dd.options[document.forms[0].date_return_dd.selectedIndex].value;
	document.forms[0].Adults.value = document.forms[0].numberOfAdults.options[document.forms[0].numberOfAdults.selectedIndex].value;
	document.forms[0].Children.value = document.forms[0].numberOfChildren.options[document.forms[0].numberOfChildren.selectedIndex].value;
}

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function validateForm(){
	// Validations took from custom_2PageVal.java on CBE...
	//-------------------------------------------------------------
	// Please select a valid departure city code (if flight and hotel selected and empty city_destination_code) 
	// Please select a valid resort
	// Please enter departure date
	// Departure date must be later than today...
	// Cannot process reservations containing flights sooner than 5 days from today
	// Please enter return date
	// Return date must be later than today...
	// Return date must be later than departure date
	// Cannot process reservations longer than a 99-night stay

	var f = document.forms[0];
	var departDate, returnDate, today, todayPlus5, departPlus99;

	if 	(f.vacation_type[0].checked){
		if (f.selectedGateway.options[f.selectedGateway.selectedIndex].value == ""){
			alert("Please select a valid departure city");
			f.selectedGateway.focus();
			return false;
		}	
	}
	if (f.selectedHotel.options[f.selectedHotel.selectedIndex].value == ""){
		alert("Please select a valid Resort");
		f.selectedHotel.focus();
		return false;
	}	
	
	// Get departure and destination date... 
	departDate = new Date(f.depart_date.value);
	returnDate = new Date(f.return_date.value);
	today = new Date();
	todayPlus5 = addDays(today,5);
	departPlus99 = addDays(departDate,99);
	

	if (isNaN(departDate)){
		alert("Please enter a valid Departure Date");
		return false;
	}
		
	if (departDate <= today){
		alert("Departure date must be later than today");
		return false;
	}
	
	if (departDate <= todayPlus5){
		alert("Cannot process reservations containing flights sooner than 5 days from today");
		return false;
	}
	
	if (isNaN(returnDate)){
		alert("Please enter a valid Return Date");
		return false;
	}
	
	if (returnDate <= today){
		alert("Return date must be later than today");
		return false;
	}
	
	if (returnDate < departDate){
		alert("Return date must be later than Departure date");
		return false;
	}

	if (returnDate >= departPlus99){
		alert("Cannot process reservations longer than a 99-night stay");
		return false;
	}
	
	
	return true;
}
