function test_weibo(){	
	document.getElementById('weibo_v').className="show ok";
	document.getElementById('show_v').className="";
	document.getElementById('l_a').style.display="none"; 
	document.getElementById('l_b').style.display=""; 
	}

function test_weibo_s(){	
	document.getElementById('weibo_v').className="";
	document.getElementById('show_v').className="show ok";
	document.getElementById('l_a').style.display=""; 
	document.getElementById('l_b').style.display="none"; 
	}

	var start = 30;
    var step = -1;
		
function count()
{
	document.getElementById("m_rr").innerHTML =start+'秒后重新发送';
	start += step;
	if(start < 0){
		document.getElementById("m_rr").style.display="none";
		document.getElementById("login_showboxphonecodebtn_loginbtn").style.display='';
		document.getElementById("login_showboxphonecodebtn_loginbtn").innerHTML ="重新获取";
		return false;
				}
	setTimeout("count()",1000);
}
function count_weibo()
{	
	document.getElementById("m_rr_weibo").innerHTML =start+'秒后重新发送';
	start += step;
	if(start < 0){
		document.getElementById("m_rr_weibo").style.display="none";
		document.getElementById("login_showboxphonecodebtn_loginbtn_weibo").style.display='';
		document.getElementById("login_showboxphonecodebtn_loginbtn_weibo").innerHTML ="重新获取";
		return false;
				}
	setTimeout("count_weibo()",1000);
}

(function(){
	//class
	function login(){
		this.opt={
			//可配置属性
			login_type:1,
			login_url:'',
			logout_url:'',
			vcode_url:'',
			register_url:'http://reg.show.sina.com.cn',
			findpw_url:'http://ret.show.sina.com.cn',
			loginbtnid:'loginbtn',
			logoutbtnid:'',
			isvcode:false,                     //是否需要增写验证码
			isreload:false,                    //是否登录成功后重载页面
			islogin:false,                     //是否是已登录状态
			istipahead:false,                  //是否打开用户名输入提示功能
			emailsuffix:['','@sina.cn','@sina.com','@vip.sina.com','@163.com','@qq.com','@126.com','@hotmail.com','@gmail.com','@sohu.com','@yahoo.cn'],//邮箱后缀
			closecallback:null,
			logincallback:null,
			//有默认值计算属性
			tipaheadnum:0,                     //当前选中的提示项索引值

			wrongtime:0                        //用户名密码输入错误的次数

			//动态声明全局变量
			//logoutbtnid:'',                  //退出按钮id
			//showboxid:'',                    //包含弹框和半透明背景id
			//showboxbgid:'',                  //半透明背景id
			//showboxpopid:'',                 //弹框id
			//showboxnameinputid:'',           //用户名文本框id
			//showboxpasswordinputid:'',       //用户密码方本框id
			//showboxvcodeinputid:'',          //验证码文本框id
			//showboxvcodeboxid:'',            //验证码整栏栏id
			//showboxsbmbtnid:'',              //提交按钮id
			//showboxserrormsgid:'',           //错误信息id
			//showboxremembernameid:'',        //记住登录名多选框id
			//showboxtipaheadid:'',            //用户名提示框id
		};
	}
	login.prototype={
		//初始化
		init:function(opt){
			for(var k in opt){
				this.opt[k]=opt[k];
			}
			if(this.opt.islogin){//为退出按钮绑定事件
				this.addevent_logout();
			}else{//为登录按钮绑定事件
				this.addevent_login();
			}

		},
		//为登录按钮绑定事件
		addevent_login:function(self){
			if(typeof self =='undefined'){
				self=this;
			}
			if(self.opt.loginbtnid!=''){
				try{
					var loginbtn=document.getElementById(self.opt.loginbtnid);
					loginbtn.onclick=function(){
						self.showbox(self);
					}	
				}catch(e){
					return;
				};
			}
		},
		//为退出按钮绑定事件
		addevent_logout:function(){
			var self=this;
			if(self.opt.logoutbtnid!=''){
				document.getElementById(self.opt.logoutbtnid).onclick=function(){self.logout(self)}
			}
		},
		//显示弹框
		showbox:function(self){
						var bW=document.body.scrollWidth;
var bH=document.body.scrollHeight;
var oH=document.documentElement.clientHeight||document.body.clientHeight; //可见区域高度
var sT=document.documentElement.scrollTop||document.body.scrollTop;  //滚动条高度
var top=sT+(oH-217)/2;
			//document.getElementsByTagName('html')[0].style.overflow='hidden';
			//是否显示验证码功能

			var isvcodedisplay;
			if(self.opt.isvcode){
				isvcodedisplay='';
			}else{
				isvcodedisplay='none';
			}
				var isphonecodedisplay;
			if(self.opt.isphonecode){
				isphonecodedisplay='';
			}else{
				isphonecodedisplay='none';
			}
		var isphonecodedisplay_weibo;
			if(self.opt.isphonecode_weibo){
				isphonecodedisplay_weibo='';
			}else{
				isphonecodedisplay_weibo='none';
			}
				//显示cookie中记住的用户名
			var remembername=self.getcookie('loginname');
			var rememberchecked='checked="checked"';
			if(typeof remembername =='undefined'){
				remembername='请输入新浪SHOW帐号';
				rememberchecked='';
			}
			//将生成的元素ID名全局化，规则为login_showbox+登录按钮id名称,登录按钮ID为程序中可确定的唯一字识别
			var box=document.createElement('div');
			self.opt.showboxid='login_showbox_'+self.opt.loginbtnid;
			self.opt.showboxbgid='login_showboxbg_'+self.opt.loginbtnid;
			self.opt.showboxpopid='login_showboxpop_'+self.opt.loginbtnid;
			self.opt.showboxclosebtnid='login_showboxclosebtn_'+self.opt.loginbtnid;
			
			self.opt.showboxnameinputid='login_showboxnameinput_'+self.opt.loginbtnid;
			self.opt.showboxpasswordinputid='login_showboxpasswordinput_'+self.opt.loginbtnid;
			self.opt.showboxvcodeinputid='login_showboxvcodeinput_'+self.opt.loginbtnid;
			self.opt.showboxsbmbtnid='login_showboxsbmbtn_'+self.opt.loginbtnid;
			
			self.opt.showboxserrormsgid='login_showboxserrormsg_'+self.opt.loginbtnid;
			self.opt.showboxvcodeboxid='login_showboxvcodebox_'+self.opt.loginbtnid;
			self.opt.showboxvcodeimgid='login_showboxvcodeimg_'+self.opt.loginbtnid;
			self.opt.showboxvcodebtnid='login_showboxvcodebtn_'+self.opt.loginbtnid;
			self.opt.showboxremembernameid='login_showboxremembername_'+self.opt.loginbtnid;
			self.opt.showboxtipaheadid='login_showboxtipahead_'+self.opt.loginbtnid;
		self.opt.showboxphonecodeboxid='login_showboxphonecodebox_'+self.opt.loginbtnid;
			self.opt.showboxphonecodeinputid='login_showboxphonecodeinput_'+self.opt.loginbtnid;
			self.opt.showboxphonecodebtnid='login_showboxphonecodebtn_'+self.opt.loginbtnid;
			self.opt.showboxphonecodeboxid_weibo='login_showboxphonecodebox_'+self.opt.loginbtnid+'_weibo';
			self.opt.showboxphonecodeinputid_weibo='login_showboxphonecodeinput_'+self.opt.loginbtnid+'_weibo';
			self.opt.showboxphonecodebtnid_weibo='login_showboxphonecodebtn_'+self.opt.loginbtnid+'_weibo';
			box.id=self.opt.showboxid;
			//生成HTML
			var h='';
			h+='<div class="bg" id="'+self.opt.showboxbgid+'" style="width:'+bW+'px;height:'+bH+'px"></div>';
			h+='<div class="login" id="'+self.opt.showboxpopid+'"  style="top:'+top+'px;margin-top:0px;">';
			h+='<div class="tit">\u7528\u6237\u767b\u5f55<a id="'+self.opt.showboxclosebtnid+'" href="#" class="close"></a></h2></div>';//用户登陆
			h+='<div class="box"><ul><li class="show ok" onclick="test_weibo_s();" id="show_v"><img src="http://passport.show.sina.com.cn/images/login/icon_logoshow.gif" width="16" height="16" title="新浪SHOW帐号登录" />新浪SHOW帐号登录</li>';
			h+='<li class="" onclick="test_weibo();" id="weibo_v"><img src="http://passport.show.sina.com.cn/images/login/icon_logoweibo.gif" width="16" height="16" title="新浪微博帐号登录" />新浪微博帐号登录</li></ul>';
			h+='<div id="l_a">';
			h+='<div class="main_weibo">';
			h+='<p><span>帐号：</span><input id="'+self.opt.showboxnameinputid+'"  type="text" class="intxt" value="'+remembername+'">';
			h+='<input id="'+self.opt.showboxremembernameid+'" type="checkbox" class="inckb" tabindex="-4" '+rememberchecked+'><label for="incbk"> 记住我</label></p>';
			h+='<p><span>密码：</span><input type="password" class="intxt" id="'+self.opt.showboxpasswordinputid+'"><a href="'+self.opt.findpw_url+'">忘记密码？</a></p>';
			h+='<p id="'+self.opt.showboxvcodeboxid+'" style="display:'+isvcodedisplay+'"><span>验证码：</span><input type="text" class="inpass" id="'+self.opt.showboxvcodeinputid+'">';
			h+='<img id="'+self.opt.showboxvcodeimgid+'" src="" width="53" height="22" /><a href="#" id="'+self.opt.showboxvcodebtnid+'">换一张</a></p>';
			h+='<p id="'+self.opt.showboxphonecodeboxid+'" style="display:'+isphonecodedisplay+'">';
			h+='<span>验证码：</span>';//手机验证码
			h+='<input id="'+self.opt.showboxphonecodeinputid+'" class="inpass"" type="text" >';
			h+='<a id="'+self.opt.showboxphonecodebtnid+'" tabindex="-7" href="#">获取手机验证码</a><a id="m_rr" style="display:none"></a>';//获取手机验证码
			h+='</p>';
			h+='<p class="tips" id="'+self.opt.showboxserrormsgid+'"></p>';
			h+='<p class="btn"><input type="button" value="登 录" id="'+self.opt.showboxsbmbtnid+'"></p>';
			h+=' </div><p class="reg">没有新浪SHOW帐号？<a href="'+self.opt.register_url+'">立即注册</a></p>';
			h+='	</div>';
			
			//构造weibo登陆框开始
			h+='<div id="l_b" style="display:none">';
			h+='<div class="main_weibo">';
            h+='<p><span>帐号：</span><input name="username" type="text" class="intxt" id="weibo_username" value="请输入新浪微博帐号"></p>';
	        h+='<p><span>密码：</span>';
            h+='<input name="password" type="password" class="intxt" id="weibo_password"></p>';
			h+='<p id="'+self.opt.showboxphonecodeboxid_weibo+'" style="display:'+isphonecodedisplay_weibo+'">';
			h+='<span>验证码：</span>';//手机验证码
			h+='<input id="'+self.opt.showboxphonecodeinputid_weibo+'" class="inpass"" type="text" >';
			h+='<a id="'+self.opt.showboxphonecodebtnid_weibo+'" tabindex="-7" href="#">获取手机验证码</a><a id="m_rr_weibo" style="display:none"></a>';//获取手机验证码
			h+='</p>';
			h+='<p class="tips" id="weibo_error"></p>';
			h+='<p class="btn"><input type="button" value="登 录" id="weibo_submit"></p>';			
           	h+='</div>';
			//构造weibo登陆框结束
			
			h+='	</div>';
			h+='</div>';
			box.innerHTML=h;
			document.body.appendChild(box);

			//绑定提交事件
			document.getElementById(self.opt.showboxsbmbtnid).onclick=function(){self.sbm(self)}
			document.getElementById(self.opt.showboxnameinputid).onclick=function(){self.show_username(self)}
			document.getElementById(self.opt.showboxphonecodeinputid).onclick=function(){self.show_showboxphonecodeinputid(self)}
			document.getElementById(self.opt.showboxphonecodeinputid_weibo).onclick=function(){self.show_showboxphonecodeinputid_weibo(self)}
            document.getElementById("weibo_username").onclick=function(){self.weibo_username(self)}
			//绑定weibo登陆提交事件
			document.getElementById('weibo_submit').onclick=function(){self.weibo_sbm(self)}
			//document.getElementById('weibo_submit').onkeyup=function(){self.weibo_sbm(self)}
			//绑定关闭事件
			document.getElementById(self.opt.showboxclosebtnid).onclick=function(){
				self.hidebox(self)
				if(typeof self.opt.closecallback=='function'){
					self.opt.closecallback();
				}	
			}
			//绑定更换验证码事件
			document.getElementById(self.opt.showboxvcodebtnid).onclick=function(){self.resetvcode(self)}
			//登陆名文本框绑定提示功能
			var showboxnameinput=document.getElementById(self.opt.showboxnameinputid);
			if(self.opt.istipahead){
				//showboxnameinput.onfocus=function(){self.tipahead(self)}
				showboxnameinput.onkeyup=tipahead
				showboxnameinput.onclick=tipaheadclick	
			}
			//用户名自动获得焦点
			setIndexLastWord(self.opt.showboxnameinputid);
			self.hidetipahead(self);//弹出框后用户名自动获得焦点，会弹出tipahead，这里自动隐藏
			//在弹框半透明背景上绑定回车提交事件
			document.getElementById('l_a').onkeyup=keysbm;

				document.getElementById("l_b").onkeyup=keysbm_weibo;
			
			//如果显示验证码，则自动生成一张验证码图片
			if(self.opt.isvcode){
				self.resetvcode(self);
			}
			//回车提交事件函数
			function keysbm(){
				var evt = arguments[0]|| window.event;
				self.keysbm(self,evt);
			}
			
			//回车提交事件函数
			function keysbm_weibo(){
				var evt = arguments[0]|| window.event;
				self.keysbm_weibo(self,evt);
			}
			
			//闭包用于型成evt和self
			function tipahead(){
				var evt = arguments[0]|| window.event;
				self.tipahead(self,evt);
			}
			//点击文本框阻止事件冒泡
			function tipaheadclick(){
				var evt = arguments[0]|| window.event;
				if(evt.stopPropagation){
					evt.stopPropagation()
				}else{
					evt.cancelBubble  = true;
				}
			}
			
			window.onresize=function(){
				var bW=document.body.scrollWidth;
				var bH=document.body.scrollHeight;
				var oH=document.documentElement.clientHeight||document.body.clientHeight; //可见区域高度
				var sT=document.documentElement.scrollTop||document.body.scrollTop;  //滚动条高度
				var top=sT+(oH-217)/2;
				document.getElementById(self.opt.showboxbgid).style.width=bW+"px";
				document.getElementById(self.opt.showboxbgid).style.height=bH+"px";
			}
		},
		//删除弹框
		hidebox:function(self){
			document.getElementsByTagName('html')[0].style.overflow='auto';
			document.body.removeChild(document.getElementById(self.opt.showboxid));
			//弹框已删除，解除调整弹框位置和事件绑定
			window.onresize=function(){return};
			window.onscroll=function(){return};
		},
		//回车提交事件函数
		keysbm:function(self,evt){
			var currentKey = evt.charCode||evt.keyCode;
			if(currentKey=='13'){//回车
				self.sbm(self);
			}
		},
		
		
		//回车提交事件函数
		keysbm_weibo:function(self,evt){
			var currentKey = evt.charCode||evt.keyCode;
			if(currentKey=='13'){//回车
				self.weibo_sbm(self);
			}
		},
		
		//登陆名提示功能
		tipahead:function(self,evt){
			//用户存储过滤后的邮箱后缀名序列
			var a=[];
			
			var tipaheadbox=document.getElementById(self.opt.showboxtipaheadid);
			var nameinput=document.getElementById(self.opt.showboxnameinputid);
			
			var name=nameinput.value;
			//截取@之前的字符串
			var name2='';
			//截取@之后的字符串
			var namelast='';
			//输入框中@字符的位置
			var n=name.indexOf('@');
			//a.length
			var l;
			//赋值变量
			if(n>=0){//已经输入@
				name2=name.substr(0,n);
				namelast=name.substr(n);
				a.length=0;
				//通过@后的字符串，生成仍然需要匹配的邮箱后缀名数组a
				a.push('');
				for(var i=0;i<self.opt.emailsuffix.length;i++){
					if(self.opt.emailsuffix[i].indexOf(namelast)>=0){
						a.push(self.opt.emailsuffix[i]);
					}
				}
			}else{//还没有输入@
				name2=name;
				a=self.opt.emailsuffix;
			}
			l=a.length;
			
			//用户名文本框在获得焦点和按下键盘都会调用这个函数，通过参数中的evt是否存在来区别
			
			//onkeyup调用上下箭头和回车键功能
			if(typeof evt!='undefined'){
				var currentKey = evt.charCode||evt.keyCode;
				if(currentKey=='40'){//下
					if(self.opt.tipaheadnum<l-1){
						self.opt.tipaheadnum++;
					}else{
						self.opt.tipaheadnum=0;
					}
				}else if(currentKey=='38'){//上
					if(self.opt.tipaheadnum>0){
						self.opt.tipaheadnum--;
					}else{
						self.opt.tipaheadnum=l-1;
					}
				}else if(currentKey=='13'){//回车
					//将当前提示列表中选中的值赋值给用户名文本框
					try{
						nameinput.value=tipaheadbox.getElementsByTagName('li')[self.opt.tipaheadnum+1].innerHTML;
					}catch(e){
						
					}
					self.hidetipahead(self);
					
					return;
				}else{
					self.opt.tipaheadnum=0;
				}
			}
			
			//生成提示列表
			if(name==''){
				self.hidetipahead(self);
			}else{
				var h='<ul>';
				h+='<li>\u8bf7\u9009\u62e9\u90ae\u7bb1\u7c7b\u578b</li>';//请选择邮箱类型
				for(var i=0;i<l;i++){
					//第一行（i==0）显示全部输入内容，其它显示@之前部份和相应的邮箱后缀拼接成的字符串
					var thisname;
					if(i==0){
						thisname=name;
					}else{
						thisname=name2;
					}
					if(i==self.opt.tipaheadnum){
						h+='<li class="selected">'+thisname+a[i]+'</li>';
					}else{
						h+='<li>'+thisname+a[i]+'</li>';
					}	
				}
				h+='</ul>';
				tipaheadbox.innerHTML=h;
				tipaheadbox.style.display='';
				
				//为除第一行提示外的其它提示绑定事件，因为第一行是说明内容
				var lis=tipaheadbox.getElementsByTagName('li');
				for(var i=0;i<lis.length;i++){
					if(i!=0){
						lis[i].onclick=function(){
							nameinput.value=this.innerHTML;
						}
						lis[i].onmouseover=function(){
							for(var j=0;j<lis.length;j++ ){
								lis[j].className='';
							}
							this.className='selected';
						}
						lis[i].onmouseout=function(){
							this.className='';
						}
					}
				}
				//为整个半透明背景和全部弹框的外包绑定人个点击隐藏提示框功能
				document.getElementById(self.opt.showboxid).onclick=function(){self.hidetipahead(self);}
				
				//鼠标离开提示框时，当前提示项回到第一个（其实是第二个因为第一个是介绍：）
				tipaheadbox.onmouseout=function(){
					self.opt.tipaheadnum=0;
					lis[1].className='selected';
				}
			}
		},
		
		//隐藏提示框
		hidetipahead:function(self){
			self.opt.tipaheadnum=0;
			try{
				var tipaheadbox=document.getElementById(self.opt.showboxtipaheadid);
				tipaheadbox.style.display='none';
				tipaheadbox.innerHTML='';	
			}catch(e){
			}
		},
		
		//更新验证码
		resetvcode:function(self){
			var time=new Date().getTime()
							var scriptSource='http://passport.show.sina.com.cn/vcode_s.php?'+time;
							excuteScript("scriptCode", scriptSource, "utf-8");
							//打开验证码步骤
								//self.opt.isvcode=true;
								//显示验证码栏
								document.getElementById(self.opt.showboxvcodeboxid).style.display='';
								var time=new Date().getTime();
								document.getElementById("login_showboxvcodeimg_loginbtn").setAttribute('src','http://passport.show.sina.com.cn/vcode_s.php?t='+time);
		},
		show_username:function(self){
				var shownameinput=document.getElementById(self.opt.showboxnameinputid);
				if (shownameinput.value=="请输入新浪SHOW帐号"){
				shownameinput.value="";
				}
			},
			show_showboxphonecodeinputid:function(self){
					//sbmbtn.disabled=false;
				var shownameinput=document.getElementById(self.opt.showboxphonecodeinputid);
				shownameinput.value="";
			},
			show_showboxphonecodeinputid_weibo:function(self){
				var sbmbtn=document.getElementById(self.opt.showboxsbmbtnid);
					document.getElementById("weibo_submit").disabled=false;
				var shownameinput=document.getElementById(self.opt.showboxphonecodeinputid_weibo);
				shownameinput.value="";
							},
		weibo_username:function(self){
				var shownameinput=document.getElementById("weibo_username");
				if (shownameinput.value=="请输入新浪微博帐号"){
				shownameinput.value="";
				}
			},
		
		//提交
		sbm:function(self){
			//alert('0');
			var reg=/(^\s*)|(\s*$)/g;
			var nameinput=document.getElementById(self.opt.showboxnameinputid);
			var name=nameinput.value;
			name=name.replace(reg, "");
			var passwordinput=document.getElementById(self.opt.showboxpasswordinputid);
			var password=passwordinput.value;
			var sbmbtn=document.getElementById(self.opt.showboxsbmbtnid);
			var vcode=document.getElementById(self.opt.showboxvcodeinputid).value;
			
			if(name==''){
				nameinput.focus();
				self.showerrormsg('\u8bf7\u8f93\u5165\u60a8\u7684\u7528\u6237\u540d',self);//请输入您的用户名
				return;
			}
			
			if(password==''){
				passwordinput.focus();
				self.showerrormsg('\u8bf7\u8f93\u5165\u60a8\u7684\u5bc6\u7801',self);//请输入您的密码
				return;
			}
			//自定义登陆方式
			if(self.opt.login_type==1){
				if(self.opt.isvcode){
					var vcodeinput=document.getElementById(self.opt.showboxvcodeinputid);
					var vcode=vcodeinput.value;
					vcode=vcode.replace(reg, "");
					if(vcode==''){
						vcodeinput.focus();
						self.showerrormsg('\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801',self);//请输入验证码
						return;
					}
				}
				
				var logindata;//ajax提交参数
				if(self.opt.isvcode){//有验证码
					logindata='name='+encodeURIComponent(name)+'&password='+encodeURIComponent(password)+'&vcode='+vcode;
				}else{
					logindata='name='+encodeURIComponent(name)+'&password='+encodeURIComponent(password);
				}
				//禁用登陆按钮
				sbmbtn.disabled=true;
				$.ajax({
					type:'post',
					url:self.opt.login_url,
					data:logindata,
					success:function(d){
						//d='{code:1,data:{name:"howlking"}}';
						eval('var t='+d);
						if(t.code==1){//通过
							//重载页面
							if(self.opt.isreload){
								//alert(1);
								window.location.reload();
							}
							//记住登录名
							var isremembername=document.getElementById(self.opt.showboxremembernameid).checked;
							if(isremembername){
								self.setcookie('loginname',name);
							}else{
								self.setcookie('loginname','');
							}
							//退出按钮ID
							if(self.opt.loginbtnid!=''){
								if(!self.opt.logoutbtnid){
									self.opt.logoutbtnid='login_logoutbtn_'+self.opt.loginbtnid;
								}
								var h='<span>\u6b22\u8fce，'+t.data.name+'</span><a id="'+self.opt.logoutbtnid+'" href="####" >\u9000\u51fa</a>';//欢迎//退出
								document.getElementById(self.opt.loginbtnid).parentNode.innerHTML=h;
								//为退出按钮绑定事件
								document.getElementById(self.opt.logoutbtnid).onclick=function(){self.logout(self)}
							}
							
							//删除登录框
							self.hidebox(self);
							//登录成功回调函数
							if(typeof self.opt.logincallback=='function'){
								self.opt.logincallback();
							}
						}else if(t.code==2){//用户名或密码错误
							self.showerrormsg('\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef',self);//用户名或密码错误
							//登录按钮解除禁用
							sbmbtn.disabled=false;
							//记录输入错误的次数
							self.opt.wrongtime++;
							if(self.opt.wrongtime>=3){
								//成功验证码
								//self.resetvcode(self);
								//打开验证码步骤
								//self.opt.isvcode=true;
								//显示验证码栏
								//alert(2);
//								document.getElementById(self.opt.showboxvcodeboxid).style.display='';
//								
//								var time=new Date().getTime();
//								document.getElementById("login_showboxvcodeimg_loginbtn").setAttribute('src','http://passport.show.sina.com.cn/vcode_s.php?t='+time);
								//alert(1);
							}
						}else if(t.code==3){//验证码错误
							self.resetvcode(self);
							self.showerrormsg('\u9a8c\u8bc1\u7801\u9519\u8bef',self);//验证码错误
							sbmbtn.disabled=false;
						}else if(t.code==4){//恶意登录
							self.resetvcode(self);
							self.showerrormsg('\u767b\u5f55\u8fc7\u4e8e\u9891\u7e41\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01',self);//登录过于频繁，请稍后再试！
							sbmbtn.disabled=false;
						}else if(t.code==5){//恶意登录
							self.resetvcode(self);
							self.showerrormsg('\u7a0b\u5e8f\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5',self);//程序异常，请稍后再试
							sbmbtn.disabled=false;
						}
					}
				})
			
			
			

			
			
			
			//新浪统一登录方式
			}else if(self.opt.login_type==2){
						if(self.opt.isvcode){
					var vcodeinput=document.getElementById(self.opt.showboxvcodeinputid);
					var vcode=vcodeinput.value;
					vcode=vcode.replace(reg, "");
					if(vcode==''){
						vcodeinput.focus();
						self.showerrormsg('\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801',self);//请输入验证码
						return;
					}
				}
					sinaSSOController.customLoginCallBack=function(t){
						//alert(t.code);
					if(t.code==1){
						//记住登录名
							var isremembername=document.getElementById(self.opt.showboxremembernameid).checked;
							if(isremembername){
								self.setcookie('loginname',name);
							}else{
								self.setcookie('loginname','');
							}
						window.location.reload();
					}else if(t.reason==-2){
						self.showerrormsg('\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef',self);//用户名或密码错误
											
							sbmbtn.disabled=false;
							//记录输入错误的次数
							self.opt.wrongtime++;
							if(self.opt.wrongtime>=3){
								//成功验证码
								self.resetvcode(self);
							//var time=new Date().getTime()
							//var scriptSource='http://passport.show.sina.com.cn/vcode_s.php?'+time;
							//excuteScript("scriptCode", scriptSource, "utf-8");
							//打开验证码步骤
								self.opt.isvcode=true;
								//显示验证码栏
								document.getElementById(self.opt.showboxvcodeboxid).style.display='';
								//var time=new Date().getTime();
								//document.getElementById("login_showboxvcodeimg_loginbtn").setAttribute('src','http://passport.show.sina.com.cn/vcode_s.php?t='+time);
							}
					}else if(t.reason==-3){
												self.showerrormsg('\u9a8c\u8bc1\u7801\u9519\u8bef',self);//验证码错误
					}else if(t.reason==-4){
						self.showerrormsg('\u767b\u5f55\u8fc7\u4e8e\u9891\u7e41\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01',self);//登录过于频繁，请稍后再试！
					}else if(t.reason==-5){
						self.showerrormsg('\u7a0b\u5e8f\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5',self);//程序异常，请稍后再试
					}
					else if(t.reason==-99){
						//没有绑定手机
						//alert("没有绑定手机");
						self.showerrormsg('没有绑定手机,请绑定手机后再登陆。<a href="http://passport.show.sina.com.cn/security/mobile/bind_mobile.php">立即绑定</a>',self);//用户名或密码错误
						//location.href="http://passport.show.sina.com.cn/security/mobile/bind_mobile.php";
					}else if(t.reason==-96){
						self.showerrormsg('请输入手机验证码',self);//用户名或密码错误
						//提示输入验证码
						self.opt.isphonecode=true;
						document.getElementById(self.opt.showboxphonecodeboxid).style.display='';
						document.getElementById(self.opt.showboxphonecodebtnid).onclick=function(){
						document.getElementById(self.opt.showboxphonecodeinputid).value="请输入手机验证码";
						document.getElementById("login_showboxphonecodebtn_loginbtn").style.display='none';
	                    document.getElementById("m_rr").style.display='';
							
							start = 30;
                            step = -1;
							var mobile_code=document.getElementById(self.opt.showboxphonecodeinputid).value;
							var version=sinaSSOConfig.version;
							sinaSSOController.login(name,password,mobile_code,vcode,version);
							count();
						
						}
						
						self.opt.isphonecode=false;	
					}else if(t.reason==-98){
						self.showerrormsg('验证码错误',self);//用户名或密码错误
					}else if(t.reason==-97){
						self.showerrormsg('验证码过期',self);//用户名或密码错误
						//alert("验证码过期");
					}else if(t.reason==-95){
						self.showerrormsg('验证码错误',self);//用户名或密码错误
						//alert("验证码错误");
					}
				}
				var mobile_code=document.getElementById(self.opt.showboxphonecodeinputid).value;
				var version=sinaSSOConfig.version;
				//var vcode=document.getElementById(self.opt.showboxvcodeinputid).value;
				sinaSSOController.login(name,password,mobile_code,vcode,version);
			}

			
		},
		
	//weibo登陆提交
		weibo_sbm:function(self){
			//alert('0');
			//exit;
			var reg=/(^\s*)|(\s*$)/g;
			var nameinput=document.getElementById("weibo_username");
			var name=nameinput.value;
			name=name.replace(reg, "");
			var passwordinput=document.getElementById("weibo_password");
			var password=passwordinput.value;
			var sbmbtn=document.getElementById("weibo_submit");
			//alert('2');
			//alert(name);
			if(name==''){
				//alert('1');
				nameinput.focus();
				self.show_weibo_errormsg('\u8bf7\u8f93\u5165\u60a8\u7684\u7528\u6237\u540d',self);//请输入您的用户名
				return;
			}
			
			if(password==''){
				passwordinput.focus();
				self.show_weibo_errormsg('\u8bf7\u8f93\u5165\u60a8\u7684\u5bc6\u7801',self);//请输入您的密码
				return;
			}
			//自定义登陆方式
			if(self.opt.login_type==1){
				if(self.opt.isvcode){
					var vcodeinput=document.getElementById(self.opt.showboxvcodeinputid);
					var vcode=vcodeinput.value;
					vcode=vcode.replace(reg, "");
					if(vcode==''){
						vcodeinput.focus();
						self.showerrormsg('\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801',self);//请输入验证码
						return;
					}
				}
				
				var logindata;//ajax提交参数
				if(self.opt.isvcode){//有验证码
					logindata='username='+encodeURIComponent(name)+'&password='+encodeURIComponent(password)+'&vcode='+vcode;
				}else{
					logindata='username='+encodeURIComponent(name)+'&password='+encodeURIComponent(password);
				}
				//禁用登陆按钮
				sbmbtn.disabled=true;
				$.ajax({
					type:'post',
					url:'http://passport.show.sina.com.cn/weibo/weibosso.php',
					data:logindata,
					success:function(d){
						//d='{code:1,data:{name:"howlking"}}';
						eval('var t='+d);
						if(t.code==1){//通过
							//重载页面
							if(self.opt.isreload){
								window.location.reload();
							}
							//记住登录名
							var isremembername=document.getElementById(self.opt.showboxremembernameid).checked;
							if(isremembername){
								self.setcookie('loginname',name);
							}else{
								self.setcookie('loginname','');
							}
							//退出按钮ID
							if(self.opt.loginbtnid!=''){
								if(!self.opt.logoutbtnid){
									self.opt.logoutbtnid='login_logoutbtn_'+self.opt.loginbtnid;
								}
								var h='<span>\u6b22\u8fce，'+t.data.name+'</span><a id="'+self.opt.logoutbtnid+'" href="####" >\u9000\u51fa</a>';//欢迎//退出
								document.getElementById(self.opt.loginbtnid).parentNode.innerHTML=h;
								//为退出按钮绑定事件
								document.getElementById(self.opt.logoutbtnid).onclick=function(){self.logout(self)}
							}
							
							//删除登录框
							self.hidebox(self);
							//登录成功回调函数
							if(typeof self.opt.logincallback=='function'){
								self.opt.logincallback();
							}
						}else if(t.code==90){//用户名或密码错误
							self.show_weibo_errormsg('\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef',self);//用户名或密码错误
							//登录按钮解除禁用
							sbmbtn.disabled=false;
							//记录输入错误的次数
							//self.opt.wrongtime++;
//							if(self.opt.wrongtime>=3){
//								//成功验证码
//								self.resetvcode(self);
//								//打开验证码步骤
//								self.opt.isvcode=true;
//								//显示验证码栏
//								document.getElementById(self.opt.showboxvcodeboxid).style.display='';
//							}
						//}else 
					}
						else if(t.code==3){//验证码错误
							self.resetvcode(self);
							self.showerrormsg('\u9a8c\u8bc1\u7801\u9519\u8bef',self);//验证码错误
							sbmbtn.disabled=false;
						}				
						else if(t.code==4){//恶意登录
							self.resetvcode(self);
							self.showerrormsg('\u767b\u5f55\u8fc7\u4e8e\u9891\u7e41\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01',self);//登录过于频繁，请稍后再试！
							sbmbtn.disabled=false;
						}else if(t.code==5){//恶意登录
							self.resetvcode(self);
							self.showerrormsg('\u7a0b\u5e8f\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5',self);//程序异常，请稍后再试
									sbmbtn.disabled=false;
						}
					}
				})
			
			//js登陆方式
			}else if(self.opt.login_type==2){
				sinaSSOController.customLoginCallBack=function(t){
					if(t.code==1){
						//alert(t.money);
						window.location.reload();
					}else if(t.code==90){
						self.show_weibo_errormsg('\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef',self);//用户名或密码错误
					}else if(t.code==3){
						self.show_weibo_errormsg('\u9a8c\u8bc1\u7801\u9519\u8bef',self);//验证码错误
					}else if(t.code==4){
						self.show_weibo_errormsg('\u767b\u5f55\u8fc7\u4e8e\u9891\u7e41\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01',self);//登录过于频繁，请稍后再试！
					}else if(t.code==5){
						self.show_weibo_errormsg('\u7a0b\u5e8f\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5',self);//程序异常，请稍后再试
					}
					else if(t.reason==-99){
						//没有绑定手机
						//alert("没有绑定手机");
						self.show_weibo_errormsg('没有绑定手机,请绑定手机后再登陆。<a href="http://passport.show.sina.com.cn/security/mobile/bind_mobile.php">立即绑定</a>',self);//用户名或密码错误
						//location.href="http://passport.show.sina.com.cn/security/mobile/bind_mobile.php";
					}else if(t.reason==-96){
						self.show_weibo_errormsg('请输入手机验证码',self);//用户名或密码错误
						//提示输入验证码
						self.opt.isphonecode_weibo=true;
						document.getElementById(self.opt.showboxphonecodeboxid_weibo).style.display='';
						document.getElementById(self.opt.showboxphonecodebtnid_weibo).onclick=function(){
						document.getElementById(self.opt.showboxphonecodeinputid_weibo).value="请输入手机验证码";
						document.getElementById("login_showboxphonecodebtn_loginbtn_weibo").style.display='none';
	                    document.getElementById("m_rr_weibo").style.display='';
							//var scriptSource=self.opt.phonecode_url+'?'+'name='+encodeURIComponent(name)+'&password='+encodeURIComponent(password);
							//excuteScript("scriptPhonecCode", scriptSource, "utf-8");
							start = 30;
                            step = -1;
							var mobile_code=document.getElementById(self.opt.showboxphonecodeinputid_weibo).value;
							var version=sinaSSOConfig.version;
							sinaSSOController.login_weibo(name,password,mobile_code,version);
							count_weibo();
						
							//this.innerHTML="重新获取";
						}
						self.opt.isphonecode=false;	
					}else if(t.reason==-98){
						self.show_weibo_errormsg('验证码错误',self);//用户名或密码错误
					}else if(t.reason==-97){
						self.show_weibo_errormsg('验证码过期',self);//用户名或密码错误
						//alert("验证码过期");
					}else if(t.reason==-95){
						self.show_weibo_errormsg('验证码错误',self);//用户名或密码错误
						//alert("验证码错误");
					}
				}
				var version=sinaSSOConfig.version;
				var mobile_code=document.getElementById(self.opt.showboxphonecodeinputid_weibo).value;
				sinaSSOController.login_weibo(name,password,mobile_code,version);
			}
			
		},
		
		
		logout:function(self){
			$.ajax({
				type:'post',
				url:self.opt.logout_url,
				success:function(d){
					//d='{code:1,data:{}}';
					eval('var t='+d);
					if(t.code==1){//成功退出
						var h='<a  id="'+self.opt.loginbtnid+'" href="####">\u767b\u5f55</a>';//登录
						document.getElementById(self.opt.logoutbtnid).parentNode.innerHTML=h;
						//重新为登录按钮绑定事件
						self.addevent_login(self);
					}
				}
			})
		},
		showerrormsg:function(msg,self){
			var showboxserrormsg=document.getElementById(self.opt.showboxserrormsgid);
			showboxserrormsg.innerHTML=msg;
		},
		show_weibo_errormsg:function(msg,self){
			var showboxserrormsg=document.getElementById('weibo_error');
			showboxserrormsg.innerHTML=msg;
		},

		//弹框居中显视//不兼容google//用CSS方法代替些方法保留
		resitebox:function(self){

			/*var winW=document.documentElement.clientWidth;//窗口宽
			var winH=document.documentElement.clientHeight;//窗口高
			var docW=document.body.clientWidth;//内容宽
			var docH=document.body.clientHeight;//内容高
			var sroL=document.documentElement.scrollLeft;//滚动条距左
			var sroT=document.documentElement.scrollTop;//滚动条距上

			var showboxbg=document.getElementById(self.opt.showboxbgid);
			var showboxpop=document.getElementById(self.opt.showboxpopid);

			var showboxpopH=showboxpop.clientHeight;
			var showboxpopW=showboxpop.clientWidth;

			//背景宽高
			showboxbg.style.width=docW+sroL+'px';
			showboxbg.style.height=docH+'px';
			//定位弹框
			showboxpop.style.top=(winH-showboxpopH)/2+sroT+'px';
			showboxpop.style.left=(winW-showboxpopW)/2+sroL+'px';*/
		},
		setcookie:function(name,value){
			var Days = 30;
			var exp = new Date(); 
			exp.setTime(exp.getTime() + Days*24*60*60*1000);
			document.cookie=name+'='+value+';expires='+exp.toGMTString();
		},
		getcookie:function(name){
			var o={};
			var c=document.cookie;
			c=c.split(';');
			for(var i=0;i<c.length;i++){
				c[i]=c[i].split('=');
				if(c[i][0] && c[i][1]){
					c[i][0]=c[i][0].replace(/(^\s*)|(\s*$)/g, "");
					c[i][1]=c[i][1].replace(/(^\s*)|(\s*$)/g, "");
					o[c[i][0]]=c[i][1];	
				}
			}
			return o[name];
		}
	}
	
	
		//自动获得焦点后将光标放到最后一个字符
	function setIndexLastWord(inputId) {  
        var tar = document.getElementById(inputId);  
        if(tar.setSelectionRange) {  
             tar.setSelectionRange(tar.value.length , tar.value.length);  
        } else {  
             var range = tar.createTextRange();  
             range.moveStart("character",tar.value.length);  
             range.moveEnd("character",tar.value.length);  
             range.select();  
        }  
        tar.focus();  
 	} 
	
	function excuteScript(id, scriptSource, charset) {
		var head = document.getElementsByTagName('head')[0];
		var oldScript = document.getElementById(id);
		if (oldScript) {
			head.removeChild(oldScript);
		}
		var newScript = document.createElement('script');
		if (charset) {
			newScript.charset = charset;
		} else { 
			newScript.charset = 'gb2312';
		}
		newScript.id = id;
		newScript.type = 'text/javascript';
		newScript.src = scriptSource+'&'+(new Date()).getTime(); 
		head.appendChild(newScript);
			};
	
	//指定全局类名
	window.login=login;
	//跳转前验证是否登陆
})()
