/* Autentica CPF ou CNPJ*/

function validaCPF(cad,cpf) {
var x = 0;
var ncpf = '';
for (x=0; x<cpf.length; x++)
   {
   if ( (cpf.charAt(x) == "0" || cpf.charAt(x) == "1" || cpf.charAt(x) == "2" || cpf.charAt(x) == "3" || cpf.charAt(x) == "4" || cpf.charAt(x) == "5" || cpf.charAt(x) == "6" || cpf.charAt(x) == "7" || cpf.charAt(x) == "8" || cpf.charAt(x) == "9" ))
      ncpf = ncpf + cpf.charAt(x);
   }
cpf = ncpf;

if (cpf.length <=11)
   {
   	if (cad == 'CAD' )
	    {
		//cpf = document.form1.PECPF.value;
		valor = true;
		erro = new String;
		if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";	
		if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999" || cpf == "              "){
			  erro += "Numero de CPF invalido!"
		}
		var a = [];
		var b = new Number;
		var c = 11;
		for (i=0; i<11; i++){
			a[i] = cpf.charAt(i);
			if (i < 9) b += (a[i] *  --c);
		}
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
		for (y=0; y<10; y++) b += (a[y] *  c--); 
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			erro +="Digito verificador com problema!";
		}
		if (erro.length > 0){
			alert(erro);
			return false;
		}
		return true;
		}
    }
else
    {
	alert(erro);
	return false;
	}
}

	
/* Formatação para qualquer mascara */

function formatar(src, mask) 
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida) 
  {
	src.value += texto.substring(0,1);
  }
}

 /* Cria uma janela para formulários */
 
function dialog(x,alt,larg)
{
//window.showModelessDialog
//caixa = "window.showModelessDialog(x,'', 'Resizable:no; DialogHeight:630px; DialogWidth:840px; Edge:raised; Help:no; Scroll:no; Status:no; Center:yes;');";
//eval(caixa);
caixa = window.open(x,'', 'Height='+alt+', Width='+larg+', Scrollbars,  Center:yes;');
caixa.focus();
}


var req; 

function loadXMLDoc(url) 
{ 
    req = null; 
    // Procura por um objeto nativo (Mozilla/Safari) 
    if (window.XMLHttpRequest) { 
        req = new XMLHttpRequest(); 
        req.onreadystatechange = processReqChange; 
        req.open("GET", url, true); 
        req.send(null); 
    // Procura por uma versão ActiveX (IE) 
    } else if (window.ActiveXObject) { 
        req = new ActiveXObject("Microsoft.XMLHTTP"); 
        if (req) { 
            req.onreadystatechange = processReqChange; 
            req.open("GET", url, true); 
            req.send(); 
        } 
    } 
} 

function processReqChange() 
{ 
    // apenas quando o estado for "completado" 
    if (req.readyState == 4) { 
        // apenas se o servidor retornar "OK" 
        if (req.status == 200) { 
            // procura pela div id="news" e insere o conteudo 
            // retornado nela, como texto HTML 
            document.getElementById('news').innerHTML = req.responseText; 
        } else { 
            alert("Houve um problema ao obter os dados:\n" + req.statusText); 
        } 
    } 
} 

function buscarNoticias(obj) 
{ 
    loadXMLDoc("http://localhost/news.php"); 
} 

function dialoga(x,alt,larg)
{
//window.showModelessDialog
//caixa = "window.showModelessDialog(x,'', 'Resizable:no; DialogHeight:630px; DialogWidth:840px; Edge:raised; Help:no; Scroll:no; Status:no; Center:yes;');";
//eval(caixa);
caixa = window.open(x,'', 'Height='+alt+', Width='+larg+', Scrollbars,  Center:yes;');
caixa.focus();
}

function texto(txt,cat,tam)
   {	   
   
   if(txt.value.length > tam)
      {
	  txt.value = -(txt.value.length - tam);     
	  
	  var ntxt = [];
	  var i = 0;
	  for (i=0; i<tam; i++)
	     {
		 ntxt[i] = txt.value.charAt(i);
		 //alert('ok');
		 }
	  txt.value = ntxt;//*/
	  txt.value = cat.value;
	  }
   else
      {
	  cat.value = txt.value;
	  }
   
   }
   
//Funcao que conta o limite de caracteres   
   function textContador(x, y, limit) {   
      var txtarea    = document.getElementById(x);   
      var remtxtarea = document.getElementById(y);   
         
      if (txtarea.value.length > limit) {   
         //txtarea.value = txtarea.value.substring(0, limit);   
         remtxtarea.value = -(txtarea.value.length - limit);            
         document.getElementById("campostatus").innerHTML = "<span style=\'background-color:#FF0000; color:#fff; padding: 0 5 0 5; font-weight:bold\'>Limite de caracteres excedido !</span>";            
      } else {   
         //remtxtarea.value = limit - txtarea.value.length;   
         remtxtarea.value = txtarea.value.length;   
         document.getElementById("campostatus").innerHTML = "&nbsp;";   
      }   
   }   