var cid = getQueryStringRegExp('cid');
$(document).ready(function(){
	$("#cbAddToAdvSearch").click(function(){
		if($(this).attr("checked")){
			$("#AdvSearch").show();
		}else{
			$("#AdvSearch").hide();
		};
	});
	$("#btnAddFile").click(function(){
		AddField(0,"","","<option value='1'>筛选-不可筛选</option><option value='2'>筛选-单选</option><option value='3'>筛选-多选</option>");
	});
	$("#btnSubmit").click(function(){
		//if($('#txtFieldName').val().Trim() == ''){alert('属性名称必须填写');return false;};
		if($('#ddlCategories').val().Trim() == null){alert('请选择属性组');return false;};
		
		var flag=true;
		$("#field_body").children().each(function(i){
			var fn=$(this).children().children("input[@name^=fn]").val().Trim();
			var fv=$(this).children().children("input[@name^=fv]").val().Trim();
			if(fn=="" || fv==""){
				alert("属性名称和预设值不能为空！");
				flag=false;
			//}
			//else if(fv.indexOf(",")!=-1){
				//alert("预设值不能含有,");
				//$(this).children().children("input[@name^=fv]").val("");
				//flag=false;
			}else{
				$(this).children().children("input[@name^=fi]").attr("name","fi"+i);
				$(this).children().children("input[@name^=fn]").attr("name","fn"+i);
				$(this).children().children("input[@name^=fv]").attr("name","fv"+i);
				$(this).children().children("select[@name^=fs]").attr("name","fs"+i);
			};			
		});
		if(!flag){return false;}else{$("<input type='hidden' name='field_num'/>").val($("#field_body").children().length).appendTo($("#field"));};
	});
	if(cid != ''){
		$('#ddlCategories').val(cid);
		$.ajax({
			type:"get",
			dataType:"json",
            url:'ProductField.aspx?NoCopyRight=1',
            data:"type=ajax&Action=GetProductField&cid="+cid,
            cache:false,
            success:function(msg){
				var data = msg.Fields;
				$.each(data,function(i,n){
					var fi=n.ProductFieldID;
					var fn=n.FieldName;
					var fv=n.FieldValues;
					var advSear=n.AddToAdvSearch;
					var isMul=n.IsMultiple;
					if(advSear=="0"){
						AddField(fi,fn,fv,"<option value='1' selected>筛选-不可筛选</option><option value='2'>筛选-单选</option><option value='3'>筛选-多选</option>");
					}else{
						if(isMul=="0"){
							AddField(fi,fn,fv,"<option value='1'>筛选-不可筛选</option><option value='2' selected>筛选-单选</option><option value='3'>筛选-多选</option>");
						}else{
							AddField(fi,fn,fv,"<option value='1'>筛选-不可筛选</option><option value='2'>筛选-单选</option><option value='3' selected>筛选-多选</option>");
						};
					}
				});
            },//end success
            error:function(){alert('error');}
        });//end ajax
   }else{	
		AddField(0,"","","<option value='1'>筛选-不可筛选</option><option value='2'>筛选-单选</option><option value='3'>筛选-多选</option>");
   }
});

function AddField(fi,fn,fv,fs)
{
	var tempTr=null;
	tempTr=$("<tr><td class='fn'><input type='hidden' name='fi' value='"+fi+"' /><input type='text' value='"+fn+"' name='fn' class='fieldname' /></td><td class='fv'><input type='text' value='"+fv+"' name='fv' class='fieldvalue'/></td><td class='fs'><select name='fs'>"+fs+"</select></td></tr>").appendTo($("#field_body"));
	var tempControl = $("<td></td>").appendTo(tempTr);
	$("<span class='del'>X</span>").addClass("hand").appendTo(tempControl).click(function(){
		$(this).parent().parent().remove();
	});
}