﻿var _fuid = "";
var _regUrl = "";

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() + "&fuid=" + encodeURIComponent(_fuid),
	       dataType:'json',
	       success: function(json){fcallback(json);},
	       error : function(){
			    alert("System error, please try again!");
		    }
	     });
    };
    
    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;
	        
	        _ajax(name.value,pass.value,url,fcallback);

	        return false;
        }
    }
}();

/*
*wwii login
*/
var fcallback = function(json){
    if(json.AuthKey == null){
			window.location.href = "http://passport.bitcomet.com/client/login/?l=en-us&continue="+encodeURIComponent(document.URL);
			return false;
	}
	document.form1.auth.value = json.AuthKey;
	document.form1.verify.value = json.Verify;
	document.form1.action = json.Url;
	document.form1.password.value = "";
	document.form1.fuid.value = json.FUID;
	document.form1.username.value = json.UserName;
	
	document.form1.submit();
}

function doLogin(){
    return gameAPILogin.login("/login/login.ashx",fcallback);
}

function setUserInfo(id,name){	
	if(isShowLogin()){
		if(document.getElementById("wl")){
			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",'');
		}
	}else{
		showWL3();
	}
}

function showLogin(){
	if(isShowLogin()){
		if(document.getElementById("wl")){
			document.getElementById("wl").style.display = '';
			document.getElementById("wl2").style.display = 'none';
		}
	}else{
		showWL3();
	}
}

function showWL3(){
	if(document.getElementById("wl3")){
		$('#wl').css("display",'none');
		$('#wl2').css("display",'none');
		$('#wl3').css("display",'');
	}
}

function isShowLogin(){
	var url = location.href;
	return (url.indexOf('www.wwiiwarfare.com') != -1)?false:true;
}

function goToReg(){
	_regUrl = 'http://passport.bitcomet.com/client/register/?l=en-us&continue='+encodeURIComponent(document.URL);
	if(_fuid != ""){
		_regUrl += "&fuid=" + encodeURIComponent(_fuid);
	}
	$("#regA2").attr("href",_regUrl);
	$("#regA1").attr("href",_regUrl);

	document.formx["continue"].value = document.URL;
}

function form_onsubmit(){
	var name = document.formx.username;
	var pass = document.formx.password;
	if(name.value=='' || pass.value == ''){
		alert('Please fill in username and password!');
		return false;
	}

	return true;
}

function RegBtn_onClick(){
	if(_regUrl != ""){
		window.location.href=_regUrl;
	}
}

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://stat.playcomet.com/AdClick?gameid=" + gameid + "&fuid=" + parms + "&callback=?", function(json){});
	}
}

$(function(){
	var args = document.location.search;
	var pos1 = args.indexOf("fuid=");
	if(pos1 != -1){
		var temp = args.substr(pos1);
		var pos2 = temp.indexOf("&");
		if(pos2 != -1){
			temp = temp.substr(0,pos2);
		}
		_fuid = decodeURIComponent(temp.substr(temp.indexOf("fuid=") + 5));
	}

	goToReg();
	txj(1001);
});