﻿        function MenuItemClick(obj){
            var cItem = $(".menu .item_light");
            cItem.removeClass();
            cItem.addClass("item");
            $(obj).removeClass();
            $(obj).addClass("item_light");
        }
        
        function TabItemClick(obj){
            var cItem = $(obj).parent(".tab").find(".item_light");
            if(cItem != null){
                cItem.removeClass("item_light");
                cItem.addClass("item");
            }
            $(obj).removeClass("item");
            $(obj).addClass("item_light");
            
            var link = $(obj).attr("url");
            jQuery.ajax({
                type: "GET",
                url: link,
                cache: false,
                success: function(xml) {
                    $(obj).parent(".tab").next("div").html($(xml).find("#tracuu").html());
                    jQuery("input[id*='txtDate']").datepicker(jQuery.datepicker.regional['vi']);
                    StationOutChange();
                }
            });
        }
        $(document).ready(function(){
            $(".menu .item").click(function(){
                MenuItemClick($(this));
            });
            //MenuItemClick($(".menu .item:nth-child(1)"));
            
            $(".tab .item").click(function(){
                TabItemClick($(this));
            });
            if($(".tab .item:nth-child(1)").length > 0) TabItemClick($(".tab .item:nth-child(1)"));
        });
        
        function GotoTop(){
            jQuery(document).scrollTop(0);
        }
        
        function LoadGiaVe(){
            var trainline = jQuery("select[id*='ddlTrainLine']").attr("value");
            var train = jQuery(".train").attr("value");
            var date = jQuery("input[id*='txtDate']").attr("value");
            var stationout = jQuery("select[id*='ddlStationOut']").attr("value");
            var stationin = jQuery("select[id*='ddlStationIn']").attr("value");
            var seat = jQuery("select[id*='ddlSeat']").attr("value");
            var link = "/DesktopModules/TrainTimeAndFare/giave.aspx";
            var str_query = "type=bind&date=" + date + "&trainline=" + trainline + "&train=" + train + "&stationin=" + stationin + "&stationout=" + stationout + "&seat=" + seat;
            
            if(train == "0" && seat == ""){
                alert("Quý khách chú ý: Không thể chọn đồng thời Mã tàu và Loại chỗ là \"Tất cả\"");
                return false;
            }
            
            var direction = "hnout";
            if(jQuery("select[id*='ddlStationOut'] option").index($("select[id*='ddlStationOut'] option:selected")) != 0){
                direction = "hnin";
            }
            
            jQuery.ajax({
                type: "POST",
                url: link,
                data: str_query,
                cache: false,
                success: function(xml) {
                    jQuery("#content").html(jQuery(xml).find("#content").html());
                    jQuery("#content tr td:nth-child(1)").css({"text-align":"left", "font-weight":"bold"});
                    if(direction=="hnout") jQuery("#content tr th:nth-child(1)").text("TÊN GA ĐẾN \\ MÁC TÀU");
                    else if(direction == "hnin" && trainline=="1") jQuery("#content tr th:nth-child(1)").text("GA ĐI - GA ĐẾN \\ TÀU");
                    else jQuery("#content tr th:nth-child(1)").text("GA ĐI \\ TÀU");
                    jQuery("#content tr").each(function(idx){
                        if(jQuery(this).find("td:nth-child(1)").text().length == 1 && idx > 0) jQuery(this).remove();
                        else{
                            if(direction == "hnin" && trainline=="1") jQuery(this).find("td:nth-child(1)").text(jQuery(this).find("td:nth-child(1)").text() + " - HÀ NỘI");
                        }
                    });
                }
            });
            return false;
        }
        
        function LoadGioTau(){
            var trainline = jQuery("select[id*='ddlTrainLine']").attr("value");
            var train = jQuery(".train").attr("value");
            var date = jQuery("input[id*='txtDate']").attr("value");
            var stationout = jQuery("select[id*='ddlStationOut']").attr("value");
            var stationin = jQuery("select[id*='ddlStationIn']").attr("value");
            var direction = "hnout";
            if(jQuery("select[id*='ddlStationOut'] option").index($("select[id*='ddlStationOut'] option:selected")) != 0){
                direction = "hnin";
            }
            var station = direction == "hnout" ? stationin : stationout;
            var link = "/DesktopModules/TrainTimeAndFare/giotau.aspx";
            var str_query = "type=bind&date=" + date + "&trainline=" + trainline + "&train=" + train + "&station=" + station + "&direction=" + direction;
            
            jQuery.ajax({
                type: "POST",
                url: link,
                data: str_query,
                cache: false,
                success: function(xml) {
                    jQuery("#content").html(jQuery(xml).find("#content").html());
                    jQuery("#content tr td:nth-child(1)").css({"text-align":"left", "font-weight":"bold"});
                    jQuery("#content tr").each(function(){
                        jQuery(this).find("td").each(function(idx){
                            if(idx > 0){
                                jQuery(this).text(jQuery(this).text().replace(",","."));
                            }
                        });
                    });
                    jQuery("#content tr th:nth-child(1)").text("TÊN GA \\ MÁC TÀU");
                }
            });
            return false;
        }
        
        function StationOutChange(){
            var obj = "select[id*='ddlStationOut']";
            var objin = "select[id*='ddlStationIn']";
            var stationout = jQuery.trim(jQuery(obj).attr("value"));
            jQuery(objin).children().remove();
            if(jQuery("select[id*='ddlStationOut'] option").index($("select[id*='ddlStationOut'] option:selected")) == 0){
                //stationout == "HÀ NỘI" || 
                jQuery(obj).find("option").each(function(idx){
                    if(idx != 0){
                        //jQuery.trim(jQuery(this).attr("value")) != "HÀ NỘI"
                        jQuery(objin).append(jQuery(this).clone());
                    }
                });
            }
            else{
                jQuery(objin).append(jQuery(obj + " option:nth-child(1)").clone());
                //jQuery(objin).append("<option value='HÀ NỘI'>HÀ NỘI</option>");
            }
            jQuery(objin).find("option:last-child").attr("selected","selected");
        }
        
        function StationOutChangeGioTau(){
            StationOutChange();
            var trainline = jQuery("select[id*='ddlTrainLine']").attr("value");
            var direction = jQuery("select[id*='ddlStationOut'] option").index($("select[id*='ddlStationOut'] option:selected")) == 0 ? "hnout" : "hnin";
            var link = "/DesktopModules/TrainTimeAndFare/giotau.aspx";
            var str_query = "type=trainlinechange&trainline=" + trainline + "&direction=" + direction;
            jQuery(".train").children().remove();
            jQuery(".train").append("<option value=\"\">--- Tất cả ---</option>");
            
            jQuery.ajax({
                type: "POST",
                url: link,
                data: str_query,
                cache: false,
                success: function(xml) {
                    var str_train = jQuery(xml).find("span[id*='lbTrain']").html();
                    if(str_train != ""){
                        var train_array = str_train.split(",");
                        var train_value;
                        for(i=0; i<train_array.length; i++){
                            train_value = train_array[i].split("|");
                            if(train_value.length == 2){
                                jQuery(".train").append("<option value=\"" + train_value[1] + "\">" + train_value[0] + "</option>");
                            }
                        }
                    }
                    
                },
              error: function(request,status,errorThrown){
                    alert(errorThrown);
              }
            });
        }
        
        function TrainLineChange(){
            var trainline = jQuery("select[id*='ddlTrainLine']").attr("value");
            var stationin = jQuery("select[id*='ddlStationIn']");
            var link = "/DesktopModules/TrainTimeAndFare/giave.aspx";
            var str_query = "type=trainlinechange&trainline=" + trainline;
            
            jQuery.ajax({
                type: "POST",
                url: link,
                data: str_query,
                cache: false,
                success: function(xml) {
                    var str_train = jQuery(xml).find("span[id*='lbTrain']").html();
                    var str_station = jQuery(xml).find("span[id*='lbStation']").html();
                    var str_seat = jQuery(xml).find("span[id*='lbSeat']").html();
                    jQuery(".train").children().remove();
                    jQuery(".train").append("<option value=\"0\">--- Tất cả ---</option>");
                    if(str_train != ""){
                        var train_array = str_train.split(",");
                        var train_value;
                        for(i=0; i<train_array.length; i++){
                            train_value = train_array[i].split("|");
                            if(train_value.length == 2){
                                jQuery(".train").append("<option value=\"" + train_value[1] + "\">" + train_value[0] + "</option>");
                            }
                        }
                    }
                    jQuery("select[id*='ddlStationOut']").children().remove();
                    jQuery("select[id*='ddlStationOut']").append("<option value=\"HÀ NỘI\">HÀ NỘI</option>");
                    if(str_station != ""){
                        var station_array = str_station.split(",");
                        for(i=0; i<station_array.length; i++){
                            jQuery("select[id*='ddlStationOut']").append("<option value=\"" + station_array[i] + "\">" + station_array[i] + "</option>");
                        }
                        StationOutChange();
                    }
                    jQuery("select[id*='ddlSeat']").children().remove();
                    jQuery("select[id*='ddlSeat']").append("<option value=\"\">--- Tất cả ---</option>");
                    if(str_seat != ""){
                        var seat_array = str_seat.split(",");
                        for(i=0; i<seat_array.length; i++){
                            jQuery("select[id*='ddlSeat']").append("<option value=\"" + seat_array[i] + "\">" + seat_array[i] + "</option>");
                        }
                    }
                }
            });
        }
        
        function TrainLineChangeGioTau(){
            var trainline = jQuery("select[id*='ddlTrainLine']").attr("value");
            var stationin = jQuery("select[id*='ddlStationIn']");
            var link = "/DesktopModules/TrainTimeAndFare/giotau.aspx";
            var str_query = "type=trainlinechange&trainline=" + trainline;
            jQuery(".train").children().remove();
            jQuery(".train").append("<option value=\"\">--- Tất cả ---</option>");
            jQuery("select[id*='ddlStationOut']").children().remove();
            //jQuery("select[id*='ddlStationOut']").append("<option value=\"HÀ NỘI\">HÀ NỘI</option>");
            jQuery("select[id*='ddlStationIn']").children().remove();
            
            jQuery.ajax({
                type: "POST",
                url: link,
                data: str_query,
                cache: false,
                success: function(xml) {
                    var str_train = jQuery(xml).find("span[id*='lbTrain']").html();
                    var str_station = jQuery(xml).find("span[id*='lbStation']").html();
                    
                    if(str_train != ""){
                        var train_array = str_train.split(",");
                        var train_value;
                        for(i=0; i<train_array.length; i++){
                            train_value = train_array[i].split("|");
                            if(train_value.length == 2){
                                jQuery(".train").append("<option value=\"" + train_value[1] + "\">" + train_value[0] + "</option>");
                            }
                        }
                    }
                    
                    if(str_station != ""){
                        var station_array = str_station.split(",");
                        for(i=0; i<station_array.length; i++){
                            jQuery("select[id*='ddlStationOut']").append("<option value=\"" + station_array[i] + "\">" + station_array[i] + "</option>");
                        }
                        StationOutChange();
                    }
                },
              error: function(request,status,errorThrown){
                    alert(errorThrown);
              }
            });
        }
        
        function ZoomPriceTable(){
            if(jQuery(".tbl_search").length > 0){
                jQuery("#pnlZoomContent").children().remove();
                jQuery("#pnlZoomContent").append(jQuery(".tbl_search").clone());
                jQuery("#pnlZoom").show("slow");
            }
        }
        
        function Print(){
            //ZoomPriceTable();
            jQuery(".tbl_search").printElement();
        }
