//Chamada de funções
function call()
{	
	reloaded();
	exibeAlteraSenha();
	exibeCotacao();
	alerta();
	//istanciaLb();
	//$("#iso").lightBox();
	
	$("#suggests").autocomplete('ajax/getsuggest.php');

	$("#flush").click(function() {
		var ac = $("#suggests").data('autocompleter');
		if (ac && $.isFunction(ac.cacheFlush)) {
			ac.cacheFlush();
		} else {
			alert('Error flushing cache');
		}
	});
}

//Exibe cotação
function exibeCotacao()
{
	var cot = document.getElementById('cotacao').value;
	if(cot == "on")
	{
		document.getElementById('cotacao_bg').style.visibility = 'visible';
		document.getElementById('totalprodutos').innerHTML = document.getElementById('cotacao_total').value;
	}
}

//Exibe o Campo de Alterar Senha
function exibeAlteraSenha()
{
	var ns = document.getElementById('nova_senha').value;
	
	if(ns == "on")
	{
		altura_body = document.getElementById('tudo').clientHeight;
		if(altura_body> 820)
		document.getElementById('cortina').style.height = altura_body + "px";
		
		document.getElementById('cortina').style.visibility = 'visible';
		document.getElementById('alterar_senha').style.visibility = 'visible';
		
		corpo_alt = 200;
		
		var altura = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
		var largura = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
		
		var margin=(altura - corpo_alt)/2;
		if(margin<0)
		{
			document.getElementById('alterar_senha').style.marginTop = 0;
			altura_old = altura;
		}
		else
		{
			document.getElementById('alterar_senha').style.marginTop=margin+"px";
			altura_old = altura;
		}	
		
		var marginL=(largura - 400)/2;
		
		if(navigator.appName == "Microsoft Internet Explorer")
	{
		document.getElementById('alterar_senha').style.marginLeft=(marginL - (largura/2))+"px";
	}
	else
		document.getElementById('alterar_senha').style.marginLeft=marginL+"px";
	}
}

//Oculta o campo de Senha
function ocultaAlteraSenha()
{
	document.getElementById('cortina').style.visibility = 'hidden';
	document.getElementById('alterar_senha').style.visibility = 'hidden';
}

//Exibe o Campo de Aguarde
function exibeAguarde()
{	
	altura_body = document.getElementById('tudo').clientHeight;
	if(altura_body> 820)
	document.getElementById('cortina').style.height = altura_body + "px";
	
	document.getElementById('cortina').style.visibility = 'visible';
	document.getElementById('aguarde').style.visibility = 'visible';
		
	corpo_alt = 200;
		
	var altura = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
	var largura = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
		
	var margin = (altura - corpo_alt)/2;
	if(margin<0)
	{
		document.getElementById('aguarde').style.marginTop = 0;
		altura_old = altura;
	}
	else
	{
		document.getElementById('aguarde').style.marginTop=margin+"px";
		altura_old = altura;
	}	
		
	var marginL=(largura - 400)/2;
		
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		document.getElementById('aguarde').style.marginLeft=(marginL - (largura/2))+"px";
	}
	else
		document.getElementById('aguarde').style.marginLeft=marginL+"px";
		
}

//Oculta o campo aguarde
function ocultaAguarde()
{
	document.getElementById('cortina').style.visibility = 'hidden';
	document.getElementById('aguarde').style.visibility = 'hidden';
}

//Verifica reloaded
function reloaded()
{
	var pagina = document.getElementById('pagina').value;
	var campos_string = document.getElementById('campos').value;
	var campos = campos_string.split(',');
	var dados_string = document.getElementById('dados').value;
	var dados = dados_string.split(',');
	
	if(pagina != "")
	{		
		for(c = 0; c < campos.length; c++)
		{
			document.getElementById(campos[c]).value = dados[c];
		}
	}
}

//Exibe o Campo de login
function exibeLogin()
{
	altura_body = document.getElementById('tudo').clientHeight;
	if(altura_body> 820)
	document.getElementById('cortina').style.height = altura_body + "px";
	
	document.getElementById('cortina').style.visibility = 'visible';
	document.getElementById('login').style.visibility = 'visible';
	
	corpo_alt = 200;
	
	var altura = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
	var largura = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
	
	var margin=(altura - corpo_alt)/2;
	if(margin<0)
	{
		document.getElementById('login').style.marginTop = 0;
		altura_old = altura;
	}
	else
	{
		document.getElementById('login').style.marginTop=margin+"px";
		altura_old = altura;
	}	
	
	var marginL=(largura - 400)/2;
	
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		document.getElementById('login').style.marginLeft=(marginL - (largura/2))+"px";
	}
	else
		document.getElementById('login').style.marginLeft=marginL+"px";

	document.getElementById('username').focus();
}

//Oculta o campo de login
function ocultaLogin()
{
	document.getElementById('cortina').style.visibility = 'hidden';
	document.getElementById('login').style.visibility = 'hidden';
}

//Alerta Status
function alerta()
{
	var status = document.getElementById('status').value;
	
	if(status != "")
	{
		alert(status);
	}
}

//AJAX
function logar()
{
	http_request = false;

	if(window.XMLHttpRequest) 
	{ 
		// Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType) 
		{
					http_request.overrideMimeType('text/xml');
					// See note below about this line
		}
	}
	else if(window.ActiveXObject) 
	{ 
		// IE
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e)
			{
			}
		}
	}
	
	if (!http_request) 
	{
		alert('Não foi possível criar a instancia para comunicação com servidor!');
		return false;
	}
	else
	{
		http_request.onreadystatechange = loginResult(http_request);
		http_request.open('POST','?id=1',true);
		http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_request.send(dados);
	}
}

function loginResult(http_request)
{
	if(http_request.readyState == 4)
	{
		if(http_request.status == 200) 
		{
			//implementação livre para o programador!!!
	 		alert(http_request.response);
		} 
		else
		{
        	alert("Problemas tentando receber resposta do servidor");
        }
    }
}

//Categoria
function categoriaAvancar(categoria)
{	
	if(paginacao == pagina_categoria)
		getProdutos(categoria)
	else
	{
		var tamanho = "-="+(coordenadas[paginacao+1]*180)+"px";
		$("#produtos_categoria").animate({marginLeft: tamanho},1500);
		paginacao += 1;
		//$("#produtos_categoria").animate({marginLeft: "-=900px"},2000);
		//paginacao += 1;
	}
}

function categoriaVoltar()
{
	if(paginacao > 1)
	{
		var tamanho = "+="+(coordenadas[paginacao]*180)+"px";
		$("#produtos_categoria").animate({marginLeft: tamanho},1500);
		paginacao -= 1;
	}
}

function istanciaLb()
{
	for(l=0;l<indice;l++)
	{
		$('#foto'+l).lightBox();
	}
}

function sendBuscar()
{
	var produto = document.getElementById('suggests').value;
	window.location = "?id=15&q="+produto;
}

