var AFTER_loginshow="";
$(document).ready(function(){
	$("#bodydivlist").load("/user/in_frame_mylist.jsp?r="+Math.random(),{},function(){$("#bodydiv41").load("/user/in_bottom2.jsp?r="+Math.random());});
});
//user login info
function init_login()
{
	$.get("/user/head_userinfo.jsp?r="+Math.random(),function(data){
	    try{
			$("#loginbar").html(data.trim());
		    loginflag=(document.getElementById("user_info_loginflag").getAttribute("info")=="true")?true:false;
		}catch(e){}

		try{
		    window.opener.document.getElementById("loginbar").innerHTML = data.trim();
			window.opener.loginflag=(window.opener.document.getElementById("user_info_loginflag").getAttribute("info")=="true")?true:false;
		}catch(e){ }

		try{ my_initNoreadMsgMn(); }catch(e){ }
		try{ window.opener.my_initNoreadMsgMn();}catch(e){}
        //2010-10-11 luogc
		if(typeof(loadWinRecmd)=="function") loadWinRecmd();
	});
	//callback aft login
	if(AFTER_loginshow !="")
	{
		var aaa = AFTER_loginshow;
		AFTER_loginshow="";
		eval(aaa);
	}
}

function login_show(back)
{
	if($('#xlg_rdimg_x').css("display")=="block")
	{
		xlogin_chImg();
	}
	if(typeof(back)!="undefined") 
	{
		AFTER_loginshow = back;
	}
	$("#login_shadow").css({opacity:"0.7",filter:'alpha(opacity=70)'});
	$("#login_shadow").height($(document).height()).show();
	$(document).scrollTop(0);
	$("#login_frame").css({top: 200}).show(300,function(){
	    //xlogin_chImg();
		$("#login_frame .login_title").css({cursor:"move"});
		//load show phonenumber
		try{
		    var lastLoginPh = LocalData.get("lastLoginPh123");
		 
			if(lastLoginPh!=null && lastLoginPh!="" && lastLoginPh.match(xlg_phreg)!=null){
                $("#xlg_ph").val(lastLoginPh);
				$i("xlg_pwd").focus();
			}
			else{
                $i("xlg_ph").focus();
			}

			var login_type=LocalData.get("login_type");
			if(typeof login_type =='undefined' || login_type==''){
                    login_type='3';
			}
			login_type_select(login_type);

		}catch(e){
			$i("xlg_ph").focus();
		}

		try{
			$("#login_frame").draggable({handle:".login_title"});
		}catch(e){}
	});
	
}
function login_hide()
{
	$("#login_shadow").hide();
	$("#login_frame").hide(300);
	xlogin_reset();
}

/***********  pwd tishi 2010-10-13 **************/
function login_pwdts_show1(flag)
{
	var ph = $i("xlg_ph").value.trim();
    $("#pwdtishi1").show();
	if(flag || ph=="" || !xlg_phreg.test(ph))
	{
		if(ph=="" || !xlg_phreg.test(ph))
		{
			$("#pwdtishi1tt").html("　没有密码可以使用随机短信登录。");
			$("#pwdtishi1cc").html("<div style='margin:3px auto;'>您的手机号：<input type='text' id='pwdtsInp1' size=12 onkeyup='login_pwdts_hkey(event,this)'/></div><a href='javascript:void(0)' style='display:block;width:179px;height:24px;line-height:24px;vertical-align:middle;text-align:center;background:url(/user/images1/login_but1.gif) no-repeat;' onclick='login_pwdts_send()'>立即免费获取随机短信</a>");
		}
		else
		{
			$("#pwdtishi1tt").html("　没有密码可以使用随机短信登录。");
			$("#pwdtishi1cc").html("<div style='margin:3px auto;'>您的手机号：<input type='text' id='pwdtsInp1' size=12 value='"+ph+"' onkeyup='$(\"#xlg_ph\").val(this.value)'/></div><a href='javascript:void(0)' style='display:block;width:179px;height:24px;line-height:24px;vertical-align:middle;text-align:center;background:url(/user/images1/login_but1.gif) no-repeat;' onclick='login_pwdts_send()'>立即免费获取随机短信</a>");
		}
	}
	else
	{
		$("#pwdtishi1tt").html("　对不起，您输入的密码不正确。");
		$("#pwdtishi1cc").html("手机号："+ph+"<br/>如果忘记密码，请使用随机短信登录吧。<a href='javascript:void(0)' style='display:block;width:179px;height:24px;line-height:24px;vertical-align:middle;text-align:center;background:url(/user/images1/login_but1.gif) no-repeat;' onclick='login_pwdts_send(\""+ph+"\")'>使用并立即免费获取随机短信</a>");
	}
}
function login_pwdts_hkey(e,obj){
    $("#xlg_ph").val(obj.value);
	var c = e.which ? e.which : e.keyCode; 
	if(c==13)
	{
		
		login_pwdts_send();
	}
}
function login_pwdts_grandsuc()
{
	if(pwdtsflag!="1") return;
    login_type_select(2);
	login_pwdts_hide1();
	login_pwdts_show2();
}
function login_pwdts_grandfail()
{
	//if(pwdtsflag!="1") return;
   // alert(123);
}
function login_pwdts_hide1()
{
	$("#pwdtishi1").hide(); 
}
var pwdtsflag = "0";
function login_pwdts_send(ph){
	if(ph)
	{
		xlogin_showRcodeFrm(1);
	}
	else{
		if($i("pwdtsInp1")) ph = $i("pwdtsInp1").value.trim();
		if(ph=="" || !xlg_phreg.test(ph))
		{
			alert("请输入正确的手机号");
		}
		else{
            xlogin_showRcodeFrm(1);
		}
	}
}
function login_pwdts_show2()
{
	$("#pwdtishi2").show();
	var ph = $i("xlg_ph").value.trim();
	$("#pwdtishi2tt").html("　随机码已经发送至您的手机。");
	$("#pwdtishi2cc").html("手机号："+ph+"<br/>请再次输入您手机收到的随机码。");
	
}
function login_pwdts_hide2()
{
	$("#pwdtishi2").hide(); 
}
/***********  pwd tishi 2010-10-13 end **************/

//login type change
function login_type_select(value)
{
	$("#pwdtishi1").hide();
	$("#pwdtishi2").hide();
	if(value=="1")
	{
		$i("login_type").setAttribute("info","1");
		$i("login_type").innerHTML = "密码登录";
		$i("login_type1").style.display="block";
		$i("login_type2").style.display="none";
		$i("login_getrdbtd").style.display="none";
		$i("xlg_pw_tishi").innerHTML = "<a href='javascript:login_pwdts_show1(true)'  style='color:#f60;font-weight:bold;'>没有密码</a>";
	}
	else
	{
		$i("login_type").setAttribute("info","3");
		$i("login_type").innerHTML = "随机密码";
		$i("login_type1").style.display="none";
		$i("login_type2").style.display="block";
		$i("login_getrdbtd").style.display="block";
		if($i("xlg_rand").value.trim()=="")
		{
			$i("xlg_rand").value="在此输入随机码";
			$i("xlg_rand").style.color='#999';
		}
	}
}
function login_hdlType(obj)
{
	//show items
	$("#login_typediv ul").slideDown(300);
	$("#login_typediv ul li").mouseover(function(e){
	     $(this).css({color:"#FF6600"}).mouseout(function(e){
		       $(this).css({color:"#000000"});
		 });
	});
}
function listen_bodyclick(e)
{
	// body listener
	var event = e || window.event;
	var obj = (event.target) ?  event.target : event.srcElement;

    if(obj.getAttribute("id")!="login_type")
	{
		$("#login_typediv ul").slideUp(100);
	}
}
function login_loadString(s){
    return "<table cellspacing='0' cellpadding='0' align='center' style='margin:auto;'><tr><td><img src='/user/images1/loading.gif'></td><td>"+s+"</td></tr></table></td></tr></table>";
}
//************************************
var xlg_phreg = /^1[358]\d{9}$/;
var xlg_pwreg = /\W/g;
var xlg_randreg = /^\w{6}$/;
var xlg_imgrandreg = /^\w{4}$/;
var xlogin_chRcodeFlag = false;
//get random code
function xlogin_getRand()
{
	xlg_tishi_reset();
	var ph = $i("xlg_ph").value.trim();
	if(ph=="" || !xlg_phreg.test(ph))
	{
        $("#xlg_ph_tishi").html("<span style='color:red'><strong>手机号</strong>格式不正确</span>");
		$("#xlg_ph").select();
		return;
	}
	else
	{
		xlogin_showRcodeFrm();
	}
}
//randcode frame luogc 2009-11-6
function xlogin_showRcodeFrm(pwdts)
{
	pwdtsflag = pwdts ? pwdts : "0";
	var doc = document.documentElement.scrollHeight >= document.body.scrollHeight ? document.documentElement : document.body ;
    if(navigator.userAgent.indexOf("Safari") !=-1) doc = document.body;

	xlogin_chRcodeFlag = false;
    $("#login_shadow").css({"z-index":101}).show();
	//$("#xlg_rc_frm").css({top:doc.scrollTop+doc.clientHeight/2}).show();
	$("#xlg_rc_frm").css({top:300}).show();
	$("#xlg_rd_tishi2").html("");
	$("#xlg_imgrand2").val("");
	xlogin_chImg2();
}
function xlogin_hideRcodeFrm()
{
	$("#login_shadow").css({"z-index":99}).show();
	$("#xlg_rc_frm").hide();
}
function xlogin_chkRcodeSend()
{
	if(xlogin_chRcodeFlag) return;
    
	var ph = $i("xlg_ph").value.trim();
	var imgrand2 = $i("xlg_imgrand2").value.trim();
    if(imgrand2=="")
	{
		$("#xlg_rd_tishi2").html("<span style='color:red'><strong>验证码</strong>不能为空</span>");
		$i("xlg_imgrand2").focus();
		return;
	}
	else if(!xlg_imgrandreg.test(imgrand2))
	{
		$("#xlg_rd_tishi2").html("<span style='color:red'><strong>验证码</strong>格式不正确</span>");
		$i("xlg_imgrand2").select();
		return;
	}
	else
	{
		xlogin_chRcodeFlag = true;
		AJLOG.G_mod("21.1");
		$("#xlg_rd_tishi2").html("<span style='color:green'>获取中...</span>");
		$.get("/user/xlogin_server.jsp?mt=2&ph="+ph+"&rd="+imgrand2+"&r="+Math.random(),back_xLogin_getRandom);
	}

}
function xlogin_chImg2()
{
	$i("xlg_rdimg2").src="/user/xlogin_createimg2.jsp?r"+Math.random(); 
}

//***************
function xlogin_chImg()
{
	$i("xlg_rdimg").src="/user/xlogin_createimg.jsp?r"+Math.random(); 
}
function xlogin_showImg()
{
	if($('#xlg_rdimg_x').css("display")=="none")
	{
		$('#xlg_rdimg_x').show();
		$i("xlg_rdimg").src="/user/xlogin_createimg.jsp?r"+Math.random(); 
	}
}
//login normal 
var xlogin_onflag = false;
function xlogin_check()
{
	if(xlogin_onflag) return;
	xlg_tishi_reset();
	var ph = $i("xlg_ph").value.trim();
	var type = $i("login_type").getAttribute("info");
	var imgrand = $i("xlg_imgrand").value.trim();

	if(ph=="")
	{
        $("#xlg_ph_tishi").html("<span style='color:red'><strong>手机号</strong>不能为空</span>");
		$("#xlg_ph").select();
		return;
	}
	else if(!xlg_phreg.test(ph))
	{
        $("#xlg_ph_tishi").html("<span style='color:red'><strong>手机号</strong>格式不正确</span>");
		$("#xlg_ph").select();
		return;
	}
	else if(imgrand=="")
	{
		$("#xlg_rd_tishi").html("<span style='color:red'><strong>验证码</strong>不能为空</span>");
		$i("xlg_imgrand").focus();
		return;
	}
	else if(!xlg_imgrandreg.test(imgrand))
	{
		$("#xlg_rd_tishi").html("<span style='color:red'><strong>验证码</strong>格式不正确</span>");
		$i("xlg_imgrand").select();
		return;
	}

	if(type=="1")
	{
		//normal login
		var pwd = $i("xlg_pwd").value.trim();
		if(pwd=="")
		{
			$("#xlg_pw_tishi").html("<span style='color:red'><strong>密码</strong>不能为空</span>");
			$("#xlg_pwd").focus();
			return;
		}
		else if(xlg_pwreg.test(pwd))
		{
			$("#xlg_pw_tishi").html("<span style='color:red'><strong>密码</strong>含有非法字符</span>");
			$("#xlg_pwd").select();
			return;
		}
		else
		{
			xlogin_onflag = true;
			$i("xlg_infos").innerHTML = login_loadString("登录中...");
			LocalData.set("login_type","1");
			window.setTimeout(function(){
			    xlogin_iframeAct("/user/xlogin_server.jsp?mt=1&r="+Math.random()+"&ph="+ph+"&rd="+imgrand+"&pw="+pwd);
			},2000);
		}
	}
	else
	{
		//random login
		var rand = $i("xlg_rand").value.trim();
		if(rand=="" || !xlg_randreg.test(rand))
		{
			$("#xlg_pw2_tishi").html("<span style='color:red'><strong>随机码</strong>格式不正确</span>");
			$("#xlg_rand").select();
			return;
		}
		else
		{
			xlogin_onflag = true;
			$i("xlg_infos").innerHTML = login_loadString("登录中...");
			LocalData.set("login_type","3");
			window.setTimeout(function(){
			    $.get("/user/xlogin_server.jsp?mt=3&ph="+ph+"&rd="+imgrand+"&r="+Math.random()+"&pw="+rand,back_xLogin_Rand);
			},2000);
		}
	}
}
function xlogin_iframeAct(url)
{
	 this.parseURL = function(url)
	{
		var obb = {};
        obb.action = url.substring(0,url.indexOf("?"));
		var pp = url.substring(url.indexOf("?")+1,url.length);
		var arr = pp.split("&");
		var tem = "";
		for(var i=0;i<arr.length;i++)
		{
			tem = arr[i].split("=");
			obb[tem[0]]=tem[1];
		}
		return obb;
	};
	this._init = function(url)
	{
		var obj = this.parseURL(url);
		$i("xlogin_form1").action=obj.action;
		$i("xlogin_form1").ph.value=obj.ph;
		$i("xlogin_form1").pw.value=obj.pw;
		$i("xlogin_form1").rd.value=obj.rd;
		$i("xlogin_form1").r.value=Math.random();
		$i("xlogin_form1").mt.value=obj.mt;
		$i("xlogin_form1").target="xlogin_iframe";
	    $i("xlogin_form1").submit();
	};
	return this._init(url);
}
//reset 
function xlogin_reset()
{
	login_type_select(1);
	$i("xlg_ph").value = "";
	$i("xlg_pwd").value = "";
	$i("xlg_rand").value = "";
	$i("xlg_imgrand").value = "";
	$i("xlg_infos").innerHTML = "";
}

//logout
function xlogin_logout(cb)
{
	if(typeof cb == 'undefined')
		cb = "";
	window.location.href="/user/xlogin_server.jsp?mt=4&cb="+cb+"&r="+Math.random();
	
}
function xlogin_logout2()
{
	$.get("/user/xlogin_server.jsp?mt=4&r="+Math.random(),function(data){
		loginflag=false;
	    init_login();
        loginHandler(2);
	});
}
//************************************
//returns*************
function back_xLogin(data)
{
	$i("xlg_imgrand").value="";
	xlogin_onflag  = false;
	
	var ph = $i("xlg_ph").value.trim();
	xlogin_chImg();
	xlg_tishi_reset();
	eval("var obj = "+data);
	if(obj.code=="0")
	{
 
		LocalData.set("lastLoginPh123",ph);

		$i("xlg_infos").innerHTML = xlg_Icon(0,"<span style='color:green'>登录成功</span>");
		AJLOG.G_login(ph,0,0,'');
		window.setTimeout(function(){
			login_hide();
			loginHandler(1);
			if($i("ms_login"))
			{
				if(typeof AFTER_loginshow !='undefined'&&AFTER_loginshow!="")
				{
					eval(AFTER_loginshow);
					AFTER_loginshow = "";
				}else
				{
					MS_h.uinfo();
				}
			}
			else
				init_login();
		},1000);
	}
	else
	{
		 AJLOG.G_login(ph,0,1,obj.msg);
        if(obj.code=="4") $("#xlg_rd_tishi").html("<span style='color:red'><strong>验证码</strong>不正确</span>");
        else $("#xlg_infos").html(xlg_Icon(1,"<span style='color:red'>"+obj.msg+"</span>"));

		if(obj.msg.indexOf("密码")!=-1) login_pwdts_show1();
	}
}
function back_xLogin_getRandom(data)
{
	xlogin_chRcodeFlag = false;
	xlogin_chImg2();
	$('#xlg_rd_tishi2').html('');
	eval("var obj = "+data);
	if(obj.code=="0")
	{
		 $("#xlg_rd_tishi2").html("<span style='color:green'>获取成功</span>");
		 window.setTimeout(function(){
			 xlogin_hideRcodeFrm();
			 login_pwdts_grandsuc();
		 },1000);
		  
	}
	else
	{
		switch(obj.code)
		{
			case "0":$("#xlg_rd_tishi2").html("<span style='color:green'>获取成功</span>");break;
			case "6":$("#xlg_rd_tishi2").html("<span style='color:red'><strong>验证码</strong>不正确</span>");break;
			default: $("#xlg_rd_tishi2").html(xlg_Icon(1,"<span style='color:red'>"+obj.msg+"</span>")); break;
		}
		login_pwdts_grandfail(); 
	}
}
function back_xLogin_Rand(data)
{
	$i("xlg_imgrand").value="";
	xlogin_onflag  = false;
	
	xlogin_chImg();
	var ph = $i("xlg_ph").value.trim();
	xlg_tishi_reset();
	eval("var obj = "+data);
	if(obj.code=="0")
	{
		LocalData.set("lastLoginPh123",ph);
		AJLOG.G_login(ph,1,0,"");
		$i("xlg_infos").innerHTML = xlg_Icon(0,"<span style='color:green'>登录成功</span>");
		window.setTimeout(function(){
			login_hide();
			loginHandler(1);
			init_login();
		},1000);
	}
	else
	{
		AJLOG.G_login(ph,1,1,obj.msg);
		switch(obj.code)
		{
			case "0":$("#xlg_pw2_tishi").html("<span style='color:green'>登录成功</span>");break;
			case "1":case "2":$("#xlg_pw2_tishi").html("<span style='color:red'><strong>随机码</strong>不正确</span>");break;
			case "6":$("#xlg_rd_tishi").html("<span style='color:red'><strong>验证码</strong>不正确</span>");break;
			default: $("#xlg_infos").html(xlg_Icon(1,"<span style='color:red'>"+obj.msg+"</span>")); break;
		}
	}
}
//监听登录，退出登录
//flag 1:登录成功  2：退出
function loginHandler(flag)
{
	 
	//用户收藏铃音
	try{
		if($i("plbox_info")) PLBOX.getInfo();
	}catch(e){}
	try{  if(window.opener.document.getElementById("plbox_info"))  window.opener.PLBOX.getInfo(); }catch(e){}

	if(flag=="1")
	{
		loginflag=true;
		//推荐服务
		try{
			//$("#ondoFrm").hide();
		    //$("#randFrm").hide();
		   // $("#recomFrm").show();
			//init_recom1();
			
			if($("#diydiv").length>0){
			    $.get("diymonth_url.jsp",{r:Math.random()},function(data){
				  $("#diydiv").html(data.trim());
				});
			}

			$("#bodydivlist").load("/user/in_frame_mylist.jsp?r="+Math.random(),{},function(){$("#bodydiv41").load("/user/in_bottom2.jsp?r="+Math.random());});
			
		}catch(e){ }

		try{
			window.opener.loginflag=true;
			//$(window.opener.document.getElementById("randFrm")).hide();
			//$(window.opener.document.getElementById("ondoFrm")).hide();
			//$(window.opener.document.getElementById("recomFrm")).show();
			//window.opener.init_recom1();
		}catch(e){}
	}
	else
	{
		loginflag=false;

		//随便听听切换
	    try{  
			$("#ondoFrm").show();
			//$("#randFrm").show();
		    $("#recomFrm").hide();
			$("#bodydivlist").hide();
			//init_random(); 
		}catch(e){}
		
		//从我的12530退出
		try{
		    if((window.location+"").indexOf("/user/my_index")!=-1)
				window.location = "/user/";
		}catch(e){}
	}
}
function xlg_Icon(iconflag, str)
{
	var img = "";
	switch(iconflag)
	{
	    case 0:img = "icon_ok3.gif";break;
		case 1:img = "icon_warning3.gif";break;
		case 2:img = "icon_stop3.gif";break;
	}
	return "<table cellspacing='0' cellpadding='0' width='100%'><tr><td align='center'><table cellspacing='0' cellpadding='0'><tr><td><img src='/user/images1/"
	+img+"' />&nbsp;</td><td align='left'>"+str+"</td></tr></table></td></tr></table>";
}
function xlg_handle13(e, fn)
{
	e = e || window.event;
	var c = e.which ? e.which : e.keyCode;
	if(c==13) fn(e);
}
function xlg_tishi_reset()
{
	$("#xlg_ph_tishi").html("");
	$("#xlg_pw_tishi").html("<a href='javascript:login_pwdts_show1(true)'  style='color:#f60;font-weight:bold;'>没有密码</a>");
	$("#xlg_pw2_tishi").html("");
	$("#xlg_rd_tishi").html("");
	$('#xlg_rd_tishi2').html('');
	$("#xlg_infos").html("");
}
//********************
$(function(){
   $(document.body).click(function(e){
       listen_bodyclick(e);
   });
});
