﻿    var province = $("#sel_province");
    var city = $("#sel_city");
    var area = $("#sel_town");
    var ProvinceValue = 0;
    var CityValue = 0;
    var TownValue = 0;
    function LoadCity() {
        province = $("#sel_province");
        city = $("#sel_city");
        area = $("#sel_town");
        province.html("");
        city.html("");
        area.html("");
        province.append("<option value=0>请选择</option>");
        city.append("<option value=0>请选择</option>");
        area.append("<option value=0>请选择</option>");
        LoadSelProvince();        
        province.change(function () {
            city.html("");
            city.append("<option value=0>请选择</option>");
            area.html("");
            area.append("<option value=0>请选择</option>");
            LoadSelCity();
            ChangeAddressForSelect();
        });
        city.change(function () {
            area.html("");
            area.append("<option value=0>请选择</option>");
            LoadSelDistrict();
            ChangeAddressForSelect();
        });
        area.change(function() {
            ChangeAddressForSelect();
        });
        addressFocus();
        $("#txt_ship_address").focus(function() {
            if ($("#txt_ship_address").val() == "请填写您的详细地址") {
                $("#txt_ship_address").val("");
                $("#txt_ship_address").css({ color: "#000000" });
            }
        });
        $("#txt_ship_address").blur(function() {
            addressFocus();
        });     
        
    }

    function LoadSelProvince(ProvinceValue) {
        $.ajax({
            type: "POST",
            url: "/checkout/GetProvince", // "/?" + Math.random(),
            success: function (data) { func_suc_getXmlProvice(data); },
            error: function () { LoadSelProvince(); }
        });
    }

    function LoadSelCity() {
        $.ajax({
            type: "POST",
            url: "/checkout/GetCity/" + province.val(), // "/?" + Math.random(),
            success: function (data) { func_suc_getXmlCity(data); },
            error: function () { LoadSelCity(); }
        });
    }
    
    function LoadSelDistrict() {
        $.ajax({
            type: "POST",
            url: "/checkout/GetDistrict/" + city.val(), // "/?" + Math.random(),
            success: function (data) { func_suc_getXmlArea(data) },
            error: function () { LoadSelDistrict(); }
        });
    }

    function func_suc_getXmlProvice(xml) {
        var sheng = $(xml).find("CityModel");
        sheng.each(function(i) {
            province.append("<option value=" + sheng.eq(i).find("id").text() + ">" + sheng.eq(i).find("name").text() + "</option>");
        });
        province.val(ProvinceValue);
        LoadSelCity();
    }
    
    
    function func_suc_getXmlCity(xml) {
        var xml_shi = $(xml).find("CityModel");
        var city_html = "";
        xml_shi.each(function(j) {
            city.append("<option  value=" + xml_shi.eq(j).find("id").text() + ">" + xml_shi.eq(j).find("name").text() + "</option>");
        });
        if (province.val() == "0" || province.val() == 0) {
            city.html("");
            area.html("");
            city.append("<option value=0>请选择</option>");
            area.append("<option value=0>请选择</option>");
        }
        city.val(CityValue);
        LoadSelDistrict();
    }
    function func_suc_getXmlArea(xml) {
        var xml_xianqu = $(xml).find("CityModel");
        area.html("");
        xml_xianqu.each(function(i) {
            area.append("<option  value=" + xml_xianqu.eq(i).find("id").text() + ">" + xml_xianqu.eq(i).find("name").text() + "</option>");
        });
        if (city.val() == "0" || city.val() == 0) {
            area.html("");
            area.append("<option value=0>请选择</option>");
        }
        area.val(TownValue);
    }

    function ChangeAddressForSelect() {
        var p = $('#sel_province option:selected').text();
        var c = $('#sel_city option:selected').text();
        var t = $('#sel_town option:selected').text();
        $("#div_country_province_city").html(p + " " + c + " " + t + "&nbsp;&nbsp;");
    }

    function addressFocus() {
        if ($("#txt_ship_address").val() == "") {
            $("#txt_ship_address").val("请填写您的详细地址");
            $("#txt_ship_address").css({ color: "#878787" });
        }

        if ($("#txt_ship_address").val() != "请填写您的详细地址") {
            $("#txt_ship_address").css({ color: "#000000" });
        }
    }    
   

