var accounterror=0;
var pwerro = 0;
var emailerro = 0;

function validate_form(_obj, lg){
 
 
 if(lg=='pt'){
    var aux_txt = "Preenchimento obrigatório.";
  }
  if(lg=='gb'){
    var aux_txt = "Required field.";
  }
  if(lg=='fr'){
    var aux_txt = "Champ obligatoire.";
  }
  
  var errors = false;
  if(pwerro==1 || emailerro==1) errors = true;

   $('.required', _obj).each(function(){
		  	var _tag  = $(this)[0].tagName;
  			var _type = $(this).attr('type');
  			var _emai = $(this).attr('email');
        var _sel  = $(this).attr('selecta');
        var _num  = $(this).attr('num');
        var _fich  = $(this).attr('fich');
        var _defVal = $(this).attr("defaultValue");
		    
		    if($(this).val()=="" || $(this).val()==_defVal){
              
              $(this).css("border","1px solid #FF0000");
              
              $(this).bind('focus', function(ev){ $(this).css("border","1px solid #999999"); });
              
              if(errors == false) errors = true;
          }
		    
		    if(_emai == 1){
		        
  					if(verify_email($(this).val()) == false){
  		
              $(this).css("border","1px solid #FF0000");
              
  						$(this).bind('focus', function(ev){ $(this).css("border","1px solid #999999"); });
  						
    						if(errors == false){
                   errors = true;
                }
  						
  					}
  					
  				}
		
		    if(_tag == 'SELECT') {
          
    
          if (($(this).val()=="") || ($(this).val()==0)){
            
            $(this).parent().parent().css("border","1px solid #FF0000");
              
  					$(this).bind('focus', function(ev){ $(this).parent().parent().css("border","1px solid #999999"); });
						if(errors == false) errors = true;
          }
  		 
       }
       
       if(_num>0){
       
          if( isNaN( $(this).val() ) || $(this).val().length<parseInt(_num) ){
          
              $(this).css("border","1px solid #FF0000");
              $(this).bind('focus', function(ev){ $(this).css("border","1px solid #999999"); });
						  if(errors == false) errors = true;
						  
          }
          
       }
      
         if( _fich == 1  ){
         
             if($(this).val()==""){
              
              $(this).css("border","1px solid #FF0000");
              
              $(this).bind('focus', function(ev){ $(this).css("border","1px solid #999999"); });
              
              if(errors == false) errors = true;
            }
         }
		  
		});
  
  if(errors == true){	 
    alert(aux_txt);
		return false;
	} else {
		return true;		
	}
	
}


function verify_email(_mail){
	
  var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
  if (_mail.search(emailRegEx) == -1) {
		status = false;
	} else {
		status = true;
    //falta verificar email por ajax
	}
	return status;
}

