var Register = {
	registData: {},
	validateFields: {
		username: {validators: {notEmpty: {message: '登录账号不能为空'}}},
		name: {validators: {notEmpty: {message: '单位名称不能为空'}}},
		idCard: {validators: {notEmpty: {message: '统一社会信用代码不能为空'}}},
		agentName: {validators: {notEmpty: {message: '人才联络员不能为空'}}},
		agentPhone: {
			validators: {
				notEmpty: {
					message: '人才联络员手机号不能为空'
				}, regexp: {
					regexp: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
					message: "人才联络员手机号格式不合法"
				}
			}
		},
		verificationCode: {validators: {notEmpty: {message: '手机验证码不能为空'}}},
		legal: {validators: {notEmpty: {message: '法人代表不能为空'}}},
		street: {validators: {notEmpty: {message: '所属街道不能为空'}}},
		address: {validators: {notEmpty: {message: '单位地址不能为空'}}},
		ephone: {
			validators: {
				notEmpty: {
					message: '单位电话不能为空'
				}, regexp: {
					regexp: /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/,
					message: "单位电话格式不合法"
				}
			}
		},
		agentEmail: {
			validators: {
				notEmpty: {
					message: '电子邮箱不能为空'
				},
				regexp: {
					regexp: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
					message: "电子邮箱格式不正确"
				}
			}
		}
	}
};

var enterpriseCountDown = 0;
var enterpriseSmsBtn = $("#enterprise_sms_btn");
var persionCountDown = 0;
var psSmsBtn = $("#ps_sms_btn");
var lock = false;

Register.addSubmit = function() {
	var regType = $(".active").attr("regType");
	if (regType == "en") {                                                          //企业用户注册
		Register.enterpriseUserRegister();
	} else if (regType == "ps") {                                                   //个人用户注册
		Register.personUserRegister();
	}
};

/**
* 验证数据
*/
Register.validate = function () {
	$('#ep_form').data("bootstrapValidator").resetForm();
	$('#ep_form').bootstrapValidator('validate');
	return $("#ep_form").data('bootstrapValidator').isValid();
}


var isDis = false;
Register.enterpriseUserRegister = function() {
	if(!Register.validate()){
		return ;
	}
	isDis = $("#type").prop("disabled");
	var source = $("#source").val();
	var username = $("#username").val();
	var password = $("#password").val();
	var rePassword = $("#re_password").val();
	var name = $("#name").val();
	var idCard = $("#idCard").val();
	var agentName = $("#agentName").val();
	var agentPhone = $("#agentPhone").val();
	var verificationCode = $("#verificationCode").val();
	var legal = $("#legal").val();
	var street = $("#street").val();
	var address = $("#address").val();
	if(isDis){
		$("#type").removeAttr("disabled");
	}
	var type = $("#type").val();
	var agentEmail = $("#agentEmail").val();
	var enterpriseTag = $("#enterpriseTag").val();
	var ephone = $("#ephone").val();
	var industryField = $("#industryField").val();
	if (username==null || username=='') {
		Feng.info("请填写账号!");
		return;
	}
	if (username.length > 50) {
		Feng.info("账号最多50个字符!");
		return;
	}
	if(source != 2){
		if (password==null || password=='') {
			Feng.info("请填写密码!");
			return;
		}
		if (!/^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$/.test(password)){
			Feng.info("密码格式错误，密码由字母与数字组成，且长度必须大于8个字符");
			return;
		}
		if (rePassword==null || rePassword=='') {
			Feng.info("请填写重复密码!");
			return;
		}
		if (password != rePassword) {
			Feng.info("两次密码填写不一致!");
			return;
		}
	}
	if (name==null || name=='') {
		Feng.info("请填写单位名称!");
		return;
	}
	if (name.length > 100) {
		Feng.info("单位名称最多100个字符!");
		return;
	}
	if (idCard==null || idCard=='') {
		Feng.info("请填写统一社会信用代码!");
		return;
	}
	if (type==null || type=='') {
		Feng.info("请选择申报类型!");
		return;
	}
	if (idCard.length > 40) {
		Feng.info("统一社会信用代码最多40个字符!");
		return;
	}
	if (agentName==null || agentName=='') {
		Feng.info("请填写人才联络员!");
		return;
	}
	if (agentName.length > 30) {
		Feng.info("人才联络员最多30个字符!");
		return;
	}
	if (agentPhone==null || agentPhone=='') {
		Feng.info("请填写人才联络员电话!");
		return;
	}
	if(ephone == null || ephone==''){
		Feng.info("请填写单位电话");
		return;
	}
	if (verificationCode==null || verificationCode=='') {
		Feng.info("请填写手机验证码!");
		return;
	}
	if (legal==null || legal=='') {
		Feng.info("请填写法人代表!");
		return;
	}
	if (street==null || street=='') {
		Feng.info("请选择所属街道!");
		return;
	}
	if (address==null || address=='') {
		Feng.info("请填写单位地址!");
		return;
	}
	if(type==null || type==''){
		Feng.info("请选择企业类别");
		return ;
	}
	if (agentEmail==null || agentEmail=='') {
		Feng.info("请填写电子邮箱!");
		return;
	}
	if(type==1 ){
		if(enterpriseTag==null||enterpriseTag==''){
			Feng.info("请选择企业标签!");
			return;
		}
		if(industryField == null || industryField ==''){
			Feng.info("请选择行业领域");
			return ;
		}
	}
	var x=document.getElementById("imgurl").value;
    if(x==""){
    	Feng.info("请上传营业执照照片!");
		return;
	}
	if(!/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/.test(ephone)){
		Feng.info("单位电话格式不合法!");
		return;
	}
	if(!/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/.test(agentPhone)){
		Feng.info("人才联络员电话格式不合法!");
		return;
	}
	if(!/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/.test(agentEmail)){
		Feng.info("电子邮箱格式不合法!");
		return;
	}
	if(!lock){
		lock = true;
	}else{
		Feng.info("不能重复点击");
		return;
	}
	$("#ep_form")[0].submit();
};

Register.personUserRegister = function() {
	var source = $("#ps_source").val();
	var username = $("#ps_username").val();
	var password = $("#ps_password").val();
	var rePassword = $("#ps_re_password").val();
	var name = $("#ps_name").val();
	var sex = $("#ps_sex").val();
	var idCard = $("#ps_idCard").val();
	var phone = $("#ps_phone").val();
	var verificationCode = $("#ps_verificationCode").val();
	if (username==null || username=='') {
		Feng.info("请填写账号!");
		return;
	}
	if (username.length > 50) {
		Feng.info("账号最多20个字符!");
		return;
	}
	if(source !=2 ){
		if (password==null || password=='') {
			Feng.info("请填写密码!");
			return;
		}
		if (rePassword==null || rePassword=='') {
			Feng.info("请填写重复密码!");
			return;
		}
		if (password != rePassword) {
			Feng.info("两次密码填写不一致!");
			return;
		}
	}
	if (name==null || name=='') {
		Feng.info("请填写姓名!");
		return;
	}
	if (name.length > 20) {
		Feng.info("姓名最多20个字符!");
		return;
	}
	if (sex==null || sex=='') {
		Feng.info("请选择性别!");
		return;
	}
	if (idCard==null || idCard=='') {
		Feng.info("请填写身份证号!");
		return;
	}
	if (phone==null || phone=='') {
		Feng.info("请填写手机号!");
		return;
	}
	if (verificationCode==null || verificationCode=='') {
		Feng.info("请填写手机验证码!")
		return;
	}
	if(!/0?(13|14|15|17|18|19)[0-9]{9}/.test(phone)){
		Feng.info("手机号格式不合法!");
		return;
	}
	var da = {"source":source,"username":username, "password":password, "name":name, "sex":sex, "idCard":idCard, "phone":phone, "verificationCode":verificationCode};
	var ajax = new $ax(Feng.ctxPath + "/api/personalUser/register", function(data){
        Feng.info(data.msg);
        if (data.code == 200) {
        	parent.layer.close(window.parent.Login.layerIndex);
		}
    },function(data){
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.set(da);
    ajax.start();
};

Register.getEnterpriseSms = function() {	
	var agentPhone = $("#agentPhone").val();
	if (agentPhone==null || agentPhone=="") {
		Feng.info("请填写人才联络员手机号!");
		return;
	}
	agentPhone = $.trim(agentPhone);
	if (!Feng.checkMobilePhoneNum(agentPhone)) {
		Feng.info("手机号格式有误，请填写正确的手机号码!");
		return;
	}
	
	Register.enterpriseBtnCountDown();
	
	var ajax = new $ax(Feng.ctxPath + "/api/common/verificationCode?phone="+agentPhone+"&type=1", function(data){
        Feng.info(data.msg);
    },function(data){
        Feng.error("操作失败!" + data.responseJSON.message + "!");
    });
    ajax.set(null);
    ajax.start();
};

Register.enterpriseBtnCountDown = function() {
	if (enterpriseCountDown == 0) {
		enterpriseCountDown = 60;
	} else if (enterpriseCountDown == 1) {
		enterpriseSmsBtn.removeAttr("disabled");
		enterpriseSmsBtn.html("获取验证码");
		enterpriseCountDown = 0;
		return;
	}
	enterpriseSmsBtn.attr("disabled", true);
	enterpriseSmsBtn.html(enterpriseCountDown + "秒");
	enterpriseCountDown--;
	setTimeout("Register.enterpriseBtnCountDown()", 1000);
};

Register.getPersionSms = function() {
	var phone = $("#ps_phone").val();
	if (phone==null || phone=="") {
		Feng.info("请填写手机号!");
		return;
	}
	phone = $.trim(phone);
	if (!Feng.checkMobilePhoneNum(phone)) {
		Feng.info("手机号格式有误，请填写正确的手机号码!");
		return;
	}
	Register.getPersionBtnCountDown();
	var ajax = new $ax(Feng.ctxPath + "/api/common/verificationCode?phone="+phone+"&type=1", function(data){
		Feng.info(data.msg);
	},function(data){
		Feng.error("操作失败!" + data.responseJSON.message + "!");
	});
	ajax.set(null);
	ajax.start();
};

Register.getPersionBtnCountDown = function() {
	if (persionCountDown == 0) {
		persionCountDown = 60;
	} else if (persionCountDown == 1) {
		psSmsBtn.removeAttr("disabled");
		psSmsBtn.html("获取验证码");
		persionCountDown = 0;
		return;
	}
	psSmsBtn.attr("disabled", true);
	psSmsBtn.html(persionCountDown + "秒");
	persionCountDown--;
	setTimeout("Register.getPersionBtnCountDown()", 1000);
};

/**
 * 企业用户注册回调
 */
Register.epCallBack = function(data) {
	lock = false;
	var nowDis = $("#type").prop("disabled");
	if(isDis!=nowDis){
		$("#type").attr("disabled","disabled");
	}
	Feng.info(data.msg);
	if (data.code == 200) {
		parent.layer.close(window.parent.Login.layerIndex);
	}
};
Register.typeChange = function(){
	$("#type").removeAttr("disabled");
	var type = $("#type").val();
	if(type==1){
		$("#enterpriseTag,#industryField").parent().parent().attr("style","display:block");
	}else{
		$("#enterpriseTag,#industryField").val("");
		$("#enterpriseTag,#industryField").parent().parent().attr("style","display:none");

	}
}


$(function(){
	$('#ep_form').bootstrapValidator({
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		group:'.rowGroup',
		fields: Register.validateFields,
		live: 'enabled',
		message: '该字段不能为空'
	});
	var userType = Feng.getUrlValue("userType");
	var username = Feng.getUrlValue("username");
	var name = Feng.getUrlValue("name");
	var idCard = Feng.getUrlValue("idCard");
	var type = Feng.getUrlValue("type");
	if(userType == 1){			//企业用户trigger
		$("#source").val(2);
		$("#username").val(username).attr("readonly","readonly");
		$("#name").val(name).attr("readonly","readonly");
		$("#idCard").val(idCard).attr("readonly","readonly");
		$("#type").val(type).trigger("change");
		$("#type").attr("disabled","disabled");
		$("#password").val("").attr("placeholder","密码为聚才网账号密码").attr("readonly","readonly");
		$("#re_password").val("").attr("placeholder","密码为聚才网账号密码").attr("readonly","readonly");
		$("#personTab").css("pointer-events","none");
	} else if(userType == 2){	//个人用户
		$("#personTab").css("pointer-events","block");
		$("#personTab").children().click();
		$("#ps_source").val(2);
		$("#ps_username").val(name).attr("readonly","readonly");
		$("#ps_idCard").val(idCard).attr("readonly","readonly");
		$("#ps_password").val("").attr("placeholder","密码为聚才网账号密码").attr("readonly","readonly");
		$("#ps_re_password").val("").attr("placeholder","密码为聚才网账号密码").attr("readonly","readonly");
		$("#enTab").css("pointer-events","none");
	} else{						//无

	}
	var arr = [
		{"name":"street","code":"un_street"},
		{"name":"enterpriseTag","code":"un_enterprise_enterpriseTag"},
		{"name":"industryField","code":"un_industryField"}];
	Feng.findChildDictBatch(JSON.stringify(arr));
	$("#imgurl").change(function (e) {
		var tag = e.target;
		var file = tag.files[0];
		var imgSrc;
		var reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = function() {
			var src = $("#imgurl").val();
			var sn = src.lastIndexOf(".");
			var suffix = src.substring(sn+1,src.length);
			if(suffix == 'pdf' || suffix == 'PDF'){
				$("#photoImg").attr("src", Feng.ctxPath + "/static/img/Pdf.png");
			}else{
				imgSrc = this.result;
				$("#photoImg").attr("src", imgSrc);
			}
		};
	});
});