function aleatorio(inferior,superior){ 
			numPossibilidades = superior - inferior 
			aleat = Math.random() * numPossibilidades 
			aleat = Math.floor(aleat) 
			return parseInt(inferior) + aleat 
		} 	
function listrar(){
	$('table tr:even').addClass('tab');
		$('table tr').mouseover(function(){
			$(this).addClass('over');
				}).mouseout(function(){
			$(this).removeClass('over');
		});
}
function editar(){
	$('a.linkEditar').each(function(){
		$(this).html('<img src="imagens/write.png" width="18px" alt="Editar" title="Editar"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente editar ?")){
				return false;
			}			   
		});		
	});
}
function excluir(){
	$('a.linkExcluir').each(function(){
		$(this).html('<img src="imagens/delete.png" width="18px" alt="Excluir" title="Excluir"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente excluir ?")){
				return false;
			}			   
		});		
	});
}

function excluirVideo(){
	//alert('excluirVideo');
	$('a.linkExcluirVideo').each(function(){
		$(this).html('<img src="imagens/delete.png" width="18px" alt="Excluir" title="Excluir"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente excluir ?")){
				return false;
			}else{
				$.get($(this).attr('href'),function(response){
					//alert(response);
					var id = response;
					if(id > 0){
						$('#linhaVideo'+id).remove();	
					}	
				});				
				return false;
			}
		});		
	});
}
function excluirImagem(){
	//alert('excluirImagem');
	$('a.linkExcluirImagem').each(function(){
		$(this).html('<img src="imagens/delete.png" width="18px" alt="Excluir" title="Excluir"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente excluir ?")){
				return false;
			}else{
				$.get($(this).attr('href'),function(response){
					//alert(response);
					var id = response;
					if(id > 0){
						$('#linhaImagem'+id).remove();
					}	
				});				
				return false;
			}
		});		
	});
}

function download(){
	$('a.linkDownload').each(function(){
		$(this).html('<img src="imagens/download.png" width="18px" alt="Donwload" title="Download"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente fazer o Download ?")){
				return false;
			}			   
		});		
	});
}
function pagar(){
	$('a.linkPagar').each(function(){
		$(this).html('<img src="imagens/pagar2.png" width="17px" style="border:0px;" heigth="17px" alt="Pagar" title="Pagar Conta"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente Mudar o Status da Conta ?")){
				return false;
			}			   
		});		
	});
}
function finalizar(){
	$('a.linkFinalizar').each(function(){
		$(this).html('<img src="imagens/finalizar.png" width="18px" alt="Finalizar" title="Finalizar"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente Mudar o Status para Finalizado ?")){
				return false;
			}			   
		});		
	 });
}
	
function executar(){
	$('a.linkExecutar').each(function(){
		$(this).html('<img src="imagens/finalizar.png" width="18px" alt="Executado" title="Executado"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente Mudar o Status para Executado ?")){
				return false;
			}			   
		});		
   });
}
function rotear(){
	$('a.linkRotear').each(function(){		
		$(this).html('<img src="imagens/rotear.png" width="18px" alt="Rotear" title="Rotear"/>');
		$(this).click(function(){
			if(!confirm("Deseja realmente Rotear ?")){
				return false;
			}			   
		});		
	 });
}
		
function atender(){
		$('a.linkAtender').each(function(){				
			$(this).html('<img src="imagens/atender.png" width="18px" alt="Atender" title="Atender"/>');
			$(this).click(function(){
				if(!confirm("Deseja realmente Mudar o Status para Em Andamento ?")){
					return false;
				}			   
			});	
		});	
	}
function linkArquivos(){
	$('a.linkArquivos').each(function(){
		$(this).html('<img src="imagens/download.png" width="18px" alt="Arquivos" title="Arquivos"/>');			
	 });
	}
	
function linkRat(){
	$('a.linkRat').each(function(){
		$(this).html('<img src="imagens/relatorio.png" width="18px" alt="Relat&oacute;rio RAT" title="Relat&oacute;rio RAT"/>');			
	 });
	}
	
function datePicker(){
		$('input[alt="datapicker"]').each(function(){
			$(this).datepicker({
			   dateFormat: 'dd/mm/yy',
			   showOn: 'both',
			   buttonImageOnly: true,
			   buttonImage: 'calendar.gif',
			   buttonText: 'Calendario'
			   });
		});
}	
function atualizaPermissao(id_colaborador,valor){
	$('#retro'+valor).html("<img src='imagens/load.gif' />");
	$.post('permissoes_colaborador.php',{acao:'Ajax',colaborador:id_colaborador,classe:valor,rand:aleatorio(0,3000)},function(response){
					//alert(response);
					$('#retro'+valor).html(response);
					//alert('#retro'+valor);
	});
}
function validarCPF(cpf){
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     //window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  //window.alert("CPF inválido. Tente novamente.");
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 //window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     //window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }

function valida_cnpj(CNPJ) {
  erro = new String;
  if (CNPJ.length < 18) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
  if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
  if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
  }
  //substituir os caracteres que nao sao numeros
  if(document.layers && parseInt(navigator.appVersion) == 4){
  x = CNPJ.substring(0,2);
  x += CNPJ.substring(3,6);
  x += CNPJ.substring(7,10);
  x += CNPJ.substring(11,15);
  x += CNPJ.substring(16,18);
  CNPJ = x; 
  } else {
  CNPJ = CNPJ.replace(".","");
  CNPJ = CNPJ.replace(".","");
  CNPJ = CNPJ.replace("-","");
  CNPJ = CNPJ.replace("/","");
  }
  var nonNumbers = /\D/;
  if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n"; 
  var a = [];
  var b = new Number;
  var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
  for (i=0; i<12; i++){
  a[i] = CNPJ.charAt(i);
  b += a[i] * c[i+1];
  }
  if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
  b = 0;
  for (y=0; y<13; y++) {
  b += (a[y] * c[y]); 
  }
  if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
  if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
 	 erro +="Dígito verificador com problema!";
  }
  if (erro.length > 0){
 	 //alert('CNPJ inválido');
 	 return false;
  } else {
  	//alert("CNPJ válido!");
  }
 	 return true;
}

function retira_acentos2(palavra) {   
		com_acento = "áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ";   
		sem_acento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC";   
		nova="";   
		for(i=0;i<palavra.length;i++) {   
			if (com_acento.search(palavra.substr(i,1))>=0) {   
					nova+=sem_acento.substr(com_acento.search(palavra.substr(i,1)),1);   
			} else {   
					nova+=palavra.substr(i,1);   
			}   
		}   
		return nova;   
}

function retira_acentos( texto ) {    	   
			   return ((texto));
}


function VerificaData(digData){
        var bissexto = 0;
        var data = digData; 
        var tam = data.length;
        if (tam == 10) 
        {
                var dia = data.substr(0,2)
                var mes = data.substr(3,2)
                var ano = data.substr(6,4)
                if ((ano > 1900)||(ano < 2100))
                {
                        switch (mes) 
                        {
                                case '01':
                                case '03':
                                case '05':
                                case '07':
                                case '08':
                                case '10':
                                case '12':
								if  (dia <= 31){
										return true;
								}
								break                                
								case '04':              
								case '06':
								case '09':
								case '11':
								if  (dia <= 30){
										return true;
								}
								break
								case '02':
								/* Validando ano Bissexto / fevereiro / dia */ 
								if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)){ 
										bissexto = 1; 
								} 
								if ((bissexto == 1) && (dia <= 29)){ 
										return true;                             
								} 
								if ((bissexto != 1) && (dia <= 28)){ 
										return true; 
								}                       
								break                                           
                        }
                }
        }       
        //alert("A Data "+data+" é inválida!");
        return 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;
                }
}

function url_encode(str) {  
    var hex_chars = "0123456789ABCDEF";  
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/;  
    var n, strCode, hex1, hex2, strEncode = "";  
    for(n = 0; n < str.length; n++) {  
        if (noEncode.test(str.charAt(n))) {  
            strEncode += str.charAt(n);  
        } else {  
            strCode = str.charCodeAt(n);  
            hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
            hex2 = hex_chars.charAt(strCode % 16);  
            strEncode += "%" + (hex1 + hex2);  
        }  
    }  
	//alert(strEncode);    
    return strEncode;  
}  
// url_decode version 1.0  
function url_decode(str) {  
    var n, strCode, strDecode = "";  
    for (n = 0; n < str.length; n++) {  
        if (str.charAt(n) == "%") {  
            strCode = str.charAt(n + 1) + str.charAt(n + 2);  
            strDecode += String.fromCharCode(parseInt(strCode,16));  
            n += 2;  
        } else {  
            strDecode += str.charAt(n);  
        }  
    }  
	//alert(strDecode);
    return strDecode;  
} 
