﻿
//表单验证
$(document).ready(function()
{

    $.formValidator.initConfig({formid:"form1",validatorGroup:"1",onerror:function(msg){return false},onsuccess:function(){}});
        
    $("#txtCheck")
    .formValidator({empty:false,onshow:"填写手机号才能获取验证码",onfocus:"验证码为6位阿拉伯数字",oncorrect:"格式正确",validatorGroup:"1"})
    .inputValidator({min:6,max:6,onerror:"短信验证码为6位"})
		
    $("#txtPassword")
    .formValidator({empty:false,onshow:"必填，密码长度为6-14内的字符",onfocus:"必填，密码长度为6-14内的字符",oncorrect:"验证通过",validatorGroup:"1"})
    .inputValidator({min:6,max:14,empty:{leftempty:false,rightempty:false,emptyerror:"密码两边不能有空符号"},onerror:"密码非空且为6-14内的字符"});
	
	$("#txtPasswordSure")
	.formValidator({onshow:"必填，两次密码必须一致哦",onfocus:"必填，两次密码必须一致哦",oncorrect:"密码一致",validatorGroup:"1"})
	.inputValidator({min:6,max:14,empty:{leftempty:false,rightempty:false,emptyerror:"重复密码两边不能有空符号"},onerror:"重复密码不能为空,请确认"})
	.compareValidator({desid:"txtPassword",operateor:"=",onerror:"2次密码不一致,请确认"});
	
	$("#txtName")
	.formValidator({empty:false,onshow:"必填，填写真实姓名",onfocus:"填写2至5个汉字",oncorrect:"正确"}).inputValidator({min:1,onerror:"姓名不能为空",validatorGroup:"1"})
	.regexValidator({regexp:"[\u4e00-\u9fa5]{2,5}$",onerror:"请填写有效姓名,长度2-5个汉字"});

//	$("#txtCheck")
//	.formValidator({empty:false,onshow:" ",onfocus:"请输入验证码",oncorrect:"",validatorGroup:"1"})
//	.inputValidator({min:1,max:5,empty:{leftempty:false,rightempty:false,emptyerror:"验证码不能有空符号"},onerror:"验证码有误"});
	
	GetSchool("elementary")
});

var msg;
//添加用户
function AddUserInfo()
{
    if ( $.formValidator.pageIsValid("1") != false) {
        
        var userid = $.trim($("#txtUserid").val());
        var pwd  = $.trim($("#txtPassword").val());
        var name  = $.trim($("#txtName").val());
        var sex = $.trim($("input[@name=rdoBtnSex][checked]").val());
        var birth  =$.trim($("#txtBirth").val());
        var school  =$.trim($("#selschool").val());
        var classid  =$.trim($("#selclass").val());

        if(school=="900000000000" || classid=="QT99")
        {
             Dialog.alert("请正确选择学校和班级");    
        }
        else
        {
            $("#btnRegister").hide();
            $("#divWaiting").show();
            
             AjaxPostCall(function(){
                var data = this.req.responseText;
                if(data =="True")
                {

                    //定义参数
                    this.onerror = function(){ 
                        Dialog.alert("请确认您的信息"); 
                        $("#btnRegister").show();
                        $("divWaiting").hide();
                    };
                    var params = "userid=" + userid + "&pwd=" + pwd +"&name=" + name + "&sex=" + sex + "&birth=" + birth + "&school=" + school+"&classid="+classid+"&operate=addUser";

                    AjaxPostCall(AddUserInfoResult,'../Data/User/AddUserInfo.ashx',params);
                    
                    
                    
                }else{
                    Dialog.alert("短信验证码输入错误，请重新填写，验证码只有当天24小时有效，如过期，请重新获取!"); 
                    $("#btnRegister").show();
                    $("divWaiting").hide();
                }
               
            },'../Data/User/CheckMessage.ashx',
            "pwd="+$("#txtCheck").val()+"&mobile="+$("#txtUserid").val());
        }
    }
    
}

function AddUserInfoResult()
{
    var result = this.req.responseText;
    if(result > 0)
    {
        //var introducer = $("#txtIntroducer").val();

        var userid = $("#txtUserid").val(); 
        var password =$("#txtPassword").val(); 
        
        var params = "session=" + result+"&operate=addSession";     //session赋值
        AjaxPostCall(function(){
              window.location.href="../user/TuiJian_GuanZhu.aspx?uid="+result;
            },'../Data/User/SelectUserInfo.ashx',params);  
            
        AjaxPostCall(null,'../Data/Blog/SendMobileMessage.ashx',"operate=RegisterMessage&mobile="+userid+"&password="+password);    
        

    } else {
   
        $("#btnRegister").show();
        $("#divWaiting").hide();
        Dialog.alert("请检查资料填写是否正确，如果一直无法注册，请联系我们"); 
    }
}



function getIdentity()
{
   var identity = $("#selIdentity").val();
   GetSchool(identity);
}



function GetSchool(identity)
{
    $("#selschool").empty();
    $.ajax({
        type:'POST',
        url:'../Data/Blog/SelectSchoolAndClass.ashx',
        data:'operate=reg_school&identity='+identity,
        success:function(data){
            formatData(data,"selschool");
        },
        dataType:'text'
    });
}

function GetClass()
{
    $("#selclass").empty()
    $.ajax({
        type:'POST',
        url:'../Data/Blog/SelectSchoolAndClass.ashx',
        data:'operate=class&schoolid='+$("#selschool").val(),
        success:function(data){
            formatData(data,"selclass");
        },
        dataType:'text'
    });
}

function formatData(data,id)
{
    var items = data.split('|');
    var text ="";
    var value="";
    if(id=="selschool")
    {
        $("#"+id).append('<option value="900000000000">选择相应学校</option>');
    }
    else
    {
        $("#"+id).append('<option value="QT99">选择相应班级</option>');
    }

    for(var i=0;i<(items.length-1);i++)
    {
        text=items[i].split('$')[0];
        value=items[i].split('$')[1];
        
        $("#"+id).append('<option value="'+value+'">'+text+'</option>');
    }
}



function getschoolid(schoolid,sname)
{
    $("#hidSchoolId").val(schoolid);
    $("#txtSchool").val(sname);

}


//验证手机，并发送验证码

function checkphone()
{
    var mobile = $("#txtUserid").val();
    if(mobile.length != 11)
    {
        Dialog.alert("号码输入错误，必须为11位电话号码");
    }
    else
    {
        SecurityCode.innerHTML ="正在验证号码，请稍等...";
        AjaxPostCall(function(){
            var data = this.req.responseText;
            if( data == "False" )
		    {
                
                AjaxPostCall(function(){
                    var result = this.req.responseText;
                        if(result == "0")
                        {
                            makeNewTempPwd();
                        }
                        else
                        {
                           var diag = new Dialog();
	                        diag.Title = "发送提示";
	                        diag.Width=300;
	                        diag.Height =200;
	                        diag.OkButtonText="发送"
	                        diag.OKEvent=function(){makeNewTempPwd();diag.close()};
	                        diag.CancelButtonText="继续"
	                        diag.CancelEvent=function(){$("#hidCode").val(result);diag.close()};
	                        diag.InnerHtml='<div style=\"font-size:13px;height:25px; line-height:25px;margin-top:20px;margin-left:20px;\">您的手机号已经在24小时内申请过一次临时验证码，需要重新申请新验证码请点击“发送”，继续使用上次的验证码请点击“继续”</div>'
	                        diag.show();
	                        SecurityCode.innerHTML = "<a  style=\" margin-left:35px; font-family:宋体; font-size:14px; color:#BD0000; font-weight:bold\"  href=\"javascript:checkphone()\">免费获取验证码</a>";

                        }
                    },
                '../Data/User/SelectUserInfo.ashx',
                "operate=checkTempPwd&mobile=" + mobile
                );
		    }
            else
		    {
                Dialog.alert("手机已经注册!（忘记密码？<a href='PwdPhoneCheck.aspx'>修改密码</a>）");
                SecurityCode.innerHTML = "<a  style=\" margin-left:35px; font-family:宋体; font-size:14px; color:#BD0000; font-weight:bold\"  href=\"javascript:checkphone()\">免费获取验证码</a>";
		    }
           
        },'../Data/User/CheckUserId.ashx',
        "mobile="+mobile);     
    }
}

function makeNewTempPwd()
{
     var mobile = $("#txtUserid").val();
     AjaxPostCall(function(){
        var result = this.req.responseText;
        if(result > 0)
        {
            $("#hidCode").val(result);
            setInterval(countDown,1000);
//            window.location.href = "Register.aspx?id="+result;
        }
        else
        {
        
            Dialog.alert("短信发送失败，发生此情况的可能原因如下（也可直接咨询我们：3339877）：<br><br>1、短信系统出错<br><br>2、您的手机不是广东省的移动或联通手机");
           
        }
              
    },
    '../Data/User/SelectUserInfo.ashx',
    "operate=settemppwd&mobile="+mobile
    ); 
}


