
/*
	oProv:省份Select ID
	oCity:城市ID
	oCounty: 区域ID
	provID: 省份选中值
	cityID: 城市选中值
	countyID: 区域选中值
	isEdit: 是不是编辑，需要选择
*/



function BuildAreaJs(oProv,oCity, oCounty, provID, cityID, countyID, isEdit)
{		
		//填充省数据	
		if( arLay1 != null && oProv != null)
		{
			var sb1 = "";
			if(isEdit)
			{
				sb1 += "<option value='-1'>省份</option>";
			}
		
			for(var i = 0; i < arLay1.length;i++)
			{
				var strSel = "";
				if( arLay1[i][0] == provID)
				{					
					strSel = " selected ";					
				}
				sb1 += "<option value='" + arLay1[i][0] + "'" + strSel + ">" + arLay1[i][1] + "</option>";
			}
			
			$("#" + oProv).empty().append(sb1);
			
			if(oCity != null)
			{
				//填充市数据
				fillCity(oProv, oCity, oCounty,$("#" + oProv).val(), cityID, isEdit);
			}
			
			
			if(oCounty != null)
			{					
				//填充县/区数据
				fillCounty(oCity,oCounty, $("#" + oCity).val(),countyID, isEdit);
			}
			
			if(oCity != null)
			{
				$("#" + oProv).change(function(){			
					fillCity(oProv,oCity, oCounty,$("#" + oProv).val(), cityID, isEdit);			
					if(oCounty != null)
					{					
						//fillCounty(oCity,oCounty, $("#" + oCity).val(), countyID, isEdit);			
					}
					else
					{
						$("#" + oCity).unbind( "change" )
					}
				});	
			}
			else
			{
				$("#" + oProv).unbind( "change" )
			}						
		
			if(oCounty != null)
			{						
				$("#" + oCity).change(function(){
					fillCounty(oCity,oCounty, $("#" + oCity).val(), countyID, isEdit);
				});		
			}
			else
			{
				$("#" + oCity).unbind( "change" )
			}
		}											
}

//填充市数据
function fillCity(oProvince, oCity, oCounty,provID, cityID, isEdit)
{			
	if(arLay2 != null && oCity != null)	
	{		
		var sb2 = "";
					
		if( provID != -1)
		{
			for(var i = 0; i < arLay2.length;i++)
			{
				if( arLay2[i][1] == provID)
				{
					var strSel = "";
					if( cityID == arLay2[i][0])
					{
							strSel = " selected ";
					}					
					sb2 += "<option value='" + arLay2[i][0] + "'" + strSel + ">" + arLay2[i][2] + "</option>";
				}					
			}				
		}		
		if( sb2 == "")
		{
			if(isEdit && provID == -1)
			{
				sb2 = "<option value='-1'>城市</option>"
				$("#" + oCity).empty().append(sb2);			
				$("#" + oCity).show();
			}
			else
			{
				$("#" + oCity).hide();
				if( oCounty != null)
				{
					$("#" + oCounty).empty();
					$("#" + oCounty).hide();
				}
			}
		}
		else
		{			
			if( isEdit)
			{
				sb2 = "<option value='-1'>城市</option>" + sb2;
			}
									
			$("#" + oCity).empty().append(sb2);			
			$("#" + oCity).show();
		}			
	}
}

function fillCounty(oCity, oCounty, cityID,countyID, isEdit)
{
	
	//填充县/区数据
		if(arLay3 != null && oCounty != null)
		{
			var sb3 = "";
			if( cityID != -1)
			{
				for(var i = 0; i < arLay3.length;i++)
				{
					if( arLay3[i][1] == cityID)
					{
						var strSel = "";
						if( countyID == arLay3[i][0])
						{
							strSel = " selected ";
						}
						sb3 += "<option value='" + arLay3[i][0] + "'" + strSel + ">" + arLay3[i][2] + "</option>";
					}					
				}
			}
			
			
			if( sb3 == "") //找不到地区信息
			{
				//如果当前是编辑				
				if(isEdit && cityID == -1 && $("#" + oCity + " > option").length >0)
				{
					sb3 = "<option value='-1'>县/区</option>";	
					$("#" + oCounty).empty().append(sb3);										
					$("#" + oCounty).show();				
				}					
				else
				{					
					$("#" + oCounty).hide();
				}
			}
			else
			{
				if(isEdit)
				{
					sb3 = "<option value='-1'>县/区</option>" + sb3;
				}
				$("#" + oCounty).empty().append(sb3);
				$("#" + oCounty).show();
			}
		}
		else
		{
			if(oCounty != null)
			{
				$("#" + oCounty).empty();
				$("#" + oCounty).hide();
			}
		}
	}