// submit helper
function getLinkName(mode) {
	document.blah.mode.value = mode;
	if(document.blah.mode.value != '') {
		document.blah.submit();
	}
}


// Switch Divs
function switchDiv(id) {
	if(document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == '')
	document.getElementById(id).style.display = 'block';
	else
	document.getElementById(id).style.display = 'none';
}

function confirmCancel(url) {
	if(confirm("Do you really want to cancel? All current made changes will be lost.")) {
		window.location = url;
	}
}

function confirmDelete(url) {
	if(confirm("Do you really want to delete? All data will be eliminated.")) {
		window.location = url;
	}
}



function switchMap(groundicao, combineicao) {
	newimg     = new Image();
	newimg.src = 'map.php?int_airport='+groundicao+'&dest_airport='+combineicao;
	document.getElementById('mapoverview').src = newimg.src;
}

// Swap Content Function

function swapContent(fieldId, form) {

	for(var i = 0; i < form.elements[0].length; i++) {
		if(document.getElementById(form.elements[0].options[i].value)) {
			if(form.elements[0].options[i].value == fieldId) {
				document.getElementById(form.elements[0].options[i].value).style.display = 'block';
			}
			else {
				document.getElementById(form.elements[0].options[i].value).style.display = 'none';
			}
		}
	}
}

// Javascript links

function linkto(url) {
	window.location.href='' + url + '';
}
function postData(jumpTo,form) {
	document.getElementById(form).jump.value = jumpTo;
	if (document.getElementById(form).jump.value != "") {
		document.getElementById(form).submit();
	}
}

// Tooltip Event Handling

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

/*
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/
var sweetTitles = {
	xCord : 0,								// @Number: x pixel value of current cursor position
	yCord : 0,								// @Number: y pixel value of current cursor position
	tipElements : ['a','abbr','acronym'],	            // @Array: Allowable elements that can have the toolTip
	obj : Object,							// @Element: That of which you're hovering over
	tip : Object,							// @Element: The actual toolTip itself
	active : 0,								// @Number: 0: Not Active || 1: Active
	init : function() {
		if ( !document.getElementById ||
		!document.createElement ||
		!document.getElementsByTagName ) {
			return;
		}
		var i,j;
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		document.getElementsByTagName('body')[0].appendChild(this.tip);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ ) {
			var current = document.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ ) {
				// Only create Tooltips when there is a title
				if (current[j].getAttribute('title') != null &&
				current[j].getAttribute('title') != '') {
					addEvent(current[j],'mouseover',this.tipOver);
					addEvent(current[j],'mouseout',this.tipOut);
					current[j].setAttribute('tip',current[j].title);
					current[j].removeAttribute('title');
				} // END
			}
		}
	},
	updateXY : function(e) {
		if ( document.captureEvents ) {
			sweetTitles.xCord = e.pageX;
			sweetTitles.yCord = e.pageY;
		} else if ( window.event.clientX ) {
			sweetTitles.xCord = window.event.clientX+document.documentElement.scrollLeft;
			sweetTitles.yCord = window.event.clientY+document.documentElement.scrollTop;
		}
	},
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		sweetTitles.tip.style.visibility = 'hidden';
	},
	checkNode : function() {
		var trueObj = this.obj;
		if ( this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	tipOver : function(e) {
		sweetTitles.obj = this;
		tID = window.setTimeout("sweetTitles.tipShow()",25);
		sweetTitles.updateXY(e);
	},
	tipShow : function() {
		var scrX = Number(this.xCord);
		var scrY = Number(this.yCord);
		var tp = parseInt(scrY+15);
		var lt = parseInt(scrX+10);
		var anch = this.checkNode();
		var addy = '';
		var access = '';
		if ( anch.nodeName.toLowerCase() == 'a' ) {
			addy = (anch.href.length> 32 ? anch.href.toString().substring(0,32)+"..." : anch.href);
			addy = addy.replace('http://','');
			if ( !addy.search('www.flymi.')) addy = '';
			var access = ( anch.accessKey ? ' <span>['+anch.accessKey+']</span> ' : '' );
		} else {
			addy = anch.firstChild.nodeValue;
		}
		this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"<em style='display: none;'>"+access+addy+"</em></p>";
		if ( parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) <parseInt(this.tip.offsetWidth+lt) ) {
			this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
		} else {
			this.tip.style.left = lt+'px';
		}
		if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) <parseInt(this.tip.offsetHeight+tp) ) {
			this.tip.style.top = parseInt(tp-(this.tip.offsetHeight+10))+'px';
		} else {
			this.tip.style.top = tp+'px';
		}
		if (addy || anch.getAttribute('tip')) this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '.1';
		this.tipFade(10);
	},
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 80 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}
		else {
			this.tip.style.opacity = '1.0';
			this.tip.style.filter = "alpha(opacity:100)";
		}
	}
};
function pageLoader() {
	sweetTitles.init();
}

//Display Layers on MouseClick

function getPosXY(e) {
	var x = (ie) ? event.clientX + document.documentElement.scrollLeft : e.pageX;
	var y = (ie) ? event.clientY + document.documentElement.scrollTop : e.pageY;
	if(x < 0) { x = 0; }
	if(y < 0) { y = 0; }
	currX = x;
	currY = y
}

function displayInfo(id) {

	var popups = new Array();
	popups[0] = 'editpatient';
	popups[1] = 'editaircraft';

	for (var i = 0; i < popups.length; ++i)
	{
		var thisID = String(popups[i]);
		if(document.getElementById(thisID) && thisID != id)
		{
			document.getElementById(thisID).style.display = 'none';
		}
	}
	if(document.getElementById(id))
	{
		if(document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == '') {
			document.getElementById(id).style.top = (currY - 160) + "px";
			document.getElementById(id).style.left = (currX + 5) + "px";
			document.getElementById(id).style.display = 'block';
		}
		else {
			document.getElementById(id).style.display = 'none';
		}
	}
}

/* Preloader */


function preload(message) {
	if(!message) {
		message = 'Please wait ...';
	}

	var objBody = document.getElementsByTagName("body").item(0);
	var idname = "xlayer";
	var xLay = document.createElement('div');
	xLay.id = idname;
	xLay.style.position = 'absolute';
	xLay.style.zIndex = "1";  //Position of the overlay

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	xLay.style.height = (arrayPageSize[1] + 'px');
	xLay.style.display = 'block';
	//xLay.onclick = function() {clearExample(idname, 'preloader')};

	objBody.insertBefore(xLay, objBody.firstChild);

	// Hide Selectboxes in IE
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "hidden";
		}
	}

	//Create Preloader window

	var preLname = "preloader";
	var objPreload = document.createElement('div');
	objPreload.id = preLname;
	objPreload.style.position = 'absolute';
	objPreload.style.zIndex = "2";  //Position of the overlay

	objBody.insertBefore(objPreload, objBody.firstChild);

	var objLoadingImg = document.createElement("img");
	objLoadingImg.src = "images/preloader.gif";
	objPreload.appendChild(objLoadingImg);

	var objParagraph = document.createElement("p");
	var loaderText = document.createTextNode(message);
	objParagraph.appendChild(loaderText);
	objPreload.appendChild(objParagraph);

	if (objPreload) { // Center the preloader if it exists
		objPreload.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objPreload.offsetHeight) / 2) + 'px');
		objPreload.style.left = (((arrayPageSize[0] - 20 - objPreload.offsetWidth) / 2) + 'px');
	}
}


// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}


// Get Page size by Lokesh Dhakar - http://www.huddletogether.com

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


// clear preloading div layer
function clearExample(idname, id) {
	var overlay = document.getElementById(idname);
	if (overlay) {
		overlay.parentNode.removeChild(overlay);

		var iframe = document.getElementById(id);
		iframe.parentNode.removeChild(iframe);

		// Make Selectboxes visible again
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			selects = document.getElementsByTagName("select");
			for (i = 0; i != selects.length; i++) {
				selects[i].style.visibility = "visible";
			}
		}
	}
}

/* Show / hide Quotation rows */

function showTableRow(rowid, quotcount) {

	var imgID = 'plusminus_' + rowid;
	img = document.getElementById('plusminus_' + rowid).src;
	imgType = img.substring(img.lastIndexOf('/')+1,img.lastIndexOf('.')); //fetching graphic name (+ or -)

	//switch all quotation rows on/off
	for(var i = 0; i < quotcount; i++) {
		var rowID = 'quotrow_' + rowid + '_' + i;
		if(document.getElementById(rowID))
		if(document.getElementById(rowID).style.display == "none") {
			document.getElementById(rowID).style.display = "";
		}
		else {
			document.getElementById(rowID).style.display = "none";
		}
	}
	if(imgType == 'plus') {
		document.getElementById(imgID).src = "images/minus.gif";
	}
	else {
		document.getElementById(imgID).src = "images/plus.gif";
	}

}

/* Show / hide Express Quotation rows */

function showeTableRow(rowid, quotcount) {

	var imgID = 'eplusminus_' + rowid;
	img = document.getElementById('eplusminus_' + rowid).src;
	imgType = img.substring(img.lastIndexOf('/')+1,img.lastIndexOf('.')); //fetching graphic name (+ or -)

	//switch all quotation rows on/off
	for(var i = 0; i < quotcount; i++) {
		var rowID = 'equotrow_' + rowid + '_' + i;
		if(document.getElementById(rowID))
		if(document.getElementById(rowID).style.display == "none") {
			document.getElementById(rowID).style.display = "";
		}
		else {
			document.getElementById(rowID).style.display = "none";
		}
	}
	if(imgType == 'plus') {
		document.getElementById(imgID).src = "images/minus.gif";
	}
	else {
		document.getElementById(imgID).src = "images/plus.gif";
	}

}

/* Loading Events - has to be at the end of this document */


addEvent(window,'load',pageLoader);

var ie = (document.all) ? true : false;
if(!ie) document.captureEvents(Event.MOUSECLICK);
document.onclick = getPosXY;

