var Register = {};

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


Register.enterpriseUserRegister = function() {
    var username = $("#username").val();
    var name = $("#name").val();
    var idCard = $("#idCard").val();
    var agentName = $("#agentName").val();
    var agentPhone = $("#agentPhone").val();
    var legal = $("#legal").val();
    var street = $("#street").val();
    var address = $("#address").val();
    var type = $("#type").val();
    var agentEmail = $("#agentEmail").val();
    var talentType = $("#talentType").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 (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 (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(talentType==null||talentType==''){
            Feng.info("请选择企业标签!");
            return;
        }
        if(industryField == null || industryField ==''){
            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;
    }
    $("#ep_form")[0].submit();
};



/**
 * 企业用户注册回调
 */
Register.epCallBack = function(data) {
    Feng.info(data.msg);
    if (data.code == 200) {
        setInterval(function () {
            window.location.href=Feng.ctxPath + "/login.html";
        },3000);
    }
};
Register.typeChange = function(){
    var type = $("#type").val();
    if(type==1){
        $("#talentType,#industryField").parent().parent().attr("style","display:block");
    }else{
        $("#talentType,#industryField").val("");
        $("#talentType,#industryField").parent().parent().attr("style","display:none");
    }
}


$(function(){
    var arr = [
        {"name":"street","code":"un_street"},
        {"name":"talentType","code":"un_enterprise_talentType"},
        {"name":"industryField","code":"un_industryField"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
    $("select").each(function () {
        $(this).val($(this).attr("value"));
        $(this).trigger("change");
    });
    var photoImg = $("#photoImg").attr("src");
    var oldSn = photoImg.lastIndexOf(".");
    var oldSuffix = photoImg.substring(oldSn+1,photoImg.length);
    if(oldSuffix == 'pdf' || oldSuffix == 'PDF'){
        $("#photoImg").attr("src", Feng.ctxPath + "/static/img/Pdf.png");
    }
    $("#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);
            }
        };
    });
});