// Content: js dashboard data
//20080122 HW: FM22978 hotels & cars dashboard changed
//20090218 SC:  FM 27269 Change ICI dashboard 

var months = [			
	['Jan','January'],
	['Feb','February'],
	['Mar','March'],
	['Apr','April'],
	['May','May'],
	['Jun','June'],
	['Jul','July'],
	['Aug','August'],
	['Sep','September'],
	['Oct','October'],
	['Nov','November'],
	['Dec','December']];

function fillDataSelector(id, fullMonthNames, addDays) {
	var now = new Date();
	var start = new Date(now.getTime() + addDays * 3600 * 24 * 1000);
	var year = now.getFullYear();
	var month = now.getMonth();
	var selector = document.getElementById(id);
	for (c = 0; c < 12; c++) {
		var value = String(year) + String(month<9 ? "0" : "") + String(month + 1);
		var name = months[month][fullMonthNames ? 1:0] + " " + year;
		selector[c] = new Option(name, value);
		if (++month >= 12) {
			month = 0;
			year++;
		}
	}
	selector.selectedIndex = start.getYear() > now.getYear() ? 1 : (start.getMonth() > now.getMonth() ? 1 : 0);
}

function fillDaySelector(idDay, addDays) {
	var daySelector = document.getElementById(idDay);
	var index = 0;
	for (var c = 1; c <= 31; c++)
	{
		var value = String(c<=9 ? "0" : "") + String(c);
		daySelector.options[index++] = new Option(c, value);
	}	
}

function setDay(idDay, addDays) {
	var start = new Date(new Date().getTime() + addDays * 3600 * 24 * 1000);
	document.getElementById(idDay).selectedIndex = start.getDate() - 1;
}

function checkDepDate(dep_date, dep_month, ret_date, ret_month) {
	var dds = document.getElementById(dep_date);
	var rds = document.getElementById(ret_date);
	var dms = document.getElementById(dep_month);
	var rms = document.getElementById(ret_month);
	if (dms.selectedIndex > rms.selectedIndex ||  (dms.selectedIndex == rms.selectedIndex && dds.selectedIndex > rds.selectedIndex))
	{
		rms.selectedIndex = dms.selectedIndex;
		rds.selectedIndex = dds.selectedIndex;
	}	
}

function fillFromXml(file, id, className, addEmpty) {
	var xmlhttp = getXmlHttp();
	xmlhttp.open("GET", file ,false);
	xmlhttp.send(null);
	var dataDocument = xmlhttp.responseXML;
	var entries = dataDocument.getElementsByTagName('li');
	var selector = document.getElementById(id);
                if(selector != null) {
	var index = 0;
	if (addEmpty) {
		var option = new Option("", "");
		selector.options[index++] = option;
	}
	for (var i = 0; i < entries.length; i++)
	{
		if (entries[i].parentNode.attributes.length > 0 && entries[i].parentNode.attributes[0].nodeValue != className)
			continue;
		
		var name = entries[i].firstChild.data;
		var value = entries[i].attributes.length > 0 ? entries[i].attributes[0].nodeValue : name;
		var isDefault = entries[i].getAttribute("default") != null;
		if (value == "KL")
			isDefault = true;
		var option = new Option(name, value);
		if (isDefault) {
			option.selected = true;
			selector.options[index] = option;
			selector.selectedIndex = index++;
		} else {
			selector.options[index++] = option;		
		}
                       }
	}
}

function getBookingDelay(country) {
	var isIe = /MSIE [56789]/.test(navigator.userAgent) && (navigator.platform == "Win32");	
	try{
		var intReturn;
		var xmlhttp = getXmlHttp();
		xmlhttp.open("GET", "/passage/ebt/CCAExport?requestType=Languages", false);
		xmlhttp.send(null);

		//As IE has different XPath parsing support then mozilla based browsers, branch off IE here
		if (isIe && !document.implementation.hasFeature("XPath", "3.0")) {
			intReturn = getIEXmlAttrib(xmlhttp.responseXML, "//pointofsale[@code='" + country.toUpperCase() + "']", "defaultdeparturedate");
		} else {
			intReturn = getOtherXmlAttrib(xmlhttp.responseXML, "//pointofsale[@code='" + country.toUpperCase() + "']", "defaultdeparturedate");
		}
	} catch(e) {
	}

	if (!intReturn) {
		intReturn = 7;
	}
	return intReturn;
}

function getIEXmlAttrib(xmldoc, strNode, strAttrib) {
	return parseInt(xmldoc.selectSingleNode(strNode).getAttribute(strAttrib), 10);
}

function getOtherXmlAttrib(xmldoc, strNode, strAttrib) {
	return xmldoc.evaluate(strNode + "/@" + strAttrib, xmldoc, null, XPathResult.NUMBER_TYPE, null).numberValue;
}

function fill_ebt() {
	if (document.getElementById("klm-ebt") != null)
	{
		// ebt7
		var ebtPage = new EBTPage('klm-ebt');
		// add this line to determine POS.
		ebtPage.pos = 'KLM';
		var fs = new DBFlightSearch('klm-ebt', '/passage/ebtui/inputsearchcriteria.ajax?posCode=KLM&languageCode=splash', ebtPage, '/passage/ebtui/cabinclass.ajax?posCode=KLM&languageCode=splash');
	}
	else
	{
		// ebt6
		var intBookingDelay = getBookingDelay('klm');
		fillDataSelector('dep_month', 0, intBookingDelay);
		fillDataSelector('ret_month', 0, intBookingDelay + 7);
		fillDataSelector('calendarmonth', 1, 0);

		fillDaySelector('dep_day', intBookingDelay);
		setDay('dep_day', intBookingDelay);
		fillDaySelector('ret_day', intBookingDelay + 7);
		setDay('ret_day', intBookingDelay + 7);

		new TravelAgent();

		var bbmember = getParameterTD("bb");
		if (bbmember) {
			document.book2.corporateSupport.checked = true;
		}	
	}
}

function fill_ici() {
	fillFromXml("/travel/xfd/ici/departures/splash/departures.xml", "flightnumb", "carriers", false);
}

function fill_ici_jffp() {
	var dateSelection = document.getElementById("ffSearchDay");
	var today = new Date();
	var tomorrow = new Date(today.getTime() + 24 * 3600000);
	var date0 = today.getDate() + " " + months[today.getMonth()][1];
	var date1 = tomorrow.getDate() + " " + months[tomorrow.getMonth()][1];
	dateSelection.options[0] = new Option(date0, 0);
	dateSelection.options[1] = new Option(date1, 1);
}

function fill_hc() {
	fillDataSelector('dep_month_other', 0, 0);
	fillDataSelector('ret_month_other', 0, 1);
	fillDataSelector('calendarmonth_other', 1, 0);
	fillDaySelector('dep_day_other', 0);
	setDay('dep_day_other', 0);
	fillDaySelector('ret_day_other', 1);
	setDay('ret_day_other', 1);
}

function fill_tp() {
	fillDataSelector('dep_month_other', 0, 7);
	fillDataSelector('ret_month_other', 0, 14);
	fillDataSelector('calendarmonth_other', 1, 0);
	fillDaySelector('dep_day_other', 7);
	setDay('dep_day_other', 7);
	fillDaySelector('ret_day_other', 14);
	setDay('ret_day_other', 14);
	new TPAgent();
}

/* TPAgent */
TPAgent = function() {
	var aRE = /^a$/i
	var travelagent = this;
	var traveldata = new TravelData();
	var origins = new Origins(document.getElementById("from_other"));
	
	var fillAirport = function(airport) {
		var from = document.getElementById("from_other");
		var fromClass = origins.classes[from[from.selectedIndex].value];
		travelagent.destination.value = airport;
		var xmlelem = traveldata.getAirport(airport);
		travelagent.dest_airportcode.value = xmlelem.getAttribute("code");
		travelagent.dest_classcode.value = xmlelem.getAttribute("class");
		// set classes
		travelagent.setClasses(fromClass, travelagent.dest_classcode.value);
	}
}

function fill_db(db_id) {
	switch(db_id)
	{
		case 'db_ebt':  // ebt7 code!
		case 'db_ebr':
			fill_ebt();
			break;
		case 'db_tt':
		case 'db_tt2':
			var interval = null;
			function initPageObject() 
			{
				try
				{
					Page.Init();
					if (interval != null)
						clearInterval(interval);
				}
				catch (e)
				{			
					if (interval == null)
					{
						interval = setInterval(initPageObject, 100);
					}
				}
			}
			initPageObject();
			break;
		case 'db_ici':
			fill_ici();
			doPrefill();
			break;
		case 'db_ici_jffp':
			fill_ici_jffp();
			break;
		case 'db_tp':
			fill_tp();
			break;
		case 'db_hc':
			fill_hc();
			break;
		case 'db_anc':
			new AncillarySearch();
			break;
	}
}

function getXmlHttp() {
	var xmlhttp;
	if (window.ActiveXObject) {
		try {
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e) {
			try {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
			} catch (E) {
				xmlhttp=false
			}
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	// In case xmlrpc is not supported we will fallback to the old method.
	if(!xmlhttp) {
		alert("incompatible browser version");
	}
	return xmlhttp;
}

function Transformer(xmlDocument, xslPath) {
	if (window.ActiveXObject) {
		return IETransformer(xmlDocument, xslPath);
	} else {
		return FFTransformer(xmlDocument, xslPath);
	}
}

function FFTransformer(xmlDocument, xslPath) {
	var xmlhttp = getXmlHttp();
	xmlhttp.open("GET", xslPath ,false);
	xmlhttp.send(null);
	var xsltProc = new XSLTProcessor();
	xsltProc.importStylesheet(xmlhttp.responseXML);
	var resultDoc =	xsltProc.transformToDocument(xmlDocument);
	var xmlSerial = new	XMLSerializer();
	return xmlSerial.serializeToString(resultDoc);
}

function IETransformer(xmlDocument, xslPath) {
	var xmlhttp = getXmlHttp();
	xmlhttp.open("GET", xslPath ,false);
	xmlhttp.send(null);
	return xmlDocument.transformNode(xmlhttp.responseXML);
}

function doPrefill() {
	var queryStr = unescape(window.location.search);
	var args;
	var tabToSelect = null;
	if (queryStr != "") {
		queryStr = queryStr.substr(1, queryStr.length);
		var pairs = queryStr.split("&"); //Separate name value pairs
		for (var i = 0; i < pairs.length; i++) {
			args = pairs[i].split("=");

			//ICI param conversion
			if (args[0] == 'eticketAirlineCode') {args[0]="flightnumb";}
			if (args[0] == 'eticketFlightNumber') {args[0]="flightnumb2";}

			var element = document.getElementById(args[0]);
			if (element) {
				try {
					element.value = args[1];
				} catch (e) {}
			}
		}
	}
}
// Content: js tradedoubler
function initTradedoubler(strOrgID, strEventID, strReportInfo)
{

	var strLeadNumber = getParameterTD("email");
	var refImage = new Image();
	//var srcImage = "http://tracker.tradedoubler.com/report?organization=" + strOrgID + "&event=" + strEventID + "&leadNumber=" + escape(strLeadNumber) + "&reportInfo=" + escape(strReportInfo);
	var srcImage = "http://www.klm.com/travel/klm_splash/report?organization=" + strOrgID + "&event=" + strEventID + "&leadNumber=" + escape(strLeadNumber) + "&reportInfo=" + escape(strReportInfo);
	refImage.src = srcImage;
}

function getParameterTD(strName) 
{
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	var blnFound = false;
	var i, retValue;
	
	retValue = "";
	i = 0;	
	while (i != parms.length && !blnFound)
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) 
		{
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			blnFound = (strName == key);
			if (blnFound)
				retValue = val;
		}
		i++;
	}
	return retValue;
}