
// Content: js global
var navFrame, fbFrame;
var arDestinations = new Array();

function initHome() {
	// If the navigation in not loaded on the page, we load in on init.
	// This can be removed after version 1.13 or higher is deployed.
	try {
		if (!(n)) {
			loadNav();
		}
	} catch (e) {
		loadNav();
	}
	// End of temporary fix.
	checkBrowser();
	top.mainContainer = document.getElementById('maincontainer');
	updateSize();
	if (document.getElementById('language_selector')) ls = new LanguageSelector();
	FBfill(true);
	setTriggersHome();
	setOtherKLMSites();
	webSensor();
}
function initLocalHome() {
	// If the navigation in not loaded on the page, we load in on init.
	// This can be removed after version 1.13 or higher is deployed.
	try {
		if (!(n)) {
			loadNav();
		}
	} catch (e) {
		loadNav();
	}
	// End of temporary fix.
	setTriggersHome();
	setOtherKLMSites();
	webSensor();
}
function webSensor() {
	// If the navigation in not loaded on the page, we load in on init.
	// This can be removed after version 1.13 or higher is deployed.
	try {
		if (!(n)) {
			loadNav();
		}
	} catch (e) {
		loadNav();
	}
	// End of temporary fix.
	var cook=document.cookie;
	var i=cook.indexOf('KLMCOM_SESSIONCOOKIE');
	var strResolutie=window.screen.width+"x"+window.screen.height;
	if(i<0)
	{
		var id1=parseInt(Math.random()*2147418112);		
		document.cookie='KLMCOM_SESSIONCOOKIE='+id1+new Date().getTime()+'--'+strResolutie+';path=/;domain=.klm.com'
	}	
	
	
	strCDom=document.location.host.substring(document.location.host.lastIndexOf('.',document.location.host.lastIndexOf('.') - 1));
	keepdays=180;
	
	cook=document.cookie;
	i=cook.indexOf('MfTrack_js');
	if(i<0){
	        // the cookie was not yet present, create a new cookie
	        id1=parseInt(Math.random()*2147418112);
	        strCVal=id1+'.'+new Date().getTime()+'--'+strResolutie;
	        document.cookie='MfTrack_js='+strCVal+'; path=/; domain='+strCDom;
	}
	strPVal='';
	i=cook.indexOf('MfPers_js=');
	if(i<0){
	        // the cookie was not yet present, create a new cookie
	        id1=parseInt(Math.random()*2147418112);
	        strPVal=id1+'.'+new Date().getTime()+'--'+strResolutie;
	} else {
	        // change expire date of persistent cookie, with same value
	        j=cook.indexOf(";",i+11);
	        if (j<0)
	                strPVal=cook.substring(i+11);
	        else
	                strPVal=cook.substring(i+11, j - i);
	}
	expires=new Date();
	expires.setTime(expires.getTime()+(keepdays*24*60*60*1000));
	document.cookie='MfPers_js='+strPVal+'; path=/; domain='+strCDom+'; expires='+expires.toGMTString();
	
	var qs = window.location.search
	var adcamp = getParameter(qs, "adcamp")
	if (adcamp)
	{
		expires=new Date();
		expires.setTime(expires.getTime()+(30*24*60*60*1000));
		var content = "&adcamp=" + adcamp + "&adchan=" + getParameter(qs, "adchan") + "&adtype=" +
			getParameter(qs, "adtype") + "&adctry=" + getParameter(qs, "adctry") + "&adlang=" +
			getParameter(qs, "adlang") + "&http_referrer=" + document.referrer;
		document.cookie="SADCAMP=" + content + "; path=/; domain=" + strCDom;
		document.cookie="PADCAMP=" + content + "; path=/; domain=" + strCDom + "; expires="+expires.toGMTString();
	}
}
function init() {
	checkBrowser();
	top.mainContainer = document.getElementById('maincontainer');
	if (document.getElementById('language_selector')) ls = new LanguageSelector();
	FBfill();
	updateSize();
	setTriggers();
	setOtherKLMSites();
	webSensor();
}
function FBfill (home) {
	if (document.getElementById('jffp_login')) {
		fbFrame = new RPCFrame(window);
		if (home)
			fbFrame.setLocation('/travel/klm_splash/jffp.htm?loadProfile=true&date=' + new Date(), FBfillItHome, 'GET')
		else
			fbFrame.setLocation('/travel/klm_splash/jffp.htm?loadProfile=true&date=' + new Date(), FBfillIt, 'GET')
	}
}
function FBfillIt () {
	if (!fbFrame.getHTMLById('content'))
		return;
	document.getElementById('jffp_login').innerHTML = fbFrame.getHTMLById('content').innerHTML;
	toggleMilesPlus();
	try {
		checkBB();
	} catch (e) {}
	var fbNumber = getCookie('fbNumber');
	if (fbNumber)
	{
		var elems = document.getElementsByName('miles');
		for (var i = 0; i < elems.length; i++)
		{
			elems[i].value = fbNumber;
		}
	}
}

function FBfillItHome () {
	document.getElementById('jffp_login').innerHTML = fbFrame.getHTMLById('content').innerHTML;
	document.getElementById('bottom').innerHTML = '';
	var fbNumber = getCookie('fbNumber');
	if (fbNumber)
	{
		var elems = document.getElementsByName('miles');
		for (var i = 0; i < elems.length; i++)
		{
			elems[i].value = fbNumber;
		}
	}
}
function loadNav() {
	if (document.getElementById('navigation')) {
		navFrame = new RPCFrame(window);
		navFrame.setLocation('/travel/klm_splash/navigation.html', initNavigation);
		try {
			clearInterval(n);
		} catch (e) {
		}
	}
	else {
		return false;
	}
}
function initNavigation () {
	new Navigation('navigation', 'navopen', navFrame.getHTMLById('navcontent').innerHTML);
}
function isChild(ancestor, candidate) {
	if (!ancestor || !ancestor.parentNode || !candidate) return false;
	while (candidate && candidate != ancestor.parentNode) {
		if (candidate == ancestor) return true;
		try {
			candidate = candidate.parentNode;
		} catch (c) {return false}
	}
}
function createContextFunction(context, method, method2) {
	return (function(x){
		method = (method == "post") ? method2 : method;
		eval("context."+method+"(x)");
		return false;
	});
}
function initSpecials() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'specials') {
			var specials = entries[i].getElementsByTagName('li');
			for (var j = 0; j < specials.length; j++) {
				if (specials[j].getElementsByTagName('a').length > 0)
				{
					specials[j].link = specials[j].getElementsByTagName('a')[0].href;
					specials[j].getElementsByTagName('a')[0].removeAttribute("href");
					if (specials[j].getElementsByTagName('a')[0].target=='_blank')
						specials[j].onclick = function () {window.open(this.link)};
					else
						specials[j].onclick = function () {document.location.href = this.link};
				}
				specials[j].onmouseover = function () {this.className = "over"}
				specials[j].onmouseout = function () {this.className = ""};
			}
		}
	}
}
function initSpecials_mp() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'specials_jffp') {
			var specials = entries[i].getElementsByTagName('li');
			for (var j = 0; j < specials.length; j++) {
				specials[j].link = specials[j].getElementsByTagName('a')[0].href;
				specials[j].getElementsByTagName('a')[0].removeAttribute("href");
				specials[j].onclick = function () {document.location.href = this.link};
				specials[j].onmouseover = function () {this.className = "over"}
				specials[j].onmouseout = function () {this.className = ""};
			}
		}
	}
}
function setTriggers() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className.indexOf('TRIG') != -1) {
			entries[i].outline = entries[i].getElementsByTagName('div')[0];
			// create an exception for specials trigger
			if(entries[i].outline.className.indexOf('specials') == -1) {
				entries[i].link = entries[i].getElementsByTagName('a')[0];
				entries[i].onmouseover = function (e) {
					this.outline.oldClassName = this.outline.className;
					this.outline.className += "over";
				}
				entries[i].onmouseout = function (e) {
					this.outline.className = this.outline.oldClassName;
				}
				entries[i].onclick = function () {
					if (this.className.indexOf('newWin') != -1) {
						if (this.className.indexOf('KANA') !=-1) {
							window.open(this.link,'kana','width=400,height=300');
						}
						else if (this.className.indexOf('LSW') !=-1) { //local shop window
							window.open(this.link,'lsw','width=815,height=600,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes');
						}
						else {
							window.open(this.link);
						}	
					}
					else {
						document.location.href = this.link;
					}
				}
			}
		}
	}
}
function setTriggersHome() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className.indexOf('TRIG') != -1) {
			entries[i].link = entries[i].getElementsByTagName('a')[0];
			entries[i].onmouseover = function (e) {
				this.className += " TRIGOVER";
			}
			entries[i].onmouseout = function (e) {
				this.className = this.className.replace(" TRIGOVER","");
			}
			entries[i].onclick = function () {
				if (this.className.indexOf('newWin') != -1) {
					if (this.className.indexOf('KANA') !=-1) {
						window.open(this.link,'kana','width=400,height=300');
					}
					else if (this.className.indexOf('LSW') !=-1) { //local shop window
						window.open(this.link,'lsw','width=815,height=600,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes');
					}
					else {
						window.open(this.link);
					}
					this.link.disabled = true;
				}
				else {
					document.location.href = this.link;
				}
			}
		}
	}
}
function setPromos() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className.indexOf('nonair') != -1) {
			entries[i].outline = entries[i].getElementsByTagName('span')[0];
			entries[i].link = entries[i].getElementsByTagName('a')[0];
			entries[i].onclick = function () {document.location.href = this.link};
			entries[i].onmouseover = function (e) {
				this.outline.oldClassName = this.outline.className;
				this.outline.className += "over";
			}
			entries[i].onmouseout = function (e) {
				this.outline.className = this.outline.oldClassName;
			}
		}
	}
}

function toggleMilesPlus() {
	var miles = document.getElementById('mpopen');
	var milesplusblock = document.getElementById('jffpcontentcontainer');
	var seealsoblock = document.getElementById('seealso');
	milesplusblock.style.display = "block";
	miles.height = milesplusblock.offsetHeight;
	milesplusblock.style.display = "none";
	var milesclose = document.getElementById('bottom');
	miles.onclick = function() {
		if (milesplusblock.style.display == 'none') {
			milesplusblock.style.display = 'block';
			if (seealsoblock) seealsoblock.style.top = (this.height - 35) + "px";
		} else {
			milesplusblock.style.display = 'none';
			if (seealsoblock) seealsoblock.style.top = "0";
		}
	}
	milesclose.onclick = function() {
		if (milesplusblock.style.display == 'none') {
			milesplusblock.style.display = 'block';
			if (seealsoblock) seealsoblock.style.top = (this.height - 35) + "px";
		} else {
			milesplusblock.style.display = 'none';
			if (seealsoblock) seealsoblock.style.top = "0";
		}
	}
	if (top.FBopen) {
		milesplusblock.style.display = 'block';
		if (seealsoblock) seealsoblock.style.top = (miles.height - 35) + "px";
	}
}
function addEventHandler(element, type, handler) {
	try {
		element.addEventListener(type, handler, false);
	} catch(inferiorBrowserException) {
		if(element.attachEvent) 
			element.attachEvent('on'+type, handler);
		else 
			element['on'+type] = handler;
	}
	return [element, type, handler];
}
function isSafari() {
	return (/safari/i).test(navigator.userAgent);
}

function removeEventHandler(o, eventName, handler) {
	if (o.removeEventListener) {
		o.removeEventListener(eventName, handler, true);
	} else {
		o.detachEvent("on"+eventName, handler);
	}
}
function cancelEvent(e) {
	try {
		e.preventDefault();
		e.stopPropagation();
	} catch (someException) {
		e.cancelBubble = true;
		e.returnValue = false;
	}
	if (isSafari()) {
		var target = e.target;
		while (target.nodeType > 1) target = target.parentNode;
	 	if (/^a$/i.test(target.nodeName)) {
	 		target.onclick = function() {
				return false;
			};
		}
	}
	return false;
}

function calculateTop(object) {if (object) return object.offsetTop + calculateTop(object.offsetParent); else return 0;}
function calculateLeft(object) {if (object) return object.offsetLeft + calculateLeft(object.offsetParent); else return 0;}

function scaleFrame() {
	var f = document.getElementById('extFrame');
	var top;
	top = calculateTop(f);
	f.style.height = getWindowHeight() - top + "px";
	document.body.style.overflow = "hidden"; // for firefox and mozilla
	document.body.parentNode.style.overflow = "hidden"; // for ie
}
function getWindowHeight() {
var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else {
		if (document.documentElement&&document.documentElement.clientHeight) {
			windowHeight=document.documentElement.clientHeight;
		} else if (document.body&&document.body.clientHeight) windowHeight=document.body.clientHeight;
	}
	return parseInt(windowHeight);
}
function setOtherKLMSites() {
	var s = document.getElementById('otherKLMsites');
	if (s) {
		s.onchange = function () {
			var strArr = this.options[this.selectedIndex].value.split("|");
			if (strArr[0] != "#") {
				if (strArr[1] == "New window" || strArr[1] == "Popup") {
					window.open(strArr[0],"_blank");
				} else {
					document.location.href = strArr[0];
				}
			}
		}
	}
}

function logOutJFFP () {
	deleteCookie('jffp');
	document.location.href = '/travel/logon/klm_splash?logOut=true';
}
function checkBB () {
	if (getCookie('klmcomcaas') == 'b2b') {
		document.getElementById('loginbar').style.display = 'none';
		document.getElementById('loggedinbar').style.display = 'block';
	} else {
		document.getElementById('loginbar').style.display = 'block';
		document.getElementById('loggedinbar').style.display = 'none';
	}
}
function checkLoggedin () {
	if (getCookie('jffp')) {
		if (getCookie('jffp').length > 10) {
			return true;
		}
	}
	return false;
}

function checkLogin () {
	if (getCookie('jffp')) {
		if (getCookie('jffp').length > 10) {
			document.getElementById('IN').style.display = 'block';
		} else {
			document.getElementById('OUT').style.display = 'block';
		}
	} else {
		document.getElementById('OUT').style.display = 'block'
	}
}

function initConditions() {
	var entries = document.getElementsByTagName('a');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'CONDITIONS') {
			entries[i].layer = entries[i].parentNode.getElementsByTagName('div')[0];
			entries[i].container = entries[i].parentNode.parentNode.parentNode.parentNode.parentNode;
			entries[i].layer.closer = entries[i].layer.getElementsByTagName('div')[0];
			entries[i].layer.closer.container = entries[i].container;
			entries[i].layer.closer.onclick = function () {
				this.parentNode.style.display = 'none';
				//this.container.style.position = 'static';
			}
			entries[i].onclick = function () {
				closeOthers();
				//this.container.style.position = 'relative';
				this.layer.style.display = 'block';
				var high = this.layer.offsetHeight;
				var newY = 0 - high + 'px';
				this.layer.style.marginTop=newY;
			}
		}
	}
}
function closeOthers () {
	var entries = document.getElementById('content').getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'conditionslayer')
			entries[i].style.display = 'none';
		if (entries[i].className == 'webawards')
			entries[i].style.position = 'static';
	}
}
function initWABanner () {
	var wa = document.getElementById('webawards');
	if (wa) {
		wa.link = wa.getElementsByTagName('a')[0].href;
		wa.onclick = function () {document.location.href = this.link};
	}
}
function milesPlusLogon(){
	var strLocation;
	strLocation = "https://" + document.location.hostname + "/travel/logon/klm_splash?forwardurl=/klm_splash";
	document.forms.frmJFFP.action=strLocation;
	document.forms.frmJFFP.submit();
}

function checkBrowser() {
	var browser = 'unknown';
	browser = getBrowser();
	if ((browser == 'TUX_FF') || (browser == 'TUX_MOZ') || (browser == 'TUX_OP') || (browser == 'MAC_OP') || (browser == 'WIN_OP') || (browser == 'WIN_IE7') || (browser == 'WIN_IE6') || (browser == 'WIN_IE55') || (browser == 'WIN_IE50') || (browser == 'WIN_MOZ')|| (browser == 'WIN_FF') || (browser == 'MAC_SAF') || (browser == 'MAC_FF') || (browser == 'MAC_MOZ')) {
		//browser ok, so to 'normal' site
		return;
	}
	else {
		//wrong browser, so to 'browsersupport' page
		document.location.href = '/travel/klm_splash/browsersupport.html';
	}
}
function getParameter ( queryString, parameterName ) {
	var parameterName = parameterName + "=";
	if ( queryString.length > 0 ) {
		begin = queryString.indexOf ( parameterName );
		if ( begin != -1 ) {
			begin += parameterName.length;
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = queryString.length
			}
			return unescape ( queryString.substring ( begin, end ) );
		}
	return null;
	}
}
function getBrowser () {
	var mvIndex;
	var mozVers;
	var os = navigator.platform.toLowerCase(); 
	var agt = navigator.userAgent.toLowerCase(); 
	var ver = navigator.appVersion.toLowerCase();
	
	if (os.indexOf('win') != -1) {
		if (agt.indexOf("opera")!=-1) return "WIN_OP";
		else if (agt.indexOf("msie 7.0")!=-1) return "WIN_IE7";
		else if (agt.indexOf("msie 6.0")!=-1) return "WIN_IE6";
		else if (agt.indexOf("msie 5.5")!=-1) return "WIN_IE55";
		else if (agt.indexOf("msie 5.0")!=-1) return "WIN_IE50";
		else if (agt.indexOf("netscape")!=-1) return "Netscape";
		else if (agt.indexOf("firefox")!=-1) return "WIN_FF";
		else if (agt.indexOf("mozilla")!=-1) {
			var mvIndex = agt.indexOf('; rv:1.');
			var mozVers = agt.substr(mvIndex + 7, 1); 
			if (mozVers > 3) return "WIN_MOZ";
		}
	}
	else if (os.indexOf('mac') != -1) {
		if (agt.indexOf("opera")!=-1) return "MAC_OP";
		else if (agt.indexOf("safari")!=-1) {
			var safIndex = agt.indexOf('safari/');
			var safVers = agt.substr(safIndex + 7, 3);
			if (safVers >= 110) return "MAC_SAF";
		}
		else if (agt.indexOf("netscape")!=-1) return "Netscape";
		else if (agt.indexOf("firefox")!=-1) return "MAC_FF";
		else if (agt.indexOf("mozilla")!=-1) {
			var mvIndex = agt.indexOf('; rv:1.');
			var mozVers = agt.substr(mvIndex + 7, 1); 
			if (mozVers > 3) return "MAC_MOZ";
		}
	}
	else {
		if (agt.indexOf("opera")!=-1) return "TUX_OP";
		else if (agt.indexOf("netscape")!=-1) return "Netscape";
		else if (agt.indexOf("firefox")!=-1) return "TUX_FF";
		else if (agt.indexOf("mozilla")!=-1) {
			return "TUX_MOZ";
		}
	}	
}

function checkReturnKey(event, varForm, blnMilesPlusLogon)
{
	var key;
	
	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = event.which;     //firefox
	
	if (key == 13)
	{
		if (blnMilesPlusLogon)
			milesPlusLogon();
		else
			varForm.submit();		
	}
	else
	{
		return true;
    	}
}
