	// Removes leading whitespaces
	function LTrim( value ) {
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
		
	}
	// Removes ending whitespaces
	function RTrim( value ) {	
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
	}
	
	// Removes leading and ending whitespaces
	function trim( value ) {
		return LTrim(RTrim(value));
	}
	
	function ValidaForm(f){
		for (i=0; i<f.elements.length; i++) {
			objeto = f.elements[i];
			// Verificamos si el elemento es obligatorio
			if(objeto.id.search(/\*/) > 0){
				// Si es tipo de texto, entonces no debe estar vacio
				if (objeto.type == "text" || objeto.type == "password" || objeto.type == "textarea") {
					objeto.value = trim(objeto.value);
					if(trim(objeto.value) == ""){ // verificamos que no este vacio
						if(objeto.alt == "undefined" || objeto.alt == ""){
							alert("Todos los campos marcados con un asterisco tienen que tener valor.");
						} else { alert(objeto.alt); }
						objeto.select();
						objeto.focus();
						return false;
					} else { // Validamos que no tenga codigo javascript
						if(objeto.value.indexOf("<script") >= 0){
							alert("No se permite c"+String.fromCharCode(162)+"digo javascript");
							objeto.select();
							objeto.focus();
							return false;
						}
						if(objeto.name == "correo"){ // verificamos si sintaxis de un correo
							if(objeto.value.search(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig)){
								alert("La cuenta de correo electr"+String.fromCharCode(162)+"nico es incorrecta, debes escribirla de forma: nombre@servidor.dominio");
								objeto.select();
								objeto.focus();
								return false;
							}
						}
					}
				}
				// Verificamos que los <SELECT> tengan seleccionado una opcion
				if(objeto.type == "select-one" && objeto.value == "0"){
					if(objeto.alt == "undefined" || objeto.alt == ""){
						alert("Tienes que seleccionar una opci"+String.fromCharCode(162)+"n");
					} else {
						alert(objeto.alt);
					}
					objeto.focus();
					return false;
				}
				// Verificamos si las contraseñas escritas son iguales
				if(objeto.name == "contrasena_n"){
					if(objeto.value != document.getElementById("contrasena_r*").value){
						if(objeto.alt == "undefined" || objeto.alt == ""){
							alert("Las contrase"+String.fromCharCode(165)+"as escritas son diferentes.");
						} else {
							alert(objeto.alt);
						}
						objeto.value = "";
						document.getElementById("contrasena_r*").value = "";
						objeto.select();
						objeto.focus();
						return false;
					}
				}
				if(objeto.type == "checkbox" && objeto.checked == false){
					if(objeto.alt == "undefined" || objeto.alt == "") {
						alert("Tienes que activar esta opci"+String.fromCharCode(162)+"n.");
					} else {
						alert(objeto.alt);
					}
					objeto.focus();
					return false;
				}
				
				if(objeto.type == "radio"){
					var inputs = document.getElementsByName(objeto.name);
					var unRadioActivado = false;
					for(var j=0; j<inputs.length; j++){
						if(inputs[j].checked == true){
							unRadioActivado = true;
						}
					}
					
					if(unRadioActivado == false){
						if(inputs[0].alt == "undefined" || inputs[0].alt == ""){
							alert("Tienes que seleccionar al menos una opci"+String.fromCharCode(162)+"n.");
						} else {
							alert(inputs[0].alt);
						}
						//objeto.select();
						//objeto.focus();
						return false;
					}
				}
				if(objeto.type == "file"){
					if(trim(objeto.value) == ""){
						if(objeto.alt == "undefined" || objeto.alt == ""){
							alert("Falta proporcionar un archivo.");
						} else {
							alert(objeto.alt);
						}
						objeto.select();
						objeto.focus();
						return false;
					} else {
						if(typeof(objeto.validos) != "undefined" && objeto.validos != ""){
							var expreg = new RegExp(".("+objeto.validos+")/i");
							if(objeto.value.search(objeto.validos)<2){
								var t = objeto.validos;
								alert("Solo se aceptan archivos: "+ t.replace(/\|/g, ", "));
								objeto.select();
								objeto.focus();
								return false;
							}
						}
						if(typeof(objeto.novalidos) != "undefined" && objeto.novalidos != ""){
							var expreg = new RegExp (".("+objeto.novalidos+")/i");
							if(objeto.value.search(objeto.novalidos) > 0){
								var t = objeto.novalidos;
								alert("NO se aceptan archivos: "+ t.replace(/\|/g, ", "));
								objeto.select();
								objeto.focus();
								return false;
							}
						}
					}
				}
			}
		}
		return true;
	}
	
	function iniForm() {
		if(document.forms[0]){
			var inputs = document.forms[0].elements; 
			var i=0;
			if(inputs.length == 0) { return true; }
			// Verificamos que los primeros campos no sean de type=hidden, porque NO se puede hacer un FOCUS() a estos objetos
			// el i<inputs.length es para no marque error, porque si solo hay HIDDENs, marca un error porque llega a inputs.lenth
			while(i<inputs.length && (inputs[i].type == "hidden" || inputs[i].disabled || inputs[i].tagName == "FIELDSET")){ i++; }
			// esta condicion es en caso de que el FORM solo tenga inputs de tipo HIDDEN
			if((i>0 && (inputs[i-1].type == "hidden") || inputs[i].disabled)){ return true; }
			// Luego hacemos el FOCUS() al primero objeto
			// Si es de tipo TEXT, TEXTAREA, FILE entonces podemos hacerle un SELECT()
			if(inputs[i].type == "text" || inputs[i].type == "textarea" || inputs[i].type == "file" ){ inputs[i].select(); }
			// Ponemos el foco en el objeto.
			inputs[i].focus();
		}
	}
	
	function isUsername(strUsername) {
		return strUsername.search(/^[a-zA-Z][\w-\.]{3,30}$/ig);
		/********************
		^[a-zA-Z]     Indicamos que empiece con una letra mayuscula o minuscula
		[\w-\.]{3,30} Indicamos que luego puede ir una letra, numero, guion, guion bajo o punto, de 3 a 30 veces
		Asi tenemos 1 + 3 = 4 carateres minimo para un username y máximo 31 caracteres
		La funcion regresa 0 si todo es correcto y -1 en caso de error
		/********************
		flags	Significado 
		 g 		  Explorar la cadena completa 
		 i 		  No distinguir mayúsuculas de minúsuculas 
		 m 		  Permite usar varios ^y $ en el patrón 
		 s 		  Incluye el salto de línea en el comodín punto .  
		 x 		  Ignora los espacios en el patrón 
		 ********************/
	}
	
	function isEMail(strEMail){
		return strEMail.search(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig);
	}