var curCountry;
var curProvince;
var curCity;

var isSearchPage = true;//????
function initCountrySelect(arr){
	var countrySel = document.getElementById("countrySel");
	for (i = 0;i < arr.length; i++) {
		var country = arr[i];
    var option = new Option(country.countryName, country.id);
    countrySel.add(option);
	}
}

function initCountrySelect(arr,id,provinceId,cityId,countyId) {
	var countrySel = document.getElementById("countrySel");
  var provinceSel = document.getElementById("provinceSel");
  var citySel = document.getElementById("citySel");
  var countySel = document.getElementById("countySel");
    
	if(typeof countrySel != "undefined" && countrySel != null){
  	for (i = 0;i < arr.length; i++) {
    	var country = arr[i];
      var option;
      if(isSearchPage){
      	option = new Option(country.countryName, country.countryName);
			} else{
      	option = new Option(country.countryName, country.id);
			}
      countrySel.add(option);
		}
	}
    
  if(typeof countrySel != "undefined" && countrySel != null && id != null){
		selectById(countrySel,id);
	}
	selCountry(countrySel,provinceSel,citySel,countySel);
	selectById(provinceSel,provinceId);
  selProvince(provinceSel,citySel,countySel);
  selectById(citySel,cityId);
  selCity(citySel,countySel);
  if(typeof countySel != "undefined" && countySel != null){
  	selectById(countySel,countyId);
	}
}

function selectById(select,Id){
	
	var arr = select.options;
	for (i = 0;i < arr.length; i++) {
		var option = arr[i];
		
		if(option.value == Id){
    		option.selected = true;
    	}
	}
}
function selCountry(countrySel, subSelect,secondSelect,thirdSelect) {
	if(typeof subSelect != "undefined" && subSelect != null){
		subSelect.options.length = 0;
	}
	if(typeof secondSelect != "undefined" && secondSelect != null){
		secondSelect.options.length = 0;
	}
	if(typeof thirdSelect != "undefined" && thirdSelect != null){
		thirdSelect.options.length = 0;
	}
	if(typeof countrySel != "undefined" && countrySel != null){
    	for (i = 0; i < countryArr.length; i++) {
        	if (countryArr[i].id == countrySel.value) {
            	curCountry = countryArr[i];
            	break;
        	}
    	}
    } else{
    	for (i = 0; i < countryArr.length; i++) {
        	if (countryArr[i].id == "8") {
            	curCountry = countryArr[i];
            	break;
        	}
    	}
    }
    var arr = curCountry.provinceArr;
    if (arr.length == 0) {
        subSelect.options.length = 0;
        return;
    }
    
    if(isSearchPage){
    	var option = new Option("不限省市地区","");
    	subSelect.add(option);
    }
    
    for (i = 0; i < arr.length; i++) {
        var province = arr[i];
        var option;
        if(isSearchPage){
        	option = new Option(province.provinceName, province.provinceName);
        }else{
        	option = new Option(province.provinceName, province.provinceId);
        }
        subSelect.add(option);
    }
}
function selProvince(provinceSel, subSelect,secondSelect) {
	subSelect.options.length = 0;
	if(typeof secondSelect != "undefined" && secondSelect != null){
		secondSelect.options.length = 0;
	}
	
	if(typeof curCountry == "undefined" || curCountry == null){
		for (i = 0; i < countryArr.length; i++) {
        	if (countryArr[i].id == "8") {
            	curCountry = countryArr[i];
            	break;
        	}
    	}
    } 
    
    for (i = 0; i < curCountry.provinceArr.length; i++) {
    	if(isSearchPage){
    		if (curCountry.provinceArr[i].provinceName == provinceSel.value) {
            	curProvince = curCountry.provinceArr[i];
           	 	break;
        	}
    	} else{
       	if (curCountry.provinceArr[i].provinceId == provinceSel.value) {
            curProvince = curCountry.provinceArr[i];
            break;
        }
        }
    }
    if(provinceSel.value != ""){
    var arr = curProvince.cityArr;
    if (arr.length == 0) {
        subSelect.options.length = 0;
        return;
    }
    
    if(isSearchPage){
    	var option = new Option("不限省市地区","");
    	subSelect.add(option);
    }
    for (i = 0; i < arr.length; i++) {
        var city = arr[i];
        var option;
        if(isSearchPage){
        	option = new Option(city.cityName, city.cityName);
        } else{
        	option = new Option(city.cityName, city.cityId);
        }
         
        subSelect.add(option);
    }
    } else {
    	if(isSearchPage){
    		var option = new Option("不限省市地区","");
    		subSelect.add(option);
    	}
    }
}
function selCity(citySel, subSelect) {
	if(typeof subSelect != "undefined" && subSelect != null){
		subSelect.options.length = 0;
	}
	if(typeof curProvince == "undefined" && curProvince == null){
		return;
	}
	
	if(citySel.value == ""){
		return;
	}
    for (i = 0; i < curProvince.cityArr.length; i++) {
    	if(isSearchPage){
    		if (curProvince.cityArr[i].cityName == citySel.value) {
            	curCity = curProvince.cityArr[i];
           	 	break;
        	}
    	} else{
        if (curProvince.cityArr[i].cityId == citySel.value) {
            curCity = curProvince.cityArr[i];
            break;
        }
        }
    }
    
    var arr = curCity.countyArr;
    if (arr.length == 0) {
        subSelect.options.length = 0;
        return;
    }
    if(typeof subSelect != "undefined" && subSelect != null){

    for (i = 0; i < arr.length; i++) {
        var county = arr[i];
        var option = new Option(county.countyName, county.countyId);
        subSelect.add(option);
    }
    }
}

