// JS Basics for www.airtraffic.dk
// author : Kim Jensen
// created : 25.01.2003

// browsercheck...
var NN403 = false;
var NN405 = false;
var NN47 = false;
var NN6 = false;
var NN4X = false;
var NNX = false;
var IE5 = false;
var IE55 = false;
var IE6 = false;
var IE5plus = false;
var IEX = false;
var Opera6 = false;
var Opera7 = false;
var Mac = false;
var Win = false;

NN403 = ((navigator.appName.indexOf('Netscape')>-1) && (navigator.appVersion.indexOf('4.03')>-1)) ? true : false;
NN405 = ((navigator.appName.indexOf('Netscape')>-1) && (navigator.appVersion.indexOf('4.05')>-1)) ? true : false;
NN47 = ((navigator.appName.indexOf('Netscape')>-1) && (navigator.appVersion.indexOf('4.7')>-1)) ? true : false;
NN6 = ((navigator.appName.indexOf('Netscape')>-1) && (navigator.appVersion.indexOf('5.0')>-1) && (navigator.appVersion.indexOf('4.')==-1)) ? true : false;
NN4X = ((navigator.appName.indexOf('Netscape')>-1) && (navigator.appVersion.indexOf('4.')>-1)) ? true : false;
NNX = (navigator.appName.indexOf('Netscape')>-1) ? true : false;

IE5 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 5.0')>-1)) ? true : false;
IE55 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 5.5')>-1)) ? true : false;
IE6 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 6.0')>-1)) ? true : false;
IE5plus = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.slice(navigator.appVersion.indexOf('MSIE')+5,navigator.appVersion.indexOf('MSIE')+6)) <= 5) ? true : false;
IEX = (navigator.appName.indexOf('Microsoft')>-1) ? true : false;

Opera6 = ((navigator.appName.indexOf('Opera')>-1) && (navigator.appVersion.indexOf('6.')>-1)) ? true : false;
Opera7 = ((navigator.appName.indexOf('Opera')>-1) && (navigator.appVersion.indexOf('7.')>-1)) ? true : false;

Win = (navigator.platform.indexOf('Win32')>-1) ? true : false;
Mac = (navigator.platform.indexOf('Mac')>-1) ? true : false;

// check for incompatible browser...
if (NN4X || Opera6) {
	makePop(-1,-1,'wrongbrowser','incompatible_browser.html','wrongbrowser',400,400,'scrollbars=no');
}

// include stylesheet...
if (parseInt(navigator.appVersion)>3) {
	if ((Mac) && (IEX)) {document.write('<link href="css/mac_ie.css" rel="stylesheet" type="text/css">');}
	if ((Win) && (IEX || Opera6)) {document.write('<link href="css/win_ie.css" rel="stylesheet" type="text/css">');}
	if ((Mac) && (NN4X)) {document.write('<link href="css/mac_nn4x.css" rel="stylesheet" type="text/css">');}
	if ((Win) && (NN4X)) {document.write('<link href="css/win_nn4x.css" rel="stylesheet" type="text/css">');}
	if ((Mac) && (NN6)) {document.write('<link href="css/mac_nn6.css" rel="stylesheet" type="text/css">');}
	if ((Win) && (NN6)) {document.write('<link href="css/win_nn6.css" rel="stylesheet" type="text/css">');}
}

// window.open function...
function makePop(windowPosX,windowPosY,windowName,url,name,w,h,extra){
	var titlebarHeight = 28; // titlebar + chrome at bottom on a standard IE5 on a Win2kPro installa. // Used for correct centering of pop...
	str="height="+h+",width="+w+","+extra;
	if(parseInt(navigator.appVersion)>3)
		if (windowPosX == -1) {
			str+=",left="+(screen.width -w)/2+",top="+parseInt(((screen.height -h)-titlebarHeight)/2);
		}
		else {
			str+=",left="+windowPosX+",top="+windowPosY;
		}
	k = eval(windowName + "=window.open('" + url + "','" + name + "','" + str + "')");
	return k;
}

var imgsLoaded = false;

// image over function (non-layer compatibele)...
function imgOver(img_name){
	if (imgsLoaded) {
		document.images[img_name].src = eval(img_name+"_over.src");
	}
}

// image over function (non-layer compatibele)...
function imgOut(img_name){
	if (imgsLoaded) {
		document.images[img_name].src = eval(img_name+"_out.src");
	}
}

// image over function (layer compatibele)...
function layerImgOver(img_name, layer_name){
	if (imgsLoaded) {
		if (NN4X) {
			document[layer_name].document.images[img_name].src = eval(img_name+"_over.src");
		}
		else {
			document.images[img_name].src = eval(img_name+"_over.src");
		}
	}
}

// image over function (layer compatibele)...
function layerImgOut(img_name, layer_name){
	if (imgsLoaded) {
		if (NN4X) {
			document[layer_name].document.images[img_name].src = eval(img_name+"_out.src");
		}
		else {
			document.images[img_name].src = eval(img_name+"_out.src");
		}
	}
}

// swaps images in a layer (special syntax for NN4X browsers)
function swapLayeredImg(imgName, theNewImageVar, layerName) {
//	alert(NN6);
	if (imgsLoaded) {
		if (IEX) {
			document.images[imgName].src = theNewImageVar;
		}
		else if (NN6) {
			document.images[imgName].src = theNewImageVar;
		}
		else if (NN4X) {
			document[layerName].document.images[imgName].src = theNewImageVar;
		}
	}
}

// Writes to a layer...
function writeLayer(which, content) {
   if (NN4X) {
       document[which].document.open();
       document[which].document.write(content);
       document[which].document.close();
   } else if (IEX) {
	   document.all[which].innerHTML = content;
	}
	else if (NN6) {
		document.getElementById(which).innerHTML = content;
	}
}

// moves a layer...
function moveLayerTo(theLayer,x,y) {
	if (IEX) {
		document.all[theLayer].style.left = x;
		document.all[theLayer].style.top = y;
	}
	else if (NN4X) {
		eval('document.'+theLayer+'.left = x;');
		eval('document.'+theLayer+'.top = y;');
	}
	else if (NN6) {
		document.getElementById(theLayer).style.left = x;
		document.getElementById(theLayer).style.top = y;
	}
}

// fix used for showLayer among other functions...
NSfix = (NNX) ? 'document.' : '';
styleFix = (NNX) ? '' : '.style';

// shows a layer...
function showLayer(which) {
	if (NN6) {
		document.getElementById(which).style.visibility = 'visible';
	}
	else {
		eval(NSfix+which+styleFix+'.visibility="visible"');
	}
}

// hides a layer...
function hideLayer(which) {
	if (NN6) {
		document.getElementById(which).style.visibility = 'hidden';
	}
	else {
		eval(NSfix+which+styleFix+'.visibility="hidden"');
	}
}

// Image position functions...
// ---------------------
// Ask for image-position:
function ysPos(what){
	if(IEX){
		return yIE(eval('document.'+what));
	}
	else if(NN4X){
		return eval('document.'+what+'.y');
	}
	else if(NN6){
	return parseInt(yIE(eval('document.getElementById("' + what + '")')));
	}
}
 
function xsPos(what){
	if(IEX){
		return xIE(eval('document.'+what));
	}
	else if(NN4X){
		return eval('document.'+what+'.x');
	}
	else if(NN6){
		return parseInt(xIE(eval('document.getElementById("' + what + '")')));
	}
}
 
function yIE(what) {
	if(what.offsetParent) {
		return parseInt((what.offsetTop + yIE(what.offsetParent)));
	} else {
		return parseInt(what.offsetTop);
	}
}

function xIE(what) {
	if(what.offsetParent) {
		return parseInt((what.offsetLeft + xIE(what.offsetParent)));
	} else {
		return parseInt(what.offsetLeft);
	}
}
// ---------------------


//************************************************************************************************

// handles clicks in the navigation
function doLink(keyword) {
	switch (keyword) {
		case 'company' :
			location = 'index.asp';
		break;
		case 'flybook' :
			location = 'flybook.asp';
		break;
		case 'aircraft' :
			location = 'aircraft.asp';
		break;
		case 'price_inquiries' :
			location = 'price_inquiries.asp';
		break;
		case 'news' :
			location = 'news.asp';
		break;
		case 'contact' :
			location = 'contact.asp';
		break;
		case 'links' :
			location = 'links.asp';
		break;
		case 'index' :
			location = 'index.asp';
		break;
		default :
			alert('missing eventhandler for keyword: '+keyword);
		break;
	}
}

// strips non-number characters from the string which...
function forceNumber(which){
    if (!isInteger(which.value)) {
		which.value = stripCharsNotInBag(which.value,digits);
    }
}

// repositions a layer according to the offsets from a specified image.
// remember to include both a name and an id reference on the image.
function positionLayerFromImage(theImage,theLayer,theXOffset,theYOffset,visibilityState) {
	theX = xsPos(theImage);
	theY = ysPos(theImage);
	moveLayerTo(theLayer,theX+theXOffset,theY+theYOffset);
	if (visibilityState == 'show') {
		showLayer(theLayer);
	}
	else if (visibilityState == 'hide') {
		hideLayer(theLayer);
	}
}

function setDropDown(which, value) {
	theObj = eval('document.form.'+which);
	for (i=0;i<theObj.length;i++) {
		if (value == theObj[i].value) {
			theObj[i].selected = true;
			break;
		}
	}
}

function setRadio(which, value) {
	theObj = eval('document.form.'+which);
	for (i=0;i<theObj.length;i++) {
		if (value == theObj[i].value) {
			theObj[i].checked = true;
			break;
		}
	}
}

// preloads the images for the navigation
function preloadImages() {
	bn1_out = new Image();			bn1_out.src='img/gx_m_company_out.gif';
	bn2_out = new Image();			bn2_out.src='img/gx_m_flybook_out.gif';
	bn3_out = new Image();			bn3_out.src='img/gx_m_aircraft_out.gif';
	bn4_out = new Image();			bn4_out.src='img/gx_m_price_inq_out.gif';
	bn5_out = new Image();			bn5_out.src='img/gx_m_news_out.gif';
	bn6_out = new Image();			bn6_out.src='img/gx_m_contact_out.gif';
	bn7_out = new Image();			bn7_out.src='img/gx_m_links_out.gif';

	bn1_over = new Image();			bn1_over.src='img/gx_m_company_over.gif';
	bn2_over = new Image();			bn2_over.src='img/gx_m_flybook_over.gif';
	bn3_over = new Image();			bn3_over.src='img/gx_m_aircraft_over.gif';
	bn4_over = new Image();			bn4_over.src='img/gx_m_price_inq_over.gif';
	bn5_over = new Image();			bn5_over.src='img/gx_m_news_over.gif';
	bn6_over = new Image();			bn6_over.src='img/gx_m_contact_over.gif';
	bn7_over = new Image();			bn7_over.src='img/gx_m_links_over.gif';

	imgsLoaded = true;
}

// used to pop the aircraft details window (used from aircraft.asp & flybook.asp)
function doAircraftDetails(reference) {
	makePop(-1,-1,'aircraft_details','aircraft_details.asp?reference='+reference+'','aircraft_details',665,507,'scrollbars=0');
}

// FLYBOOK functions
// launches the help-popup...
function doHelp() {
	location = 'flybook3.asp';
}

// Sets a dropdown (theDropdown) when the string in 'which' matches the value in one of the options in the dropdown.
// If no match is found the dropdown is set to the first option...
function setDropdown(theDropdown,which) {
	theLength = eval('document.form.'+theDropdown+'.options.length');
	found = false;
	i = 0;
	while ((!found) && (i<theLength)) {
		theValue = eval('document.form.'+theDropdown+'.options['+i+'].value');
		if (theValue == which) {
			eval('document.form.'+theDropdown+'.options['+i+'].selected = true');
			found = true;
		}
		i++;
	}
	if (!found) { // if no match is found just set the dropdown to the first option (the blank one)
		document.form[theDropdown].options[0].selected = true;
	}
}

// Checks a targetDropdown for the textbit written in the fiels sourceIataField. If there's a match the matching option in
// the dropdown (targetDropdown) is selected... Otherwise an errormsg. is displayed (an alert)...
function checkIata(targetDropdown,sourceIataField) {
	theIataValue = document.form[sourceIataField].value;
	if ((theIataValue != '') && (theIataValue.length == 3)) { // only if the entry is 3 chars long the function can run...
		theLength = eval('document.form.'+targetDropdown+'.options.length');
		found = false;
		i = 0;
		theIataValue = theIataValue.toUpperCase();
		while ((!found) && (i<theLength)) {
			theText = eval('document.form.'+targetDropdown+'.options['+i+'].text');
			if (theText.indexOf(theIataValue)>-1) {
				eval('document.form.'+targetDropdown+'.options['+i+'].selected = true');
				found = true;
			}
			i++;
		}
		if (!found) { // error msg. no iata match found...
			document.form[targetDropdown].options[0].selected = true;
			document.form[sourceIataField].value = document.form[sourceIataField].value+'?';
			if (IEX) {
				//document.form[sourceIataField].focus();
			}
			alert('No IATA code was found.\nPlease correct your entry or select airports from the dropdown boxes.');
		}
	}
}

function checkDepartureIata() { // since no iata code is included in departure drop at the moment we need this very static non-generic function...
	theIataValue = document.form.iata1.value;
	if ((theIataValue != "") && (theIataValue.length == 3)) {
		theIataValue = theIataValue.toUpperCase();
		switch (theIataValue) {
			case 'AAL':
				document.form.departure.options[1].selected = true;
			break;
			case 'AAR':
				document.form.departure.options[2].selected = true;
			break;
			case 'BLL':
				document.form.departure.options[3].selected = true;
			break;
			case 'EBJ':
				document.form.departure.options[4].selected = true;
			break;
			case 'KRP':
				document.form.departure.options[5].selected = true;
			break;
			case 'CPH':
				document.form.departure.options[6].selected = true;
			break;
			case 'RKE':
				document.form.departure.options[7].selected = true;
			break;
			case 'ODE':
				document.form.departure.options[8].selected = true;
			break;
			default:
				alert('No IATA code was found.\nPlease correct your entry or select airports from the dropdown boxes.');
				document.form.departure.options[0].selected = true;
				document.form.iata1.value = document.form.iata1.value+'?';
				if (IEX) {
					//document.form.iata1.focus();
				}
			break;
		}
	}
}

function writeDepartureIata() {
	k = (document.form.departure.options[document.form.departure.selectedIndex].text);
	switch (k) {
		case 'Aalborg':
			document.form.iata1.value = 'AAL';
		break;
		case 'Aarhus':
			document.form.iata1.value = 'AAR';
		break;
		case 'Billund':
			document.form.iata1.value = 'BLL';
		break;
		case 'Esbjerg':
			document.form.iata1.value = 'EBJ';
		break;
		case 'Karup':
			document.form.iata1.value = 'KRP';
		break;
		case 'København kastrup':
			document.form.iata1.value = 'CPH';
		break;
		case 'København roskilde':
			document.form.iata1.value = 'RKE';
		break;
		case 'Odense':
			document.form.iata1.value = 'ODE';
		break;
		case '':
			document.form.iata1.value = '';
		break;
	}
}

function writeDestinationIata() {
	k = (document.form.destination.options[document.form.destination.selectedIndex].text);
	k = k.slice(k.indexOf('[')+1,k.indexOf(']'));
	if (k != 'n/a') {
		document.form.iata2.value = k;
	}
	else {
		document.form.iata2.value = '';
	}
}

function doCopyIataToHidden() {
	document.form.iata1_hidden.value = document.form.iata1.value;
	document.form.iata2_hidden.value = document.form.iata2.value;
}

function doSubmitQuery() {
	doCopyIataToHidden();
	document.form.submit();
}

function doLaunchAircraftDetails(theReference) {
	if (!theReference) {
		theReference = 'FAL2000';
	}
	eval("makePop(-1,-1,'aircraftDetails','aircraft_details.asp?keyword="+theReference+"','aircraftDetails',665,507)");
}

function doSendEmail(keyword) {
	l1 = 'lynge';
	l2 = '@';
	l3 = 'airtraffic';
	l4 = '.dk';
	h1 = 'henrik';
	h2 = '@';
	h3 = 'airtraffic';
	h4 = '.dk';
	c1 = 'info';
	c2 = '@';
	c3 = 'airtraffic';
	c4 = '.dk';
	if (keyword == 'company') {location = 'mailto:'+c1+c2+c3+c4;}
	else if (keyword == 'lynge') {location = 'mailto:'+l1+l2+l3+l4;}
	else if (keyword == 'henrik') {location = 'mailto:'+h1+h2+h3+h4;}
}


