// JavaScript Document
var j = jQuery.noConflict();
var prova="";
var citya="";
var areaa="";
j(function(){
    j.get("/address.xml",function(xml){
            j("body").data("xml",xml);
            j("#province_city_area").append("<select id='shenga'></select><select id='shia'></select><select id='qua'></select>");
            j("province",xml).each(function(){
                if(j(this).attr("province")==prova)
                    j("#shenga").append("<option value="+j(this).attr("province")+" selected>"+j(this).attr("province")+"</option>");
                else
                j("#shenga").append("<option value="+j(this).attr("province")+">"+j(this).attr("province")+"</option>");
            });//初始省

            LoadCity(j("#shenga").val().substr(0,3),xml); //初始市 
            LoadArea(j("#shia").val().substr(0,4),xml); //初始区
            
            j("#shenga").change(function(){
                j("#shia").empty();
                var Pvalue = j(this).val().substr(0,3);
                if(j("province[province^="+Pvalue+"] City",xml).length==0){
                    j("#shia").hide();
                    j("#qua").remove();
                    return;
                }else
                    j("#shia").show();
                LoadCity(Pvalue,xml);
                LoadArea(j("#shia").val().substr(0,4),xml);
            });
            
            j("#shia").change(function(){
                LoadArea(j(this).val().substr(0,4),xml);
            });
        }
    ); 
    
    function LoadCity(Pvalue,xml){
        j("#shia").empty();
        j("province[province^="+Pvalue+"] City",xml).each(function(){
            if(j(this).attr("City")==citya)
                j("#shia").append("<option value="+j(this).attr("City")+" selected>"+j(this).attr("City")+"</option>");
            else
            j("#shia").append("<option value="+j(this).attr("City")+">"+j(this).attr("City")+"</option>");
        });
    } 
    
    function LoadArea(Cvalue,xml){
        j("#qua").remove();
        j("#shia").after("<div style='float:left;'><select id=qua name=qua></select></div>");
        j("City[City^="+Cvalue+"] Piecearea",xml).each(function(){
            if(j(this).attr("Piecearea")==areaa)
                j("#qua").append("<option value="+j(this).attr("Piecearea")+" selected>"+j(this).attr("Piecearea")+"</option>");
            else
            j("#qua").append("<option value="+j(this).attr("Piecearea")+">"+j(this).attr("Piecearea")+"</option>");
        });
    }             
});

j.extend({
    Jprova: function(provalue){
        if(provalue==null)
            return j("#shenga option:selected").text();
        else{
            prova = provalue;
            j("#shenga option").each(function(){
                if(j(this).text()==provalue){
                    j(this).attr("selected",true);
                    j("#shenga").trigger("change");
                }
            });
        }    
    },
    Jcitya: function(cityvalue){
        if(cityvalue==null)
            return j("#shia option:selected").text();
        else{
            citya = cityvalue;
            j("#shia option").each(function(){
                if(j(this).text()==cityvalue){
                    j(this).attr("selected",true);
                    j("#shia").trigger("change");
                }
            });
        }
    },
    Jareaa: function(areavalue){
        if(areavalue==null)
            return j("#qua option:selected").text();
        else{
            areaa = areavalue;
            j("#qua option").each(function(){
                if(j(this).text()==areavalue){
                    j(this).attr("selected",true);
                }
            });
        }
    }
});