var urlLogin = 'getloginxml.aspx';
var divLogin = 'login';
var divTotalItems = 'totalItens';
var divTotalValue = 'totalValue';
var origem = 'document.formCartoon.flagBrturbo.value';

var htmlReady = "";
var htmlLoading = "<span class=\"LoginFont\">Carregando...</span>";
var httpRequestLogin = null;
var isLogged = false;
var buttonRegisterName = 'buttonRegister';

// Performs a login using ajax
function PerformLogin(email, password)
{	
	httpRequestLogin = CreateXmlHttpRequest();
	
	if(httpRequestLogin != null)
	{
		try
		{
			var btnRegister = document.getElementById (buttonRegisterName);
			var divFormLogin = 	document.getElementById(divLogin);
			
			if (btnRegister && divFormLogin)
			{
				htmlReady = divFormLogin.innerHTML;
				divFormLogin.innerHTML = htmlLoading;
				btnRegister.style.visibility = 'hidden';
				
			}
			
			httpRequestLogin.onreadystatechange = ParseLogin;
			httpRequestLogin.open("GET", urlLogin + "?email=" + escape(email) + "&password=" + escape(password));
			httpRequestLogin.send(null);
		}
		catch(e)
		{
			document.getElementById(divLogin).innerHTML = htmlReady;
			alert('Could not open URL');
		}
	}
	else
	{
		document.getElementById(divLogin).innerHTML = htmlReady;
		alert('XmlHttpRequest object could not be loaded');
	}
}

// Performs a login using ajax
function PerformLoginCartoon(email, password)
{	
	httpRequestLogin = CreateXmlHttpRequest();

	
	if(httpRequestLogin != null)
	{
		try
		{
			var btnRegister = document.getElementById (buttonRegisterName);
			var divFormLogin = 	document.getElementById(divLogin);
			
			if (btnRegister && divFormLogin)
			{
				htmlReady = divFormLogin.innerHTML;
				divFormLogin.innerHTML = htmlLoading;
				btnRegister.style.visibility = 'hidden';
				
			}
			
			httpRequestLogin.onreadystatechange = ParseLoginCartoon;
			httpRequestLogin.open("GET", urlLogin + "?email=" + escape(email) + "&password=" + escape(password));
			httpRequestLogin.send(null);
		}
		catch(e)
		{
			document.getElementById(divLogin).innerHTML = htmlReady;
			alert('Could not open URL');
		}
	}
	else
	{
		document.getElementById(divLogin).innerHTML = htmlReady;
		alert('XmlHttpRequest object could not be loaded');
	}
}

function PerformLoginCartoonTurbo(email, password)
{	
	httpRequestLogin = CreateXmlHttpRequest();
	
	if(httpRequestLogin != null)
	{
		try
		{
			var btnRegister = document.getElementById (buttonRegisterName);
			var divFormLogin = 	document.getElementById(divLogin);
			
			if (btnRegister && divFormLogin)
			{
				htmlReady = divFormLogin.innerHTML;
				divFormLogin.innerHTML = htmlLoading;
				btnRegister.style.visibility = 'hidden';
			}
			
			httpRequestLogin.onreadystatechange = ParseLoginCartoonTurbo;
			httpRequestLogin.open("GET", urlLogin + "?email=" + escape(email) + "&password=" + escape(password));
			httpRequestLogin.send(null);
		}
		catch(e)
		{
			document.getElementById(divLogin).innerHTML = htmlReady;
			alert('Could not open URL');
		}
	}
	else
	{
		document.getElementById(divLogin).innerHTML = htmlReady;
		alert('XmlHttpRequest object could not be loaded');
	}
}

// Parses the return of the login XML
function ParseLogin()
{
	if (httpRequestLogin.readyState == 4) // readyState = 4 -> Complete
    {
		if(httpRequestLogin.status == 200)  // status = 200 -> OK
		{
			// XML loaded properly, parses it
			var xml = httpRequestLogin.responseXML;
			
			// Loads the nodes
			var idNode = GetFirstNode(xml,"id");
			var statusNode = GetFirstNode(xml,"status");
			var messageNode = GetFirstNode(xml,"message");
			var totalItemsNode = GetFirstNode(xml , "total_items");
			var totalvalueNode = GetFirstNode(xml , "total_value");
			
			// Gets the values
			var id = parseInt(GetNodeValue(idNode));
			var status = parseInt(GetNodeValue(statusNode));
			var message = GetNodeValue(messageNode);
			var items = GetNodeValue(totalItemsNode);
			var value = GetNodeValue(totalvalueNode);

			// If the login was incorrect, display alert
			if(id<=0)
			{
				// Invalid login
				alert("Usuário inválido.");
				
				//Show the fields "email" and "password" and the button "Cadastre-se"
				document.getElementById(divLogin).innerHTML = htmlReady;
				document.getElementById(buttonRegisterName).style.visibility = 'visible';
			}
			else
			{
				// Valid login
				isLogged = true;
				// Loads the user name
				var nameNode = GetFirstNode(xml,"name");
				var name = GetNodeValue(nameNode);
				
				var divValue = document.getElementById (divTotalValue);
				var divItems = document.getElementById (divTotalValue);
				
				if (divValue != null && divItems != null)
				{
					divItems.innerHTML = items;
					divValue.innerHTML = value;
				}

				// Updates the user information
				PostLogin(name,'');				
			}
		}
		else
		{
			document.getElementById(divLogin).innerHTML = htmlReady;
			alert('Error loading XML: ' + httpRequestLogin.status);
		}
	}
}

function PostLogin(username, contents, startplay)
{
	isLogged = true;

	var div = document.getElementById(divLogin);
	var btnRegister = document.getElementById (buttonRegisterName);
	
	if (btnRegister)
		btnRegister.style.visibility = "hidden";

	if(div)
	{
		div.innerHTML = '<span class=\"LoginFont\">Olá, ' + username + '&nbsp;<span style=\"cursor:pointer;\" onclick=\"PerformLogout();\">(logout)</span></span>';
	}
	
	if(contents)
	{
		if(contents!='')
		{
			PerformMediaAdd(contents,startplay,false);
		}
	}
}

function PerformLogout()
{
	// Clears the playlist
	if(plIg) plIg.clear();
	
	if (isLogged)
	{
		isLogged = false;
	}
	
	// Redirects to the default page with logoff parameter
	top.location.href = 'default.aspx?logout=true&channel='+document.getElementById('urlChannel').value;
	
	/*var currentUrl = window.location.href;
	var requestParams = null;
	if(currentUrl.indexOf('?') >= 0)
		requestParams = currentUrl.substr(currentUrl.indexOf('?') + 1, currentUrl.length - 1);
	
	if(requestParams != null)
	{
		if(requestParams.indexOf('logout') >= 0)
			top.location.href = 'default.aspx?' + requestParams;
		else
			top.location.href = 'default.aspx?logout=true&' + requestParams;
	}
	else
	{
		top.location.href = 'default.aspx?logout=true';
	}*/
}

// Parses the return of the login XML
function ParseLoginCartoon()
{
	if (httpRequestLogin.readyState == 4) // readyState = 4 -> Complete
    {
		if(httpRequestLogin.status == 200)  // status = 200 -> OK
		{
			// XML loaded properly, parses it
			var xml = httpRequestLogin.responseXML;
			
			// Loads the nodes
			var idNode = GetFirstNode(xml,"id");
			var statusNode = GetFirstNode(xml,"status");
			var messageNode = GetFirstNode(xml,"message");
			var totalItemsNode = GetFirstNode(xml , "total_items");
			var totalvalueNode = GetFirstNode(xml , "total_value");
			
			// Gets the values
			var id = parseInt(GetNodeValue(idNode));
			var status = parseInt(GetNodeValue(statusNode));
			var message = GetNodeValue(messageNode);
			var items = GetNodeValue(totalItemsNode);
			var value = GetNodeValue(totalvalueNode);

			// If the login was incorrect, display alert
			if(id<=0)
			{
				
				// Invalid login				
				window.open('PopupLoginSubscriptionCartoonPrincipal.aspx?channel=65&origem=cartoon', 'login', 'top=20, width=290, height=220, toolbar=no, resizable=0');
				//window.open('Home.aspx?');
				//Show the fields "email" and "password" and the button "Cadastre-se"
				document.getElementById(divLogin).innerHTML = htmlReady;
				document.getElementById(buttonRegisterName).style.visibility = 'visible';
			}
			else
			{
				// Valid login
				isLogged = true;
				// Loads the user name
				var nameNode = GetFirstNode(xml,"name");
				var name = GetNodeValue(nameNode);
				
				var divValue = document.getElementById (divTotalValue);
				var divItems = document.getElementById (divTotalValue);
				
				if (divValue != null && divItems != null)
				{
					divItems.innerHTML = items;
					divValue.innerHTML = value;
				}

				// Updates the user information
				PostLoginCartoon(name,'','');				
			}
		}
		else
		{
			document.getElementById(divLogin).innerHTML = htmlReady;
			alert('Error loading XML: ' + httpRequestLogin.status);
		}
	}
}

function ParseLoginCartoonTurbo()
{
	if (httpRequestLogin.readyState == 4) // readyState = 4 -> Complete
    {
		if(httpRequestLogin.status == 200)  // status = 200 -> OK
		{
			// XML loaded properly, parses it
			var xml = httpRequestLogin.responseXML;
			
			// Loads the nodes
			var idNode = GetFirstNode(xml,"id");
			var statusNode = GetFirstNode(xml,"status");
			var messageNode = GetFirstNode(xml,"message");
			var totalItemsNode = GetFirstNode(xml , "total_items");
			var totalvalueNode = GetFirstNode(xml , "total_value");
			
			// Gets the values
			var id = parseInt(GetNodeValue(idNode));
			var status = parseInt(GetNodeValue(statusNode));
			var message = GetNodeValue(messageNode);
			var items = GetNodeValue(totalItemsNode);
			var value = GetNodeValue(totalvalueNode);

			// If the login was incorrect, display alert
			if(id<=0)
			{
			
				// Invalid login				
				window.open('PopupLoginSubscriptionCartoonPrincipal.aspx?channel=65&origem=cartoon.brturbo', 'login', 'top=20, width=290, height=220, toolbar=no, resizable=0');
		
				
				//Show the fields "email" and "password" and the button "Cadastre-se"
				document.getElementById(divLogin).innerHTML = htmlReady;
				document.getElementById(buttonRegisterName).style.visibility = 'visible';
			}
			else
			{
				// Valid login
				isLogged = true;
				// Loads the user name
				var nameNode = GetFirstNode(xml,"name");
				var name = GetNodeValue(nameNode);
				
				var divValue = document.getElementById (divTotalValue);
				var divItems = document.getElementById (divTotalValue);
				
				if (divValue != null && divItems != null)
				{
					divItems.innerHTML = items;
					divValue.innerHTML = value;
				}

				// Updates the user information
				PostLoginCartoonTurbo(name,'','');				
			}
		}
		else
		{
			document.getElementById(divLogin).innerHTML = htmlReady;
			alert('Error loading XML: ' + httpRequestLogin.status);
		}
	}
}

function PostLoginCartoon(username, contents, startplay)
{
	isLogged = true;
	
	var div = document.getElementById(divLogin);
	var btnRegister = document.getElementById (buttonRegisterName);
	//var origem = document.formCartoon.flagBrturbo.value;
	
	if (btnRegister)
		btnRegister.style.visibility = "hidden";

	if(div)
	{
		window.location = "cartoonLogged.aspx?origem=" + document.getElementById('hiddenPagOrigem').value;
	}
	
	if(contents)
	{
		if(contents!='')
		{
			PerformMediaAdd(contents,startplay,false);
		}
	}
}

function PostLoginCartoonTurbo(username, contents, startplay)
{
	isLogged = true;

	var div = document.getElementById(divLogin);
	var btnRegister = document.getElementById (buttonRegisterName);
	var origem =  document.formCartoon.flagBrturbo.value;	
	
	if (btnRegister)
		btnRegister.style.visibility = "hidden";

	if(div)
	{	
		window.location = "./home.aspx?channel=65&idurl=cartoon.brturbo"// +origem;
	}
	
	if(contents)
	{
		if(contents!='')
		{
			PerformMediaAdd(contents,startplay,false);
		}
	}
}

function goTo (channel)
{
	var channel;
	var startplay = document.getElementById('hiddenisLogged').value;
	var origem = document.getElementById('hiddenPagOrigem').value;
	
	window.location = "home.aspx?channel="+channel+"&origem="+origem+"&startplay="+startplay;
}