﻿var prov="";
var city="";
var area="";
$(document).ready(function(){

     $.get("/common/area.xml",function(xml){
            $("body").data("xml",xml);
            $("#province_city_area").append("<select id='_province' runat='server'></select><select id='_city'  runat='server'></select><select id='_area'  runat='server'></select>");
            $("province",xml).each(function(){
                if($(this).attr("province")==prov)
                    $("#_province").append("<option value="+$(this).attr("provinceID")+" selected>"+$(this).attr("province")+"</option>");
                else
                    $("#_province").append("<option value="+$(this).attr("provinceID")+">"+$(this).attr("province")+"</option>");
            });

            LoadCity($("#_province").val().substr(0,3),xml); //初始市 
            LoadArea($("#_city").val().substr(0,4),xml); //初始区
            
            $("#_province").change(function(){
                $("#_city").empty();
                var Pvalue = $(this).val().substr(0,3);
                if($("province[provinceID^="+Pvalue+"] City",xml).length==0){
                    $("#_city").hide();
                    $("#_area").remove();
                    return;
                }else
                    $("#_city").show();
                LoadCity(Pvalue,xml);
                LoadArea($("#_city").val().substr(0,4),xml);
            });
            
            $("#_city").change(function(){
                LoadArea($(this).val().substr(0,4),xml);
            });
        }
    ); 
    
    function LoadCity(Pvalue,xml){
        $("#_city").empty();
        $("province[provinceID^="+Pvalue+"] City",xml).each(function(){
            if($(this).attr("City")==city){
                $("#_city").append("<option value="+$(this).attr("CityID")+" selected>"+$(this).attr("City")+"</option>");}
            else{
                $("#_city").append("<option value="+$(this).attr("CityID")+">"+$(this).attr("City")+"</option>");}
        });
    } 
    
    function LoadArea(Cvalue,xml){
        $("#_area").remove();
        $("#_city").after("&nbsp;<select id=_area></select>");
        $("City[CityID^="+Cvalue+"] Piecearea",xml).each(function(){
            if($(this).attr("Piecearea")==area){
                $("#_area").append("<option selected>"+$(this).attr("Piecearea")+"</option>");}
            else{
                $("#_area").append("<option>"+$(this).attr("Piecearea")+"</option>");}
        });
    }  

//            LoadCity($("#_province").val().substr(0,3),xml); 
//            LoadArea($("#_city").val().substr(0,4),xml); 
//            
//            $("#_province").change(function(){
//                $("#_city").empty();
//                var Pvalue = $(this).val().substr(0,3);
//                if($("province[provinceID^="+Pvalue+"] City",xml).length==0){
//                    $("#_city").hide();
//                    $("#_area").hide();
//                    return;
//                }else{
//                    $("#_city").show();
//                    LoadCity(Pvalue,xml);
//                    $("#_city").trigger("change");
//                    }
//            });
//            
//            $("#_city").change(function(){                
//                var Cvalue = $(this).val().substr(0,4);
//                if($("City[CityID^="+Cvalue+"] Piecearea",xml).length==0){
//                    $("#_area").hide();
//                    return;
//                }else{
//                    $("#_area").show();
//                    LoadArea(Cvalue,xml);
//                    }
//            });
//            
//            $("#_area").change(function(){
//               
//            }); 
//            
//         }
//    ); 
//    
//    
//    function LoadCity(Pvalue,xml){
//        $("#_city").empty();
//        $("province[provinceID^="+Pvalue+"] City",xml).each(function(){
//            if($(this).attr("City")==city)
//                $("#_city").append("<option value="+$(this).attr("CityID")+" selected>"+$(this).attr("City")+"</option>");
//            else
//                $("#_city").append("<option value="+$(this).attr("CityID")+">"+$(this).attr("City")+"</option>");
//        });
//    } 
//    
//    function LoadArea(Cvalue,xml){
//        $("#_area").empty();
//        //$("#_city").after("<select id=_area></select>");
//        $("City[CityID^="+Cvalue+"] Piecearea",xml).each(function(){
//            if($(this).attr("Piecearea")==area)
//                $("#_area").append("<option selected>"+$(this).attr("Piecearea")+"</option>");
//            else
//                $("#_area").append("<option>"+$(this).attr("Piecearea")+"</option>");
//        });
//    }             
});

$.extend({

    pcv: function(){
    var selectvalue=$("#_province option:selected").text() + "|" + $("#_city option:selected").text() + "|" + $("#_area option:selected").text();
        $("#nowpcv").attr("value",selectvalue);
        },
    
    ppcv:function(){

    var selectvalue=$("#_province option:selected").text() + "|" + $("#_city option:selected").text() + "|" + $("#_area option:selected").text();
    $("#ctl00_ContentPlaceHolder1_nowpcv").attr("value",selectvalue);
    },
    
    Jprov: function(provalue){
        if(provalue==null)
            return $("#_province option:selected").text();
        else{
            prov = provalue;
            $("#_province option").each(function(){
                if($(this).text()==provalue){
                    $(this).attr("selected",true);
                    $("#_province").trigger("change");
                }
            });
        }    
    },
    Jcity: function(cityvalue){
        if(cityvalue==null)
            return $("#_city option:selected").text();
        else{
            city = cityvalue;
            $("#_city option").each(function(){
                if($(this).text()==cityvalue){
                    $(this).attr("selected",true);
                    $("#_city").trigger("change");
                }
            });
        }
    },
    Jarea: function(areavalue){
        if(areavalue==null)
            return $("#_area option:selected").text();
        else{
            area = areavalue;
            $("#_area option").each(function(){
                if($(this).text()==areavalue){
                    $(this).attr("selected",true);
                }
            });
        }
    }
})
