String.prototype.trim = function() 
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
}
	
	
function validarTexto(e, tipo) {
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla == 8) 
		return true; // Tecla de retroceso
	
	switch(tipo)
	{
		case 1: patron = /[A-Z a-z áéíóúñ ÁÉÍÓÚ üÜ ñÑ]/; // Sólo acepta letras
				break;
		case 2: patron = /[0-9 ]/; // Sólo acepta números
				break;
		case 3: patron = /[A-Za-z0-9]/; // Acepta letras y números
				break;
		case 4: patron = /[A-Z a-z áéíóú ÁÉÍÓÚ üÜ ñÑ 0-9 .#-,]/; // Acepta letras números
				break;
		case 5: patron = /[0-9]/; // Acepta números sin espacios
				break;
		case 6: patron = /[Xx]/;
				break;
		case 7: patron = /[0-9-]/; // Sólo acepta números
				break;				
	}
		
	te = String.fromCharCode(tecla);
	return patron.test(te); 
}

function convierteMinus(ind)
{
	document.forms[0].elements[ind].value = document.forms[0].elements[ind].value.toLowerCase();
}

function validarEmail(ind) 
{
	mail = document.forms[0].elements[ind].value;
	if(mail != "")
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail))
			return true;
		else 
		{
			document.forms[0].elements[ind].focus();
			document.forms[0].elements[ind].select();
   			alert("La dirección de e-mail parece incorrecta. Por favor, corrija.");
			return false;
		}
	}
}	

function validarLogin(ind)
{
	login = document.forms[0].elements[ind].value;
	if(logon != "")
	{
		if (login.length < 5)
		{
			alert("el nombre de usuario debe contener por lo menos 5 caracteres");
			document.forms[0].elements[ind].focus();
			document.forms[0].elements[ind].select();
		}
	}
}

function validarContrasena(ind)
{
	contrasena = document.forms[0].elements[ind].value;
	if(contrasena != "")
	{
		if (contrasena.length < 5)
		{
			alert("La contraseña debe contener por lo menos 5 caracteres");
			document.forms[0].elements[ind].focus();
			document.forms[0].elements[ind].select();
		}
	}
}

function validarFecha(ind){   
    var Fecha = new String(document.forms[0].elements[ind].value)   // Crear un string   
    // Cadena Año   
    var Ano = new String(Fecha.substring(0, Fecha.indexOf("-")))   
    // Cadena Mes   
    var Mes = new String(Fecha.substring(Fecha.indexOf("-") + 1 , Fecha.lastIndexOf("-")))   
    // Cadena Día   
    var Dia = new String(Fecha.substring(Fecha.lastIndexOf("-") + 1, Fecha.length))   
  	var noValida = false
	
	if (Fecha.trim() != "")
	{
		// Validar el año   
		if (isNaN(Ano) || Ano.length < 4 || parseFloat(Ano) < 1900){   
			noValida = true
		}   
		// Validar el Mes   
		if (isNaN(Mes) || parseFloat(Mes) < 1 || parseFloat(Mes) > 12){   
			noValida = true
		}   
		// Validar el Dia   
		if (isNaN(Dia) || parseInt(Dia, 10) < 1 || parseInt(Dia, 10) > 31){   
			noValida = true
		}   
		if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2) {   
			if (Mes==2 && Dia > 28 || Dia>30) {   
				noValida = true
			}   
		}   
       
		if (noValida) {
			alert("Fecha no válida")   
			document.forms[0].elements[ind].focus()
			document.forms[0].elements[ind].select()
		}
	}
} 

function limpiaCajaOtro(indice, valorHabilita)
{
	document.forms[0].elements[indice].value = "";
	document.forms[0].elements[indice].readOnly = valorHabilita;
}  

function habilitaCajaOtro(indice)
{
	if (document.forms[0].elements[indice].readOnly == true)
		document.forms[0].elements[indice].readOnly = false;
}

function comprobar()
{
	var mal=false, cad; 
	numero=document.forms[0].elements.length; 
	for(a=0; a < numero; a++)
	{ 
		cad=document.forms[0].elements[a].value;
		if (cad.trim() == "" || cad.trim() == "-1")
		{
			document.forms[0].elements[a].style.backgroundColor="#ffff97";
			mal=true;
		} 
		else
			document.forms[0].elements[a].style.backgroundColor="#CBD5EF";
	} 
	
	if(mal)
	{
		alert("Por favor, llene las cajas resaltadas.");
		return false;
	} 
	
	document.forms[0].submit()
}	

function comprobarRadios()
{
	var total = 0;
	numElementos = document.forms[0].elements.length; 
	
	for(i = 0; i < numElementos; i++)
	{
		tipoObjeto = document.forms[0].elements[i].type;
		if (tipoObjeto == "radio" && document.forms[0].elements[i].checked == true)
			total = total + 1;
	}

	return total;
}		

function contadorTexto(campo, contador, limitemax) {
	if (campo.value.length > limitemax) // if too long...trim it!
		campo.value = campo.value.substring(0, limitemax);
	// otherwise, update 'characters left' counter
	else 
		contador.value = limitemax - campo.value.length;
}

