/* -------------------------------------------------------------------------- */
/* HMPAForm.js                                                                */
/* -------------------------------------------------------------------------- */
/* Manage HMPA javascript:																										*/
/* - the form                                                                 */
/* -------------------------------------------------------------------------- */
/**
* @fileoverview This javascript of HMPA page.
*/
WDSClass.extend("HMPAForm", "WDSForm");
/**
* @class Handle the form HMPA_FORM.
* @constructor
* @return a new HMPAForm instance.
*/
function HMPAForm () {
HMPAForm.superclass.constructor.call(this, "HMPA_FORM", "", true);
this.cityList = new WDSList("CityList");
this.initCalendar();
this.SearchCookie = new WDSCookie("Search");
this.SearchSessionCookie = new WDSCookie("Search.Session");
}
/**
* get the city list
*/
HMPAForm.prototype.getCityList = function () {
return this.cityList;
}
/**
* restore the value contained in the cookie.
* Sessions cookie got priority
*/
HMPAForm.prototype.restoreCookiesValue = function () {
this.SearchCookie.load();
this.SearchSessionCookie.load();
}
/**
* save datas in the cookie.
*/
HMPAForm.prototype.saveCookiesValue = function () {
this.SearchCookie.reinit();
var today = new Date();
this.SearchCookie.setExpires(new Date(today.getFullYear()+3, today.getMonth()));
this.SearchSessionCookie.remove();
/* null : cookie die will browser die */
this.SearchSessionCookie.setExpires(new Date(today.getFullYear()+3, today.getMonth()));
for (var i=1; i<=6; i++) {
this.SearchCookie.add("document.HMPA_FORM.B_LOCATION_"+i);
this.SearchCookie.add("document.HMPA_FORM.E_LOCATION_"+i);
this.SearchSessionCookie.add("document.HMPA_FORM.Calendar_"+i+"MonthYear");
this.SearchSessionCookie.add("document.HMPA_FORM.Calendar_"+i+"Day");
this.SearchSessionCookie.add("document.HMPA_FORM.Calendar_"+i+"Hour");
this.SearchSessionCookie.add("document.HMPA_FORM.B_DATE_"+i);
}
this.SearchSessionCookie.add("document.HMPA_FORM.WDS_ADULT");
this.SearchSessionCookie.add("document.HMPA_FORM.WDS_CHILD");
this.SearchSessionCookie.add("document.HMPA_FORM.WDS_INFANT");
this.SearchSessionCookie.add("document.HMPA_FORM.WDS_FLEXIBLE");
this.SearchSessionCookie.add("document.HMPA_FORM.ARRANGE_BY");
this.SearchCookie.save();
this.SearchSessionCookie.save();
}
/**
* Checks done before performing the form
*/
HMPAForm.prototype.check = function () {
var last=0;
for (var i=1; i<=6; i++)	{
if ( this.getValue('B_LOCATION_'+i) != '-' && this.getValue('B_LOCATION_'+i) != '') {
if ( this.getValue('E_LOCATION_'+i) == '-' || this.getValue('E_LOCATION_'+i) == '') {
if ( i == 1 ) {
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectArrivalCity"));
} else {
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectArrivalCityForFlight")+i+".");
}
} else {
if (i-last>1) {
for(var j=last+1;j<i;j++) {
if ( (this.getValue('E_LOCATION_'+j) == '-' || this.getValue('E_LOCATION_'+j) == '') &&
(this.getValue('B_LOCATION_'+j) == '-' || this.getValue('B_LOCATION_'+j) == '') ) {
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectArrivalCityForFlight")+j+".");
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectDepartureCityForFlight")+j+".");
}
}
}
last = i;
this.setDisabled("B_DATE_"+i,false);
this.switchFlexibility(i, true);
}
} else {
if ( i == 1 )
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectDepartureCity"));
if ( this.getValue('E_LOCATION_'+i) != '-' && this.getValue('E_LOCATION_'+i) != '') {
if ( i == 1 ) {
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectDepartureCity"));
} else {
WDSError.add(WDSMessage.getMessage("HMPA.js.SelectDepartureCityForFlight")+i+".");
}
} else {
this.setDisabled("B_DATE_"+i,true);
this.switchFlexibility(i, false);
}
}
}
this.setPassengers();
}
/**
* set given flight's tags about date range, according to given flexibility
* @param (String) flightNumber: flight number
* @param (String) send:
*/
HMPAForm.prototype.switchFlexibility = function (flightNumber, send) {
if ( this.getValue("WDS_FLEXIBLE") == "TRUE" && send ) {
this.setDisabled("DATE_RANGE_QUALIFIER_"+flightNumber,false);
this.setDisabled("DATE_RANGE_VALUE_"+flightNumber,false);
} else {
this.setDisabled("DATE_RANGE_QUALIFIER_"+flightNumber,true);
this.setDisabled("DATE_RANGE_VALUE_"+flightNumber,true);
}
}
/**
* define tags and datas related to paxs
*/
HMPAForm.prototype.setPassengers = function () {
var nbAdults   = parseInt(this.getValue("WDS_ADULT"));
var nbChildren = parseInt(this.getValue("WDS_CHILD"));
var nbInfants  = parseInt(this.getValue("WDS_INFANT"));
if ( (nbAdults + nbChildren + nbInfants) > 9 ) {
WDSError.add(WDSMessage.getMessage("HMPA.js.Only9Passengers"));
}	else if ( nbInfants > nbAdults ) {
WDSError.add(WDSMessage.getMessage("HMPA.js.Only1InfantPerAdult"));
}	else {
for ( var i=1; i<=nbAdults; i++ ) {
this.setValue('TRAVELLER_TYPE_'+i,'ADT');
}
for ( var j=(nbAdults+1); j<=(nbAdults+nbChildren); j++ ) {
this.setValue('TRAVELLER_TYPE_'+j,'CHD');
}
for ( var k=1; k<=nbInfants; k++ ) {
this.setValue('HAS_INFANT_'+k,'TRUE');
}
for ( var l=(nbAdults+nbChildren+1); l<=9; l++ ) {
this.setDisabled("TRAVELLER_TYPE_"+l,true);
}
for ( var m=(nbInfants+1); m<=9; m++ ) {
this.setDisabled("HAS_INFANT_"+m,true);
}
}
}
/**
* cf. WireFrame: HMPA02
* Time Value is not the same depending on HMPAForm.FLEXIBLE
* The logic is to set hours to 0000 if FLEXIBLE is TRUE, used just before submit
*/
HMPAForm.prototype.setCalendarHour = function () {
if( this.getValue("WDS_FLEXIBLE") == "TRUE" ){
for(var i=1; i<=6 ; i++){
var newDate = this.getValue("B_DATE_"+i);
this.setValue("B_DATE_"+i, newDate.substr(0,8) + "0000");
}
} else {
/* set correct hour time (MORNING by defult = 0001) */
for ( var i=1; i<=6 ; i++ ) {
var correctHour = this.getValue("Calendar_"+i+"Hour");
if ( correctHour == "0001" ) {
var initialValue = this.getValue("B_DATE_"+i);
this.setValue("B_DATE_"+i, initialValue.substr(0,8) + "0000");
}
}
}
}
/**
* prepare submit
*/
HMPAForm.prototype.prepareSubmit = function () {
this.saveCookiesValue();
this.setCalendarHour();
this.isAlreadySubmited = false;
}
/**
* select tomorrow as selected date by default.
*/
HMPAForm.prototype.setDefautlDate = function () {
var today		 = new Date();
var tomorrow = new Date();
var tomorrowMonth;
var tomorrowMonthString;
var tomorrowMonthYear;
var tomorrowDayInMonth;
var tomorrowDay;
nbMillisec = today.getTime();
tomorrow.setTime(nbMillisec + 86400000); // 86400000 millisec = 24h
tomorrowDay = tomorrow.getDate().toString();
if(tomorrowDay < 10){
tomorrowDay = "0" + tomorrowDay.toString();
} else {
tomorrowDay = tomorrowDay.toString();
}
tomorrowMonth = tomorrow.getMonth() + 1;
if(tomorrowMonth < 10){
tomorrowMonthString = "0" + tomorrowMonth.toString();
} else {
tomorrowMonthString = tomorrowMonth.toString();
}
tomorrowMonthYear = tomorrowMonthString + tomorrow.getFullYear();
this.setValue("Calendar_1MonthYearId", tomorrowMonthYear);
this.setValue("Calendar_1DayId", tomorrowDay);
Calendar_1.update();
}