function SimpleMVCController() { }; var cachedViews = new Array(); var ViewsEnum = {START_VIEW: "startView", FLIGHTS_FORM: "FlightsForm", CONNECTING_FLIGHTS_FORM: "ConnectingFlightsForm", CONNECTING_FLIGHTS_RESULT: "ConnectingFlightsResult", FLIGHTS_NO_RESULT: "FlightsNoResults", FLIGHTS_ONE_RESULT: "FlightsOneResult", FLIGHTS_MULTIPLE_RESULTS: "FlightsMultipleResults"}; var data = {}; var currentMonth; var wt_controller = new SimpleMVCController(); data.searchByFlight = true; data.flightType = "arrival"; var wtCities; var todaysDate; var latestDate; //The id of the div where everything will be repainted wt_controller.resultsContainer = "whichTerminalMainContainer"; /** * Attaches an autocompleter component with a field showing matching cities after * 3 characters are entered. */ wt_controller.fillCities = function(cityList, el) { wtCities = cityList; var tokens = wtCities; var completer1 = new Autocompleter.Local(el, tokens, { 'minLength': 3, '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); } }); }; wt_controller.GotCities = function(cities) { alert(cities); }; /** * Retrieves a list of cities to be used by autocompleter components */ wt_controller.loadAutocompleterCities = function(citiesField) { if(!wtCities) { AjaxTerminalFinderHelper.getCities(wtAirportCode,{ callback: function(cityList) { wt_controller.fillCities(cityList, citiesField); } }); } else { wt_controller.fillCities(wtCities, citiesField); } }; /** * Injects the result html into the resultsContainer div */ wt_controller.showResults = function(result) { document.getElementById(wt_controller.resultsContainer).innerHTML = result; wt_controller.attachTips(); }; /** * Go back to the page */ wt_controller.showStartPage = function(result) { document.getElementById(wt_controller.resultsContainer).innerHTML = result; if(!cachedViews[ViewsEnum.START_VIEW]) { cachedViews[ViewsEnum.START_VIEW] = result; } }; /** * Hides the hideDiv component and displays the displayDiv component * This is for use in the form where the user can either choose to * enter a flight number or a destination. */ wt_controller.swapDivs = function(displayDiv, hideDiv) { document.getElementById(displayDiv).style.display = ""; document.getElementById(hideDiv).style.display = "none"; }; /** * Searches for arrival flights based on the date and either * the flight number or the airport arriving from. */ wt_controller.searchFlights = function() { wt_controller.displayLoadingImage(true); if(wt_controller.validateFlightForm()) { data.errors = []; var flightDate = new Date(); var dateStr = document.forms["wt_flightForm"].elements["hiddenDateField"].value; var splitDateStr = dateStr.split("/"); var monthYearStr = splitDateStr[1].split(","); flightDate.setDate(splitDateStr[0]); flightDate.setMonth(monthYearStr[0]-1); flightDate.setFullYear(monthYearStr[1]); var flightCode = ""; var flightDest = ""; if(data.searchByFlight) { flightCode = document.forms["wt_flightForm"].elements["wt_flightNumber"].value; data.flightCode = flightCode; } else { flightDest = document.forms["wt_flightForm"].elements["wt_flyingLocation"].value; data.flightDest = flightDest; } if(data.flightType == "arrival") { AjaxTerminalFinderHelper.searchFlights(wtAirportCode, "A", flightDate, flightCode, flightDest, wt_controller.showFlightSearchResult); } if(data.flightType == "departure") { AjaxTerminalFinderHelper.searchFlights(wtAirportCode, "D", flightDate, flightCode, flightDest, wt_controller.showFlightSearchResult); } if(data.flightType == "connectingInbound") { AjaxTerminalFinderHelper.searchFlights(wtAirportCode, "A", flightDate, flightCode, flightDest, wt_controller.showFlightSearchResult); } if(data.flightType == "connectingOutbound") { AjaxTerminalFinderHelper.searchFlights(wtAirportCode, "D", flightDate, flightCode, flightDest, wt_controller.showFlightSearchResult); } } else { if(data.flightType == "connectingInbound" || data.flightType == "connectingOutbound") { wt_controller.getView(ViewsEnum.CONNECTING_FLIGHTS_FORM,wt_controller.showFlightsForm); } else { wt_controller.getView(ViewsEnum.FLIGHTS_FORM,wt_controller.showFlightsForm); } wt_controller.displayLoadingImage(false); } }; wt_controller.showFlightNoResults = function(result) { wt_controller.processAndDisplay(result, ViewsEnum.FLIGHTS_NO_RESULT); }; /** * Validates that the given date is between today and six months. */ wt_controller.validateFlightForm = function() { var errors = new Array(); var dateStr = document.forms["wt_flightForm"].elements["hiddenDateField"].value; var splitStr = dateStr.split("/"); var day = splitStr[0]; var monthYearStr = splitStr[1].split(","); var month = monthYearStr[0]; var year = monthYearStr[1]; if(!wt_controller.checkBeforeToday(year, month, day)) { errors.push("Date is in the past"); } var date = new Date(); date.setFullYear(year); date.setMonth((month*1)-1); date.setDate(day); // entry date must be less than maximum date (13 months ahead) if( date.print("%Y%m") > wt_controller.getMaxMonthDate().print("%Y%m") ) { errors.push("Flight information held up to "+wt_controller.getMaxMonthDate().print("%m/%Y")+". Please select a date within the range."); } //need to also see if the destination is chosen, is it valid. if(!data.searchByFlight) { var arrivalFrom = document.forms["wt_flightForm"].elements["wt_flyingLocation"].value; if(arrivalFrom.length >0) { var found = false; for(var i=0;i-1) { found = true; break; } } if(!found) { if(data.flightType=="arrival" || data.flightType=="connectingInbound") { errors.push("Departure airport not found. Please double check."); } if(data.flightType=="departure" || data.flightType=="connectingOutbound") { errors.push("Destination not found. Please double check."); } //errors.push("Search airport not found. Please double check."); } } else { errors.push("Please specify an airport."); } } else { var flightCode = document.forms["wt_flightForm"].elements["wt_flightNumber"].value; if(flightCode == document.forms["wt_flightForm"].elements["wt_flightNumber"].title) { errors.push("Please enter a flight number."); } } if(errors.length>0) { data.errors = errors; return false; } else return true; }; /** * Display the first arriving/departure flights form and set up associated components * like the calendar and the autocompleter. This also caches the javascript * template for the arriving/departure flights form for future use. */ wt_controller.showFlightsForm = function(template) { var result2 = template.process(data); document.getElementById(wt_controller.resultsContainer).innerHTML = result2; wt_controller.displayLoadingImage(false); Calendar.setup({ date : todaysDate, inputField : "flightDateHiddenField", // id of the input field ifFormat : "%e/%m,%Y", // format of the input field (even if hidden, this format will be honored) button : "flightCalendar", // trigger button (well, IMG in our case) align : "Tr", // alignment (defaults to "Bl") singleClick : true, dateStatusFunc : wt_controller.dateStatusHandler }); if(!(data.flightType == "connectingOutbound")) { //set date to today's date. wt_controller.setDateNow("wt_flightForm", new Date()); } else { //set date to selected inbound flight var selectedDate = data.selectedInboundFlight.scheduledStart; wt_controller.setDateNow("wt_flightForm", selectedDate); } new TextBoxHint("wt_flightNumber", "e.g. BA1234" ); new TextBoxHint("wt_flyingLocation", "Enter city or airport" ); wt_controller.loadAutocompleterCities(document.forms["wt_flightForm"].elements["wt_flyingLocation"]); if(data.flightType == "connectingInbound" || data.flightType == "connectingOutbound") { if(!cachedViews[ViewsEnum.CONNECTING_FLIGHTS_FORM]) { cachedViews[ViewsEnum.CONNECTING_FLIGHTS_FORM] = template; } } else { if(!cachedViews[ViewsEnum.FLIGHTS_FORM]) { cachedViews[ViewsEnum.FLIGHTS_FORM] = template; } } wt_controller.attachTips(); }; wt_controller.updateFlightDateHiddenField = function(formName) { var day = document.forms[formName].elements["termDay"].value; var monthYear = document.forms[formName].elements["termMonth"].value; var dateStr = day+"/"+monthYear; document.forms[formName].elements["hiddenDateField"].value = dateStr; }; wt_controller.updateMonthDays = function() { var monthSelect = document.getElementById("termMonth"); var monthString = monthSelect.options[monthSelect.selectedIndex].value; var monthStringSplit = monthString.split(","); var month = monthStringSplit[0]*1; var yearStr = monthStringSplit[1]; var year = yearStr*1; var maxDays = wt_controller.daysInMonth(month, year); var daySelect = document.getElementById("termDay"); var selectedDay = daySelect.selectedIndex; //removeAllOptions(daySelect); daySelect.length = 0; for(var i=1; i<=maxDays; i++) { wt_controller.addOption(daySelect,i,i); } daySelect.selectedIndex = selectedDay; }; /** * Validator for the Calendar component that disables certain dates * Here, no dates before and after 6 months ares selectable */ wt_controller.dateStatusHandler = function(date, y, m, d) { // entry date cannot be before today if(wt_controller.checkBeforeToday(y,m+1,d) == false) { return true; } // entry date must be less than maximum date (13 months ahead) //if( date.print("%Y%m") > wt_controller.getMaxMonthDate().print("%Y%m") ) { // return true; //} if(date.getTime() > latestDate.getTime()) { return true; } return false; }; /** * Checks whether the given date is before today * */ wt_controller.checkBeforeToday = function(yearStr, monthStr, dayStr) { monthStr = monthStr - 1; var theDate = new Date(); theDate.__msh_oldSetFullYear( yearStr, monthStr, dayStr); if ( theDate.print("%Y%m%d") < new Date().print("%Y%m%d") ) { return false; } else { return true; } }; /** * Sets the appropriate form date elements to today's date. */ wt_controller.setDateNow = function(formName, date) { //var now = new Date(); document.forms[formName].elements['hiddenDateField'].value = date.print("%e/%m,%Y"); document.forms[formName].elements['termMonth'].value = date.print("%m,%Y"); //document.forms['carparkquote'].elements['dd'].selectedIndex=now.getDate() - 2; document.forms[formName].elements['termDay'].value = date.getDate(); }; /** * Calculates the latest date possible that can be chosen using the calendar component */ wt_controller.getMaxMonthDate = function() { var maxMonth = data.flightMonths[data.flightMonths.length-1].value; var maxMonthMySplit = maxMonth.split(","); var monthStr = maxMonthMySplit[0]; var yearStr = maxMonthMySplit[1]; var maxMonthDate = new Date(); maxMonthDate.__msh_oldSetFullYear( yearStr, monthStr-1, 1); return maxMonthDate; }; /** * Gets a given javascript view template from the server and calls the given callback function. * Takes care to cache the view if it has been loaded from the server already. * */ wt_controller.getView = function(viewName, callBackFunc) { //check local cache first if(cachedViews[viewName]) { callBackFunc(cachedViews[viewName]); } else { //get from server AjaxTerminalFinderHelper.getViewTemplate(viewName, callBackFunc); } }; /** * Navigate from the start view to the arrival flights form * */ wt_controller.goToArrivalFlights = function() { data.flightType = "arrival"; wt_controller.clearMatchingData(); wt_controller.displayLoadingImage(true); wt_controller.getView(ViewsEnum.FLIGHTS_FORM,wt_controller.showFlightsForm); }; wt_controller.goToDepartureFlights = function() { data.flightType = "departure"; wt_controller.clearMatchingData(); wt_controller.displayLoadingImage(true); wt_controller.getView(ViewsEnum.FLIGHTS_FORM,wt_controller.showFlightsForm); }; wt_controller.goToConnectingInboundFlights = function() { data.flightType = "connectingInbound"; wt_controller.clearMatchingData(); wt_controller.displayLoadingImage(true); wt_controller.getView(ViewsEnum.CONNECTING_FLIGHTS_FORM,wt_controller.showFlightsForm); }; wt_controller.goToConnectingOutboundFlights = function() { data.flightType = "connectingOutbound"; wt_controller.clearMatchingData(); wt_controller.displayLoadingImage(true); wt_controller.getView(ViewsEnum.CONNECTING_FLIGHTS_FORM,wt_controller.showFlightsForm); }; wt_controller.goToStart = function() { data.errors = []; data.searchByFlight = true; wt_controller.clearMatchingData(); wt_controller.getView(ViewsEnum.START_VIEW, wt_controller.showStartPage); $("whichTerminalMainContainer").style.display = ""; }; wt_controller.clearMatchingData = function() { data.matchingFlights = null; data.matchingFlight = null; } /** * Adds a given number of months to a date and returns it. * */ wt_controller.addMonthsToDate = function(startDate, numMonths){ var addYears = Math.floor(numMonths/12); var addMonths = numMonths - (addYears * 12); var newMonth = startDate.getMonth() + addMonths; if (startDate.getMonth() + addMonths > 11) { ++addYears; newMonth = startDate.getMonth() + addMonths - 12; } var newDate = new Date(startDate.getFullYear()+addYears,newMonth,startDate.getDate(),startDate.getHours(),startDate.getMinutes(),startDate.getSeconds()); // adjust to correct month while (newDate.getMonth() != newMonth) { newDate = wt_controller.addDaysToDate(newDate, -1); } return newDate; }; /** * Adds a given number of days to a date and returns it */ wt_controller.addDaysToDate = function(myDate,days) { return new Date(myDate.getTime() + (days*24*60*60*1000)); }; /** * Calculates the number of days in a month */ wt_controller.daysInMonth = function(iMonth, iYear) { return 32 - new Date(iYear, iMonth-1, 32).getDate(); }; /** * Event to notify form date elements that a date has been changed by the calendar * */ wt_controller.dateChanged = function(formName, dateValue) { var dates = dateValue.split("/"); // dates[0] = 31 // dates[1] = 08,07 (August 2007) document.forms[eval("\""+formName+"\"")].elements["termDay"].selectedIndex = dates[0]-1; document.forms[eval("\""+formName+"\"")].elements["termMonth"].value = dates[1]; wt_controller.updateMonthDays(); }; /** * component for implementing a hint in a textbox. The hint disappears when the focus is set to the textbox * and reappears when focus is removed. */ 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 }); }; /* * Initialize the date and month data to be used by the calendars * */ wt_controller.init = function() { dwr.engine.setHttpMethod("GET"); data.regex = /[^a-z]{2,}\s+?([A-Z]{2,})+?|[A-Z]+/; data.errors = []; todaysDate = new Date(); var daysInMonth = wt_controller.daysInMonth(todaysDate.getMonth()+1,todaysDate.getFullYear()); var flightDays = new Array(daysInMonth); var tempDate = new Date(); for(var i=1;i<=daysInMonth;i++) { tempDate.setDate(i); flightDays[i] = { value: tempDate.print("%e"), display: tempDate.print("%d")}; } var flightMonths = new Array(6); flightMonths[0] = {value: todaysDate.print("%m,%Y"), display: todaysDate.print("%b %Y")}; latestDate = wt_controller.addDaysToDate(todaysDate, 180); var numberOfMonths = 0; if(latestDate.getMonth() 0) { data.matchingFlights = results; wt_controller.getView(ViewsEnum.FLIGHTS_MULTIPLE_RESULTS,wt_controller.showFlightMultipleResults); } }; wt_controller.showFlightDetails = function(flightCode) { wt_controller.displayLoadingImage(true); for(f in data.matchingFlights) { if(data.matchingFlights[f].flightCode == flightCode) { data.matchingFlight = data.matchingFlights[f]; break; } } wt_controller.getView(ViewsEnum.FLIGHTS_ONE_RESULT,wt_controller.showFlightOneResult); wt_controller.attachTips(); }; Array.prototype.findProperty = function(property, value) { var result = null; var i = 0; for(i=0;i