var vacationsForm = Array(
	{"field" : "area1", "method" : "validateTextField"},
	{"field" : "area2", "method" : "validateTextField"},
	{"field" : "date1", "method" : "validateTextField"},
	{"field" : "date2", "method" : "validateTextField"},
	{"field" : "child_age1", "method" : "validateChildAge", "params" : [2]},
	{"field" : "child_age2", "method" : "validateChildAge", "params" : [2]},
	{"field" : "child_age3", "method" : "validateChildAge", "params" : [2]},
	{"field" : "num_rooms", "method" : "validateMinNumber", "params" : [1]}
);

function openPopUp(url){
	window.open(url,'pop2','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,height=500,width=800');
}

function VacationsPage(container){
	$("area1").value = "";
	$("area2").value = "";
	$("date1").value = "";
	$("date2").value = "";
	$("dynamic_children").value = "0";
	$("child_age1").value = "";
	$("child_age2").value = "";
	$("child_age3").value = "";
	$("adults").value = "2";
	$("infants_in_lap").value = "0";
	$("num_rooms").value = "";
	var uniqueStringDate = new Date();
	$("uniqueString").value = uniqueStringDate.getTime();
	this.container = container;
	this.cookie = "vacationPageCookie";
	this.browserBackCookie = "VacationbrowserBackCookie";
	this.maxDate = minibookerExtensionDate; 
	var o = this;
	this.panelPair = new PanelPair("whereFrom" , "whereTo", "\277Desde d\363nde?", "\277Hacia d\363nde?", "area1", "area2", 0, FlightOrigins, VacationDests, "vacationsForm","","","","");
	this.adults = new DropDown("selectObj_0");
	this.children = new DropDown("selectObj_1");
	this.infants = new DropDown("selectObj_2");
	this.hotelRooms = new DropDown("selectObj_3");
	this.hotelRooms.presetHotelRooms(1);
	this.childrenAge1 = new DropDown("selectObj_7");
	this.childrenAge2 = new DropDown("selectObj_8");  
	this.childrenAge3 = new DropDown("selectObj_9");  
	var today = new Date();
	var todayDay = today.getDate() + 3;
	this.calPair = new CalendarPair("cal0", "cal1", "date1", "date2",(today.getMonth() + 1) + "/" + todayDay + "/" + today.getFullYear(),this.maxDate);
	this.validator = new Validator("vacationsForm", "errorMessage", false);
	this.panel1 = new VacationsPanel("whereTo1", "\277Hacia d\363nde?", "area3", 3, GetawaysDestinations,"goTovacationsForm","","");
	this.panel1.onItemSelect.subscribe(this.goToDest, this, true); 
	var cookie = getCookie(this.cookie);
	// Form Submit
	$("VacationSubmit").onclick = function(){return o.submitForm('vacationsForm');}
	// Left Dropdown position
	
	if(cookie){
   		var jar = document.getElementsByClassName("cookieJar",this.container);
   		var cookieObject = eval(cookie);
   		jar[0].appendChild(document.createTextNode("B\372squeda reciente: "));
   		var link = document.createElement("a");
   		link.id = "prepopMB";
   		link.href="#";
		link.title = cookieObject[0].fields[0].readable + " hasta " +
			cookieObject[0].fields[1].readable + " el " +
			cookieObject[0].fields[2].readable + " regreso " +
			cookieObject[0].fields[3].readable + " - " +
			cookieObject[0].fields[4].readable + ", " +
			cookieObject[0].fields[5].readable + ", ";
		var from = cookieObject[0].fields[0].value;
		var to = cookieObject[0].fields[1].value;
		//special citycode string replacement
		if(to.split(" ").length > 2){
			cityTo = this.stringReplaceAll(to," ", "_");
			to = to.split(" ")[to.split(" ").length-2];
		}
		else{
			cityTo = to.replace(" ", "_");
			to = to.split(" ")[0];
		}
		if(to == "NYC"){
			to = "JFK";
		}
		if(from == "NYC"){
			from = "JFK";
		}
		link.appendChild(document.createTextNode(from + " hasta " + to));
		jar[0].appendChild(link);
		$("prepopMB").onclick = function(){o.prepopMB(cookieObject[0].fields[0].value,cityTo,cookieObject[0].fields[2].value,cookieObject[0].fields[3].value,cookieObject[0].fields[4].value,cookieObject[0].fields[5].value,cookieObject[0].fields[6].value,cookieObject[0].fields[7].value,cookieObject[0].fields[8].value,cookieObject[0].fields[9].value,cookieObject[0].fields[10].value);};
	}
	// browserBackButton Cookie
	// $("browserBack").value = Math.floor(Math.random()*110);

	
	$("tdFlBooker").style.display = 'block';
}

VacationsPage.prototype.stringReplaceAll = function(strReplaceAll,searchfor,replacewith){
	var stringReplace = strReplaceAll.replace(/\s*$/, '');
	var intIndexOfMatch = stringReplace.indexOf(" ");
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		strReplaceAll = strReplaceAll.replace(searchfor, replacewith);
		intIndexOfMatch = strReplaceAll.indexOf(searchfor);
	}
	strReplaceAll = strReplaceAll.slice(0, -1);
	return strReplaceAll;
}

/* Special function for Vacations: is only allowed to pick the return date as one day after departure date or beyond */ 
CalendarPair.prototype.dateSelect0 = function(type, args, obj){
	var currDate = obj.cal0.getSelectedDates()[0];
	var parsed = Date.parse(currDate.toString());
	var jbDate = obj.cal0.Locale.WEEKDAYS_MEDIUM[currDate.getDay()] + ", " +
	obj.cal0.Locale.MONTHS_SHORT[currDate.getMonth()] + " " + currDate.getDate() + ", " + currDate.getFullYear();
	$(obj.cal0Base + "Input").innerHTML = jbDate;
	obj.cal0Hidden.value = (currDate.getMonth() + 1) + "/" + (currDate.getDate()) + "/" + currDate.getFullYear();
	if(obj.cal1Hidden.value == ""){
		obj.cal1.setMonth(currDate.getMonth());
		obj.cal1.setYear(currDate.getFullYear());
		obj.cal1.render();
	}
	else if(Date.parse(obj.cal1Hidden.value) < parsed){
		$(obj.cal1Base + "Input").innerHTML = obj.cal1DefaultText;
		obj.cal1Hidden.value = "";
		obj.cal1.clear();
	}
	currDate = new Date((currDate.getMonth() + 1) + "/" + (currDate.getDate() + 2) + "/" + currDate.getFullYear());
	obj.cal1.cfg.setProperty("mindate", currDate);
	obj.hide(obj.cal0.oDomContainer.id);
}
  
CalendarPair.prototype.changePage1 = function(type, args, obj){
	var currDate = obj.today;
	var nextDate = obj.cal1.cfg.getProperty("pageDate");
	var maxDate = new Date(minibookerExtensionDate);
	var diff = Date.parse(nextDate.toString()) > Date.parse(currDate.toString());
	if(Date.parse(nextDate.toString()) > Date.parse(maxDate.toString())){
		obj.cal1.Style.CSS_NAV_LEFT = "calnavleft";
		obj.cal1.Style.CSS_NAV_RIGHT = "calnavH";
	}
	else if(Date.parse(nextDate.toString()) < Date.parse(maxDate.toString())){
		obj.cal1.Style.CSS_NAV_LEFT = "calnavleft";
		obj.cal1.Style.CSS_NAV_RIGHT = "calnavright";
	}
	else{
		obj.cal1.Style.CSS_NAV_LEFT = "calnavleft";
		obj.cal1.Style.CSS_NAV_RIGHT = "calnavright";
	}
	obj.cal1.render();
}

GenericPanel.prototype.setField = function(text){
	document.getElementById(this.inputName).value = text;
	if(this.hiddenId == "area2"){
		var destination = this.set[this.selectedItem.id.split("_")[1]][0];
		var cityValues = '';
		var cityTo = '';
		if(destination.indexOf('_') != -1){
			if(destination.split("_").length > 2){
				for(i=0;i<destination.split("_").length;i++){
				cityValues += destination.split("_")[i] + " ";
				}
				cityTo = cityValues;
			}
			else{
				cityTo = destination.split("_")[0] + " " + destination.split("_")[1];
			}
			this.form[this.hiddenId].value = cityTo;
		}
		else{
			this.form[this.hiddenId].value = destination;
		}
	}
	if(this.hiddenId == "area1" || this.hiddenId == "area3"){
		this.form[this.hiddenId].value = this.set[this.selectedItem.id.split("_")[1]][0];
	}
}

VacationsPage.prototype.submitForm = function(){
	var result = this.validator.validate();
	var cookieObject = new Object();
	if(result.length == undefined){
		if(this.calPair.getReadable1() == this.calPair.getReadable2())
			if(!confirm("Tu fecha de regreso es la misma que tu fecha de salida. \277Es intencional este viaje tan corto?"))
				return false;
		cookieObject.displayValue = $("panInput_1").value;
		cookieObject.fields = new Array();
		cookieObject.fields.push({"name" : "area1","value" : $("area1").value,"readable" : $("panInput_0").value});
		cookieObject.fields.push({"name" : "area2","value" : $("area2").value,"readable" : $("panInput_1").value});
		cookieObject.fields.push({"name" : this.calPair.getName1(), "value" : this.calPair.getValue1(),"readable" : this.calPair.getReadable1()});
		cookieObject.fields.push({"name" : this.calPair.getName2(),"value" : this.calPair.getValue2(),"readable" : this.calPair.getReadable2()});
		cookieObject.fields.push({"name" : this.adults.getName(),"value" : this.adults.getValue(),"readable" : this.adults.getReadable()});
		cookieObject.fields.push({"name" : this.children.getName(),"value" : this.children.getValue(),"readable" : this.children.getReadable()});
		cookieObject.fields.push({"name" : this.infants.getName(), "value" : this.infants.getValue(),"readable" : this.infants.getReadable()});
		cookieObject.fields.push({"name" : this.childrenAge1.getName(), "value" : this.childrenAge1.getValue(), "readable" : this.childrenAge1.getReadable()});						
		cookieObject.fields.push({"name" : this.childrenAge2.getName(), "value" : this.childrenAge2.getValue(), "readable" : this.childrenAge2.getReadable()});						
		cookieObject.fields.push({"name" : this.childrenAge3.getName(),"value" : this.childrenAge3.getValue(),"readable" : this.childrenAge3.getReadable()});						
		cookieObject.fields.push({"name" : this.hotelRooms.getName(),"value" : this.hotelRooms.getValue(),"readable" : this.hotelRooms.getReadable()});
		addTracking(this.cookie, cookieObject);
		document.vacationsForm.submit();
    }
	else{
		return false;
	}
}

VacationsPage.prototype.goToDest = function(e){
	location.href="http://hola.jetblue.com/enes/traveldeals/getaways/" + $("area3").value.toLowerCase() + ".html";
}
       
VacationsPage.prototype.prepopMB = function(fromCity,toCity,fromdt,todt,adults,children,infants,childrenAge1,childrenAge2,childrenAge3,hotelRooms){
	this.panelPair.panel1.preset(fromCity);
	this.panelPair.panel2.preset(toCity);
	this.adults.presetAdults(adults);
	this.children.preset(children);
	this.infants.preset(infants);
	this.calPair.preset0(fromdt);
	this.calPair.preset1(todt);
	this.childrenAge1.presetChildrenAge(childrenAge1);
	this.childrenAge2.presetChildrenAge(childrenAge2);
	this.childrenAge3.presetChildrenAge(childrenAge3);
	this.hotelRooms.presetHotelRooms(hotelRooms);
}

function showKids(on){
	if(document.getElementById){
		var kA = document.getElementById("kidsAges");
		var k1 = document.getElementById("selectObj_7");
		var k2 = document.getElementById("selectObj_8");
		var k3 = document.getElementById("selectObj_9");
		if(k1.getElementsByTagName("input")[0].value == ""){
			k1.getElementsByTagName("input")[0].value = '0';
		}
		if(k2.getElementsByTagName("input")[0].value == ""){
			k2.getElementsByTagName("input")[0].value = '0';
		}
		if(k3.getElementsByTagName("input")[0].value == ""){
			k3.getElementsByTagName("input")[0].value = '0';
		}
		switch(on){
			case 0:
				kA.style.visibility = 'hidden';  kA.style.display = 'none';
				k1.style.visibility = 'hidden';  k1.style.display = 'none';
				k2.style.visibility = 'hidden';  k2.style.display = 'none';
				k3.style.visibility = 'hidden';  k3.style.display = 'none';
				k1.getElementsByTagName("input")[0].value = "";
				k2.getElementsByTagName("input")[0].value = "";
				k3.getElementsByTagName("input")[0].value = "";
				break;
			case 1:
				kA.style.visibility = 'visible'; kA.style.display = 'block';
				k1.style.visibility = 'visible'; k1.style.display = 'block';
				k2.style.visibility = 'hidden';  k2.style.display = 'none';
				k3.style.visibility = 'hidden';  k3.style.display = 'none';
				k2.getElementsByTagName("input")[0].value = "";
				k3.getElementsByTagName("input")[0].value = "";
				break;
			case 2:
				kA.style.visibility = 'visible'; kA.style.display = 'block';
				k1.style.visibility = 'visible'; k1.style.display = 'block';
				k2.style.visibility = 'visible'; k2.style.display = 'block';
				k3.style.visibility = 'hidden';  k3.style.display = 'none';
				k3.getElementsByTagName("input")[0].value = "";
				break;
			case 3:
				kA.style.visibility = 'visible'; kA.style.display = 'block';
				k1.style.visibility = 'visible'; k1.style.display = 'block';
				k2.style.visibility = 'visible'; k2.style.display = 'block';
				k3.style.visibility = 'visible'; k3.style.display = 'block';
				break;
		}
	}
}

// Dean Edwards/Matthias Miller/John Resig
function init(){
	// quit if this function has already been called
	if (arguments.callee.done) return;
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	// kill the timer
	if (_timer) clearInterval(_timer);
	var page = new VacationsPage("tdFlBookerTop");
	initLightbox();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function(){
		if (this.readyState == "complete"){
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;