function validaCNPJ(cnpj) {
                 CNPJ = cnpj;
                 erro = new String;
                 if (CNPJ.length < 18) erro += "É necessario 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 não são números
               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 ("/","");
               }
			  // alert(CNPJ);
               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 +="O CNPJ não e valido!";
               }
               if (erro.length > 0){
                      alert(erro);
                       return false;
               } else {
                      //alert("CNPJ valido!");
               }
               return true;
       }


function checarCPF(s){
	var i;
	s = s.replace(".","");
	s = s.replace(".","");
	s = s.replace("-","");
	
	if(s<11){return false;}
	if(s=='00000000000') {return false;}
	if(s=='11111111111') {return false;}
	if(s=='22222222222') {return false;}
	if(s=='33333333333') {return false;}
	if(s=='44444444444') {return false;}
	if(s=='55555555555') {return false;}
	if(s=='66666666666') {return false;}
	if(s=='77777777777') {return false;}
	if(s=='88888888888') {return false;}
	if(s=='99999999999') {return false;}
	
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;

	for (i = 0; i < 9; i++){d1 += c.charAt(i)*(10-i);}

	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;

	if (dv.charAt(0) != d1){return false;}
	d1 *= 2;

	for (i = 0; i < 9; i++){d1 += c.charAt(i)*(11-i);}

	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1){return false;}
	
	return true;
}


function validaLogin (frm) {
	if ((frm.login.value == "") || (frm.senha.value == "")) {
		alert("Digite o login e senha corretamente.");
		return false;
	}
	else {
		return true;
	}
}

function confirma(msg,url) {
	if (confirm(msg)) {
		document.location.href = url;
	}
}

function format(value,format)
{
	value = value.replace(/\D/g,"");
	var result="";
	
	if(format.length < value.length)
		return value;
	
	for(i=0,j=0;(i<format.length)&&(j<value.length);i++)
	{
		var ch = format.charAt(i) ;
		if(ch == '#')
		{
			result += value.charAt(j++);
			continue;
		}
		result += ch;
	}
	return result;
}

/*
	função para remover espaços no inicio e fim da string
*/
function trim(str){
	return str.replace(/^\s*|\s*$/g,"");
}

/*
	funcao de validação de email
*/

function validaEmail(email){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
    return true;
  }
  else {
	return false;
  }
}

/*
	funcao de validação de data no formato 99/99/9999
*/
function validaData(data){
	var reDate = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	
	if (reDate.test(data))
		return true;
	else
		return false;
}

// funcao que verifica se a data é realmente válida
function checkData(field) 
{
	var checkstr = "0123456789"; 
	var DateField = field; 
	var Datevalue = ""; 
	var DateTemp = ""; 
	var day; 
	var month; 
	var year; 
	var leap = 0; 
	var err = 0; 
	var i; 
	err = 0; 
	DateValue = DateField.value; 

	for (i = 0; i < DateValue.length; i++) 
	{ 
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) 
		{ DateTemp = DateTemp + DateValue.substr(i,1); } 
	} 

	DateValue = DateTemp; 

	if (DateValue.length == 6) 
	{ DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); } 

	if (DateValue.length != 8) 
	{ err = 1; } 

	year = DateValue.substr(4,4); 
	if (year == 0) 
	{ err = 1; } 

	month = DateValue.substr(2,2); 
	if ((month < 1) || (month > 12)) 
	{ err = 1; } 

	day = DateValue.substr(0,2); 
	if (day < 1) 
	{ err = 1; } 

	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) 
	{ leap = 1; } 
	
	if ((month == 2) && (leap == 1) && (day > 29)) 
	{ err = 1; } 
	
	if ((month == 2) && (leap != 1) && (day > 28)) 
	{ err = 1; } 

	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) 
	{ err = 1; } 
	
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) 
	{ err = 1; } 

	if ((day == 0) && (month == 0) && (year == 00)) 
	{ err = 1; } 
	
	//== verificando se foi encontrado algum erro
	if (err == 0) 
	{ return true; } 
	else 
	{ return false;	} 
	
}
// funcao que valida data	


/*

	funcao de validação de hora no formato 99:99
*/
function validaHora(hora){
	var reTime = /^([0-1]\d|2[0-3]):[0-5]\d$/;
	
	if (reTime.test(hora))
		return true;
	else
		return false;
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
