/*行业选择弹出层填充数据*/
function trade_filldata(fillID, data_resources, determinebtn, deselectbtn, showID, resultshowID, resultID, resultCN) {
	var tradhtm = '';
	var sourse_length = parseInt(data_resources.length);
	var rows = 0;
	var subscriptnum = 0;
	if((sourse_length%3) == 0) {
		rows = sourse_length / 3;
	} else {
		rows = (sourse_length / 3) + 1;
	}
	for (var i = 0; i < rows; i++) {
		tradhtm += '<tr>';
		for (var j = 0; j < 3; j++) {
			if (data_resources[subscriptnum]) {
				var trad_array = data_resources[subscriptnum].split(",");
				tradhtm += '<td><label><input type="checkbox" data="'+trad_array[0]+','+trad_array[1]+'" name="item-list" class="input-checkbox">'+trad_array[1]+'</label></td>';
				subscriptnum ++;
			};
		};
		tradhtm += '</tr>';
	};
	$(fillID).html(tradhtm);
	// 恢复选中
	if ($(resultID).val().length > 0) {
		var idarray = $(resultID).val().split(",");
		$.each(idarray, function(index, val) {
			$(fillID+" :checkbox").unbind().each(function() {
				var boxdata_array = $(this).attr("data").split(",");
				if(val == boxdata_array[0]) {
					$(this).attr('checked',true);
				}
			});
		});
		copy_selected();
		isexceed($(fillID+" :checkbox[checked]").length);
		// 单选
		$(fillID+" label :checkbox").unbind().live("click",function(event) {
			if ($(this).find(":checkbox").attr('checked')) {
				$(this).find(":checkbox").attr('checked',false);
				isexceed($(fillID+" :checkbox[checked]").length);
			} else {
				$(this).find(":checkbox").attr('checked',true);
				isexceed($(fillID+" :checkbox[checked]").length);
			}
		});
	};
	// 单选
	$(fillID+" label :checkbox").unbind().live("click",function(event) {
		if ($(this).find(":checkbox").attr('checked')) {
			$(this).find(":checkbox").attr('checked',false);
			isexceed($(fillID+" :checkbox[checked]").length);
		} else {
			$(this).find(":checkbox").attr('checked',true);
			isexceed($(fillID+" :checkbox[checked]").length);
		}
	});
	/*判断选中的数量是否超出最大限制*/
	function isexceed(num) {
		if(num >= 1) {
			$(fillID+" :checkbox").each(function(index, el) {
				if (!$(this).attr('checked')) {
					$(this).attr('disabled',"disabled");
				}
			});
		} else {
			$(fillID+" :checkbox").each(function(index, el) {
				$(this).attr('disabled',false);
			});
		}
	}
	// 确定
	$(determinebtn).unbind().click(function() {
		copy_selected();
		close_dialog();
		searchHistory();
	});
	// 取消
	$(deselectbtn).unbind().click(function() {
		close_dialog();
	});
	// 弹出框关闭
	function close_dialog() {
		$("#maskLayer").remove();
		$(showID).hide();
	}
	// 复制选中 赋值
	function copy_selected() {
		var selectedhtm = "";
		var selectedid_array = new Array();
		var selectedcn_array = new Array();
		$(fillID+" :checkbox[checked]").unbind().each(function(index) {
			var data_array = $(this).attr("data").split(",");
			selectedhtm += '<span data-id="'+data_array[0]+'">'+data_array[1]+'<a class="close" style="top: 2px;"></a></span>';
			selectedid_array[index]=data_array[0];
			selectedcn_array[index]=data_array[1];
		});
		$(resultshowID + " .items").html(selectedhtm);
		var spanwidth = 0, itemswidth = $(resultshowID).width()-28, widtharray = new Array();
		if($(resultshowID + " .items span")) {
			$(resultshowID + " .items span").each(function(index) {
				spanwidth = spanwidth + $(this).outerWidth(true);
				widtharray[index] = $(this).outerWidth(true);
			})
		}
		var difwidth=spanwidth-itemswidth;
		if (widtharray.length == 2) {
			if(widtharray[0] + widtharray[1] > itemswidth) {
				setHeightCss(40);
			} else {
				setHeightCss(20);
			}
		} else if (widtharray.length == 3) {
			if(widtharray[0] + widtharray[1] + widtharray[2] > itemswidth) {
				if(widtharray[0] + widtharray[1] > itemswidth) {
					if(widtharray[1] + widtharray[2] > itemswidth) {
						setHeightCss(60);
					} else {
						setHeightCss(40);
					}
				} else {
					setHeightCss(40);
				}
			}
		} else {
			setHeightCss(20);
		}
		function setHeightCss (val) {
			$(resultshowID).css("height",val+"px");
			$(resultshowID + " .items").css("height",val+"px");
			$(resultshowID + " em").css("height",val+10+"px");
		}
		$(resultshowID + " span").unbind().click(function() {
			var dtid = $(this).attr("data-id");
			$(fillID+" :checkbox[checked]").unbind().each(function() {
				var dt_array = $(this).attr("data").split(",");
				if(dtid == dt_array[0]) {
					$(this).attr('checked',false);
				}
			});
			isexceed($(fillID+" :checkbox[checked]").length);
			copy_selected();
		});
		$(resultID).val(selectedid_array.join(","));
		$(resultCN).val(selectedcn_array.join(","));
	}
}