/*
	Represents the logic and control of the Bookings numbers tab.
	Mod Log:	080802 	[DA]	Commented out references to the Hvy Vehicles as requested by client
				080802	[DA]	Added Nav Buttons changes
*/
var NumbersController=Class.create(BookingsTab,
{
		initialize:function($super,Containers,Observables,Buttons,NotifyDiv,MsgController,AjaxCallDiv,TabMapper,isExpedia)
		{
			$super(TABS['NUMBERS'],Containers,Observables,Buttons,NotifyDiv,MsgController,TabMapper);
			this.STEPS={TRAVELCLASS:1,PASSENGERS:2,VEHICLES:3};
			this.REQUESTTYPE={AVAILABILITY:1, TEMPBOOK:2};
			this.MESSAGES={NUMPASSNOTVALID:1101, NUMVEHNOTVALID:1102};
			this.HideNodes=$(Containers);
			this.AjaxCallDiv=$(AjaxCallDiv);
			this.disableControls();//just in case but they should be disabled
			this.BlockBook=false;
			this.ButtonColl.enableButtons(TabMovDir['BCK']); //enable the previous buttons
			this.isExpedia=isExpedia;
		},// end initialize
		HasVehicles: function()
		{
			return (this.NumVehs.getTotalPowerVeh()>0);
		},
		ValidateVehs: function()
		{
			return (this.NumVehs.getTotalPowerVeh()<=this.NumPass.getValue())
		},
		ValidatePass: function()
		{
			return (this.NumPass.getValue()>0);
		},
		disableControls: function($super)
		{
			$super();
			this.TravelClass.disable();
			this.NumPass.disable();
			this.NumVehs.disable();
		},
		enableControls: function($super)
		{
			$super();
			this.TravelClass.enable();
			this.NumPass.enable();
			if (this.TabMapper.RouteTab.HasCoachParts() || this.TabMapper.RouteTab.IsExcursion)
				this.NumVehs.disable();
			else
				this.NumVehs.enable();
		},
		canRequest: function(step)
		{
			return (this.TravelClass.getIndex()>0 && this.ValidatePass());
		},//end canRequest
		setObservableEvent: function($super,sender,args)
		{
			$super(sender);
			this.MsgController.ClearServerMessageContainer(); //081107 - [DA] - This was missing
			this.MsgController.ClearLastMessage();
			if (sender===this.TravelClass)
			{
				if (this.TravelClass.getIndex()<1)
					this.resetControls(this.STEPS['TRAVELCLASS']);
				else if (this.ValidatePass() && this.ValidateVehiclesAndAction()) //Need a check on number of passengers > 0
					this.setRequest(this.REQUESTTYPE['AVAILABILITY']);
					//No need to show the message here
				ComboHandle(this.TravelClass.getValue(),this.HideNodes,this.TravelClass.indexToShow);
			}
			else if (sender===this.NumPass)
			{
				if (this.ValidatePass() && this.ValidateVehiclesAndAction()) //Need a check on number of passengers > 0
					this.setRequest(this.REQUESTTYPE['AVAILABILITY']);
				else
				{
					this.resetControls(this.STEPS['PASSENGERS']);
					this.MsgController.ShowLocalMsg(this.MESSAGES['NUMPASSNOTVALID']);
				}
				ComboHandle(this.NumPass.getValue(),this.HideNodes,this.NumPass.indexToShow);
			}
			else if (sender===this.NumVehs.NumCars)
			{
				this.ValidateVehiclesAndAction();
			}
			/*else if (sender===this.NumVehs.NumHvyVeh)
			{
				this.ValidateVehiclesAndAction();
			}*/ //Removed @ client's request
			else if (sender===this.NumVehs.NumMB)
			{
				this.ValidateVehiclesAndAction();
			}
			else if (sender===this.ButtonColl)
			{
				switch (args[0])
				{
					case (TabMovDir['FWD']):
					{	if (this.ValidateVehiclesAndAction())
							this.setRequest(this.REQUESTTYPE['TEMPBOOK']); //Move is done by the request itself - change this to be consistent
						break;
					}
					case (TabMovDir['BCK']):
					{
						this.disableControls(); //Disable them all
						this.ButtonColl.MoveToTab(TabMovDir['BCK']);
						this.TabMapper.RouteTab.enableControls();
						this.resetControls(this.STEPS['TRAVELCLASS']);
						break;
					}
				}
			}
		}, //end setObservableEvent
	ValidateVehiclesAndAction: function() //Show message or set up request
	{
		var toReturn=false;
		if (this.ValidateVehs())
		{
			this.ButtonColl.enableButtons();
			toReturn=true;
		}
		else
		{
			this.MsgController.ShowLocalMsg(this.MESSAGES['NUMVEHNOTVALID']);
		}
		return toReturn;
	},//ValidateVehiclesAndAction - What a stupid fucking name!!
	setRequest: function(requestType)
	{
		try
		{
			if (this.canRequest())
			{
				Element.show($(this.NotifyDiv));
				this.MsgController.ClearServerMessages();
				this.MsgController.ClearLastMessage(); //Clears any local messages
				var blnOpenEnded=(this.TabMapper.RouteTab.IsOpenEnded() ? 1 : 0); //iif
				switch (requestType)
				{
					case this.REQUESTTYPE['AVAILABILITY']:
					{
						this.AvailabiltyRequest(blnOpenEnded,$(this.NotifyDiv));
						break;
					}
					case this.REQUESTTYPE['TEMPBOOK']:
					{
						this.TempBookRequest(blnOpenEnded,$(this.NotifyDiv));
						break;
					}
				}
			}
		}
		catch (err)
		{
			alert(err)
			return false;
		}
	}, //end setRequest
	AvailabiltyRequest: function(IsOpenEnded,NotifyContainer)
	{
		new Ajax.Updater(this.AjaxCallDiv,this.url,
		{
			method: 'post',
			evalScripts: true,
			parameters:
			{
				ajaxLoad: REQUESTTYPE['AVAILABILTY'],
				VoyIdOut:	this.TabMapper.RouteTab.outVoyage.getVoyageId(),
				VoyIdRet: this.TabMapper.RouteTab.retVoyage.getVoyageId(),
				VoyPartOut: this.TabMapper.RouteTab.outVoyage.getVoyagePartId(),
				VoyPartRet: this.TabMapper.RouteTab.retVoyage.getVoyagePartId(),
				OpenEnded: IsOpenEnded,
				TravelClass: this.TravelClass.getValue(),
				NumPass: this.NumPass.getValue(),
				NumCars: this.NumVehs.getCars(),
				//NumHvyVeh: this.NumVehs.getHvyVehs(), //080802 - Removed @ client's request
				ExcursionId:this.TabMapper.RouteTab.ExcursionId,
				NumMB: this.NumVehs.getMB()
			},
			onComplete: function(transport) //note this keyword will not work in here
			{
				Element.hide(NotifyContainer);
			}
		});
	},
	TempBookRequest: function(IsOpenEnded,NotifyContainer)
		{
			var DateRet;
			if (this.TabMapper.RouteTab.IsExcursion)
				DateRet=this.TabMapper.RouteTab.DateOut.getTripDate();
			else
				DateRet=(this.TabMapper.RouteTab.DateRet.hasDates() ? this.TabMapper.RouteTab.DateRet.getTripDate():-1);

			new Ajax.Updater(this.AjaxCallDiv,this.url,
			{
				method: 'post',
				evalScripts: true,
				parameters:
				{
					ajaxLoad: REQUESTTYPE['TEMPBOOK'],
					VoyIdOut:	this.TabMapper.RouteTab.outVoyage.getVoyageId(),
					VoyIdRet: 	this.TabMapper.RouteTab.retVoyage.getVoyageId(),
					VoyPartOut: this.TabMapper.RouteTab.outVoyage.getVoyagePartId(),
					VoyPartRet: this.TabMapper.RouteTab.retVoyage.getVoyagePartId(),
					DateOut: 	this.TabMapper.RouteTab.DateOut.getTripDate(),
					DateRet: 	DateRet, //if it has date otherwise -1
					OpenEnded: 	IsOpenEnded,
					TravelClass:this.TravelClass.getValue(),
					NumPass: 	this.NumPass.getValue(),
					NumCars: 	this.NumVehs.getCars(),
					//NumHvyVeh: this.NumVehs.getHvyVehs(), //080802 - Removed @ client's request
					NumMB: 		this.NumVehs.getMB(),
					BlockBook:	this.BlockBook,
					ExcursionId:this.TabMapper.RouteTab.ExcursionId,
					isExpedia: 	this.isExpedia
				},
				onComplete: function(transport) //note this keyword will not work in here
				{
					Element.hide(NotifyContainer);
				}
			});
		},
		CheckStatus: function(IsBlock) //Checks the status of the control, if ok then show the buttons
		{
			if (this.MsgController.GetStatus())
			{
				this.ButtonColl.enableButtons();
				this.BlockBook=IsBlock;
			}
		},
		resetControls: function (step)
		{
			this.ButtonColl.disableButtons(TabMovDir['FWD']);
			if (step=='undefined' || step==null)
				step=this.STEPS['TRAVELCLASS'];

			switch (step)
			{
				case this.STEPS['TRAVELCLASS']:
				{
					this.TravelClass.resetCombo(); //reset it
					this.NumPass.resetCombo();
					this.NumVehs.resetCombos();
					break;
				}
				case this.STEPS['PASSENGERS']:
				{
					this.NumPass.resetCombo();
					this.NumVehs.resetCombos();
					break;
				}
			}//end switch - reset controls
		}//end resetControls
}); //end Numbers Tab