﻿var countryID = 0;
var provinceID = 0;
var cityID = 0;

function doBlur(e,_default){
	if (e.value == ""){
		e.value = _default;
		e.style.color = "#878586";
	}
}
function doClick(e,_default){
	if (e.value == _default){
		e.value = "";
		e.style.color = "#000";
	}
}

function SelectRegion(type, upid){
	var h = "<div class='RegionListPanel'>请选择国家城市";
	h += "<div>国家：";
	h += "<div id='countryList'></div>";
	h += "</div>";
	h += "<div>省份：";
	h += "<div id='provinceList'></div>";
	h += "</div>";
	h += "<div>城市：";
	h += "<div id='cityList'></div>";
	h += "</div>";
	h += "</div>";
	//ThickBox(h,false,true,"选择城市");
	GetRegion(type,upid);
}
function GetRegion(type,upid){
    var regions = new Array();
    if (type=="country")
    {
        for (i=0;i<CountryList.length;i++){
		    regions.add(new ListItem(CountryList[i].Text,CountryList[i].Value));
	    }
    }
	if (type=="province")
	{
	    for (i=0;i<ProvinceList.length;i++){
	        if (ProvinceList[i].Country == upid.toString())
		        regions.add(new ListItem(ProvinceList[i].Text,ProvinceList[i].Value));
	    }
	}
	else if (type=="city")
	{
	    for (i=0;i<CityList.length;i++){
	        if (CityList[i].Country == upid.toString())
		        regions.add(new ListItem(CityList[i].Text,CityList[i].Value));
	    }
	}
	ShowRegion(type,upid,regions);
	return;
	//下面是服务器调用模式
	back_Region.List(type,upid,SelectRegionResult,error)
	Updating("正在加载...");
}
function SelectRegionResult(result){
	Updating(null);
	var type = result[0];
	var upid = result[1];
	var regions = new Array();
	for (i=0;i<result[2].length;i++){
		regions.add(new ListItem(result[2][i][1],result[2][i][0]));
	}
	ShowRegion(type,upid,regions);
}
function ShowRegion(type, upid, list){
	var h = "<ul>";
	for (var i = 0; i < list.length; i++) {
		h += "<li>";
		h += "<a href='#' onclick=\"doSelectRegion('" + type + "'," + upid + "," + list[i].Value + ");return false;\">" + list[i].Text + "</a>";
		h += "</li>";
	}
	h += "</ul>";
	//$set($get(type + "List"), h);
	//下拉列表处理
	var e = $get(type + "ID_");
    e.options.length = 0;
    var option = document.createElement("OPTION");
    option.value = 0;
    
    var tmpIndex = 0;
	if (type=="country"){
		option.innerHTML = "选择国家";
		tmpIndex = countryID;
	}
	else if (type=="province"){
		option.innerHTML = "选择省份";
		tmpIndex = provinceID;
	}
	else if (type=="city"){
		option.innerHTML = "选择城市";
		tmpIndex = cityID;
	}
    e.appendChild(option);
    for (var i = 0; i < list.length; i++) {

            option = null;
            option = document.createElement("OPTION");
            option.value = list[i].Value;
            option.innerHTML = list[i].Text;
            
			if (list[i].Value == tmpIndex) {
				option.selected = true;
			}

            e.appendChild(option);
    }
    if (type=="province")
    {
        $get("cityID_").options.length = 0;
		option = null;
        option = document.createElement("OPTION");
        option.value = "0";
        option.innerHTML = "选择城市";
		$get("cityID_").appendChild(option);
    }
}
function doSelectRegion(type,upid,id){
	if (type=="country"){
		GetRegion("province",id);
	}
	else if (type=="province"){
		GetRegion("city",id);
	}
	else{
		alert(0);
	}
}

function SelectRegionChange(type,e){
	if (e.options[e.selectedIndex].value!="")
		GetRegion(type,e.options[e.selectedIndex].value);
}


function ListItem(Text, Value){
    this.Text = Text;
    this.Value = Value;
}

function CityClass(Text, Value, Country){
    this.Text = Text;
    this.Value = Value;
    this.Country = Country;
}
