/***************************************************************************************************************************/
/* Funções Globais...                                                                                                      */
/***************************************************************************************************************************/
function PopUp(url, nome, largura, altura, sroll)
{
	var esq     = (screen.width - largura) / 2;
	var top    = (screen.height - altura) / 2;
	var janela = window.open(''+ url +'',''+ nome +'','width=' + largura + ',height=' + altura + ',top=' + top + ',left=' + esq + ',scrollbars='+ sroll +'');
	if (janela == null)
	{
		window.alert('POPUP BLOQUEADA.\n\nIdentificamos que você possui um bloqueador de popup, configure-o\npara aceitar popups de nosso site.\n\nObrigado!');
	}
}

function validaEmail(email)
{
	var exp = /^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$/;
	if (!exp.test(email))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validarBusca()
{
	if (document.formbusca.palavra.value == '')
	{
		window.alert('Por favor, digite pelo menos uma palavra que você deseja buscar.');
		document.formbusca.palavra.focus();
		return false;
	}
}

function keyNumero(evento, obj) // onkeypress="javascript: return keyNumero(event, this)"
{
	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var str   = obj.value;

	if ((tecla < 48 || tecla > 58) && (tecla != 13))
	{
		return false;
	}

	return true;
}

function mascaraEntrada(campo, mascara, e) // onkeypress="javascript: return mascaraEntrada(this, '##/##/####', event);"
{
	var tecla = (window.Event) ? e.which : e.keyCode;
	var valorCampo = campo.value;
	var seps = ".-,:;/";
	var estaMascara = mascara.substring(valorCampo.length, valorCampo.length + 1);
	var proxMascara = mascara.substring(valorCampo.length + 1, valorCampo.length + 2);
	var ret = false;
	if ( tecla == 8 || tecla == 9 || tecla == 13 || tecla == 0 ) { return true; } // BackSpace, TAB ou ENTER
	if ( tecla > 47 && tecla < 58 && ( estaMascara == "#" || estaMascara == "?" ) == true ) { ret = true; } // Números
	if ( tecla > 96 && tecla < 123 && ( estaMascara == "@" || estaMascara == "?" ) == true ) { ret = true; } // Letras
	if ( tecla > 64 && tecla < 91 && ( estaMascara == "@" || estaMascara == "?" ) == true ) { eet = true; }
	if ( ret == true )
	{
		campo.value = campo.value + String.fromCharCode(tecla);
		if ( seps.indexOf(proxMascara) > -1 )
		{
			campo.value = campo.value + proxMascara;
		}
	}
	return false;
}
function mascaraEntradaCep(objeto, sMask, evtKeyPress) // onkeypress="javascript:return mascaraEntrada(this,'99999-999',event);"
{
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if (document.all)         { nTecla = evtKeyPress.keyCode; }
	else if (document.layers) { nTecla = evtKeyPress.which; }
	else                      { nTecla = evtKeyPress.which; if (nTecla == 8) { return true; } }

	sValue = objeto.value;

	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen)
	{
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		if (bolMask)
		{
			sCod += sMask.charAt(i);
			mskLen++;
		}
		else
		{
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		i++;
	}

	objeto.value = sCod;

	if (nTecla != 8)
	{
		if (sMask.charAt(i-1) == "9")
		{
			return ((nTecla > 47) && (nTecla < 58));
		}
		else { return true; }
	}
	else { return true; }
}

/***************************************************************************************************************************/
/* Funções da Página: montar_pc.asp                                                                                        */
/***************************************************************************************************************************/
function moeda(valor)
{
	valor = valor.toString();
	len   = valor.length;

	inteiro  = valor.substring(0, len - 2);
	inteiro  = (inteiro == '') ? '0' : inteiro;
	centavos = valor.substring(len - 2, len);
	centavos = (centavos == '0') ? '00' : centavos;

	len = inteiro.length;

	if (len == 4) inteiro = inteiro.substring(0, 1) + '.' + inteiro.substring(1, len);
	if (len == 5) inteiro = inteiro.substring(0, 2) + '.' + inteiro.substring(2, len);
	if (len == 6) inteiro = inteiro.substring(0, 3) + '.' + inteiro.substring(3, len);
	if (len == 7) inteiro = inteiro.substring(0, 1) + '.' + inteiro.substring(1, 4) + '.' + inteiro.substring(4, len);

	return( 'R$ '+ inteiro +','+ centavos );
}

function recalcular()
{
	var itens = document.form_montarpc.totalItens.value;
	var valor = 0;

	for(i = 0; i < itens; i++)
	{
		total_campo = document.getElementById('valor' + i).innerHTML;
		total_campo = total_campo.replace('.', '');
		total_campo = total_campo.replace(',', '.');
		total_campo = eval(total_campo);
		valor += total_campo;
	}

	document.getElementById('valor_total').value = moeda(Math.round(valor * Math.pow(10,2)));
}

function mostraValor(valor, caixa)
{
	if (valor == '')
	{
		preco = '0,00';
	}
	else
	{
		itens = valor.split('|');
		preco = itens[1];
	}

	document.getElementById(caixa).innerHTML = preco;
	recalcular();
}

function reiniciar()
{
	if (window.confirm('Você realmente deseja reiniciar a montagem de seu computador?'))
	{
		window.location.href = 'montar_pc.asp';
	}
}

function visualizar(campo)
{
	valor = document.form_montarpc[campo].value;
	if (valor != '')
	{
		itens = valor.split('|');
		janela = window.open('produtos_info.asp?produto=' + itens[0]);
		if (janela == null)
		{
			window.alert('POPUP BLOQUEADA.\n\nIdentificamos que você possui um bloqueador de popup, configure-o\npara aceitar popups de nosso site.\n\nObrigado!');
		}
	}
	else
	{
		window.alert('Por favor, selecione o produto...');
	}
}


/***************************************************************************************************************************/
/* Funções da Página: carrinho.asp                                                                                         */
/***************************************************************************************************************************/
function removerProduto(posicao)
{
	if (window.confirm('Você realmente deseja remover o produto do carrinho?'))
	{
		window.location.href = 'carrinho.asp?acao=remover&posicao='+ posicao +'';
	}
}

function atualizar()
{
	document.formAtualizarCart.submit();
}

function limparCarrinho()
{
	if (window.confirm('Você realmente deseja excluir todos os produtos do seu carrinho de compras?'))
	{
		window.location.href = 'carrinho.asp?acao=limpar';
	}
}

function validarFrete()
{
	if (document.formfrete.forma_entrega.value == '')
	{
		window.alert('Por favor, selecione a forma de entrega dos produtos.');
		document.formfrete.forma_entrega.focus();
		return false;
	}
	if (document.formfrete.cep.value.length != 9)
	{
		window.alert('Por favor, digite um CEP válido.');
		document.formfrete.cep.focus();
		return false;
	}
}

function msgLimitePesoSedex()
{
	window.alert('O limite máximo de peso para o Sedex é de 30 Kg, e sua compra excedeu esse limite. Por favor, selecione outra forma de pagamento e faça o cálculo!');
}

function validarPedido()
{
	if (document.formPedido.erroFrete.value == 'limitePesoSedex')
	{
		window.alert('O peso limite para a entrega através de Sedex é de 30 kg. O peso de seu pedido ultrapassou esse valor.\n\nPor favor, selecione outra forma de entrega e refaça o cálculo.');
		document.formfrete.forma_entrega.focus();
		return false;
	}
	if ((document.formPedido.pedido_entrega.value == '') || (document.formPedido.pedido_cep.value == ''))
	{
		window.alert('Por favor, faça o cálculo da taxa de entrega antes de prosseguir.');
		document.formfrete.forma_entrega.focus();
		return false;
	}
	if ((document.formPedido.forma_pagamento[0].checked == false) && (document.formPedido.forma_pagamento[1].checked == false))
	{
		window.alert('Por favor, selecione a forma de pagamento antes de prosseguir.');
		return false;
	}
}


/***************************************************************************************************************************/
/* Funções da Página: cadastro.asp                                                                                         */
/***************************************************************************************************************************/
function tipoPessoa(tipo)
{
	if (tipo == 'F')
	{
		document.getElementById('tabCPF').style.display  = '';
		document.getElementById('tabRG').style.display   = '';
		document.getElementById('tabCNPJ').style.display = 'none';
		document.getElementById('tabIE').style.display   = 'none';
	}
	else if (tipo == 'J')
	{
		document.getElementById('tabCPF').style.display  = 'none';
		document.getElementById('tabRG').style.display   = 'none';
		document.getElementById('tabCNPJ').style.display = '';
		document.getElementById('tabIE').style.display   = '';
	}
}

/***************************************************************************************************************************/
/* Funções da Página: fale_conosco.asp                                                                                     */
/***************************************************************************************************************************/
function validarContato()
{
	if (document.formcontato.nome.value == '')
	{
		window.alert('Por favor, digite seu nome.');
		document.formcontato.nome.focus();
		return false;
	}
	if (validaEmail(document.formcontato.email.value) == false)
	{
		window.alert('Por favor, digite seu e-mail corretamente.');
		document.formcontato.email.focus();
		return false;
	}
	if (document.formcontato.assunto.value == '')
	{
		window.alert('Por favor, digite o assunto da mensagem.');
		document.formcontato.assunto.focus();
		return false;
	}
	if (document.formcontato.mensagem.value == '')
	{
		window.alert('Por favor, digite a mensagem.');
		document.formcontato.mensagem.focus();
		return false;
	}
}
