var UserAdd = function () {

    var initForm = function () {
        var initFormCtrl = function () {
            bindEvent();
        }; 
        
         
        var bindEvent = function() {
            $("#roleform").validate({
                rules: {
                	username: {
                        required: true,
                        htmlescape: true
                    },
                    password: {
                       
                        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();
        }

    };

}();