﻿ //==========================================
 // create by cjp 2009-01
 // 省-市-县三级联动
 //==========================================
function listArea(id)
{
    var j=1;
    document.getElementById(id).options[0]= new Option("省份", "");
    for(var i=0;i<Area.length;i++){
        if(Area[i][1].length==2)
        {
            document.getElementById(id).options[j]= new Option( Area[i][0], Area[i][1]);            j++;
        }
    }
}

 function listprovince(provinceid)
    {
        document.getElementById(provinceid).length=1;
        document.getElementById(provinceid).options[0]= new Option("省份", "");
        for(var i=1;i<Area.length;i++){
            if(Area[i][1].length==2)
            {
                document.getElementById(provinceid).options[document.getElementById(provinceid).length]= new Option( Area[i][0], Area[i][1]);
            }
        }        
    }
 
function changecity(provinceid,cityid,Districtid)
{        
    document.getElementById(cityid).options[0]= new Option("请选择", ""); 
    if(document.getElementById(provinceid).value!="")
    {
        document.getElementById(cityid).value="";
        document.getElementById(cityid).style.display="block";
        document.getElementById(cityid).length=1;
        for(var i=1;i<Area.length;i++)
        {
            if(document.getElementById(provinceid).value==Area[i][1].substring(0,2) && Area[i][1].length==4)
            {                    
                document.getElementById(cityid).options[document.getElementById(cityid).length]= new Option( Area[i][0], Area[i][1]);
            }
        }             
        document.getElementById(Districtid).value=""; 
        changeDistrict(cityid,Districtid); 
    }
    else
    {
        document.getElementById(cityid).value="";
        document.getElementById(Districtid).value="";
        document.getElementById(cityid).style.display="none";
        document.getElementById(Districtid).style.display="none";
    }
}

function changeDistrict(cityid,Districtid)
{  
    document.getElementById(Districtid).options[0]= new Option("请选择", ""); 
    if(document.getElementById(cityid).value!="")
    {
        document.getElementById(Districtid).style.display="block";
        document.getElementById(Districtid).length=1;
        for(var i=1;i<Area.length;i++)
        {
            if(document.getElementById(cityid).value==Area[i][1].substring(0,4) && Area[i][1].length==6)
            {                    
                document.getElementById(Districtid).options[document.getElementById(Districtid).length]= new Option( Area[i][0], Area[i][1]);
            }
        }
    }
    else
    {
         document.getElementById(Districtid).value="";
        document.getElementById(Districtid).style.display="none";
    }
}


function changecity2(provinceid,cityid)
{        
    //document.getElementById(cityid).options[0]= new Option("请选择", ""); 
    if(document.getElementById(provinceid).value!="")
    {
        $("#"+cityid+"").show(); /*如果选择了省份 这里先显示市下拉框*/
        $("#"+cityid+"").empty();  /*清空上一次绑定数据*/
        document.getElementById(cityid).options[0]= new Option("请选择", ""); 
        for(var i=1;i<Area.length;i++)
        {
            if(document.getElementById(provinceid).value==Area[i][1].substring(0,2) && Area[i][1].length==4)
            {                    
                document.getElementById(cityid).options[document.getElementById(cityid).length]= new Option( Area[i][0], Area[i][1]);
            }
        }             
    }
    else
    {
        document.getElementById(cityid).value="";
        document.getElementById(cityid).style.display="none";
    }
}

function bindArea(code, provinceid, cityid) {
    if (code == null || code == "") {
        return;
    }
    else {
        if (code.toString().length == 2) {
            var _province = document.getElementById(provinceid).options;
            for (var i = 0; i < _province.length; i++) {
                if (_province[i].value == code) {
                    document.getElementById(provinceid).options[i].selected = true;
                }
            }
            $("#"+cityid+"").hide();
        }
        if (code.toString().length == 4) {
            var _province = document.getElementById(provinceid).options;
            for (var i = 0; i < _province.length; i++) {
                if (_province[i].value == code.toString().substring(0, 2)) {
                    document.getElementById(provinceid).options[i].selected = true;
                }
            }
            changecity2(provinceid, cityid);
            var _city = document.getElementById(cityid).options;
            for (var i = 0; i < _city.length; i++) {
                if (_city[i].value == code.toString())
                    document.getElementById(cityid).options[i].selected = true;

            }
        }     
    }
}
