//
var img_state = ["/image/check_right.gif", "/image/check_error.gif", "/image/loading.gif"];

Q().ready(function(){
	
	// reg
	Q.ajax({
		url: "/citylist.xml",
		success: function(xml){
			Q(xml).find("Country").each(function(){
				Q("#reg_country").append("<option value='" + Q(this).attr("Code") + "'>" + Q(this).attr("Name"));
			})
			
			Q("#reg_area_state").attr("src", img_state[0])
		}
	})
	
	Q("#reg_country").change(function(){
		change_reg_province();
	})

	Q("#reg_province").change(function(){
		change_reg_city();
	})
	

})

function change_reg_province()
{
	Q("#reg_area_state").attr("src", img_state[2]);
	Q("#reg_province").children().remove();
	Q("#reg_city").children().remove();
	var cid = Q("#reg_country").val();

	Q.ajax({
		url: "/citylist.xml",
		success: function(xml){
			var isset_state = Q(xml).find("Country[Code="+ cid +"]>State").attr("Code");
			if( typeof(isset_state) == 'undefined' )
			{
				Q(xml).find("Country[Code="+ cid +"]>State").find("City").each(function(){
					Q("#reg_city").append("<option value='" + Q(this).attr("Code") + "'>" + Q(this).attr("Name"));
				});
				
			}
			else
			{
				Q(xml).find("Country[Code="+ cid +"]>State").each(function(){
					Q("#reg_province").append("<option value='" + Q(this).attr("Code") + "'>" + Q(this).attr("Name"));
				});

				//

				var pid = Q("#reg_province").val();
				Q(xml).find("State[Code="+ pid +"]>City").each(function(){
					Q("#reg_city").append("<option value='" + Q(this).attr("Code") + "'>" + Q(this).attr("Name"));
				});
			}
			Q("#reg_area_state").attr("src", img_state[0]);
		}
	})
}

function change_reg_city()
{
	Q("#reg_area_state").attr("src", img_state[2]);
	Q("#reg_city").children().remove();
	var pid = Q("#reg_province").val();
	Q.ajax({
		url: "/citylist.xml",
		success: function(xml){
			Q(xml).find("State[Code="+ pid +"]>City").each(function(){
				Q("#reg_city").append("<option value='" + Q(this).attr("Code") + "'>" + Q(this).attr("Name"));
			});
			Q("#reg_area_state").attr("src", img_state[0]);
		}
	})
}

function change_building(boxid, city, area , prefix )
{
	//alert(boxid + "\n" + city+ "\n" +area+ "\n" +prefix);
	var p = prefix ? prefix : '';
	if( area == '' || city == '')
	{
		Q("#" + p + "build").html('<option value="0">暂无机构');
		return false;
	}
	if( !city || !area ) return false;
	var province = Q("#province").val();
	var addhref = '&nbsp;<a href="cp.php?ac=building&province='+province+'&city='+city+'&area='+area+'" title="若无机构或">发布机构</a>';

	Q.ajax({
		type: "GET",
		cache: false,
		url: "/ajax.php?ac=ajax_get_build",
		data: "city=" + escape(city) + "&area=" + escape(area),
		dataType: "json",
		success: function(res)
		{
			if( res == '0' )
			{
				Q("#" + p + "build").html('<option value="0">暂无机构.');
				Q("#" + p + "num").html('<option value="0">暂无楼号');
				Q("#" + p + "floor").html('<option value="0">暂无楼层');
			}
			else
			{
				
				var option = '<option value="0">选择机构';
				Q.each(res, function(k, v){
					option += '<option value="'+k+'">'+v; 
				});
				Q("#" + p + "build").html(option);
			}
		}
	}); 
}

function floor_del( id )
{
	Q("#state_"+id).html("<img src='image/load.gif'>&nbsp;");
	Q.ajax({
		type: "GET",
		url: "/ajax.php?ac=ajax_floor_del",
		data: "id=" + id,
		success: function(res)
		{
			if( res == '1' )
			{
				Q("#state_"+id).html("<img src='image/tick.gif'>&nbsp;");
				Q("#tr_"+id).fadeOut("slow");
			}
			else
			{
				Q("#state_"+id).html("<img src='image/cross.gif'>&nbsp;");
			}
		}
	}); 
}


function snum( prefix )
{
	var p = prefix ? prefix : '';
	
	var id = Q("#" + p + "build").val();
	Q.ajax({
		type: "GET",
		cache: false,
		url: "/ajax.php?ac=ajax_get_num",
		data: "id=" + id,
		dataType: "json",
		success: function(res)
		{
			if( res == '0' )
			{
				Q("#" + p + "num").html('<option value="0">暂无楼号');
				Q("#" + p + "floor").html('<option value="0">暂无楼层');
			}
			else
			{
				var option = '<option value="0">选择楼号';
				Q.each(res, function(k, v){
					option += '<option value="'+k+'">'+v; 
				});
		
				Q("#" + p + "num").html(option);
				Q("#" + p + "floor").html('<option value="0">选择楼层');
			}
		}
	}); 
}

function sfloor(prefix)
{
	var p = prefix ? prefix : '';

	var id = Q("#" + p + "num").val();
	
	Q.ajax({
		type: "GET",
		url: "/ajax.php?ac=ajax_get_floor",
		data: "id=" + id,
		dataType: "json",
		success: function(res)
		{
			if( res == '0' )
			{
				Q("#" + p + "floor").html('<option value="0">暂无楼层');
			}
			else
			{
				var option = '<option value="0">选择楼层';
				for( var i=1; i<=res; i++ )
				{
					option += '<option value="'+i+'">'+i; 
				}
				
				Q("#" + p + "floor").html(option);
			}
		}
	}); 
}

function ajax_get_subclass(subclass)
{
	Q("#state").html("<img src='../image/load.gif'>");
	var pid = Q("#pid").val();
	if (pid == 0)
	{
		Q("#state").html("<img src='../image/cross.gif'>");
		Q("#subclass").html('<option value="">暂无子类');
		return false;
	}
	Q.ajax({
		type: "GET",
		url: "/ajax.php?ac=ajax_get_subclass",
		data: "pid=" + pid,
		dataType: "json",
		success: function(res)
		{
			if( res == '0' )
			{
				Q("#state").html("<img src='../image/cross.gif'>");
				Q("#subclass").html('<option value="">暂无子类');
			}
			else
			{
				Q("#state").html("<img src='../image/tick.gif'>");
				var option = '<option value="">请选择';
				Q.each(res, function(k, v){
					option += '<option value="'+k+'">'+v.name; 
				});
		if (subclass==1) //这一行 和 上面的函数的参数liu add 
				Q("#subclass").html(option);			
			}
		}
	}); 
}