var isName  = false;
var iseName = false;
var isEmail = false;
var isPass	= false;
var rePass	= false;
var rImgSrc = '<img src="' + PUBLIC + '/Images/check_right.gif" width="13" />';
var eImgSrc = '<img src="' + PUBLIC + '/Images/check_error.gif" width="13" />';
var emailNotice  = ' 邮箱地址格式不正确!';
var emailNotice2 = ' 该邮箱已经被注册!';
var emailNotice3 = ' 由于163、126、yeah邮箱不能收到确认信，请更换注册邮箱!';
var nameNotice   = ' 姓名只能由两个到六个汉字组成!';
var enameNotice  = ' 由3-20位字母、数字或下划线构成，必须以字母开头!';
var passNotice   = ' 密码只能由6-20位字母、数字、下划线构成!';
var repassNotice = ' 两次密码输入必须一致!';
if (typeof(chkinvite)=='undefined') {
	chkinvite = 0;
}

function isMail(mail)  {
    return(new RegExp(/^[A-Za-z0-9+]+[A-Za-z0-9\.\_\-+]*@([A-Za-z0-9\-]+\.)+[A-Za-z0-9]+$/).test(mail)); 
}

function isBlockMail(mail)  {
	mail = mail.toLowerCase();
    if( mail.indexOf('@163.')!=-1 || mail.indexOf('@126.')!=-1 || mail.indexOf('@yeah.')!=-1 ) return true;
    else return false;
}

$(document).ready(function(){
	$("#ename").blur(
		function() {
			if (!$('#ename').val()) {
				return false;
			}
			var reg=/^[a-zA-Z][a-zA-Z0-9_]{1,18}[a-zA-Z0-9]$/i;
			if (!reg.test($('#ename').val())) {
				iseName = false;
				$('#enameMsg').html(chkinvite ? eImgSrc + enameNotice : eImgSrc);
			} else {
				iseName = true;
				$('#enameMsg').html(rImgSrc);
		    }
		}
	);
	$("#name").blur(
		function() {
			if (!$('#name').val()) {
				return false;
			}
			var reg = /^[\u4e00-\u9fa5]{2,6}$/i;
			if (!reg.test($('#name').val())) {
				isName = false;
				$('#nameMsg').html(chkinvite ? eImgSrc + nameNotice : eImgSrc);
			} else {
				isName = true;
				$('#nameMsg').html(rImgSrc);
		    }
		}
	);
	
	$("#password").blur(
		function() {
			if (!$('#password').val()) {
				return false;
			}
			var reg = /\w{6,20}/i;
			if (!reg.test($('#password').val())) {
				isPass = false;
				$('#passMsg').html(chkinvite ? eImgSrc + passNotice : eImgSrc);
		    } else {
				isPass = true;
			    $('#passMsg').html(rImgSrc);
			}
		}
	);
	$("#repassword").blur(
		function() {
			if (!$('#repassword').val()) {
				return false;
			} else if ($('#password').val() != $('#repassword').val()) {
				$('#repsMsg').html(chkinvite ? eImgSrc + repassNotice : eImgSrc);
			    rePass = false;
		    } else {
			    $('#repsMsg').html(rImgSrc);
				rePass = true;
			}
		}
	);

	$("#email").blur(
		function() {
			var email = $("#email").val();
			if (!email) {
				return false;
			}
			if(!isMail(email)) {
				$('#emailMsg').html(chkinvite ? eImgSrc + emailNotice : eImgSrc);
//			} else if (isBlockMail(email)) {
//				isEmail = true;
//				$('#emailMsg').html(chkinvite ? eImgSrc + emailNotice3 : '<nobr>'+eImgSrc+' <a href="javascript:void(0);" title="'+emailNotice3+'" onclick="alert(\''+emailNotice3+'\')">请更换邮箱why</a></nobr>');
			}else {
				$.post(APP+'/Public/welcomeEmail',{email: email},function(txt){
					if(txt == 1){
						isEmail = true;
						$('#emailMsg').html(rImgSrc);
					}else{
						isEmail = false;
						$('#emailMsg').html(chkinvite ? eImgSrc + emailNotice2 : eImgSrc);
					}
				});
			}
		}
	);
	$("#leaning_target").change(
		function() {
			if ($('#leaning_target').val()) {
				$('#leanMsg').html(rImgSrc);
			} else {
				$('#leanMsg').html(eImgSrc);
			}
		}
	);
})

function checkInvites() {
	var ename = $("#ename").val();
	var name  = $("#name").val();
	var email = $("#email").val();
	var password = $("#password").val();
	var leaning_target = $("#leaning_target").val();
	var sex = $("input[@name=sex][@checked]").val();
	var inviter = $("#inviter").val();
	var checkRegCode = $("#checkRegCode").val();
	

	if(email == ""){
		alert("邮箱地址不能为空!");
		$("#email").focus();
		return false;
	}
	if(!isMail(email)) {
		alert(emailNotice);
		$("#email").focus();
		return false;
	}
	if (isEmail == false) {
		alert(emailNotice2);
		$("#email").focus();
		return false;
	}
//	if (isBlockMail(email)) {
//		alert(emailNotice3);
//		$("#email").focus();
//		return false;
//	}
	if(ename == ""){
		alert('英文名不能为空');
		$("#ename").focus();
		return false;
	}
	if (iseName == false) {
		alert(enameNotice);
		$("#ename").focus();
		return false;
	}
	if(name == ""){
		alert('姓名不能为空');
		$("#name").focus();
		return false;
	}
	if (isName == false) {
		alert(nameNotice);
		$("#name").focus();
		return false;
	}
	
	if(password == ""){
		alert("密码不能为空!");
		$("#password").focus();
		return false;
	}
	if(isPass == false){
		alert(passNotice);
		$("#password").focus();
		return false;
	}
	if($("#repassword").val() == ""){
		alert("确认密码不能为空!");
		$("#again").focus();
		return false;
	}
	if(rePass == false){
		alert("确认密码输入错误，请检查!");
		$("#again").focus();
		return false;
	}
	if(!leaning_target){
		alert("请选择一个学习目标!");
		$("#again").focus();
		return false;
	}
	//$('#sub').attr('disabled', true);
	//$('#sub').val('正在提交请稍后...');
	//return true;
	$.post(APP+"/Public/doreg",{email:email,name:name,ename:ename,sex:sex,password:password,leaning_target:leaning_target,inviter:inviter,checkRegCode:checkRegCode},function(res) {
		if (res['succ'] == 'succ') {
			location.href = res['url'];
		} else {
			alert(res['msg']);
		}
	},'json')
	return false;
}

//注册验证
function checkReg(t) {
	if (typeof(t)=='undefined') {
		t = '';
	}
	var ename = $("#ename").val();
	var name  = $("#name").val();
	var email = $("#email").val();
	var password = $("#password").val();
	var leaning_target = $("#leaning_target").val();
	var sex = $("input[@name=sex][@checked]").val();

	if( ename == "") {
		alert('英文名不能为空');
		$("#ename").focus();
		return false;
	}
	if (iseName == false) {
		alert(enameNotice);
		$("#ename").focus();
		return false;
	}
	if(name == ""){
		alert('姓名不能为空');
		$("#name").focus();
		return false;
	}
	if (isName == false) {
		alert(nameNotice);
		$("#name").focus();
		return false;
	}
	if(email == ""){
		alert("邮箱地址不能为空!");
		$("#email").focus();
		return false;
	}
	if(!isMail(email)) {
		alert(emailNotice);
		$("#email").focus();
		return false;
	}
	if (isEmail == false) {
		alert(emailNotice2);
		$("#email").focus();
		return false;
	}		
//	if (isBlockMail(email)) {
//		alert(emailNotice3);
//		$("#email").focus();
//		return false;
//	}
	if(password == ""){
		alert("密码不能为空!");
		$("#password").focus();
		return false;
	}
	if(isPass == false){
		alert(passNotice);
		$("#password").focus();
		return false;
	}
	if(!leaning_target){
		alert("请选择一个学习目标!");
		$("#again").focus();
		return false;
	}
	//$('#regform').attr('disabled', true);
	//return true;
//	return false;
	$.post(APP+"/Public/doreg",{email:email,name:name,ename:ename,sex:sex,password:password,leaning_target:leaning_target},function(res) {
		if (t == 'act')	{
			if (res['succ'] == 'succ') {
				checkEmailVerify(res['uid'], 'checks')
			} else {
				alert(res['msg']);
			}
		} else {
			if (res['succ'] == 'succ') {
				location.href = res['url'];
			} else {
				alert(res['msg']);
			}
		}
		
	},'json')
	return false;
}

function checkLogin(t) {
	if (typeof(t)=='undefined') {
		t = '';
	}
	var email = $("#loginEmail").val();
	var password = $("#password_login").val();
	if(email == "" || email =='请输入Email'){
		alert("邮箱地址不能为空!");
		$("#loginEmail").focus();
		return false;
	}
	if(!isMail(email)) {
		alert('邮箱地址输入错误，请检查!');
		$("#loginEmail").focus();
		return false;
	}
	if(password == ""){
		alert("密码不能为空!");
		$("#password_login").focus();
		return false;
	}
	$.post(APP+"/Public/checklogin",{account:email,password:password},function(res) {
		if (t == 'act')	{
			if (res['succ'] == 'succ') {
				$.unblockUI({ fadeOut: 200 }); 
				$.blockUI({ message:'登陆成功请提交您的帖子',css: { width: '460px',padding: '10px',border:'7px solid #aaa',cursor: 'default'} });
        		setTimeout($.unblockUI, 3000);
			} else {
				if (res['info'])	{
					$.unblockUI({ fadeOut: 200 }); 
    				$.blockUI({ message:res['info'],css: { width: '500px',padding: '0px',border:'1px solid #aaa',cursor: 'default'} }); 
				} else {
					alert(res['msg']);
				}
				
			}
		} else {
			if (res['succ'] == 'succ') {
				location.href = res['url'];
			} else {
				if (res['url'])	{
					location.href = res['url'];
				} else {
					alert(res['msg']);
				}
			}
		}
		
	},'json')
	return false;
}

function checkEmailVerify(uid, checks) {
	if (typeof(checks)=='undefined') {
		checks = '';
	}
	$.post(APP+"/Public/checkEmailVerify",{uid:uid},function(txt) {
		if(txt=='1') {
			$.unblockUI({ fadeOut: 200 }); 
			$.blockUI({ message:'登陆成功请提交您的帖子',css: { width: '460px',padding: '10px',border:'7px solid #aaa',cursor: 'default'} });
        	setTimeout($.unblockUI, 3000);
		} else if (txt=='2') {
			$.unblockUI({ fadeOut: 200 }); 
			$.blockUI({ message: $('#loginForm'),css: { width: '350px',padding: '20px',border:'7px solid #aaa',cursor: 'default'}});
		} else {
			if (checks == '') {
				alert('请先通过Email确认,开通您的账号');
			} else {
				$.unblockUI({ fadeOut: 200 });
				$.blockUI({ message:txt,css: { width: '500px',padding: '0px',border:'1px solid #aaa',cursor: 'default'} }); 
			}			
		}
	})
}
/**
 * 弹出窗口
 */
function box(msg, width) {
	$.blockUI({ message:msg,css: { width: width+'px',padding: '10px',border:'7px solid #aaa',cursor: 'default'} }); 
}
//密码强度
function CharMode(iN) {
	if (iN >= 48 && iN <= 57) //数字
		return 1;
	if (iN >= 65 && iN <= 90) //大写字母
		return 2;
	if (iN >= 97 && iN <= 122) //小写
		return 4;
	else
		return 8; //特殊字符
}
//计算出当前密码当中一共有多少种模式
function bitTotal(num) {
	modes = 0;
    for (i=0; i<4; i++) {
		if (num & 1) modes++;
		num>>>=1;
    }
   return modes;
}
//返回密码的强度级别
function checkStrong(sPW) {
	if (sPW.length <= 4)
		return 0; //密码太短
		Modes=0;
		for (i=0; i<sPW.length; i++) {
			Modes|=CharMode(sPW.charCodeAt(i)
		);
    }
	return bitTotal(Modes);
}
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
function pwStrength(pwd) {
	O_color="#eeeeee";
    L_color="#FF0000";
    M_color="#FF9900";
    H_color="#33CC00";
	if (pwd == null || pwd == '') {
		Lcolor = Mcolor = Hcolor = O_color;
    } else {
		S_level = checkStrong(pwd);
		switch(S_level) {
			case 0:
				Lcolor = Mcolor = Hcolor = O_color;
			case 1:
				Lcolor = L_color;
				Mcolor = Hcolor = O_color;
				break;
			case 2:
				Lcolor = Mcolor = M_color;
				Hcolor = O_color;
				break;
			default:
				Lcolor = Mcolor = Hcolor = H_color;
		
		}
   }
   $('#strength_L').css('background', Lcolor);
   $('#strength_M').css('background', Mcolor);
   $('#strength_H').css('background', Hcolor);
   return;
}
