// JavaScript Document
//-- Adicionar las fuinciones de trim a la clase String 

String.prototype.ltrim = function ()
{
    return this.replace( /^\s*/, "" );
}

String.prototype.rtrim = function ()
{
    return this.replace( /\s*$/, "" );
}
    
String.prototype.trim =    function ()
{
    return this.rtrim().ltrim();
}

var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
function is_valid_email( email){
   return emailPattern.test(email.trim()); 
}

function is_valid_file( fileInput ){
	tipo =  fileInput.getAttribute('fexts');
	if( tipo !=null ){
		 tipo =',' + tipo + ',' ; 
		 
		 strF = fileInput.value.trim();
		pos_punto =strF .lastIndexOf('.');
		ext = strF.substring(pos_punto+1) ;
		 
		return  tipo.toLowerCase().indexOf(','+ ext.toLowerCase() +',') >=0; 
		
	}
	else return true ;
	
}

function esValido(entryForm, header_error ,footer_error, extraMessage  ){
	elementos = entryForm.elements ;
	errores = '';
	
	firstElement = null ;
 

	for ( i = 0; i <  elementos.length; i++) {
	
     if ( elementos[i].getAttribute('requerido')!=null && ( elementos[i].type == "text" ||  elementos [i].type == "file" ) ) 
         if( elementos[i].value.trim() =='' ){
			 errores += elementos[i].getAttribute('validacion') +'\r\n';
			 if (firstElement ==null )
			    firstElement =elementos[i] ;
			 
	      }
    
   }// for 
  
  if(entryForm.email.value.trim() !='' && ! is_valid_email(entryForm.email.value.trim()) ) {
	  errores += extraMessage.mail_incorrecto  +'\r\n';
	}
  if(entryForm.nom_archivo.value.trim() !='' && ! is_valid_file( entryForm.nom_archivo ) ){
	  errores += extraMessage.tipo_de_fichero_incorrecto +'\r\n';
  }
   
   if( errores =='')
     return true ;
	else 
	{
	 alert( header_error  + 
		   '\r\n--------------------------------------------------------------------\r\n' +
		    errores + 
		   '\r\n--------------------------------------------------------------------\r\n'+
		    footer_error 
		   );
	 if( firstElement != null)
	  firstElement.focus();
	  
	  return false ;
		
	}
	
}
