﻿var QuickQuoteController=Class.create(
{
	initialize:function(Observables,SearchButton,NotifyDiv,ResponseDiv,FormDiv,MsgContainer) //Notify Div
	{
		this.url=location.href;
		this.registerObservables(Observables);
		this.SearchButton=$(SearchButton);
		this.displayDivs=null;
		this.isDisabled=false;
		this.REQUESTTYPE={OUTWARD:1,RETURN:2};
		this.STEPS={VOYAGEROUTE:1,VOYAGETYPE:2,OUTDATE:3,RETDATE:5};
		this.disableControls();
		this.VoyageRoute.enable();
		this.hasReturn=false;
		if (NotifyDiv !=null)
		{
			this.NotifyDiv=NotifyDiv;
			Element.hide($(this.NotifyDiv));
		}
		this.ResponseDiv=$(ResponseDiv);
		Element.hide($(this.ResponseDiv));
		this.FormDiv=$(FormDiv);
		this.MsgController=new MessageController(MsgContainer);
	},//end initialize
	disableControls: function()
	{
		this.VoyageRoute.disable();
		this.VoyageType.disable();
		this.DateOut.disable();
		this.DateRet.disable();
		this.NumPassSel.disableControls();
		this.VehSel.disable();
		this.disableButton();
	},//end disableControls
	enableControls: function()
	{
		this.VoyageRoute.enable();
		this.VoyageType.enable();
		this.DateOut.enable();
		this.DateRet.enable();
		this.NumPassSel.enableControls();
		this.VehSel.enable();
		this.enableButton();
	},//end enableControls
	disableButton: function()
	{
		//Element.toggleClassName($(this.SearchButton), 'disable');
		$(this.SearchButton).removeClassName('button');
		$(this.SearchButton).addClassName('disable');
		Form.Element.disable(this.SearchButton);
	},
	enableButton: function()
	{
		$(this.SearchButton).removeClassName('disable'); //$ is needed due to ie 6 bug - in theory not needed
		$(this.SearchButton).addClassName('button')
		Form.Element.enable(this.SearchButton);
	},
	registerObservables: function(Observables)
	{
		if (Observables.size != undefined)
		{
			Observables.each(function(property)
			{
				this[property.key]=property.value;
				this[property.key].setObserver(this);
			},this);
			this.Observables=Observables;
		}
		else
			this.Observables=null;
	},//register Observables
	setPortIds: function()
	{
		var portSelection=this.VoyageRoute.getValue().split("-");
		this.fromPortId=portSelection[0];
		this.toPortId=portSelection[1];
	},//end setPortIds
	RaiseStartAgain: function()
	{
		this.ResponseDiv.innerHTML="&nbsp;";
		Element.hide(this.ResponseDiv);
		Element.show(this.FormDiv);
		this.MsgController.ClearServerMessageContainer();
		return false;
	},
	setObservableEvent: function(sender,args)
	{
		if (sender===this.VoyageRoute)
		{
			if (this.VoyageRoute.getIndex()<1)
			{
				this.VoyageType.resetCombo(); //reset it
				this.VoyageType.disable();
				this.DateOut.resetAndDisable();
				this.DateRet.resetAndDisable();
				this.NumPassSel.resetAndDisable();
				this.VehSel.disable();
				this.VehSel.resetCombo();
			}
			else
			{
				this.setPortIds();
				this.VoyageType.enable();
			}
		}
		else if (sender===this.VoyageType)
		{
			if (this.VoyageType.getIndex()<1)
			{
				this.DateOut.resetAndDisable();
				this.DateRet.resetAndDisable();
				this.NumPassSel.resetAndDisable();
				this.VehSel.resetCombo();
				this.VehSel.disable();
			}
			else
			{
				if (this.VoyageType.getValue().charAt(1)=="S")
				{
					this.hasReturn=true;
					this.DateRet.resetCombos();
				}
				else
				{
					this.hasReturn=false;
					this.DateRet.resetCombos();
					this.DateRet.disable();
				}
				this.DateOut.enable();
				this.DateOut.resetCombos();
				this.NumPassSel.resetAndDisable();
				this.VehSel.resetCombo();
				this.VehSel.disable();
			}
		}
		else if (sender===this.DateOut)
		{
			this.DateRet.resetCombos(); //Always clear it out - u change u clear :)
			clearSelectButTitle(this.DateRet.cmbMonthYear); //clear it out
			if (this.hasReturn && this.DateOut.hasDates())
			{
				this.DateRet.dtStartDate=this.DateOut.getTripDateAsDate(); //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();
				this.NumPassSel.resetAndDisable(); //not needed probably coz the clear will fire an event
				this.VehSel.resetCombo();
				this.VehSel.disable();
			}
			else
			{
				this.DateRet.disable();
				if (this.DateOut.hasDates())
					this.NumPassSel.enableControls();
				else
				{
					this.NumPassSel.resetAndDisable();
					this.VehSel.resetCombo();
					this.VehSel.disable();
				}
			}
		}
		else if (sender===this.DateRet)
		{
			if (this.DateRet.hasDates())
				this.NumPassSel.enableControls();
			else
			{
				this.NumPassSel.resetAndDisable();
				this.VehSel.resetCombo();
				this.VehSel.disable();
			}
		}
		else if (sender===this.NumPassSel)
		{
			var numPass=args[0];
			if (numPass>this.NumPassSel.MaxPassengers)
			{
				this.VehSel.disable();
				this.VehSel.resetCombo();
				this.disableButton();
				alert(this.NumPassSel.MessageExceedsMax);
			}
			else if(numPass<1)
			{
				this.VehSel.resetCombo();
				this.VehSel.disable();
				this.disableButton();
			}
			else
			{
				this.VehSel.resetCombo();
				this.VehSel.enable();
				this.enableButton();
			}
		}
		else if (sender===this.SearchButton)
		{
			this.setRequest();
		}
	},//end setObservableEvent
	setRequest: function()
	{
		try
		{
			this.setPortIds();
			if (this.canRequest())
			{
				var div=this.ResponseDiv;
				Element.show(div);
				Element.show($(this.NotifyDiv));
				Element.hide($(this.FormDiv));
				var NotifyMsg=$(this.NotifyDiv);
				new Ajax.Updater(div, this.url,
				{
					method: 'post',
					evalScripts: true,
					parameters:
					{
						ajaxLoad: 		REQUESTTYPE['QUICKQUOTE'],
						voyageType:		this.VoyageType.getValue(),
						tripDateOut: 	this.DateOut.getTripDate(),
						tripDateRet: 	(this.hasReturn ? this.DateRet.getTripDate() : -1),
						portIdOutFrom: 	this.fromPortId,
						portIdOutTo: 	this.toPortId,
						PassengerTotal:	this.NumPassSel.getTotalCount(),
						NumSeniors:		this.NumPassSel.NumSeniors.getValue(),
						NumAdults:		this.NumPassSel.NumAdults.getValue(),
						NumYouths:		this.NumPassSel.NumYouths.getValue(),
						NumChildren:	this.NumPassSel.NumChildren.getValue(),
						NumInfants:		this.NumPassSel.NumInfants.getValue(),
						VehType:		this.VehSel.getValue()
					},
					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
	canRequest: function()
	{
		//A catalogue of errors should be built up
		//we should never get to this stage but...
		var blnTest=true;
		if (this.VoyageRoute.getIndex()<1)
			blnTest=false;
		if (this.VoyageType.getIndex()<1)
			blnTest=false;
		if (this.hasReturn)
			if (!this.DateOut.hasDates() || !this.DateRet.hasDates())
				blnTest=false;
		else if (!this.DateOut.hasDates())
			blnTest=false;

		if (!this.NumPassSel.isValid())
			blnTest=false;
		return blnTest;
	}//end canRequest
});//end QuickQuoteController

var NumPassSelectionFull=Class.create(
{
	initialize: function(ControlHierarchy,Controls,MessageExceedsMax,MaxPass)
	{
		this.registerControls(ControlHierarchy,Controls);
		this.observer=null;
		this.MaxPassengers=MaxPass;
		this.MessageExceedsMax=MessageExceedsMax;
	},//end initialize
	setObserver: function(observer)
	{
		this.observer=observer;
	},//end setObserver
	getTotalCount: function()
	{
		//remember a select has the value as string so a cast is needed coz + will just concat
		var total=new Number(this.NumSeniors.getValue()) + new Number(this.NumAdults.getValue()) + new Number(this.NumYouths.getValue()) + new Number(this.NumChildren.getValue()) + new Number(this.NumInfants.getValue());
		return total;
	},//end getTotalCount
	isValid: function()
	{
		return (this.getTotalCount()<=this.MaxPassengers && this.getTotalCount()>0);
	},//end isValid
	OnChange: function ()
	{
		this.observer.setObservableEvent(this,[this.getTotalCount()]);
	}, //end OnChange
	registerControls: function(ControlHierarchy,Controls)
	{
		if (Controls.size != undefined)
		{
			Controls.each(function(property)
			{
				this[property.key]=new ObservableCombo($(ControlHierarchy + property.value),0);
				//alert('Registered ' + property.key + ' As ' + this[property.key]);
				this[property.key].disable();
			},this);
		}
	},//Register Controls
	disableControls: function()
	{
		this.NumSeniors.disable();
		this.NumAdults.disable();
		this.NumYouths.disable();
		this.NumChildren.disable();
		this.NumInfants.disable();
	},
	enableControls: function()
	{
		this.NumSeniors.enable();
		this.NumAdults.enable();
		this.NumYouths.enable();
		this.NumChildren.enable();
		this.NumInfants.enable();
	},
	resetCombos: function()
	{
		this.NumSeniors.resetCombo();
		this.NumAdults.resetCombo();
		this.NumYouths.resetCombo();
		this.NumChildren.resetCombo();
		this.NumInfants.resetCombo();
	},
	resetAndDisable: function()
	{
		this.resetCombos();
		this.disableControls();
	}
});//end NumPassSelectionFull

