/*
用法
     <script src="../js/provinceInfo.js" language="javascript"></script> 
     <select name="province" id="province" onChange="P.getCity('province', 'city','area');"  >
     <option value="0">省份/直辖市</option>
     </select>
     <select name="city" id="city" onChange="P.getArea('province','city','area');" >
     <option value="" >县/市</option>
     </select>
     <select name="area" id="area" >
     <option value="" >镇/区</option>
     </select>  
   <script language="javaScript">P.getProvince('province', 'city','area');</script>
*/

var P = new Object();
P = 
{
	Province :new Array('101101','北京'),

	City :new Array(),
	County:new Array(),
	//初始化
	init:function()
	{
		this.City["101101"] =new Array('101101000','','101101101','北京市');
	},
	//得到省选择列表
	getProvince:function(strProvince,strCity,strArea)
	{
		var provinceObj = document.getElementById(strProvince);
		if(this.Province.length != 0)
		{
			provinceObj.options.length = 0
			provinceObj.options[0] = new Option('直辖市/省','')
			provinceObj.options[0].selected = true
			var j = 1
			for(i=0;i<this.Province.length;i+=2)
			{
				if(this.Province[i] == "" || this.Province[i+1] == "")
				{
					continue
				}
				else
				{
					provinceObj.options[j] = new Option(this.Province[i+1],this.Province[i]); 
					j++
				}
			}
		}
	},

	//得到市选择列表
	getCity:function(strProvince,strCity,strArea)
	{
		var provinceObj = document.getElementById(strProvince);
		var cityObj = document.getElementById(strCity);
		var areaObj = document.getElementById(strArea);
		if(provinceObj.selectedIndex != 0)
		{
			cityObj.options.length = 0
			cityObj.options[0] = new Option('市','')
			cityObj.options[0].selected = true
			cityObj.disabled = true
			areaObj.options.length = 0
			areaObj.options[0] = new Option('区','')
			areaObj.options[0].selected = true
			areaObj.disabled = true
			CityIndex = provinceObj.options[provinceObj.selectedIndex].value
			var j = 1
			for(i=0;i<this.City[CityIndex].length;i+=2)
			{
				if(this.City[CityIndex][i] == "" || this.City[CityIndex][i+1] == "")
				{
					continue
				}
				else
				{
					cityObj.options[j] = new Option(this.City[CityIndex][i+1],this.City[CityIndex][i])
					j++
				}
			}
			if(cityObj.options.length > 1) cityObj.disabled = false
		}
		else
		{
			cityObj.options.length = 0
			cityObj.options[0] = new Option('市','')
			cityObj.options[0].selected = true
			cityObj.disabled = true
			areaObj.options.length = 0
			areaObj.options[0] = new Option('区','')
			areaObj.options[0].selected = true
			areaObj.disabled = true
		}
	},
    //取到地区列表
	getArea:function(strProvince,strCity,strCounty)
	{
	    var TheProvince = document.getElementById(strProvince);
		var TheCity = document.getElementById(strCity);
		var TheCounty = document.getElementById(strCounty);
		if(TheProvince.selectedIndex != 0 && TheCity.selectedIndex != 0)
		{
			TheCounty.options.length = 0
			TheCounty.options[0] = new Option('区','')
			TheCounty.options[0].selected = true
			TheCounty.disabled = true
			CountyIndex = TheCity.options[TheCity.selectedIndex].value
			var j = 1
			for(i=0;i<this.County[CountyIndex].length;i+=2)
			{
				if(this.County[CountyIndex][i] == "" || this.County[CountyIndex][i+1] == "")
				{
					continue
				}
				else
				{
					TheCounty.options[j] = new Option(this.County[CountyIndex][i+1],this.County[CountyIndex][i])
					j++
				}
			}
			if(TheCounty.options.length > 1) TheCounty.disabled = false
		}
		else
		{
			TheCounty.options.length = 0
			TheCounty.options[0] = new Option('区','')
			TheCounty.options[0].selected = true
			TheCounty.disabled = true
		}
	},
	//选择省市
	selectcity:function(province,city,area,street,houseid)
	{
		strDataListHtml="";
	   if(province=='' || province==0)
		{
			province=publicMyPersonInfoObj[0].province;
		}
	
		strDataListHtml+='<select name="province" id="bornProvince" onChange="P.getCity('+'\''+'bornProvince'+'\',\''+'bornCity'+'\',\''+'bornUptown'+'\');">';
		var intBornProvince='';
		intBornProvince=province
		for(bp=0;bp<P.Province.length;bp+=2)
		{
			if(intBornProvince==P.Province[bp])
			{
			  strDataListHtml+='<option value="'+P.Province[bp]+'" selected="selected" >'+P.Province[bp+1]+'</option>';
			}else
			{
			  strDataListHtml+='<option value="'+P.Province[bp]+'" >'+P.Province[bp+1]+'</option>';
			}
		}
		strDataListHtml+='</select>';
	
	
	    if(city=='')
		{
			city=publicMyPersonInfoObj[0].city;
		}
	
	
		strDataListHtml+=' <select name="city" id="bornCity" onChange="P.getArea('+'\''+'bornProvince'+'\',\''+'bornCity'+'\',\''+'bornUptown'+'\');">';
		var intBornCity='',intNowBornProvinceId;
		intNowBornProvinceId=province
		intBornCity=city;
		for(bc=0;bc<P.City[intNowBornProvinceId].length;bc+=2)
		{
			if(intBornCity==P.City[intNowBornProvinceId][bc])
			{
			  strDataListHtml+='<option value="'+P.City[intNowBornProvinceId][bc]+'" selected="selected" >'+P.City[intNowBornProvinceId][bc+1]+'</option>';
			}else
			{
			  strDataListHtml+='<option value="'+P.City[intNowBornProvinceId][bc]+'" >'+P.City[intNowBornProvinceId][bc+1]+'</option>';
			}
		}
		strDataListHtml+='</select>';
	
	
		if(area=='')
		{
			area=publicMyPersonInfoObj[0].uptown;
		}
	
		strDataListHtml+=' <select onChange="P.showStreetSelect(this.value,0);" name="area" id="bornUptown">';
		var intBornUptown='',intNowBornCityId;
		intNowBornCityId=city
		intBornUptown=area
		for(bu=0;bu<P.County[intNowBornCityId].length;bu+=2)
		{
			if(intBornUptown==P.County[intNowBornCityId][bu])
			{
			  strDataListHtml+='<option value="'+P.County[intNowBornCityId][bu]+'" selected="selected" >'+P.County[intNowBornCityId][bu+1]+'</option>';
			}else
			{
			  strDataListHtml+='<option value="'+P.County[intNowBornCityId][bu]+'" >'+P.County[intNowBornCityId][bu+1]+'</option>';
			}
		}
		strDataListHtml+='</select>';
		strDataListHtml+='<span id="streetSelectSpan"><select name="street"><option>选择街道</option></select></span>';//载入街道
		strDataListHtml+='<span id="houseSelectSpan"><select name="house"> <option>所属楼区</option></select></span>';//载入小区
		document.write(strDataListHtml);
		if (street)
		{
			P.showStreetSelect(area,street);
		}
		if (houseid)
		{
			P.showHouseSelect(street,houseid);
		}
	},
	
	//载入街道
	showStreetSelect:function(areaid,street)
	{
		var rand = Math.round(Math.random() * 10000);
		$.get("/user/ajaxgetstreets/", { rad: Math.random(),areaid: areaid }, function(data)
		{
			 var dataInfo=eval("(" + data + ")");
			 P.makeStreetSelect(dataInfo,street)
		});
	},
	//选择街道
	makeStreetSelect:function(dataInfo,street)
	{
		var strDataHtml=''
		if(dataInfo.length > 0 && dataInfo.length!=0 && typeof(dataInfo) != "undefined")
		{
			 strDataHtml+='<select name="street" onChange="P.showHouseSelect(this.value,0);" id="streetSelect" >';
			 strDataHtml+='<option value="0">所属街道</option>';
			 publicStreetSum=dataInfo[0].streetSum;
			 for(i=1;i<=dataInfo.length;i++)
			 {
			 	if (street==dataInfo[i-1].streetid)
			 	{
			 		strDataHtml+='<option value="'+dataInfo[i-1].streetid+'"  selected="selected" >'+dataInfo[i-1].name+'</option>';
			 	}
			 	else
			 	{
			 		strDataHtml+='<option value="'+dataInfo[i-1].streetid+'">'+dataInfo[i-1].name+'</option>';
			 	}
			 }
			 strDataHtml+='</select>'; 
		}else
		{
			 publicStreetSum=0
			 strDataHtml+='<select name="street" id="streetSelect" >';
			 strDataHtml+='<option value="0">所属街道</option>';
			 strDataHtml+='</select>'; 
		}
		$('#streetSelectSpan').html(strDataHtml);
	},
	
	//载入小区信息
	showHouseSelect:function(streetid,houseid)
	{
		var rand = Math.round(Math.random() * 10000);
		$.get("/user/ajaxgetstreets/", { rad: Math.random(),streetid: streetid }, function(data)
		{
			 var dataInfo=eval("(" + data + ")");
//			 alert(dataInfo);
			 P.makeHouseSelect(dataInfo,houseid)
		});
	},
	
	//选择小区
	makeHouseSelect:function(dataInfo,houseid)
	{
		var strDataHtml=''
		if(dataInfo.length > 0 && dataInfo.length!=0 && typeof(dataInfo) != "undefined")
		{
			 strDataHtml+='<select name="house" id="houseSelect" >';
			 strDataHtml+='<option value="0">所属楼区</option>';
			 publicStreetSum=dataInfo[0].streetSum;
//			 alert(houseid);
			 for(ii=1;ii<=dataInfo.length;ii++)
			 {
			 	if (houseid==dataInfo[ii-1].houseid)
			 	{
//			 		alert(dataInfo[ii-1]);alert(dataInfo.length);alert(dataInfo[ii-1].houseid);alert(houseid);
			 		strDataHtml+='<option value="'+dataInfo[ii-1].houseid+'"  selected="selected" >'+dataInfo[ii-1].name+'</option>';
			 	}
			 	else
			 	{
			 		strDataHtml+='<option value="'+dataInfo[ii-1].houseid+'">'+dataInfo[ii-1].letter+dataInfo[ii-1].name+'</option>';
			 	}
			 }
			 strDataHtml+='</select>'; 
		}else
		{
			 publicStreetSum=0
			 strDataHtml+='<select name="house" id="houseSelect" >';
			 strDataHtml+='<option value="0">所属楼区</option>';
			 strDataHtml+='</select>'; 
		}
		$('#houseSelectSpan').html(strDataHtml);
	},
	
	//获取地址的文字说明
	receiveAddress:function()
	{
		var receiveAddress='',index,val='';
		var idArray = new Array('bornCity','bornUptown','streetSelect','houseSelect')
		for(i=0; i<idArray.length;i++)
		{
			obj=document.getElementById(idArray[i]);
			index=obj.selectedIndex; //序号，取当前选中选项的序号	
			val = obj.options[index].text;
			receiveAddress+=val;
		}
		return receiveAddress;
	},
	
	//显示信息
	showProvince:function()
	{
		$('#bornProvince').show();
	},
	showCity:function()
	{
		$('#bornCity').show();
	},
	showArea:function()
	{
		$('#bornUptown').show();
	},
	showStreet:function()
	{
		$('#streetSelect').show();
	},

	//隐藏信息
	hideProvince:function()
	{
		$('#bornProvince').hide();
	},
	hideCity:function()
	{
		$('#bornCity').hide();
	},
	hideArea:function()
	{
		$('#bornUptown').hide();
	},
	hideStreet:function()
	{
		$('#streetSelect').hide();
	},
	hideHouse:function()
	{
		$('#houseSelect').hide();
	},
	
	//显示区域
	showOneArea:function(areaid,id)
	{
		strDataHtml='<select name="areaid">';
		for(i=0; i<P.County["101101101"].length; i+=2)
		{
			if (areaid == P.County["101101101"][i])
			{
				strDataHtml+='<option value="'+P.County["101101101"][i]+'"  selected="selected" >'+P.County["101101101"][i+1]+'</option>';
			}
			else
			{
				strDataHtml+='<option value="'+P.County["101101101"][i]+'" >'+P.County["101101101"][i+1]+'</option>';
			}
		}
		return strDataHtml;
	}
}
//初始化
P.init();
