﻿function AreaSelect(formName, provCtrlName, cityCtrlName, areaCtrlName, controlClientID) {
    this.selectProvinceTitle = "中国";
    this.selectCityTitle = "市";
    this.selectAreaTitle = "区";
    this.formName = formName;
    this.provCtrlName = provCtrlName;
    this.cityCtrlName = cityCtrlName;
    this.areaCtrlName = areaCtrlName;
    this.controlClientID = controlClientID;

    this.GetProvID = function() {
        var selectValue = document.getElementById(controlClientID + '_city_hid').value;
        if (selectValue.length == 6) {
            selectValue = selectValue.substring(0, 2) + "0000";
        }
        return parseInt(selectValue);
    }
    this.SetValue = function(id) {
        return document.getElementById(controlClientID + '_city_hid').value = id;
    }
    this.GetCityID = function() {
        var selectValue = document.getElementById(controlClientID + '_city_hid').value;
        if (selectValue.length == 6) {
            selectValue = selectValue.substring(0, 4) + "00";
        }
        return parseInt(selectValue);
    }
    
    this.GetAreaID = function() {
        return document.getElementById(controlClientID + '_city_hid').value;
    }
    
    this.InitControl = function() {
        var prov_id = this.GetProvID();
        var city_id = this.GetCityID();
        var area_id = this.GetAreaID();
        //省份select
        with (eval("document." + this.formName + ".elements['" + this.provCtrlName + "']")) {
            length = province.length + 1;
            var nIndex = 0;
            options[nIndex].value = "";
            options[nIndex++].text = this.selectProvinceTitle;
            //
            for (var i = 0; i < province.length; i++) {
                options[nIndex].value = province[i].lID;
                if (options[nIndex].value == prov_id)
                    selectedIndex = nIndex;
                options[nIndex++].text = province[i].strName;
            }
            onchange = Function(controlClientID + ".changeProvince();");
        }

        //城市select
        var cityLength = 1;
        if (!isNaN(prov_id)) {
            for (var i = 0; i < city.length; i++) {
                if (city[i].lParentID == prov_id)
                    cityLength++;
            }
        }
        with (eval("document." + this.formName + ".elements['" + this.cityCtrlName + "']")) {
            length = cityLength;
            var nIndex = 0;
            options[nIndex].value = prov_id;
            options[nIndex++].text = this.selectCityTitle;
            //
            for (var i = 0; i < city.length; i++) {
                if (nIndex >= cityLength)
                    break;
                if (city[i].lParentID == prov_id) {
                    options[nIndex].value = city[i].lID;
                    if (options[nIndex].value == city_id)
                        selectedIndex = nIndex;
                    options[nIndex++].text = city[i].strName;
                }
            }

            onchange = Function(controlClientID + ".changeCity();");
        }
        //地区select
        var areaLength = 1;
        if (!isNaN(city_id)) {
            for (var i = 0; i < area.length; i++) {
                if (area[i].lParentID == city_id)
                    areaLength++;
            }
        }
        with (eval("document." + this.formName + ".elements['" + this.areaCtrlName + "']")) {
            length = areaLength;
            var nIndex = 0;
            options[nIndex].value = city_id;
            options[nIndex++].text = this.selectAreaTitle;
            //
            for (var i = 0; i < area.length; i++) {
                if (nIndex >= areaLength)
                    break;
                if (area[i].lParentID == city_id) {
                    options[nIndex].value = area[i].lID;
                    if (options[nIndex].value == area_id)
                        selectedIndex = nIndex;
                    options[nIndex++].text = area[i].strName;
                }
            }
            onchange = Function(controlClientID + ".changeArea();");
        }
    }
    //　　this.SetControl = function(provID,cityID,areaID)
    //    { 
    //    	this.SetProvID(provID);
    //	    this.SetCityID(cityID);
    //        this.SetAreaID(areaID);

    //        this.InitControl();
    //    }
    this.SetControl = function(areaID) {
        if (areaID.length = 6) {
            this.SetValue(areaID);
        }


        this.InitControl();
    }
    /**
    * 选择省份
    * this.formName 页面表单名称
    * this.provCtrlName 省份域
    * this.cityCtrlName 城市域
    * this.areaCtrlName 地区域
    */
    this.changeProvince = function() {
        //省份select
        with (eval("document." + this.formName + ".elements['" + this.provCtrlName + "']")) {
            this.provID = options[selectedIndex].value;
            this.SetValue(this.provID);
        }
        //城市select
        var cityLength = 1;
        if (!isNaN(this.provID)) {
            for (var i = 0; i < city.length; i++) {
                if (city[i].lParentID == this.provID)
                    cityLength++;
            }
        }
        with (eval("document." + this.formName + ".elements['" + this.cityCtrlName + "']")) {
            length = cityLength;
            var nIndex = 0;
            options[nIndex].value = this.provID;
            options[nIndex++].text = this.selectCityTitle;
            //
            for (var i = 0; i < city.length; i++) {
                if (nIndex >= cityLength)
                    break;
                if (city[i].lParentID == this.provID) {
                    options[nIndex].value = city[i].lID;
                    options[nIndex++].text = city[i].strName;
                }
            }
            selectedIndex = 0;
        }
        //地区select
        with (eval("document." + this.formName + ".elements['" + this.areaCtrlName + "']")) {
            length = 1;
            var nIndex = 0;
            options[nIndex].value = "";
            options[nIndex++].text = this.selectAreaTitle;
        }
    }
    this.changeCity = function() {
        //城市select
        with (eval("document." + this.formName + ".elements['" + this.cityCtrlName + "']")) {
            this.cityID = options[selectedIndex].value;
            this.SetValue(this.cityID);

        }
        //地区select
        var areaLength = 1;
        if (!isNaN(this.cityID)) {
            for (var i = 0; i < area.length; i++) {
                if (area[i].lParentID == this.cityID)
                    areaLength++;
            }
        }
        with (eval("document." + this.formName + ".elements['" + this.areaCtrlName + "']")) {
            length = areaLength;
            var nIndex = 0;
            options[nIndex].value = this.cityID;
            options[nIndex++].text = this.selectAreaTitle;
            //
            for (var i = 0; i < area.length; i++) {
                if (nIndex >= areaLength)
                    break;
                if (area[i].lParentID == this.cityID) {
                    options[nIndex].value = area[i].lID;
                    options[nIndex++].text = area[i].strName;
                }
            }
            selectedIndex = 0;
        }
    }
    this.changeArea = function() {
        //城市select
        with (eval("document." + this.formName + ".elements['" + this.areaCtrlName + "']")) {
            this.areaID = options[selectedIndex].value;
            this.SetValue(this.areaID);
        }
    }

    this.Check = function() {

        var code = this.GetAreaID();

        if (code.substr(2, 4) == "0000") {

            with (eval("document." + this.formName + ".elements['" + this.cityCtrlName + "']")) {
                if (options.length > 1) {
                    return false;
                }
            }
        } else if (code.substr(4, 2) == "00") {
            with (eval("document." + this.formName + ".elements['" + this.areaCtrlName + "']")) {
                if (length > 1) {
                    return false;
                }
            }
        }

        if (code.length == 6 && !isNaN(code)) { 
            return true;
        }
        
        return false;


        //        if (isNaN(this.GetProvID()) || isNaN(this.GetCityID()) || isNaN(this.GetAreaID())) {
        //            return false;
        //        }



        //            for (var i = 0; i < area.length; i++) {
        //                if (area[i].lParentID == city_id) {
        //                    
        //                }
        //            }

    }

}
function comefrom(lID, lParentID, strName) {
    this.lID = lID;
    this.lParentID = lParentID;
    this.strName = strName;
}