function ServiceBooking() {
	this.serviceForm = null;
	this.motOnlyPrice = 0;
	this.serviceTypes = new Array();
	this.servicePrices = new Array();
	this.serviceAndMotPrices = new Array();
	this.serviceCal = false;

	this.SetServiceBookingForm = SetServiceBookingForm;
	this.AddServiceType = AddServiceType;
	this.AddServicePrice = AddServicePrice;
	this.SelectServiceType = SelectServiceType;
	this.SelectServiceTime = SelectServiceTime;
	this.ShowBookingClick = ShowBookingClick;
	this.CheckBookingForm = CheckBookingForm;
	this.CalcServicePrice = CalcServicePrice;
	this.InitServiceCalendar = InitServiceCalendar;
}

function SetServiceBookingForm(formObject) {
	this.serviceForm = formObject;
}

function AddServiceType(servicetypeid, serviceType) {
	this.serviceTypes[servicetypeid] = serviceType;
}

function AddServicePrice(servicetypeid, serviceType, motOnly, servicePrice, withMotPrice) {
	if (motOnly == 1) {
		this.serviceTypes[0] = serviceType;
		this.motOnlyPrice = servicePrice;
	} else {
		this.serviceTypes[servicetypeid] = serviceType;
		this.servicePrices[servicetypeid] = servicePrice;
		this.serviceAndMotPrices[servicetypeid] = withMotPrice;
	}
}

function SelectServiceType(serviceType, withMot, quotedPrice, motOnly) {
	this.serviceForm.service_type.value = serviceType;
	this.serviceForm.withmot.value = withMot;
	this.serviceForm.price_quoted.value = quotedPrice;
	this.serviceForm.mot_only.value = motOnly;
	this.serviceForm.submit();
}

function SelectServiceTime(serviceTime) {
	this.serviceForm.service_time.value = serviceTime;
	this.serviceForm.submit();
}

function ShowBookingClick(swfFile) {
	var clickSwf = new SWFObject(swfFile, "ServiceClick"+this.gadgetId, 200, 30, "6,0,0,0", "#ffffff");
	clickSwf.addParam("wmode", "transparent");
	clickSwf.write("ServiceClick"+this.gadgetId);
}

function CheckBookingForm(bookingPhase) {
	if (bookingPhase == 'reg') {
		var missingFields = '';
		if (this.serviceForm.regno.value == '') missingFields += "\nYour car registration number";
		if (this.serviceForm.door.value == '') missingFields += "\nDoor Number";
		if (this.serviceForm.postcode.value == '') missingFields += "\nPostcode";
		if (missingFields != '') {
			alert("Please enter the following information:\n"+missingFields);
			return;
		}
	} else if (bookingPhase == 'confirm') {
		var missingFields = '';
		if (this.serviceForm.customername.value == '') missingFields += "\nYour Name";
		if (this.serviceForm.telephone.value == '' && this.serviceForm.email.value == '')  missingFields += "\nTelephone Number or Email Address";
		if (missingFields != '') {
			alert("Please enter the following information:\n"+missingFields);
			return;
		}
	}
	this.serviceForm.submit();
}

function CalcServicePrice(hidePrices) {
	var quotedPrice = 0;
	var servicePrice = 0;
	var servicePriceWithMot = 0;
	var serviceTypeId = this.serviceForm.servicetypeid.value;
	
	if (serviceTypeId > 0) {
		servicePrice = this.servicePrices[serviceTypeId];
		servicePriceWithMot = this.serviceAndMotPrices[serviceTypeId];
	}
	
	if ((serviceTypeId == 0 && this.motOnlyPrice > 0) || servicePriceWithMot > 0 || hidePrices == 1) {
		document.getElementById("motRow"+this.gadgetId).style.visibility = 'visible';
	} else {
		document.getElementById("motRow"+this.gadgetId).style.visibility = 'hidden';
		this.serviceForm.inc_mot.selectedIndex = 0;
	}
	var withMot = parseInt(this.serviceForm.inc_mot.value, 10);
		
	if (hidePrices == 1) {
		quotedPrice = 0;
	} else if (serviceTypeId > 0) {
		if (withMot) {
			quotedPrice = servicePriceWithMot;
		} else {
			quotedPrice = servicePrice;
		}
	} else {
		if (withMot) {
			quotedPrice = this.motOnlyPrice;
		}
	}
	
	if (quotedPrice > 0 || (hidePrices == 1 && (serviceTypeId > 0 || withMot))) {
		var costRowVisibility = (hidePrices == 1) ? 'hidden' : 'visible';
		document.getElementById("serviceCostRow"+this.gadgetId).style.visibility = costRowVisibility;
		document.getElementById("serviceNotesRow"+this.gadgetId).style.visibility = 'visible';
		document.getElementById("serviceButtonRow"+this.gadgetId).style.visibility = 'visible';
		document.getElementById("serviceCostCell"+this.gadgetId).innerHTML = '&pound;'+quotedPrice.toFixed(2);
	} else {
		document.getElementById("serviceCostRow"+this.gadgetId).style.visibility = 'hidden';
		document.getElementById("serviceNotesRow"+this.gadgetId).style.visibility = 'hidden';
		document.getElementById("serviceButtonRow"+this.gadgetId).style.visibility = 'hidden';
		document.getElementById("serviceCostCell"+this.gadgetId).innerHTML = '';
	}
	
	var motOnly = (serviceTypeId == 0 && withMot == 1) ? 1 : 0;
	var serviceTypeDescription = this.serviceTypes[serviceTypeId];
	
	this.serviceForm.service_type.value = serviceTypeDescription;
	this.serviceForm.withmot.value = withMot;
	this.serviceForm.price_quoted.value = quotedPrice;
	this.serviceForm.mot_only.value = motOnly;
}

function InitServiceCalendar() {
	var serviceCal = new FlatCalendar();
	serviceCal.SetFlatCalendarForm(this.serviceForm);
	serviceCal.InitFlatCalendar('gridCalendar'+this.gadgetId);
}

