// js file for populating/controlling booking selector behaviour


var BookingSelector = {
	
	pickupSelectID : 'moturisPickupRegion',
	dropoffSelectID : 'moturisDropoffRegion',
	vehicleSelectID : 'moturisVehicle',
	locations:false,
	vehicles:false,
	pickupLocation:false,
	dropoffLocation:false,
	
	// a function to setup the booking selector (pass in locations and vehicles)
	setup:function(locations, vehicles) {
		this.locations = locations;
		this.vehicles = vehicles;
		this.populatePickup(locations);
		this.populateDropoff(locations);
		this.onChangePickupLocation();
		this.onChangeDropoffLocation();
	},

	// a function to populate the pickup dropdown
	populatePickup:function(locs) {
		this._populateLocationDD(this.pickupSelectID, locs);
	},
	
	// a function to populate the dropoff dropdown
	populateDropoff:function(locs) {
		this._populateLocationDD(this.dropoffSelectID, locs)
	},
	
	// a function to populate a dropdown with given id with given location data
	_populateLocationDD:function(id, locs) {
		if (!(dd=$(id))) return;
		dd.options.length = 0;
		for (lc in locs) {
			loc=locs[lc];
			dd.options[dd.length]=new Option(BookingSelector._locSelectorText(loc),loc.LocCode);
		}
	},
	
	// a function to return the text for a location selector option
	_locSelectorText:function(loc) {
		txt = loc.LocCode+" ("+loc.City+" "+loc.State+")";
		return (txt);
	},
	
	// a function to deal with a change of pickup location
	onChangePickupLocation:function() {
		dd=$(this.pickupSelectID);
		lc=dd.options[dd.selectedIndex].value;
		thisLocation=this.locations[lc];
		this.pickupLocation = thisLocation;
		fromDate=DBDate(thisLocation.ValidFrom);
		// Check if station opening in future
		nw = new Date();
		if (fromDate.getTime() > nw.getTime()) {
			alert("The station in "+thisLocation.LocName+" will open on "+thisLocation.ValidFrom+". Pick ups are possible from this date onwards. Thank you.");
			// see if the month we want is in the pickup month dropdown
			ym = ''+(fromDate.getFullYear())+(fromDate.getMonth()+1);
			found=-1;
			dd=$('moturisPickupMonth');
			i=0;
			$A(dd.options).each(
				(found>=0)?function(){}:function(opt) {
					if (opt.value==ym) {
						found=i;
						opt.selected=true;
						changed_pickupmonth();
					}
					i++;
				}
			);
			if (found>=0) {
				found=-1;
				dd=$('moturisPickupDay');
				i=0;
				$A(dd.options).each(
					(found>=0)?function(){}:function(opt) {
						if (opt.value==fromDate.getDate()) {
							found=i;
							opt.selected=true;
							changed_dropoffmonth(-1, -1);
						}
						i++;
					}
				);
			}
		}
		this.populateVehiclesForLocation(thisLocation);
	},
	
	// a function to deal with a change of dropoff location
	onChangeDropoffLocation:function() {
		dd=$(this.dropoffSelectID);
		lc=dd.options[dd.selectedIndex].value;
		thisLocation=this.locations[lc];
		this.dropOffLocation = thisLocation;
		fromDate=DBDate(thisLocation.ValidFrom);
		// Check if station opening in future
		nw = new Date();
		if (fromDate.getTime() > nw.getTime()) {
			alert("The station in "+thisLocation.LocName+" will open on "+thisLocation.ValidFrom+". Drop offs are possible from this date onwards. Thank you.");
			// see if the month we want is in the dropff month dropdown
			ym = ''+(fromDate.getFullYear())+(fromDate.getMonth()+1);
			found=-1;
			dd=$('moturisDropoffMonth');
			i=0;
			$A(dd.options).each(
				(found>=0)?function(){}:function(opt) {
					if (opt.value==ym) {
						found=i;
						opt.selected=true;
						changed_dropoffmonth(-1, -1);
					}
					i++;
				}
			);
			if (found>=0) {
				found=-1;
				dd=$('moturisDropoffDay');
				i=0;
				$A(dd.options).each(
					(found>=0)?function(){}:function(opt) {
						if (opt.value==fromDate.getDate()) {
							found=i;
							opt.selected=true;
						}
						i++;
					}
				);
			}
		}
	},
	
	// a function to populate the vehicles dropdown for given location
	populateVehiclesForLocation:function(loc) {
		dd=$(this.vehicleSelectID);
		dd.options.length = 0;
		
		// RVs
		if (loc.RVCodes.length) {
			loc.RVCodes.each(
				function(RVCode) {
					veh = BookingSelector.vehicles.RVs[RVCode];
					dd.options[dd.length]=new Option(veh.Model, veh.RVCode);
				}
			)
		}
		
		// separator
		if (loc.RVCodes.length && loc.MCCodes.length) dd.options[dd.length]=new Option("--------------------------------------------", "sep");
		
		// MCs
		if (loc.MCCodes.length) {
			loc.MCCodes.each(
				function(MCCode) {
					veh = BookingSelector.vehicles.MCs[MCCode];
					dd.options[dd.length]=new Option(veh.Model, veh.MCCode);
				}
			)
		}
	},
	
	// a function to check if it is ok to submit form
	okToSubmit:function() {
		pickDate=$('moturisPickupMonth').value + eval($('moturisPickupDay').value).toPaddedString(2);
		dropDate=$('moturisDropoffMonth').value + eval($('moturisDropoffDay').value).toPaddedString(2);
		pickBits=this.pickupLocation.ValidFrom.split("/").reverse(); pickBits[1]=eval(pickBits[1]).toPaddedString(2); pickBits[2]=eval(pickBits[2]).toPaddedString(2);
		dropBits=this.dropOffLocation.ValidFrom.split("/").reverse(); dropBits[1]=eval(dropBits[1]).toPaddedString(2); dropBits[2]=eval(dropBits[2]).toPaddedString(2);
		pickCheck=pickBits.join('');
		dropCheck=dropBits.join('');
		if (pickDate<pickCheck) {
			alert('The pick up date for this station must be on or after '+this.pickupLocation.ValidFrom+'. Thank you.');
			return false;
		}
		if (dropDate<dropCheck) {
			alert('The drop off date for this station must be on or after '+this.dropOffLocation.ValidFrom+'. Thank you.');
			return false;
		}
		return true;
	}

};


function DBDate(dt) {
	a=dt.split("/");
	d=new Date(a[2],a[1]-1,a[0]);
	return d;
}
