function login(type,svrid){
	var frm = document.form1;
	var frm2 = document.form2;
	if(typeof svrid == "undefined") svrid=1;

	var action = $('#loginaction');
	var x = $('#loginaction').val();
	var name = frm.username;
	var pass = frm.password;
	if(action.val()=='login'){		
		if(name.value=='' || pass.value == ''){
			alert('请填写用户名密码');
			return false;
		}
	}
	var auth = frm.auth;
	var verify = frm.verify;
	var fcallback = function(json){
		if(json.auth == 'need_validatecode'){
			window.location.href = "http://passport.txwy.com/client/login/?l=zh-cn&continue="+encodeURIComponent(document.URL);
			return;
		}
		auth.value = json.auth;
		verify.value = json.verify;
		action.val('login');
		pass.value = '';

		frm2.auth.value = auth.value;
		frm2.verify.value = verify.value;
		frm2.action.value = 'login';
		
		(svrid == 1)?frm.submit() : frm2.submit();
		$('#loginaction').val(x);
	}
	
	if(typeof type == "undefined") type="";
	ajax(name.value,pass.value,fcallback,type);

	return true;
}

function ajax(name,pass, fcallback,type){
	var d = new Date()
	$.ajax({
	   async:false,
	   type: "GET",
	   url: "/login/warlord_api.aspx?username="+encodeURIComponent(name)+"&password="+encodeURIComponent(pass)+"&r="+Math.random()+"&d="+d.getMinutes()+'_'+d.getSeconds()+'_'+d.getMilliseconds()+"&type="+type,
	   dataType:'json',
	   success: function(json){fcallback(json);},
	   error : function(){
			alert("登录错误，请刷新页面重试！");
		}
	 });	
}
function setUserInfo(id,name){	
	if(document.getElementById("wl")){
		var s = $('#wl2').html();
		$('#wl2').html(s.replace('{$user}',decodeURIComponent(name)));
		$('#wl').css("display",'none');
		$('#wl2').css("display",'');
		$('#loginaction').val("d");
		
	}else{
		$("#regLi").html('');
		$("#logLi").html(' 你好, <a href="http://blog.mdbchina.com/u/'+name+'" target="_blank">'+decodeURIComponent(name)+'</a> | <a href="http://passport.txwy.com/client/logout/?continue='+encodeURIComponent(document.URL)+'&l=zh-cn">退出</a>');
	}
}

function showLogin(){
	if(document.getElementById("wl"))
		document.getElementById("wl").style.display = '';
}

function goToReg(){
	var url = encodeURIComponent(document.URL);
	var regurl = 'http://passport.txwy.com/client/register/?l=zh-cn&continue='+url;
	$("#regA1").attr("href",regurl);
	$("#regA2").attr("href",regurl);

	$("a").each(function(){
		var h = $(this).attr('href');
		if(typeof h != "undefined"){
			if(h.indexOf('continue=') != -1){
				$(this).attr('href',h.substr(0,h.indexOf('continue=')) + "continue=" + url);
			}
		}
	});

	document.formx["continue"].value = document.URL;
}

function txj(gameid){
	var parms = document.location.search;
	var pos = parms.indexOf("fuid=");
	if(pos > -1){
		parms = parms.substr(pos + 5, parms.length);
		pos = parms.indexOf("&");
		if(pos > -1){
			parms = parms.substr(0, pos);
		}
		$.getJSON("http://txj.txwy.com/union/AdClick?gameid=" + gameid + "&fuid=" + parms + "&callback=?", function(json){});
	}
}

$(function(){	
	var d= new Date();
	var m = d.getMonth() + 1;
	$("#timeEM").html(d.getFullYear() + "-" + m + "-" + d.getDate());

	goToReg();
	txj(3);
});

function showSwitchService(o){
	document.getElementById(o).style.display="";
	}
function hideSwitchService(o){
	document.getElementById(o).style.display="none";
	}

function getCard(){
	var email = $('#email').val();
	var re = /^([\w.])+\@(([\w])+\.)[a-zA-Z0-9]{2,}/i;
	if(!re.test(email)) {alert("邮箱格式错误！"); return;}
	$.ajax({
	   async:false,
	   type: "GET",
	   url: "/getcard/sendcard.ashx?email="+email,
	   dataType:'json',
	   success: function(json){
		   if(typeof json.MessageCode != "undefined"){
			if(json.MessageCode == 1) alert("发送成功，请查看您的邮箱！");
			if(json.MessageCode == 0) alert("邮箱已经使用过！");
			if(json.MessageCode == 2) alert("对不起，您已超过获取限制！");
		   }else{
			alert("发送错误，请查重试！");
		   }
		},
	   error : function(){
			alert("发送错误，请查重试！");
		}
	});
}