// JavaScript Document
String.prototype.trim = function(c, t)
{
	return c = "[" + (c == undefined ? " " : c.replace(/([\^\]\\-])/g, "\\\$1")) + "]+", this.replace(new RegExp((t != 2 ? "^" : "") + c + (t != 1 ? "|" + c + "$" : ""), "g"), "");
};

function abrirAjax()
{
	var ajax;
	try
	{
		ajax = new XMLHttpRequest();
		if (ajax.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			ajax.overrideMimeType('text/html');
		}
	}
	catch(ee)
	{
		try
		{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E)
			{
				ajax = false;
			}
		}
	}
	return ajax;
}

function ExcluirLink(id)
{
	var ok = confirm("Excluir link?");
	if (ok)
	{
		var ajax = abrirAjax();
		ajax.open("GET", "ajax/excluir_link.php?id=" + id, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				//eval(resultado);
				
				window.location.reload();
				
			 }
		  }
		}
		ajax.send(null);
	}
}


function ExcluirNovidade(id)
{
	var ok = confirm("Excluir novidade?");
	if (ok)
	{
		var ajax = abrirAjax();
		ajax.open("GET", "ajax/excluir_novidade.php?id=" + id, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				//eval(resultado);
				
				window.location.reload();
				
			 }
		  }
		}
		ajax.send(null);
	}
}


function ExcluirPromocao(id)
{
	var ok = confirm("Excluir promoção?");
	if (ok)
	{
		var ajax = abrirAjax();
		ajax.open("GET", "ajax/excluir_promocao.php?id=" + id, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				//eval(resultado);
				
				window.location.reload();
				
			 }
		  }
		}
		ajax.send(null);
	}
}


function ExcluirTipoProduto(id)
{
	var ok = confirm("Excluir tipo produto?");
	if (ok)
	{
		var ajax = abrirAjax();
		ajax.open("GET", "ajax/excluir_tipo_produto.php?id=" + id, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				//eval(resultado);
				
				window.location.reload();
				
			 }
		  }
		}
		ajax.send(null);
	}
}


function ExcluirProduto(id)
{
	var ok = confirm("Excluir produto?");
	if (ok)
	{
		var ajax = abrirAjax();
		ajax.open("GET", "ajax/excluir_produto.php?id=" + id, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				//eval(resultado);
				
				window.location.reload();
			 }
		  }
		}
		ajax.send(null);
	}
}


function GravarUsuario(id)
{
	var login = document.getElementById("login").value;
	var ajax = abrirAjax();
	ajax.open("GET", "ajax/existe_usuario.php?login=" + login + "&id=" + id, true);
	ajax.onreadystatechange = function ()
	{
	  if (ajax.readyState == 4)
	  {
		 if (ajax.status == 200)
		 {
			resultado = ajax.responseText;
			resultado = resultado.replace(/\+/g, " ");
			resultado = unescape(resultado);
			resultado = resultado.trim();
			eval(resultado);
			
			if(existe)
			{
				alert("O login: " + document.getElementById("login").value + " já existe.");
				document.getElementById("login").focus();
			} else
			{
				if (ValidarFormulario(0)) document.forms[0].submit();
			}
		 }
	  }
	}
	ajax.send(null);
}


function ResetarSenha(id)
{
	var ajax = abrirAjax();
	ajax.open("GET", "ajax/resetar_senha.php?id=" + id, true);
	ajax.onreadystatechange = function ()
	{
	  if (ajax.readyState == 4)
	  {
		 if (ajax.status == 200)
		 {
			resultado = ajax.responseText;
			resultado = resultado.replace(/\+/g, " ");
			resultado = unescape(resultado);
			resultado = resultado.trim();
			//eval(resultado);
		 }
	  }
	}
	ajax.send(null);
}


function MudarSenha(id)
{
	var atual = document.getElementById("senha_atual");
	var nova = document.getElementById("nova_senha");
	
	if ((atual.value != "") && (nova.value != ""))
	{
		var ajax = abrirAjax();
		ajax.open("GET", "ajax/mudar_senha.php?id=" + id + "&atual=" + atual.value + "&nova=" + nova.value, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				//eval(resultado);
				atual.value = "";
				nova.value = "";
				alert("Senha alterada.");
			 }
		  }
		}
		ajax.send(null);
	} else
	{
		var msg = ""
		if (atual.value == "") msg += "- O campo senha atual não pode estar vazio.\n";
		if (nova.value == "") msg += "- O campo nova senha não pode estar vazio.";
		if (msg != "") alert(msg);
	}
}


function Newsletter()
{
	var email = document.getElementById("email");
	if (echeck(email.value) == true)
	{
		var ajax = abrirAjax();
		ajax.open("GET", "admin/ajax/newsletter.php?email=" + email.value, true);
		ajax.onreadystatechange = function ()
		{
		  if (ajax.readyState == 4)
		  {
			 if (ajax.status == 200)
			 {
				resultado = ajax.responseText;
				resultado = resultado.replace(/\+/g, " ");
				resultado = unescape(resultado);
				resultado = resultado.trim();
				eval(resultado);
				
				if (gravou)
				{
					alert("Seu e-mail foi cadastrado com sucesso.");
				} else
				{
					alert("O e-mail informado já consta em nosso newsletter.");
				}
				email.value = "";
			 }
		  }
		}
		ajax.send(null);
	} else 
	{
		alert("Informe um e-mail válido.");
		email.value = "";
		email.focus();
	}
}