/**
	$Revision: 1.45 $
*/
var saf = (navigator.userAgent.lastIndexOf("Safari") > 0);

/* == generic form definition == */
var FormDef = function(v) {
  this.conf = v;
  this.form = $(this.conf.formId)
}

/* == prototype lib == */
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string') element = document.getElementById(element);
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}
var activeForm = null;
var theId = null;
// set the active product after loading the modul
function initModules(id,bookID, ieFixInit) {
	if (!id || id == '') id = 'b0';
	activeForm = bookID;
	theId = id;
	if(bookID>3 || bookID<1) return; // bookID does not exist
	else if(bookID==1 || bookID==2) {
		selOn(id, true);
	} else if(bookID==3) {
		var formEl = $("prodsel");
		for (var i = 0; i < formEl.options.length; i++) {
	
			if (formEl.options[i].value == id) {
				formEl.selectedIndex = i;
			}
		}
		formEl.disabled = false;	
	}
	if (!ieFixInit) switchForm(id);
	if(ie && !ieFixInit) window.attachEvent("onload", resetPSel);
}
function resetPSel() {
	if ($("prodselector")) initModules(theId, activeForm, true);
}

// switch the traveldetails layer
function switchForm(id) {
	var oact = eval ('modules.' + modules.active);
	var nact = eval('modules.' + id);
	if (oact) { // reset current form
		addCSSClass($(oact), "invisible");
	}
	removeClassName($(nact), "invisible");
	modules.active=id;
	// footer fix
	var pageLoaded = $("end") ? 1 : 0; // all loaded?
	if(pageLoaded) {
		$("footer").style.bottom = "1px";
		setTimeout('$("footer").style.bottom = "0px"', 300);
	}
}

function selOn(id, init) { // switch selector on
	for (var i = 0; i < modules.num; i++) {
		var sel = $('b'+ i);
		if (sel) {
			sel.checked = false;
			sel.disabled = false;
			if (sel.id == id) sel.checked = true;
		}
	}
}

/* ============= great helpers ============= */
function removeClassName (elem, className) {
	if (elem) elem.className = elem.className.replace(className, "").trim(); 
}
function addCSSClass (elem, className) {
	if (elem) {
		removeClassName (elem, className);
		elem.className = (elem.className + " " + className).trim();
	}
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/,"");
}

var calDateObj = null;
/* ============= calendar & aps helpers for external calls from iframe ============= */
function showCal(ddObj) {
	var f_Id, hiddenId;
	f_Id = ddObj.monDD; 
	calDateObj = ddObj;
	// to calculate the position of the calendar frame
	addCSSClass($("apsFrame"), "invisible"); // make sure that the APS is invisible
	var calObj = $("calFrame");
	var offX = 0; var offY = 0;
	var refObj;
	function getBookDist(obj) {
		if(obj.offsetParent.id!="book") {
			offX += obj.offsetParent.offsetTop;
			offY += obj.offsetParent.offsetLeft;
			getBookDist(obj.offsetParent);
		} else {refObj=obj; return true;}
	}
	getBookDist(f_Id);
	calObj.style.top = offX + "px";
	calObj.style.left = offY + "px";
	if(osx) calObj.style.width="19em"; // remember the big carbon scrollers
	removeClassName(calObj, "invisible");
	var frameWin = calObj.contentWindow;
	if (frameWin && !saf) frameWin.location.replace(book_cal);
	else calObj.src = book_cal;
	if(ie) calObj.onblur = hideCurFrame;
}
function hideCal() {
	addCSSClass(document.getElementById("calFrame"), "invisible");
	calDateObj = null;
}
function hideCurFrame(hey) { // is called from doc within the iframe, onblur
	if(hey=="cal") addCSSClass($("calFrame"), "invisible");
	else if(hey=="aps") {
		addCSSClass($("apsFrame"), "invisible");
		addCSSClass($("apsFrame2"), "invisible");
	} else { // when called by hideFrame.html
		addCSSClass($("calFrame"), "invisible");
		addCSSClass($("apsFrame"), "invisible");
		addCSSClass($("apsFrame2"), "invisible");
	}
}
var apField = null;
function showAps(field, dir) {
	addCSSClass($("calFrame"), "invisible"); // make sure that the calendar is invisible
	var apsObj;
	if(dir==1 || dir==3) {
		addCSSClass($("apsFrame2"), "invisible");
		apsObj = $("apsFrame"); 
	} else {
		addCSSClass($("apsFrame"), "invisible");
		apsObj = $("apsFrame2"); 
	}
	var offX = 0; var offY = 0;
	var refObj;
	function getBookDist(obj) {
		if(obj.offsetParent.id!="book") {
			offY += obj.offsetParent.offsetTop;
			offX += obj.offsetParent.offsetLeft;
			getBookDist(obj.offsetParent);
		} else {refObj=obj; return true;}
	}
	getBookDist(field);
	apsObj.style.top = offY + "px";
	apsObj.style.left = offX + "px";
	if(osx) { apsObj.style.width="21em"; apsObj.style.height="26.5em"; } // remember the big carbon scrollers
	removeClassName(apsObj, "invisible");	
	var frameWin = apsObj.contentWindow;
	var apsPage=2; // default
	// dir can be 1 (inbound), 2 (outbound), or 3 (inbound intern.) and 4 (outbound intern.)
	if(dir==1) apsPage = book_aps1;
	else if(dir==2) apsPage = book_aps2;
	else if(dir==3) apsPage = book_aps3;
	else if(dir==4) apsPage = book_aps4;
	if (frameWin && !saf) frameWin.location.replace(apsPage);
	else apsObj.src = apsPage;
	if(ie) apsObj.onblur = hideCurFrame;
	apField = field; // remember this!
}
function setApAndHideAps(ap,cv) {
	if (apField.parent.fromF == apField) {
		if (apField.parent.params.fromF_hasIATA) {
			apField.parent.setFrom(ap, cv);
		} else {
			apField.parent.setFrom(ap);
		}
	} else if (apField.parent.toF == apField) {
		if (apField.parent.params.toF_hasIATA) {
			apField.parent.setTo(ap, cv);
		} else {
			apField.parent.setTo(ap);
		}
	}
	addCSSClass($("apsFrame"), "invisible");
	addCSSClass($("apsFrame2"), "invisible");
	apField = null;
}

/* ============= calendar functions 2 ================ */
var NOW = new Date(); // global right now date
NOW.setHours(0); NOW.setMinutes(0); NOW.setSeconds(0); NOW.setMilliseconds(0); // reset the values w/no importance

DateHelper = function() {};
/** Prints the date in a string according to the given format. */
DateHelper.print = function (str, date) {
	var m = date.getMonth();
	var d = date.getDate();
	var y = date.getFullYear();
	var w = date.getDay();
	var s = {};
	s["%b"] = Calendar._SMN[m]; // abbreviated month name
	s["%B"] = Calendar._MN[m]; // full month name
	s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
	s["%e"] = d; // the day of the month (range 1 to 31)
	s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
	s["%n"] = "\n";		// a newline character
	s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
	s["%Y"] = y;		// year with the century
	s["%%"] = "%";		// a literal '%' character
	
	var re = /%./g;
	var a = str.match(re);
	for (var i = 0; i < a.length; i++) {
		var tmp = s[a[i]];
		if (tmp) {
			re = new RegExp(a[i], 'g');
			str = str.replace(re, tmp);
		}
	}
	return str;
};
DateHelper.getMonthDays = function(month, year) {
	var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
		return 29;
	} else {
		return monthDays[month];
	}
};

/* ============= validation ============= */
var Val = new Object();
Val.checkDate = function(dw, newDt) { // general date validation
	var msg = '';
	if (this.isTooFarFuture(newDt, dw.conf)) msg += Msg.err_future + "\n";
	if (dw.type == 'dep') {
		if (!this.isMinOffset(newDt, dw.conf)) msg += Msg.err_vald + dw.conf.minOffset + "\n";
	}
	if (dw.type == 'arr') {
	    if (dw.depDW) msg += this.isDtAfter(dw.depDW, newDt);
    }
    return msg;
}
Val.isPast = function(newDt) {
	if(NOW.getTime() > newDt.getTime()) return true;
	else return false;
}
Val.isTooFarFuture = function(newDt, conf) { // if d1 is more than 330 days ahead
	if(newDt.getTime() > NOW.getTime() + (1000*60*60*24)* conf.maxMonths * 30) return true;
	else return false;
}
Val.isMinOffset = function(newDt, conf) {
	if (conf.minValOffset && newDt.getTime() >= (NOW.getTime() + conf.minValOffset*(24*60*60*1000))) return true; 
	else if (newDt.getTime() >= (NOW.getTime() + conf.minOffset*(24*60*60*1000))) return true;
	else return false;
}
Val.isMinRetOffset = function(fromDW,toDW) { // offset between deparutre and arrival
	var toD = toDW.date ? toDW.date.getTime() : toDW.getTime();
	var fromD = fromDW.date.getTime() + parseInt(fromDW.conf.minRetOffset)*(24*60*60*1000);
	if (fromDW.conf.minRetValOffset) {
		fromD = fromDW.date.getTime() + parseInt(fromDW.conf.minRetValOffset)*(24*60*60*1000);
		if(toD >= fromD) return '';
 	    else return Msg.err_ret_min;
	} else {
	if(toD >= fromD) return '';
	else return Msg.err_ret_soon + fromDW.conf.minRetOffset + "\n";
	}
}
Val.checkDMYSet = function(dw) { // check if date of birst is set
	if (dw.conf.dobSep) {
		if (dw.dayDD.selectedIndex > 1 && dw.monDD.selectedIndex > 1 && dw.yearDD.selectedIndex > 1) return '';
		else return Msg.err_dob + "\n";
	}
}
Val.isDtAfter = function(fromDW, toDW) { // return is after departure
	var toD = toDW.date ? toDW.date.getTime() : toDW.getTime();
	if (fromDW.date.getTime() > toD) return Msg.err_return  + "\n"; 
	else return '';
}
Val.verifyDates = function (fromDW, toDW) { // validation of dates
	var msg = '';
	if (!this.isMinOffset(fromDW.date, fromDW.conf)) msg += Msg.err_vald + (fromDW.conf.minValOffset ? fromDW.conf.minValOffset : fromDW.conf.minOffset) + "\n";
	if (this.isTooFarFuture(fromDW.date, fromDW.conf) || this.isTooFarFuture(toDW.date, toDW.conf)) msg += Msg.err_future + "\n";
	if (!toDW.disabled) {
		msg += Val.isDtAfter(fromDW, toDW);
	}
	return msg;
}
Val.checkFt = function(ft, compare) { // check if from and to fields are set
	var msg = '';
	if (ft) {
		if (ft.fromF.options && ft.fromF.options[ft.fromF.selectedIndex].value == "") { // "Please Select" option
			msg += Msg.err_airport + "\n";
			return msg;	
		}
		if  (ft.toF && ft.toF.value == '') msg += Msg.err_airport + "\n";
		if (compare) {
			msg += Val.compareFt(ft);
		}
	}
	return msg;
}

Val.compareFt = function(ft) { // compare airports
	var msg = '';
	var failed = false;
	if (ft.fromF.type == ft.toF.type) { // same field types
	  if (ft.fromF.type == "text") { // both are free text
	  	 failed = (ft.getFrom().trim().toLowerCase() == ft.getTo().trim().toLowerCase());
	  } else { // both are lists
		 failed = (ft.getFrom() == ft.getTo());
 	  }
	} else if (ft.fromF.options) { // compare 'from' list with 'to' free text
		failed = (ft.getFrom().trim().toLowerCase() == ft.getTo().trim().toLowerCase()); // compare option value to freetext
		if (!failed) { // check for TLC code in airportatlas string, eg. "BERN /BRN"
			var airport = ft.getTo().split("/");
			if (airport.length > 1) {
				failed = (ft.getFrom().trim().toLowerCase() == airport[1].trim().toLowerCase());
			}
		}
		if (!failed) {
			failed = (ft.fromF.options[ft.fromF.selectedIndex].text.trim().toLowerCase() == ft.getTo().trim().toLowerCase()); // compare option text to freetext
		}
	}
	msg += failed ? (Msg.err_airport_eq + "\n") : '';
	return msg;
}

Val.checkTar = function(elem) { // check tarif, if not adult, date of birth must be set
	if (elem && elem.depDD != null && elem.tarDD.options[elem.tarDD.selectedIndex].value != "adult") {
		var minInd = (elem.conf.dobSep) ? 1 : 0;
		if (elem.depDD.dayDD.selectedIndex <=minInd || elem.depDD.monDD.selectedIndex <= minInd || elem.depDD.yearDD.selectedIndex <= minInd) {
			return Msg.err_dob + "\n";
		}
	}
	return '';
}
Val.checkPxTar = function(adults, youth, students) {
	if (adults.getVal() > 0 && (youth.getVal() > 0 || students.getVal() > 0)) return Msg.err_tarif + "\n";
	if (adults.getVal() == 0 && youth.getVal() == 0 && students.getVal() == 0) return Msg.err_pxnum + "\n";
	return '';
}
/* =========== base dropdown list =========== */
ListWidget = function () {};
ListWidget.prototype.kill = function(elem) { // clear child nodes
	var counter = elem.length;
	for(var i=0; i<counter; i++) {
		elem.remove(0);
	}
}
ListWidget.prototype.getOpt = function(text, value) { // create an optionlist
	opt = document.createElement("option");
	opt.text =  text;
	opt.value = value;
	return opt;
}

ListWidget.prototype.addOpt = function(obj, text, value) { // add an optionlist
	opt = this.getOpt(text,value);
	if(saf) obj.appendChild(opt); // Safari does not understand "add"
	 else obj.options.add(opt);
}


/* ============= base date dropdown ============= */
DDWidget = function() {
	this.date = null;
};
DDWidget.prototype = new ListWidget;
DDWidget.prototype.calcDays = function(nokill) {//recalculate day dropdown list
	var mNum = DateHelper.getMonthDays(this.date.getMonth(), this.date.getFullYear());
	if (!nokill) this.kill(this.dayDD);
	for(var i=1; i<=mNum; i++) {
		var dayNum = (i<10) ? ('0'+i) : i;
		this.addOpt(this.dayDD, dayNum, dayNum);
	}
}
DDWidget.prototype.getD = function(pattern) { // day, optional pattern
	if (this.date != null) {
		if (pattern) return DateHelper.print(pattern, this.date);
		else return this.date.getDate();
	}
}
DDWidget.prototype.getM = function(pattern) { // month, optional pattern
	if (this.date != null) {
		if (pattern) return DateHelper.print(pattern, this.date);
		else return this.date.getMonth();
	}
}
DDWidget.prototype.getY = function(pattern) { // year, optional pattern
	if (this.date != null) {
		if (pattern) return DateHelper.print(pattern, this.date);
		else return this.date.getYear();
	}
}
DDWidget.prototype.getDateStr = function(pattern) { // year, optional pattern
	if (this.date != null) {
		return DateHelper.print(pattern, this.date);
	}
}

/* ============= Date Double DropDown Object ============= */
DMWidget = function (params) { // dayID, mon+year ID, form config object, departure widget
	this.dayDD = $(params.dayID); // day DropDown
	this.monDD = $(params.monID); // monthyear DropDown
	this.dayDD.parent = this.monDD.parent = this;
	this.conf = params.formDef.conf;
	this.depDW = params.depDW;
	this.type = params.type; // dep or arr
	this.precalc = params.precalc; // precalculate return date, if arrival date is set?
	if (this.depDW && this.precalc) {
		this.depDW.depDW = this; // register arr widget within the dep widget
		this.depDW.precalc = this.precalc;	// tell the dep widget to update the arr date on change
	}
	this.date = null;
	this.disabled = false;
	this.mddValPattern = "%Y-%m";
	this.init();
	this.monDD.onchange = function() {
		this.parent.updateMdd();
	}
	this.dayDD.onchange = function() {
		this.parent.updateDdd();
	}
}

DMWidget.prototype = new DDWidget;
DMWidget.prototype.init = function () { // initializer
	if(this.conf.minOffset != null) {
		var offset = parseInt(this.conf.minOffset) + parseInt((this.depDW) ? this.conf.minRetOffset : 0);
		this.date = new Date(NOW.getTime() + offset *(24*60*60*1000));
	} else {
		this.date = new Date(NOW.getTime());
	}
	var m = this.date.getMonth();
	var y = this.date.getFullYear();
	var tempDate = new Date(y, m, 1);
	this.kill(this.monDD);
	if (this.conf.checkPastOnly && NOW.getMonth() < m) {
		if (m > 0) {
			m--;
		} else {
			m = 11;
		}
	}
	for(var i=0; i<=this.conf.maxMonths; i++) {
		
		if(m+1 == 13) {m=0; y++;}
		tempDate.setMonth(m);
		tempDate.setYear(y);
		this.addOpt(this.monDD, DateHelper.print(this.conf.mddVisPattern, tempDate), y + "-" + m);
		m++;
	}
	this.calcDays();
	if (this.conf.checkPastOnly && NOW.getMonth() < this.date.getMonth()) {
	  this.monDD.selectedIndex = 1;
	}
	this.dayDD.selectedIndex = this.date.getDate()-1;
}
DMWidget.prototype.updateMdd = function() { // monthyear dropdown
	var selDay = (this.dayDD.selectedIndex == -1) ? 1 : this.dayDD.selectedIndex + 1;
	var year_month = this.monDD.options[this.monDD.selectedIndex].value.split("-");
	selDay = selDay > DateHelper.getMonthDays(year_month[1], year_month[0]) ? 1 : selDay;
	this.date.setDate(selDay);
	this.date.setMonth(year_month[1]);
	this.date.setYear(year_month[0]);
	this.calcDays();
	this.dayDD.selectedIndex = this.date.getDate()-1;
	if (this.type == 'dep' && this.precalc && this.depDW) {
		this.depDW.update(new Date(this.date.getTime() + this.conf.minRetOffset*(24*60*60*1000)), true);
	}
}
DMWidget.prototype.updateDdd = function() { // day dropdown
	this.date.setDate((this.dayDD.selectedIndex == -1) ? 1 : this.dayDD.selectedIndex + 1);
	if (this.type == 'dep' && this.precalc && this.depDW) {
		this.depDW.update(new Date(this.date.getTime() + this.conf.minRetOffset*(24*60*60*1000)), true);
	}
}
DMWidget.prototype.update = function(newDate, isDropDown) { //update the dropdowns, with newdate if necessary (calendar popup), if its from dropdown, don't check precalc
	if (newDate) {
		newDate.setHours(0); newDate.setMinutes(0); newDate.setSeconds(0); newDate.setMilliseconds(0);
		var msg = '';
		if (!isDropDown) {
			if (this.conf.checkPastOnly) { // simple past check
				msg = (Val.isPast(newDate)) ? Msg.err_past + "\n" : '';
			} else {
				msg = Val.checkDate(this, newDate);
			}
		}
		if (msg != null && msg != '') {
			alert(msg);
		    return;
		} else {this.date = newDate;}
	}
	if (this.type == 'dep' && this.precalc && this.depDW && !isDropDown) { // update from calendar? precalculate arrival
		this.depDW.update(new Date(this.date.getTime() + this.conf.minRetOffset*(24*60*60*1000)), true);
	}
	var els = this.monDD.getElementsByTagName("option");
	for(var i = 0; i < els.length; i++) { // set the dropdown for months
		if(els[i].value == (this.date.getFullYear() + "-" + this.date.getMonth()) ) this.monDD.selectedIndex = i;
	}
	this.calcDays();
	this.dayDD.selectedIndex = this.date.getDate()-1;
}

DMWidget.prototype.toggle = function (status) { // status true for enabled | false for disabled
	this.disabled = status;
	this.dayDD.disabled = status;
	this.monDD.disabled = status;
}

/* ============= Date of Birth Drop Down ============= */
DMYWidget = function(params) { // Constructor
	this.dayDD = $(params.dayID); // day DropDown
	this.monDD = $(params.monID); // month DropDown
	this.yearDD = $(params.yearID); // month DropDown
	this.dayDD.parent = this.monDD.parent = this.yearDD.parent = this; // reference to widget
	this.conf = params.formDef.conf;
	this.date = null;
	this.init(); // init the date and fill the dropdowns
	// event handler:
	this.dayDD.onchange = this.monDD.onchange = this.yearDD.onchange = function(evt) {this.parent.update(evt);}
}
DMYWidget.prototype = new DDWidget;
DMYWidget.prototype.init = function () { // initializer
	this.date = new Date(NOW.getFullYear(), 0, 1);
	var tempDate = new Date(this.date.getTime());
	this.kill(this.monDD);
	this.addOpt(this.monDD, this.conf.dobVisM, '');
	this.addOpt(this.monDD, this.conf.dobVisM.replace(/./g, this.conf.dobSep), '');
	for(var m=0; m<12; m++) {
		tempDate.setMonth(m);
		this.addOpt(this.monDD, DateHelper.print("%m", tempDate), m);
	}
	this.kill(this.yearDD);
	this.addOpt(this.yearDD, this.conf.dobVisY, '');
	this.addOpt(this.yearDD, this.conf.dobVisY.replace(/./g, this.conf.dobSep), '');
	for(var y=this.date.getFullYear(); y > this.date.getFullYear() - this.conf.maxDobY; y--) {
		tempDate.setYear(y);
		this.addOpt(this.yearDD, DateHelper.print("%Y", tempDate), y);
	}
	this.kill(this.dayDD);
	this.addOpt(this.dayDD, this.conf.dobVisD, '');
	this.addOpt(this.dayDD, this.conf.dobVisD.replace(/./g, this.conf.dobSep), '');
	this.calcDays(true);
}
DMYWidget.prototype.update = function(evt) { // update the date
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	var elem = (evt.target) ? evt.target : evt.srcElement; // get the object which fired the event
	if (elem.options[elem.selectedIndex].value == '') return;
	if (elem == this.dayDD) {
		this.date.setDate(elem.options[elem.selectedIndex].value);
	} else {
		var recalc = false;
		if (this.dayDD.options[this.dayDD.options.length-1].value > DateHelper.getMonthDays(this.monDD.options[this.monDD.selectedIndex].value, this.yearDD.options[this.yearDD.selectedIndex].value)) {// check if the day is still available in selected month
			this.date.setDate(1);
			recalc = true;
		} else if (this.dayDD.options[this.dayDD.options.length-1].value != DateHelper.getMonthDays(this.monDD.options[this.monDD.selectedIndex].value, this.yearDD.options[this.yearDD.selectedIndex].value)) { // check if the new month has more days
			recalc = true;
		}
		if (elem == this.monDD) {
			this.date.setMonth(elem.options[elem.selectedIndex].value);
		} else if (elem == this.yearDD) {
			this.date.setYear(elem.options[elem.selectedIndex].value);
		}
		if (recalc) { // recalculate day dropdown if necessary
			this.kill(this.dayDD);
			this.addOpt(this.dayDD, this.conf.dobVisD, '');
			this.addOpt(this.dayDD, this.conf.dobVisD.replace(/./g, this.conf.dobSep), '');
			this.calcDays(true);
			this.dayDD.selectedIndex = this.date.getDate() + (this.conf.dobSep ? 1 : 0);
		}
	}
}
DMYWidget.prototype.disable = function(reset) {// opitonal reset the dropdowns
	this.dayDD.disabled = this.monDD.disabled = this.yearDD.disabled = true;
	if (reset) {
		this.dayDD.selectedIndex = this.monDD.selectedIndex = this.yearDD.selectedIndex = 0;
	}
}
DMYWidget.prototype.enable = function(reset) {
	this.dayDD.disabled = this.monDD.disabled = this.yearDD.disabled = false;
	if (reset) {
		this.dayDD.selectedIndex = this.monDD.selectedIndex = this.yearDD.selectedIndex = 0;
	}
}
TARWidget = function (params) { // "Tarif" dropdown widget, holds the dependency to a date of birth element and number of travellers
	this.tarDD = $(params.tarDDid); // tarif dropdown
	this.tarDD.parent = this;
	this.conf = params.formDef.conf;
	this.depDD = (params.depElem) ? params.depElem : null; // dependent DoB object
	this.pxnumDD = params.pxnumElem; // number of traveles dropdown
	this.pxNum = params.pxNum;
	this.init();
}
TARWidget.prototype.init = function() {
	this.tarDD.selectedIndex = 0;
	if (this.pxnumDD) {
		eval("this.pxnumDD.update(this.pxNum. " + this.tarDD.options[this.tarDD.selectedIndex].value + ");");
	}
	if (this.depDD != null) {
		if (this.tarDD.options[this.tarDD.selectedIndex].value == "adult") {this.depDD.disable(true);}
		this.tarDD.onchange = function() {
			if (this.options[this.selectedIndex].value != "adult") {this.parent.depDD.enable();}
			else {this.parent.depDD.disable(true);}
			eval("this.parent.pxnumDD.update(this.parent.pxNum. " + this.options[this.selectedIndex].value + ");"); // set the numbers of passengers
		}
	}
}
TARWidget.prototype.getVal = function() { // returns selected value
	if (this.tarDD) {
		return this.tarDD.options[this.tarDD.selectedIndex].value;
	}
}

/* From / To airports handling */
var FTWidget = function(params) { // from and to field accessor widget
	this.fromF = $(params.fromID);
	this.toF = $(params.toID);
	this.params = params;
	if (this.params.toF_hasIATA) {
		this.toF_IATA = '';
	}
	if (this.params.fromF_hasIATA) {
		this.fromF_IATA = '';
	}
	this.fft_i = null;
	this.fromF.parent = this.toF.parent = this;
}
FTWidget.prototype.getFrom = function (asIATA) {
	if (asIATA && this.fromF_IATA) {
		return this.fromF_IATA;
	} 
	return this.getVal(this.fromF);
}
FTWidget.prototype.getTo = function (asIATA) {
	if (asIATA && this.toF_IATA) {
		return this.toF_IATA;
	} return this.getVal(this.toF);
}
FTWidget.prototype.getVal = function (field) {
	if (field) {
		if (field.type == "select") return field.options[field.selectedIndex].value;
		else return field.value;
	}
}
FTWidget.prototype.setTo = function (value, IATA) {
  try { // values from autosuggest function?
  	if (window.fft && (window.fft.params.toID + "_iata") == value.tarId) {
  		with (window.fft) {
  			toF.value = value.inpVal;
  			toF_IATA = value.tarVal;
  		}
  		if (window.fft_i != null) window.fft_i.setFrom(value.inpVal, value.tarVal);
  	} else if (window.ffti && (window.ffti.params.toID + "_iata") == value.tarId) {
  		with (window.ffti) {
  			toF.value = value.inpVal;
  			toF_IATA = value.tarVal;
  		}
  	} else if (this.toF && !this.toF.selectedIndex) {
		this.toF.value = value;
		if (this.fft_i != null) this.fft_i.fromF.value = value;
		if (this.params.toF_hasIATA && IATA) {
			this.toF_IATA = IATA;
			if (this.fft_i != null && this.fft_i.params.fromF_hasIATA && IATA) this.fft_i.fromF_IATA = IATA;
		}
  	}
  } catch (e) {}
	
}
FTWidget.prototype.setFrom = function (value, IATA) {
    try { // values from autosuggest function?
		if (window.fft && (window.fft.params.toID + "_iata") == value.tarId) {
	  		with (window.fft) {
	  			fromF.value = value.inpVal;
	  			fromF_IATA = value.tarVal;
	  		}
	  	} else if (window.fft_i && (window.fft_i.params.toID + "_iata") == value.tarId) {
	  		with (window.fft_i) {
	  			fromF.value = value.inpVal;
	  			fromF_IATA = value.tarVal;
	  		}
	  	} else if (this.fromF && !this.fromF.selectedIndex) {
			this.fromF.value = value;
			if (this.params.fromF_hasIATA && IATA) {
				this.fromF_IATA = IATA;
			}
		}
  	} catch (e) {}
	
}
FTWidget.prototype.setDependency = function (fft_i) { // open jaw
	if (fft_i) {
		this.fft_i = fft_i;
	}
}
PXWidget = function(params, init) { // Constructor Number of Travellers list
	this.pxnumDD = $(params.pxnumID); // day DropDown
	this.conf = params.formDef.conf;
	if (init) this.init(); // init the dropdown, optional. may be done from Tarif widget
}
PXWidget.prototype = new ListWidget;
PXWidget.prototype.init = function () { // initializer
	this.kill(this.pxnumDD);
	for(var i=1; i<=4; i++) {
		this.addOpt(this.pxnumDD, i, i);
	}
}
PXWidget.prototype.update = function (num) { // initializer
	this.kill(this.pxnumDD);
	for(var i=1; i<=num; i++) {
		this.addOpt(this.pxnumDD, i, i);
	}
}
PXWidget.prototype.getVal = function() { // returns selected value
	if (this.pxnumDD) {
		return this.pxnumDD.options[this.pxnumDD.selectedIndex].value;
	}
}

function toggleOneway(elem, toDW, toTimeID, aDepId) { // disable return date, return time and inbound departure, if checked
	if (elem && toDW) {
	  toDW.toggle(elem.checked);
	  if (toTimeID) {
	  	var toTimeElem = $(toTimeID);
	  	if (toTimeElem) {
	  		toTimeElem.disabled = elem.checked;
	  	}
	  }
	  if (aDepId && $(aDepId)) {
	  	$(aDepId).disabled = elem.checked;
	  }
	}
}
/* ============= Simple Date info ============= */
CheckoutWidget = function(params) { // Constructor
	this.dateString = $(params.dateID); //
	this.date = null;
	this.conf = params.formDef.conf;
	this.nightDD = $(params.nightID);
	this.nightDD.parent = this;
	this.depDW = params.depDW;
	this.precalc = params.precalc; // precalculate checkout date, if check in date is set?
	if (this.depDW && this.precalc) {
		this.depDW.depDW = this; // register this widget within the dep widget
		this.depDW.precalc = this.precalc;	// tell the dep widget to update the check out date on change
	}
	this.init();
	this.nightDD.onchange = function() {
		var offset = this.options[this.selectedIndex].value;
		this.parent.update(new Date(this.parent.depDW.date.getTime() + offset *(24*60*60*1000)));
	}
}

CheckoutWidget.prototype = new DDWidget;
CheckoutWidget.prototype.init = function () {
	var offset = this.nightDD.options[this.nightDD.selectedIndex].value;
	this.date = new Date(this.depDW.date.getTime() + offset *(24*60*60*1000));
	this.dateString.innerHTML = this.getDateStr("%d-%b-%Y");
}

CheckoutWidget.prototype.update = function (newDate) {
	var offset = this.nightDD.options[this.nightDD.selectedIndex].value
	if (newDate) {
		newDate.setHours(0); newDate.setMinutes(0); newDate.setSeconds(0); newDate.setMilliseconds(0);
		this.date = new Date(this.depDW.date.getTime() + offset *(24*60*60*1000));;
	}
	this.dateString.innerHTML = this.getDateStr("%d-%b-%Y");
}
