function ajaxgoodstype(catid,goodsid){
    $.get(APP+"Goods/attr_html.html", {catid:catid,goodsid:goodsid}, function(data){
    		$("#ajaxtable_group_type").html(data);
	});
}
function ajaxpgoodstype(goodsid){//订货商品
    $.get(APP+"Goods/pattr_html.html", {goodsid:goodsid}, function(data){
    		$("#ajaxtable_group_type").html(data);
    		if(data){
    			$("#goods_stock").css('display','table-row');
    		}
	});
}
function addRow(obj,type,attr_id){//属性+
	var html = $(obj).parent().parent().html();
	html = html.replace("addRow", "delRow");
	html = html.replace("+", "-");
	if(type==1){//货品
		var num=parseInt($("input[name='count']").val())+1;
		$("input[name='count']").val(num);
		html = html.replace(/\]\[1\]/g, "]["+num+"]");
		html = html.replace(/showdiv_1/g, "showdiv_"+num);
		html = html.replace("add", "del");
		$("<tr id='showdiv_"+num+"'>"+html+"</tr>").insertAfter($(obj).parent().parent());
		$('#showdiv_'+num).find('input').val('');
		$('#showdiv_'+num).find('select').val('');
	}else if(type=='ser'){//服务属性
		var num=parseInt($("input[name='count']").val())+1;
		$("input[name='count']").val(num);
		html = html.replace(/\]\[1\]/g, "]["+num+"]");
		html = html.replace(/showdiv_1/g, "showdiv_"+num);
		html = html.replace("add", "del");
		$("<tr id='showdiv_"+num+"'>"+html+"</tr>").insertAfter($(obj).parent().parent());
		$('#showdiv_'+num).find('input').val('');
		$('#showdiv_'+num).find('select').val('');
	}else{//属性
		var num=parseInt($("input[name='"+attr_id+"_count']").val())+1;
		$("input[name='"+attr_id+"_count']").val(num);
		html = html.replace(/\]\[1\]/g, "]["+num+"]");
		html = html.replace(/showdiv_1/g, "showdiv_"+num);
		html = html.replace(/img1_button/g, "img"+num+"_button");
		html = html.replace(/_img1_id/g, "img"+num+"_id");
		$("<div class='clearfix attribute mb10' id='showdiv_"+num+"_"+attr_id+"'>"+html+"</div>").insertAfter($(obj).parent().parent());
		$('#showdiv_'+num+"_"+attr_id).find('input').val('');
		$('#showdiv_'+num+"_"+attr_id).find('select').val('');
		$('#showdiv_'+num+"_"+attr_id).find('img').attr('src','/Public/images/add_img_icon.png');
	}
}
function delRow(obj,attrid){//属性-
	if(attrid != 'ser'){
		var attr_ids_val = $('#attr_ids').val();
        if(attr_ids_val){
             var attr_ids = attr_ids_val.replace(','+attrid+',',",");
		     $('#attr_ids').val(attr_ids);
        }
	}
	$(obj).parent().parent().remove();
}
function check_same(obj){//检查配置
	var selectbox = $(obj).parent().parent().find("select");
	var row_value = '';
	for(i=0;i<selectbox.length;i++){
		if($(selectbox[i]).val()!='')
			row_value += $(selectbox[i]).val();
		else
		{
			$(obj).parent().parent().find("input[name='stock_cfg[]']").val("");
			return;
		}
	}
	//开始检测是否存在该配置
	var stock_cfg = $("input[name='stock_cfg[]']");
	for(i=0;i<stock_cfg.length;i++){
		if(row_value==$(stock_cfg[i]).val()&&row_value!=''&&stock_cfg[i]!=obj)
		{
			$.showSuccess('属性规格组合重复');
			$(obj).parent().parent().find("input[name='stock_cfg[]']").val("");
			$(obj).val("");
			return;
		}
	}
	$(obj).parent().parent().find("input[name='stock_cfg[]']").val(row_value);
}
function check_attr_sn(obj){//检查货品编号是否重复
	var attr_sn = '';
	if($(obj).val()!=''){
		attr_sn = $(obj).val(); 
	}else{
		$(obj).parent().parent().find("input[name='goods_attr_sn[]']").val("");
		return;
	}
	//开始检测
	var goods_attr_sn = $("input[name='goods_attr_sn[]']");
	var errmsg = '【'+attr_sn+'】此货品编号已存在，请重新输入';
	if($(obj).parent().parent().find("input[name='goods_attr_sn[]']").val()!=attr_sn){
		for(i=0;i<goods_attr_sn.length;i++){
			if(attr_sn==$(goods_attr_sn[i]).val()&&attr_sn!=''&&goods_attr_sn[i]!=obj){
				$.showSuccess(errmsg);
				$(obj).parent().parent().find("input[name='goods_attr_sn[]']").val("");
				$(obj).val("");
				return;
			}
		}
		var query = new Object();
		query.goods_attr_sn = attr_sn;
		var url = APP + "Goods/check_goods_attr_sn.html"
		$.ajax({
			url: url,
			data: query,
			type: "post",
			dataType: "json",
			success: function (data) {
				if (data.info==1) {
					$.showSuccess(errmsg);
					$(obj).parent().parent().find("input[name='goods_attr_sn[]']").val("");
					$(obj).val("");
					return;
				}else{
					$(obj).parent().parent().find("input[name='goods_attr_sn[]']").val(attr_sn);
				}
			}
		});
	}
}
function search_brand() {
	var shop_id=$("input[name='shop_id']").val();
    var keyword = $('#search_brand_keyword').val();
    var query = new Object();
    query.keyword = keyword;
	query.shop_id=shop_id;
    var url = APP + "Goods/search_brand.html"
    $.ajax({
        url: url,
        data: query,
        type: "post",
        dataType: "json",
        success: function (obj) {
            if (obj.status) {
                $('#brand_id').html(obj.options);
                return false;
            } else {
                return false;
            }
        }
    });
}
function search_suppliers() {
    var keyword = $('#search_suppliers_keyword').val();
    var query = new Object();
    query.keyword = keyword;
    var url = APP + "Goods/search_suppliers.html"
    $.ajax({
        url: url,
        data: query,
        type: "post",
        dataType: "json",
        success: function (obj) {
            if (obj.status) {
                $('#suppliers_id').html(obj.options);
                return false;
            } else {
                return false;
            }
        }
    });
}
function change_marketprice(){
	var shopprice=$('#shop_price').val();
	var rate=$('#rate_config').val();
	var marketprice=(rate/100*shopprice).toFixed(2);
	if(marketprice && marketprice != 'NaN'){
		$('#market_price').val(marketprice);
	}
}




function ajaxaddRow(){//属性+
	var attrid = $('#attrid').val();
	if(attrid == ''){
		$.showErr('请选择属性！');return;
	}
	
	var attr_arr = new Array();
	var attr_ids_val = $('#attr_ids').val();
	attr_arr = attr_ids_val.split(",");
	if(attr_ids_val == ''){
		$('#attr_ids').val(',');
		var attr_ids_val = $('#attr_ids').val();
	}
	
	for(var i = 0 ;i<attr_arr.length;i++){
        if(attr_arr[i] == "" || typeof(attr_arr[i]) == "undefined"){
            attr_arr.splice(i,1);
            i= i-1;
        }
    }
	
	if(attr_ids_val.indexOf(',' + attrid + ',') > -1){
		$.showErr('已添加过该属性值了！');return;
	}
	if(attr_arr.length > 2){
		$.showErr('最多可选三个属性！');return;
	}
	var query = new Object();
	query.attrid = attrid;
	
	var url = APP + "Goods/search_attrinfo.html"
	$.ajax({
		url: url,
		data: query,
		type: "post",
		success: function (ret) {
			if(ret.status){
				$('#showdiv').append(ret.html);
				var attr_ids = attr_ids_val + attrid + ',';
				$('#attr_ids').val(attr_ids);
			}else{
				$.showErr(ret.msg);
			}
		}
	});
}
function addRowg(obj,type,attr_id){//添加组合商品
	var html = $(obj).parent().parent().html();
	html = html.replace("addRowg", "delRowg");
	html = html.replace("+", "-");

		var num=parseInt($("input[name='count']").val())+1;
		$("input[name='count']").val(num);
		html = html.replace(/\]\[1\]/g, "]["+num+"]");
		html = html.replace(/showdiv_1/g, "showdiv_"+num);
		html = html.replace("add", "del");
		$("<tr id='showdiv_"+num+"'>"+html+"</tr>").insertAfter($(obj).parent().parent());
		//$('#showdiv_'+num).find('input').val('');
		$('#showdiv_'+num).find('select').val('');
		$('#showdiv_'+num).find('span').html('');

}
function delRowg(obj){//删除组合商品
	$(obj).parent().parent().remove();
}
function change_type(type){
	setTimeout(function(){
		if(type==1){
			$("#groupgoods").attr('style','');
			$("#limit_lower_count").attr('style','display:none;');
			$("#is_type_1").attr('disabled','true');
			//$("#supply_price").attr('style',"display:none;");
			$("#goods_attr").attr('style',"display:none;");
			$("#is_show").attr('style',"display:none;");
		}else{
			$("#groupgoods").attr('style',"display:none;");
			$("#limit_lower_count").attr('style','');
			//$("#supply_price").attr('style',"");
			$("#is_type_1").removeAttr('disabled');
			$("#goods_attr").attr('style',"");
			$("#is_show").attr('style',"");
		}
	},2000);
}
function ratio_num(obj){
	var num = parseFloat($(obj).val());
	var is_type = $(obj).parent().next().val();
	if(!isNaN(num)){
		num = 1;
	}
	if(is_type == 0 && String(num).indexOf(".")>-1){
		$.showErr('该商品为普通商品，必须为整数');return false;
	}
}