var current_id = "tab1";
var ok = true;//函数是否可以执行
$('#house_type').val('0');

$(function(){
	
	$(".ly3_t span").click(function(){
		if(ok == false){
			return;
		}
		ok = false;
		var click_id = this.id;//tab1~tab7
		if (click_id == "tab1"){
			house="0";
		}else{
			if(click_id == "tab2"){
				house="1";
			}else{
				house="2";
			}
		}
		if(click_id == current_id){//如果和当前tab一样,解决快速点击时出现的命令堆积bug。
			ok = true;
			return;
		}
		var tab_class = click_id+"_active";
		$('#'+current_id).removeClass(current_id+'_active').css({color: "#000"});
		$(this).addClass(tab_class).css({color: "#ff3900"});
		$('#house_type').val(house);
		ok=true;
		current_id = click_id;
	}).mouseover(function(){
		$(this).css({color: "#ff3900"});
	}).mouseout(function(){
		if( this.id != current_id ){
			$(this).css({color: "#000"});
		}
	});
	
	var nowdate = new Date(dateNow);
	var nowdateyear = nowdate.getFullYear();	
	//# 家财险
	$('#order_start_time').datepicker({ dateFormat: 'yy-mm-dd',yearRange: nowdateyear+':2020',mandatory: true,showOn: "both", buttonImage: "/template/entry/images/date.gif", buttonImageOnly: true,onSelect: function(dateText){
		start_time(dateText);
	}
	});
	$('#p_own').cnCity({provinceChange:function(){areaFormChange('frm','own',$('#p_own').val())},cityId:'#area_own',defaultCity:areaNow,defaultProvince:areaNow,productType:'own'});
	$.formValidator.initConfig({onError:function(msg){alert(msg)}});
	$("#order_start_time").formValidator({onshow:"",onfocus:"请填写希望保单生效日期",oncorrect:""}).InputValidator({min:1,onerror:"请填写希望保单生效日期"});
	$("#order_end_time").formValidator({onshow:"",onfocus:"请填写希望保单生效日期",oncorrect:""}).InputValidator({min:1,onerror:"请填写希望保单生效日期"});
	//$("#order_time_limit").formValidator({onshow:"",onfocus:"请正确选择保单期限",oncorrect:""}).InputValidator({min:1,onerror:"请正确选择保单期限"});

	$("#order_time_limit").change(limit);
	$('#submit_own').click(function(){validator();});
	
	$("#submit_own").attr("disabled",false);
});

function validator(){
	if($('#area_own').val() == '00'){
		alert('请先选择受理地');
		return false;
	}		
	if(jQuery.formValidator.PageIsValid()){
		$('#frm').submit();
		//return true;
	}else{
		return false;
	}
}

function start_time(start){
	var limit = $('#order_time_limit').val();
	if(start!==""){
		var now = new Date(dateNow);
		var temp = new Date(start.replace(/-/g, "\/"));
		var temp2 = new Date(start.replace(/-/g, "\/"));
		temp.setDate(temp.getDate());
		temp2.setDate(temp2.getDate()-364);
		if (now > temp){
			$('#order_start_time').val('');
			$('#end').html(datestr);
			$('#order_end_time').val(datestr);
			alert("保单起效日期必须晚于当天,请重新填写");
			return false;
		}
		else{
			if(now < temp2){
				$('#order_start_time').val('');
				$('#end').html(datestr);
				$('#order_end_time').val(datestr);
				alert("保单起效日期须在1年之内,请重新填写");
				return false;
			}else{
				temp.setMonth(temp.getMonth()+Number(limit));
				temp.setDate(temp.getDate()-1);
				var datestr = temp.getFullYear();
				datestr += "-";
				datestr += temp.getMonth()>=9?(temp.getMonth() +1).toString():"0" + (temp.getMonth()+1);
				datestr += "-";
				datestr += temp.getDate()>=9?temp.getDate().toString():"0" + temp.getDate();
				$('#end').html(datestr);
				$('#order_end_time').val(datestr);
			}
		}
	}
}

function limit(){
	var start = $('#order_start_time').val();
	start_time(start);
}
