$(document).ready(function() {
	

	// 登录文本框提示
	$(".alertLogin #login_username").focus(function() {
				var b = $(this).val();
				if (b == '请输入用户名') {
					$(this).val("");
				}
			});

	$(".alertLogin #login_username").blur(function() {
				var a = $(this).val();
				if (a == '') {
					$(this).val("请输入用户名");
				}
			});

	$(".alertLogin #login_password").focus(function() {
				var b = $(this).val();
				if (b == '请输入密码') {
					$(this).val("");
				}
				//$(".alertLogin #login_password").attr("type", "password");
			});

			
	//频道页登录
channellogin = function(){
   	var userName = $("#login_username").val();
   	var paswd = $("#login_password").val();
   	var autoLogin = $("#autoLogin").attr("checked");
   	if (userName.length == 0 || userName == '请输入用户名'){
   		alert("用户名不能为空！");
   		showErrors('[{error:"用户名不能为空！"}]');
   		return false;
   	}
   	if (paswd.length == 0){
   		//alert("密码不能为空！");
   		showErrors('[{error:"密码不能为空！"}]');
   		return false;
   	}
   	$("#loginBtn").val("登录中...");
   	$.ajax({
   		type:"post",
   		url:"/index.jhtml?method=login&userName="+encodeURI(userName)+"&paswd="+paswd+"&autoLogin="+autoLogin+"&loginType=ajax&refresh=1",
   		success:function(data){
   			loginResponse(data);
   		}
   	});
}

});



loginResponse = function(originalRequest){
	var result = originalRequest;
	if (result == "succ|0" || result == "succ|1"){
		if(result == "succ|1"){
			window.location.reload();
		}
	}else{
		if (result == 1){
			alert('抱歉！该用户不存在或密码有误，请重新输入。');
			//showErrors("[{'error':'抱歉！该用户不存在或密码有误，请重新输入。'}]");
		}else if (result == 2){
			alert('该帐号已被禁用，如有异议，请与短趣网客服联系！');
			//showErrors("[{'error':'该帐号已被禁用，如有异议，请与短趣网客服联系！'}]");
		}else{
			alert('系统繁忙！请稍候再试。');
			//showErrors("[{'error':'系统繁忙！请稍候再试。'}]");
		}
		$("#loginBtn").val("登录");
	}
}

alertLogin = function () {
	var sHeight, sWidth;
	var divHeigh = $("#alertLogin").height() / 2;
	var divWidth = $("#alertLogin").width() / 2;
	sHeight = document.documentElement.clientHeight / 2 - divHeigh
			+ document.documentElement.scrollTop;
	sWidth = document.documentElement.clientWidth / 2 - divWidth
			+ document.documentElement.scrollLeft;
	$("#alertLogin").show();
	$("#alertLogin").css({
				top : sHeight + 'px',
				left : sWidth + 'px'
			});
	$("#erbigBox").css({
				height : $(document.body).height()+ 'px',
				width : $(window).width()+ 'px'
			});

	$("#alertLoginClose").click(function() {
				$("#alertLogin").hide();
				$("#erbigBox").css({
							height : '0',
							width : '0'
						});
			});
}

toReg = function(){
	var strUrl = window.parent.location.href;
	strUrl = encodeURIComponent(strUrl);
	window.location.href="http://www.duanqu.com/reg.jhtml?method=reg&strUrl="+strUrl;
}




getMsgCount = function (){
	if ($("#bar_msg_count").length>0){
		$.ajax({
			type:'post',
			url:'/newIndex.jhtml?method=getMsgCount',
			success:function(data){
				$("#bar_msg_count").html(data);
			}
		});
	}
};
$(document).ready(function (){
	//调用
getMsgCount();
$('#login_password').bind('keyup',function(event) {
	//按回车键时，执行登录
   if (event.keyCode=="13"){
      channellogin();
   }
});
});

function pagelogin() {
	var userName = $("#userName").val();
   	var paswd = $("#paswd").val();
   	var remember = document.getElementById("remember");
   	var lastUrl = $("#lastUrl").val();
   	var remember1 = "";
   	
   	if (userName.length == 0 || userName == '请输入用户名'){
   		//alert("用户名不能为空！");
   		showErrors('[{error:"用户名不能为空！"}]');
   		return false;
   	}
   	if (paswd.length == 0){
   		//alert("密码不能为空！");
   		showErrors('[{error:"密码不能为空！"}]');
   		return false;
   	}
   	
   	if(remember.checked == true)
		remember1 = 1;
	else
		remember1 = 0;
	var url = "/login.jhtml";
	var pars = "method=mainlogin&userName="+encodeURI(userName)+"&paswd=" + paswd
			+ "&lastUrl=" + lastUrl + "&remember="+remember+"&isalert="+0;
   	$.post(url + '?' + pars, function(xml) {
		$(xml).find("user").each(function() {
			var succ = $(this).children("succ").text();
			var user = $(this).children("user").text();
			var isalert = $(this).children("isalert").text();
			if (succ == "true") {
				var lastUrl = $(this).children("lastUrl").text();
				window.location.href = lastUrl;
			}
			if(isalert == 0){
				if (succ == "true") {
					lastUrl = $(this).children("lastUrl").text();
					window.location.href = lastUrl;
				} else 	if (succ == "false") {
//					alert("抱歉！没有该用户,请重新输入。");
					showErrors('[{error:"抱歉！没有该用户,请重新输入。"}]');
					document.getElementById("paswd").value = "";
				}else if (succ == "1") {
//					alert("抱歉！密码有误,请重新输入。");
					showErrors('[{error:"抱歉！密码有误,请重新输入。"}]');
					document.getElementById("paswd").value = "";
				} else  if (succ == "2"){
//					alert("该帐号已被禁用，如有异议，请与短趣网客服联系！");
					showErrors('[{error:"该帐号已被禁用，如有异议，请与短趣网客服联系！"}]');
					document.getElementById("paswd").value = "";
				}else  if (succ == "-2"){
					//alert("该帐号还没激活，请先激活该帐号！");
					showErrors('[{error:"该帐号还没激活，请先激活该帐号！"}]');
					document.getElementById("paswd").value = "";
					closeDivForLogin('ShowDiv','bg');
				}
			}else {
				if (succ == "true") {
					window.location.reload(true);
				}
			}
		});
	});
}
