// JScript File
var RouteController=Class.create(BookingsTab,
{
	initialize:function($super,Containers,Observables,Buttons,NotifyDiv,MsgController,TabMapper,ExMaltaPorts,VoyageReturnLabel)
	{
		$super(TABS['ROUTE'],Containers,Observables,Buttons,NotifyDiv,MsgController,TabMapper);
		this.fromPortId=null;
		this.toPortId=null;
		this.outVoyage=new Voyage();
		this.retVoyage=new Voyage();
		this.hasReturn=false;
		this.STEPS={VOYAGEROUTE:1,VOYAGETYPE:2,OUTDATE:3,OUTTRIP:4,RETDATE:5,RETTRIP:6}; //Actually retdate is not quite right, its now retdate and retport depending on wheter ex-Malta or ex-Sicily
		this.REQUESTTYPE={OUTWARD:1,RETURN:2};
		this.HideNodes=$(Containers);
		this.IsExcursion=false;
		this.ExcursionId=0;
		this.ExMaltaPorts=ExMaltaPorts;
		this.ExMalta=false;
		this.VoyageReturnLabel=VoyageReturnLabel;
	},//end initialize
	SetExMalta: function(DepPort)
	{
		var blnTest=false;
		this.ExMaltaPorts.each(function(port)
		{
			if (port==DepPort)
			{
				blnTest=true;
				throw $break;
			}
		});
		this.ExMalta=blnTest;
	},//end SetExMalta
	HasCoachParts: function()
	{
		return (this.outVoyage.HasCoachParts || this.retVoyage.HasCoachParts);
	},//end HasCoachParts
	canRequest: function(step)
	{
		var blnCanRequest=false;
		if (step==undefined || step==null)
			step=0;
		if (this.VoyageRoute.getIndex()>0 && this.VoyageType.getIndex()>0 && this.DateOut.hasDates())
		{
			blnCanRequest=true;
		}
		if (step==this.STEPS['RETDATE'])
		{
			if (this.DateRet.hasDates())
				blnCanRequest=true;
			else
				blnCanRequest=false;
		}
		return blnCanRequest;
	},//end canRequest
	resetDivContent: function(div)
	{
		this.getContentDiv(this.getDiv(div)).innerHTML="";  //Clear the content if anything
	},//end resetDivContent
	disableControls: function($super)
	{
		$super();
		this.VoyageRoute.disable();
		this.VoyageType.disable();
		this.DateOut.disable();
		this.DateRet.disable();
		this.VoyageRouteReturn.disable();
	},//end disableControls
	enableControls: function($super)
	{
		$super();
		this.VoyageRoute.enable();
		this.VoyageType.enable();
		this.DateOut.enable();
		this.DateRet.enable();
		this.VoyageRouteReturn.enable();
	},//end enableControls
	IsOpenEnded: function()
	{
		return (this.VoyageType.getValue().charAt(0)=="3")	//Frigging magic number...fix this
	}, //End IsOpenEnded
	resetControls: function (step)
	{
		this.ButtonColl.disableButtons(); //just in case they're visible
		switch (step)
		{
			case this.STEPS['VOYAGEROUTE']:
			{
				this.VoyageType.resetCombo(); //reset it
				this.DateOut.resetCombos();
				this.outVoyage.clearVoyage();
				this.DateRet.resetCombos();
				this.resetDivContent(this.STEPS['OUTTRIP']);
				this.resetDivContent(this.STEPS['RETTRIP']); //just in case
				this.VoyageRouteReturn.resetCombo();
				this.ExMalta=false;
				break;
			}
			case this.STEPS['VOYAGETYPE']:
			{
				this.resetDivContent(this.STEPS['OUTTRIP']);
				this.resetDivContent(this.STEPS['RETTRIP']);
				this.DateOut.resetCombos();
				this.outVoyage.clearVoyage();
				this.DateRet.resetCombos();
				this.VoyageRouteReturn.resetCombo();
				break;
			}
			case this.STEPS['OUTDATE']:
			{
				this.resetDivContent(this.STEPS['OUTTRIP']); //clear out the trip date out
				this.resetDivContent(this.STEPS['RETTRIP']); //clear out the trip dare ret
				this.DateRet.resetCombos();
				this.VoyageRouteReturn.resetCombo();
				this.outVoyage.clearVoyage();
				this.retVoyage.clearVoyage();
				if (this.DateOut.cmbMonthYear.selectedIndex==0)
					HideSteps($(this.HideNodes).slice(this.STEPS['OUTDATE']),true);
				else
					HideSteps($(this.HideNodes).slice(this.STEPS['OUTTRIP']),false); //Hide the ones below
				break;
			}
			case this.STEPS['RETDATE']:
			{
				this.resetDivContent(this.STEPS['RETTRIP']);
				if (this.DateRet.cmbMonthYear.selectedIndex==0)
				{
					HideSteps($(this.HideNodes).slice(this.STEPS['RETDATE']),true);
					this.retVoyage.clearVoyage();
				}
				break;
			}
		}//end switch
	},//end resetControls
	setVoyageRouteReturn: function()
	{
		if (this.HasReturnRoutes())
		{
			Element.show($(this.VoyageRouteReturn.Combo));
			Element.show($(this.VoyageReturnLabel));
			this.VoyageRouteReturn.setValue(this.toPortId);
		}
		else
		{
			this.VoyageRouteReturn.resetCombo();
			Element.hide($(this.VoyageRouteReturn.Combo));
			Element.hide($(this.VoyageReturnLabel));
		}
		//this.VoyageRouteReturn //WTF?? 080802 - [DA] - Commented out as no sense!!
	},//end setVoyageRouteReturn
	HasReturnRoutes: function()
	{
		return (this.ExMalta && this.hasReturn); //Amended out, see below
		//return this.hasReturn; //080827 - [DA] - All return ports need to have this, client request
	},//end HasReturnRoutes
	setObservableEvent: function($super,sender,args)
	{
		$super(sender);
		if (sender===this.VoyageRoute)
		{
			if (this.VoyageRoute.getIndex()<1)
				this.resetControls(this.STEPS['VOYAGEROUTE']);
			else
			{
				this.setPortIds();
				this.setRequest(this.REQUESTTYPE['OUTWARD']);
			}
			ComboHandle(this.VoyageRoute.getValue(),this.HideNodes,this.VoyageRoute.indexToShow);
		}
		else if (sender===this.VoyageType)
		{
			if (this.VoyageType.getIndex()<1)
				this.resetControls(this.STEPS['VOYAGETYPE']);
			else
			{
				if (this.VoyageType.getValue().charAt(1)=="S")
				{
					this.hasReturn=true;
					this.ButtonColl.disableButtons();
				}
				else
				{
					this.hasReturn=false;
					this.DateRet.resetCombos();
					this.DateRet.disable();
					this.resetDivContent(this.STEPS['RETTRIP']);
					HideSteps($(this.HideNodes).slice(this.STEPS['OUTTRIP']),true);
						 //just in case they're visible
				}
				this.setVoyageRouteReturn();
				this.setRequest(this.REQUESTTYPE['OUTWARD']);
			}
			ComboHandle(this.VoyageType.getValue(),this.HideNodes,this.VoyageType.indexToShow);
		}
		else if (sender===this.DateOut)
		{
			if (this.canRequest())
				this.setRequest(this.REQUESTTYPE['OUTWARD']);
			else
			{
				this.resetControls(this.STEPS['OUTDATE']);
				this.setVoyageRouteReturn();
			}
		}
		else if (sender===this.VoyageRouteReturn)
		{
			if (this.canRequest(this.STEPS['RETDATE']))
				this.setRequest(this.REQUESTTYPE['RETURN']);
			else
				this.resetControls(this.STEPS['RETDATE']);
		}
		else if (sender===this.DateRet)
		{
			if (this.canRequest(this.STEPS['RETDATE']))
				this.setRequest(this.REQUESTTYPE['RETURN']);
			else
				this.resetControls(this.STEPS['RETDATE']);
		}
		else if (sender===this.ButtonColl)
		{
			this.disableControls();
			this.TabMapper.NumbersTab.enableControls(); //Enables all the controls and also checks according to the voyage wheter cars are allowed
			this.TabMapper.NumbersTab.ButtonColl.disableButtons(TabMovDir['FWD']);
			this.ButtonColl.MoveToTab(TabMovDir['FWD']);
		}
	},//end setObservableEvent
	SetUpFromExcursion: function(VoyageOutData,VoyageRetData)
	{
		this.outVoyage.setVoyage(VoyageOutData.get('VoyageInfo'),VoyageOutData.get('HasCoachParts'),VoyageOutData.get('TripDateOutDep'),VoyageOutData.get('TripDateOutArr'));
		this.retVoyage.setVoyage(VoyageRetData.get('VoyageInfo'),VoyageRetData.get('HasCoachParts'),VoyageRetData.get('TripDateOutDep'),VoyageRetData.get('TripDateOutArr'));
		this.ButtonColl.showAll(true);
	},
	setPortIds: function()
	{
		var WasExcursion=this.IsExcursion;
		var portSelection=this.VoyageRoute.getValue().split("-");
		this.fromPortId=portSelection[0];
		this.SetExMalta(this.fromPortId);
		this.toPortId=portSelection[1];
		this.ExcursionId=portSelection[2];
		this.IsExcursion=this.ExcursionId==0 ? false : true;
		if (this.IsExcursion)
		{
			var opts=$A(this.VoyageType.getOptions());
			var idx=-1;
			var blnFound=false;
			opts.each(function(itm)
			{
				idx++;
				if (itm.value.charAt(1)=="S")
				{
					blnFound=true;
					throw $break;
				}
			});
			if (!blnFound)
			{
				idx=0;
			}
			this.VoyageType.setIndex(idx);
			this.hasReturn=true;
			ComboHandle(this.VoyageType.getValue(),this.HideNodes,this.VoyageType.indexToShow);
			this.VoyageType.disable();
		}
		else
		{
			this.VoyageType.enable();
			if (WasExcursion)
			{
				this.VoyageType.setIndex(0);
				this.resetControls(this.STEPS['VOYAGETYPE']);
			}
		}
	},//end setPortIds
	setRequest: function(requestType)
	{
		try
		{
			this.setPortIds();
			if (this.canRequest())
			{
				var div=null;
				Element.show($(this.NotifyDiv));
				var NotifyMsg=$(this.NotifyDiv);
				var fromPortId=this.fromPortId;
				var toPortId=this.toPortId;
				//var viaPortId=-1;
				var selectedDate=this.DateOut.getTripDate();
				var timeValue=-1; // this is the arrival time of the trip, only used on return leg trips, in case of journeys on the same date
				this.ButtonColl.disableButtons(); //whenever there is a request it has to be disabled!!
				if (requestType==this.REQUESTTYPE['OUTWARD'])
				{
					div=this.getDiv(this.STEPS['OUTTRIP']);
				}
				else
				{
					div=this.getDiv(this.STEPS['RETTRIP']);
					//in this case we have to reverse the ports
					fromPortId=this.toPortId;
					toPortId=this.fromPortId;
					if (this.HasReturnRoutes())
						fromPortId=this.VoyageRouteReturn.getValue();
					selectedDate=this.DateRet.getTripDate();
					timeValue=this.outVoyage.ArrivalDate.getHours() + ':'+this.outVoyage.ArrivalDate.getMinutes();
				}
				new Ajax.Updater(this.getContentDiv(div), this.url,
				{
					method: 'post',
					evalScripts: true,
					parameters:
					{
						ajaxLoad:		REQUESTTYPE['DATE_SELECTION'],
						tripDateOut: 	this.DateOut.getTripDate(),
						tripDate: 		selectedDate,
						portIdOutFrom: 	fromPortId,
						portIdOutTo:	toPortId,
						divToShow: 		div,
						VoyageLeg: 		requestType,
						timeOfArr: 		timeValue, //This represents the time of arrival of the outrip;
						ExcursionId: 	this.ExcursionId
					},
					onComplete: function(transport) //note this keyword will not work in here
					{
						Element.hide(NotifyMsg);
					}
				});
			}
		}
		catch (err)
		{
			alert(err); //should not really do this...for debugging only but...
		}
	},//end setRequest
	CheckVoyageType: function(requestType)
	{
		var nextDiv=this.getDiv(this.STEPS['RETDATE']);
		switch (requestType)
		{
			case this.REQUESTTYPE['OUTWARD']:
			{
				if (this.hasReturn)
				{
					if (!Element.visible(nextDiv))
						Effect.BlindDown($(nextDiv));
				}
				else
				{
					this.DateRet.resetCombos();
					this.resetControls(this.STEPS['RETDATE']); //Reset everything below
					this.ButtonColl.showAll(true);
				}
				break;
			}
			case this.REQUESTTYPE['RETURN']:
			{
				this.ButtonColl.showAll(true);
				break;
			}
		}
	},//end CheckVoyageType
	VoyageSelected: function(objId,rowId,rowIndex,VoyageLeg,HasCoachParts)
	{
		var rows=$A($(rowId).getElementsByTagName('tr')).map(Element.extend);
		var rNum=0;
		rows.each(function(r)
		{
			if (++rNum==rowIndex)
			{
				if (!r.hasClassName('highlight'))
				r.addClassName('highlight');
			}
			else
			{
				if (r.hasClassName('highlight'))
					r.removeClassName('highlight');
			}
		});
		this.CheckVoyageType(VoyageLeg);
		if (VoyageLeg==this.REQUESTTYPE['OUTWARD'])
		{
			this.outVoyage.setVoyage($F(objId),HasCoachParts,$F('hdDepDateOut_'+rowIndex),$F('hdArrDateOut_'+rowIndex));

			if (this.hasReturn)
			{
				var dateParts=$F('hdArrDateOut_'+rowIndex).split("-");
				var time=dateParts[3].split(":");
				this.DateRet.dtStartDate=this.outVoyage.ArrivalDate; //Set the start date of return trip to the arrival date of out trip
				clearSelectButTitle(this.DateRet.cmbMonthYear); //clear it out
				this.DateRet.setUpMonthYears();
				this.DateRet.setUpDayDates();
				this.DateRet.enable();
			}
		}
		else
		{
			this.retVoyage.setVoyage($F(objId),HasCoachParts,$F('hdDepDateRet_'+rowIndex),$F('hdDepDateRet_'+rowIndex));
		}
	}//end VoyageSelected
});//end Route Controller

var Voyage=Class.create(
{
	initialize:function()
	{
		this.clearVoyage();
	},//end initialize
	getVoyageId: function()
	{
		return this.VoyageId;
	},//end getVoyageID
	getVoyagePartId: function()
	{
		return this.VoyagePartId;
	},//end getVoyagePartId
	setVoyage: function(VoyageString,HasCoachParts,TravelDateDep,TravelDateArr)
	{
		VoyageStringParts=VoyageString.split("-");
		this.VoyageId=VoyageStringParts[0];
		this.VoyagePartId=VoyageStringParts[1];
		this.HasCoachParts=HasCoachParts;
		this.TravelDate=TravelDate;
		this.DepartureDate=this.parseDateString(TravelDateDep);
		this.ArrivalDate=this.parseDateString(TravelDateArr);
	},//end setVoyage
    parseDateString: function(DateString)
    {
    	var dateParts=DateString.split("-");
    	var time=dateParts[3].split(":");
		return new Date(dateParts[0],dateParts[1]-1,dateParts[2],time[0],time[1]); //-1 coz its a month
    },
	clearVoyage: function()
	{
		this.VoyageId=-1;
		this.VoyagePartId=-1;
		this.HasCoachParts=false;
		this.DepartureDate=null;
		this.ArrivalDate=null;
	}//end clearVoyage
});//end Class Voyage