$(document).ready(function(){
	var xoops_url = "http://" + location.hostname;
	var mobileflag = 0;
	$.formValidator.initConfig({formid:"regform",onerror:function(msg){alert(msg)},onsuccess:function(){$("#mobile").attr("disabled",false);}});
	$("#uname")
		.formValidator({onshow:"请输入用户名",onfocus:"用户名至少4个字符,最多20个字符",oncorrect:"该用户名可以注册"})
		.inputValidator({min:4,max:20,onerror:"用户名长度为4-20位"})
		.regexValidator({regexp:"uname",datatype:"enum",onerror:"用户名不合法"})
	    .ajaxValidator({
		    type : "GET",
			url : xoops_url+"/modules/system/include/ajaxregcheck.php?op=unamecheck",
			success : function(msg){	
	            if( msg == "unameexist" )
				{
	                return false;
				}
	            else if(msg == "unameok")
				{
	                return true;
				}
			},
			error: "服务器没有返回数据，可能服务器忙，请重试",
			onerror : "该用户名不可用，请更换用户名",
			onwait : "正在对用户名进行合法性校验，请稍候..."
		});
	$("#pass")
		.formValidator({onshow:"请输入密码",onfocus:"密码不能为空",oncorrect:"密码合法"})
		.inputValidator({min:6,max:20,empty:{leftempty:false,rightempty:false,emptyerror:"密码两边不能有空符号"},onerror:"密码长度为6-20位,请确认"})
		.regexValidator({regexp:"password",datatype:"enum",onerror:"密码只能为字母、数字、下划线"});
	$("#vpass")
		.formValidator({onshow:"请输入重复密码",onfocus:"两次密码必须一致",oncorrect:"密码一致"})
		.inputValidator({min:6,max:20,empty:{leftempty:false,rightempty:false,emptyerror:"重复密码两边不能有空符号"},onerror:"重复密码长度为6-20位,请确认"})
		.compareValidator({desid:"pass",operateor:"=",onerror:"两次次密码不一致,请确认"});
	$("#email")
		.formValidator({onshow:"请输入邮箱",onfocus:"请输入您的常用邮箱,格式为: xxxx@tom.com",oncorrect:"您已填写正确的邮箱"})
		.inputValidator({min:6,max:100,onerror:"你输入的邮箱长度非法,请确认"})
		.regexValidator({regexp:"email",datatype:"enum",onerror:"你输入的邮箱格式不正确"})
		.ajaxValidator({
		    type : "GET",
			url : xoops_url+"/modules/system/include/ajaxregcheck.php?op=emailcheck",
			success : function(msg){	
	            if( msg == "emailexist" )
				{
	                return false;
				}
	            else if(msg == "emailok")
				{
	                return true;
	                mobileflag = 1;
				}
			},
			error: "服务器没有返回数据，可能服务器忙，请重试",
			onerror : "该邮箱不可用，请使用其他邮箱",
			onwait : "正在对邮箱进行合法性校验，请稍候..."
		});
	$("#mobile")
		.formValidator({onshow:"请输入您的手机号，以便中大奖及时通知您",oncorrect:"您已正确填写手机号",onempty:"请输入您的手机号，以便中大奖及时通知您"})
		.inputValidator({min:11,max:11,onerror:"手机号码必须是11位的,请确认"})
		.regexValidator({regexp:"mobile",datatype:"enum",onerror:"你输入的手机号码格式不正确"})
		.ajaxValidator({
		    type : "GET",
			url : xoops_url+"/modules/system/include/ajaxregcheck.php?op=mobilecheck",
			success : function(msg){	
	            if( msg == "mobileexist" )
				{
	                return false;
				}
	            else if(msg == "mobileok")
				{
				    mobileflag = 1;
				    $("#getcaptcha").attr('disabled',false);
	                return true;
				}
			},
			error: "服务器没有返回数据，可能服务器忙，请重试",
			onerror : "该手机号已被使用，请使用其他手机号",
			onwait : "正在对手机号进行合法性校验，请稍候..."
		});
	$("#name")
		.formValidator({onshow:"请输入您的真实姓名",onfocus:"真实姓名不能为空,一旦注册将不能修改",oncorrect:"您已填写真实姓名"})
		.inputValidator({min:4,max:30,onerror:"你输入的姓名不合法,请确认"})
		.regexValidator({regexp:"realname",datatype:"enum",onerror:"你输入的姓名不合法"});
	$("#captcha")
		.formValidator({onshow:"点击获取验证码",onfocus:"点击获取验证码",oncorrect:"您已填写正确的验证码"})
		.inputValidator({min:4,max:4,onerror:"验证码只能为4位"})
		.ajaxValidator({
		    type : "GET",
			url : xoops_url+"/modules/system/include/ajaxregcheck.php?op=captchacheck",
			success : function(msg){	
	            if( msg == "captchawrong" )
				{
	                return false;
				}
	            else if(msg == "captchaok")
				{
	                return true;
				}
			},
			error: "服务器没有返回数据，可能服务器忙，请重试",
			onerror : "验证码错误",
			onwait : "正在对验证码进行合法性校验，请稍候..."
		});
	
	$("input:checkbox[@name='isadult']")
		.formValidator({defaultvalue:"agree"})
		.inputValidator({min:1,max:1,onerror:"您必须同意以下条款才能注册"});

	
	$("#identify")
		.formValidator({onshow:"请输入15或18位的身份证",onfocus:"输入15或18位的身份证",oncorrect:"输入正确"})
		.functionValidator({fun:isCardID});
	$("#withdraw_pass")
		.formValidator({onshow:"请输入提款密码",onfocus:"提款密码不能为空",oncorrect:"提款密码合法"})
		.inputValidator({min:6,max:20,empty:{leftempty:false,rightempty:false,emptyerror:"密码两边不能有空符号"},onerror:"密码长度为4-20位,请确认"})
		.regexValidator({regexp:"password",datatype:"enum",onerror:"密码只能为字母、数字、下划线"});
	$("#vwithdraw_pass")
		.formValidator({onshow:"请输入重复提款密码",onfocus:"两次提款密码必须一致",oncorrect:"提款密码一致"})
		.inputValidator({min:6,max:20,empty:{leftempty:false,rightempty:false,emptyerror:"重复密码两边不能有空符号"},onerror:"重复密码长度为4-20位,请确认"})
		.compareValidator({desid:"withdraw_pass",operateor:"=",onerror:"两次输入的密码不一致,请确认"});
	$("#province")
		.formValidator({onshow:"请选择省份",onfocus:"请选择省份",oncorrect:"已选择省份"})
		.inputValidator({min:1,onerror:"请选择省份"});
	$("#area_id")
		.formValidator({onshow:"请选择城市",onfocus:"请选择城市",oncorrect:"已选择城市"})
		.inputValidator({min:1,onerror:"请选择城市"});
	$("#bankname")
		.formValidator({onshow:"请输入开户行全称",onfocus:"请输入开户行全称",oncorrect:"输入正确"})
		.inputValidator({min:10,onerror:"请输入正确的开户行"});
	$("#bankaccount")
		.formValidator({onshow:"请输入银行卡号码",onfocus:"请输入银行卡号码",oncorrect:"输入正确"})
		.regexValidator({regexp:"num",datatype:"enum",onerror:"银行卡号码只能为数字"});
		
	$("#tel")
		.formValidator({empty:true,onshow:"请输入你的联系电话，可以为空",onfocus:"格式例如：0577-88888888",oncorrect:"输入正确"});
	$("#job")
		.formValidator({empty:true,onshow:"请输入你的职业，可以为空",onfocus:"请输入你的职业，可以为空",oncorrect:"输入正确"});
	$("#address")
		.formValidator({empty:true,onshow:"请输入你的地址，可以为空",onfocus:"请输入你的地址，可以为空",oncorrect:"输入正确"});
	$("#postcode")
		.formValidator({empty:true,onshow:"请输入邮编",onfocus:"邮编为6位数字",oncorrect:"输入正确"});
	$("#birthday")
		.formValidator({empty:true,onshow:"请选择你的生日",oncorrect:"输入正确"});
	$("#qq")
		.formValidator({empty:true,onshow:"请输入您的QQ号",onfocus:"请输入您的QQ号",oncorrect:"输入正确"});
		
	$("#identify,#withdraw_pass,#vwithdraw_pass,#province,#area_id,#bankname,#bankaccount").unFormValidator(true);
	$("#tel,#job,#address,#postcode,#birthday,#qq").unFormValidator(true);
	
	$("#optiondsc2").click(function(){
		if($(this).attr('checked')){
			$('#optionds2').slideDown();
			$("#identify,#withdraw_pass,#vwithdraw_pass,#province,#area_id,#bankname,#bankaccount").unFormValidator(false);
		}else{
			$('#optionds2').slideUp();
			$("#identify,#withdraw_pass,#vwithdraw_pass,#province,#area_id,#bankname,#bankaccount").unFormValidator(true);
		}
	});
	
	$("#optiondsc3").click(function(){
		if($(this).attr('checked')){
			$('#optionds3').slideDown();
			$("#tel,#job,#address,#postcode,#birthday,#qq").unFormValidator(false);
		}else{
			$('#optionds3').slideUp();
			$("#tel,#job,#address,#postcode,#birthday,#qq").unFormValidator(true);
		}
	});
	
	$("#getcaptcha").click(function(){
	    if(mobileflag==0){
	        alert("请填写正确的手机号");
	        $("#mobile").focus();
	        return false;
	    }
	    var mobile = $("#mobile").val();
	    var myReg = /^(13|15|18)[0-9]{9}$/;
	    if(myReg.test(mobile)){
	        $.ajax({
                type:"GET",
				data:"mobile="+mobile,
                url:xoops_url+"/class/captcha/image/scripts/smscaptcha.php",
			    success:function(msg){
			        if(msg=='succ'){
			            alert('短信发送成功，请查收');
                        $("#getcaptcha").attr("disabled",true);
                        $("#mobile").attr("disabled",true);
                        countdown();
			        }else{
			            alert(msg);
			        }
			    }
			        
            });
	    }else{
	        alert("手机号码不合法");
	    }
	});
	
});

var time = 30;
function countdown(){
    time = time-1;
    if(time==0){
        $("#getcaptcha").attr("disabled",false);
        $("#getcaptcha").attr("value","点击获取验证码");
        time = 30;
    }else{
        $("#getcaptcha").attr("value"," "+time+" 秒后可以重新发送验证码。。。");
        setTimeout("countdown()",1000);
    }
}