﻿//==========================================
// 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 listEnprovince(provinceid) {
    document.getElementById(provinceid).length = 1;
    document.getElementById(provinceid).options[0] = new Option("--Choose--", "");
    for (var i = 0; i < Area.length; i++) {
        if (Area[i][1].length == 2) {
            document.getElementById(provinceid).options[document.getElementById(provinceid).length] = new Option(Area[i][2], Area[i][1]);
        }
    }
}

function changeEncity(provinceid, cityid, Districtid) {
    document.getElementById(cityid).options[0] = new Option("--Choose--", "");
    if (document.getElementById(provinceid).value != "") {
        document.getElementById(cityid).value = "";
        document.getElementById(cityid).length = 1;
        for (var i = 0; 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][2], Area[i][1]);
            }
        }
        if (document.getElementById(cityid).length > 1)
            document.getElementById(cityid).style.display = "block";
        if (Districtid != "") {
            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 changeEnDistrict(cityid, Districtid) {
    document.getElementById(Districtid).options[0] = new Option("--Choose--", "");
    if (document.getElementById(cityid).value != "") {
        document.getElementById(Districtid).length = 1;
        for (var i = 0; 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][2], Area[i][1]);
            }
        }
        if (document.getElementById(Districtid).length > 1) {
            document.getElementById(Districtid).style.display = "block";
        }
    }
    else {
        document.getElementById(Districtid).value = "";
        document.getElementById(Districtid).style.display = "none";
    }
}

function bindEnArea(code, provinceid, cityid, Districtid) {
    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;
                }
            }
            document.getElementById(cityid).style.display = "block";
            document.getElementById(Districtid).style.display = "none";
        }
        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;
                }
            }
            changeEncity(provinceid, cityid, Districtid);
            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;

            }
            changeEnDistrict(cityid, Districtid);
            document.getElementById(cityid).style.display = "block";
            document.getElementById(Districtid).style.display = "block";
        }
        if (code.toString().length == 6) {
            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;
                }
            }
            changeEncity(provinceid, cityid, Districtid);
            var _city = document.getElementById(cityid).options;
            for (var i = 0; i < _city.length; i++) {
                if (_city[i].value == code.toString().substring(0, 4))
                    document.getElementById(cityid).options[i].selected = true;

            }
            changeEnDistrict(cityid, Districtid);
            var _district = document.getElementById(Districtid).options;
            for (var i = 0; i < _district.length; i++) {
                if (_district[i].value == code.toString())
                    document.getElementById(Districtid).options[i].selected = true;
            }
            document.getElementById(cityid).style.display = "block";
            document.getElementById(Districtid).style.display = "block";
        }
    }
}

//*******************************************************英文部分结束*******************************************

function listprovince(provinceid) {
    document.getElementById(provinceid).length = 1;
    document.getElementById(provinceid).options[0] = new Option("--请选择--", "");
    for (var i = 0; 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).length = 1;
        for (var i = 0; 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]);
            }
        }
        if (document.getElementById(cityid).length > 1)
            document.getElementById(cityid).style.display = "block";
        if (Districtid != "") {
            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).length = 1;
        for (var i = 0; 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]);
            }
        }
        if (document.getElementById(Districtid).length > 1) {
            document.getElementById(Districtid).style.display = "block";
        }
    }
    else {
        document.getElementById(Districtid).value = "";
        document.getElementById(Districtid).style.display = "none";
    }
}

function bindArea(code, provinceid, cityid, Districtid) {
    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;
                }
            }
            document.getElementById(cityid).style.display = "block";
            document.getElementById(Districtid).style.display = "none";
        }
        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;
                }
            }
            changecity(provinceid, cityid, Districtid);
            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;

            }
            changeDistrict(cityid, Districtid);
            document.getElementById(cityid).style.display = "block";
            document.getElementById(Districtid).style.display = "block";
        }
        if (code.toString().length == 6) {
            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;
                }
            }
            changecity(provinceid, cityid, Districtid);
            var _city = document.getElementById(cityid).options;
            for (var i = 0; i < _city.length; i++) {
                if (_city[i].value == code.toString().substring(0, 4))
                    document.getElementById(cityid).options[i].selected = true;

            }
            changeDistrict(cityid, Districtid);
            var _district = document.getElementById(Districtid).options;
            for (var i = 0; i < _district.length; i++) {
                if (_district[i].value == code.toString())
                    document.getElementById(Districtid).options[i].selected = true;
            }
            document.getElementById(cityid).style.display = "block";
            document.getElementById(Districtid).style.display = "block";
        }
    }
}

//获得地区名称
function GetAddressName(code) {
    var temp = "";
    if (code != "") {
        for (var i = 0; i < Area.length; i++) {
            if (Area[i][1] == code) {
                temp = Area[i][0];
                break;
            }
        }
    }
    document.write(temp);
}


//获得地区名称
function GetEnAddressName(code) {
    var temp = "";
    if (code != "") {
        for (var i = 0; i < Area.length; i++) {
            if (Area[i][1] == code) {
                temp = Area[i][2];
                break;
            }
        }
    }
    document.write(temp);
}


//所在地
function BindCity(code, id, id2, id3) {
    listprovince(id); //加载省份列表
    if (code.toString().length == 6) {
        $("#" + id).val(code.toString().substring(0, 2));
        changecity(id, id2, id3);
        $("#" + id2).val(code.toString().substring(0, 4));
        changeDistrict(id2, id3);
        $("#" + id3).val(code.toString());
    }
    else if (code.toString().length == 4) {
        $("#" + id).val(code.toString().substring(0, 2));
        changecity(id, id2, id3);
        $("#" + id2).val(code);
        changeDistrict(id2, id3);
    }
    else if (code.toString().length == 2) {
        $("#" + id).val(code);
        changecity(id, id2, id3);
    }
}
function GetCityName(code) {
    var temp = "";
    if (code != "") {
        if (code.toString().length == 6) {
            for (var i = 0; i < Area.length; i++) {
                if (Area[i][1] == code.toString().substring(0, 2)) {
                    temp = Area[i][0];
                }
                if (Area[i][1] == code.toString().substring(0, 4)) {
                    temp += "-" + Area[i][0];
                }
                if (Area[i][1] == code) {
                    temp += "-" + Area[i][0];
                }
            }
        }
        else if (code.toString().length == 4) {
            for (var i = 0; i < Area.length; i++) {
                if (Area[i][1] == code.toString().substring(0, 2)) {
                    temp = Area[i][0];
                }
                if (Area[i][1] == code) {
                    temp += "-" + Area[i][0];
                }
            }
        }
        else {
            for (var i = 0; i < Area.length; i++) {
                if (Area[i][1] == code) {
                    temp = Area[i][0];
                    break;
                }
            }
        }
    }
    return temp;
}

//根据城市编号来写出对应的城市名称    2009-10-16  by wby
function WriteCityName(code) {
    var temp = "";
    if (code != "") {
        if (code.toString().length == 6) {
            for (var i = 0; i < Area.length; i++) {
                if (Area[i][1] == code.toString().substring(0, 2)) {
                    temp = Area[i][0];
                }
                if (Area[i][1] == code.toString().substring(0, 4)) {
                    temp += "-" + Area[i][0];
                }
                if (Area[i][1] == code) {
                    temp += "-" + Area[i][0];
                }
            }
        }
        else if (code.toString().length == 4) {
            for (var i = 0; i < Area.length; i++) {
                if (Area[i][1] == code.toString().substring(0, 2)) {
                    temp = Area[i][0];
                }
                if (Area[i][1] == code) {
                    temp += "-" + Area[i][0];
                }
            }
        }
        else {
            for (var i = 0; i < Area.length; i++) {
                if (Area[i][1] == code) {
                    temp = Area[i][0];
                    break;
                }
            }
        }
    }
    document.write(temp);
}

