$(document).ready(function(){
	//修改商品
	//if(pid != '0' && cid != '0'){
        //绑定属性和参数
        var props=propertes.props;
		var paras=propertes.paras;
		$.each(props,function(i,n){
			if(n.value!=0){
				$("select[name=prop]").eq(i).val(n.value);
			};
		});
		$.each(paras,function(i,n){
			$.each(n.options,function(j,m){
				$("input[name=group]").eq(n.id-1).siblings("dl").find("input[name=paravalue]").eq(m.index-1).val(m.value);
			});
		});
    //};
    
    $('.upic').change(function(){
        var filename=$(this).val().replace(/.*(\/|\\)/, "");//文件名
        var fileExt=(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';//后缀名
        if(fileExt != "jpeg" && fileExt != "jpg" && fileExt != "gif"){
            alert('请选择一个jpg后gif的图片文件');
            return;
        };
		$(this).parent().parent().parent().addClass('support');
		if($.browser.mozilla)
		{
			var data=$(this)[0].files.item(0).getAsDataURL();
			$(this).parent().parent().siblings(".preview").html('<img src="'+data+'"/>');
			return;
		}
		$(this).parent().parent().siblings(".preview").html('<img src="'+$(this).val()+'"/>');
		$(this).parent().parent().parent().mouseover(function(){$(this).addClass('hover');}).mouseout(function(){$(this).removeClass('hover');});		
    });
    $('.del').click(function(){
		var file = $(this).parent().siblings(".upload").children().children(".upic");
		file.after(file.clone(true).val("")).remove();
		$(this).parent().siblings(".preview").empty();
		$(this).parent().parent().removeClass('support').mouseout().unbind('mouseover');
    });
    if($.browser.msie){
		$('li').filter('.support').mouseover(function(){$(this).addClass('hover');}).mouseout(function(){$(this).removeClass('hover');});
	};
	/*
	$('#btnSave').click(function(){
		GetPropPara();
	});
	*/
	$.validator.methods.strlen= function(value,element,param){
       var  str =$(element).val();
       var  len =0;
       for(var i=0;i<str.length;i++){
          if(str.charCodeAt(i)>255){
             len +=2;
          }
          else{
             len +=1;
          }
       }
       return (len -param)<=0;
	}
	//$("#form1").validate({meta: "validate",debug:"ture"});
	$("#form1").validate({
		event: "keyup",
		rules: {
			tbName: {
				required: true,
				maxlength:60
			},
			tbSubName: {
				strlen:40
			},
			tbWeight: {
				required: true,
				number:true,
				range: [0.001,100]
			},
			tbMarketPrice: {
				required: true,
				number:true,
				range: [0,999999]
			},
			tbMemberPrice: {
				required: true,
				number:true,
				range: [0,999999]
			},
			tbStorage: {
				required: true,
				number:true,
				range: [1,999999]
			}
		},
		messages: {
			tbName: {
				required: "请输入商品名称",
				maxlength: "最大长度只能在30个汉字(60个字符)以内"
			},
			tbSubName: {
				strlen: "最多允许输入20个汉字40个字符)"
			},
			tbWeight: {
				required: "请输入商品重量",
				range: "请输入合法的商品重量"
			},
			tbMarketPrice: {
				required: "请输入市场价",
				range: "请输入合法的市场价(0-999999)"
			},
			tbMemberPrice: {
				required: "请输入会员价",
				range: "请输入合法的会员价(0-999999)"
			},
			tbStorage: {
				required: "请输入库存",
				range: "请输入合法的库存(1-999999)"
			}
		},
		debug:"false",
		submitHandler:function(form){
			GetPropPara();
			alert("提交表单");
			//$(form).ajaxSubmit();
			form.submit();
		}
	});
});
function GetPropPara()
{
	//参数
	var gcount=$("input[name=group]").length;
	var paras='"paras":[';
	if(gcount!=0){
		var flag=false;
		$("input[name=group]").each(function(i){
			var tempGroup='{"id":"'+(i+1)+'","group":"'+$(this).val()+'","options":[';
			var paraname=$(this).siblings("dl").find("input[name=paraname]");
			var tempGroupValue="";
			paraname.each(function(j){
				var tempValue=$(this).next().val().Trim();
				if(tempValue!=""){
					tempGroupValue+='{"index":"'+(j+1)+'","name":"'+$(this).val()+'","value":"'+tempValue+'"}';
					tempGroupValue+=',';
				};
			});
			if(tempGroupValue!=""){
				tempGroupValue=tempGroupValue.substring(0,tempGroupValue.length-1);
				paras+=tempGroup;
				paras+=tempGroupValue;
				paras+=']}'
				paras+=',';
				flag=true;
			};			
		});
		if(flag){
			paras=paras.substring(0,paras.length-1);
		};		
	};
	paras+=']';
	$('input:hidden[name="paras"]').val(paras);
}

