$(document).ready(function(){
	//修改商品
	if(pid != '0' && cid != '0'){
        //绑定属性和参数
        var props=propertes.props;
		var paras=propertes.paras;
		$.each(props,function(i,n){
			if(n!=undefined && n.value!=0){
				$("select[name=prop]").eq(n.id-1).val(n.value);
			};
		});
		if(paras!= undefined ){
		    $.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").uploadPreview({ width: 0, height: 0, imgDiv: ".preview", imgType: ["gif","jpg","jpeg"], maxwidth: 3000, maxheight: 3000 });
    $('.upic').change(function(){
        var filename=$(this).val().replace(/.*(\/|\\)/, "");//文件名
        var fileExt=(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';//后缀名
        if(fileExt != "jpeg" && fileExt != "jpg" && fileExt != "gif" && fileExt != "png"){
            alert('请选择一个jpg或gif的图片文件');
            return;
        };
		$(this).parent().parent().parent().addClass('support');
		$(this).parent().parent().siblings("[name='isDel']").val("1");
		if($.browser.msie)
		{
		    if($.browser.version == "6.0")
		    {
		        $(this).parent().parent().siblings(".preview").html('<img src="'+$(this).val()+'"/>');
		    }else{
		        var previewDiv =  $(this).parent().parent().siblings(".preview");
		        previewDiv.css({ filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)" });
                previewDiv[0].filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "scale";
                try 
                {
                    previewDiv[0].filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = getPath($(this)[0]);
                }
                catch (e) 
                {
                    alert("无效的图片文件！");
                    return;
                }
		    }
		}
		else
		{
			var data=$(this)[0].files.item(0).getAsDataURL();			
			$(this).parent().parent().siblings(".preview").html('<img src="'+data+'"/>');
			return;
		}
		
		$(this).parent().parent().parent().mouseover(function(){$(this).addClass('hover');}).mouseout(function(){$(this).removeClass('hover');});		
    });
    
    $('.moveleft').click(function(){
        var currObj = $(this).parent().parent();
        var addObj = currObj.prev().clone(true);
        if(addObj.hasClass("first")){
            currObj.addClass("first");
            addObj.removeClass("first");
        };
        if(currObj.hasClass("last")){
            currObj.removeClass("last");
            addObj.addClass("last");
        };
        currObj.after(addObj);
        currObj.prev().remove();       
    });
    
    $('.moveright').click(function(){
        var currObj = $(this).parent().parent();
        var addObj = currObj.next().clone(true);
        if(addObj.hasClass("last")){
            currObj.addClass("last");
            addObj.removeClass("last");
        };
        if(currObj.hasClass("first")){
            currObj.removeClass("first");
            addObj.addClass("first");
        };
        currObj.before(addObj);
        currObj.next().remove();
    });
    
    $('.del').click(function(){
        $(this).parent().siblings("[name='isDel']").val("1");
		var file = $(this).parent().siblings(".upload").children().children(".upic");
		file.after(file.clone(true).val("")).remove();
		$(this).parent().siblings(".preview").after('<div class="preview"></div>').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');});
	};
	$.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:50
			},
			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]
			},			
			ftbContent:{
				required: true
			},			
			
            ftbPackingList: {
				strlen:250
			},
			
            ftbAfterService: {
				strlen:500
			}
		},
		
		messages: {
			tbName: {
				required: "请输入商品名称",
				maxlength: "最大长度只能在30个汉字(60个字符)以内"
			},
			tbSubName: {
				strlen: "最多允许输入25个汉字50个字符)"
			},
			tbWeight: {
				required: "请输入商品重量",
				range: "请输入合法的商品重量"
			},
			tbMarketPrice: {
				required: "请输入市场价",
				range: "请输入合法的市场价(0-999999)"
			},
			tbMemberPrice: {
				required: "请输入会员价",
				range: "请输入合法的会员价(0-999999)"
			},
			tbStorage: {
				required: "请输入库存",
				range: "请输入合法的库存(1-999999)"
			},
            ftbContent: {
				required: "请输入商品详细描述"
			},
            ftbPackingList: {
				strlen: "最多允许输入250个汉字500个字符"
			},
            ftbAfterService: {
				strlen: "最多允许输入500个汉字1000个字符"
			}
		},
		debug:"false",
		submitHandler:function(form){
			GetPropPara();
			if(!CheckPrice())return false;
			//$(form).ajaxSubmit();
			form.submit();
		}
	});
});
function CheckPrice()
{
     var flag = true;
     $('input[name=price]').each(function(){
        if($(this).val()=='')
        {
            alert("请输入价格");
            flag = false;
            $(this).focus();
            return false;
        }
        if(isNaN(Number($(this).val())))
        {
            alert("请输入有效的价格");
            flag = false;
            $(this).focus();
            return false;
        }
//        if($(this).val()!=$(this).val().replace(/\D/g,""))
//        {
//            alert("请输入有效的价格");
//            $(this).focus();
//            return false;
//        }
    });
     return flag;
}
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().replace(/\"/g,"“");
				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);
}

//获得上传控件的值，obj为上传控件对象
function getPath(obj) {
    if (obj) {
        if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
            obj.select();
            return document.selection.createRange().text;
        }
        else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
            if (obj.files) {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}
//清空上传控件的值，obj为上传控件对象
function clearvalue(obj) {
    obj.select();
    document.execCommand("delete");
}  

