 function asociaLabels(f)
 {
   var labels = f.getElementsByTagName( "label" );
	 for(i=0; i < labels.length; i++ )
	 {
		lab = labels[i];
		campo = lab.htmlFor;
		//alert(campo);
		if(campo != '' )
		{//alert(campo);
//alert(campo);
campo = document.getElementById( campo );

		if(null === campo ){
			 alert("Error en campo "+lab.htmlFor);
			 return(false);
		}



			if(campo.label != lab)
			{
				//campo = document.getElementsByName( campo );
				campo.clase = campo.className;
				lab.clase = lab.className;
				lab.texto = lab.innerHTML;
				
				if(campo)
				{campo.label = lab;
				}
				else
				{campo.label = null;
				}
			}
		}
	 }
 }

function trim( strValue ) {
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
  return strValue;
}

function val_blanco(c)
{

  if(c.type == 'radio')
  {
    //buscar que opcion esta chequeada
    azz = document.getElementsByName(c.name); 
    for(t=0;t<azz.length;t++)
    {
      p = azz[t];
      if( p.checked){
       return('');
      }
    }
    return(MSG_VACIO);
  }
  else
  {
    if(trim(c.value) == '' ){return(MSG_VACIO);}
  }
  return('');
}

function  val_entero( c ) {
  var strValue = trim(c.value);
  if (strValue.length > 0)
  {
   var objRegExp  = /(^-?\d\d*$)/;
   if ( !objRegExp.test(strValue) )
   {return(MSG_ENTERO);}
  }
  return('');
}


function  val_numero( c ) {
  var strValue = trim(c.value);
  if (strValue.length > 0)
  {
   var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
   if ( !objRegExp.test(strValue) )
   {return(MSG_NUMERO);}
  }
  return('');
}


function  val_menor( c, n ) {
  ret = val_numero(c);
  if( ret.length == 0)
  {
   if ( (c.value)*1 < (n*1) || c.value.length == 0){ ret = ''; }
   else{ ret = MSG_MENOR + n;}
  }
  return(ret);
}

function  val_mayor( c, n ) {
  ret = val_numero(c);
  if( ret.length == 0)
  {
   if ( (c.value)*1 > (n*1) || c.value.length == 0){ ret = ''; }
   else{ ret = MSG_MAYOR + n;}
  }
  return(ret);
}

function  val_email( c ) {
  var strValue = trim(c.value);
  if (strValue.length > 0)
  {
   //var objRegExp  =  /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
   var objRegExp  = /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;
   //alert(1);
   if ( !objRegExp.test(strValue) )
   {return(MSG_EMAIL);}
  }
  return('');
}

function parteFecha (cadena, formato, busca)
 {
  respuesta = '';
  formato = ' '+formato.toUpperCase();
  busca = busca.toUpperCase() ;
  posicion = formato.indexOf(busca,0);
  if(posicion)
  { posicion--;
    respuesta = cadena.substring(posicion, posicion + busca.length);
  }
  return(respuesta);
}

function val_fecha(c, formato)
{
  var strValue = trim(c.value);
  var retorno = '';
  
  if (strValue.length > 0)
  {
     formato = formato.toUpperCase();
     var dia = parteFecha(strValue, formato, 'DD');
     var mes = parteFecha(strValue, formato, 'MM');
     var ano = parteFecha(strValue, formato, 'YYYY');    
	  var hoy=new Date();
	  hoy.setYear(ano);
	  hoy.setMonth(mes-1);
	  hoy.setDate(dia);
	  
		
		var separador = formato.replace('YYYY','').replace('MM','').replace('DD', '');
		var temporal = strValue.replace(ano,'');
		temporal = temporal.replace(mes,'');
		temporal = temporal.replace(dia,'');
		
		if(document.all)//ie
		{//alert('ie');
			 if( ano<2000)
			 {ano = ano-1900;
			 }
		}
		else//firefox
		{
			//alert('ns');
			ano = ano-1900;
		}
		
		//alert(hoy.getYear());
		//alert(ano);
		if(hoy.getYear() != ano || hoy.getMonth() != mes-1 || hoy.getDate() != dia || separador != temporal)
		{ retorno = ( MSG_FECHA + formato);
		}
  }
  return(retorno);
}

function val_hora(c, formato)
{
  var strValue = trim(c.value);
  var retorno = '';
  
  if (strValue.length > 0)
  {
     formato = formato.toUpperCase();
	var hora = parteFecha(strValue, formato, 'HH');
	var minuto = parteFecha(strValue, formato, 'MI');
	var segundo = parteFecha(strValue, formato, 'SS');  
  
  	  var hoy=new Date();
	  hoy.setHours(hora);
	  hoy.setMinutes(minuto);
	  hoy.setSeconds(segundo);
	  
		var separador = formato.replace('HH', '').replace('MI','').replace('SS','');
		var temporal = strValue.replace(hora,'').replace(minuto,'').replace(segundo,'');
		if(hoy.getHours() != hora || hoy.getMinutes() != minuto || hoy.getSeconds() != segundo || separador != temporal)
		{ retorno = ( MSG_HORA + formato);
		}
  }
  return(retorno);
}

function val_fechaHora(c, formato)
{
  var strValue = trim(c.value);
  formato = formato.toUpperCase();  
  var retorno = '';
  if(strValue.length > 0)
  {
    var valores = Array();
    valores[0]='';
    valores[1]='';
    valores = strValue.split(' ');
    formatos = formato.split(' ');
    if(valores.length >1)
	{	
	//alert( valores.length );
		if( valores[0].length >0 && valores[1].length > 0)
		{
		var aux = new Date();
		aux.value = valores[0];
		ret1 = val_fecha(aux,formatos[0]);
		
		//var hora = new Date();
		aux.value  = valores[1];
		ret2 = val_hora(aux,formatos[1]);
		
		retorno = ret1+ret2;
		}
		else
		{retorno='error';
		}
	}
	else
	{retorno='error';
	}
	
    if(retorno.length > 0){retorno = MSG_FECHAHORA+formato};
	
  }
  return(retorno);
}

function  val_alfa( c ) {
  var strValue = trim(c.value);
  if (strValue.length > 0)
  {
   var objRegExp  =  new RegExp('^[a-zA-Z]+$');
   if ( !objRegExp.test(strValue) )
   {return(MSG_ALFA);}
  }
  return('');
}

function  val_alfaNum( c ) {
  var strValue = trim(c.value);
  if (strValue.length > 0)
  {
   var objRegExp  =  new RegExp('^[a-zA-Z0-9]+$');
   if ( !objRegExp.test(strValue) )
   {return(MSG_ALFANUM);}
  }
  return('');
}

function  val_igual( c, campo2 ) {
  var strValue = trim(c.value);

  if (strValue.length > 0)
  {
   //campo = document.getElementById( campo2 );
   //getElementsByName
   campo = document.getElementById( campo2 );
   if(campo.value != strValue)
   {return(MSG_IGUAL);}
   }
  return('');
}

function  val_minLong( c, longitud ) {
  var strValue = trim(c.value);
  if (strValue.length < longitud)
  {
   return(MSG_MINLONG+longitud);
  }
  return('');
}

function  val_maxLong( c, longitud ) {
  var strValue = trim(c.value);
  if (strValue.length > longitud)
  {
   return(MSG_MAXLONG+longitud);
  }
  return('');
}

 function validaCampo(c, validacion)
 {
    mensaje = '';
	validaciones = validacion.split('|');
	for(i=0; i < validaciones.length; i++)
	{
	  parametros = validaciones[i].split(',');
	  
		switch (parametros[0])
		{
		case 'blank':
		  vret = val_blanco(c);
		  break
		case 'entero':
		  vret = val_entero(c);
		  break
		case 'numero':
		  vret = val_numero(c);
		  break
		case 'menor':
		  vret = val_menor(c, parametros[1]);
		  break
		case 'mayor':
		  vret = val_mayor(c, parametros[1]);
		  break
		case 'email':
		  vret = val_email(c);
		  break
		case 'fecha':
		  vret = val_fecha(c, parametros[1]);
		  break
		case 'hora':
		  vret = val_hora(c, parametros[1]);
		  break
		case 'fechaHora':
		  vret = val_fechaHora(c, parametros[1]);
		  break
		case 'alfa':
		  vret = val_alfa(c);
		  break
		case 'alfaNum':
		  vret = val_alfaNum(c);
		  break
		case 'igual':
		  vret = val_igual(c, parametros[1]);
		  break
		case 'minLong':
		  vret = val_minLong(c, parametros[1]);
		  break
		case 'maxLong':
		  vret = val_maxLong(c, parametros[1]);
		  break
		default:
		} 
	  if (vret.length > 0)	
	  {
	  mensaje = (mensaje + "<br>" + vret);
	  }
	}
	
		 if(null === c.label ){
			 alert("error en campo"+c.name);
			}	
			 
	if ( mensaje.length > 0)
	{
          c.label.innerHTML = mensaje;
	  c.label.className = 'validaLabel';	  
	  c.className = 'validaForma';
	  mensaje = false;	  
	}
	else
	{

			 
      	  c.label.innerHTML = c.label.texto;
	  c.label.className = c.label.clase;
	  c.className = c.clase;
	  mensaje = true;
	}
	return(mensaje);
 }

 function validaForma(f)
 {
   respuestaT = true;
   respCampo = true;
   primerCampo = null;
   var campos = f.getElementsByTagName( "input" );
   //se recorre cada uno de los inputs
   for(x=0; x < campos.length; x++ )
   {
	 c = campos[x];
	 if (c.alt.length > 0)
	 {
	    respCampo = validaCampo(c, c.alt);
	    if(respCampo == false)
	    {respuestaT = false;
		  if(primerCampo == null)
		  {primerCampo = c;
		  }
	    }
	 }
   }

   var campos = f.getElementsByTagName( "textarea" );
   //se recorre cada uno de los inputs
   for(x=0; x < campos.length; x++ )
   {
	 c = campos[x];

	 if (c.title.length > 0)
	 {
	    respCampo = validaCampo(c, c.title);
	    if(respCampo == false)
	    {respuestaT = false;
		  if(primerCampo == null)
		  {primerCampo = c;
		  }
	    }
	 }
   }

   var campos = f.getElementsByTagName( "select" );
   //se recorre cada uno de los inputs
   for(x=0; x < campos.length; x++ )
   {
	 c = campos[x];
 
	 if (c.title.length > 0)
	 {
	    respCampo = validaCampo(c, c.title);
	    if(respCampo == false)
	    {respuestaT = false;
		  if(primerCampo == null)
		  {primerCampo = c;
		  }
	    }
	 }
   }
   
   if(respuestaT == false)
   {primerCampo.focus();
   }
   return(respuestaT);
 }
  
  function validacion(f)
  {
    //if (f.asocia == null)
	{
	  asociaLabels(f);
	  f.asocia = true;
	}
	return( validaForma(f) );
  }