/* Bibliotecas Predecessoras
    (*) miscLib.js
	     - addEvent
*/
var cor_primaria  = '#FEF5CA';

Date.prototype.Dtos = function()
{
  var dd = this.getDate();
  var mm = this.getMonth() + 1;
  var yy = this.getFullYear();
	
  return( [ yy , ( mm < 10 ? '0' : '' ) + mm , ( dd < 10 ? '0' : '' ) + dd ].join( '' ) );
}

Array.prototype.hasEmptyElements = function()
{
  for( var i = 0 ; i < this.length ; i++ )
   if( ( this[ i ] == '' ) || ( this[ i ] == null ) || ( this[ i ] == undefined ) )
	  return( true );
			
   return( false );
}

String.prototype.isNumber = function()
{
  return( !isNaN( this.split( '.' ).join( '' ).split( ',' ).join( '' ).split( '-' ).join( '' ) ) );
}

String.prototype.isDate = function()
{
  if( this.split( '/' ).length != 3 ) return( false );

   var dd = Number( this.split( '/' )[ 0 ] );
   var mm = Number( this.split( '/' )[ 1 ] );
   var yy = Number( this.split( '/' )[ 2 ] );
   var dt = new Date( yy , mm -1 , dd  );
	
  return( [ yy , ( mm < 10 ? '0' : '' ) + mm , ( dd < 10 ? '0' : '' ) + dd ].join( '' ) == dt.Dtos() );
}

String.prototype.isMail = function()
{
 var test;
 var pt1    = this.split( '@' );
 var valid  = '.-_@';
		
	if( ( pt1.length != 2 ) || ( pt1[ 0 ].length == 0 ) || ( pt1[ 1 ].length == 0 ) ) 
		return( false );
	else
	{
	  for( var i = 0 ; i < valid.length - 1 ; i++ )
	  {
		if( pt1[ 0 ].split( valid.charAt( i ) ).hasEmptyElements() ) return( false );
		if( pt1[ 1 ].split( valid.charAt( i ) ).hasEmptyElements() ) return( false );
	   }			
	}
	for( var i = 0 ; i < this.length ; i++ )
	{
	  var charac = this.toUpperCase().charCodeAt( i );
	  
		if( valid.indexOf( String.fromCharCode( charac ) ) == -1 )
		if(!( ( ( charac >= 65 ) && ( charac <= 90 ) ) || ( ( charac >= 48 ) && ( charac <= 57 ) ) ) ) 
		   return( false );
	}	
	
  return( true );
}

String.prototype.isCNPJ = function()
{
 var b = [6,5,4,3,2,9,8,7,6,5,4,3,2], c = this;

  if((c = c.replace(/[^\d]/g,"").split("")).length != 14) return ( false );
  for(var i = 0, n = 0; i < 12; n += c[i] * b[++i]);
  if(c[12] != (((n %= 11) < 2) ? 0 : 11 - n)) return ( false );
  for(var i = 0, n = 0; i <= 12; n += c[i] * b[i++]);
  if(c[13] != (((n %= 11) < 2) ? 0 : 11 - n)) return ( false );

  return ( true );
}

function fnc_controla_campos()
{
  for( var j = 0; j <  document.forms.length; j++ ) 
  { 
    var formulario = eval( document.forms[ j ] );
	for( var i = 0; i < formulario.length; i++ )
	{	// adicionar evento na foco do campo	
		addEvent( formulario.elements[ i ] , 'focus' , setBackgroundColor , false );
		// adicionar evento na blur do campo
	   addEvent( formulario.elements[ i ] , 'blur'   , clearBackgroundColor , false );
	}	
  }
}	

function fnc_verifica_tipo( p_campo )
{
  if( !p_campo.readOnly && ( p_campo.type != 'button' || p_campo.type != 'hidden' ) ) return( false );
																						   
 return( true );																						   
}

function fnc_verifica_form( form )
{
 var showMessage = function(p_msg, p_campo ){ alert( p_msg ); /*if( tip ) tip.show( p_msg );*/ p_campo.focus();  return; }
 var temSecao    = ( fnc_verifica_form.arguments.length == 2 )?true:false;
 var secao	     = ( temSecao )?fnc_verifica_form.arguments[ 1 ]:'';
 var mensagem    = '';

 for( var i = 0; i < form.length; i++ )
 {
  var campo 	= form.elements[ i ];
  var tipo  	= campo.getAttribute('tipo');
  var requerido = campo.getAttribute('obrigatorio');
  var display	= campo.getAttribute('display');
  var erro	    = false ;
  
  if ( requerido == 1 )
  { // validacao simples 
	if ( ( temSecao && secao == campo.getAttribute('secao') ) || !temSecao )
    {
	 if ( !campo.getAttribute('value') ) 
	 {
		  mensagem =  'O campo ' + display + ' ' + String.fromCharCode( 233 ) + '  requerido.';  
		  return( showMessage( mensagem , campo  ) );		
	 } 
    }
	/* 
	     U R G E  N T E  !!! 
	  revisar e corrigir rotina de 
	  verificação de tipo de dados 
	  
	 if( tipo && campo.getAttribute('value') ) 
	 { 
		   mensagem = 'O valor do campo ' + display  + ' deve ser ';
		   
		   if( tipo == 'numerico' && !campo.getAttribute('value').isNumber() ) 
		   {
			 erro 	  = true;
			 mensagem += ' num' + String.fromCharCode( 233 ) + 'rico.';
		   } 
				
		   if( tipo == 'data' 	 && !campo.getAttribute('value').isDate()   ) 
		   {
			erro 	  = true;
			mensagem += ' uma data v' + String.fromCharCode( 225 ) + 'lida.';
		   }
		   
		   if( tipo == 'email'    && !campo.getAttribute('value').isMail()   )
		   { 
			 erro 	   = true;
			 mensagem += ' um e-mail v'+ String.fromCharCode( 225 )+'lido.';
		   }
			  
		   if( tipo == 'cnpj' 	 && !campo.getAttribute('value').isCNPJ()   ) 
		   {
			 erro 	   = true;
			 mensagem += 'um CNPJ v' + String.fromCharCode( 225 ) + 'lido.';
		   }
	
		 if( erro ) 
		 {
		  return( showMessage( mensagem , campo  ) );
		 }
		 
		 return( true );		
	 } 	 
	 */
	
   }
  }
 return ( true );
}
