/**
 * 选择配送区域
 */
function addRegion(){
	var sheng = $('#province').val();
	var shi = $('#city_id').val();
	var xian = $('#district').val();
	var areas  = document.getElementById("areas");
	var sheng_name = document.forms['myform'].elements['province'];
	var shi_name = document.forms['myform'].elements['city_id'];
	var xian_name = document.forms['myform'].elements['district'];
	if (xian > 0){
		regionId = xian;
		regionName = xian_name.options[xian_name.selectedIndex].text;
	}else{
		if (shi > 0){
			regionId = shi;
			regionName = shi_name.options[shi_name.selectedIndex].text;
		}else{
			if (sheng > 0){
				regionId = sheng;
				regionName = sheng_name.options[sheng_name.selectedIndex].text;
			}else{
				return;
			}
		}
	}
	// 检查该地区是否已经存在
	exists = false;
	for(i = 0; i < document.forms['myform'].elements.length; i++){
		if(document.forms['myform'].elements[i].type=="checkbox"){
			if(document.forms['myform'].elements[i].value == regionId){
				exists = true;alert('选定的地区已经存在');
			}
		}
	}
	// 创建checkbox
	if (!exists){areas.innerHTML += "<input type='checkbox' name='regions[]' value='" + regionId + "' checked='true' /> " + regionName + "&nbsp;&nbsp;";}
}
/**
 * 添加
 */
function add_area(){
	var type = $("input[name='type']:checked").val();
	var check=sub_check(type);
	if(!check){return false;}
	
	var query=new Object();
	query = $("form[name='myform']").serialize();
	$.ajax({
		type:'post',
		data:query,
		url:APP+"Shop/Order/add_ship.html",
		success:function(data){
			if(data.status==1){
				$.showSuccess(data.info,0,'hc');
				window.location.href=data.url;
			}else{
				$.showErr(data.info,0,'hc');
			}
		}
	});
};
/**
 * 提交验证
 */
function sub_check(type){
	var name=$("input[name='name']").val();
	if(name==''){$.showErr('请填写配送区域名称',0,'hc');return false;}
	
	var money=$("input[name='money']").val();
	if(money==''){$.showErr('请填写免费额度',0,'hc');return false;}
	if(money<=0 || isNaN(money)){$.showErr('请填写正确免费额度',0,'hc');return false;}
	
	if(type == 1){
		var first_weight=$("input[name='first_weight']").val();
		if(first_weight==''){$.showErr('请填写首重',0,'hc');return false;}
		if(first_weight<=0 || isNaN(first_weight)){$.showErr('请填写正确的首重',0,'hc');return false;}
		
		var first_weight_money=$("input[name='first_weight_money']").val();
		if(first_weight_money==''){$.showErr('请填写首重费用',0,'hc');return false;}
		if(first_weight_money<=0 || isNaN(first_weight_money)){$.showErr('请填写正确首重费用',0,'hc');return false;}
		
		var x_weight=$("input[name='x_weight']").val();
		if(x_weight==''){$.showErr('请填写续重',0,'hc');return false;}
		if(x_weight<=0 || isNaN(x_weight)){$.showErr('请填写正确的续重',0,'hc');return false;}
		
		var x_weight_money=$("input[name='x_weight_money']").val();
		if(x_weight_money==''){$.showErr('请填写续重费用',0,'hc');return false;}
		if(x_weight_money<=0 || isNaN(x_weight_money)){$.showErr('请填写正确续重费用',0,'hc');return false;}
		
	}else if(type == 2){
		var per_money=$("input[name='per_money']").val();
		if(per_money==''){$.showErr('请填写单件商品费用',0,'hc');return false;}
		if(per_money<=0 || isNaN(per_money)){$.showErr('请填写正确的单件商品费用',0,'hc');return false;}
		
	}
	return true;
}