/* ------------------------------------------------------------------------------------------------------------ */
/* Panels/CommonForms/CommonSearchForm.js                                                  Lufthansa project -- */
/* ------------------------------------------------------------------------------------------------------------ */
/* Common search form to FMPA and ADVS                                                                       */
/* ------------------------------------------------------------------------------------------------------------ */
/* Author : Yannick Adam                                                                                     */
/* ------------------------------------------------------------------------------------------------------------ */

WDSClass.extend("CommonSearchForm", "WDSForm");
function CommonSearchForm () {
	CommonSearchForm.superclass.constructor.call(this, "CommonSearchForm", "loadingAVAI");
	/* attributes */
	this.TravelClassList = null;	
	this.SearchCookie    = null;
}


/* CommonSearchForm_check  */

/* * check the form and submit it when no error found */
/* * displays the error panel otherwise */
CommonSearchForm.prototype.check = function () {
	WDSError.init();
	this.preCheck();
  if (WDSError.hasError()) {
		WDSError.showAlert();    
  }
  else {
		this.saveCookie();
		
		var lSearchType = WDSCommon.getTagValue( document.CommonSearchForm.SearchType );
		if( lSearchType == 'FLEX' || lSearchType == 'NOFLEX')  {
			if (this.WebStatAllow()){
				this.MultiTrack(document.FARE_DRIVEN_FORM);
			}
			document.FARE_DRIVEN_FORM.submit();
		} else {
			if (this.WebStatAllow()){
				this.MultiTrack(document.SCHEDULE_DRIVEN_FORM);
			}
			document.SCHEDULE_DRIVEN_FORM.submit();
		}
	}
}


/* MultiTrack */
/* * Call the dcsMultiTrack fct, with all the parameter */
CommonSearchForm.prototype.MultiTrack = function (iForm) {
		/* Select the Correct SearchType*/
		var lSearchType = WDSCommon.getTagValue( document.CommonSearchForm.SearchType );
		var lSearch = '';
		if (lSearchType == 'FLEX')
		{
			lSearch = 'FDFLEX';
		}
		else 
		{
			if(lSearchType == 'NOFLEX')
			{
				lSearch = 'FD';
			}
			else
			{
				lSearch = 'SD';
			}
		}
		/* Select the Correct CabinType*/
		var lCabin = WDSCommon.getTagValue( document.CommonSearchForm,'CABIN');
		
		/* Select the Correct TripType*/
		var lTripType = WDSCommon.getTagValue( document.CommonSearchForm,'TRIP_TYPE');
		var lTrip = '';
		if (lTripType == 'O')
		{
			lTrip = 'OW';
		}
		else
		{
			lTrip = 'RT';
		}	
		var nbPax = parseInt(WDSCommon.getTagValue(document.CommonSearchForm,"NUMBER_OF_ADULTS")) + parseInt(WDSCommon.getTagValue(document.CommonSearchForm,"NUMBER_OF_CHILDREN")) + parseInt(WDSCommon.getTagValue(document.CommonSearchForm,"NUMBER_OF_INFANTS")) ;
		dcsMultiTrack('DCSext.BFO',WDSCommon.getTagValue( iForm,'B_LOCATION_1'),
			'DCSext.BFD',WDSCommon.getTagValue( iForm,'E_LOCATION_1' ),
			'DCSext.BFDepDate',OutboundCalendar.getFormattedDate("YYYYmmDD"),
			'DCSext.BFRetDate',InboundCalendar.getFormattedDate("YYYYmmDD"),
			'DCSext.BFTravelType',lTrip,	
			'DCSext.BFSearchType',lSearch,
			'DCSext.BFPax','ADT'+WDSCommon.getTagValue(document.CommonSearchForm,"NUMBER_OF_ADULTS")+',CHD'+WDSCommon.getTagValue(document.CommonSearchForm,"NUMBER_OF_CHILDREN")+',INF'+WDSCommon.getTagValue(document.CommonSearchForm,"NUMBER_OF_INFANTS"),
			'DCSext.BFAmountofPax',nbPax,	
			'DCSext.BFBooClass',lCabin,	
			'DCSext.BFRequestType','FLIGHT');
}

/* CommonSearchForm_checkBKGD  */

/* * check the form and submit it when no error found */
/* * displays the error panel otherwise */
CommonSearchForm.prototype.checkBKGD = function () {
	WDSError.init();
	this.preCheckBKGD();
  if (WDSError.hasError()) {
		WDSError.showAlert();    
  }
  else {
		this.saveCookie();		
		document.SUBMIT_TO_BKGD_FORM.submit();
	}	
}


/* CommonSearchForm_manageBackButton  */
CommonSearchForm.prototype.manageBackButton = function () {
	/* Setup the Round Trip or One way radio button */
	if( WDSCommon.getTagValue( document.CommonSearchForm.TRIP_TYPE ) == 'R' ) {
		this.selectRoundTrip();
	}	else {
		this.selectOneWayTrip();
	}
		
	/* Update calendar dates with the values stored previously */
	if( WDSCommon.getTagValue( document.CommonSearchForm.SEL_BDATE1 ) != '' ) {
		OutboundCalendar.updateFromPopup( new Date( WDSCommon.getTagValue( document.CommonSearchForm.SEL_BDATE1 )) );
	}
	if( WDSCommon.getTagValue( document.CommonSearchForm.SEL_BDATE2 ) != '' ) {
		InboundCalendar.updateFromPopup( new Date( WDSCommon.getTagValue( document.CommonSearchForm.SEL_BDATE2 )) );
	}
	
}


/* CommonSearchForm_loadCookie */
CommonSearchForm.prototype.loadCookie = function () {
	this.SearchCookie.load();
	WDSTrace.dump("-"+this.SearchCookie.getText()+"-");	
	this.manageBackButton();
}


/* CommonSearchForm_flexChange  */
CommonSearchForm.prototype.flexChange = function () {
	/* Store flexibility value in a hidden field in case the user presses the back button */
	/* WDSCommon.updateTag( document.CommonSearchForm, 'SEL_FLEX', WDSCommon.getTagValue( document.CommonSearchForm.FLEXIBILITY ) ); */
}


/* CommonSearchForm_cabinChange */
CommonSearchForm.prototype.cabinChange = function () {
	/* Store cabin value in a hidden field in case the user presses the back button */
	WDSCommon.updateTag( document.CommonSearchForm, 'SEL_CABIN', WDSCommon.getTagValue( document.CommonSearchForm.CABIN ) );
}


/* CommonSearchForm_changeAdults  */
CommonSearchForm.prototype.changeAdults = function () {
	/* Verify number of adults >= Number of infants */
	var lNmbAdults = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_ADULTS );
	var lNmbInfants = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_INFANTS );	
	if( parseInt(lNmbInfants,10) > parseInt(lNmbAdults,10) ) { 
		WDSCommon.updateTag( document.CommonSearchForm, 'NUMBER_OF_INFANTS', lNmbAdults );
	}	
	/* Verify number of adults + number of children <= 9 */
	var lNmbChildren = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_CHILDREN );
	if( parseInt(lNmbAdults,10) + parseInt(lNmbChildren,10) > 9 ) {
		/* remove some children */
		var lMaxChildren = 9 - parseInt(lNmbAdults,10);
		WDSCommon.updateTag( document.CommonSearchForm, 'NUMBER_OF_CHILDREN', ""+lMaxChildren );
	}
}


/* CommonSearchForm_changeChildren */
CommonSearchForm.prototype.changeChildren = function () {
	/* Verify number of adults + number of children <= 9 */
	var lNmbChildren = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_CHILDREN );
	var lNmbAdults = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_ADULTS );
	if( parseInt(lNmbAdults,10) + parseInt(lNmbChildren,10) > 9 ) {
		/* remove some adults */
		var lMaxAdults = 9 - parseInt(lNmbChildren,10);
		WDSCommon.updateTag( document.CommonSearchForm, 'NUMBER_OF_ADULTS', ""+lMaxAdults );
		
		/* If the number of adults has changed, maybe we need to reduce the number of infants. */
		this.changeAdults();
	}
}


/* CommonSearchForm_changeInfants */
CommonSearchForm.prototype.changeInfants = function () {	
	/* Verify number of adults >= Number of infants  */
	var lNmbAdults = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_ADULTS );
	var lNmbInfants = WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_INFANTS );	
	if( parseInt(lNmbInfants,10) > parseInt(lNmbAdults,10) ) { 
		WDSCommon.updateTag( document.CommonSearchForm, 'NUMBER_OF_ADULTS', lNmbInfants );

		/* If the number of adults has changed, maybe we need to reduce the number of children	*/	
		this.changeAdults();
	}
}


/* CommonSearchForm_selectOneWayTrip */

CommonSearchForm.prototype.selectOneWayTrip = function () {
	document.getElementById('dateSelectInbound').style.display = 'none';
}


/* CommonSearchForm_selectRoundTrip */
CommonSearchForm.prototype.selectRoundTrip = function () {
	document.getElementById('dateSelectInbound').style.display = '';
}



/* CommonSearchForm_preCheck */
CommonSearchForm.prototype.preCheck = function () {
	/* Check that both airports are filled	*/	
	AirportListComplete.associate('CommonSearchForm', 'B_LOCATION_INPUT', 'B_LOCATION');
	AirportListComplete.associate('CommonSearchForm', 'E_LOCATION_INPUT', 'E_LOCATION');	
	WDSCheck.checkMandatory(document.CommonSearchForm.B_LOCATION, WDSMessage.getMessage("FMPA.js.BLocationMandatory") );
	WDSCheck.checkMandatory(document.CommonSearchForm.E_LOCATION, WDSMessage.getMessage("FMPA.js.ELocationMandatory") );
	
	/* Check that number of childs + number of adults < 10 */
	if( WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_INFANTS ) > WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_ADULTS ) ) {
		WDSError.add (WDSMessage.getMessage ("FMPA.js.NumberOfPassengerTooHigh"));
	}

	/* Select the correct form to fill */
	var lSearchType = WDSCommon.getTagValue( document.CommonSearchForm.SearchType );
	if( lSearchType == 'FLEX' || lSearchType == 'NOFLEX')  {
		this.fillForm( document.FARE_DRIVEN_FORM );
		this.fillFormSpecificFareDriven();
	} else {
		this.fillForm( document.SCHEDULE_DRIVEN_FORM );
		this.fillFormSpecificScheduleDriven();
	}
}


/* CommonSearchForm_preCheckBKGD  */
CommonSearchForm.prototype.preCheckBKGD = function () {
	WDSCheck.checkMandatory(document.ViewBookingForm.REC_LOC, WDSMessage.getMessage("FMPA.js.RECLOCMandatory") );
	WDSCheck.checkMandatory(document.ViewBookingForm.LAST_NAME, WDSMessage.getMessage("FMPA.js.LastNameMandatory") );
	
	WDSCommon.updateTag( document.SUBMIT_TO_BKGD_FORM, "DIRECT_RETRIEVE_LASTNAME", WDSCommon.getTagValue( document.ViewBookingForm.LAST_NAME ) );					
	WDSCommon.updateTag( document.SUBMIT_TO_BKGD_FORM, "REC_LOC", WDSCommon.getTagValue( document.ViewBookingForm.REC_LOC ) );							
}


/* CommonSearchForm_fillForm  */

CommonSearchForm.prototype.fillForm = function (iForm) {
	/* Store common flight information */
	WDSCommon.updateTag( iForm, "TRIP_TYPE", WDSCommon.getTagValue( document.CommonSearchForm.TRIP_TYPE ) );	
	WDSCommon.updateTag( iForm, "B_DATE_1", OutboundCalendar.getFormattedDate("YYYYmmDDHHMM").substr(0,8) + "0000" );
	
	WDSCommon.updateTag( iForm, "B_LOCATION_1", WDSCommon.getTagValue( document.CommonSearchForm.B_LOCATION ) );
	WDSCommon.updateTag( iForm, "E_LOCATION_1", WDSCommon.getTagValue( document.CommonSearchForm.E_LOCATION ) );
	if( WDSCommon.getTagValue( document.CommonSearchForm.TRIP_TYPE ) == 'R' ) {
		WDSCommon.updateTag( iForm, "B_DATE_2", InboundCalendar.getFormattedDate("YYYYmmDDHHMM").substr(0,8) + "0100" );		
		WDSCommon.updateTag( iForm, "B_LOCATION_2", WDSCommon.getTagValue( document.CommonSearchForm.E_LOCATION ) );
		WDSCommon.updateTag( iForm, "E_LOCATION_2", WDSCommon.getTagValue( document.CommonSearchForm.B_LOCATION ) );	
	}

	/* Store adults and infants */
	var lNumberOfInfants = parseInt( WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_INFANTS ), 10 );
	var lNumberOfAdults = parseInt( WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_ADULTS ), 10 );
	for( var i=1; i <= lNumberOfAdults; i++ ) {
		WDSCommon.updateTag( iForm, "TRAVELLER_TYPE_" + i, "ADT" );
		if( i <= lNumberOfInfants ) {
			WDSCommon.updateTag( iForm, "HAS_INFANT_" + i, "TRUE" );
		}
	}

	/* Store children */
	var lNumberOfChildren = parseInt( WDSCommon.getTagValue( document.CommonSearchForm.NUMBER_OF_CHILDREN ), 10 );
	for( var i=lNumberOfAdults+1; i <= lNumberOfAdults+lNumberOfChildren; i++ ) {
		WDSCommon.updateTag( iForm, "TRAVELLER_TYPE_" + i, "CHD" );
	}

}


/* CommonSearchForm_fillFormSpecificFareDriven */ 
CommonSearchForm.prototype.fillFormSpecificFareDriven = function () {	
	if( WDSCommon.getTagValue( document.CommonSearchForm.SearchType ) == 'FLEX' ) {
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "DATE_RANGE_VALUE_1", "3" );
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "DATE_RANGE_VALUE_2", "3" );		
	} else {
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "DATE_RANGE_VALUE_1", "0" );
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "DATE_RANGE_VALUE_2", "0" );				
	}		
	WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "COMMERCIAL_FARE_FAMILY_1", this.getAssociatedFamily( WDSCommon.getTagValue( document.CommonSearchForm.CABIN ) ) );						
	if( WDSCommon.getTagValue( document.CommonSearchForm.TRIP_TYPE ) == 'R' ) {
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "PRICING_TYPE", "I" );
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "DISPLAY_TYPE", "1" );				
	} else {
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "PRICING_TYPE", "C" );
		WDSCommon.updateTag( document.FARE_DRIVEN_FORM, "DISPLAY_TYPE", "2" );				
	}	
}


/* CommonSearchForm_fillFormSpecificScheduleDriven */ 
CommonSearchForm.prototype.fillFormSpecificScheduleDriven = function () {
	WDSCommon.updateTag( document.SCHEDULE_DRIVEN_FORM, "CABIN", WDSCommon.getTagValue( document.CommonSearchForm.CABIN ) );	
}
