var areaLevelId = Array(1,2,3,4,5);
var areaLevelName = Array("continent","country","provinces","city","district");

//判断_sVal是否为空
function isNull(_sVal){
	return ( _sVal == null ||_sVal == "" || _sVal == "undefined");
}

//在数组arr中搜索值为id的元素的索引
function searchId(arr,id){
    if(isNull(arr)){
        return -1;
    }
    var nid ;
	var minIndex = 0;
	var maxIndex = arr.length - 1;
	var num;
	try
	{
	nid = parseInt(id);
    }
    catch(ex)
    {}
	while(minIndex <= maxIndex){
		num = Math.floor((minIndex + maxIndex)/2);
		if(parseInt(arr[num]) == nid){
			return num;
		}
		else{
			if(parseInt(arr[num]) > nid){
			 //alert(parseInt(arr[num]));
				maxIndex = num - 1;
			}
			else{
				minIndex = num + 1;
			}
		}
	}
	return -1;
}

function selectArea(areaLevel,object,controlIdList){
    if(isNull(areaLevel) || isNaN(areaLevel) || isNull(object)){
        return;
    }
    if(isNull(controlIdList) || controlIdList.length == 0){
        return;
    }
    var SelectId = object.value;
    switch (areaLevel){
        case "1":
            if(controlIdList.length >= 2){
                for(var k = 1;k<controlIdList.length; k++){
                    selectInit(document.getElementById(controlIdList[k]));
                }
                if(SelectId!="0"){
                    for(var i =0; i<countryParentId.length; i++){
                        if(countryParentId[i] == SelectId){
                            var option = createOption(countryName[i], countryId[i]);
                            document.getElementById(controlIdList[1]).options.add(option);
                        }
                    }
                }
            }
            break;
        case "2":
            if(controlIdList.length >= 3){
                for(var k = 2;k<controlIdList.length; k++){
                    selectInit(document.getElementById(controlIdList[k]));
                }
                if(SelectId!="0"){
                    for(var i =0; i<provincesParentId.length; i++){
                        if(provincesParentId[i] == SelectId){
                            var option = createOption(provincesName[i], provincesId[i]);
                            document.getElementById(controlIdList[2]).options.add(option);
                        }
                    }
                }
            }
            break;
        case "3":
            if(controlIdList.length >= 4){
                for(var k = 3;k<controlIdList.length; k++){
                    selectInit(document.getElementById(controlIdList[k]));
                }
                if(SelectId!="0"){
                    for(var i =0; i<cityParentId.length; i++){
                        if(cityParentId[i] == SelectId){
                            var option = createOption(cityName[i], cityId[i]);
                            document.getElementById(controlIdList[3]).options.add(option);
                        }
                    }
                }
            }
            break;
        case "4":
            if(controlIdList.length >= 5){
                for(var k = 4;k<controlIdList.length; k++){
                    selectInit(document.getElementById(controlIdList[k]));
                }
                if(SelectId!="0"){
                    for(var i =0; i<districtParentId.length; i++){
                        if(districtParentId[i] == SelectId){
                            var option = createOption(districtName[i], districtId[i]);
                            document.getElementById(controlIdList[4]).options.add(option);
                        }
                    }
                }
            }
            break;
        case "5":
            break;
    }
}

function initControl(areaLevel, areaId, controlIdList){
    if(isNull(areaLevel) || isNaN(areaLevel) || isNull(areaId) || isNaN(areaId)){
        return;
    }
    if(isNull(controlIdList) || controlIdList.length == 0){
        return;
    }
    switch (areaLevel){
        case "0":
            getContinent(continentId,continentName,document.getElementById(controlIdList[0]));
            break;
        case "1":
            selectInit(document.getElementById(controlIdList[0]));
            for(var i = 0; i < continentId.length; i++){
                var option  = createOption(continentName[i], continentId[i]);
                document.getElementById(controlIdList[0]).options.add(option);
            }
            document.getElementById(controlIdList[0]).value = areaId;
            /////////////////////////////////////////////////////
            getNextData(1, countryId, countryName, areaId, countryParentId, controlIdList);
            break;
        case "2":
            selectInit(document.getElementById(controlIdList[1]));
            GetNowData(countryId, countryName, areaId, countryParentId, controlIdList[1]);
            getNextData(2, provincesId, provincesName, areaId, provincesParentId, controlIdList);
            getParentData(1,areaId,controlIdList);
            break;
        case "3":
            selectInit(document.getElementById(controlIdList[2]));
            GetNowData(provincesId, provincesName, areaId, provincesParentId, controlIdList[2]);
            getNextData(3, cityId, cityName, areaId, cityParentId, controlIdList);
            getParentData(3,areaId,controlIdList);
            break;
        case "4":
            selectInit(document.getElementById(controlIdList[3]));
            GetNowData(cityId, cityName, areaId, cityParentId, controlIdList[3]);
            getNextData(4, districtId, districtName, areaId, districtParentId, controlIdList);
            getParentData(4,areaId,controlIdList);
            break;
        case "5":
            selectInit(document.getElementById(controlIdList[4]));
            GetNowData(districtId, districtName, areaId, districtParentId, controlIdList[4]);
            getParentData(5,areaId,controlIdList);
            break;
    }
}

function getAreaArr(lv){
    var areaArr = Array(3);
    switch (lv){
        case 1:
            areaArr[0]=continentId;
            areaArr[1]=null;
            areaArr[2]=continentName;
            break;
        case 2:
            areaArr[0]=countryId;
            areaArr[1]=countryParentId;
            areaArr[2]=countryName;
            break;
        case 3:
            areaArr[0]=provincesId;
            areaArr[1]=provincesParentId;
            areaArr[2]=provincesName;
            break;
        case 4:
            areaArr[0]=cityId;
            areaArr[1]=cityParentId;
            areaArr[2]=cityName;
            break;
        case 5:
            areaArr[0]=districtId;
            areaArr[1]=districtParentId;
            areaArr[2]=districtName;
            break;
    }
    return areaArr;
}

function getParentData(lv,areaId,controlIdList){
    var aid = areaId;
    var p;
    for(var i = lv; i > 1; i--){
        if(i > 2){
            var areaArr = getAreaArr(i);
            var pAreaArr = getAreaArr(i - 1);
            var ain = searchId(areaArr[0],aid);
            var pid = areaArr[1][ain];
            var pain = searchId(pAreaArr[0],pid);
            var ppid = pAreaArr[1][pain];
            selectInit(document.getElementById(controlIdList[i - 2]));
            for(var j = 0;j < pAreaArr[0].length; j++){
            
                if(pAreaArr[1][j] == ppid){
                    var option = createOption(pAreaArr[2][j], pAreaArr[0][j]);
                    document.getElementById(controlIdList[i - 2]).options.add(option);
                }
        
            }
            document.getElementById(controlIdList[i - 2]).value = pAreaArr[0][pain];
            aid = pAreaArr[0][pain];
            p = ppid;  
        }
        else{
            var aa = getAreaArr(1);
            selectInit(document.getElementById(controlIdList[0]));
            for(var j = 0;j < aa[0].length; j++){
                    var option = createOption(aa[2][j], aa[0][j]);
                    document.getElementById(controlIdList[0]).options.add(option);
            }
            document.getElementById(controlIdList[0]).value = p;
        }
    }
}

function getNextData(lv, arrId, arrName, areaId, ParentId, controlIdList){
    if(controlIdList.length >= lv +1){
        for(var k = lv ;k<controlIdList.length; k++){
            selectInit(document.getElementById(controlIdList[k]));
        }
        for(var i =0; i<ParentId.length; i++){
            if(ParentId[i] == areaId){
                var option = createOption(arrName[i], arrId[i]);
                document.getElementById(controlIdList[lv]).options.add(option);
            }
        }
                
    }
}

function GetNowData(arrId, arrName, areaId, ParentId, controlId){
   var countryIn = searchId(arrId,areaId);
   if(countryIn != -1){
        var cpId = ParentId[countryIn];
        for(var i = 0; i < arrId.length; i++){
            if(ParentId[i] == cpId){
                var option  = createOption(arrName[i], arrId[i]);
                document.getElementById(controlId).options.add(option);
            }
        }
        document.getElementById(controlId).value = areaId;
   }
   else{
        document.getElementById(controlId).selectedIndex = 0;
   }
}

//创建一个option对象
function createOption(optText,optValue){
    var option = document.createElement("OPTION");
    option.value = optValue;
    option.text = optText;
    return option;
}

//重置选择框内容
function selectInit(object){
    if(!isNull(object)){
    object.options.length = 0;
    }
    var option = createOption("--请选择--","0");
    object.options.add(option);
    //object.selectedIndex = 0;
}

//选择框绑定数据
function getContinent(arrId,arrName,object){
    selectInit(object);
    var n = arrId.length;
    for(var i = 0; i < n; i++){
        var option = createOption(arrName[i],arrId[i]);
        object.options.add(option);
    }
    object.selectedIndex = 0;
}

