var cookieValue = $.cookie("DFOPassport");
var objUpdateInterval = null;
var loggedInStatus = false;



function NxLaunchGameMain()
{
    if(loggedInStatus == true)
    {
        createCookie("game","true",180);
        NxLaunchGame();
    }else
    {
        location.href='../StartGame.aspx';
    }
}
    //function NxLaunchGameMain(){
	//    open('http://nxcache.nexon.net/dungeonfighter/web/html/dfo_download_popup.html','miniwin','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=510,height=465');
    //}




function onUpdateResponse(resultObject, responseXML)
{
	if (resultObject.ErrorCode == undefined)	// do Logout if the session doesn't exist
	{
		NgbLogin.Logout();
	}
	else if (resultObject.ErrorCode == CommonError.NoError)		// OK
	{
		window.clearInterval(objUpdateInterval);
// After ExpireLimit(Second), do UpdateSession
		objUpdateInterval = setInterval("doUpdateSession()", resultObject.UpdateInterval * 1000);	
}
	else if (resultObject.ErrorCode == AuthSystemError.Disconnected)
	{
// Replace the following line to a custom code
		alert("Disconnected by other session.");
		NgbLogin.Logout();
	}
	else
	{
// Replace the following line to a custom code
		alert("Disconnected.");
		NgbLogin.Logout();
	}
}
function doUpdateSession()
{
	AuthSystem.UpdateSession(onUpdateResponse);
	loggedInStatus = true;
}


function startUpdateSession()
{

		setTimeout("doUpdateSession()", 1);
}


//var loginBox = ".loginBox";
var cookieName = "DFOPassport";
var cookieValue = $.cookie(cookieName);
function signupBoxLoad(){
	$("#toggleLogin").click(function(){
		$("#LoginSection").load("../Controls/Passport.aspx .login_window", loginBoxLoad);
	});
}


function loginBoxLoad(){
    var el = $("input").not($(".login_window input").not('[disabled=true]'));
    el.disabled = true;

	$('.username').attr('value','');//ensure username resets to blank upon refresh of page
	$('.login_window input').focus(function(){
		$(this).css('color', '#333');//change the color of the text as the user is typing their info
		if ($(this).hasClass('username'))
			$(this).css('background-position','100% 0')//clear the ID field when user clicks
		if ($(this).hasClass('password'))
			$(this).css('background-position','100% 0')//show the password field for user input
	});  
	$('.remember_me img').toggle(function(){//need img source for checkbox checked and unchecked *mabinogi used in example
		$(this).attr('src','http://nxcache.nexon.net/dungeonfighter/web/common/remember_checkbox_checked.gif')},//change the checkbox image once clicked,
	function(){
		$(this).attr('src','http://nxcache.nexon.net/dungeonfighter/web/common/remember_checkbox_unchecked.gif')//change back to other image if clicked again
	});
	if (cookieValue != null) {
		$("input[id*=tbID]").val(cookieValue).css({color: '#333',backgroundPosition: '100% 0'});
		$('.remember_me img').click();
	}
	$(".submit").click(function(){
		 
		
		var alertStr = ($("input[id*=tbPass]").val() == "" ? "Please enter your Password" : 
						($("input[id*=tbID]").val() == "ID" || $("input[id*=tbID]").val() == "" ? "Please input your ID" : ""));		
		if (alertStr != "") {
			alert(alertStr);
			return false;
		}
		else {
			var postParams = $.grep($("form").serializeArray(), function(value) {return (value.name == "tbID" || value.name == "tbPass");});
			$.ajax({
				url: "../Controls/Passport.aspx",
				type: "POST",
				data: postParams,
				datatype: "html",
				success: function(data){
					var alertData = data.split("<script language=\"javascript\">");
					if (alertData[1] != null) {
						alertData = alertData[1].split("</script>");
						eval(alertData[0]);
					}
					else {
						if ($(".remember_me img[src*=unchecked]").size() < 1)
							$.cookie(cookieName,postParams[0].value.toString(),{expires: 365,path: "/",domain:"nexon.net"});
						else
							$.cookie(cookieName, null, {path: "/",domain:"nexon.net"});
						$("#LoginSection").load("../Controls/Passport.aspx .logoutAjax", function(){el.removeAttr("disabled"); startUpdateSession();});
					}
				},
				error: function(xhr){
					alert("I'm sorry. There was an error. Please try again later. "+ xhr.status + " " + xhr.statusText);
				}
			});
			return false;
		}
	});
}


