// JavaScript Document
var j = jQuery.noConflict();
var prov="";
var city="";
var area="";
j(function(){
    j.get("/address.xml",function(xml){
            j("body").data("xml",xml);
            j("#province_city_area").append("<span><select id='sheng'></select><select id='shi'></select><select id='qu'></select></span>");
            j("province",xml).each(function(){
                if(j(this).attr("province")==prov)
                    j("#sheng").append("<option value="+j(this).attr("province")+" selected>"+j(this).attr("province")+"</option>");
                else
                j("#sheng").append("<option value="+j(this).attr("province")+">"+j(this).attr("province")+"</option>");
            });//初始省

            LoadCity(j("#sheng").val().substr(0,3),xml); //初始市 
            LoadArea(j("#shi").val().substr(0,4),xml); //初始区
            
            j("#sheng").change(function(){
                j("#shi").empty();
                var Pvalue = j(this).val().substr(0,3);
                if(j("province[province^="+Pvalue+"] City",xml).length==0){
                    j("#shi").hide();
                    j("#qu").remove();
                    return;
                }else
                    j("#shi").show();
                LoadCity(Pvalue,xml);
                LoadArea(j("#shi").val().substr(0,4),xml);
            });
            
            j("#shi").change(function(){
                LoadArea(j(this).val().substr(0,4),xml);
            });
        }
    ); 
    
    function LoadCity(Pvalue,xml){
        j("#shi").empty();
        j("province[province^="+Pvalue+"] City",xml).each(function(){
            if(j(this).attr("City")==city)
                j("#shi").append("<option value="+j(this).attr("City")+" selected>"+j(this).attr("City")+"</option>");
            else
            j("#shi").append("<option value="+j(this).attr("City")+">"+j(this).attr("City")+"</option>");
        });
    } 
    
    function LoadArea(Cvalue,xml){
        j("#qu").remove();
        j("#shi").after("<div style='float:left; margin:0px; padding:0px;'><select id=qu name=qu></select></div>");
        j("City[City^="+Cvalue+"] Piecearea",xml).each(function(){
            if(j(this).attr("Piecearea")==area)
                j("#qu").append("<option value="+j(this).attr("Piecearea")+" selected>"+j(this).attr("Piecearea")+"</option>");
            else
            j("#qu").append("<option value="+j(this).attr("Piecearea")+">"+j(this).attr("Piecearea")+"</option>");
        });
    }             
});

j.extend({
    Jprov: function(provalue){
        if(provalue==null)
            return j("#sheng option:selected").text();
        else{
            prov = provalue;
            j("#sheng option").each(function(){
                if(j(this).text()==provalue){
                    j(this).attr("selected",true);
                    j("#sheng").trigger("change");
                }
            });
        }    
    },
    Jcity: function(cityvalue){
        if(cityvalue==null)
            return j("#shi option:selected").text();
        else{
            city = cityvalue;
            j("#shi option").each(function(){
                if(j(this).text()==cityvalue){
                    j(this).attr("selected",true);
                    j("#shi").trigger("change");
                }
            });
        }
    },
    Jarea: function(areavalue){
        if(areavalue==null)
            return j("#qu option:selected").text();
        else{
            area = areavalue;
            j("#qu option").each(function(){
                if(j(this).text()==areavalue){
                    j(this).attr("selected",true);
                }
            });
        }
    }
});