  $(document).ready(function(){

	$("#example").dialog({autoOpen:false});	
    
    $("#qfs").submit(function() {
	
	//Define Date
	var date = new Date();
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
	var m = date.getMonth() + 1;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	
	currdate = day + "/" + month + "/" + year;
	
	//Passenger Numbers
	var adults = $("#NSA").val();
	var children = $("#NSC").val();
	var infants = $("#NSI").val();
	//Determine number of dependants
	var minors = parseInt(infants)+parseInt(children);
	//determine number of carer people
	var responsible = parseInt(adults)+parseInt(adults);
	
	//Outbound Date stuff
	var outbounddatefull = $("#outbounddate").val();
	var brokenoutbound = outbounddatefull.split("/");
	var outbounddate = brokenoutbound[0];
	if (outbounddate<10)
	  {
	  	var outbounddate = "0"+x;
	  }
	var outboundmonth = brokenoutbound[1];
	var outboundyear = brokenoutbound[2];
	
	//Outbound Date stuff
	var inbounddatefull = $("#inbounddate").val();
	var brokeninbound = inbounddatefull.split("/");
	var inbounddate = brokeninbound[0];
	if (inbounddate<10)
	  {
	  	var inbounddate = "0"+x;
	  }
	var inboundmonth = brokeninbound[1];
	var inboundyear = brokeninbound[2];
	
	
	var departure = $("#departure").val();
	var destination = $("#destination").val();
	
	var throwto = $("#throwto").val();
	
	var fade = 5000;
	var error = false;
	
	validation0();
	
	function validation0(){
		if (adults < 1){
			$(("div#example")).text("Invalid number of adults selected!").show();
			error = true;
		}else{
			validation1();
		}
	}
	
	function validation1(){
		if (outbounddatefull == ""){
			$W.text("No outbound date selected").show();
			error = true;
		}else{
			validation2();
		}
	}
	
	function validation2(){
		if (inbounddatefull == ""){
			$((("div#example"))).text("No inbound date selected").show();
			error = true;
		}else{
			validation3();
		}
	}
	
	function validation3(){
		if (outboundyear == year){
			if (month == outboundmonth){
				if (outbounddate < day) {
					$((("div#example"))).text("Outbound date cannot be before today's date!").show();
					error = true;
				}else{
					validation3a();
				}
			}
			if (outboundmonth < month){
				$((("div#example"))).text("Outbound date cannot be before today's date!").show();
				error = true;
			}else{
				validation3a();
			}
		}
	}
	
	function validation3a(){
		if (inboundyear == outboundyear){
			if (inboundmonth == outboundmonth){
				if (inbounddate < outbounddate) {
					$((("div#example"))).text("Return date cannot be before departure date!").show();
					error = true;
				}else{
					validation4();
				}
			}
			if (inboundmonth < outboundmonth){
				$((("div#example"))).text("Return date cannot be before departure date!").show();
				error = true;
			}else{
				validation4();
			}
		}	
	}
	
	function validation4(){
		if (departure == ""){
			$((("div#example"))).text("No departing airport selected!").show();
			error = true;
		}else{
			validation5();
		}
	}
	
	/*function validation4a(){
		for ( var i=0, len=departures.length; i<len; ++i ){
			if (departures[i] == departure) {
				validation5();
			}
		}
			
				$((("div#example"))).text("Invalid Departure Point selected.").show();
				var error = true;
	}*/
	
	function validation5(){
		if (destination == ""){
			$((("div#example"))).text("No destination airport selected!").show();
			error = true;
		}else{
			validation6();
		}
	}
	
	function validation6(){
		if (responsible < minors){
			$((("div#example"))).text("You may only have 2 children/infants per adult.").show();
			error = true;
		}else{
			submit();
		}
	}
	
	function submit(){
		if (error == true){
			return false;
		}else{
			$("#qfs").action = throwto;
			$("#qfs").method = "POST";
			$("#qfs").submit();
			return true;
		}
	}
	
	if (error=true){
		showDialog();
	}
		
	function showDialog(){
		$("#example").dialog("open");
		return false;
	}

	
	
	return false;
    });

  });
