$(document).ready(function(){

	window.onload = function(){
	}

	$("ul.examinationType > li:eq(0)").click(function(){
		$(this).addClass("current");
		$(this).siblings("li").removeClass("current");
		$("ul.examinationContentList > li:eq(0)").stop().fadeIn();
		$("ul.examinationContentList > li:eq(0)").siblings().stop().hide();
	});

	$("ul.examinationType > li:eq(1)").click(function(){
		$(this).addClass("current");
		$(this).siblings().removeClass("current");
		$("ul.examinationContentList > li:eq(1)").stop().fadeIn();
		$("ul.examinationContentList > li:eq(1)").siblings().stop().hide();
	});

	$("ul.examinationType > li:eq(2)").click(function(){
		$(this).addClass("current");
		$(this).siblings().removeClass("current");
		$("ul.examinationContentList > li:eq(2)").stop().fadeIn();
		$("ul.examinationContentList > li:eq(2)").siblings().stop().hide();
	});

	$("ul.examinationType > li:eq(3)").click(function(){
		$(this).addClass("current");
		$(this).siblings().removeClass("current");
		$("ul.examinationContentList > li:eq(3)").stop().fadeIn();
		$("ul.examinationContentList > li:eq(3)").siblings().stop().hide();
	});

	$("ul.examinationType > li:eq(4)").click(function(){
		$(this).addClass("current");
		$(this).siblings().removeClass("current");
		$("ul.examinationContentList > li:eq(4)").stop().fadeIn();
		$("ul.examinationContentList > li:eq(4)").siblings().stop().hide();
	});

	$("ul.examinationType > li:eq(5)").click(function(){
		$(this).addClass("current");
		$(this).siblings().removeClass("current");
		$("ul.examinationContentList > li:eq(5)").stop().fadeIn();
		$("ul.examinationContentList > li:eq(5)").siblings().stop().hide();
	});

	$("ul.answerSheetList > li > dl > dt").click(function(){
		$(this).siblings("dd").toggle();
		//$(this).siblings("dd").stop().show();
		//$(this).parent("dl").parent("li").siblings("li").children("dl").children("dd").stop().hide();
	});

	// $("ul.memberBoxTitle > li:eq(0)").click(function(){
	// 	$(this).addClass("current");
	// 	$(this).siblings("li").removeClass("current");
	// 	$("ul.memberBoxList > li:eq(0)").stop().show();
	// 	$("ul.memberBoxList > li:eq(0)").siblings().stop().hide();
	// });
	//
	// $("ul.memberBoxTitle > li:eq(1)").click(function(){
	// 	$(this).addClass("current");
	// 	$(this).siblings("li").removeClass("current");
	// 	$("ul.memberBoxList > li:eq(1)").stop().show();
	// 	$("ul.memberBoxList > li:eq(1)").siblings().stop().hide();
	// });
	//
	// $("ul.memberBoxTitle > li:eq(2)").click(function(){
	// 	$(".blackBox").hide();
	// 	$(".memberBox").hide();
	// });
	//
	// $(".memberLogin").click(function(){
	// 	$(".blackBox").fadeIn();
	// 	$(".memberBox").fadeIn();
	// 	$("ul.memberBoxTitle > li:eq(0)").addClass("current");
	// 	$("ul.memberBoxTitle > li:eq(0)").siblings("li").removeClass("current");
	// 	$("ul.memberBoxList > li:eq(0)").stop().show();
	// 	$("ul.memberBoxList > li:eq(0)").siblings().stop().hide();
	// });
	//
	// $(".memberRegsiter").click(function(){
	// 	$(".blackBox").fadeIn();
	// 	$(".memberBox").fadeIn();
	// 	$("ul.memberBoxTitle > li:eq(1)").addClass("current");
	// 	$("ul.memberBoxTitle > li:eq(1)").siblings("li").removeClass("current");
	// 	$("ul.memberBoxList > li:eq(1)").stop().show();
	// 	$("ul.memberBoxList > li:eq(1)").siblings().stop().hide();
	// });

});

//函数：E-Mail检测
function isMail(str){
	var Re = /^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/;
	if( Re.test(str) ){
		return true;
	}else{
		return false;
	}
}

//函数：手机号检测
function isPhone(str){
	var Re = /^1[3-9]\d{9}$/;
	if( Re.test(str) ){
		return true;
	}else{
		return false;
	}
}

//函数：数字检测
function isNum(str){
	var Re = /^[0-9]+$/;
	if( Re.test(str) ){
		return true;
	}else{
		return false;
	}
}


//函数：数字检测
function isNumber(val){
  var reg = /^[\d|\.|,]+$/;
  return reg.test(val);
}

//函数：检查是否为 YYYY-MM-DD || YYYY/MM/DD 的日期格式
function isDate(str){
	var Re = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
	if( Re.test(str) ){
		return true;
	}else{
		return false;
	}
}

//函数：匹配身份证(15位或18位)
function isIdcard(str){
	var Re = /\d{15}|\d{18}/;
	if( Re.test(str) ){
		return true;
	}else{
		return false;
	}
}


//函数：必填写项及长度
function isReqired(str, minl, maxl){
	var Re = eval("/^\\S{"+ minl +","+ maxl +"}$/");
	if( Re.test(str) ){
		return true;
	}else{
		return false;
	}
}

//弹窗,页脚放操作按钮，无操作自动关闭弹窗
function msgalert(content, title='', footer=''){
	if( title=='' ){
		$('#myModal .modal-header').hide();
	}
	else{
		$('#myModal .modal-title').html(title);
		$('#myModal .modal-header').show();
	}

	$('#myModal .modal-body').html(content);
	$('#myModal').modal({backdrop: 'static', keyboard: false});
	if( footer=='' ){
		$('#myModal .close').show();
		setTimeout(function(){
			$('#myModal').modal('hide');
		},2000);
	}
	else{
		$('#myModal .close').hide();
		$('#myModal .modal-footer').html(footer);
	}
}

//弹窗,手动关闭
function showbox(url, msg, acts){
	if( url!='' ){
			$.get(url,'',function(data){
				if( data.message!='' ){
					var content = data.error==0 ? data.message : '<span class="text-danger">'+data.message+'</span>';
					$('.messageInfo').html(content);
					$(".blackBox").fadeIn();
					$(".messageBox").fadeIn();
					setTimeout(function(){
						$(".blackBox").fadeOut();
						$(".messageBox").fadeOut();
						if( data.error==0 ){
							if( data.gourl!='' )
								window.location.href=data.gourl;
						}
					},2000);
				}
				else{
					$('.messageInfo').html(content);
					$(".blackBox").fadeIn();
					$(".messageBox").fadeIn();
					//$('.messageBox').css('width', '600px');
				}
			});
	}
	else{

		$('.messageInfo').html(msg);
		$('.messageBtn').html(acts);
		$(".blackBox").fadeIn();
		$(".messageBox").fadeIn();
	}
}
//关闭提示弹窗
function closemsgalert(){
	$(".blackBox").fadeOut();
	$(".messageBox").fadeOut();
	$('.messageInfo').html('');
	$('.messageBtn').html('');
}

//选择地区
function sel_region(obj, level, str){
	var id = $(obj).val();
	$.post('/region/selregion.html',{"id":id,"level":level},function(data){
		if( data.content!='' ){
			$('#'+str).html(data.content);
			$('#'+str).show();
			if( level==2 ){
				$(obj).parent().find('select[name=district]').hide();
			}
		}
	});
}


//会员注册获取手机验证码
function getRegCode(){
	var mobile = $('#mobile').val();
	if( !isPhone(mobile) ){
		$('.pls-mobile').html('无效的手机号码');
		return false;
	}
	getCode(mobile, 'reg', 'btn-getCode');
}

//发送手机验证码
function getCode(mobile, act, btn){
	$.post('/Member/get_mcode.html',{"mobile":mobile,"do":act},function(data){
	  if( data.error==1 ){
			if(act=='reg'){
				$('.pls-code').html(data.message);
			}
			if(act=='bind'||act=='fpwd'){
				$('.pls-vcode').html(data.message);
			}

	  }
	  else{
		  alert(data.message);
		  var times = 60 * 100; // 60秒
		  countTime = setInterval(function(){
			times = --times < 0 ? 0 : times;
			var ms = Math.floor(times / 100).toString();
			if(times == 0) {
			  clearInterval(countTime);
			}
			if(ms == 0){
			    $("."+btn).css({"background-color":"#cb2c2c"}).val('获取验证码');
				$("."+btn).attr("disabled", false);
			}else{
			    $("."+btn).css({"background-color":"#CCC"}).val(ms+'秒后重新获取');
				$("."+btn).attr("disabled", true);
			}
		  }, 10);
	  }
	});
}

$(function(){
	//关闭提示
	$('.messageBoxClose').click(function(){
		closemsgalert();
	});

	//登录
	$('.input-group input').focus(function(){
		$(this).parents('.input-group').find('span').html('');
	});
	$('#logform').ajaxForm({
		beforeSubmit:  checkLogin,  // pre-submit callback
		success:       completeLogin,  // post-submit callback
		dataType:      'json'
	});
	function checkLogin(){
		var logname = $('#logname').val();
		var pwd = $('#logpwd').val();
		if( !isPhone(logname) && !isIdcard(logname) ){
			$('.pls-logname').html('请输入手机号或身份证号');
			return false;
		}
		if( !isReqired(pwd, 4, 20) ){
			$('.pls-logpwd').html('请输入4-20位数字或字母');
			return false;
		}
		$('.btn-login').attr("disabled", true);
	}
	function completeLogin(res){
		if( res.message!='' ){
			$('.pls-logpwd').html(res.message);
		}
		if( res.error==0 ){
			$(".blackBox").hide();
			$(".loginbox").hide();
			$('#logform')[0].reset();
			location.href = res.gourl;
		}
		$('.btn-login').attr("disabled", false);
		//$('.top-meminfo').html(res.content);
	}

	//注册
	$('#regform').ajaxForm({
		beforeSubmit:  checkReg,  // pre-submit callback
		success:       completeReg,  // post-submit callback
		dataType:      'json'
	});
	function checkReg(){
		var realname = $('#realname').val();
		var idcard = $('#idcard').val();
		var mobile = $('#mobile').val();
		//var email = $('#email').val();
		var code = $('#code').val();
		var repwd = $('#re-password').val();
		var pwd = $('#password').val();
		if( !isReqired(realname, 2, 5) ){
			$('.pls-realname').html('请输入身份证对应的姓名');
			return false;
		}
		// if( !isMail(email) ){
		// 	$('.pls-email').html('无效的邮箱地址');
		// 	return false;
		// }
		if( !isPhone(mobile) ){
			$('.pls-mobile').html('无效的手机号');
			return false;
		}
		if( !isIdcard(idcard) ){
			$('.pls-idcard').html('无效的身份证号');
			return false;
		}
		if( !isReqired(pwd, 4, 20) ){
			$('.pls-pwd').html('请输入4-20位数字或字母');
			return false;
		}
		if( pwd!=repwd ){
			$('.pls-repwd').html('两次输入的密码不一致');
			return false;
		}
		if( code.length!=6 ){
			$('.pls-code').html('验证码输入不正确');
			return false;
		}

	}
	function completeReg(res){
		if( res.message!='' ){
			if( res.content=='' ){
				alert(res.message);
			}
			else{
				$('.'+res.content).html(res.message);
			}
		}
		if( res.error==0 ){
			$(".blackBox").hide();
			$(".memberBox").hide();
			$('#regform')[0].reset();
			location.href = res.gourl;
		}
		$('.btn-register').attr("disabled", false);

	}

	//通用返回
	function complete(res){
		if( res.message!='' ){
			msgalert(res.message);
		}
		if( res.gourl!='' )
			setTimeout(function(){window.location.href = res.gourl;},2000);
	}


});
