function addVariety(){
	var tempHtml = $.Template7.renderTpl("vareity_template",{index:$("#vareity_info>.box-pd").length});
	$("#vareity_info").append(tempHtml);
	$.resetText();
	$(".select").uiSelect();
}

function delVarety(obj, id){
	$(obj).closest(".box-pd").remove();
	if(id){
		ajax("",{id:id},function(){

		},false);
	}
}

function addNetWork(){
	var html = $.Template7.renderTpl("netWork_tempalte",{});
	$("#netWorkTerrace").append(html);
	$.resetText();
	$(".select").uiSelect();
	//$(".network-table").hoverTable(true, true);
}

function removeNetWork(obj, id){
	$(obj).closest("tr").remove();
	if(id){
		ajax("",{id:id},function(){

		},false);
	}
}

function formatIndex(){
	var index = $(".tv-box").length;
	for(var i = 0;i<index;i++){
		$(".tv-box:eq("+i+")").find(".font20").text(i < 9 ? "0"+parseInt(i+1) : parseInt(i+1));
	}
}

function addTv(){
	var html = $.Template7.renderTpl("tv_tempalte",{});
	$("#tvlist").append(html);
	formatIndex();
	addNetGrade($(".tv-box").find(".grade").get($(".tv-box").length - 1));
	$(".text").input();
	$(".netLastDate").datetimepicker();
}

function addNetGrade(obj){
	var html = $.Template7.renderTpl("netWork_tempalte",{});
	$(obj).next().find("tbody").append(html);
	$.resetText();
	$(".select").uiSelect();
	//$(obj).next().hoverTable(true, true);
}

function removeTv(obj, id){
	$(obj).closest(".box-pd").remove();
	formatIndex();
	if(id){
		ajax("",{id:id},function(){

		},false);
	}
}
//相加
function accDiv(arg1,arg2){ 
 var t1=0,t2=0,r1,r2; 
 if(arg1==0)
 {
	 return arg2;
 }
 if(arg2==0)
 {
	 return arg1;
 }
try{
    t1=arg1.toString().split(".")[1].length
   }catch(e){
	   
   }
try{
  t2=arg2.toString().split(".")[1].length
  }catch(e){
	  
  } 
  with(Math){ 
   r1=Number(arg1.toString().replace(".","")) 
   r2=Number(arg2.toString().replace(".","")) 
   return (r1/r2)*pow(10,t2-t1);
}
}
//保存综艺和栏目
function saveVareityData(){
	var tvData = [], netData = [],netGroup = {},doubanData ={};
	var flag = true;
	//节目id
	var programId = $("#programId").val();
	
	var tvProps = ["audienceRating4","avgNum4","audienceRating1845Woman","avgNum1845Woman","audienceRating1830Woman","avgNum1830Woman","audienceRating2045Man","avgNum2045Man","audienceRating2045","avgNum2045","sexMan","sexWoman","age17","age1824","age2530","age3135","age3640","age4150","age5160","age60","platform","type"];
	var tvPropsCH = ["4+收视率","4+到达率","女性18-45岁收视率","女性18-45岁到达率","女性18-30岁收视率","女性18-30岁到达率","男性20-45岁收视率","男性20-45岁到达率","全部20-45岁收视率","全部20-45岁到达率","男性","女性","1-17岁","18-24岁","25-20岁","31-35岁","35-40岁","41-50岁","51-60岁","60岁以上","播放电视台"];
	var tvPlatform = []
	//电视台数据
	$('#vareity_info .box-pd').each(function(){
		var obj = new Object();
		var obj1 = new Object();
		//35城数据
		obj[tvProps[tvProps.length-2]] = $(this).find('option:selected').val();
		if(isInArray2(tvPlatform,obj[tvProps[tvProps.length-2]])){
			top.alert("电视台数据:播放电视台不允许重复");
			flag = false;
			return false;
		}else{
			tvPlatform.push(obj[tvProps[tvProps.length-2]]);
		}
		var onOff = true;
		$(this).find(".variety-50.line  td input").each(function(index){
			obj[tvProps[index]] = $(this).val();
			if(obj[tvProps[index]]!= '' && !isNumber(obj[tvProps[index]])){
				top.alert("电视台数据:35城" + tvPropsCH[index] + "请输入非负数");
				onOff = false;
				return false;
			}
			if(index == 1 || index == 3 || index == 5 || index == 7 || index == 9){
				if(obj[tvProps[index]]!= '' && !isInteger(Number(obj[tvProps[index]]))){
					top.alert("电视台数据:35城" + tvPropsCH[index] + "请输入整数")
					onOff = false;
					return false;
				}
			}
		})
		if(!onOff){
			flag = false;
			return false;
		}
		var sex = parseFloat(nullToZero(obj[tvProps[10]])) + parseFloat(nullToZero(obj[tvProps[11]]));
		if(parseInt(sex) != 0 && parseInt(sex) != 100){
			top.alert("电视台数据:35城男女比例之和不等于100");
			flag = false;
			return false;
		}
		var age = 0;
		for(var i= 12; i< 20; i++){
			//age = parseFloat(nullToZero(age)) + parseFloat(nullToZero(obj[tvProps[i]]));
			age=accDiv(nullToZero(age),nullToZero(obj[tvProps[i]]))
		}
		if(parseInt(age) != 0 && parseInt(age) != 100){
			top.alert("电视台数据:35城受众人群构成比例之和不等于100");
			flag = false;
			return false;
		}

		obj[tvProps[tvProps.length-1]] = 1;		
		tvData.push(obj);
		//29城数据
		var onOff1 = true;
		obj1[tvProps[tvProps.length-2]] = $(this).find('option:selected').val();
		$(this).find(".variety-50.nop td input").each(function(index){
			obj1[tvProps[index]] = $(this).val();
			if(obj1[tvProps[index]]!= '' && !isNumber(obj1[tvProps[index]])){
				top.alert("电视台数据:29省城" + tvPropsCH[index] + "请输入非负数");
				onOff1 = false;
				return false;
			}
			if(index == 1 || index == 3 || index == 5 || index == 7 || index == 9){
				if(obj1[tvProps[index]]!= '' && !isInteger(Number(obj1[tvProps[index]]))){
					top.alert("电视台数据:29城" + tvPropsCH[index] + "请输入整数")
					onOff1 = false;
					return false;
				}
			}
		})
		if(!onOff1){
			flag = false;
			return false;
		}
		var sex1 = parseFloat(nullToZero(obj1[tvProps[10]])) + parseFloat(nullToZero(obj1[tvProps[11]]));
		if( parseInt(sex1) != 0 && parseInt(sex1) != 100){
			top.alert("电视台数据:29省城男女比例之和不等于100");
			flag = false;
			return false;
		}
		var age1 = 0;
		for(var i= 12; i< 20; i++){
			age1=accDiv(nullToZero(age1),nullToZero(obj[tvProps[i]]))
			//age1 = parseFloat(nullToZero(age1)) + parseFloat(nullToZero(obj1[tvProps[i]]));
		}
		if(parseInt(age1) != 0 && parseInt(age1) != 100){
			top.alert("电视台数据:29城受众人群构成比例之和不等于100");
			flag = false;
			return false;
		}
		
		obj1[tvProps[tvProps.length-1]] = 2;		
		tvData.push(obj1);
	})
	
	if(!flag){
		return false;
	}
	
	var flag1 = true;
	//网络平台数据
	var netLastDate = $("#netLastDate").val();
	var netProps = ["platformName","clickTimes","score","lastDate"];
	var netPropsCH = ["视频平台","点击量","评分","截止日期"];
	var platform = [];
	$("#netWorkTerrace tr").each(function(){
		var obj = new Object();
		$(this).find('td input,select').each(function(n){
			obj[netProps[n]] = $(this).val();
			if(n == 0){
				if(isInArray2(platform,obj[netProps[n]])){
					top.alert("网络平台数据:视频平台不能重复");
					flag1 = false;
					return false;
				}else{
					platform.push(obj[netProps[n]]);
				}	
			}
			if(n!= 0 && obj[netProps[n]] != '' && !isNumber(obj[netProps[n]])){
				top.alert("网络平台数据:" + netPropsCH[n] + "请输入非负数");
				flag1 = false;
				return false;
			}
			
			if(n == 2 && parseFloat(obj[netProps[2]])>10){
				top.alert("网络平台数据:评分不能大于10分");
				flag1 = false;
				return false;
			}
		});
		obj[netProps[netProps.length-1]] = netLastDate;
		netData.push(obj);
	});
	if(!flag1){
		return false; 
	}
	//网络平台人群受众构成
	var netGroupProps = ["sexMan","sexWoman","age17","age1824","age2530","age3135","age3640","age40"];
	var netGroupPropsCH = ["男性","女性","1-17岁","18-24岁","25-30岁","31-35岁","36-40岁","40岁以上"];
	$("#netGroupTerrace tr td input").each(function(n){
		netGroup[netGroupProps[n]] = $(this).val();
	});
	
	for(var i=0; i<netGroupProps.length; i++){
		if(netGroup[netGroupProps[i]] != '' &&!isNumber(netGroup[netGroupProps[i]])){
			console.log(netGroup[netGroupProps[i]]);
			top.alert("网络平台人群受众构成" + netGroupPropsCH[i]+"请输入非负数");
			return false;
		}
	}
	var sex = parseFloat(nullToZero(netGroup[netGroupProps[0]]))+parseFloat(nullToZero(netGroup[netGroupProps[1]]));
	if(parseInt(sex) != 0 && parseInt(sex) != 100){
		top.alert("网络平台人群受众构成:男女比例之和不等于100");
		return false;
	};
	
	var age = 0; 
	for(var i = 2; i<netGroupProps.length; i++){
		age =  parseFloat(nullToZero(age)) + parseFloat(nullToZero(netGroup[netGroupProps[i]]));
	}
	if(parseInt(age) != 0 && parseInt(age) != 100){
		top.alert("网络平台人群受众构成:年龄段比例之和不等于100");
		return false;
	}
	//豆瓣数据
	var doubanProps = ["lastDate","score","commentCount"];
	var doubanPropsCH =["截止日期","评分","评论数"];
	$("#doubanTerrace tr td input").each(function(n){
		doubanData[doubanProps[n]] = $(this).val();
	});	
	
	for(var i= 1; i<doubanProps.length; i++){
		if(doubanData[doubanProps[i]] != '' && !isNumber(doubanData[doubanProps[i]])){
			top.alert("豆瓣数据：" + doubanPropsCH[i] + "请输入非负数");
			return false;
		}
	}
	
	if(parseFloat(doubanData[doubanProps[1]])> 10){
		top.alert("豆瓣数据：评分不能大于10分");
		return false;
	}
	
	var params = {"programId":programId,"tvData":tvData,"netGroup":netGroup,"doubanData":doubanData,"netData":netData};
	console.log(params);
	
	$.ajax({type : "POST", url : "save_program_data_info.do", contentType:"application/json", data :JSON.stringify(params), success : function(res){
			if(res.code== 1){
				top.alert("保存成功。", 1, function(){
					location.href = "program_data_vareity.do?id=" + programId;
	            });
			}else{
				alert(res.msg);
			}
		} 
	});
}

//保存本部电视剧数据
function saveTvData1(){
	var tvData = [];	
	var programId = $("#programId").val();
	var tvProps = ["audienceRating","avgNum", "sexMan","sexWoman","age17","age1824","age2530","age3135","age3640","age4150","age5160","age60","platform","type"];
	var tvPropsCH = ["收视率","到达率", "男性","女性","1-17岁","18-24岁","25-20岁","31-35岁","35-40岁","41-50岁","51-60岁","60岁以上","播放电视台"];
    var flag = true;
    var tvPlatform = [];
	//电视台数据
	$('#vareity_info .box-pd').each(function(){
		var obj = new Object();
		var obj1 = new Object();
		//35城数据
		obj[tvProps[tvProps.length-2]] = $(this).find('option:selected').val();
		if(isInArray2(tvPlatform,obj[tvProps[tvProps.length-2]])){
			top.alert("电视台数据:播放电视台不允许重复");
			flag = false;
			return false;
		}else{
			tvPlatform.push(obj[tvProps[tvProps.length-2]]);
		}
		var onOff = true;
		$(this).find(".variety-50.line  td input").each(function(index){
			obj[tvProps[index]] = $(this).val();
			if(obj[tvProps[index]]!= '' && !isNumber(obj[tvProps[index]])){
				top.alert("电视台数据:35城" + tvPropsCH[index] + "请输入非负数");
				onOff = false;
				return false;
			}
			if(index == 1){
				if(obj[tvProps[index]]!= '' && !isInteger(Number(obj[tvProps[index]]))){
					top.alert("电视台数据:35城" + tvPropsCH[index] + "请输入整数")
					onOff = false;
					return false;
				}
			}
		})
		if(!onOff){
			flag = false;
			return false;
		}
		var sex = parseFloat(nullToZero(obj[tvProps[2]])) + parseFloat(nullToZero(obj[tvProps[3]]));
		if(parseInt(sex) != 0 && parseInt(sex) != 100){
			top.alert("电视台数据:35城男女比例之和不等于100");
			flag = false;
			return false;
		}
		var age = 0;
		for(var i= 4; i< 12; i++){
			age = parseFloat(nullToZero(age)) + parseFloat(nullToZero(obj[tvProps[i]]));
		}
		if(parseInt(age) != 0 && parseInt(age) != 100){
			top.alert("电视台数据:35城受众人群年龄段比例之和不等于100");
			flag = false;
			return false;
		}
		obj[tvProps[tvProps.length-1]] = 1;		
		tvData.push(obj);
		//29城数据
		obj1[tvProps[tvProps.length-2]] = $(this).find('option:selected').val();
		var onOff1 = true;
		$(this).find(".variety-50.nop td input").each(function(index){
			obj1[tvProps[index]] = $(this).val();
			if(obj1[tvProps[index]]!= '' && !isNumber(obj1[tvProps[index]])){
				top.alert("电视台数据:29省城" + tvPropsCH[index] + "请输入非负数");
				onOff1 = false;
				return false;
			}
			if(index == 1){
				if(obj1[tvProps[index]]!= '' && !isInteger(Number(obj1[tvProps[index]]))){
					top.alert("电视台数据:29省城" + tvPropsCH[index] + "请输入整数")
					onOff1 = false;
					return false;
				}
			}
		})
		if(!onOff1){
			flag = false;
			return false;
		}
		var sex1 = parseFloat(nullToZero(obj1[tvProps[2]])) + parseFloat(nullToZero(obj1[tvProps[3]]));
		if(parseInt(sex1) !=0 && parseInt(sex1) != 100){
			top.alert("电视台数据:29省城男女比例之和不等于100");
			flag = false;
			return false;
		}
		var age1 = 0;
		for(var i= 4; i< 12; i++){
			age1 = parseFloat(nullToZero(age1)) + parseFloat(nullToZero(obj1[tvProps[i]]));
		}
		if(parseInt(age1) != 0 && parseInt(age1) != 100){
			top.alert("电视台数据:29省城受众人群年龄段比例之和不等于100");
			flag = false;
			return false;
		}
		obj1[tvProps[tvProps.length-1]] = 2;		
		tvData.push(obj1);
	})	
	if(!flag){
		return false;
	}
	var params = {"programId":programId,"tvData1":tvData};		
	console.log(params);

	$.ajax({type : "POST", url : "save_program_data_info.do", contentType:"application/json", data :JSON.stringify(params), success : function(res){
			if(res.code== 1){
				top.alert("保存成功。", 1, function(){
					location.href = "program_data_tv.do?id=" + programId;
	            });
			}else{
				alert(res.msg);
			}
		} 
	});
}

//保存本部电视剧数据
function saveTvData2(){
	var netData = [], netGroup = {},doubanData ={};
	var programId = $("#programId").val();
	var flag = true;
	
	//网络平台数据
	var netProps = ["platformName","clickTimes","score","lastDate"];
	var netPropsCH = ["视频平台","点击量","评分","截止日期"];
	$("#tvlist .box-pd").each(function(){
		var netLastDate = $(this).find("input[name='date']").val();
		if(netLastDate == ''){
			top.alert("网络平台数据:请填写截止日期");
			flag = false;
			return false;
		}
		var platform = [];
		var onOff = true;
		$(this).find(".netWorkTerrace tr").each(function(){	
			var obj = new Object();
			$(this).find('td input,select').each(function(n){
				obj[netProps[n]] = $(this).val();
				if(n == 0){
					if(isInArray2(platform,obj[netProps[n]])){
						top.alert("网络平台数据:视频平台不能重复");
						onOff = false;
						return false;
					}else{
						platform.push(obj[netProps[n]]);
					}	
				}
				if(n!= 0 && obj[netProps[n]] != '' && !isNumber(obj[netProps[n]])){
					top.alert("网络平台数据:" + netPropsCH[n] + "请输入非负数");
					onOff = false;
					return false;
				}			
				if(n == 2 && parseFloat(obj[netProps[2]])>10){
					top.alert("网络平台数据:评分不能大于10分");
					onOff = false;
					return false;
				}
			});
			if(!onOff){
				flag = false;
				return false;
			}
			obj[netProps[netProps.length-1]] = netLastDate;
			netData.push(obj);
		});
	})
	
	if(!flag){
		return false;
	}
	
	//网络平台人群受众构成
	var netGroupProps = ["sexMan","sexWoman","age17","age1824","age2530","age3135","age3640","age40"];
	var netGroupPropsCH = ["男性","女性","1-17岁","18-24岁","25-30岁","31-35岁","36-40岁","40岁以上"];
	$("#netGroupTerrace tr td input").each(function(n){
		netGroup[netGroupProps[n]] = $(this).val();
	});
	
	for(var i=0; i<netGroupProps.length; i++){
		if(netGroup[netGroupProps[i]] != '' &&!isNumber(netGroup[netGroupProps[i]])){
			top.alert("网络平台人群受众构成" + netGroupPropsCH[i]+"请输入非负数");
			return false;
		}
	}
	var sex = parseFloat(nullToZero(netGroup[netGroupProps[0]]))+parseFloat(nullToZero(netGroup[netGroupProps[1]]));
	if( parseInt(sex) != 0 && parseInt(sex) != 100){
		top.alert("网络平台人群受众构成:男女比例之和不等于100");
		return false;
	};
	
	var age = 0; 
	for(var i = 2; i<netGroupProps.length; i++){
		age =  parseFloat(nullToZero(age)) + parseFloat(nullToZero(netGroup[netGroupProps[i]]));
	}
	if( parseInt(age) != 0 && parseInt(age) != 100){
		top.alert("网络平台人群受众构成:年龄段比例之和不等于100");
		return false;
	}
	
	//豆瓣数据
	var doubanProps = ["lastDate","score","commentCount"];
	var doubanPropsCH =["截止日期","评分","评论数"];
	$("#doubanTerrace tr td input").each(function(n){
		doubanData[doubanProps[n]] = $(this).val();
	});	
	
	for(var i= 1; i<doubanProps.length; i++){
		if(doubanData[doubanProps[i]] != '' && !isNumber(doubanData[doubanProps[i]])){
			top.alert("豆瓣数据：" + doubanPropsCH[i] + "请输入非负数");
			return false;
		}
	}
	if(parseFloat(doubanData[doubanProps[1]])> 10){
		top.alert("豆瓣数据：评分不能大于10分");
		return false;
	}
	
	var params = {"programId":programId,"netGroup":netGroup,"doubanData":doubanData,"netData":netData};
	console.log(params);
	
	$.ajax({type : "POST", url : "save_program_data_info.do", contentType:"application/json", data :JSON.stringify(params), success : function(res){
			if(res.code== 1){
				top.alert("保存成功。", 1, function(){
					location.href = "program_data_tv.do?id=" + programId;
	            });
			}else{
				alert(res.msg);
			}
		} 
	});
}

//查看不可编辑
function readOnly(){
	var mode = $('#mode').val();
	if(mode == 1){
		$("input").attr('readonly', true);
		$('select').attr('disabled', true);
		$('.max-btn').addClass("contentTxt");
	}
}

function isNumber(val){
    var regPos = /^\d+(\.\d+)?$/; 
    if(regPos.test(val)){
        return true;
    }else{
        return false;
    }
}

function isInteger(obj) {
	 return parseInt(obj, 10) === obj
}

function nullToZero(obj){
	if(obj == ''){
		return 0;
	}
	return obj;
}


