function Dashboard (home, selected, app) {
	this.container = document.getElementById('dashboard');
	home ? this.home = true : this.home = false;
	app ? this.app = true : this.app = false;
	this.tablist = document.getElementById('tabs');
	this.tabs = this.tablist.getElementsByTagName('li');
	this.toolContainer = document.getElementById('tool');
	this.initTabs();
	this.selectedTab = null;
	if (!this.home) {
		this.bottomImage = document.getElementById('db_bottom');
		this.toolContainer.style.display = 'none';
	} else {
		if (selected)
			this.selectTab(this.getTabIndex(selected))
		else
			this.selectTab(0);
	}
	this.sourceFrame = new RPCFrame(window);
}
Dashboard.prototype.initTabs = function () {
	for (var i = 0; i < this.tabs.length; i++) {
		this.initTab(i);
	}
}
Dashboard.prototype.initTab = function (i) {
	var li = this.tabs[i];
	li.a = li.getElementsByTagName('a')[0];
	if (this.app) li.a.href = 'emptycontentdashboardpage.html?db=' + li.id;
	//if user is logged in (jffp cookie) then set ici tab to jffp version
	if (checkLoggedin && li.id == "db_ici") {
		li.id = "db_ici_jffp";
	}
	if (i == 0) li.className = 'first';
	li.dashboard = this;
	li.onclick = function () {
		if (this.dashboard.app) {
			document.location.href = 'emptycontentdashboardpage.html?db=' + this.id;
		} else {
			this.a.blur();
			this.dashboard.openTab(this.id);
		}
	}
}
Dashboard.prototype.openTab = function (id) {
	if (id == this.selectedTab && (!this.home)) {
		this.close();
		return;
	}
	this.selectTab(id);
	this.loadtext = setTimeout(createContextFunction(this, "showLoadText"), 500);
	this.sourceFrame.setLocation(id + ".html", createContextFunction(this, "loadContent"));
	if (!this.home) this.bottomImage.style.display = 'none';
}
Dashboard.prototype.showLoadText = function () {
	this.toolContainer.innerHTML = "loading...";
}
Dashboard.prototype.loadContent = function () {
	if (this.loadtext) {clearTimeout(this.loadtext);this.loadtext = null}
	this.toolContainer.innerHTML = this.sourceFrame.getHTMLById('content').innerHTML;
	this.toolContainer.style.display = 'block';
	if (!this.home) this.createCloser();
}
Dashboard.prototype.createCloser = function () {
	var c = document.createElement('div');
	c.id = 'db_close';
	c.onclick = createContextFunction(this, "close");
	this.toolContainer.appendChild(c);
}
Dashboard.prototype.getTabIndex = function (selected) {
	for (var i=0; i < this.tabs.length; i++) {
		if (this.tabs[i].id == selected) return i;
	}
	return 0;
}
Dashboard.prototype.selectTab = function (i) {
	if (this.selectedTab != null) {
		this.tabs[this.selectedTab].className =  this.tabs[this.selectedTab].className.replace('selected', '');
	}
	this.tabs[i].className += 'selected';
	this.selectedTab = i;
}
Dashboard.prototype.close = function () {
	if (this.selectedTab != null) this.tabs[this.selectedTab].className =  this.tabs[this.selectedTab].className.replace('selected', '');
	this.selectedTab = null;
	this.toolContainer.style.display = 'none';
	if (!this.home) this.bottomImage.style.display = 'block';
}
/* calendar functions */
var type = '';
var days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];

function showCalendar(depret, txt) {
	top.type = depret;
	var container = document.getElementById('calendarcontainer');
	var header = document.getElementById('calendartop');
	header.innerHTML = txt; 
	if (document.all) {
		var entries = document.getElementsByTagName('select');
		for (var i = 0; i < entries.length; i++) {
			if (entries[i].className == 'cal_hide') {
				entries[i].style.display = 'none';
			}
		}
	}
	var today = new Date();
	document.getElementById('calendarmonth').options.selectedIndex = 0;
	updateMonth((today.getMonth() + 1) + '_' + today.getFullYear(), 0);
	container.style.display = 'block';
}
function fill(type, day, month) {
	closeCalendar();
	document.getElementById(type + '_day').options.selectedIndex = day;
	document.getElementById(type + '_month').options.selectedIndex = month;
}
function closeCalendar () {
	var container = document.getElementById('calendarcontainer');
	if (document.all) {
		var entries = document.getElementsByTagName('select');
		for (var i = 0; i < entries.length; i++) {
			if (entries[i].className == 'cal_hide') {
				entries[i].style.display = 'block';
			}
		}
	}
	container.style.display = 'none';
}
function updateMonth(monthyear, selection) {
	var month = monthyear.split('_')[0];
	var year = monthyear.split('_')[1];
	var noOfDays = getNumberOfDays(month, year);
	var firstDay = getFirstDay(month, year);
	fillMonth(month, firstDay, noOfDays, selection);
}
function getNumberOfDays(m, y) {
	var days = 31;
	switch (parseInt(m)) {
		case 4: case 6: case 9: case 11:
			days = 30;
			break;
		case 2:
		  if ((y % 4 == 0) ^ (y % 100 == 0) ^ (y % 400 == 0))
			days = 29;
		  else
			days = 28;
		  break;
	}
	return days;
}
function getFirstDay(m, y) {
	d = new Date();
	d.setYear(y);
	d.setMonth(m-1);
	d.setDate(1);
	d.setHours(12);
	return (d.getDay() - 1 >= 0 ? d.getDay() - 1 : d.getDay() + 6);
}
function fillMonth(month, firstDay, noOfDays, monthIndex) {
	var firstSet = false;
	var dayCounter = 1;
	var today = new Date();
	dateToday = today.getDate();
	monthToday = today.getMonth() + 1;
	var sHTML = '<table cellspacing="0"><tr class="head">'
	for (var i = 0; i < days.length; i ++) {
		sHTML += '<td>' + days[i] + '</td>\n';
	}
	sHTML += '</tr>';
	while (dayCounter <= noOfDays) {
		sHTML += '<tr>';
		for (i = 0; i < 7; i++) {
			if (!firstSet && i < firstDay) {
				sHTML += '<td>&nbsp;</td>\n';
			} else {
				firstSet = true;
				if (dayCounter <= noOfDays) {
					if ((monthToday == month) && (dayCounter < dateToday)) {
						sHTML += '<td>' + dayCounter + '</td>\n';
					} else {
						sHTML += '<td><a href="javascript:fill(top.type, ' + (dayCounter - 1) + ' , ' + monthIndex + ');">' + dayCounter + '</a></td>\n';
					}

				} else {
					sHTML += '<td>&nbsp;</td>\n';
				}
				dayCounter++;
			}
		}
		sHTML += '</tr>'
	}
	sHTML += '</table>';
	document.getElementById('monthtable').innerHTML = sHTML;
}
/* find destination functions */
var destFinder = null;

function showDestination() {
	if (!destFinder) 
		destFinder = new DestinationFinder();
	else
		destFinder.open();
}

function DestinationFinder () {
	this.container = document.getElementById('destinationcontainer');
	this.closeBut = document.getElementById('closed');
	this.closeBut.onclick = createContextFunction(this, "close");
	this.src = new RPCFrame(window);
	this.countrySelectbox = document.getElementById('dest_country');
	this.countrySelectbox.onchange = createContextFunction(this, "updateAirports", "updateAirports");
	this.aiportSelectbox = document.getElementById('dest_airport');
	this.sel = document.getElementById('sel');
	this.sel.onclick = createContextFunction(this, "fillDest");
	this.countries = new Array();
	this.src.setLocation('/showdestinations.xml', createContextFunction(this, "fillCountries"));
	this.open();
	return this;
}
DestinationFinder.prototype.close = function () {
	var bookingbtn = document.getElementById('bookingbtn');
	bookingbtn.style.display = 'block';
	this.container.style.display = 'none'
	if (document.all) {
		var entries = document.getElementsByTagName('select');
		for (var i = 0; i < entries.length; i++) {
			if (entries[i].className == 'dest_hide') {
				entries[i].style.display = 'block';
			}
		}
	}
}
DestinationFinder.prototype.open = function () {
	var bookingbtn = document.getElementById('bookingbtn');
	bookingbtn.style.display = 'none';
	this.container.style.display = 'block'
	if (document.all) {
		var entries = document.getElementsByTagName('select');
		for (var i = 0; i < entries.length; i++) {
			if (entries[i].className == 'dest_hide') {
				entries[i].style.display = 'none';
			}
		}
	}
}

DestinationFinder.prototype.fillCountries = function () {
 var entries = this.src.getHTMLByTag('li');
 for (var i = 0; i  < entries.length; i++) {
  if (entries[i].parentNode.parentNode.tagName.toUpperCase() == 'DIV') this.fillCountry(entries[i]);
 }
 this.fillselectBox();
 this.updateAirports();
}

DestinationFinder.prototype.fillCountry = function (countryNode) {
 this.countries[this.countries.length] = new Country(countryNode.innerHTML.substring(0,countryNode.innerHTML.toUpperCase().indexOf('<UL>')));
 this.fillAirports(countryNode, this.countries.length - 1);
}

DestinationFinder.prototype.fillAirports = function (countryNode,countryIdx) {
 var entries = countryNode.getElementsByTagName('li');
 for (var i = 0; i  < entries.length; i++) {
  if (entries[i].parentNode.parentNode == countryNode) this.countries[countryIdx].airports[this.countries[countryIdx].airports.length] = entries[i].innerHTML;
 }
}

DestinationFinder.prototype.getAirportsFromCity = function (cityNode, countryIdx) {
	var entries = cityNode.getElementsByTagName('li');
	for (var i = 0; i  < entries.length; i++) {
		if (entries[i].parentNode.parentNode == cityNode) {
			this.countries[countryIdx].airports[this.countries[countryIdx].airports.length] = cityNode.innerHTML.substring(0, cityNode.innerHTML.toUpperCase().indexOf('<UL>')) + ' (' + entries[i].innerHTML + ')';
		}
	}
}

DestinationFinder.prototype.fillselectBox = function () {
	var newOption;
	for (var i = 0; i  < this.countries.length; i++) {
		newOption = document.createElement('option')
		newOption.innerHTML = this.countries[i].name;
		this.countrySelectbox.appendChild(newOption);
	}
}

DestinationFinder.prototype.updateAirports = function () {
 while (this.aiportSelectbox.hasChildNodes()) {
  this.aiportSelectbox.removeChild(this.aiportSelectbox.firstChild);
 }

 var newOption;
 var c = this.countrySelectbox.selectedIndex;
 if (c >= 0)
  for (var i = 0; i  < this.countries[c].airports.length; i++) {
   newOption = document.createElement('option');
   newOption.innerHTML = this.countries[c].airports[i];
   this.aiportSelectbox.appendChild(newOption);
 }
}

function Country (name) {
	this.name = name;
	this.airports = new Array();
}

DestinationFinder.prototype.fillDest = function () {
	this.destination = this.aiportSelectbox.options[this.aiportSelectbox.options.selectedIndex].innerHTML;
	for(i=0;i<arDestinations.length;i++) {
		if (arDestinations[i] == this.destination) {
			dest_code = i;
		}
	}
	document.getElementById('dest_fill').value = this.aiportSelectbox.options[this.aiportSelectbox.options.selectedIndex].innerHTML;
	document.getElementById('dest_code_fill').value = arDestinationIDs[dest_code];
	this.close();
}

function switchBox(action, element) {	
	
	if(action == 'show'){
		document.getElementById(element).style.display = 'block';
	}
	if(action == 'hide'){
		document.getElementById(element).style.display = 'none';
	}
	
}
