var usertype = 0;//用户申请ROO的状态
var roosqpass = '';
var formflag = false;
var ival=null;

function enterroo(mid){
	if('' == mid){//没有登录
		$("#roosqlogin").css("height", "250px");
		showAlertDiv('roosqlogin');
	}else {//已经登录但没有申请
		showAlertDiv('roonote');
	}
}

function targetroo($mid){
	$.ajax({
		type:'POST',
		url:roourl+'setroosq.php',
		data:'mid='+mid+'&rand='+Math.random(),
		success:function(msg){
			if('ok' == msg){
				hideAlertDiv('roonote');
				$('#roosqlogin').hide();
				//alert(document.getElementById('rooform'));
				document.getElementById('rooform').submit();
//				$("#rooform").submit();
				location.reload();
			}else{
				//申请失败！
			}
		}
	});
}

var roosqemailerrnum = 1;
function checkusertype(){
	if(0 == usertype){
		var email = $("#roouser").val();
		if('' == email){
			$("#roosqerr").html('请输入电子邮件！');
			if(roosqemailerrnum < 3){
				roosqemailerrnum ++;
			}else {
				$('#roovalidatetr').show();
				$("#roovalidate").val('');
				$('#roosqlogin').attr('class', 'wdarea1 zxrjsq zxrjdl');
				$("#roosqlogin").css('height', 'auto');
				//changevalidateimg("roovalidateimg");
			}
			return false;
		}
		var emailreg = /^((\w+([-+.]\w+)*))@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;;
		if(!emailreg.test(email)){
			$("#roosqerr").html('邮箱格式不正确，请重新输入！');
			if(roosqemailerrnum < 3){
				roosqemailerrnum ++;
			}else {
				$('#roovalidatetr').show();
				$("#roovalidate").val('');
				$('#roosqlogin').attr('class', 'wdarea1 zxrjsq zxrjdl');
				$("#roosqlogin").css('height', 'auto');
				//changevalidateimg("roovalidateimg");
			}
			return false;
		}
		if('none' != $("#roovalidatetr").css('display')){
			validate = $("#roovalidate").val();
			if('' == validate){
				$("#roosqerr").html('请输入验证码！');
				changevalidateimg("roovalidateimg");
				return false;
			}else {
				var vflag = false;
				$.ajax({
					type:'POST',
					url:roourl+'checkvalidate.php',
					data:'validate='+validate+'&rand='+Math.random(),
					async:false,
					success:function(msg){
						if('error' == msg){
							vflag = false;
							$("#roosqerr").html('验证码错误！');
							changevalidateimg("roovalidateimg");
						}else {
							vflag = true;
							$('#roovalidatetr').hide();
							//changevalidateimg("roovalidateimg");
							//$("#roovalidate").val('');
							$("#roosqerr").html('');
						}
					}
				});
				if(!vflag){
					return false;
				}
			}
		}
		$.ajax({
			type:'POST',
			url:roourl+'checkusertype.php',
			data:'email='+email+'&rand='+Math.random(),
			success:function(msg){
				if('ok' == msg){//是申请过的用户
					$('#roopwdtr').show();
					$('#roosqnote').html('亲爱的会员，您已经成功申请过 RedOffice 在线办公软件，登录后即可使用。');
					$('#rootj').html('登　录');
					$('#roowjpwd').text('忘记密码？');
					$("#roosqlogin").css('height', 'auto');
					usertype = 1;
					$("#roouser").attr('readonly', true);
					$("#roouser").attr('disabled', true);
				}else if('no' == msg){//没有申请的用户
					$('#roopwdtr').show();
					$('#roosqnote').html('亲爱的会员，填写登录信息后即可使用 RedOffice 在线办公软件。');
					$('#rootj').html('登　录');
					$('#roowjpwd').text('忘记密码？');
					$("#roosqlogin").css('height', 'auto');
					usertype = 2;
					$("#roouser").attr('readonly', true);
					$("#roouser").attr('disabled', true);
				}else{//不是注册用户
					usertype = 3;
					var chars = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
					var pwd = "";
				    for(var i = 0; i < 6 ; i ++) {
				        var id = Math.ceil(Math.random()*35);
				        pwd += chars[id];
				    }
					$.ajax({
						type:'POST',
						url:roourl+'roouserreg.php',
						data:'user='+email+ '&pwd=' + pwd + '&rand='+Math.random(),
						success:function(msg){
							if('ok' == msg){
								var url = sso+'/login?outer=true&username=' + email + '&password=' + pwd + '&rand=' + Math.random() + '&jsoncallback=?';
								$.getJSON(url, {}, function(json){
									if('sucesses' == json.info){//成功
										//showAlertDiv2('roosqlogin', 'roouserregnote');
										changeDiv('roosqlogin', 'roouserregnote', 'roosqerr', '');
										//document.getElementById('rooform').submit();
									}
								});
							}else {
								usertype = 0;//申请失败
							}
						}
					});
				}
			}				
		});
	}else if(3 == usertype){
		$("#roosqerr").html('进行中...');
		return false;
	}else {
		var user = $("#roouser").val();
		var pwd = $("#roopwd").val();
		var pwdlen = pwd.length;
		if('' == pwd){
			$("#roosqerr").html('请输入密码！');
			return false;
		}
		if(pwdlen<6 || pwdlen>12){
			$("#roosqerr").html('密码长度为6-12个字符！');
			return false;
		}
		for (var i=0; i<pwdlen; i++) {
			var intCode = pwd.charCodeAt(i);
			if((((pwd.charCodeAt(i) >= 0x3400) && (pwd.charCodeAt(i) < 0x9FFF)) || (pwd.charCodeAt(i) >= 0xF900)))
			{
				$("#roosqerr").html('密码应由英文字母、符号或数字组成！');
				return false;
			}
		}
		
		if(1 == usertype || 2 == usertype){
			var url = sso+'/login?outer=true&username=' + user + '&password=' + pwd + '&rand=' + Math.random() + '&jsoncallback=?';
			$.getJSON(url, {}, function(json){
				if(true || 'sucesses' == json.info){//成功
					if(2 == usertype){
						//修改申请状态
						$.ajax({
							type:'POST',
							url:roourl+'setroosq.php',
							data:'mid='+user+'&rand='+Math.random(),
							async:false,
							success:function(msg){
							if('ok' == msg){
								hideAlertDiv('roosqlogin');
								f = $("form#rooform");
//								f.submit();
//								location.reload();
								formflag = true;
							}else{
								if('waiting' == msg){
									changeDiv('roosqlogin', 'alertdivnote', 'alertnotep', '您已经提交了申请，我们将在3个工作日内给您回复，请及时查收邮件，感谢您关注 RedOffice 在线办公软件。');
								}
							}
						}
					});
				}else {
					hideAlertDiv('roosqlogin');
					formflag = true;
//					document.getElementById('rooform').submit();
//					location.reload();
				}
			}else {
				$("#roosqerr").html('密码不正确 请重新输入！');
			}
		});					
		ival = setInterval(formsubmit, 500);
	}else {
		
	}
}
}

function formsubmit(){
	if(formflag){
		clearInterval(ival);
		ival = null;
		document.getElementById('rooform').submit();
		location.reload();
	}else {
		if(60000 <= ival){//超过一分钟后停止，认为登录失败
			clearInterval(ival);
		}
	}		
}

$(function(){
	
});