var type;
var $forgetUserType=0;
var isPassYZ=false;
$(function () {
	type = getUrlSearch("userType");
	if(type == 1){
		$("#singleUser").addClass('on').siblings().removeClass('on');
	}
	if(type == 2){
		$("#enterUser").addClass('on').siblings().removeClass('on');
	}
});

function checkFormat(){
	if($forgetUserType==0)
	{
		var sfz=$("#sfz").val().replace(/^\s+|\s+$/g, "");
		if(!isCardNo(sfz))
		{
			document.getElementById("sfztip").innerHTML = '<span class="cin1">输入正确的身份证号码！</span>';
			return false;
		}else{
			document.getElementById("sfztip").innerHTML = '';
			return true;
		}
	}else{
		var xydm=$("#xydm").val().replace(/^\s+|\s+$/g, "");
		if(!isSocialCredit(xydm))
		{
			document.getElementById("xydmtip").innerHTML = '<span class="cin1">输入正确的统一社会信用代码！</span>';
			return false;
		}else{
			document.getElementById("xydmtip").innerHTML = '';
			return true;
		}
	}
}

function changeToChoice(){
	if(!checkFormat())
	{
		return;
	}
	if(!isPassYZ)
	{
		document.getElementById("sfyztip").innerHTML = '<span class="cin1">请先通过验证！</span>';
		return;
	}else{
		document.getElementById("sfyztip").innerHTML = '';
	}
	var code="";
	if($forgetUserType==0)
	{
		code=$("#sfz").val().replace(/^\s+|\s+$/g, "");
	}else{
		code=$("#xydm").val().replace(/^\s+|\s+$/g, "");
	}
	check_code_exist(code);
}

function tabShow2(oNav, oCon, sEvent) {
	var oElem = oNav.children();
	oCon.hide().eq(0).show();
	oElem.each(function (index) {
		$(this).bind(sEvent, function () {
			$(this).addClass('on').siblings().removeClass('on');
			oCon.hide().eq(index).show();
			$forgetUserType=index;
		})
	})
}
tabShow2($('.zhmm-tab'), $('.zhmm-tc1'), 'click');
imgVer({
	el: '$("#imgVer1")',
	width: '260',
	height: '116',
	img: [
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-1.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-2.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-3.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-4.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-5.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-6.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-7.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-8.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-9.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-10.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-11.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-12.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-13.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-14.jpg',
		'https://ceshi.livibank.ltd/static/home/passwd/ver/ver-15.jpg'
	],
	success: function (e) {
		//alert('执行登录函数');
		$(".verBox").css({
			"display": "none"
		});
		$(".djyzs1").addClass("sure-ok")
		$(".djyzs1 span").html("通过");
		isPassYZ=true;
		document.getElementById("sfyztip").innerHTML = '';
	},
	error: function () {
		//alert('错误什么都不执行')
	}
});
$(".djyzs1").on('click', function () {
	if (!isPassYZ) {
		$(".verBox").css({
			"display": "block"
		});
	}
})
$('.hdk-1 em').click(function () {
	$('.verBox').hide();
})

function checkUserExists(){
	if(type == 2){
		var socialCode = $()
		check_socialcode_exist();
	}
}
//
function check_code_exist(code){
	$.ajax({
		   headers : {
				'Accept' : 'application/json',
				'Content-Type' : 'application/json'
			},
		   type: "POST",
		   url:window.location.origin+"/aip/v1/gov/person/check",
		   data:JSON.stringify({"certificateNum":code}),
		   success:function(msg){			
			   if (msg.toString() != "false") {
				   if ($forgetUserType == 0) {
					   document.getElementById("sfztip").innerHTML = '<span class="cin1">无此用户信息！</span>';
				   } else {
					   document.getElementById("xydmtip").innerHTML = '<span class="cin1">无此用户信息！</span>';
				   }
				   return;
			   } else {
				   //跳转至用户找回界面，并将证件号码写入凭证中
				   // 写入数据
				   localStorage.setItem('certificateNum', code);  //存储至本地
				   localStorage.setItem('userType', $forgetUserType.toString());
				   //跳转到找回密码2界面
				   forget_submit(code,$forgetUserType.toString());
				   // window.location.href = window.location.origin + "/oauth2/register/forgetPwd2.html";
			   }
		   },
		   error:function(e){
			   debugger;
		   }
	   });
}

function forget_submit(code,userType){
	code=encrypt(code);
	var bs = new Base64();
	var code = bs.encode(code);
	$.ajax({
		   headers : {
				'Content-Type' : 'application/json'
			},
		   type: "POST",
		   url:window.location.origin+"https://ceshi.livibank.ltd/forpwd/",
		   data:JSON.stringify({"certificateNum":code,"userType":userType}),
		   success:function(data){			
			   if(data == "ok"){
				//刷新页面
				window.location.reload();
				return;
			   }else{
				alert(data);
				//刷新页面
				window.location.reload();
				return;
			   }
		   },
		   error:function(){
			   //刷新页面
				window.location.reload();
				return;
		   }
	   });
}

function yhjh_check(code){
	code=encrypted(code);
	var bs = new Base64();
	var code = bs.encode(code);
	$.ajax({
		headers: {
			'Accept': 'application/json',
			'Content-Type': 'application/json'
		},
		url: window.location.origin + "/aip/v1/gov/govperson/checksfjh",
		type: "POST",
		data: JSON.stringify({ "phone": code, "certificateNum": code }),
		success: function (userdata) {
			if (userdata != null&&userdata.phone!=null&&userdata.certificateNum!=null) {
				//激活手机号
				$jhsjh = userdata.phone;
				$jhzjh = userdata.certificateNum;
				//提示用户激活
				$("#jhtip").show();
				document.getElementById("hqyzmtip").innerHTML = '点击获取验证码，短信将发送至您的<span>[' + $jhsjh.substring(0, 3) + '****' + $jhsjh.substring(7) + ']</span>手机号上，请您注意查收';
			}
		},
		error: function (msg) {
			// document.getElementById("legalphonetip").innerHTML='<span class="rin1">'+msg+'</span>';
			// isSuccess = false;
		}
	});
}


//兼容IE低版本location.origin
if (window["context"] == undefined) {
	if (!window.location.origin) {
		window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
	}
	window["context"] = location.origin + "/V6.0";
}

function getUID() {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
};
var captchaKey = getUID();
window.captchaText = 0;
function getCaptcha() {
	$.ajax({
		'type': 'GET',
		'url':window.location.origin+ "/c2ssocaptcha/" + captchaKey + "/captchanumber",
		'success': function (data) {
			window.captchaText = data;
		}
	});
};
getCaptcha();

function isCardNo(code) {
	// 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
	var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	if (reg.test(code) === false) {
		//alert("身份证输入不合法");  
		return false;
	} else {
		if (code.length == 15) {
			code = cardfive(code);
		}
		//18位身份证需要验证最后一位校验位
		if (code.length == 18) {
			code = code.split('');
			//∑(ai×Wi)(mod 11)
			//加权因子
			var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
			//校验位
			var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
			var sum = 0;
			var ai = 0;
			var wi = 0;
			for (var i = 0; i < 17; i++) {
				ai = code[i];
				wi = factor[i];
				sum += ai * wi;
			}
			var last = parity[sum % 11];
			if (parity[sum % 11] != code[17]) {
				tip = "校验位错误";
				return false;
			}
		}
	}
	return true;
}

/**
 * 检查信用编码是否合法
 * @param code
 * @returns
 */
function isSocialCredit(socialCreditNo) {
	var reg = /^[0-9A-Z]+$/;
	if (socialCreditNo.length != 18) {
		return false;
	}else if(!reg.test(socialCreditNo)){
		return false;
	}
	return true;
}