//Retorna o objeto
function $(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}

//Função para ocultar um objeto
function oculta(id)
{
	$(id).style.display = "none";
}

//Função para mostrar um objeto
function exibe(id)
{
	$(id).style.display = "block";
}


//Funções para setas (Fotos)
function validaSetas() {
	
	$('imgsProdutos').onload = function () {
		$('status').innerHTML = "FOTOS";
	}
	
	$('imgsProdutos').onload = function () {
		$('status').innerHTML = "FOTOS";
	}
	
	//setaEsq
	if(cont>0) {
		$('setaEsq').src = 'imgs/setas/setaEv.gif';
		$('setaEsq').style.cursor = 'pointer';
	} else {
		$('setaEsq').src = "imgs/setas/setaE.gif";
		$('setaEsq').style.cursor = 'default';
	}
	//setaDir
	if(cont>=tam-1) {
		$('setaDir').src = 'imgs/setas/setaD.gif';
		$('setaDir').style.cursor = 'default';
	} else {
		$('setaDir').src = "imgs/setas/setaDv.gif";
		$('setaDir').style.cursor = 'pointer';
	}
	
}

//Seta Esquerda
function mouseDownE() {
	if(cont!=0) {
		var obj = $('setaEsq');
		obj.src='imgs/setas/setaEvb.gif'
	}
}
function mouseClickE() {
	if(cont!=0) {
		var obj = $('setaEsq');
		obj.src='imgs/setas/setaEv.gif'
		cont--;
		$('imgsProdutos').src = imgs[cont];
		$('status').innerHTML = "CARREGANDO...";
	}
	validaSetas();
}

//Seta Direita
function mouseDownD() {
	if(cont<tam-1) {	
		var obj = $('setaDir');
		obj.src='imgs/setas/setaDvb.gif'
	}
	
}
function mouseClickD(obj) {
	if(cont<tam-1) {
		var obj = $('setaDir');
		obj.src='imgs/setas/setaDv.gif';
		cont++;
		$('imgsProdutos').src = imgs[cont];
		$('status').innerHTML = "CARREGANDO...";		
	}
	validaSetas();
}

function abreGaleria(id, pos) {

   var width = 600;
   var height = 600;
   
   var left = (screen.width - width)/2;
   var top = 0;
   window.open("galeria.php?id="+id+"&pos="+pos,"_blank", 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}	

function galeriaFotos(table, pos)  {

   var width = 600;
   var height = 600;
   
   var left = (screen.width - width)/2;
   var top = 0;
   window.open("galeria_fotos.php?table="+table+"&pos="+pos,"_blank", 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}	
function abreJanela(url, width, height, janela) {

   var width = width;
   var height = height;
   
   var left = (screen.width - width)/2;
   var top = (screen.height - height)/2;
   window.open(url,janela, 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}
function getRequest ()
{
    var xmlhttp = null;
    
    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch(ee)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E)
            {
                xmlhttp = false;
            }
        }
    }
    
    return xmlhttp;
}
function abreAjax(pg, local ,loader)
{	
    var pagina = new Ajax();
	if(loader)
		pagina.setLoader($(loader));
		
	pagina.loadContent(pg,$(local));
}

function returnNum (id) {
	var valor = id.value;
	var nNum = "";
	for(i=0;i<valor.length;i++) {
		if(!isNaN(valor.substring(i, i+1)) && valor.substring(i, i+1)!=" ")
			nNum += valor.substring(i, i+1);
	}
	id.value = nNum;
}
//Funções carrinho de compras
function setaQuantidade(pos, qtd) {
	
	var nNum = "";
	for(i=0;i<qtd.length;i++) {		
		if(!isNaN(qtd.substring(i, i+1)))
			nNum += qtd.substring(i, i+1);
	}
	if(nNum=="") nNum = 1;
	qtd = nNum;
	if(qtd==0) {
		menosItem();
	}
	$('loading_carrinho').style.display = 'block';
	
	abreAjax("carrinho.php?pos="+pos+"&qtd="+qtd,"carrinho_de_compras");
}
var itemIdPos = 0
function removeItem(pos) {
  abreAjax("carrinho.php?pos="+pos+"&act=remove","carrinho_de_compras");  
}

function calculaFrete() {
	var codigo_tipo = $('tipo_frete').value;
	var origem= '';
	
	if(codigo_tipo == 3) {
		// Transportadora
		origem = $('estado').value;
	} else {
		// Sedex/PAC
		origem = $('cep_origem').value;
	}
	
	abreAjax("carrinho.php?origem="+origem+"&act=frete&tf="+codigo_tipo,"carrinho_de_compras");
}


function buscaCalc() {
	abreAjax("https://pagseguro.uol.com.br/Parcelamento.aspx","AjaxPanel1$RBS_Holder");
}


function trocaAba(id, id2) {
		$(id2).style.display = "none";
		$('img1_'+id2).src = "imgs/aba/left2.gif";
		$('bg_'+id2).style.backgroundImage = "url('imgs/aba/bg2.gif')";
		$('texto_'+id2).style.color = 'black';
		$('img2_'+id2).src = "imgs/aba/right2.gif";
	
		$(id).style.display = "block";
		$('img1_'+id).src = "imgs/aba/fim.gif";
		$('bg_'+id).style.backgroundImage = "url('imgs/aba/bg.gif')";
		$('texto_'+id).style.color = 'white';
		$('img2_'+id).src = "imgs/aba/fim2.gif";

}
function inputProx(id, id2) {
	var aux = $(id).maxLength;
	if($(id).value.length==aux) {		
		$(id2).focus();
	}
}
function validaCep()	{
	if($('cep').value=="") {
		$('msg').innerHTML = "Digite seu CEP!";
		$('cep').focus();
		return false;
	}
	return true;
}
function validaCadastro() {
	if(validaCep()) {		
		if(validaEmail($('email'))) {
			$('erro_cep').style.display = "none";
		} else {
			$('msg').innerHTML = "E-mail Inválido!";
			$('erro_cep').style.display = "block";
			return false;
		}
	} else {
		$('erro_cep').style.display = "block";	
		return false;
	}
}
function cepVal(valor) {
	var pagina = new Ajax();
	pagina.loadContent('valida_cep.php?cep='+valor,$('cidade_uf'));
}
function corrigeCep(campo) {
	var valor = $(campo).value;
	valor = valor.replace("-","");
	var nValor = "";
	
	for(i=0;i<valor.length;i++) {
		if(i<=9) {
			var num = valor.substring(i, i+1);
			nValor += num;
			if(i==4)
				nValor += "-";
		}
	}
	$(campo).value = nValor;
	
}
function mudaCor(id, cor) {
	abreAjax("carrinho.php?act=cor&id="+id+"&id_cor="+cor,"carrinho_de_compras");	
}
function teclaEnter(e, fct)  {
	if(e.keyCode==13) {
		eval(fct);	
	}
}

function teclaReal (e)  {
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	if(e.keyCode!=9 && e.keyCode!=40 && e.keyCode!=39 && e.keyCode!=38 && e.keyCode!=37) {
		if(keynum !=8) {
			keychar = String.fromCharCode(keynum);
			numcheck = /\d/;		
			return numcheck.test(keychar);	
		}
	}
}

function restrito() {
   var login = document.getElementById('username').value;
   var senha = document.getElementById('password').value;
   
   var width = 650;
   var height = 550;
   var left = (screen.width - width)/2;
   var top = 0;
   var janela = 'restrito';
   var src = "../institucional/galeriaUsers.php?login="+encodeURI(login)+"&senha="+encodeURI(senha);
   var win = window.open(src,janela, 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');	
}

function getRepresentante(sigla)
{
	abreAjax('get_representantes.php?nome=' + sigla , 'div_representantes')
}

function mudaDivFrete(obj) {
	if(obj.value == '3') {
		$('div_estados').style.display = 'block';
		$('div_cep').style.display = 'none';
		$('descr_frete').innerHTML = 'Estado:';
	} else {
		$('cep_origem').value = '';
		$('div_estados').style.display = 'none';
		$('div_cep').style.display = 'block';
		$('descr_frete').innerHTML = 'CEP:';
		$('div_cep').select();
	}
}
function validaPagamento(tipo) {
	var obj = jQuery("input[name='pagamento_" + tipo + "']").is(":checked");
	if(!obj) {
		alert('Selecione uma condição de pagamento.');
	} else {
		jQuery('#form_pagamento_' + tipo).submit();
	}	
	return false;
}