﻿$(function () {
    BindCity();
//    $.ajax({
//        async: false,
//        type: "POST",
//        contentType: "application/json;utf-8",
//        url: "/Index.aspx/BindInfo",
//        success: function (data) {
//            alert(data.d);
//            if (data != "") {
//                if (data.d.split(",")[0] != '')
//                    $("#arrcity").val(data.d.split(",")[0]);
//                if (data.d.split(",")[1] != '')
//                    $("#city2").val(data.d.split(",")[1]);
//            }

//        },
//        error: function () {
//            Chesudi.Common.Message.show({ id: 'MSG00001' });
//        }
//    });
    $("#arrcity").blur(function () { $("#city2").val($("#arrcity").val()); }); //出还车城市

});

function fixPNG(myImage) {
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
        var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";
        var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";
        var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' ";
        var imgStyle = "display:inline-block;" + myImage.style.cssText;
        var strNewHTML = "<span " + imgID + imgClass + imgTitle

   + " style=\"" + "width:" + myImage.width

   + "px; height:" + myImage.height

   + "px;" + imgStyle + ";"

   + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"

   + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>";
        myImage.outerHTML = strNewHTML;
    } 
} 



//添加到收藏夹
function AddFavorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e) {
            Chesudi.Common.Message.show({ id: 'MSG00015' });
        }
    }
}

//绑定城市
function BindCity() {
    var cityid = "1";
    var citys = returnCitySN["cname"];
    if (citys.indexOf("北京") != -1) {
        cityid = "3";
        $("#arrcity ").val("北京");
        $("#city2").val("北京");
        
    }
    else if (citys.indexOf("苏州") != -1) {
        cityid = "4";
        $("#arrcity ").val("苏州");
        $("#city2").val("苏州");
       
    }
    else if (citys.indexOf("广州") != -1) {
        cityid = "5";
        $("#arrcity ").val("广州");
        $("#city2").val("广州");
      
    }
    else if (citys.indexOf("深圳") != -1) {
        cityid = "6";
        $("#arrcity ").val("深圳");
        $("#city2").val("深圳");
       
    }
    else if (citys.indexOf("杭州") != -1) {
        cityid = "7";
        $("#arrcity ").val("杭州");
        $("#city2").val("杭州");
        __doPostBack("rptArea$ctl05$LinkButton1", "");
    }
    else if (citys.indexOf("上海") != -1) {
        cityid = "1";
        $("#arrcity ").val("上海");
        $("#city2").val("上海");
    }
    var city = cityid;
    $("#divarea li[lang='" + city + "']").addClass("cityselected");
    $(".areali").mouseover(function () { $(this).addClass("cityselected").siblings().removeClass("cityselected"); });
    $("#ddl_takehours").change(function () {
        $("#ddl_returnhours").val($("#ddl_takehours").val());
    });
    $("#arrcity").blur(function () {
        $("#city2").val($("#arrcity").val())
    });

}

//预定特惠车
function BookSpecialCar(carId, areId) {
    $.ajax({
        async: false,
        type: "POST",
        contentType: "application/json;utf-8",
        url: "/Index.aspx/BookSpecialCar",
        data: "{carId:'" + carId + "',areId:'" + areId + "'}",
        success: function (data) {
            if (data.d == "请完善个人信息") {
                Chesudi.Common.Message.show({ id: 'MSG00016' });
                window.location.href = "/UserMember/InformationManagement.aspx";
            }
            else
                window.location.href = data.d;
        },
        error: function () {
            Chesudi.Common.Message.show({ id: 'MSG00001' });
        }
    });
}
function hrefPage() {
    window.location.href = "/UserMember/InformationManagement.aspx";
  
}
//预定
function BookCar(takeTime, returnTime, takeCity, returnCity) {
    var takeHours = $("#ddl_takehours").val();
    var returnHours = $("#ddl_returnhours").val();
    $.ajax({
        async: false,
        type: "POST",
        contentType: "application/json;utf-8",
        url: "/Index.aspx/BookCar",
        data: "{takeCity:'" + takeCity + "',returnCity:'" + returnCity + "',takeTime:'" + takeTime + "',returnTime:'" + returnTime + "',takeHours:'" + takeHours + "',returnHours:'" + returnHours + "'}",
        success: function (data) {
            if (data.d != "") {
                if (data.d == "请完善个人信息") {
                    Chesudi.Common.tip.confirm({ id: "DvNewCellDel", title: "", left: "35%", top: "350px", msg: "为了保护您的会员权益，现场取车需要会员本人携带第二代身份证，请您填写身份证信息！", func: hrefPage });
                }
                else
                    Chesudi.Common.tip.info({ title: '', left: "35%", top: "45%", msg: data.d });
            }
            else {
                window.location.href = "OnlineBooking.aspx?action=orderCar";
            }
        },
        error: function () {
            Chesudi.Common.Message.show({ id: 'MSG00001' });
        }
    });
}

//出还车日期判断
function CheckingDate() {

    $("html").addClass("hb");
    var msg = "";
    var takeCity = $("#arrcity").val();
    var returnCity = $("#city2").val();

    if (takeCity == "" || takeCity == "中文/拼音...") {
//        alert("请输入取车城市!");
        Chesudi.Common.Message.show({ id: 'MSG00017' });
        return;
    }
    if (returnCity == "" || returnCity == "中文/拼音...") {
        Chesudi.Common.Message.show({ id: 'MSG00018' });
        return;
    }
    if (takeCity != returnCity) {
        Chesudi.Common.Message.show({ id: 'MSG00019' });
        return false;
    }
    if ($("#txt_TakeTime").val() == "" || $("#txt_TakeTime").val() == "请选择日期...") {
        Chesudi.Common.Message.show({ id: 'MSG00020' });
        return false;
    }
    if ($("#txt_returnTime").val() == "" || $("#txt_returnTime").val() == "请选择日期...") {
        Chesudi.Common.Message.show({ id: 'MSG00021' });
        return false;
    }    
    if ($("#txt_TakeTime").val() == $("#txt_returnTime").val() && parseInt($("#ddl_takehours").val()) > parseInt($("#ddl_returnhours").val())) {
        Chesudi.Common.Message.show({ id: 'MSG00022' });
        return false;
    }


    var st = $("#txt_TakeTime").val().replace("-", "").replace("-", "");
    var et = $("#txt_returnTime").val().replace("-", "").replace("-", "");

     if (parseInt(et) == 20120121) {
         Chesudi.Common.Message.show({ id: 'MSG00038' });
         return false;
     }

     if (parseInt(st) == 20120128) {
         Chesudi.Common.Message.show({ id: 'MSG00039' });
         return false;
     }

    if (parseInt(st) >= 20120122 && parseInt(st) <= 20120127) {
        Chesudi.Common.Message.show({ id: 'MSG00037' });
        return false;
     }
    if (parseInt(et) >= 20120122 && parseInt(et) <= 20120127) {
        Chesudi.Common.Message.show({ id: 'MSG00037' });
        return false;
    }


    if (parseInt(st) > parseInt(et)) {
        Chesudi.Common.Message.show({ id: 'MSG00022' });
        return false;
    }
    var dt1 = $("#txt_TakeTime").val() + " " + $("#ddl_takehours").val() + ":00:00";
    var dt2 = $("#txt_returnTime").val() + " " + $("#ddl_returnhours").val() + ":00:00";

    var dtnow = new Date();
    if (dtnow.format("yyyy-MM-dd") == $("#txt_TakeTime").val()) {
        if ($("#ddl_takehours").val() - dtnow.getHours() < 0) {
            Chesudi.Common.Message.show({ id: 'MSG00011'});
            return false;
        }
    }
    var datespan = DiffLong(dt2, dt1);
  
    var dsday = datespan.split(',')[1];
   
    if (parseInt(dsday) < 1) {
        Chesudi.Common.Message.show({ id: 'MSG00012' });
        return false;
    }
    if (parseInt(dsday) >= 90) {
        Chesudi.Common.Message.show({ id: 'MSG00013' });
        return false;
    }

    var datespan2 = DiffLong(dt1, dateToStr(dtnow));
    var datespan3 = DiffLong(dt2, dateToStr(dtnow));
    var dsday2 = datespan2.split(',')[1];
    if (parseInt(dsday2) > 30||datespan3.split(',')[1]>30) {
        var endt = strToDate(dtnow.format("yyyy-MM-dd"));
        endt.addDays(30);
        Chesudi.Common.Message.show({ id: 'MSG00007', msge: '' + dtnow.format("yyyy-MM-dd") + '至' + endt.format("yyyy-MM-dd") + '.' });
        return false;
    }


    var $state = true;
    if ($state == true) {
        BookCar($("#txt_TakeTime").val(), $("#txt_returnTime").val(), takeCity, returnCity);
    }
    else {
        return $state;
    }
}


