var FlightsTypeEnum = {arrival:"ARRIVAL", departure:"DEPARTURE"} //An Enum type object used to denote flight type var flightTableMinimized = true; //expanded/minimized state of the data table var MIN_FLIGHT_ROWS = 3; //number of flight rows to show in the minimized state var MAX_FLIGHT_ROWS = 10; //number of flight rows to show in the maximized state var currentDateTime = new Date(); //current data and time var currentTime = null; //current time var currentDate = null; //current date var flights = null; //global variable holding flights returned from the server. var OrderByEnum = {time:"time", flight:"flight number", route:"route", status:"status", terminal:"terminal"}; //An Enum object specifying the "order by" column of the data column var orderBy = OrderByEnum.time; //can be by "time", "flight number", "route", "status", and "terminal" //global variable storing the current "order by" column var sortDirection = 1; //global variable storing an "order direction" (1 for ascending and -1 for descending) var timeSortDirection = 1; var flightSortDirection = -1; var routeSortDirecton = -1; var statusSortDirection = -1; var terminalSortDirection = -1; var cities = null; //global variable storing the names of cities. This is to be used for the Autocomplete functionality on the origin/destination field // a global month names array var gsMonthNames = new Array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); // a global day names array var gsDayNames = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ); // Zero-Fill //debug function to inspect a javascript object function dumpObj(obj, name, indent, depth) { if (depth > MAX_DUMP_DEPTH) { return indent + name + ": \n"; } if (typeof obj == "object") { var child = null; var output = indent + name + "\n"; indent += "\t"; for (var item in obj) { try { child = obj[item]; } catch (e) { child = ""; } if (typeof child == "object") { output += dumpObj(child, item, indent, depth + 1); } else { output += indent + item + ": " + child + "\n"; } } return output; } else { return obj; } } //Overriding the String, number, and date prototypes for some formatting later on String.prototype.string = function(l) { var s = '', i = 0; while (i++ < l) { s += this; } return s; } Number.prototype.zf = function(l) { return this.toString().zf(l); } String.prototype.zf = function(l) { return '0'.string(l - this.length) + this; } Date.prototype.format = function(f) { if (!this.valueOf()) return ' '; var d = this; return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi, function($1) { switch ($1.toLowerCase()) { case 'yyyy': return d.getFullYear(); case 'mmmm': return gsMonthNames[d.getMonth()]; case 'mmm': return gsMonthNames[d.getMonth()].substr(0, 3); case 'mm': return (d.getMonth() + 1).zf(2); case 'dddd': return gsDayNames[d.getDay()]; case 'ddd': return gsDayNames[d.getDay()].substr(0, 3); case 'dd': return d.getDate().zf(2); case 'hh': return d.getHours().zf(2); case 'nn': return d.getMinutes().zf(2); case 'ss': return d.getSeconds().zf(2); case 'a/p': return d.getHours() < 12 ? 'a' : 'p'; } } ); } tmpDate = new Date(); currentDateStr = tmpDate.format('ddd dd mmmm yyyy'); //currentDateStr = "Thu 27 October 2005" function TextBoxHint(inputBox, textHint) { if(!document.getElementById) return false; // If browser does not understand, get out now! if(!document.getElementById(inputBox)) return false; // If text box is not found get out! var inputBoxId = document.getElementById(inputBox); // Assign the hbw_postcode to postCodeBox inputBoxId.value = textHint; $(inputBox).addEvent("focus", function() // When the user clicks in the box { if(inputBoxId.value == textHint) // If the value is the default value inputBoxId.value = ''; // Clear the box }); $(inputBox).addEvent("blur", function() { if(inputBoxId.value == '') // If the box is empty inputBoxId.value = textHint; // Fill the box with instruction }); } //updates the data and time in the specified format on the portlet function updateDateAndTime() { currentTime = currentDateTime.format('hh:nn')+" GMT"; currentDate = currentDateTime.format('ddd dd mmm'); //document.getElementById('time').innerHTML = currentTime; document.getElementById('date').innerHTML = currentDate; //document.getElementById('updated').innerHTML = "Last updated "+currentDateTime.format('hh:nn'); } function loadAutoCompleterCities() { if(cities) { //don't do anything cities already loaded } else { AjaxFlightInformationHelper.getCitiesByAirport(airportCode, fillCities); } } //callback function to store the cities in the cities object retrieved from the server (after calling a remote function AjaxFlightInformationHelper.getCities()); function fillCities(cityList) { cities = cityList; //new Autocompleter.Local('routeTextbox', 'routeSuggestList', cities.toArray(), {minChars: 2, fullSearch: true}); var el = $('searchArrivalFrom'); var el2 = $('searchDepTo'); /** * Local */ var tokens = cities; //alert(cities); var completer1 = new Autocompleter.Local(el, tokens, { 'minLength': 2, 'delay': 0, 'filterTokens': function() { var regex = new RegExp('.*' + this.queryValue.escapeRegExp() + '.*', 'i'); return this.tokens.filter(function(token){ return (regex.test(token)); }); }, 'injectChoice': function(choice) { var el = new Element('li').setHTML(this.markQueryValue(choice)); el.inputValue = choice; this.addChoiceEvents(el).injectInside(this.choices); } }); var completer2 = new Autocompleter.Local(el2, tokens, { 'minLength': 2, 'delay': 0, 'filterTokens': function() { var regex = new RegExp('.*' + this.queryValue.escapeRegExp() + '.*', 'i'); return this.tokens.filter(function(token){ return (regex.test(token)); }); }, 'injectChoice': function(choice) { var el2 = new Element('li') .setHTML(this.markQueryValue(choice)); el2.inputValue = choice; this.addChoiceEvents(el2).injectInside(this.choices); } }); } //init function for this page. Any initialization can be done here. var MAX_DUMP_DEPTH = 10; //another debug method to inspect javascript method function dump(arr,level) { var dumped_text = ""; if(!level) level = 0; //The padding given at the beginning of the line. var level_padding = ""; for(var j=0;j \"" + value + "\"\n"; } } } else { //Stings/Chars/Numbers etc. dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; } return dumped_text; } //attaching the init method to the browser's onload event function callOnLoad(init) { if (window.addEventListener) { window.addEventListener("load", init, false); } else if (window.attachEvent) { window.attachEvent("onload", init); } else { window.onload = init; } } //calling remote method to get suggested cities list function updateRouteSuggestList(autocompleter, token) { //AjaxFlightInformationHelper.suggestRoute(token, function(data){ autocompleter.setChoices(data); }); AjaxFlightInformationHelper.getCitiesByAirport(airportCode, function(data){ autocompleter.setChoices(data); }); } //helper functionf or AutoCompeleter.DWR function nameValueSelector(tag) { return tag; } //Main method that calls remote method to search or load flight information function callRemoteSearchMethod() { //AjaxFlightInformationHelper.getDummyFlightInfo(showFlightInfo); //AjaxFlightInformationHelper.findFlights(flightType, airportCode, terminalId, flightNumber, route, orderBy, sortDirection, showFlightInfo); var flightNumber = ""; var route = ""; if(flightType == FlightsTypeEnum.arrival) { //Clear the flight data first clearFlightsTable("arrivalFlightsTable"); flightNumber = document.getElementById('arrivalFlightNo').value; if(flightNumber == document.getElementById('arrivalFlightNo').title) flightNumber = ""; route = document.getElementById('searchArrivalFrom').value; if(route == document.getElementById('searchArrivalFrom').title) route = ""; if(flightNumber.length>0 && ((flightNumber.length>7) || !flightNumber.match(/[a-zA-Z]+[0-9]*/))) { document.getElementById("arrivalErrorSpan").innerHTML = ERROR_INVALID_FLIGHT_NUMBER; return; } else { document.getElementById("arrivalErrorSpan").innerHTML = ""; } } else { //clear the flight data first clearFlightsTable("departureFlightsTable"); flightNumber = document.getElementById('depFlightNo').value; if(flightNumber == document.getElementById('depFlightNo').title) flightNumber = ""; route = document.getElementById('searchDepTo').value; if(route == document.getElementById('searchDepTo').title) route = ""; if(flightNumber.length>0 && ((flightNumber.length>7) || !flightNumber.match(/[a-zA-Z]+[0-9]*/))) { document.getElementById("depErrorSpan").innerHTML = ERROR_INVALID_FLIGHT_NUMBER; return; } else { document.getElementById("depErrorSpan").innerHTML = ""; } } if((terminalId==null) || (terminalId == "null")) { showGif(); AjaxFlightInformationHelper.findFlights(flightType, airportCode, "", flightNumber, route, orderBy, sortDirection, showFlightInfo); } else { showGif(); AjaxFlightInformationHelper.findFlights(flightType, airportCode, terminalId, flightNumber, route, orderBy, sortDirection, showFlightInfo); } document.getElementById("arrivalErrorSpan").innerHTML = ""; document.getElementById("depErrorSpan").innerHTML = ""; } var getScheduled = function(flight) { return flight.scheduled;} var getCode = function(flight) { return flight.code;} var getRouteString = function(flight) { return flight.routeString;} var getStatus = function(flight) { return flight.status;} var getTerminal = function(flight) { return flight.terminal;} var getTerminalName = function(flight){ return flight.terminalName;} var getBlank = function(flight) { return " ";} function showGif() { var busyGif = document.getElementById('busygif'); busyGif.style.display = 'inline'; busyGif.style.height = '10pt'; } function hideGif() { var busyGif = document.getElementById('busygif'); busyGif.style.display = 'none'; busyGif.style.height = '0pt'; } //toggles the flight table between minimized and maximized states and brings back flight information data if necessary function toggleCollapseFlightTable() { showGif(); flightTableMinimized = !flightTableMinimized; if(flights == null) { callRemoteSearchMethod(); } else { setTimeout("showFlightInfo(flights);", 10); } return; } //callback method that is called after flights are retrieved from the server function showFlightInfo(result) { //console.time("Flight Information Painting"); flights = result; //dwr.util.removeAllRows("flightsTable"); //dwr.util.addRows("flightsTable", flights, [ getScheduled, getCode, getRouteString, getStatus, getTerminalName, getBlank, getBlank ]); if(flightType == FlightsTypeEnum.arrival) { clearFlightsTable("arrivalFlightsTable"); } else { clearFlightsTable("departureFlightsTable"); } //alert("findFlights results: "+flights.length); fillFlightsTable(flights); //console.timeEnd("Flight Information Painting"); updateDateAndTime(); //flights[item].scheduled.format('hh:nn'), flights[item].code, flights[item].routeString, flights[item].status, flights[item].terminalName, " ", " "); hideGif(); } //helper method to clear the tbody of a table (arrival or departures flight table) function clearFlightsTable(tablename) { var mytable = document.getElementById(tablename); var oldtablebody = mytable.getElementsByTagName("tbody")[0]; clearTable(oldtablebody); //var mytbody = document.createElement("tbody"); //mytable.replaceChild(mytbody, oldtablebody); //oldtablebody.innerHTML = ""; } function clearTable(tbody) { while (tbody.rows.length > 0) { tbody.deleteRow(0); } } //helper method to repaint the flights table (whether it be arrival or departures) function fillFlightsTable(flights) { var mytable = null; var flightsDiv = null; if(flightType == FlightsTypeEnum.arrival) { flightsDiv = document.getElementById("arrivals"); mytable = document.getElementById("arrivalFlightsTable"); } else { flightsDiv = document.getElementById("departures"); mytable = document.getElementById("departureFlightsTable"); } mytablebody = document.createElement("tbody"); var flightsToShow = 0; var showMoreRows = false; var showLessRows = false; var flightNum = flights.length; if(flightTableMinimized) { if(flightNum > 0 && flightNum <= 3) flightsToShow = flights.length; else { flightsToShow = 3; showMoreRows = true; } flightsDiv.style.height = ""; flightsDiv.style.overflow = "hidden"; flightsDiv.style.overflowX = "hidden"; } else { if(flightNum>=3) { showLessRows = true; } flightsToShow = flights.length; if(flightNum>=0 && flightNum <=10) { //unscrolled version flightsDiv.style.height = ""; flightsDiv.style.overflow = "hidden"; flightsDiv.style.overflowX = "hidden"; } else { flightsDiv.style.height = "220px"; flightsDiv.style.overflow = "scroll"; flightsDiv.style.overflowX = "hidden"; //scrollable version after 10 rows. } } var oldDate = null; var newDate = null; oldtablebody = mytable.getElementsByTagName("tbody")[0]; if(flights.length == 0) { //addDateRow(mytablebody, 0, "No Matching Results."); if(flightType == FlightsTypeEnum.arrival) addDateRow(mytablebody, 0, MSG_NO_MATCHING_ARRIVALS); else addDateRow(mytablebody, 0, MSG_NO_MATCHING_DEPS); } for(var item=0;item