var $j = jQuery.noConflict();

$j(function() {
    //Declare and init vars
    var ajaxBaseUrl = $j("#ajaxBaseUrl").val();

    //Run this when the departure station changes
    $j("#departStation").change(function() {

        //Declare and init vars
        CallType = "getdestinationstationsforpartial";
        var url = ajaxBaseUrl + "/rail/GetDestinationStationsForPartial";
        var isCoachStation = $j("#isCoachStation") ? $j("#isCoachStation").val() : "False";
        //        var stationName = $j(this).find('option:selected').text;
        var stationId = $j(this).find('option:selected').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();

        //Prepare the connection url
        url += "?sid=" + stationId;
        url += "&ics=" + isCoachStation;

        //Call the required action
        MakeAjaxCall(url);
    });

    //Run this when the destination station changes
    $j("#destinationStation").change(function() {

        //Declare and init vars
        CallType = "gettickettypesforpartial";
        var url = ajaxBaseUrl + "/rail/GetTicketTypesForPartial";
        //        var stationName = $j(this).find('option:selected').text;
        var stationId = $j(this).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);
    });

    /* form validation - check they have selected a destination and ticket type */
    $j("#fastTrackForm").submit(function() {
        $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" && checkChildren() == false) {
        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);
        }

        //Validate the child ages
        var rooms = $j("#ftRooms").find('option:selected').html();
        var idx = 0;
        for (idx = 1; idx <= parseInt(rooms); idx++) {
            if ($j("#ftChildren" + idx).find('option:selected').html() != "0") {

                var noOfChildren = $j("#ftChildren" + idx).find('option:selected').html();
                var subIndex = 0;
                for (subIndex = 1; subIndex <= parseInt(noOfChildren); subIndex++) {
                    if ($j("#ftChildAges_" + idx + "_" + subIndex).attr('selectedIndex') == 0) {
                        var missed = "Please choose the age of child " + subIndex + " in room " + idx + ".";
                        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("#step1Row").before(errorMsg);
            $j("div#errorMsg").fadeIn("slow");
            return false;
        }

        return true; // return true if no errors
    });

});
