// JavaScript Document

// TODAS AS PÁGINAS ________________________________________________|
//
var baseSite;
baseSite = "http://www.americacartuchos.com.br/";

jQuery().ready(function(){
						$("body").fadeIn(500);
		jQuery('#navigation').accordion({
			active: false,
			header: '.head',
			event: 'click',
			fillSpace: false,
			animated: 'easeslide',
			autoheight: false
		});

	jQuery("#conteudo").equalHeights();
	var y_fixo = jQuery("#publicidade").offset().top;
	jQuery(window).scroll(function () {
	  jQuery("#publicidade").animate({
		  top: y_fixo+$(document).scrollTop()+"px"},
		  {duration:300,queue:false}
		  );
	 });
});	

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}

jQuery(function($){  
	if (document.getElementById('telefone')){
		$("#telefone").mask("(99) 9999-9999");
	}
	if (document.getElementById('cep')){
		$("#cep").mask("99999-999");
		
	}
	if (document.getElementById('cpf')){
		$("#cpf").mask("999.999.999-99");
		
	}
	if (document.getElementById('data')){
		$("#data").mask("99/99/9999");
		
	}
	if (document.getElementById('telefone1')){
		$("#telefone1").mask("(99) 9999-9999");
		
	}
	if (document.getElementById('telefone2')){
		$("#telefone2").mask("(99) 9999-9999");
		
	}
	if (document.getElementById('cnpj')){
		$("#cnpj").mask("99.999.999/9999-99");
	}
});

//busca
function enviaBusca(){
	buscaCat = $("#buscaCat").val();
	query = $("#query").val();
	location.href=baseSite+'busca/' + buscaCat + '/' + query;
}

function enviaBusca2(){
	buscaCat = $("#buscaCat").val();
	query = $("#query").val();
	document.getElementById('frmBusca').action = baseSite+'busca/' + buscaCat + '/' + query;
}

//
//___________________________________________________________________|


// CONTATO.ASP ______________________________________________________|

function validaContato(){
	if ($('#nome').val() == ''){
		alert('Favor preencher o campo Nome!');
		$('#nome').focus();
		return false;
	}else if ($('#telefone').val() == ''){
		alert('Favor preencher o campo Telefone!');
		$('#telefone').focus();
		return false;
	}else if (!checkMail($('#email1').val())){
		alert('Favor preencher um e-mail válido!');
		$('#email1').focus();
		return false;
	}else if ($('#msg').val() == ''){
		alert('Favor preencher o campo Mensagem!');
		$('#msg').focus();
		return false;
	}else{
		return true;
	}
}

//___________________________________________________________________|


// CARRINHO.ASP ______________________________________________________|

function ajaxCep(s){
	if (s.length == 9){
		if (s.indexOf("_") == -1){
			//alert('enviou: tipo = "cep", cepDestino = ' + s);
			$('#valortotalcarrinho').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
			$('#cidade').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
			$('#frete').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
			$.post(baseSite+"ajax.asp?calcFrete=s", { tipo: "cep", cepDestino: s },
				   
				   function(data){
					   //alert('retornou: ' + data);
						if (data.indexOf(";") && data.indexOf("erro") == -1){	
							data = data.split(";");
							$('#cidade').html(unescape(data[2]));
							$('#frete').html(unescape(data[1]));
							$('#valortotalcarrinho').html(unescape(data[0]));
						}else{
							$('#cidade').html("");
							$('#frete').html("");
							$('#valortotalcarrinho').html("");
							alert('Cep inválido\nPor favor, digite novamente!');
						}
					});
			
		}
	}
}

function ajaxQtd(s,ID){
	//alert('enviou: tipo = "qtd", qtdChanged = ' + s + ', ide = ' + ID);
	$('#valortotalcarrinho').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
	$('#total' + ID).html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
	$('#frete').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
	$.post(baseSite+"ajax.asp?calcFrete=s", { tipo: "qtd", qtdChanged: s, ide: ID },
		   function(data){
			   //alert('retornou: ' + data);
				if (data.indexOf(";")){
					data = data.split(";");
					$('#frete').html(unescape(data[2]));
					$('#total' + ID).html(unescape(data[1]));
					$('#valortotalcarrinho').html(unescape(data[0]));
				}
			});
}

function ajaxPresente(s,ID){
	//alert('enviou: tipo = "presente", presente = ' + s + ', ide = ' + ID);
	$('#valortotalcarrinho').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
	$.post(baseSite+"ajax.asp?calcFrete=s", { tipo: "presente", presente: s, ide: ID },
		   function(data){
			   //alert('retornou: ' + data);
					$('#valortotalcarrinho').html(data);
			});
}

function ajaxRemoveItem(ide){
	$('#valortotalcarrinho').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
	$.post(baseSite+"ajax.asp?removeItem=s", { ID: ide },
		   function(data){
			   //alert(data);
			   data = data.split(";");
				if (data[0] == "0"){
				//VAZIO
					$('#item' + ide).slideToggle(500);
					$('#fretecarrinho').slideToggle(500);
					$('#noprodutos').slideToggle(500);
				//SOBROU PRODUTO
				}else{
					$('#item' + ide).slideToggle(500);
					$('#frete').html(unescape(data[2]));
				}
				$('#valortotalcarrinho').html(data[1]);
				

			});
}


//___________________________________________________________________|

// CADASTRO.ASP ______________________________________________________|


function changeP(f){
	if (f==1){
		//Fisica
		$('#pessoaJ1').hide();
		$('#pessoaJ2').hide();
		$('#pessoaF1').fadeIn(500);
		$('#pessoaF2').fadeIn(500);
		document.getElementById('pessoa').checked=1;
	}else{
		//Jurídica
		$('#pessoaF1').hide();
		$('#pessoaF2').hide();
		$('#pessoaJ1').fadeIn(500);
		$('#pessoaJ2').fadeIn(500);
		document.getElementById('pessoa2').checked=1;
	}
}

function validaClientes(p){
	if (document.getElementById('pessoa').checked == 1){
		//alert('fisica');
		nick	 = document.getElementById('nick').value;
		nome = document.getElementById('nome').value;
		sobrenome = document.getElementById('sobrenome').value;
		cpf = document.getElementById('cpf').value;
		rg = document.getElementById('rg').value;
		data = document.getElementById('data').value;
		email = document.getElementById('email').value;
		senha = document.getElementById('senha').value;
		senha2 = document.getElementById('senha2').value;
		barras = data.split('/');
		if (barras.length == 3){
			dia = barras[0];
			mes = barras[1];
			ano = barras[2];
			resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano >= 1900));
			if (!resultado) {
				data = 'erro';
			}

		}else{
			data = 'erro';
		}
		
		if (nick == ""){
			alert('Favor preencher o campo Apelido!');
			$('#nick').focus();
			return false;
		}else if (nome == ""){
			alert('Favor preencher o campo Nome!');
			$('#nome').focus();
			return false;
		}else if (sobrenome == ""){
			alert('Favor preencher o campo Sobrenome!');
			$('#sobrenome').focus();
			return false;
		}else if (cpf.length < 14){
			alert('Favor preencher o campo CPF corretamente!');
			$('#cpf').focus();
			return false;
		}else if (rg == ""){
			alert('Favor preencher o campo RG');
			$('#rg').focus();
			return false;
		}else if (data == 'erro'){
			alert('Formato de data inválido!');
			$('#data').focus();
			return false;
		}else if (!checkMail(email)){
			alert('Favor preencher o campo E-mail corretamente!');
			$('#email').focus();
			return false;
		}else if (senha == ""){
			alert('Favor preencher o campo Senha!');
			$('#senha').focus();
			return false;
		}else if (senha != senha2){
			alert('Senhas não conferem');
			$('#senha').focus();
			return false;
		}
	}else{
		//alert('juridica');
		razao = document.getElementById('razao').value;
		nome = document.getElementById('nome2').value;
		cnpj = document.getElementById('cnpj').value;
		email = document.getElementById('email2').value;
		senha = document.getElementById('senha21').value;
		senha2 = document.getElementById('senha22').value;
		if (razao == ""){
			alert('Favor preencher o campo Razão Social!');
			$('#razao').focus();
			return false;
		}else if (nome == ""){
			alert('Favor preencher o campo Nome!');
			$('#nome2').focus();
			return false;
		}else if (cnpj.length < 14){
			alert('Favor preencher o campo CNPJ corretamente!');
			$('#cnpj').focus();
			return false;
		}else if (!checkMail(email)){
			alert('Favor preencher o campo E-mail corretamente!');
			$('#email2').focus();
			return false;
		}else if (senha == ""){
			alert('Favor preencher o campo Senha!');
			$('#senha21').focus();
			return false;
		}else if (senha != senha2){
			alert('Senhas não conferem');
			$('#senha21').focus();
			return false;
		}
	}
	// # VALIDA ENDEREÇO
	if ($('#cep').val() == ''){
		alert('Favor preencher o campo Cep!');
		$('#cep').focus();
		return false;
	}else if ($('#endereco').val() == ''){
		alert('Favor preencher o campo Endereço!');
		$('#endereco').focus();
		return false;
	}else if ($('#numero').val() == ''){
		alert('Favor preencher o campo Número!');
		$('#numero').focus();
		return false;
	}else if ($('#bairro').val() == ''){
		alert('Favor preencher o campo Bairro!');
		$('#bairro').focus();
		return false;
	}else if ($('#cidade').val() == ''){
		alert('Favor preencher o campo Cidade!');
		$('#cidade').focus();
		return false;
	}else if ($('#telefone1').val() == ''){
		alert('Favor preencher o campo Telefone 1!');
		$('#telefone1').focus();
		return false;
	//}else if ($('#referencia').val() == ''){
		//alert('Favor preencher o campo Referência!');
		//$('#referencia').focus();
		//return false;
	}else{
		return true;
	}
}


function ajaxCepCad(s){
	if (s.length == 9){
		if (s.indexOf("_") == -1){
			//alert('enviou: tipo = "cep", cepDestino = ' + s);
			$('#endereco').val('Carregando...');
			$('#bairro').val('Carregando...');
			$('#cidade').val('Carregando...');
			$('#estadoCampo').html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
			$.post(baseSite+"ajax.asp?cadastro=s", { cep: s },
				   function(data){
					   //alert('retornou: ' + data);
						data = data.split(";");
						if (data[0] != "" && data[1] != "" && data[2] != ""){	
							$('#endereco').val(unescape(data[0]));
							$('#bairro').val(unescape(data[1]));
							$('#cidade').val(unescape(data[2]));
							$('#estadoCampo').html(unescape(data[3]));
						}else{
							$('#endereco').val('');
							$('#bairro').val('');
							$('#cidade').val('');
							$('#estadoCampo').html(unescape(data[3]));
							alert('Cep inválido\nPor favor, digite novamente!');
						}
					});
			
		}
	}
}



//___________________________________________________________________|


// DEFAULT.ASP ______________________________________________________|

function validaNews(){
	if ($('#nomeNews').val() == ''){
		alert('Favor preencher o campo Nome!');
		$('#nomeNews').focus();
	}else if (!checkMail($('#emailNews').val())){
		alert('Favor preencher o campo E-mail corretamente!');
		$('#emailNews').focus();
	}else{
		$('#formNews').submit();
	}
		
}

//___________________________________________________________________|


// IDENTIFICACAO.ASP ______________________________________________________|

function onSenhaFocus(){
	document.getElementById("compra2").checked = 1;
}

function onCepFocus(){
	document.getElementById("compra1").checked = 1;
}

function onCompra1Focus(){
	$('#cep').focus();
}

function onCompra2Focus(){
	$('#senha').focus();
}

function onEmailFocus(){
	document.getElementById("compra2").checked = 1;
}

function validaIdentificacao(){
	if ($('#compra2:checked').val() != null){
		if (!checkMail($('#email').val())){
			alert("Favor preencher o campo E-mail corretamente!");
			$('#email').focus();
			return false;
		}else if ($('#senha').val() == ''){
			alert("Favor preencher o campo Senha!");
			$('#senha').focus();
			return false;
		}else{
			return true;
		}
	}else{
		if ($('#cep').val() == ''){
			alert('Favor preencher o campo CEP corretamente!');
			$('#cep').focus();
			return false;
		}else{
			return true;
		}
	}
}

//___________________________________________________________________|


// CONFIRMAÇÃO.ASP ______________________________________________________|

function selecionaPagamento(){
	if (document.getElementById('pagUol').checked){
		document.getElementById('formUol').submit();
	}else{
		document.getElementById('formFaturado').submit();
	}
}

//___________________________________________________________________|

// RECARREGUE.ASP ______________________________________________________|

function mudaMarca_mudaModelos(s){
	$("#divModelos").html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
	$.post("ajax.asp?caso=modelos", { idMarca: s }, function(data){
													$("#divModelos").html(data);
													 });
}

//___________________________________________________________________|

function mudaModelo_pedeRecarga(s){
	location.href = baseSite + "busca2.asp?id=" + s;
	
}

// CATEGORIAS.ASP ______________________________________________________|

function esqueci(){
	if (!checkMail($("#emailLogin").val())){
		alert("Preencha o campo email corretamente e clique em Esqueci minha senha!");
		$("#emailLogin").focus();
	}else{
		ema = $("#emailLogin").val();
		$("#emailLogin").val("Aguarde...");
		$.post("ajax.asp?caso=esqueci", { email: ema }, function(data){
														$("#emailLogin").val("");
														if (data == "1"){
															final = "Sua senha foi enviada para seu email com sucesso!";
														}else{
															final = "Email não cadastrado!"
														}
														alert(final);
														 });
	}
}

//___________________________________________________________________|


// CONFIRMAÇÃO2.ASP ______________________________________________________|

function ajaxRec(preco,qtd){
	$("#preco").fadeOut(200);
	setTimeout(function(){
		$("#preco").html('<div style="font-family:verdana; font-size:12px; color:#FF0000; font-weight:bold;">Carregando...</div>');
		$("#preco").fadeIn(200);
	},200)
	$.post("ajaxRec.asp", { preco: preco, qtd: qtd }, function(data){
		setTimeout(function(){													   
			$("#preco").fadeOut(200);
			setTimeout(function(){
				$("#preco").html(data);
				$("#preco").fadeIn(200);
			},200);
		},200);
	});
}

//___________________________________________________________________|

