// Booking Forms

function FormValidation(type) {
	theform = document.getElementById("form1");
	today = new Date();
	month = leadingZero(today.getMonth()+1);
	year = takeYear(today);
	errormessage = "Some missing informations:\n\n";
	if (theform.initial.value == "") { errormessage += "- Please enter your Initials. \n"; }
	if (theform.firstname.value == "") { errormessage += "- Please enter your First Name. \n"; }
	if (theform.surname.value == "") { errormessage += "- Please enter your Surname. \n"; }
	if (theform.initial.value == "") { errormessage += "- Please enter your Initials. \n"; }
	if (theform.address.value == "" && theform.address2.value == "") { errormessage += "- Please enter at least one Address Line. \n"; }
	if (theform.town.value == "") { errormessage += "- Please enter your Town or City. \n"; }
	if (theform.county.value == "") { errormessage += "- Please enter your County. \n"; }
	if (theform.postcode.value == "") { errormessage += "- Please enter your Postcode. \n"; }
	if (theform.phone.value == "") { errormessage += "- Please enter your Phone. \n"; }
	if ((theform.email.value == "") || (theform.email.value.length > 0 && (theform.email.value.indexOf("@",0) == - 1 || theform.email.value.indexOf(".",0) == - 1))) { errormessage += "- Please enter a valid e-mail address\n";  }
	if (theform.CCholder.value == "" || theform.CCholder.value.indexOf(" ",0) == -1 || theform.CCholder.value.length < 5) { errormessage += "- Please enter a valid Credit Card Holder Name. \n"; }
	if ((theform.CCexpmonth.value < month && theform.CCexpyear.value <= (year - 2000)) || theform.CCexpyear.value < (year - 2000)) { errormessage += "- Credit Card expired. \n"; }
	if (theform.CCnum.value == "") { errormessage += "- Please enter your Credit Card Number. \n"; }
	if ((theform.CCType.value == "solo" || theform.CCType.value == "maestro") && theform.CCissue.value == "") { errormessage += "- Please enter your Issue Number"; }
	if (theform.terms.checked == false) { errormessage += "- Please accept terms and agreement. \n"; }
	
	if (type == 0) {
		if (theform.carmake.value == "") { errormessage += "- Please enter your Car Make. \n"; }
		if (theform.carmodel.value == "") { errormessage += "- Please enter your Car Model. \n"; }
		if (theform.carcolour.value == "") { errormessage += "- Please enter your Car Colour. \n"; }
		if (theform.phone.value == "") { errormessage += "- Please enter your Phone. \n"; }
		if (theform.carreg.value == "") { errormessage += "- Please enter your Car Registration Number. \n"; }	
	}
	
	if (errormessage != "Some missing informations:\n\n") { alert(errormessage); }
	else {
		if (type == 1) {
			bookHotel();
		} else if (type == 0) {
			bookCarPark();	
		} else if (type == 2) {
			bookLounge();
		}
	}
}

function leadingZero(nr)
{
	if (nr < 10) nr = "0" + nr;
	return nr;
}

function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}


function sendForm(name) {
	theform = document.getElementById("book"+name);
	if(theform.choice.value == "HotelWithParking") {
		request = "checkin_day="+theform.checkin_day.value;
		request += "&hotelname="+theform.hotelname.value.replace("&", "%26");
		request += "&checkin_monthyear="+theform.checkin_monthyear.value;
		request += "&hotelcode="+theform.hotelcode.value;
		request += "&boardbasis="+theform.boardbasis.value;
		request += "&price="+theform.price.value;
		request += "&airport="+theform.airport.value;
		if (theform.roomparks) { request += "&roomparks="+theform.roomparks.value; }
		else { request += "&roomparks=0"; }
		request += "&parkingdays="+theform.parkingdays.value;
		request += "&room="+theform.room.value;
		if (theform.room2) { request += "&room2="+theform.room2.value; }
		if (theform.collectcar) { request += "&collectcar="+theform.collectcar.value; }
		request += "&startbooking=y";
		req2 = createXMLHttpRequest();
		req2.onreadystatechange = handleResponse2;
		req2.open('post', 'book_hotel.php', true);
		req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req2.setRequestHeader("Content-length", request.length);
		req2.setRequestHeader("Connection", "close");
		req2.send(request);
	} else {
		request = "day="+theform.day.value;
		request += "&carparkname="+theform.carparkname.value.replace("&", "%26");
		request += "&monthyear="+theform.monthyear.value;
		request += "&arrivaltime="+theform.arrivaltime.value;
		request += "&day_to="+theform.day_to.value;
		request += "&monthyear_to="+theform.monthyear_to.value;
		request += "&departtime="+theform.departtime.value;
		request += "&airport="+theform.airport.value;
		request += "&totalprice="+theform.totalprice.value;
		request += "&parkingcode="+theform.parkingcode.value;
		request += "&gateprice="+theform.gateprice.value;
		req2 = createXMLHttpRequest();
		req2.onreadystatechange = handleResponse2;
		req2.open('post', 'book_carpark.php', true);
		req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req2.setRequestHeader("Content-length", request.length);
		req2.setRequestHeader("Connection", "close");
		req2.send(request);
	}
}

function sendFormLounge(i) {
		theform = document.getElementById("book"+i);
		request = "day="+theform.day.value;
		request += "&month_year="+theform.month_year.value;
		request += "&arrivaltime="+theform.arrivaltime.value;
		request += "&airport="+theform.airport.value;
		request += "&loungename="+theform.loungename.value;
		request += "&loungecode="+theform.loungecode.value;
		request += "&loungeprice="+theform.loungeprice.value;
		request += "&adults="+theform.adults.value;
		request += "&child="+theform.child.value;
		request += "&infants="+theform.infants.value;
		req2 = createXMLHttpRequest();
		req2.onreadystatechange = handleResponse2;
		req2.open('post', 'book_lounge.php', true);
		req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req2.setRequestHeader("Content-length", request.length);
		req2.setRequestHeader("Connection", "close");
		req2.send(request);
}

function bookHotel() {
	theform = document.getElementById("form1");
	request = "checkin_day="+theform.checkin_day.value;
	request += "&hotelname="+theform.hotelname.value.replace("&", "%26");
	request += "&checkin_monthyear="+theform.checkin_monthyear.value;
	request += "&hotelcode="+theform.hotelcode.value;
	request += "&boardbasis="+theform.boardbasis.value;
	request += "&price="+theform.price.value;
	request += "&airport="+theform.airport.value;
	if (theform.roomparks) { request += "&roomparks="+theform.roomparks.value; }
	else { request += "&roomparks=0"; }
	request += "&parkingdays="+theform.parkingdays.value;
	request += "&room="+theform.room.value;
	if (theform.room2) { request += "&room2="+theform.room2.value; }
	if (theform.collectcar) { request += "&collectcar="+theform.collectcar.value; }
	request += "&book=y";
	request += "&title="+theform.title.value;
	request += "&initial="+theform.initial.value;
	request += "&firstname="+theform.firstname.value;
	request += "&marketing="+theform.marketing.checked;
	request += "&surname="+theform.surname.value;
	request += "&county="+theform.county.value;
	request += "&address2="+theform.address2.value;
	request += "&address="+theform.address.value;
	request += "&town="+theform.town.value;
	request += "&postcode="+theform.postcode.value;
	request += "&phone="+theform.phone.value;
	request += "&email="+theform.email.value;
	request += "&CCnum="+theform.CCnum.value;
	request += "&CCexpmonth="+theform.CCexpmonth.value;
	request += "&CCexpyear="+theform.CCexpyear.value;
	request += "&CCissue="+theform.CCissue.value;
	request += "&CCholder="+theform.hotelcode.value;
	request += "&CCscode="+theform.boardbasis.value;
	req2 = createXMLHttpRequest();
	req2.open('post', 'book_hotel.php', true);
	req2.onreadystatechange = handleResponse2;
	req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req2.setRequestHeader("Content-length", request.length);
	req2.setRequestHeader("Connection", "close");
	req2.send(request);
}

function bookCarPark() {
	theform = document.getElementById("form1");
	request = "day="+theform.day.value;
	request += "&carparkname="+theform.carparkname.value.replace("&", "%26");
	request += "&monthyear="+theform.monthyear.value;
	request += "&arrivaltime="+theform.arrivaltime.value;
	request += "&day_to="+theform.day_to.value;
	request += "&monthyear_to="+theform.monthyear_to.value;
	request += "&departtime="+theform.departtime.value;
	request += "&parkingcode="+theform.parkingcode.value;
	request += "&gateprice="+theform.gateprice.value;
	request += "&totalprice="+theform.totalprice.value;
	request += "&airport="+theform.airport.value;
	request += "&carmake="+theform.carmake.value;
	request += "&carmodel="+theform.carmodel.value;
	request += "&carcolour="+theform.carcolour.value;
	request += "&carreg="+theform.carreg.value;
	request += "&passenger="+theform.passenger.value;
	request += "&flightno="+theform.flightno.value;
	request += "&duration="+theform.duration.value;
	request += "&firstname="+theform.firstname.value;
	request += "&marketing="+theform.marketing.checked;
	request += "&surname="+theform.surname.value;
	request += "&county="+theform.county.value;
	request += "&address2="+theform.address2.value;
	request += "&book=y";
	request += "&title="+theform.title.value;
	request += "&initial="+theform.initial.value;
	request += "&surname="+theform.surname.value;
	request += "&address="+theform.address.value;
	request += "&town="+theform.town.value;
	request += "&postcode="+theform.postcode.value;
	request += "&phone="+theform.phone.value;
	request += "&email="+theform.email.value;
	request += "&CCnum="+theform.CCnum.value;
	request += "&CCexpmonth="+theform.CCexpmonth.value;
	request += "&CCexpyear="+theform.CCexpyear.value;
	request += "&CCissue="+theform.CCissue.value;
	request += "&CCholder="+theform.CCholder.value;
	request += "&CCscode="+theform.CCscode.value;
	req2 = createXMLHttpRequest();
	req2.onreadystatechange = handleResponse2;
	req2.open('post', 'book_carpark.php', true);
	req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req2.setRequestHeader("Content-length", request.length);
	req2.setRequestHeader("Connection", "close");
	req2.send(request);
}

function bookLounge() {
	theform = document.getElementById("form1");
	request = "day="+theform.day.value;
	request += "&month_year="+theform.month_year.value;
	request += "&arrivaltime="+theform.arrivaltime.value;
	request += "&airport="+theform.airport.value;
	request += "&loungename="+theform.loungename.value.replace("&", "%26");
	request += "&loungecode="+theform.loungecode.value;
	request += "&loungeprice="+theform.loungeprice.value;
	request += "&adults="+theform.adults.value;
	request += "&child="+theform.child.value;
	request += "&infants="+theform.infants.value;
	request += "&firstname="+theform.firstname.value;
	request += "&marketing="+theform.marketing.checked;
	request += "&surname="+theform.surname.value;
	request += "&county="+theform.county.value;
	request += "&address2="+theform.address2.value;
	request += "&book=y";
	request += "&title="+theform.title.value;
	request += "&initial="+theform.initial.value;
	request += "&surname="+theform.surname.value;
	request += "&address="+theform.address.value;
	request += "&town="+theform.town.value;
	request += "&postcode="+theform.postcode.value;
	request += "&phone="+theform.phone.value;
	request += "&email="+theform.email.value;
	request += "&CCnum="+theform.CCnum.value;
	request += "&CCexpmonth="+theform.CCexpmonth.value;
	request += "&CCexpyear="+theform.CCexpyear.value;
	request += "&CCissue="+theform.CCissue.value;
	request += "&CCholder="+theform.CCholder.value;
	request += "&CCscode="+theform.CCscode.value;
	req2 = createXMLHttpRequest();
	req2.onreadystatechange = handleResponse2;
	req2.open('post', 'book_lounge.php', true);
	req2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req2.setRequestHeader("Content-length", request.length);
	req2.setRequestHeader("Connection", "close");
	req2.send(request);
}

function handleResponse2() {
		if (req2.readyState == 4) {				
				document.getElementById('contents').innerHTML = req2.responseText;
		} else {
				document.getElementById('contents').innerHTML = "<p>Loading...<br /><img src='images/loader.gif'></p>";
			}
}