﻿var _fuid = "";
var _regUrl = "";
var _serverId = 0;
AsyncScript = function(){

	var userAgent = navigator.userAgent.toLowerCase();
	Browser = {
		version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
		isSafari: /webkit/.test( userAgent ),
		isOpera: /opera/.test( userAgent ),
		isMsie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
		isMozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	}

	var _node = function(nodeType,attributes,win){
		var w = win || window, d=w.document, n=d.createElement(nodeType);
		for (var i in attributes) {
			if (attributes[i]) {
				n.setAttribute(i, attributes[i]);
			}
		}
		return n;
	};

	var _scriptNode = function(url,win,charset){
		var c = charset || "UTF-8";
		return _node("script",{
		"id":"alicn",
		"type":"text/javascript",
		"charset": c,
		"src":url
		},win);
	};

	return {
		script:function(url,fn,scope){
			var w=scope||window, d=w.document, h=d.getElementsByTagName("head")[0], n;
			n = _scriptNode(url,w,"UTF-8");
			
			if(Browser.isMsie){
					n.onreadystatechange = function(){
						var rs = this.readyState;
						if("loaded" === rs || "complete" === rs){
							h.appendChild(n);
							fn();
						}
					}
				}else{
					h.appendChild(n);
					n.onload = function(){
					fn();
				}
			}
		}
	}
}();


var gameAPILogin = function(){
    var _ajax = function(url,callbackname){
        var d = new Date();
		url += "?r="+Math.random()+"&d="+d.getMinutes()+'_'+d.getSeconds()+'_'+d.getMilliseconds()+"&jsonp="+callbackname+"&serverid="+_serverId;
		if(_fuid != "")
			url+= "&fuid=" + encodeURIComponent(_fuid);
		AsyncScript.script(url,function(){},null);
    };
    
    return {
        login : function(url,callbackname){
	        _ajax(url,callbackname);
			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;
	}

	var url = json.Url;
	if(url.indexOf(',') > -1){
		url = url.split(',')[_serverId];
	}

	document.form1.auth.value = json.AuthKey;
	document.form1.verify.value = json.Verify;
	document.form1.action = url;
	document.form1.password.value = "";
	document.form1.fuid.value = json.FUID;
	document.form1.username.value = json.UserName;
	
	document.form1.submit();
}

function doLogin(srvId){
	_serverId = srvId;
    return gameAPILogin.login("http://login.ww2.playcomet.com/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);

	if(document.formx)
		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);
});

var switchList = [];
function showSwitchService(o){
	document.getElementById(o).style.display="";
	SwitchService(o, "none");
	
	if(document.getElementById("overlay"))
	document.getElementById("overlay").style.visibility="visible";
}

function hideSwitchService(o){
	document.getElementById(o).style.display="none";

	if(document.getElementById("overlay"))
	document.getElementById("overlay").style.visibility="hidden";
}
function SwitchService(o, displayType){
	var isIn = 0;
	if(switchList.length > 0){
		for(var i = 0; i < switchList.length; i++){
			if(o == switchList[i]){
				isIn = 1;
				continue;
			}
			document.getElementById(switchList[i]).style.display=displayType;
		}

		if(!isIn) switchList[switchList.length] = o;
	}else{
		switchList[0] = o;
	}
}


var getCardCallback = function(MessageCode){
	if(typeof MessageCode != "undefined"){
		if(MessageCode == 1) alert("Sent successfully, please check the code in your email box.");
		if(MessageCode == 0) alert("Newbie Gift Code has been sent to your Email, please check it out. If your operation fails, try again in 20 minutes.");
		if(MessageCode == 2) alert("Sorry, the limit for the claim shall not be exceeded.");
	   }else{
		alert("Error, please try again.");
	   }
};

function getCard(){
	var id = $('#ServerSelector').find('option:selected').val();
	t = (id == 0) ? "wwii_new_user_card" : "wwii_new_user_card2"

	emailid = "email";
	
	var email = $('#' + emailid).val();
	var re = /^([\w.])+\@(([\w])+\.)[a-zA-Z0-9]{2,}/i;
	if(!re.test(email)) {alert("Email Address Error!"); return;}
	var url = "http://login.ww2.playcomet.com/login/sendcard.ashx?email="+email+"&jsonp=getCardCallback&t="+t;
	AsyncScript.script(url,function(){},null);
}

var fcallback2 = function(srvIds){
    if(srvIds == null){
			return false;
	}

	if(srvIds.length == 0){
		$("#srvcontainer").html($("#server3C").html() + $("#server2C").html());
	}else{
		var s = '';
		for (var i=0; i<srvIds.length; i++)
		{
			if($("#server" + srvIds[i]))
				s += $("#server" + srvIds[i] + "C").html();
		}
		$("#srvcontainer").html(s);
	}
}

