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('/cbe/calendar.html?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
			//var obj_calwindow = window.open('http://localhost:8080/u3k/cbe/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");
	var fromFlightTD = document.getElementById("fromFlightTD");
	if(document.forms[0].vacation_type[0].checked) {
		resortFlightTD.style.display="";
		fromFlightTD.style.display="";
	}else{
		resortFlightTD.style.display='none';
		fromFlightTD.style.display='none';
	}
	
}	

function sendToCBE(){
	if (validateForm()){
		setHidden();		
		 document.forms[0].action="http://partners.kcdataservices.com/web30/custom/buildMyOwn.jsp?ui_path=usa3000";
		 //document.forms[0].action=" http://qa02-bookonline.applevacations.com/u3k/custom/buildMyOwn.jsp?ui_path=usa3000";
		//document.forms[0].action="http://localhost:9082/u3k/custom/buildMyOwn.jsp?ui_path=usa3000";		 
		document.forms[0].submit();
	}	
}

function setHidden(){
	
	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;
	if (document.forms[0].agn_id.value  == "57871"){	// if secrets no childrens...
		document.forms[0].Children.value = 0;
	}else{
		document.forms[0].Children.value = document.forms[0].children1.options[document.forms[0].children1.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 destination");
		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,2);
	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 2 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;
}

var destValues = [ "", "BDA", "100", "101", "CZM", "HUX", "ZIH", "200",
       			"201", "203", "204", "205", "LRM", "SJD", "PVR", "PUJ","POP"];
       	var destText = [ "- Select a destination city -", "Bermuda",
       			"Cancun - Hotel Zone Resorts", "Cancun - Riviera Maya Resorts",
       			"Cozumel", "Huatulco", "Ixtapa/Zihuatanejo",
       			"Jamaica - Montego Bay Resorts", "Jamaica - Braco Resorts",
       			"Jamaica - Negril Resorts", "Jamaica - Ocho Rios Resorts",
       			"Jamaica - Runaway Bay Resorts", "La Romana", "Los Cabos",
       			"Puerto Vallarta/Nayarit", "Punta Cana","Puerto Plata" ];
       	
       	function restrictDestinations(dep) {
       		
       		var list = document.forms[0].selectedHotel;
       	   
       		 list.options.length=1;
       		 
       		while (list.length != 0) {
       			list.options[list.length - 1] = null;
       		}
       		if (dep == "") {
       			list.options[list.length] = new Option(
       					"- Select departure above -", "");

       		} else if (dep == "BWI") {
       			appendDestinations("");
       			appendDestinations("BDA");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("PUJ");
       		} else if (dep == "ORD") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("CZM");
       			appendDestinations("HUX");
       			appendDestinations("ZIH");
       			appendDestinations("200");
       			appendDestinations("201");
       			appendDestinations("203");
       			appendDestinations("204");
       			appendDestinations("205");
       			appendDestinations("SJD");
       			appendDestinations("PVR");
       			appendDestinations("PUJ");
       			appendDestinations("POP");       			
       		} else if (dep == "CVG") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("PUJ");
       		} else if (dep == "CLE") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("PUJ");
       	  	} else if (dep == "CMH") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       		} else if (dep == "DTW") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("200");
       			appendDestinations("201");
       			appendDestinations("203");
       			appendDestinations("204");
       			appendDestinations("205");
       			appendDestinations("PVR");
       			appendDestinations("PUJ");
       		} else if (dep == "PIT") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");	
       			appendDestinations("PUJ");
       		} else if (dep == "PHL") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("PUJ");
       			appendDestinations("BDA");
       	 	   } else if (dep == "STL") {
       			appendDestinations("");
       			appendDestinations("100");
       			appendDestinations("101");
       			appendDestinations("200");
       			appendDestinations("201");
       			appendDestinations("203");
       			appendDestinations("204");
       			appendDestinations("205");
       			appendDestinations("PVR");
       			appendDestinations("PUJ");
       		} else if (dep == "LAN") {       			
       			appendDestinations("");       			
       			appendDestinations("101");
       			appendDestinations("100");
       		} else {
       			list.options[list.length] = new Option(
       					"- No destinations available -", "");

       		}

       	}       
       	function appendDestinations(adddest) {
       		//appends dests to destination options
       		var list = document.forms[0].selectedHotel;
       		for (i = 0; i < destValues.length; i++) {
       			if (destValues[i] == adddest) {
       				list.options[list.length] = new Option(destText[i],
       						destValues[i]);
       				break;
       			}
       		}
       	}       	
       	
       function restrictDestinations2(){    	 
    	   document.getElementById("selectedHotel").options.length=1;
    	   // appendDestinations("");
    	    appendDestinations("BDA");
  			appendDestinations("100");
  			appendDestinations("101");
  			appendDestinations("CZM");
  			appendDestinations("HUX");
  			appendDestinations("ZIH");  			
  			appendDestinations("200");
  			appendDestinations("201");
  			appendDestinations("203");
  			appendDestinations("204");
  			appendDestinations("205");
  			appendDestinations("SJD");
  			appendDestinations("PVR");
  			appendDestinations("PUJ");  			
  			appendDestinations("POP");
      			
      		
       }