//Booking Request Form Code

//***********************************************************************************************
//* Detect browser information
//***********************************************************************************************
mySpec		= 	navigator.userAgent.toLowerCase();
myBrowser 	= 	navigator.appName.toLowerCase();
myVersion 	= 	parseInt(navigator.appVersion.toLowerCase());
var v	 	= 	navigator.appVersion;	
myOpr	 	= 	v.substr(0,4);

NS4 = 	(myBrowser.indexOf('netscape') != -1 && myVersion <=4) ? 1:0
NS6 = 	(myBrowser.indexOf('netscape') != -1 && myVersion >=5) ? 1:0
IE  = 	(myBrowser.indexOf('microsoft') != -1) ? 1:0
OPR6 = 	(myBrowser.indexOf('opera') != -1 && myOpr <= ('6.05')) ? 1:0

debug = 0;

myDate = new Date ();
getDate (myDate.getDate(),myDate.getMonth(),myDate.getYear(),myDate.getHours(),myDate.getMinutes());

//***********************************************************************************************
//* Define Variables and Arrays to use
//***********************************************************************************************
myLayer 		= "step";
mySteps 		= "4";
myCurrentStep 	= "0";
myConfirm 		= "cm";
myEmpty			=" is a required field entry";
myTemp 			= "";
maxDay			= 0;
oneWay			= false;

myPage0		= new Array	();
myPage1		= new Array ("myName","myAddress","myTel","myEmail");
myPage1txt	= new Array ("Name","Address","Telephone Number","Email Address");
myPage2		= new Array ("myPickupAdd","myPickupTime","myPickupDest");
myPage2txt 	= new Array ("Pick Up Address","Pick Up Time","Destination Address");
myPage3		= new Array ("myReturnPickup","myReturnTime","myReturnAdd");
myPage3txt	= new Array ("Return Pick up Address","Return Time","Return Address");
myPage4		= new Array ("myPassengers");
myPage4txt	= new Array ("No. of Passengers");

myFocus 	= new Array ("myName","myPickupAdd","myReturnPickup","myNotes");
//***********************************************************************************************
//* Start the booking process
//***********************************************************************************************
function openBooking()
{
	window.open('booking.htm','booking','width=600,height=540,status=yes');
}

function myBooking() {
	showLayer (myCurrentStep);
	setDates(myDate.getDate(),myDate.getMonth(),myDate.getYear());	
}
			
function myNextStep() {
	
	if (checkPage(myCurrentStep))	{
		if (myCurrentStep == mySteps) {
			sendForm();
		}
		else 
		{
			if (oneWay == true) {
				if (myCurrentStep == 2) {
					myCurrentStep ++
				}
			}
			myCurrentStep ++;
			showLayer (myCurrentStep);
			if (debug) { alert(eval(myFocus[myCurrentStep - 1]));}
			eval(eval(myFocus[myCurrentStep -1]) + '.focus()');
		}
	}
}

function sendForm()
{
	//hideAll();
/*	alert ("Sending Booking Request");
	if (IE)	{
		eval(myConfirm + '.style.visibility = "visible"');
	} else if (OPR6 || NS6) {
		eval('document.getElementById(' + "myConfirm" + ').style.visibility = "visible"');
	} else if (NS4) {
	}*/
	eval("document.forms['" + myForm + "'].submit()");
}
//***********************************************************************************************
//*  Hide all Layers
//***********************************************************************************************
function hideAll()
{
	for (i=0;i<=mySteps;i++) {
		if (IE)	{
			eval(myLayer + i + '.style.visibility = "hidden"');
		} else if (OPR6 || NS6) {
			eval('document.getElementById(' + "myLayer + i" + ').style.visibility = "hidden"');
		} else if (NS4) {
		}
	}
}

//***********************************************************************************************
//*  Show a specific layer
//***********************************************************************************************
function showLayer(i)
{
	hideAll();
	if (debug)	{ alert ("The layer to show is " + i); }
	if (IE)	{
		eval(myLayer + i + '.style.visibility = "visible"');
	} else if (OPR6 || NS6) {
		eval('document.getElementById(' + "myLayer + i" + ').style.visibility = "visible"');
	} else if (NS4) {
		eval(myLayer + i + '.style.visibility = "show"');
	}
}

//***********************************************************************************************
//*  Backward buttons
//***********************************************************************************************
function myBackward() {
	if (oneWay == true) {
		if (myCurrentStep == 4) {
			myCurrentStep --;
		}	
	}
	myCurrentStep --;
	showLayer (myCurrentStep);
}

//***********************************************************************************************
//* Form Functions - Check page for empty fields on submit
//***********************************************************************************************
function checkPage (page)
{
	if (page != 0)
	{
		//alert ("Checking page " + page);
		for (i=0;i<eval("myPage"+page).length;i++)
		{
			if (eval(eval(eval("myPage"+page)[i]) + '.value') == '')
			{
				alert (eval("myPage"+page+"txt")[i] + myEmpty);
				eval (eval(eval("myPage"+page)[i]) + '.focus()');
				return (false)
			}
		}
		return (true);
	} else {
		return (true);
	}
}

//***********************************************************************************************
//* Form Functions - CAuto fill fields, on check box controls and select boxes
//***********************************************************************************************
function myAutoFill(step, part, element)
{
	//*****************************************
	//* Auto fill elements on step 2
	//*****************************************
	if (step == 2) {
		if (part == 1) {
			if (eval(element + ".checked"))	{
				document.frmBooking.txtPickup.value = eval(myAddress + ".value");
			} else {
				document.frmBooking.txtPickup.value = "";
			}
		} else if (part == 2) {
			if (eval(element + ".selectedIndex") != 0) {
				document.frmBooking.txtDestination.value = eval(element + '.options['+eval(element + '.selectedIndex')+'].text');
			} else {
				document.frmBooking.txtDestination.value = "";
			}
		} else if (part == 3) {
			if (eval(element + ".checked"))	{
				//alert ("One Way");
				oneWay = true;
			} else {
				//alert ("Return");
				oneWay = false;
			}
		}	
	}
	//*****************************************
	//* Auto fill elements on step 3
	//*****************************************
	if (step == 3) {
		if (part == 1) {
			if (eval(element + ".checked"))	{
				document.frmBooking.txtReturn.value = eval(myPickupDest + ".value");
			} else {
				document.frmBooking.txtReturn.value = "";
			}
		} else if (part == 2) {
			if (eval(element + ".checked"))	{
				document.frmBooking.txtFinalDest.value = eval(myPickupAdd + ".value");
			} else {
				document.frmBooking.txtFinalDest.value = "";
			}
		} 
	}	
}

//***********************************************************************************************
//* Set the Date select boxes on the form checks that the dates are ok
//***********************************************************************************************
function setDates(day,month,year)
{
	if (NS6 || OPR6) {
		year += 1900;
	}
	if (Number(year < 2000)) {
		year += 1900;
	}
	
	document.frmBooking.pickupDay.value 	= day
	document.frmBooking.pickupMonth.value 	= month + 1;
	document.frmBooking.pickupYear.value 	= year;
	
	baseDay		= document.frmBooking.pickupDay.value;
	baseMonth 	= document.frmBooking.pickupMonth.value;
	baseYear	= document.frmBooking.pickupYear.value;
	
	document.frmBooking.returnDay.value 	= day;
	document.frmBooking.returnMonth.value 	= month +1;
	document.frmBooking.returnYear.value 	= year;
	
	targetDay 	= day;
	targetMonth = month + 1;
	targetYear 	= year;
}

function returnDates(day,month,year)
{	
	document.frmBooking.returnDay.value 	= day;	
	document.frmBooking.returnMonth.value 	= month;
	document.frmBooking.returnYear.value 	= year;
	
	targetDay 	= day;
	targetMonth = month;
	targetYear 	= year;
}

function checkDates (element,day,month,year)
{
//**********************************************************************
//* Outbound Journey Dates
//**********************************************************************
if (okDates(day,month,year)) {
//alert("Max Day: " + maxDay);
	if (element == 1) {
		//alert("Year: " + year);
		//alert("Base Year: " + baseYear);
		if (Number(year) < Number(baseYear) || Number(day) > Number(maxDay)) {
			alert ("Invalid Date Entered");
			setDates(myDate.getDate(),myDate.getMonth(),myDate.getYear());	
		} else if (Number(year) == Number(baseYear) || Number(day) > Number(maxDay)) {
			//alert("Month: " + month);
			//alert("Base Month: " + baseMonth);
			if (Number(month) < Number(baseMonth) || Number(day) > Number(maxDay)) {
				alert ("Invalid Date Entered");
				setDates(myDate.getDate(),myDate.getMonth(),myDate.getYear());	
			} else if (Number(month) == Number(baseMonth) || Number(day) > Number(maxDay)) {
				//alert("Day: " + day);
				//alert("Base Day: " + baseDay);
				if (Number(day) < Number(baseDay) || Number(day) > Number(maxDay)) {
					alert("Invalid Date Entered");
					setDates(myDate.getDate(),myDate.getMonth(),myDate.getYear());	
				} else {
					returnDates(day,month,year);
				}
			} else {
				returnDates(day,month,year);
			}
		} else {
			returnDates(day,month,year);
		}
	}
//**********************************************************************
//* Return Journey Dates
//**********************************************************************
	if (element == 2) {
		if (Number(year) < Number(targetYear) || Number(day) > Number(maxDay)) {
			alert ("Invalid Date Entered");
			returnDates(targetDay,targetMonth,targetYear);
		} else if (Number(year) == Number(targetYear) || Number(day) > Number(maxDay)) {
			if (Number(month) < Number(targetMonth) || Number(day) > Number(maxDay)) {
				alert ("Invalid Date Entered");
				returnDates(targetDay,targetMonth,targetYear);
			} else if (Number(month) == Number(targetMonth) || Number(day) > Number(maxDay)) {
				if (Number(day) < Number(targetDay) || Number(day) > Number(maxDay)) {
					alert("Invalid Date Entered");
					returnDates(targetDay,targetMonth,targetYear);
					} 
				} 
			} 
		}
	}
}	

//***********************************************************************************************
//* Form Functions - Verify Email is a global function for all.
//***********************************************************************************************
function verifyEmail(email) 
{
	if (eval(email + '.value') != "" && eval(email + '.value') != null)
	{
		var mailCool = eval(email + '.value.indexOf("@")');
	
		if ((mailCool) == -1)
		{
			alert ("You must enter a valid Email Address!");
			eval(email + '.focus()');
		}
	}
}

//*******************************************************************************
//* Define the Date Object and get the date and time formatted
//*******************************************************************************
function getDate(day,month,year,hours,minutes)
{
	myMonths 	= new Array ("Janurary","Februrary","March","April","May","June","July","August","September","October","November","December");
	myDates 	= new Array ("1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15","16th","17th","18th","19th","20th","21th","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st");
	
	sDay = day; sMonth = month; sYear = year;	//Short Date variables
	lDay = day; lMonth = month; lYear = year;	//Long Date Variables
	
	//Get the Short date and format it
	if (sDay < 10) {
		sDay = "0" + sDay;
	}

	sMonth ++;
	if (sMonth < 10) {
		sMonth = "0" + sMonth;
	}
	
	if (NS6 || OPR6) {
		lYear += 1900;
		sYear += 1900;
	}
	
	myShortDate = sDay + "/" + sMonth + "/" + sYear;
	//alert ("Short Date: " + myShortDate);

	//Get the Long date and format it
	lDay 	= myDates[lDay - 1];
	lMonth 	= myMonths[lMonth];
	
	myLongDate = lDay + " " + lMonth + " " + lYear
	//alert ("Long Date: " + myLongDate);
	
	//Get the time and format it
	if (hours < 10) {
		hours = "0" + hours;
	}
	if (minutes < 10) {
		minutes = "0" + minutes;
	}
	myTime = hours + ":" + minutes;
	//alert("The Time: " + time);
}

function okDates(day,month,year)
{
	my30s 	= new Array ("10","4","6","11");
	my31s 	= new Array ("1","3","5","7","8","9","12");
	my29 	= new Array ("2004","2008","2012","2016","2020");

	if (month != 2) {
		for (i=0;i<my30s.length;i++) {
			if (month == my30s[i]) {
				maxDay = 30;
				return true;
			}
		}
		for (i=0;i<my31s.length;i++) {
			if (month == my31s[i]) {
				maxDay = 31;
				return true;
			}
		}
	} else {
		for (i=0;i<=my29.length;i++) {
			if (year == my29[i]) {
				maxDay = 29;
				return true;
			} else {
				maxDay = 28
				return true;
			}
		}
	}
}