// JavaScript Document
function validar()
{erro=0;erro1=0;

	for(i=0;i<document.getElementsByTagName('input').length;i++){//validar input
		if(document.getElementsByTagName('input').item(i).getAttribute('obrigatorio')!=null)//se indicar se é obrigatorio ou nao
		{document.getElementsByTagName('input').item(i).style.backgroundColor='#FFF';
			if(document.getElementsByTagName('input').item(i).getAttribute('obrigatorio')==1)//se for obrigatorio
			{
				
				if(document.getElementsByTagName('input').item(i).value==""){
					document.getElementsByTagName('input').item(i).style.backgroundColor='#C00';
					erro=1;
				}
			}
			erro1=validarTipo(document.getElementsByTagName('input').item(i));
			
			if(erro1==1){
			  erro=1;	
			}
		}
		
	}
	return erro;
}

function validarTipo(obj){//valida os tipos
errot=0;
 if(obj.value!=""){//verifica se valor esta preenchido
	if(obj.getAttribute('tipo')=='texto'){//valida texto
		errot=0;
	}
	if(obj.getAttribute('tipo')=='inteiro'){//valida numeros inteiros
		 if(validaDecimal(obj)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 
			 }
	}
	if(obj.getAttribute('tipo')=='real'){//valida numeros reais
		 if(validaReal(obj)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 
			 }
	}
	
	if(obj.getAttribute('tipo')=='data'){//valida datas
		if(validaData(obj.value)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	if(obj.getAttribute('tipo')=='tempo'){//valida TEMPO
		if(validaTempo(obj.value)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	if(obj.getAttribute('tipo')=='datatempo'){//valida TEMPO
		if(validaDataTempo(obj.value)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	if(obj.getAttribute('tipo')=='fone'){//valida TELEFONE
		if(validaFone(obj.value)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	if(obj.getAttribute('tipo')=='cpf'){//valida CPF
		if(validaCpf(obj)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	if(obj.getAttribute('tipo')=='cnpj'){//valida CNPJ
		if(validaCnpj(obj)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	if(obj.getAttribute('tipo')=='email'){//valida Email
		if(validaEmail(obj.value)==false){
		  obj.style.backgroundColor='#C00';
		  errot=1;
		 }else{
			 obj.style.backgroundColor='#FFF';
			 }
	}
	
	
  }//se estiver preenchido
	return errot;
}

function validaDecimal(obj){//valida numeros inteiros
 valora=obj.value;
 contador=new Number();
 contador=0;
  val=valora;
  valora=valora.split(',');
  for(j=0;j<valora.length;j++){
  val=val.replace(',','.');
  }
  valora= val;
 
 if(isNaN(parseFloat(valora))){
   return false;
 }
 else{
	valora=valora.split('.');
   if(valora.length > 1){
	 return false;
   }
   else{
     return true;
   }
 }
}
 
function validaReal(obj){//valida valores com .
  valora=new String(obj.value);
  contador=new Number();
  contador=0;
  val=valora;
  valora=valora.split(',');
  for(j=0;j<valora.length;j++){
  val=val.replace(',','.');
  }
  valora= val;

 obj.value=valora;
 if(isNaN(parseFloat(valora))){
   return false;
 }
 else{
	valora=valora.split('.');
   if(valora.length > 2){
	 return false;
   }
   else{
     return true;
   }
 }
}

function validaData(date){//valida se é data
 this.date=new String(date);
 this.arr_date=new Array();
 this.arr_date=this.date.split("/");
 this.returning=false;
 this.DaysMonth=Array(31,28,31,30,31,30,31,31,30,31,30,31);
 
 try{
   if(this.date.length==10){
      if(this.date.charAt(2)=="/" && this.date.charAt(5)=="/"){
         this.Day=this.arr_date[0];
	     this.Month=this.arr_date[1];
	     this.Year=this.arr_date[2];
         this.Day=new Number(this.Day);
	     this.Month=new Number(this.Month);
	     this.Year=new Number(this.Year);
         this.Day=parseInt(this.Day);
	     this.Month=parseInt(this.Month);
	     this.Year=parseInt(this.Year);	
	 
	     if((this.Year % 4)==0 && ((this.Year % 100)!=0 || (this.Year % 400)==0)){
	       this.DaysMonth[1]=29; // Fevereiro em ano bissexto
	     }
	   
	     if(this.Year > 0){
	        if(this.Month > 0 && this.Month <= 12){
			   if(this.Day > 0 && this.Day <= parseInt(this.DaysMonth[eval(this.Month-1)])){
			     this.returning=true;
			   }
			   else{
				 this.returning=false;  
			   }
			}
			else{
			  this.returning=false;
			}
	     }
	     else{
	       this.returning=false;
	     }	 
	  }
	  else{
        this.returning=false;
	  }   
   }
   else{
     this.returning=false;
   }
 }
 catch(e){
   this.returning=false; 
 }
 
 return this.returning;  
}


function validaTempo(time){
 this.time=new String(time);
 this.arr_time=new Array();
 this.arr_time=this.time.split(":");
 this.returning=false;
 
 try{
   if(this.time.length==8){
      if(this.time.charAt(2)==":" && this.time.charAt(5)==":"){
        this.hora=this.arr_time[0];
	    this.minuto=this.arr_time[1]
	    this.segundo=this.arr_time[2];
        this.hora=new Number(this.hora);
	    this.minuto=new Number(this.minuto);
	    this.segundo=new Number(this.segundo);	
		
		if(this.hora >= 0 && this.hora <= 23){
		   if(this.minuto >= 0 && this.minuto <= 59){
		      if(this.segundo >= 0 && this.segundo <= 59){
			    this.returning=true; 
			  }
			  else{
		        this.returning=false;	  
			  }
		   }
		   else{
		     this.returning=false; 
		   }
		}
		else{
	      this.returning=false; 
		}
	  }
	  else{
	    this.returning=false; 	 	
	  }
   }
   else{
     this.returning=false;
   }
 }
 catch(e){
   this.returning=false; 	 
 }
 
 return this.returning;
}

function validaDataTempo(data){
	data=data.split(' ');

	if(data.length==1){
	ret=false;	
	}

	if(validaData(data[0])==false)
	{
		ret=false
	}else{if(validaTempo(data[1])==false)
	{ret=false;}else{ret=true;}}
	
	return ret;
}
function validaFone(fone)
{
 this.fone=new String(fone);
 this.exp_fone=new RegExp(/^\(\d{2}\)\d{4}-\d{4}$/);
 this.returning=false;
 
 try{
   if(this.fone.length==13){
      if(this.exp_fone.test(this.fone)){
		this.returning=true;
	  }
	  else{
	    this.returning=false;
	  }
   }
 }
 catch(e){
   this.returning=false;	 
 }
 
 return this.returning;	
}

function validaCpf(id_object){
 this.returning=true;
  this.CPF=new String(id_object.value);
  
  for(x=0;x<this.CPF.length;x++){
    this.CPF=this.CPF.replace(".","");
    this.CPF=this.CPF.replace("-","");	
  }
  
  if((id_object).value != "" && (id_object).value.charAt(3)=="." && (id_object).value.charAt(7)=="." && (id_object).value.charAt(11)=="-"){
    try{
      this.POSICAO;
      this.I;
      this.SOMA;
      this.DV;
      this.DV_INFORMADO;

      this.DIGITO=new Array(10);
      this.DV_INFORMADO=this.CPF.substr(9,2);

      for(this.I=0;this.I<=8;this.I++){
        this.DIGITO[this.I]=this.CPF.substr(this.I,1);
      }

      this.POSICAO=10;
      this.SOMA=0;
  
      for(this.I=0;this.I<=8;this.I++){
        this.SOMA=eval(this.SOMA + this.DIGITO[this.I] * this.POSICAO);
        this.POSICAO=eval(this.POSICAO - 1);
      }
  
      this.DIGITO[9]=(this.SOMA % 11);
  
      if(this.DIGITO[9] < 2){
        this.DIGITO[9]=0;
      }
      else{
        this.DIGITO[9]=eval(11 - this.DIGITO[9]);
      }

      this.POSICAO=11;
      this.SOMA=0;
  
      for(this.I=0;this.I<=9;this.I++){
        this.SOMA=eval(this.SOMA + this.DIGITO[this.I] * this.POSICAO);
        this.POSICAO=eval(this.POSICAO - 1);
      }
  
      this.DIGITO[10]=(this.SOMA % 11);
  
      if(this.DIGITO[10] < 2){
        this.DIGITO[10]=0;
      }
      else{
        this.DIGITO[10]=eval(11 - this.DIGITO[10]);
      }

      this.DV=eval(this.DIGITO[9] * 10 + this.DIGITO[10]);
  
      if(this.DV != this.DV_INFORMADO){
        this.returning=false;
      }
  }
    catch(e){
      this.returning=false;
    }
  }
  else{
    this.returning=false;
  }
  
  return this.returning;	
}


function validaCnpj(id_object){
 this.CNPJ=(id_object).value;
  this.returning=true;
  
  if(this.CNPJ.length < 18){
    this.returning=false;
  }
  
  if((this.CNPJ.charAt(2) != ".") || (this.CNPJ.charAt(6) != ".") || (this.CNPJ.charAt(10) != "/") || (this.CNPJ.charAt(15) != "-")){
    this.returning=false;
  }

  if(document.layers && parseInt(navigator.appVersion)==4){
    this.x=this.CNPJ.substring(0,2);
    this.x+=this.CNPJ.substring(3,6);
    this.x+=this.CNPJ.substring(7,10);
    this.x+=this.CNPJ.substring(11,15);
    this.x+=this.CNPJ.substring(16,18);
    this.CNPJ=this.x; 
  }
  else{
    this.CNPJ=this.CNPJ.replace(".","");
    this.CNPJ=this.CNPJ.replace(".","");
    this.CNPJ=this.CNPJ.replace("-","");
    this.CNPJ=this.CNPJ.replace("/","");
  }

  this.nonNumbers=/\D/;
  
  if(this.nonNumbers.test(this.CNPJ)){
	this.returning=false; 
  }
  
  this.a=[];
  this.b=new Number();
  this.c=[6,5,4,3,2,9,8,7,6,5,4,3,2];

  for(i=0;i<12;i++){
    this.a[i]=this.CNPJ.charAt(i);
    this.b+=this.a[i]*this.c[i+1];
  }
  
  if((this.x=this.b % 11) < 2){
    this.a[12]=0;
  }
  else{
	this.a[12]=eval(11-this.x);
  }
  
  this.b=0;
  for(y=0;y<13;y++){
    this.b+=eval(this.a[y]*this.c[y]); 
  }

  if((this.x=this.b % 11) < 2){
	this.a[13]=0;
  }
  else{
	this.a[13]=eval(11-this.x);
  }
  
  if((this.CNPJ.charAt(12) != this.a[12]) || (this.CNPJ.charAt(13) != this.a[13])){
    this.returning=false;
  }
    return this.returning;
 }
 
function validaEmail(mail){
  var txt = mail;
  if ((txt.length != 0) && ((txt.indexOf("@") < 1) || (txt.indexOf('.') < 7)))
  {
    return false;
	
  }else{return true;}


}


