$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    活动列表页    ***************************/
	
	$("#btn-create").click(function(){
		if($("#orderId").val() == "" || $("#orderId").val() == -1){
			$(this).attr("href","javascript:void(0);");
			bootbox.alert("请选择订单！");
			$(this).attr("title","请选择订单");
			return false;
		}
	});

	$('ul#orderul_son_ul li').live("click",function(){
		$("#myform").attr("action",ctx+"/advertiser/main/ad/target/campaign/list?orderId="+$("#orderId").val()+"&advertiserId="+$("#advertiserId").val());
		refreshData();
	});
	
 	//状态全部、开启、停用
 	if($("#status").val() == 0 || $("#status").val() == ""){
 		$("#selectBar_status a:first").addClass("selectBar_change");
	}
 	$("#selectBar_status a").each(function(){
 		var value = $(this).attr("value");
 		if($("#status").val() == value){
 			$(this).addClass("selectBar_change").siblings('.selectBar_change').removeClass('selectBar_change');
 		}
 	});
 	$("#selectBar_status a").click(function(){
 		$("#status").val($(this).attr("value"));
 		refreshData();
 	});
 
	//开启,暂停
	$("#btn-enable,#btn-disable,#btn-delete").click(function(){
		if($(this).attr("id") == "btn-enable"){
			url = ctx+"/advertiser/main/ad/target/campaign/enable";
		}else if($(this).attr("id") == "btn-disable"){
			url = ctx+"/advertiser/main/ad/target/campaign/disable";
		}else if($(this).attr("id") == "btn-delete"){
			url = ctx+"/advertiser/main/ad/target/campaign/delete";
		}
		singleOper($(this),url);
	});
			
	//批量开启,批量暂停
	$("#all-btn-enable,#all-btn-disable,#all-btn-delete").click(function(){
		if($(this).attr("id") == "all-btn-enable"){
			url = ctx+"/advertiser/main/ad/target/campaign/enable";
		}else if($(this).attr("id") == "all-btn-disable"){
			url = ctx+"/advertiser/main/ad/target/campaign/disable";
		}else if($(this).attr("id") == "all-btn-delete"){
			url = ctx+"/advertiser/main/ad/target/campaign/delete";
		}
		batchOpear($(this),url);
	});
	
	/***************************    创建，编辑活动    ***************************/
	
	//初始校验表单
 	if($("#validateForm").html()){
 		$('#dateRangePicker').dateRangePicker();
 		$("#validateForm").autovalidate();
 		$(".input-text").focus(function(){
 			$(this).parent("div.input-group").find("span.help-inline").text("").removeClass("help-inline").addClass("spanVaildate");
 		});
 	}
	
	$(".btn-save").click(function(){
		$("div.input-group").find("span.help-inline").text("").removeClass("help-inline").addClass("spanVaildate");
		if (!$("#validateForm").validate()) {
			return false;
		}
		var falg = validateMoney();
		if(falg == false){
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$(".btn-cancel").click(function(){
		window.location.href = ctx+"/advertiser/main/ad/target/campaign/list?orderId="+$("#orderId").val()+"&advertiserId="+$("#advertiserId").val();
	});
	
	$("#totalBudget").blur(function(){
		validateMoney();
	});
});
		
function exportExcel_list(){
	var searchValue = $("#searchValue").val();
	if(searchValue == "请输入关键字"){
		searchValue = "";
	}
	var param = "?orderId="+$("#orderId").val()+"&advertiserId="+$("#advertiserId").val()+"&status="+$("#status").val()+"&searchValue="+searchValue;
	$("#exportExcel").attr("href",$("#ctx").val()+"/advertiser/main/ad/target/campaign/exportDataReportExcel"+param);
	return false;
}

function validateMoney(){
	var reg=/^\d{1,8}\.?\d{0,2}$/;
	var $this = $("#totalBudget");
	if($this.val().match(reg)!=null){
		if(($this.val().indexOf('.') == -1 && $this.val().length > 8) || $this.val() <= 0){
			$this.css("box-shadow","0 0 6px #ff0000").attr('title','总预算金额整数位最多8位');
			$this.parent("div").find("span.spanVaildate").html("总预算金额整数位最多8位");
			return false;
		}else if($this.val() > parseInt($("#orderTotalBudget").val())){
			$this.css("box-shadow","0 0 6px #ff0000").attr('title','总预算不能大于订单总预算');
			$this.parent("div").find("span.spanVaildate").html("总预算不能大于订单总预算");
			return false;
		}else{
			$this.css("box-shadow","none").attr('title','');
			$this.parent("div").find("span.spanVaildate").html("");
		}
	}else{
		$this.css("box-shadow","0 0 6px #ff0000").attr('title','总预算金额整数位最多8位且最多有2位小数');
		$this.parent("div").find("span.spanVaildate").html("总预算金额整数位最多8位且最多有2位小数");
		return false;
	}
}