var UserAdd = function () {

    var initForm = function () {
        var initFormCtrl = function () {
            bindEvent();
        };


        function passwordLevel(password) {
            var Modes = 0;
            for (i = 0; i < password.length; i++) {
                Modes |= CharMode(password.charCodeAt(i));
            }
            return bitTotal(Modes);

            //CharMode函数
            function CharMode(iN) {
                if (iN >= 48 && iN <= 57)//数字
                    return 1;
                if (iN >= 65 && iN <= 90) //大写字母
                    return 2;
                if ((iN >= 97 && iN <= 122) || (iN >= 65 && iN <= 90)) //大小写
                    return 4;
                else
                    return 8; //特殊字符
            }

            //bitTotal函数
            function bitTotal(num) {
                modes = 0;
                for (i = 0; i < 4; i++) {
                    if (num & 1) modes++;
                    num >>>= 1;
                }
                return modes;
            }
        }

        $.validator.addMethod("strongPsw",function(value,element){
            if(!value||value=='')
            {
                return true;
            }
            if(passwordLevel(value)==3||passwordLevel(value)==4){return true;}
            return false;
        },"8位以上，需同时包含大写、小写及数字");
         
        var bindEvent = function() {
            $("#roleform").validate({
                rules: {
                	username: {
                        required: true,
                        htmlescape: true
                    },
                    password: {
                        minlength: 6,    //最小长度
                        maxlength: 50,   //最大长度
                        //strongPsw: true,  //密码强度
                        htmlescape: true
                    },
                    email: {
                        htmlescape: true
                    }
                    
                },
                messages: {
                	confirmNewPassword: {equalTo: "输入与上面相同的密码"},
                	username: {
                        htmlescape: "请您不要输入特殊字符!"
                    },
                    password: {
                        htmlescape: "请您不要输入特殊字符!"
                    },
                    email: {
                        htmlescape: "请您不要输入特殊字符!"
                    }
                },
                submitHandler: function (form) 
                {	var test=/^[a-zA-Z0-9]{3,12}$/;
                	if(!test.test($("#username").val())){
                		$("#username").after("<label class='error' for='password'>只允许英文+数字，长度为3~12</label>");
                		return; 
                	}else{
                		$("#username").next("label").remove();
                	} 
                	var roles = $("#roles").val();
                	var acountsid = $("#accountsid").val();
                	var password; 
                	if($("#password").val()&&$("#password").val()!='')
                	{
                		password = sha256_digest($("#password").val());
                	}
                	
                    var formdata = 
                    {
                        username:$("#username").val() || '',
                        password:password,
                        email:$("#email").val() || '',
                        description:$("#description").val() || '',
                        empid:$("#empid").val() || '',
                        userid:$("#userid").val()
                    };
                      
                    var accountId="";
                    var issubmit=true;
                    $("#table tbody tr").each(function(i,val){
                    	
                    	// var dealerNo=$(this).find("td:eq(1) select option[data-attr='sys:dealer']").val()//获取为经销商编码的ID
                    	formdata["account"+i]=$(this).find("td:first select").val();
                    	formdata["roles"+i]=$(this).find("td:eq(1) select").val();

                    	if(accountId.indexOf(","+$(this).find("td:first select").val()+",")>=0){
                    		Cfapp.alert({ 
                    	        message: "存在重复公众号，请重新选择",
                    	        btntext: "确认",
                    	        success: function () {
                    	        }
                    	    });
                    		issubmit=false; 
                    		return false;
                    	}else{
                    		accountId+=","+$(this).find("td:first select").val()+",";
                    	}
                    });

                    accountId="";
                    $("#qytable tbody tr").each(function(i,val){

                        formdata["qyaccount"+i]=$(this).find("td:first select").val();
                        formdata["qyroles"+i]=$(this).find("td:eq(1) select").val();

                        if(accountId.indexOf(","+$(this).find("td:first select").val()+",")>=0){
                            Cfapp.alert({
                                message: "存在重复企业号，请重新选择",
                                btntext: "确认",
                                success: function () {
                                }
                            });
                            issubmit=false;
                            return false;
                        }else{
                            accountId+=","+$(this).find("td:first select").val()+",";
                        }
                    });


                    var defaultAccountId = '';
                    //判断默认账号
                    $('#roleform input[type="radio"]').each(function (index) {
                        if ($(this).bootstrapSwitch('state')) {
                            //defaultAccountId = formdata["qyaccount"+index];
                            defaultAccountId = $(this).parent().parent().parent().parent().parent().find('td:first .select2').val();
                        }
                    });

                    if (!defaultAccountId || defaultAccountId == '') {
                        Cfapp.alert({
                            message: "请选择默认的账号",
                            btntext: "确认",
                            success: function () {
                            }
                        });
                        issubmit = false;
                        return false;
                    }
                    console.log(defaultAccountId);
                    formdata.defaultAccId = defaultAccountId;
                   // console.log(formdata);  
                    if(issubmit){
                    	formSubmit(formdata); 
                    }
                }
        }); }

        initFormCtrl();
    };

    return {

        //main function to initiate the module
        init: function () {
            initForm();
        }

    };

}();