function validarNombrePersona(input) {
	if(typeof(input)!='string') return false;
	if(!checkChars(input.toLowerCase(),'abcdefghijklmnñopqrstuvwxyzáéíóúäëïöü\'´ ')) return false;
	return true;
}
function validarTelefono(input) {
	if(typeof(input)!='string') return false;
	if(!checkChars(input,'0123456789-, ')) return false;
	return true;
}
function checkRut(rut,dv) {
	if(!checkChars(rut,'0123456789') || !checkChars(dv.toLowerCase(),'0123456789k')) return false;
	var M=0,S=1;
	for(;rut;rut=Math.floor(rut/10)) S=(S+rut%10*(9-M++%6))%11;
	if(dv.toLowerCase() != (S?S-1:'k')) return false;
	else return true;
}
function trim(text) {
	return text.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
function checkEmail(input) {
	var mails=input.toLowerCase().split(',');
	var mail='';
	for(var i=0;i<mails.length;i++) {
		mail=mails[i].toLowerCase().replace(/^\s+/g,'').replace(/\s+$/g,'');
		if (mail!='' && !mail.match(/^[a-z0-9._%+-]+@(?:[a-z0-9-]+\.)+(?:[a-z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$/)) return false;
	}
	return true;
}
function filterList(input) {
	var elems=input.toLowerCase().split(',');
	var elem='';
	var output='';
	for(var i=0;i<elems.length;i++) {
		elem=elems[i].toLowerCase().replace(/^\s+/g,'').replace(/\s+$/g,'');
		if (elem!='') output+=','+elem;
	}
	output=output.slice(1);
	return output;
}
function getElement(elementId) {
	return document.getElementById(elementId);
}
function createAjaxLoader() {
	var ajaxLoader = new Object();
	ajaxLoader._httpgetquery = null;
	ajaxLoader._httppostquery = null;
	ajaxLoader.httpGetRequestTimeout = null;
	ajaxLoader.httpPostRequestTimeout = null;
	ajaxLoader.setGetRequestTimeout = function(func, time) {
		ajaxLoader.httpGetRequestTimeout = setTimeout(func, time);
	};
	ajaxLoader.setPostRequestTimeout = function(func, time) {
		ajaxLoader.httpPostRequestTimeout = setTimeout(func, time);
	};
	ajaxLoader.ajaxGet = function(url, func) {
		if(window.XMLHttpRequest) ajaxLoader._httpgetquery = new XMLHttpRequest();
		else if(window.ActiveXObject) ajaxLoader._httpgetquery = new ActiveXObject("Microsoft.XMLHTTP");
		if(ajaxLoader._httpgetquery) {
			ajaxLoader._httpgetquery.onreadystatechange = function() {
				if(ajaxLoader._httpgetquery.readyState == 4 && ajaxLoader._httpgetquery.status == 200) {
					clearTimeout(ajaxLoader.httpGetRequestTimeout);
					func(ajaxLoader._httpgetquery.responseText);
				}
			};
			ajaxLoader._httpgetquery.open('GET', url, true);
			ajaxLoader._httpgetquery.send(null);
		}
	};
	ajaxLoader.ajaxPost = function(url, params, func, contentType) {
		if(window.XMLHttpRequest) ajaxLoader._httppostquery = new XMLHttpRequest();
		else if(window.ActiveXObject) ajaxLoader._httppostquery = new ActiveXObject("Microsoft.XMLHTTP");
		if(ajaxLoader._httppostquery) {
			ajaxLoader._httppostquery.onreadystatechange = function() {
				if(ajaxLoader._httppostquery.readyState == 4 && ajaxLoader._httppostquery.status == 200) {
					clearTimeout(ajaxLoader.httpPostRequestTimeout);
					func(ajaxLoader._httppostquery.responseText);
				}
			};
			ajaxLoader._httppostquery.open('POST', url, true);
			if(contentType) ajaxLoader._httppostquery.setRequestHeader("Content-Type", contentType);
			ajaxLoader._httppostquery.send(params);
		}
	};
	return ajaxLoader;
}
function setText(elementId, text) {
	// Validar parámetros.
	var element = document.getElementById(elementId);
	if(element == null || typeof text != 'string') return;
	// Eliminar todos los nodos hijo de tipo 3 (Text) del elemento.
	for(var i=0;i<element.childNodes.length;i++) {
		if(element.childNodes[i].nodeType == 3) {
			element.removeChild(element.childNodes[i]);
		}
	}
	// Añadir el nodo de tipo 3 (Text) al elemento.
	element.appendChild(document.createTextNode(text));
}
function checkChars(text, charsAllowed) {
	var allowed;
	// Validar parámetros.
	if(typeof text != 'string' || typeof charsAllowed != 'string' || charsAllowed == '') return false;
	for(var i=0;i<text.length;i++) {
		allowed = false;
		for(var j=0;j<charsAllowed.length;j++) {
			if(text[i] == charsAllowed[j]) {
				allowed = true;
				break;
			}
		}
		if(!allowed) return false;
	}
	return true;
}
function limitTextLength(textElementId, limit, statusElementId) {
	var textElement = document.getElementById(textElementId);
	if(textElement.value.length > limit) textElement.value = textElement.value.substring(0, limit);
	else setText(statusElementId, "("+textElement.value.length+"/"+limit+")");
}
