var xxxCountry;
var xxxProvince;
var xxxCity;
var xxxValueOcx;
var xxxtype=-1;
var xxxdefaultvalue=-1;
var areaName ="";
//得到值
function getValue(){
	var CountryValue=xxxCountry.value;
	var ProvinceValue=xxxProvince.value;
	var CityValue=xxxCity.value;
	if(CityValue.length>6){
		return CityValue;
	}
	
	if(ProvinceValue.length>3){
		return ProvinceValue;
	}
	if(CountryValue.length==3)
		return CountryValue;
	return "";
}
function getValue2(){
	var ProvinceValue=xxxProvince.value;
	var CityValue=xxxCity.value;
	if(CityValue.length>6){
		return CityValue;
	}
	if(ProvinceValue.length>3){
		return ProvinceValue;
	}
	return "";
}
//设置默认值
function setValue(value){
	if (value.length>=3){
		xxxCountry.value=value.substring(0,3);
		xxxCountry.onchange();
	}
	if(value.length>=6){
		xxxProvince.value=value.substring(0,6);
		xxxProvince.onchange();
	}
	if(value.length>=9){
		xxxCity.value=value.substring(0,9);
		xxxCity.onchange();
	}
}

function setValue2(value){
	if(value.length>=6){
		xxxProvince.value=value.substring(0,6);
		xxxProvince.onchange();
	}
	if(value.length>=9){
		xxxCity.value=value.substring(0,9);
		xxxCity.onchange();
	}
}
//装载全部控件
function setupOcx(Country,Province,City,ValueOcx,Type,Defultvalue){
	xxxCountry=Country;
	xxxProvince=Province;
	xxxCity=City;
	xxxtype=Type;
	xxxdefaultvalue=Defultvalue;
	xxxValueOcx=ValueOcx;
	ValueOcx.value=Defultvalue;
	setupCountry(xxxCountry,"001");
	setupProvince(xxxProvince,xxxdefaultvalue,"001",xxxtype);
	setupCity(xxxCity,xxxdefaultvalue,xxxdefaultvalue,xxxtype);
	xxxCountry.onchange=function(){
		var countryCode=this.value;
		setupProvince(xxxProvince,countryCode,countryCode,xxxtype);
		setupCity(xxxCity,countryCode,xxxtype,xxxtype);
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue();
		}
	};
	xxxProvince.onchange=function(){
		var provinceCode=this.value;
		setupCity(xxxCity,provinceCode,provinceCode,xxxtype);
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue();
		}
	};
	xxxCity.onchange=function(){
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue();
		}
	}
}


//装载全部控件
function setupOcx2(Province,City,ValueOcx,Type,Defultvalue){
	xxxProvince=Province;
	xxxCity=City;
	xxxtype=Type;
	xxxdefaultvalue=Defultvalue;
	xxxValueOcx=ValueOcx;
	ValueOcx.value=Defultvalue;
	setupProvince(xxxProvince,xxxdefaultvalue,"001",xxxtype);
	setupCity(xxxCity,xxxdefaultvalue,xxxdefaultvalue,xxxtype);
	xxxProvince.onchange=function(){
		var provinceCode=this.value;
		setupCity(xxxCity,provinceCode,provinceCode,xxxtype);
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue2();
		}
	};
	xxxCity.onchange=function(){
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue2();
		}
	}
}

//装载全部控件
function setupOcx_Query(Country,Province,City,ValueOcx,Defultvalue){
	xxxCountry=Country;
	xxxProvince=Province;
	xxxCity=City;
	xxxtype="0";
	xxxdefaultvalue=Defultvalue;
	xxxValueOcx=ValueOcx;
	setupCountry_Query(xxxCountry,"0",xxxtype);
	setupProvince(xxxProvince,0,"0",xxxtype);
	setupCity(xxxCity,0,0,xxxtype);
	xxxCountry.onchange=function(){
		var countryCode=this.value;
		setupProvince(xxxProvince,countryCode,countryCode,xxxtype);
		setupCity(xxxCity,countryCode,countryCode,xxxtype);
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue();
		}
		xxxProvince.onchange();
	};
	xxxProvince.onchange=function(){
		var provinceCode=this.value;
		setupCity(xxxCity,provinceCode,provinceCode,xxxtype);
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue();
		}
	};
	xxxCity.onchange=function(){
		if (xxxValueOcx!=null){
			xxxValueOcx.value=getValue();
		}
	}
}





//装载城市
function setupCity(city,initValue,provinceCode,type){
//清除列表

	city.options.length=0;
	if(provinceCode == "-1")
	{
		city.options[city.options.length] = new Option ("-请选择(城市)-", "-1");
		city.style.display="none";
		return;
	}
	if(provinceCode == "0")
	{
		city.options[city.options.length] = new Option ("----不限----", "0");
		city.style.display="none";
		return;
	}
	provinceCode=provinceCode.substring(0,6);
	initValue=initValue.substring(0,9);
	var cityCodeList = eval("CityCodes" + provinceCode + ".split(',')");
	if(cityCodeList=="")
		city.style.display="none";
	else
		city.style.display="";
	var cityNameList = eval("CityNames" + provinceCode + ".split(',')");
	if(type == "-1")
	{
		city.options[city.options.length] = new Option ("-请选择(城市)-", "-1");
	}else
	{
		city.options[city.options.length] = new Option ("----不限----", "0");
	}
	for(var i = 0; i < cityCodeList.length; i++)
	{
		if(cityNameList[i]!=""){
			var option = new Option (cityNameList[i], cityCodeList[i]);
			city.options[city.options.length] = option;
			
			if(cityCodeList[i] == initValue)
			{
				option.selected = true;
			}
		}
	}
}
//装载省份
function setupProvince(province,initValue,countryCode,type){
//清除列表

	province.options.length=0;
	if(countryCode == "")
	{
		province.style.display="none";
		return;
	}
	if(countryCode == "-1")
	{
		province.options[province.options.length] = new Option ("-请选择(省份)-", "-1");
		return;
	}
	if(countryCode == "0")
	{
		province.options[province.options.length] = new Option ("----不限----", "0");
		return;
	}
	countryCode=countryCode.substring(0,3);
	initValue=initValue.substring(0,6);
	var provinceCodeList = eval("ProvinceCodes" + countryCode + ".split(',')");
	if(provinceCodeList=="")
		province.style.display="none";
	else
		province.style.display="";
	var provinceNameList = eval("ProvinceNames" + countryCode + ".split(',')");
	if(type == "-1")
	{
		province.options[province.options.length] = new Option ("-请选择(省份)-", "-1");
	}else
	{
		province.options[province.options.length] = new Option ("----不限----", "0");
	}
	for(var i = 0; i < provinceCodeList.length; i++)
	{
		if (provinceNameList[i]!="")
		{
			var option = new Option (provinceNameList[i], provinceCodeList[i]);
			province.options[province.options.length] = option;
			
			if(provinceCodeList[i] == initValue)
			{
				option.selected = true;
			}
		}
	}
}

//装载国家		
function setupCountry(Country, initValue)
{
	initValue=initValue.substring(0,3);
	var countryCodeList  = countryCodes.split(',');
	var countryNameList = countryNames.split(',');
	for(var i = 0; i < countryCodeList.length; i++)
	{
		var option = new Option (countryNameList[i], countryCodeList[i]);
		Country.options[Country.options.length] = option;
		
		if(countryCodeList[i] == initValue)
		{
			option.selected = true;
		}		
	}
}

function setupCountry_Query(Country, initValue,type)
{
	var provinceCodeList  = countryCodes.split(',');
	var provinceNameList = countryNames.split(',');
	if(type == "-1")
	{
		Country.options[Country.options.length] = new Option ("-请选择(省份)-", "-1");
	}else
	{
		Country.options[Country.options.length] = new Option ("----不限----", "0");
	}
	for(var i = 0; i < provinceCodeList.length; i++)
	{
		var option = new Option (provinceNameList[i], provinceCodeList[i]);
		Country.options[Country.options.length] = option;
		
		if(provinceCodeList[i] == initValue)
		{
			option.selected = true;
		}		
	}
}

/********************************************************
/*
/* 根据省份的编码取得省份的名称。
/* 如果省份的编码不存在返回空的字符串。
/*
/*******************************************************/
function getProvinceNameByCode(name,code,size)
{
    var provinceCodes =eval(name+"Codes"+size);
    var provinceNames = eval(name+"Names"+size);
	name = "";
    var provinceCodeList = provinceCodes.split(',');
    var provinceNameList = provinceNames.split(',');
    for(var i = 0; i < provinceCodeList.length; i++)
    {
        if(code == provinceCodeList[i])
        {
            name = provinceNameList[i];
            break;
        }
    }
    return name;
}


/**
 * 根据编码得省份
 * @param Defultvalue
 * @return
 */
function getAreaName(Defultvalue){
	var vst ;
	areaName='';
	var code = Defultvalue.replace(" ", "");
	if(code.length>=6){
		vst=code.substring(0,6);
		areaName += getProvinceNameByCode('Province',vst,vst.substring(0,3))+"省";
	}
	if(code.length>=9){
		vst=code.substring(0,9);
		areaName += getProvinceNameByCode('City',vst,vst.substring(0,6))+"市";
	}
	document.write(areaName);
}

/**
 * 用于AJAX情况下获取区域名称
 */
function getAreaNameInAJAX(Defultvalue,rtnId){
	var vst ;
	areaName='';
	var code = Defultvalue.replace(" ", "");
	if(code.length>=6){
		vst=code.substring(0,6);
		areaName += getProvinceNameByCode('Province',vst,vst.substring(0,3))+"省";
	}
	if(code.length>=9){
		vst=code.substring(0,9);
		areaName += getProvinceNameByCode('City',vst,vst.substring(0,6))+"市";
	}
	$("#"+rtnId).html(areaName);
}

function getCountryNameByAreaCode(areaCode,rtnId){
	var vst ;
	var countryName ='';
	areaCode = areaCode.replace(" ", "");
	if(areaCode.length>=3){
		vst=areaCode.substring(0,3);
		countryName += getCountryNameByCode('country',vst);
	}
	$("#"+rtnId).html(countryName);
}

function getCountryNameByCode(name,code)
{
    var provinceCodes =eval(name+"Codes");
    var provinceNames = eval(name+"Names");
	name = "";
    var provinceCodeList = provinceCodes.split(',');
    var provinceNameList = provinceNames.split(',');
    for(var i = 0; i < provinceCodeList.length; i++)
    {
        if(code == provinceCodeList[i])
        {
            name = provinceNameList[i];
            break;
        }
    }
    return name;
}

/**
 * 获取国家名称
 * @param code 国家编码或者区域编码
 */
function getCountryName(code){
	var rtn = "";
	code = code.replace(" ", "");
	if(code.length>3){
		code=code.substring(0,3);
	}
    var countryCodesList = eval("countryCodes").split(',');
    var countryNamesList = eval("countryNames").split(',');
    for(var i = 0; i < countryCodesList.length; i++)
    {
        if(code == countryCodesList[i])
        {
            rtn = countryNamesList[i];
            break;
        }
    }
    return rtn;
}


/**
 * 获取省份名称
 * @param code 省份编码或者区域编码
 */
function getProvinceName(code){
	var rtn = "";
	code = code.replace(" ", "");
	if(code.length>6){
		code=code.substring(0,6);
	}
	var countryCode=code.substring(0,3);
	var provinceCodes =eval("ProvinceCodes"+countryCode);
    var provinceNames = eval("ProvinceNames"+countryCode);
    var provinceCodesList = provinceCodes.split(',');
    var provinceNamesList = provinceNames.split(',');
    for(var i = 0; i < provinceCodesList.length; i++)
    {
        if(code == provinceCodesList[i])
        {
            rtn = provinceNamesList[i];
            break;
        }
    }
    return rtn;
}


/**
 * 获取城市名称
 * @param code 城市编码或者区域编码
 */
function getCityName(code){
	var rtn = "";
	code = code.replace(" ", "");
	if(code.length>9){
		code=code.substring(0,9);
	}
	var provinceCode=code.substring(0,6);
	var cityCodes =eval("CityCodes"+provinceCode);
    var cityNames = eval("CityNames"+provinceCode);
    var cityCodesList = cityCodes.split(',');
    var cityNamesList = cityNames.split(',');
    for(var i = 0; i < cityCodesList.length; i++)
    {
        if(code == cityCodesList[i])
        {
            rtn = cityNamesList[i];
            break;
        }
    }
    return rtn;
}


function convert(areaCode,rtnId){
	var vst ;
	var rtn ='';
	areaCode = areaCode.replace(" ", "");
	switch (areaCode.length) {
		case 3:
			vst=areaCode;
			rtn += getCountryName(vst.substring(0,3));
			break;
		case 6:
			vst=areaCode;
			rtn += getCountryName(vst.substring(0,3));
			rtn +="　";
			rtn += getProvinceName(vst.substring(0,6));
			break;
		case 9:
			vst=areaCode;
			rtn += getCountryName(vst.substring(0,3));
			rtn +="　";
			rtn += getProvinceName(vst.substring(0,6));
			rtn +="　";
			rtn += getCityName(vst.substring(0,9));
			break;
		default:
			rtn="其它";
			break;
	}
	$("#"+rtnId).html(rtn);
}

