  function valida(theForm)
{

  error=0;
	if (trim(theForm.username) == "")
	{
		alert("Debe rellenar el campo Usuario");
		theForm.username.focus();
		error=1;
	}else if (!recoger(theForm.username.value,7))
 	{
	    alert ("Debe introducir únicamente letras y números en el campo Usuario");
      theForm.username.value = "";
			theForm.username.focus ();
			error = 1;
 	}
	
  
  if (trim(theForm.userpassword) == "")
	{
		alert("Debe rellenar el campo Contraseña");
		theForm.userpassword.focus();
		error=1;
	}else if (!recoger(theForm.userpassword.value,7))
 	{
	    alert ("Debe introducir únicamente letras y números en el campo Contraseña");
      theForm.userpassword.value = "";
			theForm.userpassword.focus ();
			error = 1;
 	}

  if (error==0){
      theForm.submit();
/*    } else {
      alert("Validación en proceso\n\n  Espere por favor");
    }*/
    
  }

}

function trim(e)
{
  var izq = 0;
  var der = 0;
  for (i=0; e.value.substr(i,1) == " " && i < e.value.length; i++)
    izq ++;
  for (i = e.value.length -1; e.value.substr(i,1) == " " && i >= 0; i --)
    der++;
  e.value = e.value.substr(izq, e.value.length - izq - der);
  if (izq==der && (e.value.charAt(0)==" " || e.value.charAt(0)==""))
  {
    return "";
  }
}

function recoger(campo,tipo_comprobacion)
{
  if (tipo_comprobacion == 1){
      // validamos numeros
         var checkOk = "0123456789";
     }else if( tipo_comprobacion == 2){
	      // validamos minusculas.
	         var checkOk = "abcdefghijklmnopqrstuvwxyz";
       }else if( tipo_comprobacion == 3){
		 // validamos minusculas y numeros.
			   var checkOk = "abcdefghijklmnopqrstuvwxyz0123456789";
		}else if( tipo_comprobacion == 4){
		   // validamos mayusculas.
		 	   var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";		
	         }else if( tipo_comprobacion == 5){
			  // validamos mayusculas,numeros.
					var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                 }else if( tipo_comprobacion == 6){
		         // validamos que el campo sea mayusculas,minusculas.
               	      var checkOk="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		          }else if( tipo_comprobacion == 7){
			       // validamos mayusculas,minusculas,numeros.
			       	   var checkOk="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		        	}else if( tipo_comprobacion == 8){
			       // validamos numeros y /.
			       	   var checkOk="0123456789/";
					   }else if( tipo_comprobacion == 9){
					   //validamos cifras(numeros con .)
					   var checkOk="0123456789.";
   					   }else if( tipo_comprobacion == 10){
					   //validamos cifras(numeros con .)
					   var checkOk="0123456789.,";
		        	}
            var checkStr = campo;
		if (checkStr == "")
		{
			return false;
		}
		for ( i = 0; i< checkStr.length; i++) 
		{
			ch=checkStr.charAt(i);
			for ( j= 0; j< checkOk.length; j++)
			{
	        	if (ch == checkOk.charAt(j))
				{
		        break;
		        }
			if(j == checkOk.length-1)
			{
				return false;		
			}
		}
	}
	return true;
}
