var global_fantasy_emailok = false;
var global_fantasy_pwdok = false;
var global_fantasy_vaildok = false;

function showregdiv(){
	$("#div_reg").show();  
}
function hideregdiv(){
	$("#div_reg").hide();
}
function clear_error(){
	$("#errmsg").html("");
}
function show_entrydiv(){
	$("#div_logined").show();
	$("#h1_logined").show();
	$("#div_login").hide();
	$("#h1_login").hide();
}
function show_logindiv(){
	$("#div_logined").hide();
	$("#h1_logined").hide();
	$("#div_login").show();
	$("#h1_login").show();
}
function refresh_code(){
	var r = Math.round(Math.random()*10000);
	$('#imgcode').attr('src','');
	$('#imgcode').attr('src',SW_URL_FANTASY+"auth_code.php?"+r);
}
function islogin(){
	var id_user_cookie = Cookie.get("id_user");
	var id_4_sw_user_cookie = Cookie.get("id_4_sw_user");
	if(id_4_sw_user_cookie != null) {
		return true;
	}else{
		return false;
	}
}
function checklogin(){
	if (islogin()){
		show_entrydiv();
	}else{
		show_logindiv();
		try{
			var rem_email = Cookie.get("sw_rember_email").split("_|_")[1];
		}catch(e){}
		if (rem_email != undefined){
			 $("#sw_email")[0].value = rem_email;
		}
	}
}
function validateSWIDEmail(){
	global_fantasy_emailok = false;
	var idEmail = $("#idEmail").val();
	if(idEmail!=""){
		if (!Utils.isEmail(idEmail)) {
			$("#emailmsg").html("<p>EMAIL错误!</p>");
		}else{
			$("#loginmsg").html("");
			validateEmail(idEmail,function (result){
				if (result.result == 'success') {
				   $("#emailmsg").html("<font color='green'>Email可以使用</font>");
				   global_fantasy_emailok = true;
			      }else{
				   $("#emailmsg").html("<font color='red'>Email已被占用</font>");
			      }
			}) ;
		}
	}
}
function confirmpwd(){
	global_fantasy_pwdok = false;
	var idPassword = $("#idPassword").val();
	var pwdconfirm = $("#pwdconfirm").val();
	if (pwdconfirm != idPassword){
		$("#errmsg").html("<font color='red'>两次输入的密码不一致，请重新输入！</font>");
		setTimeout("clear_error()",2000);
	}else{
		global_fantasy_pwdok = true;
	}
}
function verify_code(){
	global_fantasy_vaildok = false;
	var code = $("#input_code").val();
	$.get(SW_URL_FANTASY+'verify.php', {authcode:code}, function(d) {
		if (d == "1"){
			global_fantasy_vaildok = true;
			return true;
		}else{
			$("#errmsg").html("<font color='red'>验证码错误，请重新输入！</font>");
			setTimeout("clear_error()",2000);
			return false;
		}
	});
}
function regSWID(){
	var idEmail = $("#idEmail").val();
	var idPassword = $("#idPassword").val();
	var idNickName = $("#idNickName").val();
	var pwdconfirm = $("#pwdconfirm").val();
	//alert("global_fantasy_emailok:"+global_fantasy_emailok +"/global_fantasy_pwdok:"+global_fantasy_pwdok +"/global_fantasy_vaildok:"+ global_fantasy_vaildok);
	if (global_fantasy_emailok && global_fantasy_pwdok && global_fantasy_vaildok){
		$("#errmsg").html("<p>注册中，请稍等...</p>");
		if(idEmail!=""&&idPassword!=""&&idNickName!=""){
			document.getElementById('a_btnok').onclick=function(){};
			regID(idEmail,idPassword,idNickName,function (result){
				//alert("result:"+result.result);
				if (result.result == 'success') {
					$("#errmsg").html("<p>注册成功！</p>");
					window.location.reload();
				}else{
					$("#errmsg").html("<p>注册失败！</p>");
					document.getElementById('a_btnok').onclick=regSWID;
				}
			});
		}
	}
}
function swlogout(){
	logout();
	window.location.reload();
}
function swlogin(callback) {
	var email = $("#sw_email").val();
	var password =$("#sw_password").val();
	if (!Utils.isEmail(email)) {
		$("#loginmsg").html("<p>EMAIL错误!</p>");
	}else if (!Utils.isPassword(password)) {  
		$("#loginmsg").html("<p>密码错误!</p>");
	} else {
		$("#loginmsg").html("");
        $.getJSON('http://id.shawei.tom.com/user.do?m=login&format=json&jsonpCallback=?', {
            'email' :email,
            'password' :password
        }, function(json) {
            if (json.result == 'success') {
		show_entrydiv();
		var rembercount = $("#rembercount").val();
		if(rembercount){
			Cookie.clear("sw_rember_email");
			var tempcookie = Cookie.get("id_user");
			Cookie.set("sw_rember_email",tempcookie);
		}
		window.location.reload();
            } else {
		$("#loginmsg").html("<p>用户名或密码错误</p>");
            }
        });
    }
}
function confiremail(){
	var email = $("#sw_email").val();
	if (!Utils.isEmail(email)) {
		$("#loginmsg").html("<p>EMAIL错误!</p>");
	}else{
		$("#loginmsg").html("");
	}
}
