$j(function() {

    //Initialize the form controls
    InitializeFormControls();

    //Run this when the departure station changes
    $j("#departStation").change(function() {

        //Declare and init vars
        callType = "getdestinationstations";
		var url = "rail/GetDestinationStations";
        var isCoachStation = $j("#isCoachStation") ? $j("#isCoachStation").val() : "False";
        var stationId = $j(this).find('option:selected').val();
		var packageProviderID = $j("#packageDetailsId").val();

        //Enable and remove all destination stations in the dropdown listbox
        $j("#destinationStation").removeAttr("disabled");
        $j("select#destinationStation option").remove();

        //Clear and disable the Ticket Types dropdown listbox
        DisableTicketTypes();
		DisableProvider();

        //Prepare the connection url
        url += "?sid=" + stationId;
        url += "&ics=" + isCoachStation;
		url += "&pid=" + packageProviderID;

        //Call the required action
        MakeAjaxCall(url);
    });

    //Run this when the destination station changes
    $j("#destinationStation").change(function() {

		callType = "getpackageprovider";
		var url = "rail/GetPackageProvider";
        var stationId = $j(this).find('option:selected').val();
		var destinationStationCode = $j("#departStation").find('option:selected').val();
		var isCoachStation = $j("#isCoachStation") ? $j("#isCoachStation").val() : "False";
		var packageProviderID = $j("#packageDetailsId").val();

		$j("#providerId").removeAttr("disabled");
		$j("select#providerId option").remove();
		
		DisableTicketTypes();

        //Prepare the connection url
        url += "?sid=" + stationId;
        url += "&dsid=" + destinationStationCode;
		url += "&pid=" + packageProviderID;
		url += "&ics=" + isCoachStation;

        //Call the required action
        MakeAjaxCall(url);
    });
	
	$j("#providerId").change(function() {
		//Declare and init vars
        callType = "gettickettypes";
		var url = "rail/GetTicketTypes";
		var stationId = $j('#destinationStation').find('option:selected').val();
		var packageDetailsId = $j('#providerId').find('option:selected').val();
		
		//Enable and remove all ticket types in the dropdown listbox
        $j("#TicketId").removeAttr("disabled");
        $j("select#TicketId option").remove();
		
		//Prepare the connection url
        url += "?sid=" + stationId;
        url += "&pid=" + packageDetailsId;

        //Call the required action
        MakeAjaxCall(url);
	});

    /* gradual form - show steps 2 and 3 after step 1 has been completed */
    $j("#TicketId").change(function() {	
		$j("#ftDateRow").show("normal");
        $j("#ftGuestsAndStar").show("normal");
    });

    /* form validation - check they have selected a destination and ticket type */
    $j("form#fastTrackForm").submit(function(event) {

        $j("div#errorMsg").remove();

        var errors = new Array(); // create array for missed fields

        //check if departure station has been chosen
        if ($j("#departStation").attr("selectedIndex") == 0) {
            var missed = "Please choose a Departure Station";
            errors.push(missed);
        }

        // check if destination has been chosen
        if ($j("#destinationStation").attr("selectedIndex") == 0) {
            var missed = "Please choose a Destination Station";
            errors.push(missed);
        }
		
        // check if ticket type has been chosen
        if ($j("#TicketId").attr("selectedIndex") == 0) {
            var missed = "Please choose a Ticket type";
            errors.push(missed);
        }

        // check if only one adult is selected and no children
        if ($j("#ftRooms").find('option:selected').html() == "1" && $j("#ftAdults1").find('option:selected').html() == "1" && $j("#ftChildren1").find('option:selected').html() == "0") {
            var missed = "Unfortunately we do not issue tickets for rail bookings where there is just one person travelling.";
            errors.push(missed);
        }

        //if any errors, display error message
        if (errors.length > 0) {
            var errorMsg = "";
            errorMsg += "<div id=\"errorMsg\" class=\"hide\">";
            errorMsg += "<ul>";
            for (i = 0; i < errors.length; i++) {
                errorMsg += "<li>" + errors[i] + "</li>";
            }
            errorMsg += "</ul>";
            errorMsg += "</div>";
            $j("#ftTravelDetailsWrapRail").before(errorMsg);
            $j("div#errorMsg").fadeIn("slow");
            event.stopImmediatePropagation();
			return false;
        }

        return true; // return true if no errors
    });
});
