var gameAPILogin = function(){
    var _ajax = function(name,pass,url,fcallback){
        var d = new Date();
	    $.ajax({
	       async:false,
	       type: "GET",
	       url: url+"?username="+encodeURIComponent(name)+"&password="+encodeURIComponent(pass)+"&r="+Math.random()+"&d="+d.getMinutes()+'_'+d.getSeconds()+'_'+d.getMilliseconds(),
	       dataType:'json',
	       success: function(json){fcallback(json);},
	       error : function(){
			    alert("登录错误，请刷新页面重试！");
		    }
	     });
    };
    
    return {
        login : function(url,fcallback){
            var action = document.getElementById("loginaction");
	        var name = document.form1.username;
	        var pass = document.form1.password;
	        var vcode = document.form1.code;
	        if(action.value=='login'){		
		        if(name.value=='' || pass.value == ''){
			        alert('请填写用户名密码');
			        return false;
		        }
	        }
	        _ajax(name.value,pass.value,url,fcallback);

	        return false;
        }
    }
}();

/*
*wulinzuqiu login
*/
var fcallback = function(json){
    if(json.AuthKey == null){
			window.location.href = "http://passport.txwy.com:8080/client/login/?l=zh-cn&continue="+encodeURIComponent(document.URL);
			return false;
	}
	document.form1.verify_code.value = json.AuthKey;
	document.form1.timestamp.value = json.TimeStamp;
	document.form1.nickname.value = json.UserName;
	document.form1.username.value = json.UserName;
	document.form1.action = json.Url;
	document.form1.password.value = "";
	
	document.form1.submit();
}

function doLogin(){
    return gameAPILogin.login("/login/login.ashx",fcallback);
}

function form_onsubmit(){
	var name = document.formx.username;
	var pass = document.formx.password;
	if(name.value=='' || pass.value == ''){
		alert('请填写用户名密码');
		return false;
	}

	return true;
}

function setUserInfo(id,name){	
	if(document.getElementById("wl") && document.getElementById("wl2")){
		var s = $('#wl2').html();
		s = s.replace('%7B','{').replace('%7D','}');
		$('#wl2').html(s.replace('{$userencode}',name).replace('{$user}',decodeURIComponent(name)));
		$('#wl').css("display",'none');
		$('#wl2').css("display",'');
		$('#loginaction').val("d");
	}
}

function showLogin(){
	if(document.getElementById("wl")){
		document.getElementById("wl").style.display = '';
		$('#loginaction').val("login");
	}
}

function goToReg(){
	var url = encodeURIComponent(document.URL);
	var regurl = 'http://passport.txwy.com:8080/client/register/?l=zh-cn&continue='+url;
	$("#regA2").attr("href",regurl);
	$("#regA1").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 AddFavorite(sURL, sTitle)
{
	try
	{
		window.external.addFavorite(sURL, sTitle);
	}
	catch (e)
	{
		try
		{
			window.sidebar.addPanel(sTitle, sURL, "");
		}
		catch (e)
		{
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
	}
}
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);
			 }
	}
}

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: "/login/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("发送错误，请查重试！");
		}
	});
}

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(){
	goToReg();
	if(document.getElementById("regSpan"))
		document.getElementById("regSpan").innerHTML = document.getElementById("tempSpan").innerHTML;
	txj(1);
});
