/**
 * Classe de validação de formulários
 * @author DJ Adriano Fernandes [ Eurodance.com.br ]
 * @version 1.0
 **/
 
/**
 * Construtor da classe
 **/
validar = function() {
  
	this.invalid    = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	this.arrCampos  = new Array();
	this.campo      = "";
	this.aviso      = "";
	this.tipo	    = "";
	this.formulario = "";	
	this.erro       = "";
  
}

/**
 * Cria os métodos da classe
 **/
validar.prototype = {

  /**
   * Getters e Setters
   **/
  setCampo : function( campo ) { 
    this.campo = campo;
  },
  
  setAviso : function( aviso ) {
    this.aviso = aviso;
  },

  setTipo : function( tipo ) {
    this.tipo = tipo;
  },
  
  setFormulario : function( formulario ) {
    this.formulario = formulario;
  },  
  
  getCampo : function() {
    return this.campo;
  },
  
  getAviso : function() {
    return this.aviso;
  },

  getTipo : function() {
    return this.tipo;
  },
  
  getFormulario : function() {
    return this.formulario;
  },  

  /**
   * Função para adicionar os campos que serão validados num array
   **/
  adicionarCampo : function() {
    
    if( this.arrCampos == "" ) {
    
      this.arrCampos[ 0 ] = Array( this.campo, this.aviso, this.tipo );
      
    } else {
    
      this.arrCampos[ this.arrCampos.length++ ] = Array( this.campo, this.aviso, this.tipo );	
      
    }
    
  },
  
  /**
   * Função que valida o formulário
   **/
  validarCampo : function() { 
        
    for( i = 0; i < this.arrCampos.length; i++ ) {
      
      var campo = this.arrCampos[ i ][ 0 ];
      var aviso = this.arrCampos[ i ][ 1 ];		
      var tipo  = this.arrCampos[ i ][ 2 ];		      
      
      /**
       * Verifica se é um campo de email
       * E valida se é um email valido
       **/
      if( campo.indexOf( "Email" ) != -1 || campo.indexOf( "email" ) != -1 ) {						
      
        if( global.Dom.$( campo ).value == "" || this.invalid.test( global.Dom.$( campo ).value ) == false ){
        
          alert( aviso );
          global.Dom.$( campo ).focus();			
          return false;
          break;			
          
        }
        
      } else {
      
        /**
         * Verifica se foi especificado um tipo de campo
         **/
        if( typeof( tipo ) == 'undefined' || tipo == '' ) {
        
          if( global.Dom.$( campo ).value == "" ) {
          
            alert( aviso );
            global.Dom.$( campo ).focus();			
            return false;
            break;			
            
          }
          
        } else {
        
          switch( tipo ) {
            
            /**
             * Tipo Inteiro
             **/
            case "int":
              
              if( isNaN( global.Dom.$( campo ).value ) || parseInt( global.Dom.$( campo ).value ) == 0 ) {
                
                alert( aviso );
                global.Dom.$( campo ).focus();			
                return false;
                break;                
                
              }
              
            break;
            
            /**
			 * Tipo Checkbox
			 **/
            case "checkbox":
              
              if( this.formulario != '' ) {
				  
				this.elementos = global.Dom.$( this.formulario ).getElementsByTagName( 'input' );
				
				for( this.i = 0; this.i < this.elementos.length; this.i++ ) {
					
					if( this.elementos[ this.i ].type == 'checkbox' ) {
						
						if( this.elementos[ this.i ].checked == false ) {
							this.erro += ',true';
						} else {
							this.erro += ',false';							
						}
						
					}
					
				}
				
                if( this.erro.indexOf( 'false' ) == -1 ) {
					alert( this.aviso );				
					return false;			
				} else {
					return true;	
				}
                
              }
              
            break;            
            
          }
        
        }
        
      }
      
    }
    
    return true;
    
  }  

}