

function prepare_validation(validation_hook){

	var forms= $$('FORM.need_validation'); 

	forms.each(function(n) {
	  	
	 var form_id = n.readAttribute('id');
	 n.onsubmit= function(){ 
	 	 var has_errors = 0;
		 if(validation_hook && typeof validation_hook == "function" && !validation_hook(form_id))  has_errors++; 
		 if(!check_fields(form_id)) has_errors++;
		 return has_errors > 0 ? false : true;  
		} 
	  
	});


}


function check_fields(form){
	
	var current_form = $(form);
	
	var required_elements= current_form.select('.required:enabled'); 
	
	var errors = 0 ; 
	
	var errors_text = '' ; 
	
	var main_pointer= this;
	
	required_elements.each(function(n) {
	  	
		var type =  new String( n.readAttribute('class') );
			
				
			if(n.hasClassName("custom_validate")){
				
				if(typeof custom_validate == "function") errors += main_pointer.custom_validate(n);
							
			}			
	
			if(type.include('text') && !n.hasClassName('optional') ){
				
				var value = n.value;
				
				if( value == "" || !value.match(/^.+$/) ){ 
						
						n.addClassName('err');
						
						n.onfocus = function(){ clear_error(this); } 
						
						var err_msg = ': no puede estar vacio'; 
						
						errors_text += n.readAttribute('alt') ? '|'+n.readAttribute('alt')+err_msg : '' ; 
						
						errors++ ; 
						
						}
				
						if( type.include('mail') ){
						
						var emailFilter=/^.+@.+\..{2,}$/;
						var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	
						
						if(!(emailFilter.test(value))|| value.match(illegalChars)){ 
						
							var t =  n.previous('label');
							
							if(t) t.addClassName('validation_error');
							
							n.addClassName('validation_error');
							
							n.onfocus = function(){ clear_error(this); } 
							
							var err_msg = ': formato de email incorrecto'; 
							
							errors_text += n.readAttribute('alt') ? '|'+n.readAttribute('alt')+err_msg : '' ; 
							
							errors++ ; 
						
							}// end match mail
				
						}
					
					
				
				}// end match match text
				
					
				if(type.include('cuit') ){
				
					var value = n.value;
					
					var cuitFilter=/^[0-9\-]+/;
						var n_err = 0;
					
					if( !(cuitFilter.test(value)) ) n_err=1 ;
					
					var _class = new String(n.readAttribute('class'));
										
						var vec= Array(10);
						esCuit=false;
						var cuit = value;	
						var cuit_rearmado = "";
						
					
						
					    for (i=0; i < cuit.length; i++) {   
					        caracter=cuit.charAt( i);
					        if ( caracter.charCodeAt(0) >= 48 && caracter.charCodeAt(0) <= 57 )     {
					            cuit_rearmado +=caracter;
					        }
					    }
					    cuit=cuit_rearmado;

				        x=i=dv=0;
				        
						// Multiplico los dígitos.
				        vec[0] = cuit.charAt(  0) * 5;
				        vec[1] = cuit.charAt(  1) * 4;
				        vec[2] = cuit.charAt(  2) * 3;
				        vec[3] = cuit.charAt(  3) * 2;
				        vec[4] = cuit.charAt(  4) * 7;
				        vec[5] = cuit.charAt(  5) * 6;
				        vec[6] = cuit.charAt(  6) * 5;
				        vec[7] = cuit.charAt(  7) * 4;
				        vec[8] = cuit.charAt(  8) * 3;
				        vec[9] = cuit.charAt(  9) * 2;
				                    
				        // Suma cada uno de los resultado.
				        for( i = 0;i<=9; i++) {
				            x += vec[i];
				        }
				        
						dv = (11 - (x % 11)) % 11;
				        
						if ( dv == cuit.charAt( 10) ) {
				            esCuit=true;
				        }else{
							n_err=2;
						}
				        
				        
					
					if( n_err > 0 && value.match(/[a-zA-Z0-9]+/g) ){ 
														
						n.addClassName('err');
						
						n.onfocus = function(){ clear_error(this); } 
						
						var err_msg = '' ;
						
						switch(n_err){
						
						case 1: err_msg = 'debe completar el cuit'; break;
						case 2: err_msg = 'cuit incorrecto'; break;
						
						default : err_msg = ': cuit incorrecto';
						}
						
						errors_text += n.readAttribute('alt') ? '|'+n.readAttribute('alt')+err_msg : '' ; 
						
						errors++ ; 
						
						}
				
				}
				
				
				
					/*
				if(type.include('number') ){
				
					var value = n.value;
					
					var numberFilter=/^[0-9\.]+$/;
					

					
					var _class = new String(n.readAttribute('class'));
					var intervalRE = new RegExp("[0-9]+\-[0-9]+", "gi")
					var interval_info = _class.match(intervalRE) ; 
					
					var n_err = 0 ; 
					var interval = new Array();
					interval[0]= 0 ;
					interval[1]= 0 ;
					
					if( !(numberFilter.test(value)) ) n_err=1 ;
					
					if( interval_info !== null) {
						
						var cvalue= parseInt(value) ? parseInt(value) : 0 ;
						
						interval = interval_info[0].split(/\-/);
						
						interval[0] = interval[0] && parseInt(interval[0]) ? parseInt(interval[0]) : 0 ;
						
						interval[1] = interval[1] && parseInt(interval[1]) ? parseInt(interval[1]) : 0 ;
						
						
						if( !(interval[0] >= cvalue || interval[1] <= cvalue ) ){
						
						n_err=2;
						
						}
						
					
					}
					
					
					if( n_err > 0 ){ 
						
								
						n.addClassName('err');
						
						n.onfocus = function(){ clear_error(this); } 
						
						var err_msg = '' ;
						
						switch(n_err){
						
						case 1: err_msg = ': formato numerico incorrecto'; break;
						case 2: err_msg = ': en numero debe ser mayor a '+interval[0]+' y menor a '+interval[1]; break;
						
						default : err_msg = ': formato numerico incorrecto';
						}
						
						errors_text += n.readAttribute('alt') ? '|'+n.readAttribute('alt')+err_msg : '' ; 
						
						errors++ ; 
						
						}
				
				}
				
				*/
				
				if(type.include('choice') ){
				
				var value = n.options[n.selectedIndex].value;
				
					if( value == "" || value == "-1" ){ 
						
						n.addClassName('err');
						
						n.onfocus = function(){ clear_error(this); } 
						
						var err_msg = ': debe seleccionar una opcion'; 
						
						errors_text += n.readAttribute('alt') ? '|'+n.readAttribute('alt')+err_msg : '' ; 
						
						errors++ ; 
						
					} 
				
				}
	  	
	});
	
	//if(errors > 0 ) flash_error(errors_text);
	

	
	return (errors > 0 ? false : true ); 
	
}


function isValidField(t,k){
if(k=="email"){
	if( t.indexOf("@example.com")>-1){return false;};
	var emailFilter=/^.+@.+\..{2,}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if(!(emailFilter.test(t))||t.match(illegalChars)){return(false);}else{return (true);}
	return false
	}//end email
}



function clear_error(element){
	
	var current_element = $(element);
	
	current_element.removeClassName('err');
	
	current_element.removeClassName('validation_error');

	current_element.removeAttribute('onfocus');

}
