
//*****************************
//*****************************
$(document).ready(function()
{
	$("#update_status_textinput").click(function(e)
	{
		if($("#update_status_textinput").val() != " Update your status here")
			return;
		$('#update_status_textinputname').css('display', 'block');
		$('#update_status_textinput').css('border-left-width', '0px');
		$('#update_status_textinput').val('is ');
		setCaretToEnd(document.getElementById('update_status_textinput'));
	});
})
function setCaretToEnd(control)
{
	if(control.createTextRange)
	{
		var range = control.createTextRange();
		range.collapse(false);
		range.select();
	}
	else if (control.setSelectionRange)
	{
		control.focus();
		var length = control.value.length;
		control.setSelectionRange(length, length);
	}
}
function closeSubscription()
{
	var conf = confirm("WARNING: Warning: If you close your subscription, your server will be shut down and all your configuration settings will be removed. This action cannot be reversed!\n\nAre you sure you wish to close the server?");
	return conf;
}
//*****************************
//PM SEND
//*****************************
$(document).ready(function()
{
	if($("#send_select").val() != 'manual_entry')
		$('#username').css('display', 'none');
	$("#send_select").keyup(function(e)
	{
		if($("#send_select").val() != 'manual_entry')
			$('#username').css('display', 'none');
		else
			$('#username').css('display', 'inline');
	});
	$("#send_select").change(function(e)
	{
		if($("#send_select").val() != 'manual_entry')
			$('#username').css('display', 'none');
		else
			$('#username').css('display', 'inline');
	});
})
//*******************************************************************************************
//*******************************************************************************************
function getConfiguration(e)
{
	e.style.display = 'none';
	document.getElementById('config_loading').style.display = 'block';
	$.ajax
	({
		type:			"POST",
		url:			'/ajax_requests.php?action=get_configuration&rnd=' + Math.round(Math.random()*10000000),
		processData:	false,
		data:			'GMCONFIGID=' + e.parentNode.GMCONFIGID.value + '&SUBID=' + e.parentNode.SUBID.value,
		dataType:		'html',
		success: function(html)
		{
			html = Base64.decode(html);
			resultCode		= html.toString().substring(0, 1);
			resultString	= html.toString().substring(1);
			e.parentNode.configurationData.value = resultString;
			document.getElementById('configuration_form').style.display = 'block';
			e.style.display = 'block';
			document.getElementById('config_loading').style.display = 'none';
			return;
		},
		error: function(html)
		{
			e.parentNode.configurationData.value = '';
			e.style.display = 'block';
			document.getElementById('config_loading').style.display = 'none';
			return;
		}
	});
}


//*******************************************************************************************
//*******************************************************************************************
function getCommandLine(e, SUBID)
{
	document.getElementById('command_line_ui01').style.display = 'none';
	document.getElementById('command_line_ui02').style.display = 'block';
	$.ajax
	({
		type:			"POST",
		url:			'/ajax_requests.php?action=get_command_line&rnd=' + Math.round(Math.random()*10000000),
		processData:	false,
		data:			'&SUBID=' + SUBID,
		dataType:		'html',
		success: function(html)
		{
			resultCode		= html.toString().substring(0, 1);
			resultString	= html.toString().substring(1);		
			if (resultCode == 1) 
			{
				document.getElementById('command_line_ui02').style.display = 'none';
				document.getElementById('command_line_ui04').innerHTML = resultString;
				document.getElementById('command_line_ui03').style.display = 'block';
			}
			else 
			{
				document.getElementById('command_line_ui02').style.display = 'none';
				document.getElementById('command_line_ui05').innerHTML = resultString;
				document.getElementById('command_line_ui05').style.display = 'block';
			}
			return;
		},
		error: function(html)
		{
			document.getElementById('command_line_ui02').style.display = 'none';
			document.getElementById('command_line_ui05').innerHTML = resultString;
			document.getElementById('command_line_ui05').style.display = 'block';
			return;
		}
	});
}


//*******************************************************************************************
//*******************************************************************************************
function getConsoleLog(e, SUBID)
{
	document.getElementById('console_log_ui01').style.display = 'none';
	document.getElementById('console_log_ui02').style.display = 'block';
	document.getElementById('console_log_ui03').style.display = 'none';
	$.ajax
	({
		type:			"POST",
		url:			'/ajax_requests.php?action=get_consoleLog&rnd=' + Math.round(Math.random()*10000000),
		processData:	false,
		data:			'&SUBID=' + SUBID,
		dataType:		'html',
		success: function(html)
		{
			html = Base64.decode(html);
			resultCode		= html.toString().substring(0, 1);
			resultString	= html.toString().substring(1);			
			document.getElementById('console_log_ui02').style.display = 'none';
			document.getElementById('console_log_ui04').innerHTML = resultString;
			document.getElementById('console_log_ui03').style.display = 'block';
			return;
		},
		error: function(html)
		{			
			document.getElementById('console_log_ui02').style.display = 'none';
			document.getElementById('console_log_ui04').innerHTML = resultString;
			document.getElementById('console_log_ui03').style.display = 'block';
			return;
		}
	});
}
//*******************************************************************************************
//*******************************************************************************************
function hideConsoleLog(e)
{
	document.getElementById('console_log_ui01').style.display = 'block';	
	document.getElementById('console_log_ui02').style.display = 'none';
	document.getElementById('console_log_ui03').style.display = 'none';
}
//PROFILE - ALIAS SEARCH
$(document).ready(function()
{
	if (document.getElementById("alias_server_search_name") != null)
	{
		$('#alias_server_search_name').keyup(function(e)
		{
			if (e.keyCode == 13)
				getAliasServerOptions();
		});
	}
	if (document.getElementById("alias_server_options") != null)
	{
		$('#alias_server_options').change(function(e)
		{
			if ($('#alias_server_options').val() == 'CUSTOMENTRY')
			{
				$('#alias_server_manualentry').css('display', 'block');
				$('#alias_server_options_add_button').css('display', 'none');
			}
			else
			{
				$('#alias_server_manualentry').css('display', 'none');
				$('#alias_server_options_add_button').css('display', 'block');
			}
		});
		$('#alias_server_options').keyup(function(e)
		{
			if (e.keyCode == 13)
				return;
			if ($('#alias_server_options').val() == 'CUSTOMENTRY')
			{
				$('#alias_server_manualentry').css('display', 'block');
				$('#alias_server_options_add_button').css('display', 'none');
			}
			else
			{
				$('#alias_server_manualentry').css('display', 'none');
				$('#alias_server_options_add_button').css('display', 'block');
			}
		});
	}
});
function getAliasServerOptions()
{
	//RESET STATE
	$('#alias_server_options').html("<option>Searching...</option>");
	$('#alias_server_options_add_button').css('display', 'none');
	$('#alias_servers').html("");
	$('#alias_servers').css('display', 'none');
	//RESET VISIBILITY
	$('#alias_server_visibilty').css('display', 'block');
	$('#alias_save_button').css('display', 'none');
	var playerName = document.getElementById('alias_server_search_name').value;
	$.ajax(
	{
		type: "GET",
		url: '/ajax_requests.php?action=getAliasServers&playerName=' + encodeURIComponent(playerName) + '&rnd=' + Math.round(Math.random() * 10000000),
		processData: false,
		dataType: 'html',
		success: function(html)
		{
			$('#alias_server_options').html(html);
			$('#alias_server_options').children(":first").attr('selected', 'selected');
			if ($('#alias_server_options').val() == 'CUSTOMENTRY')
			{
				$('#alias_server_manualentry').css('display', 'block');
				$('#alias_server_options_add_button').css('display', 'none');
			}
			else
			{
				$('#alias_server_manualentry').css('display', 'none');
				$('#alias_server_options_add_button').css('display', 'block');
			}
		},
		error: function(html)
		{
			alert("Server Request Failed");
		}
	});
}
function addAliasServerFromOptions()
{
	var GAMESERVERID = $('#alias_server_options').val();
	var gameServerText = $('#alias_server_options').find(":selected").text();
	addAliasServer(GAMESERVERID, gameServerText);
}
function addAliasServerFromManualEntry()
{
	var ip = document.getElementById('alias_server_manualentry_ip').value;
	var port = document.getElementById('alias_server_manualentry_port').value;
	$.ajax(
	{
		type: 'GET',
		url: '/ajax_requests.php?action=getAliasServer&ip=' + ip + '&port=' + port + '&rnd=' + Math.round(Math.random() * 10000000),
		processData: false,
		dataType: 'xml',
		success: function(xml)
		{
			if (xml.getElementsByTagName('error').length > 0)
			{
				alert(xml.getElementsByTagName('error').item(0).firstChild.data)
				return;
			}
			var GAMESERVERID = xml.getElementsByTagName('GAMESERVERID').item(0).firstChild.data;
			var gameServerText = decodeURIComponent(xml.getElementsByTagName('gameServerText').item(0).firstChild.data);
			addAliasServer(GAMESERVERID, gameServerText);
		},
		error: function(xml)
		{
			alert("Server Request Failed");
		}
	});
}
function addAliasServer(GAMESERVERID, gameserverHTML)
{
	if (GAMESERVERID == 'CUSTOMENTRY')
		return;
	//ALIAS SERVERS
	if (($('#alias_servers').children("#" + GAMESERVERID)).length > 0)
		return;
	var selectedCount = $('#alias_servers').children().length / 2;
	$('#alias_servers').append("<div id='" + GAMESERVERID + "'>" + gameserverHTML + "</div>");
	$('#alias_servers').append("<input type='hidden' name='ID" + selectedCount + "' value='" + GAMESERVERID + "'>");
	$('#alias_servers').css('display', 'block');
	//ALIAS SERVERS OPTIONS
	$('#alias_server_options').children("#" + GAMESERVERID).remove();
	if ($('#alias_server_options').children().length < 2)
	{
		$('#alias_server_options_add_button').css('display', 'none');
		$('#alias_server_manualentry').css('display', 'block');
	}
	//ALIAS SAVE BUTTON
	$('#alias_save_button').css('display', 'inline')
}
function saveAlias()
{
	//ALIAS SERVERS
	var nickname = document.getElementById('alias_server_search_name').value;
	nickname = encodeURIComponent(nickname);
	nickname = nickname.replace(new RegExp(/'/g), '%27');
	$('#alias_servers').append("<input type='hidden' name='nickname' value='" + nickname + "'>");
	$('#alias_servers').append("<input type='hidden' name='addNickName_x'>");
	//SUBMIT
	document.getElementById('alias_servers_form').submit();
}