﻿function slideTxtOpen(sender) {
	sender.parent().siblings(".routeTxtMore").slideToggle("normal");
	sender.parent().parent().addClass("open");
}
function slideTxtClosed(sender) {
	sender.parent().siblings(".routeTxtMore").slideToggle("normal");
	sender.parent().parent().removeClass("open");
}

/* Source: http://www.w3schools.com/js/js_cookies.asp 
Returns a cookie value based on the provided c_name
*/
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

// Sets a cookie
// Updated the script by adding ";path=/", else id didn't work properly
function setCookie(c_name, value, expiredays) {
	var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/;";
}

// Updates the cookie with the search page request
function UpdateSearchPageRequestToCookies(nValuesFromRequest, cookieNameNValues, expireInDays, separatorChar)
{
	// Get the NValues from the cookie.
	var lNValuesFromCookie = getCookie(cookieNameNValues);

	// Merge the cookie NValues with the ones from the Request.
	lNValuesFromCookie = MergeNValuesFromRequestWithStoredValues(lNValuesFromCookie, nValuesFromRequest, separatorChar);

	// Set the NValues to the cookie
	setCookie(cookieNameNValues, lNValuesFromCookie, expireInDays);
}

// Merge N-values together so they are sorted in the order of addition
function MergeNValuesFromRequestWithStoredValues(nValuesFromCookie, nValuesFromRequest, separatorChar)
{
	// Create lists of the nValues
	var lNValuesFromCookieList = nValuesFromCookie.split(separatorChar);
	var lNValuesFromRequestList = nValuesFromRequest.split(separatorChar);

	var lReturnValue = new Array();

	// 1) Only keep the cookie values that still exist in the request.
	for (i = 0; i < lNValuesFromCookieList.length; i++) {
		var j = 0;
		var lFound = false;

		// Implemented with a while to use as less loops as possible
		while (!lFound && j < lNValuesFromRequestList.length) {
			if (lNValuesFromCookieList[i] == lNValuesFromRequestList[j] && lNValuesFromCookieList[i] != undefined && lNValuesFromCookieList[i] != '') {
				lFound = true;
				lReturnValue.push(lNValuesFromCookieList[i]);
			}

			j++;
		}
	}

	// 2) Check if there are values in the request, that are not yet in the array that we will return
	for (k = 0; k < lNValuesFromRequestList.length; k++) {
		var lFound = false;
		var l = 0;

		while (!lFound && l < lReturnValue.length) {
			// Check if the value is found.
			if (lNValuesFromRequestList[k] == lReturnValue[l] && lNValuesFromRequestList[k] != undefined && lNValuesFromRequestList[k] != '') {
				lFound = true;
			}
			l++;
		}

		if (!lFound) {
			lReturnValue.push(lNValuesFromRequestList[k]);
		}
	}

	// 3 Create a string from the values in the array.
	var lReturnValueAsString = '';
	for (i = 0; i < lReturnValue.length; i++) {
		if (lReturnValue[i] != undefined && lReturnValue[i] != '')
			lReturnValueAsString += lReturnValue[i] + separatorChar;
	}

	if (lReturnValue.length == 1)
		return lReturnValueAsString.substring(0, lReturnValueAsString.length - 1);
	else
		return lReturnValueAsString;
}

function DisplayOverlay(overlayContentId)
{
	// Get the scroll and window offset heights
	var scrollOffsetHeight, windowHeight;
	if (window.addEventListener) {
		scrollOffsetHeight = window.pageYOffset;
		windowHeight = window.innerHeight;
	}
	else { // IE
		scrollOffsetHeight = document.documentElement.scrollTop || document.body.scrollTop;
		windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
	}

	$('#overlayBackground').height($(document).height());
	$('.overlayContent').css({ 'display': 'none' });
	$('#pageOverlay').css({ 'display': 'block' });
	$('#' + overlayContentId).css({ 'left': ($(document).width() - $('#' + overlayContentId).width()) / 2 });
	$('#' + overlayContentId).css({ 'top': scrollOffsetHeight + ($(window).height() - $('#' + overlayContentId).height()) / 2 });
	$('#' + overlayContentId).css({ 'display': 'block' });
	ReloadSifr();
}

function HideOverlay(overlayContentId) {
	$('#pageOverlay').css({ 'display': 'none' });
}

function ReloadSifr()
{
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', sifrConfigFile);
	html_doc.appendChild(js);
}

/* Functionality to select the current cell of the price table. */

// highlight the current cell and reset the previous selected cell.
function HighlightCell(recordId) {

	if (!window.previousSelectedElement) {
		window.previousSelectedElement = { cellId: '', className: '', onClick: '' };
	}

	// find the previous selected cell
	var lPreviousSelectedCell = document.getElementById(previousSelectedElement.cellId);

	// restore the previous selected cell
	if (lPreviousSelectedCell) {
		// set the className back to initial name
		lPreviousSelectedCell.className = previousSelectedElement.className;
		
		// set the onclick event back to initial event
		lPreviousSelectedCell.onclick = previousSelectedElement.onClick;
	}

	var lCell = document.getElementById(recordId);

	// make the selection
	if (lCell) {
		var lCellClasses = lCell.className.split(" ");
		// remove canHoveronhover class if was added (this is the case for IE 6), csshover3.htc does this
		// also remove spaces
		for (var i = 0; i < lCellClasses.length; i++) {
			if (lCellClasses[i] == "canHoveronhover" || lCellClasses[i] == "") {
				// remove the current element and decrease the index
				lCellClasses.splice(i, 1);
				i--;
			}
		}
		// keep the current className
		previousSelectedElement.className = lCellClasses.join(" ");
		
		// replace canHover class with activePrice class
		for (var i = 0; i < lCellClasses.length; i++) {
			if (lCellClasses[i] == "canHover")
				lCellClasses[i] = "activePrice";
		}
		
		// make current cell active
		lCell.className = lCellClasses.join(" ");
		
		// keep the onclick event
		previousSelectedElement.onClick = lCell.onclick;
		
		// disable the current onclick event
		lCell.onclick = "";
		
		// keep the current cell id
		previousSelectedElement.cellId = recordId;
	}
}
/* End of functionality to select the current cell of the price table. */

function openFlightInformation(url) {
    var lWindow = window.open(url, 'flightInfo', 'width=310,height=200,status=no,toolbar=no,location=no,menubar=no,titlebar=no');
    lWindow.focus();
}