/*---------------------------
	COMPRA PREMIADA
---------------------------*/

function compraPremiadaIndique()
{
	var nome = false;
	var mail = false;
	
	with(document.compra_premiada)
	{
		for (x=1; x<=4; x++)
		{
			var strNome = eval("nome_"+x);
			var strMail = eval("email_"+x);
			
			if (strNome.value != "" && strMail.value != "")
			{
				nome = true;
				mail = true;
			}
		}
		
		if (nome != false && mail != false)
		{
			submit();
		}
		else
		{
			alert('Você deve preencher ao menos uma indicação.');
		}
	}
}


/*---------------------------
	VERIFICA CUPOM
---------------------------*/

function verificaCupom()
{
	doc = document.getElementById('cupom');
	location.href = "acoes.php?acao=verifica_cupom&cupom="+doc.value;
}


/*---------------------------
	ATUALIZA CESTA
---------------------------*/

function atualizaCesta()
{
	document.carrinho.submit();
}


/*---------------------------
	FINALIZA COMPRA
---------------------------*/

function finalizarPedido()
{
	location.href = "acoes.php?acao=verifica_sessao";
}


/*---------------------------
	PERMITE DIGITOS
---------------------------*/

function IsNumeric(sText,sDigitos)
{
	var ValidChars = sDigitos;
	var IsNumber   =  true;
	var Char;
	
	for (i=0; i<sText.length && IsNumber == true; i++) 
	{ 
	Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}



/*---------------------------
	VALIDA ANTES DA COMPRA
---------------------------*/

function validaAntesCompra()
{
	with(document.compra)
	{
		if (tamanho.value == ""){
			alert("Selecione o tamanho desejado.");
			tamanho.focus();
			return;
		}
		if (cor.value == ""){
			alert("Selecione a cor desejada.");
			cor.focus();
			return;
		}
		submit();
	}
}



/*---------------------------
	CALCULO SEDEX
---------------------------*/

function calculoSedex()
{
	with(document.calculo_sedex)
	{
		if (cep1.value == ""){
			alert("Preencha os 5 primeiros digitos de seu cep.");
			cep1.focus();
			return;
		}
		if (IsNumeric(cep1.value,"0123456789") == false){
			alert("O campo cep deve conter somente números.");	
			cep1.focus();
			return;
		}
		if (cep2.value == ""){
			alert("Preencha os 3 digitos restantes de seu cep.");
			cep2.focus();
			return;
		}
		if (IsNumeric(cep2.value,"0123456789") == false){
			alert("O campo cep deve conter somente números.");	
			cep2.focus();
			return;
		}
		submit();
	}
}


/*---------------------------
	VALIDA CADASTRO
---------------------------*/

function enviaCadastro()
{
	with(document.cadastro)
	{
		if (nome.value == "" || nome.value < 3){
			alert("Preencha seu nome.");
			nome.focus();
			return;
		}
		if (email.value == "" || email.value < 10){
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}	
		if (email.value.indexOf("@",0) < 0 || email.value.indexOf(".")<0){
			alert("E-mail inválido, formato requerido: seunome@provedor.com.br");
			email.focus();
			return;
		}
		if (cpf.value == ""){
			alert("Você deve preencher o campo cpf.");	
			cpf.focus();
			return;
		}
		if (!isCpf(cpf.value)){
			alert("O cpf digitado é inválido.");
			cpf.focus();
			return;
		}
		if (rg.value != ""){
			if (IsNumeric(rg.value,"0123456789") == false){
				alert("O campo rg deve conter somente números."+rg.value);	
				rg.focus();
				return;
			}
		}
		if (dia.value == ""){
			alert("Preencha o dia do seu nascimento.");
			dia.focus();
			return;
		}	
		if (mes.value == ""){
			alert("Preencha o mês do seu nascimento.");
			mes.focus();
			return;
		}	
		if (ano.value == ""){
			alert("Preencha o ano do seu nascimento.");
			ano.focus();
			return;
		}	
		if (sexo.value == ""){
			alert("Selecione seu sexo.");
			sexo.focus();
			return;
		}
		if (senha_pr.value == "" || senha_pr.value < 5){
			alert("Você deve digitar uma senha de acesso, a mesma deve conter de 5 a 10 caracteres.");
			senha_pr.focus();
			return;
		}
		if (senha_ok.value != senha_pr.value){
			alert("A confirmação de senha não confere com a senha digitada.");
			senha_ok.focus();
			return;
		}	
		if (cep.value == ""){
			alert("Preencha o cep de sua rua.");
			cep.focus();
			return;
		}
		if (IsNumeric(cep.value,"0123456789-") == false){
			alert("O campo cep deve conter somente números.");	
			cep.focus();
			return;
		}
		if (tipo.value == ""){
			alert("Selecione o tipo de endereço.");
			tipo.focus();
			return;
		}
		if (endereco.value == ""){
			alert("Preencha seu endereço. O mesmo poderá ser usado para entrega de futuros pedidos.");
			endereco.focus();
			return;
		}
		if (numero.value == ""){
			alert("Preencha o número de sua residência.");
			numero.focus();
			return;
		}
		if (IsNumeric(numero.value,"0123456789") == false){
			alert("O campo numero deve conter somente números.");	
			numero.focus();
			return;
		}
		if (bairro.value == ""){
			alert("Preencha o bairro onde reside.");
			bairro.focus();
			return;
		}
		if (cidade.value == ""){
			alert("Preencha a cidade onde reside.");
			cidade.focus();
			return;
		}
		if (estado.value == ""){
			alert("Selecione o estado onde reside.");
			estado.focus();
			return;
		}
		if (ddd_1.value == "" || ddd_1.value < 3){
			alert("Preencha o código de área de sua região.");
			ddd_1.focus();
			return;
		}	
		if (IsNumeric(ddd_1.value,"0123456789") == false){
			alert("O campo ddd deve conter somente números.");
			ddd_1.focus();
			return;
		}
		if (telefone_1.value == "" || telefone_1.value < 8){
			alert("Preencha seu telefone.");
			telefone_1.focus();
			return;
		}	
		if (IsNumeric(telefone_1.value,"0123456789") == false){
			alert("O campo telefone deve conter somente números.");
			telefone_1.focus();
			return;
		}
		if (ddd_2.value != "" || telefone_2.value != ""){
			if (ddd_2.value == "" || ddd_2.value < 3){
				alert("Preencha o código de área de sua região.");
				ddd_2.focus();
				return;
			}	
			if (IsNumeric(ddd_2.value,"0123456789") == false){
				alert("O campo ddd deve conter somente números.");
				ddd_2.focus();
				return;
			}
			if (telefone_2.value == "" || telefone_2.value < 8){
				alert("Preencha seu telefone.");
				telefone_2.focus();
				return;
			}	
			if (IsNumeric(telefone_2.value,"0123456789") == false){
				alert("O campo telefone deve conter somente números.");
				telefone_2.focus();
				return;
			}
		}
		submit();
	}
}


/*---------------------------
	VALIDA CONTATO
---------------------------*/

function enviaContato()
{
	with(document.contato)
	{
		if (nome.value == "" || nome.value < 3){
			alert("Preencha seu nome.");
			nome.focus();
			return;
		}
		if (email.value == "" || email.value < 10){
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}	
		if (email.value.indexOf("@",0) < 0 || email.value.indexOf(".")<0){
			alert("E-mail inválido, formato requerido: seunome@provedor.com.br");
			email.focus();
			return;
		}		
		if (ddd.value == "" || ddd.value < 3){
			alert("Preencha o código de área de sua região.");
			ddd.focus();
			return;
		}	
		if (IsNumeric(ddd.value,"0123456789") == false){
			alert("O campo ddd deve conter somente números.");
			ddd.focus();
			return;
		}
		if (telefone.value == "" || telefone.value < 8){
			alert("Preencha seu telefone.");
			telefone.focus();
			return;
		}	
		if (IsNumeric(telefone.value,"0123456789") == false){
			alert("O campo telefone deve conter somente números.");
			telefone.focus();
			return;
		}
		if (assunto.value == ""){
			alert("Selecione o assunto de seu interesse.");
			assunto.focus();
			return;
		}
		if (mensagem.value == ""){
			alert("Você deve escrever uma mensagem.");
			mensagem.focus();
			return;
		}
		submit();
	}
}


/*---------------------------
	VALIDA TROCA/DEVOLUÇÃO
---------------------------*/

function enviaTroca()
{
	with(document.troca)
	{
		if (cpf.value == ""){
			alert("Você deve preencher o campo cpf.");	
			cpf.focus();
			return;
		}
		if (!isCpf(cpf.value)){
			alert("O cpf digitado é inválido.");
			cpf.focus();
			return;
		}
		if (numero_pedido.value == ""){
			alert("Você deve preencher o campo número do pedido.");	
			numero_pedido.focus();
			return;
		}
		if (IsNumeric(numero_pedido.value,"0123456789") == false){
			alert("O campo número do pedido deve conter somente números.");
			numero_pedido.focus();
			return;
		}
		if (nota_fiscal.value == ""){
			alert("Você deve preencher o campo número da nota fiscal.");	
			nota_fiscal.focus();
			return;
		}
		if (IsNumeric(nota_fiscal.value,"0123456789") == false){
			alert("O campo número da nota fiscal deve conter somente números.");
			nota_fiscal.focus();
			return;
		}
		if (referencia.value == ""){
			alert("Você deve informar a referência do(s) produtos(s).");	
			referencia.focus();
			return;
		}
		if (ddd.value == "" || ddd.value < 3){
			alert("Preencha o código de área de sua região.");
			ddd.focus();
			return;
		}	
		if (IsNumeric(ddd.value,"0123456789") == false){
			alert("O campo ddd deve conter somente números.");
			ddd.focus();
			return;
		}
		if (telefone.value == "" || telefone.value < 8){
			alert("Preencha seu telefone.");
			telefone.focus();
			return;
		}	
		if (IsNumeric(telefone.value,"0123456789") == false){
			alert("O campo telefone deve conter somente números.");
			telefone.focus();
			return;
		}
		if (tipo.value == ""){
			alert("Selecione o tipo de solicitação.");
			tipo.focus();
			return;
		}
		if (mensagem.value == ""){
			alert("Você deve escrever uma mensagem.");
			mensagem.focus();
			return;
		}
		submit();
	}
}



/*---------------------------
	VALIDA LOGIN CARRINHO
---------------------------*/

function carrinhoIdent()
{
	with (document.login)
	{
		if (email.value == "" || email.value < 10){
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}	
		if (email.value.indexOf("@",0) < 0 || email.value.indexOf(".")<0){
			alert("E-mail inválido, formato requerido: seunome@provedor.com.br");
			email.focus();
			return;
		}
		if (tipo[0].checked == true && cepDest.value == ""){
			alert("Preencha seu cep");
			cep.focus();
			return;
		}
		if (tipo[1].checked == true && passw.value == ""){
			alert("Preencha sua senha");
			passw.focus();
			return;
		}
		submit();	
	}
}



/*---------------------------
	VALIDA LOGIN ADMIN
---------------------------*/

function adminIdent()
{
	with (document.login)
	{
		if (email.value == "" || email.value < 10){
			alert("Preencha seu e-mail.");
			email.focus();
			return;
		}	
		if (email.value.indexOf("@",0) < 0 || email.value.indexOf(".")<0){
			alert("E-mail inválido, formato requerido: seunome@provedor.com.br");
			email.focus();
			return;
		}
		if (passw.value == ""){
			alert("Preencha sua senha");
			passw.focus();
			return;
		}
		submit();	
	}
}


/*---------------------------
	FINALIZA COMPRA
---------------------------*/

function finalizaCompra()
{
	cep_cadastro = document.getElementById("cep_cadastro");
	
	with (document.entrega)
	{
		if (outro_endereco.value == "S")
		{
			if (o_endereco.value == ""){
				alert("Preencha seu endereço. O mesmo poderá ser usado para entrega de futuros pedidos.");
				o_endereco.focus();
				return;
			}
			if (o_numero.value == ""){
				alert("Preencha o número de sua residência.");
				o_numero.focus();
				return;
			}
			if (IsNumeric(o_numero.value,"0123456789") == false){
				alert("O campo numero deve conter somente números.");	
				o_numero.focus();
				return;
			}	
			if (o_bairro.value == ""){
				alert("Preencha o bairro onde reside.");
				o_bairro.focus();
				return;
			}			
			if (o_cep1.value == ""){
				alert("Preencha o cep de sua rua.");
				o_cep1.focus();
				return;
			}
			if (IsNumeric(o_cep1.value,"0123456789") == false){
				alert("O campo cep deve conter somente números.");	
				o_cep1.focus();
				return;
			}
			if (o_cep2.value == ""){
				alert("Preencha a 2ª parte do cep de sua rua.");
				o_cep2.focus();
				return;
			}
			if (IsNumeric(o_cep2.value,"0123456789") == false){
				alert("O campo cep deve conter somente números.");	
				o_cep2.focus();
				return;
			}
			if (o_cidade.value == ""){
				alert("Preencha a cidade onde reside.");
				o_cidade.focus();
				return;
			}
			if (o_estado.value == ""){
				alert("Selecione o estado onde reside.");
				o_estado.focus();
				return;
			}
		} else {
			if (cep_cadastro.value == ""){
				alert("Atenção, seu cep encontrasse em branco em nossa base de dados. É importante que você o atualize imediatamente. Clique em OK para ser redirecionado a página de atualização de seu cadastro.");
				location.href = 'home.php?pagina=adm_cadastro&titulo=MEU CADASTRO';
				return;	
			}
		}
		submit();
	}
}


/*---------------------------
	EFETUA PAGAMENTO
---------------------------*/

function efetuaPagto()
{
	with (document.pagamento)
	{
		if (bradesco_tipo[0].checked == false && bradesco_tipo[1].checked == false){
			alert("Selecione a forma de pagamento.");
			return;	
		}
		submit();
	}
}



/*---------------------------
	ATUALIZA ENDEREÇO
---------------------------*/

function atualizaDadoEntrega()
{
	var tc  = document.getElementById("total_compra").value;
	var pt  = document.getElementById("peso_total").value;
	var p1  = document.getElementById("o_cep1").value;
	var p2  = document.getElementById("o_cep2").value;
	var cep = p1+p2;
	acao_endereco.location = "acoes.php?acao=outro_endereco&cep="+cep+"&total_compra="+tc+"&peso_total="+pt;
}



/*---------------------------
	GERA BOLETO 2ª VIA
---------------------------*/

function geraBoleto(id_pedido,id_cadastro,id)
{
	boleto.location = "adm_boleto.php?id_pedido="+id_pedido+"&id_cadastro="+id_cadastro+"&id="+id;
}




function prosseguir(id,ent){
	if(ent == ''){
			alert('Você deve selecionar a forma de entrega do pedido.');
		} else{
			window.open('confirmacao.php?id='+id+'&entrega='+ent,'conteudo','scrolling=no');
		}
}