var journey_1_title = '';
var journey_2_title = '';

var journey_1_hour_title = '';
var journey_2_hour_title = '';

var journey_1_where_title = '';
var journey_2_where_title = '';

function getForm_1(thisSelect) {
	var state = thisSelect.options[thisSelect.selectedIndex].value;

	var journey_value = document.getElementById('journey').options[document.getElementById('journey').selectedIndex].value;


	if(state=='Chamonix') {
		journey_1_title = 'Chamonix - Geneva';
		journey_2_title = 'Geneva - Chamonix';

		journey_1_hour_title = 'Departure';
		journey_2_hour_title = 'Arrival';

		journey_1_where_title = 'Pick-up';
		journey_2_where_title = 'Drop-off';

		if(journey_value.length>0) getForm_2(document.getElementById('journey'));
	}
	else if(state.length==0) {
		document.getElementById('journey_1').innerHTML = '';
		document.getElementById('journey_1').innerHTML = '';
		document.getElementById('personal_details').innerHTML = '';
		document.getElementById('children_ages').innerHTML = '';
	}
	else {
		journey_1_title = 'Geneva - Chamonix';
		journey_2_title = 'Chamonix - Geneva';

		journey_1_hour_title = 'Arrival';
		journey_2_hour_title = 'Departure';

		journey_1_where_title = 'Drop-off';
		journey_2_where_title = 'Pick-up';

		if(journey_value.length>0) getForm_2(document.getElementById('journey'));
	}
}
function getForm_2(thisSelect) {
	var journey_1 = '';
	journey_1 += "<p>" + journey_1_title + "</p><br />";
	journey_1 += "<label for=\"journey_1_date\" class=\"cvt_narrow\">Date <span class=\"cvt_compulsory\">*</span></label>";
	journey_1 += "<input type=\"text\" class=\"cvt_date\" name=\"journey_1_date\" id=\"journey_1_date\" readonly=\"readonly\" onclick=\"scwShow(this,event);\" />";
	journey_1 += "<img src=\"images/calendar.gif\" width=\"20px\" height=\"19px\" class=\"cvt_date\" alt=\"Select a Date\" title=\"Select a Date\" onclick=\"scwShow(scwID('journey_1_date'),event);\" /><br />";
	journey_1 += "<label for=\"journey_1_hour\" class=\"cvt_narrow\">Flight " + journey_1_hour_title + " Time <span class=\"cvt_compulsory\">*</span></label><select class=\"cvt_narrow_1\" name=\"journey_1_hour\">";
	journey_1 += "		<option value=\"\">HH</option>";
	journey_1 += "		" + make_number_options('', 0, 23, '0', '00', 1);
	journey_1 += "	</select>";
	journey_1 += "	<label for=\"journey_1_minute\" class=\"cvt_very_narrow\">:</label>";
	journey_1 += "	<select class=\"cvt_narrow_2\" name=\"journey_1_minute\">";
	journey_1 += "		<option value=\"\">MM</option>";
	journey_1 += "		" + make_number_options('', 0, 59, '0', '00', 10);
	journey_1 += "	</select><br />";
	journey_1 += "<label for=\"journey_1_flight\" class=\"cvt\">Flight No. <span class=\"cvt_compulsory\">*</span></label><input type=\"text\" class=\"cvt_txt\" name=\"journey_1_flight\" id=\"journey_1_flight\" /><br />";
	journey_1 += "<label for=\"journey_1_where\" class=\"cvt_full_width\">" + journey_1_where_title + " Point <span class=\"cvt_compulsory\">*</span></label><br />";
	journey_1 += "<textarea name=\"journey_1_where\" id=\"journey_1_where\" class=\"cvt\"></textarea><br />";

	var journey_2 = '';
	journey_2 += "<p>" + journey_2_title + "</p><br />";
	journey_2 += "<label for=\"journey_2_date\" class=\"cvt_narrow\">Date <span class=\"cvt_compulsory\">*</span></label>";
	journey_2 += "<input type=\"text\" class=\"cvt_date\" name=\"journey_2_date\" id=\"journey_2_date\" readonly=\"readonly\" onclick=\"scwShow(this,event);\" />";
	journey_2 += "<img src=\"images/calendar.gif\" width=\"20px\" height=\"19px\" class=\"cvt_date\" alt=\"Select a Date\" title=\"Select a Date\" onclick=\"scwShow(scwID('journey_2_date'),event);\" /><br />";
	journey_2 += "<label for=\"journey_2_hour\" class=\"cvt_narrow\">Flight " + journey_2_hour_title + " Time <span class=\"cvt_compulsory\">*</span></label><select class=\"cvt_narrow_1\" name=\"journey_2_hour\">";
	journey_2 += "		<option value=\"\">HH</option>";
	journey_2 += "		" + make_number_options('', 0, 23, '0', '00', 1);
	journey_2 += "	</select>";
	journey_2 += "	<label for=\"journey_2_minute\" class=\"cvt_very_narrow\">:</label>";
	journey_2 += "	<select class=\"cvt_narrow_2\" name=\"journey_2_minute\">";
	journey_2 += "		<option value=\"\">MM</option>";
	journey_2 += "		" + make_number_options('', 0, 59, '0', '00', 10);
	journey_2 += "	</select><br />";
	journey_2 += "<label for=\"journey_2_flight\" class=\"cvt\">Flight No. <span class=\"cvt_compulsory\">*</span></label><input type=\"text\" class=\"cvt_txt\" name=\"journey_2_flight\" id=\"journey_2_flight\" /><br />";
	journey_2 += "<label for=\"journey_2_where\" class=\"cvt_full_width\">" + journey_2_where_title + " Point <span class=\"cvt_compulsory\">*</span></label><br />";
	journey_2 += "<textarea name=\"journey_2_where\" id=\"journey_2_where\" class=\"cvt\"></textarea><br />";

	var personal_details = '';
	personal_details += "<p>Personal Details:</p><br />";
	personal_details += "<label for=\"title\" class=\"cvt\">Title <span class=\"cvt_compulsory\">*</span></label><select name=\"title\" id=\"title\" class=\"cvt\">";
	personal_details += "		<option value=\"Mr\">Mr</option>";
	personal_details += "		<option value=\"Mrs\">Mrs</option>";
	personal_details += "		<option value=\"Miss\">Miss</option>";
	personal_details += "		<option value=\"Ms\">Ms</option>";
	personal_details += "	</select><br />";
	personal_details += "<label for=\"first_name\" class=\"cvt\">First Name <span class=\"cvt_compulsory\">*</span></label><input type=\"text\" class=\"cvt_txt\" name=\"first_name\" id=\"first_name\" /><br />";
	personal_details += "<label for=\"last_name\" class=\"cvt\">Last Name <span class=\"cvt_compulsory\">*</span></label><input type=\"text\" class=\"cvt_txt\" name=\"last_name\" id=\"last_name\" /><br />";
	personal_details += "<label for=\"email\" class=\"cvt\">Email <span class=\"cvt_compulsory\">*</span></label><input type=\"text\" class=\"cvt_txt\" name=\"email\" id=\"email\" /><br />";
	personal_details += "<label for=\"phone\" class=\"cvt\">Phone <span class=\"cvt_compulsory\">*</span></label><input type=\"text\" class=\"cvt_txt\" name=\"phone\" id=\"phone\" /><br />";
	personal_details += "<label for=\"no_adults\" class=\"cvt\">Number Of Adults <span class=\"cvt_compulsory\">*</span></label><select name=\"no_adults\" id=\"no_adults\" class=\"cvt\">";
	personal_details += "		" + make_number_options('', 1, 50, '', '', 1);
	personal_details += "	</select><br />";
	personal_details += "<label for=\"no_children\" class=\"cvt\">Children Under 12 <span class=\"cvt_compulsory\">*</span></label><select name=\"no_children\" id=\"no_children\" class=\"cvt\" onchange=\"getForm_3(this)\">";
	personal_details += "		" + make_number_options('', 0, 10, '', 'None', 1);
	personal_details += "		<option value=\"gt_10\">&gt;10</option>";
	personal_details += "	</select><br />";

	var state = thisSelect.options[thisSelect.selectedIndex].value;

	if(state == "One Way") {
		document.getElementById('journey_1').innerHTML = journey_1;
		document.getElementById('journey_2').innerHTML = '';
		document.getElementById('personal_details').innerHTML = personal_details;
	}
	else if(state.length>0) {
		document.getElementById('journey_1').innerHTML = journey_1;
		document.getElementById('journey_2').innerHTML = journey_2;
		document.getElementById('personal_details').innerHTML = personal_details;
	}
	else {
		document.getElementById('journey_1').innerHTML = '';
		document.getElementById('journey_1').innerHTML = '';
		document.getElementById('personal_details').innerHTML = '';
		document.getElementById('children_ages').innerHTML = '';
	}
}
function getForm_3(thisSelect) {
	var state = thisSelect.options[thisSelect.selectedIndex].value;
	if(state=='gt_10') {
		if(confirm('For bookings with more than 10 children please contect us for a quote, \nclick \'OK\' to be taken to our contact page.')) document.location.href="http://www.chamonix-valley-transfers.com/contact_us.php";
	}
	else if(state>0) {
		newHtml = '';
		for(i=1; i<((state*1)+1); i++) {
			newHtml += "<label for=\"child_age_" + i + "\" class=\"cvt\">Age of child " + i + " <span class=\"cvt_compulsory\">*</span></label><select name=\"child_age[" + i + "]\" id=\"child_age_" + i + "\" class=\"cvt\">";
			newHtml += "		" + make_number_options('', 0, 11, '', '0', 1);
			newHtml += "</select><br />";
		}
		document.getElementById('children_ages').innerHTML = newHtml;
	}
	else document.getElementById('children_ages').innerHTML = '';
}

function parseForm(thisForm) {
	with(thisForm) {
		var starting_at_value = document.getElementById('starting_at').options[document.getElementById('starting_at').selectedIndex].value;
		if(starting_at_value.length==0) {
			alert("Please tell us where your first journey will start.");
			document.getElementById('starting_at').focus();
			return false;
		}
		var journey_value = journey.options[journey.selectedIndex].value;
		if(journey_value.length==0) {
			alert("Please tell us whether you require a one way or return journey.");
			journey.focus();
			return false;
		}
		var transfer_type_value = transfer_type.options[transfer_type.selectedIndex].value;
		if(transfer_type_value.length==0) {
			alert("Please tell us whether you require a shared, private or VIP transfer.");
			transfer_type.focus();
			return false;
		}

		var journey_1_date_value = journey_1_date.value;
		if(journey_1_date_value.length==0) {
			alert("Please select the date of your first journey.");
			journey_1_date.focus();
			return false;
		}

		var journey_1_hour_value = journey_1_hour.options[journey_1_hour.selectedIndex].value;
		if(journey_1_hour_value.length==0) {
			var msg = "Please select the hour of your flight ";
			if(starting_at_value=='Chamonix') msg += "departure.";
			else msg += "arrival.";
			alert(msg);
			journey_1_hour.focus();
			return false;
		}

		var journey_1_minute_value = journey_1_minute.options[journey_1_minute.selectedIndex].value;
		if(journey_1_minute_value.length==0) {
			var msg = "Please select the minute of your flight ";
			if(starting_at_value=='Chamonix') msg += "departure.";
			else msg += "arrival.";
			alert(msg);
			journey_1_minute.focus();
			return false;
		}

		var Date_obj = new Date();
		var my_year = y2k(Date_obj.getYear());
		var my_month = Date_obj.getMonth();
		var my_day = Date_obj.getDate();
		var my_hour = Date_obj.getHours();
		var my_minute = Date_obj.getMinutes();
		var todayUTC = Date.UTC(my_year,my_month,my_day,my_hour,my_minute,0);

		var sd = journey_1_date_value.split("/");
		var start_day = sd[0];
		var start_month = sd[1]-1;
		var start_year = sd[2];
		var startUTC = Date.UTC(start_year,start_month,start_day,journey_1_hour_value,journey_1_minute_value,0);
		if(todayUTC>startUTC) {
			alert("Please enter a valid date in the future for your first journey!");
			journey_1_date.focus();
			return false;
		}
		else if((todayUTC+(60*60*24*1000))>startUTC) {
			if(confirm("For bookings less than 24hrs in the future please call us!\nClick 'OK' to be taken to our contact details")) document.location.href="http://www.chamonix-valley-transfers.com/contact.php";
			return false;
		}

		var journey_1_flight_value = journey_1_flight.value;
		if(journey_1_flight_value.length==0) {
			alert("Please enter the flight number for your first journey.");
			journey_1_flight.focus();
			return false;
		}

		var journey_1_where_value = journey_1_where.value;
		if(journey_1_where_value.length==0) {
			var msg = "Please tell us where in chamonix we should ";
			if(starting_at_value=='Chamonix') msg += "pick you up.";
			else msg += "drop you off.";
			alert(msg);
			journey_1_where.focus();
			return false;
		}

		if(journey_value=='Return') {
			var journey_2_date_value = journey_2_date.value;
			if(journey_2_date_value.length==0) {
				alert("Please select the date of your second journey.");
				journey_2_date.focus();
				return false;
			}

			var journey_2_hour_value = journey_2_hour.options[journey_2_hour.selectedIndex].value;
			if(journey_2_hour_value.length==0) {
				var msg = "Please select the hour of your flight ";
				if(starting_at_value=='Chamonix') msg += "arrival.";
				else msg += "departure.";
				alert(msg);
				journey_2_hour.focus();
				return false;
			}

			var journey_2_minute_value = journey_2_minute.options[journey_2_minute.selectedIndex].value;
			if(journey_2_minute_value.length==0) {
				var msg = "Please select the minute of your flight ";
				if(starting_at_value=='Chamonix') msg += "arrival.";
				else msg += "departure.";
				alert(msg);
				journey_2_minute.focus();
				return false;
			}

			var sd = journey_2_date_value.split("/");
			var start_day = sd[0];
			var start_month = sd[1]-1;
			var start_year = sd[2];
			var startUTC = Date.UTC(start_year,start_month,start_day,journey_2_hour_value,journey_2_minute_value,0);

			if(todayUTC>startUTC) {
				alert("Please enter a valid date in the future for your second journey!");
				journey_2_date.focus();
				return false;
			}
			else if((todayUTC+(60*60*24*1000))>startUTC) {
				if(confirm("For bookings less than 24hrs in the future please call us!\nClick 'OK' to be taken to our contact details")) document.location.href="http://www.chamonix-valley-transfers.com/contact_us.php";
			}

			var journey_2_flight_value = journey_2_flight.value;
			if(journey_2_flight_value.length==0) {
				alert("Please enter the flight number for your second journey.");
				journey_2_flight.focus();
				return false;
			}

			var journey_2_where_value = journey_2_where.value;
			if(journey_2_where_value.length==0) {
				var msg = "Please tell us where in chamonix we should ";
				if(starting_at_value=='Chamonix') msg += "drop you off.";
				else msg += "pick you up.";
				alert(msg);
				journey_2_where.focus();
				return false;
			}

		}

		var first_name_value = first_name.value;
		if(first_name_value.length==0) {
			alert("Please enter your first name.");
			first_name.focus();
			return false;
		}
		var last_name_value = last_name.value;
		if(last_name_value.length==0) {
			alert("Please enter your last name.");
			last_name.focus();
			return false;
		}
		if(validate_email(email,"Please enter a valid email address!")==false) {
			email.focus();
			return false;
		}
		var phone_value = phone.value;
		if(phone_value.length==0) {
			alert("Please enter your phone number.");
			phone.focus();
			return false;
		}
		return true;
	}
}


function make_number_options(selected, start, end, prefix, zero, interval) {
	var number_options = "";
	for(x=start; x<=end; x=x+interval) {
		display_number = "";
		if(x=='0') display_day = zero;
		else if(x<10) display_day = prefix + x;
		else display_day = x;
		selected_str = "";
		if(selected.length>0 && selected==x) selected_str = " selected=\"selected\"";
		number_options += "<option value=\""+x+"\""+selected_str+">"+display_day+"</option>\n";
	}
	return number_options;
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		}
		else {
			return true
		}
	}
}

function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

