
// JavaScript Document
$(document).ready( function(){
	//Formulário
	$("#campobusca").focus( function(){
		if ( $(this).val() == $(this).attr('title') ){
			$(this).val('');
		}
	});
	$('#campobusca').blur( function(){
		if ( $(this).val() == '' ){
			$(this).val( $(this).attr('title') );
		}
	});
	$("textarea").focus( function(){
		if ( $(this).val() == $(this).attr('title') ){
			$(this).val('');
		}
	});
	$('textarea').blur( function(){
		if ( $(this).val() == '' ){
			$(this).val( $(this).attr('title') );
		}
	});

});




Ajax = ({
	news: function(dir){
		var status = true;
		if($('#news_nome').val() == ""){
			$('#resposta').html('Informe o nome');
			$('#news_nome').focus();
			status = false;
		}
		if($('#news_email').val() == ""){
			$('#resposta').html('Informe um email');
			$('#news_email').focus();
			status = false;
		}
		if(this.checkMail($('#news_email').val()) != true){
			$('#resposta').html('Informe um email valido');
			$('#news_email').focus();
			status = false;
		}
		if(status == true){
			if(dir == 1){
				var link = '../';
			}else{
				var link = '';
			}
			var parametros = 'news_nome='+$('#news_nome').val()+'&news_email='+$('#news_email').val();
			$.ajax({
				url: link+'ajax.php?modulo=news',
				type: 'post',
				data: parametros,
				success: function(retorno){
					alert(retorno);
				}
			})
		}
	},
	combo_produto: function(){
		var combo = $('#combo_categoria').val();
		if(combo != 000){
			var parametros = 'cat='+combo;
			$.ajax({
				url: '../ajax.php?modulo=combo_produto',
				type: 'post',
				data: parametros,
				success: function(retorno){
					if(retorno == 0){
						$('#combo_produto').html('<option>Sem produtos cadastrados.</option>');
					}else{
						$('#combo_produto').attr('disabled','');
						$('#combo_produto').html('<option value="000">Selecione o produto:</option>');
						$('#combo_produto').append(retorno);
					}
				}
			})	
		}else{
			return false;
		}
	},
	sac: function(){
		if($('#combo_area').val() == 000){
			$('#combo_area').focus();		
			alert('Informe a área');			
			return false;
		}
		if($('#sac_nome').val() == ""){
			alert('Informe o nome');
			$('#sac_nome').focus();
			return false;
		}
		if($('#sac_email').val() == ""){
			alert('Informe o email');
			$('#sac_email').focus();
			return false;
		}
		if(this.checkMail($('#sac_email').val()) != true){
			alert('Email invalido');
			$('#sac_email').focus();
			return false;
		}					
		
		if($('#sac_estado').val() == ""){
			alert('Informe o estado');
			$('#sac_estado').focus();
			return false;
		}
		if($('#sac_cidade').val() == ""){
			alert('Informe o cidade');
			$('#sac_cidade').focus();
			return false;
		}
		if($('#sac_msg').val() == ""){
			alert('Informe o mensagem');
			$('#sac_msg').focus();
			return false;
		}
		if($('#sac_telr').val() == ""){
			alert('Informe o celular');
			$('#sac_telr').focus();
			return false;
		}
		parametros = 'email='+$('#sac_email').val();
		$.ajax({
			url:'../ajax.php?modulo=validaURLemail',
			type:'post',
			data:parametros,
			success:function(retorno){
				if(retorno != 0){
					alert('URL de email inexistente.');
					$('#sac_email').focus();
					return false;
				}else{
					document.sac_form.submit();
				}
			}				
		})		
	},
	trabalhe: function(){
		if($('#trab_nome').val() == ""){
			alert('Informe o nome');
			$('#trab_nome').focus();
			return false;
		}
		if($('#trab_ciduf').val() == ""){
			alert('Informe o Cidade/estado');
			$('#trab_ciduf').focus();
			return false;
		}
		if($('#trab_area').val() == ""){
			alert('Informe o área de interesse');
			$('#trab_area').focus();
			return false;
		}
		if($('#trab_msg').val() == ""){
			alert('Informe a mensagem');
			$('#trab_msg').focus();
			return false;
		}
	},
	espacos:function(str,n){
		if (n <= 0)
		    return "";
		else if (n > String(str).length)
		    return str;
		else
		    return String(str).substring(0,n);
	},
	cep:function(){		
		$.ajax({
			url:'../ajax.php?modulo=cep',
			type:'post',
			data:'cep='+$('#sac_cep').val(),
			success:function(retorno){				
				if(retorno != 1){
					explode = retorno.split('|');
					if(explode.length == 5){
						$('#sac_bairro').val(explode[2]);
						$('#sac_cidade').val(explode[3]);
						$('#sac_end').val(explode[0]+' '+explode[1]);
						var uf = Ajax.espacos(explode[4],2);
						i = document.getElementById("sac_estado").options.length;						 
					     while(i--){			
					    	 if(document.getElementById("sac_estado").options[i].getAttribute("value") == uf){
					    		 break;					    		 
					    	 }					    	
					     }
					     document.getElementById("sac_estado").selectedIndex = i;
					}else if(explode.length == 2){						
						$('#sac_cidade').val(explode[0]);
						var uf = Ajax.espacos(explode[1],2);							
						i = document.getElementById("sac_estado").options.length;						 
					     while(i--){					    	
					    	 if(document.getElementById("sac_estado").options[i].getAttribute("value") == uf){					    		
					    		 break;					    		 
					    	 }
					     }
					     document.getElementById("sac_estado").selectedIndex = i;
					     alert('Não foi possível encontrar todos os dados com o seu CEP.\nComplete o restante para continuar.')
					     $('#sac_bairro').focus();
					}
				}else{
					alert('Cep não encontrato');
				}
			},
			beforeSend:function(){
				$('#sac_bairro').val('Carregando...');
				$('#sac_cidade').val('Carregando...');
				$('#sac_end').val('Carregando...');				 
			},
			complete:function(){
				if($('#sac_bairro').val() == 'Carregando...'){
					$('#sac_bairro').val('');
				}
				if($('#sac_cidade').val() == 'Carregando...'){
					$('#sac_cidade').val('');
				}
				if($('#sac_end').val() == 'Carregando...'){
					$('#sac_end').val('');
				}
			}
		})
	},
	checkMail: function(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;
		}
	},
	mascara: function(o,f){
		v_obj=o;
    	v_fun=f;
    	this.execmascara();
	},
	execmascara: function(){
		v_obj.value=v_fun(v_obj.value);
	},
	telefone: function(v){
		v=v.replace(/\D/g,"")            
	    v=v.replace(/^(\d\d)(\d)/g,"($1) $2")
	    v=v.replace(/(\d{4})(\d)/,"$1-$2")
	    return v
	},
	addFav : function (){
	    var url      = "http://www.hertapecalier.com.br";
	    var title    = "Hertape Calier | Saúde animal";
	    if (window.sidebar) window.sidebar.addPanel(title, url,"");
	    else if(window.opera && window.print){
	        var mbm = document.createElement('a');
	        mbm.setAttribute('rel','sidebar');
	        mbm.setAttribute('href',url);
	        mbm.setAttribute('title',title);
	        mbm.click();
	    }
	    else if(document.all){window.external.AddFavorite(url, title);}
	}
})
