var lastIndex=-1; 
var lastDDIndex=-1;
var airports = new Array();
var seaports = new Array();
var trainStations = new Array();

airports[0] = "-- Select --";
airports[1] = "Cardiff";
airports[2] = "Gatwick";
airports[3] = "Heathrow";
airports[4] = "Luton";
airports[5] = "Stanstead";
airports[6] = "Bristol International";

seaports[0] = "-- Select --";
seaports[1] = "None available";

trainStations[0]  = "-- Select --";
trainStations[1]  = "Bristol Temple Meads";
trainStations[2]  = "Montpelier";
trainStations[3]  = "Stapleton Road";
trainStations[4]  = "Lawrence Hill";
trainStations[5]  = "Redland";
trainStations[6]  = "Clifton Down";
trainStations[7]  = "Parson Street";
trainStations[8]  = "Bedminster";
trainStations[9]  = "Sea Mills";
trainStations[10] = "Filton Abbey Wood";
trainStations[11] = "Bristol Parkway";
trainStations[12] = "Patchway";
trainStations[13] = "Shirehampton";
trainStations[14] = "Keynsham";
trainStations[15] = "Avonmouth";
trainStations[16] = "St Andrews Road";
trainStations[17] = "Pilning";
trainStations[18] = "Severn Beach";
trainStations[19] = "Nailsea & Backwell";
trainStations[20] = "Yate";
trainStations[21] = "Yatton";
trainStations[22] = "Worle";
trainStations[23] = "Weston Milton";
trainStations[24] = "Weston-super-Mare";

function SelectOptionButton(index, ddIndex) {
	/*
	ddIndex: Index of DropDown to load info 0-Top 1-Down
	Index: Index of radio button
	*/
	
	var i;				
	//Select the radio button
	if (ddIndex == 0)
		frm_userInput.input_fromDest[index].checked = true;	
	else
		frm_userInput.input_toDest[index].checked = true;	
	
	
	//Set visibility	
	if (div_pretext[ddIndex].style.display != "none" ) {
		
		div_pretext[ddIndex].style.display = "none";
		div_goVia[ddIndex].style.display = "block";
	}	
	
	if (index >=0 && index <= 2) {
	    div_dd[ddIndex].style.display = "block";		
		div_address[ddIndex].style.display = "none";
	}
	else if (index == 3) {
		div_dd[ddIndex].style.display = "none";		
		div_address[ddIndex].style.display = "block";
	}
	
	//Load info in Drop Down
	if (lastIndex != index || lastDDIndex != ddIndex) {
		lastIndex = index;
		lastDDIndex = ddIndex;
		if (index == 0) {
			ClearDropdown(frm_userInput.select_DD[ddIndex]);
			FillDropdown(frm_userInput.select_DD[ddIndex], airports)
		}
		else if (index == 1) {
			ClearDropdown(frm_userInput.select_DD[ddIndex]);
			FillDropdown(frm_userInput.select_DD[ddIndex], seaports)
		}
		else if (index == 2) {
			ClearDropdown(frm_userInput.select_DD[ddIndex]);
			FillDropdown(frm_userInput.select_DD[ddIndex], trainStations)
		}
	}	
}

function select_govia(ddIndex, eventOrigin){	
	//eventOrigin: "checkbox" or "alink"	
	
	if (eventOrigin == "alink")  //then Toggle, "checkbox" would handle itself
		frm_userInput.input_goVia[ddIndex].checked = !frm_userInput.input_goVia[ddIndex].checked;

	//Set Visibility	
	if (frm_userInput.input_goVia[ddIndex].checked != true) 
		div_via[ddIndex].style.display = "none";	
	else
		div_via[ddIndex].style.display = "block";	
}


function adjust_govia(ddIndex) {
	var arr = new Array();
	var i, j;
	arr[0] = frm_userInput.street1[ddIndex];
	arr[1] = frm_userInput.street2[ddIndex];
	arr[2] = frm_userInput.street3[ddIndex];
	arr[3] = frm_userInput.street4[ddIndex];
	arr[4] = frm_userInput.street5[ddIndex];
	
	for (i=0; i<5; i++) {
		if (arr[i].value == "") {
			for (j=i+1; j<5; j++) {
				if (arr[j].value != "") {
					arr[i].value = arr[j].value;
					arr[j].value = "";
					//arr[i].focus();
					//arr[i].select();
					break;
				}				
			}
		}
	}
}

function processLuggage() {
	if (frm_userInput.select_luggageType.selectedIndex == 1) {
		frm_userInput.input_luggage.disabled = true;
		frm_userInput.input_luggage.value = "";
	}
	else
		frm_userInput.input_luggage.disabled = false;
}

function ClearDropdown(selDD) {
	var i, l;
	
	l = selDD.options.length;
	
	for(i=l-1; i>=0; i--) {
		selDD.remove(i);	
	}
}
function FillDropdown(selDD, textArray) {
	var i;	
	for(i=0; i<textArray.length; i++) {
		option = document.createElement("OPTION");
		option.text = textArray[i];
		selDD.add(option, i);
	}
}

function validate_frm () {
	var butSelected, i, j, k, str;
	
	//alert(frm_userInput.select_DD[0].selectedIndex);
	//alert("");
	
	adjust_govia(0);
	adjust_govia(1);
		
	//Validating the 'Pickup From' Part
	
	butSelected = false;
	for (i=0; i<4; i++) {
		if (frm_userInput.input_fromDest[i].checked) { //a radio is selected
			if (i>=0 && i<=2) { //Airport or Seaport or Train Station
				if (frm_userInput.select_DD[0].length == 0 ||			//no dynamic list defined yet
					frm_userInput.select_DD[0].selectedIndex == -1 ||	//nothing selected yet in the list
					frm_userInput.select_DD[0].selectedIndex == 0) {	//first option selected means nothing selected yet
					
					if (i == 0)	alert("Please select an Air Port in 'Pickup From'.");
					else if (i == 1) alert("Please select a Sea Port in 'Pickup From'.");
					else if (i == 2) alert("Please select a Train Station in 'Pickup From'.");
					
					return false;					
				}
				else if(i == 1 && frm_userInput.select_DD[0].selectedIndex == 1) { //seaport selected, but none available
				    alert("Currently our service is not available for any sea port.");
				    return false;
				}
			}
			else if (i == 3){ //Address
				if (frm_userInput.addr[0].value == "") { alert("Please enter 'Building Name/No' in 'Pickup From' address."); return false; }
				else if (frm_userInput.street[0].value == "") { alert("Please enter 'Street' in 'Pickup From' address."); return false; }
				else if (frm_userInput.city[0].value == "") { alert("Please enter 'Town/City' in 'Pickup From' address."); return false; }
				else if (frm_userInput.county[0].value == "") { alert("Please enter 'County' in 'Pickup From' address."); return false; }
				else if (frm_userInput.pCode[0].value == "") { alert("Please enter 'Post Code' in 'Pickup From' address."); return false; }
				
			}			
			butSelected = true;						
		}
	}
		
	if (!butSelected) {	//no radio is selected
		alert("Please select 'Pickup From' location.");
		return false;
	}	
	
	if (frm_userInput.input_goVia[0].checked == true){ //Go Via
		if (frm_userInput.street1[0].value == "" && frm_userInput.street2[0].value == "" &&
			frm_userInput.street3[0].value == "" && frm_userInput.street4[0].value == "" &&
			frm_userInput.street5[0].value == ""){
			alert("Please enter at-least one 'Street' in 'Pickup From' go via."); return false; 
		}				
	}			

	//Validating the 'Drop Off' Part
		
	butSelected = false;
	for (i=0; i<4; i++) {
		if (frm_userInput.input_toDest[i].checked) { //a radio is selected
			if (i>=0 && i<=2) { //Airport or Seaport or Train Station
				if (frm_userInput.select_DD[1].length == 0 ||			//no dynamic list defined yet
					frm_userInput.select_DD[1].selectedIndex == -1 ||	//nothing selected yet in the list
					frm_userInput.select_DD[1].selectedIndex == 0) {	//first option selected means nothing selected yet
					
					if (i == 0)	alert("Please select an Air Port in 'Drop Off'.");
					else if (i == 1) alert("Please select a Sea Port in 'Drop Off'.");
					else if (i == 2) alert("Please select a Train Station in 'Drop Off'.");
					
					return false;
				}
				else if(i == 1 && frm_userInput.select_DD[1].selectedIndex == 1) { //seaport selected, but none available
				    alert("Currently our service is not available for any sea port.");
				    return false;
				}
			}
			else if (i == 3) { //Address			
				if (frm_userInput.addr[1].value == "") { alert("Please enter 'Building Name/No' in 'Pickup From' address."); return false; }
				else if (frm_userInput.street[1].value == "") { alert("Please enter 'Street' in 'Drop Off' address."); return false; }
				else if (frm_userInput.city[1].value == "") { alert("Please enter 'Town/City' in 'Drop Off' address."); return false; }
				else if (frm_userInput.county[1].value == "") { alert("Please enter 'County' in 'Drop Off' address."); return false; }
				else if (frm_userInput.pCode[1].value == "") { alert("Please enter 'Post Code' in 'Drop Off' address."); return false; }
				
			}			
			butSelected = true;			
		}
	}
	
	if (!butSelected) {	//no radio is selected
		alert("Please select 'Drop Off' location.");
		return false;
	}

	if (frm_userInput.input_goVia[1].checked == true){ //Go Via
		if (frm_userInput.street1[1].value == "" && frm_userInput.street2[1].value == "" &&
			frm_userInput.street3[1].value == "" && frm_userInput.street4[1].value == "" &&
			frm_userInput.street5[1].value == ""){
			alert("Please enter at-least one 'Street' in 'Pickup From' go via."); return false; 
		}				
	}
	
	//Validating the rest of the form
	if (frm_userInput.input_pickup_dd.value == "") { alert("Please enter Day of 'Pickup Date'."); return false; }
	else if (parseInt(frm_userInput.input_pickup_dd.value) < 1 || parseInt(frm_userInput.input_pickup_dd.value) > 31)
		{ alert("Please enter Day of 'Pickup Date' correctly. It must be a number"); return false; }
	else if (frm_userInput.input_pickup_mm.value == "" ) { alert("Please enter Month of 'Pickup Date'."); return false; }
	else if (parseInt(frm_userInput.input_pickup_mm.value) < 1 || parseInt(frm_userInput.input_pickup_mm.value) > 12)
		{ alert("Please enter Month of 'Pickup Date' correctly. Example: entre June not 06"); return false; }
	else if (frm_userInput.input_pickup_yyyy.value == "") { alert("Please enter Year of 'Pickup Date'."); return false; }
	else if (parseInt(frm_userInput.input_pickup_yyyy.value) < 2006 || parseInt(frm_userInput.input_pickup_yyyy.value) > 2020)
		{ alert("Please enter Year of 'Pickup Date' correctly. Must be 4 figures."); return false; }
		
	else if (frm_userInput.input_pickup_min.value == "") { alert("Please enter Minute of 'Pickup Time'."); return false; }
	else if (parseInt(frm_userInput.input_pickup_min.value) < 0 || parseInt(frm_userInput.input_pickup_min.value) > 59)
		 { alert("Please enter Minutes of 'Pickup Time' correctly."); return false; }	
	else if (frm_userInput.input_pickup_hr.value == "") { alert("Please enter Hour of 'Pickup Time'."); return false; }
	else if (parseInt(frm_userInput.input_pickup_hr.value) < 0 || parseInt(frm_userInput.input_pickup_hr.value) > 23)
		 { alert("Please enter Hour of 'Pickup Time' correctly."); return false; }	

	if (!frm_userInput.input_pickdrop.checked) { alert("Please tell the type of your journey.\nWhether it would be 'Pickup-Drop off' or 'Return' also."); return false; }	
	//if (!frm_userInput.input_pickdrop_return.checked)
	
	if (frm_userInput.input_totPassengers.value == "") { alert("Please enter 'Number of Passengers'."); return false; }	
	if (frm_userInput.select_luggageType.selectedIndex == 0) { alert("Please select 'Type of Luggage'."); return false; }	
	if (frm_userInput.input_luggage == "") { alert("Please enter 'Amount of Luggage'."); return false; }

	if (frm_userInput.select_payMode.selectedIndex == 0) { alert("Please select 'How do you wish to pay'.?"); return false; }
	
	//if (frm_userInput.textarea_comments.value = "") { alert("Please enter 'Comments/Special Requirements'."); return false; }
	
	//if (frm_userInput.input_companyName.value == "") { alert("Please enter your company name."); return false; }
	if (frm_userInput.input_custName.value == "") { alert("Please enter your name."); return false; }
	else if (frm_userInput.input_custSurname.value == "") { alert("Please enter your surname."); return false; }
	else if (frm_userInput.input_custContactNo.value == "") { alert("Please enter your contact number."); return false; }
	else if (frm_userInput.input_custEmail.value == "") { alert("Please enter your email address.");	return false; }
		
	if (frm_userInput.select_hearAbout.selectedIndex == 0) { alert("Please select 'How did you hear about us'.?"); return false; }		
		
	//Validating Email Address	
	str = frm_userInput.input_custEmail.value;
	
	if (!(
			str.substring(0,0) != "@" &&				//first character should not be an @ sign
			str.indexOf("@") > 0 &&						//@ should appear (atleast after one character)
			str.indexOf("@") == str.lastIndexOf("@") &&	//@ should appear only once
		    str.indexOf(".") > str.indexOf("@") + 1	&&	//. should appear after @ (and there should be atleast one character in b/w)
		    str.lastIndexOf(".") < str.length-1	&&		//there should be alteast one character after last .
		    str.length >= 10							//email address should should have minimum length of 10 characters
		)) {
		
		alert("Please Enter a valid email address.");
		
		return false;
	}
	
	
	return true;
}
