function $(item){
	return document.getElementById(item);
}
isUndef  = function(a){ return typeof a == "undefined";};
function getCookie(name){
    return (document.cookie.match(new RegExp("(^"+name+"| "+name+")=([^;]*)"))==null)?"":unescape(RegExp.$2);
}

function showtab(btnid,tabid,tabnumber)
{
	for (i = 1;i<=tabnumber;i++)
	{
		if($(tabid+"_btn"+i))$(tabid+"_btn"+i).className = "";
		if($(tabid+"_sub"+i))$(tabid+"_sub"+i).style.display = "none";
	}
	if($(tabid+"_btn"+btnid))$(tabid+"_btn"+btnid).className = "selected";
	if($(tabid+"_sub"+btnid))$(tabid+"_sub"+btnid).style.display = "block";
}

function setCookie(name,value,hours){
	//alert(arguments.length);
	if(arguments.length>2){
		var expireDate=new Date(new Date().getTime()+hours*3600000);
		document.cookie = name + "=" + escape(value) + "; path=/; domain=xunlei.com; expires=" + expireDate.toGMTString() ;
	}else
		document.cookie = name + "=" + escape(value) + "; path=/; domain=xunlei.com"; 
}

function onGetLogin()
{
	//if($('flashplay')){$('flashplay').style.display = "none";}
	Login.showInterface(refreshPage);
}

function logout()
{
	setCookie("sessionid","");	
	setCookie("usrname","");
	refreshPage();
}

function refreshPage()
{
	top.location.href=top.location.href;
}

//设置首页
function SetHome(obj,vrl){
        try{
                obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
        }
        catch(e){
                if(window.netscape) {
                        try {
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                        }
                        catch (e) {
                                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
                        }
                        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                        prefs.setCharPref('browser.startup.homepage',vrl);
                 }
        }
}


var  Login = {
	popDiv:null,
	loginRet:false,
	callBackFunc:function(){},
	showInterface: function(callBackFunc){
		if(Login.popDiv==null){
			Login.popDiv = document.createElement("DIV");
			Login.popDiv.id = "login_pop_id";
			Login.popDiv.innerHTML ='<div class="login_bg"><div class="login_c"><h6><span><a href="javascript:Login.exit();void(0);" class="a_ a_close"></a></span>迅雷用户登录</h6>'
				+'<div class="logindiv"><form id="loginform" action="http://login.xunlei.com/login/"  method="post" onsubmit="return Login.validateInput();">'
				+'<fieldset><input type="hidden" name="u1"><ul><li><label for="u">请输入您的帐号：</label>'
				+'<input id="u" name="u" type="text" value="" class="input_lo"  maxlength="50" /></li><li><label>请输入您的密码：</label>'
				+'<input name="p" type="password" maxlength="50"  class="input_lo"/></li><li><label>输入验证码：</label>'
				+'<input name="verifycode" type="text" maxlength="10"  class="input_lo yz" /><img id="verify_code"/>'
				+'<a href="#" onclick="$(\'verify_code\').src=\'http://verify.xunlei.com/image?cachetime=\'+new Date().getTime();return false;">换张图片</a></li>'
				+'<li class="ct"><input type="submit" class="button_comm" value="登录" /><input type="reset" class="button_comm" value="重置" /></li>'
				+'<li class="ct"><a href="http://i.xunlei.com/register/password_1.htm" target="_blank">忘记密码啦？</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;'
				+'<a href="http://i.xunlei.com/register/register.htm" target="_blank">快速注册</a></li></ul></fieldset></form>'
				+'<iframe name="login_submit_iframe" style="display:none" onload="Login.checkRet()"></iframe></div></div></div>';
			
		//Insertion.bottom(document.body, Login.popDiv);	
		document.body.appendChild(Login.popDiv);
		Login.popDiv.style.position = "absolute";
		
		Login.backDiv = document.createElement("DIV");
		Login.backDiv.style.backgroundColor = "Black";
		Login.backDiv.style.filter = "alpha(opacity=40)";
		Login.backDiv.style.MozOpacity="0.4";
		Login.backDiv.style.position = "absolute";
		Login.backDiv.style.left = "0px";
		Login.backDiv.style.top = "0px";

		document.body.appendChild(Login.backDiv);
		//Insertion.bottom(document.body,Login.backDiv);
		Login.backDiv.style.zIndex =1000;
		Login.popDiv.style.zIndex  = 1001;
		}
		else
		{
			Login.popDiv.style.display =Login.backDiv.style.display ="";
		}
		
		if(! isUndef(callBackFunc))
			Login.callBackFunc = callBackFunc;	
		if(window.attachEvent){
			window.attachEvent("onresize",this.setPos);
			window.attachEvent("onscroll",this.setPos);
		}else{
			window.addEventListener("onresize",this.setPos,false);
			window.addEventListener("onscroll",this.setPos,false);
		}
		//window.onscroll =window.onresize=this.setPos;
		this.setPos();
		$('verify_code').src = 'http://verify.xunlei.com/image?cachetime='+new Date().getTime();
		$('u').focus();	
	},
	validateInput: function(){
		if($('loginform').u.value.length == 0)
		{
			alert("请您输入用户名!")
			return false;		
		}
		if($('loginform').p.value.length == 0)
		{
			alert("请您输入密码!")
			return false;		
		}
		if($('loginform').verifycode.value.length == 0)
		{
			alert("请您输入验证码!")
			return false;		
		}	

		this.disablePopDiv(true);
		Login.loginRet = true;
		$('u1').value = document.location;
		return true;
	},
	setPos:function(){
		Login.popDiv.style.left = (document.documentElement.clientWidth/2 + document.documentElement.scrollLeft -207) +"px";
		Login.popDiv.style.top = (document.documentElement.clientHeight/2 + document.documentElement.scrollTop-142 + 43) +"px";
		Login.backDiv.style.width = Math.max(document.body.scrollWidth, document.documentElement.clientWidth) +"px";
		Login.backDiv.style.height = Math.max(document.body.scrollHeight, document.documentElement.clientHeight)+"px";
	},
	
	exit: function(){
		$('loginform').reset();		
		if($('flashplay'))
			$('flashplay').style.display = "block";
	
		Login.popDiv.style.display =Login.backDiv.style.display ="none";
		this.disablePopDiv(false);
		if(window.detachEvent){
			window.detachEvent("onresize",this.setPos);
			window.detachEvent("onscroll",this.setPos);
		}else{
			window.removeEventListener("onresize",this.setPos,false);
			window.removeEventListener("onscroll",this.setPos,false);
		}
	},
	
	disablePopDiv: function(tag){
	},
	
	checkRet: function(){
		if(Login.loginRet){
			var  verifyRet = getCookie("blogresult");
			//alert(verifyRet);
			if(verifyRet=="0"){
				alert("登陆成功");
				//LoginBanner.set();
				changeSelTab('block');			
				Login.exit();
				Login.callBackFunc();
				return;			
			}
			if(verifyRet=="1")
				alert("验证码错误");
			else if(verifyRet=="2")
				alert("密码错误");
			else
				alert("登陆失败");
			this.disablePopDiv(false);
			$('verify_code').src='http://verify.xunlei.com/image?cachetime='+new Date().getTime();
		}
	}	
}

function loadScript2(scriptstr){
	var ifr=document.createElement("script");
	ifr.src="http://mediapv.sandai.net/commstat/VZONE1?itemid=1&"+scriptstr+"&d="+new Date().getTime();
	ifr.attachEvent('onload',function (){});		
}