// JavaScript Document

function controlTecla(e) 
{
	var correctos = new Array("1","2","3","4","5","6","7","8","9","0");
	var estado=false;
	var codigoTecla, cadenaTecla;

	if(document.all)
	{
		codigoTecla = event.keyCode
		cadenaTecla = (String.fromCharCode(event.keyCode));
	}
	else if(document.layers)
	{
		codigoTecla = e.which
		cadenaTecla = String.fromCharCode(e.which);
	}
	else if(document.getElementById)
	{
		codigoTecla = (window.Event) ? e.which : e.keyCode;
		cadenaTecla=(String.fromCharCode(codigoTecla));
	}

	for(i=0;i<correctos.length;i++)
	{
		if(cadenaTecla==correctos[i])
			estado=true;
	}

	if(estado==false)
	{
		if(document.all)
			event.returnValue = false;
		else
			return false;
	}
}

function check_email (email) 
{
	// Por defecto, se entiende que la dirección de e-mail es correcta

	// 1 - Que la dirección contenga únicamente caracteres válidos
	allowed_chars = new Array ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "@", "_", "-");
	for (i = 0; i < email.length; i++) 
	{
		comparations = 0;
		for (j = 0; j < allowed_chars.length; j++) 
		{
			if ((email.charAt (i) != allowed_chars [j]) && (email.charAt (i) != allowed_chars [j].toUpperCase()))
			{
				comparations++;
			} 
			else 
			{
				break;
			}
		}
		if (comparations == allowed_chars.length) 
		{
			return (false);
		}
	}
	delete allowed_chars;
		
	// 2 - Comprobación de uso de una única arroba
	count_arrobas = 0;
	for (i = 0; i < email.length; i++) 
	{
		if (email.charAt (i) == '@') 
		{
			count_arrobas++;
		}
	}
	if (count_arrobas != 1) 
	{
		return (false);
	}
		

	// 3 - Comprobación de dominios
	haypunto=false
	for (i = email.length-5; i < email.length-2; i++) 
	{
		if (email.charAt (i) == '.') 
		{
			haypunto=true;
		}
	}
	if (haypunto)
	{
		return (true);
	}
	else
	{
		return(false);
	}
}

function trim(texto)
{
	var longitud=texto.length;
	var i;
	var j;
	for (i=0; i<longitud; i++){
		if (texto.substring(i,i+1)!==" "){
			break;
		}
	}
	for (j=longitud; j>0; j--){
		if (texto.substring(j-1,j)!==" "){
			break;
		}
	}
	if (j==0) {
		texto = "";
	}else{
		texto = texto.substring(i,j);
	}
	return texto;
}