function attachEventListener(target, eventType, functionRef, capture) {
    if (typeof target.addEventListener != "undefined") {
        target.addEventListener(eventType, functionRef, capture);
    } else if (typeof target.attachEvent != "undefined") {
        target.attachEvent("on" + eventType, functionRef);
    } else {
        return false;
    }
    return true;
}

function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if (node == null) node = document;
	if (tag == null) tag = "*";
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function target_blank() {
	if (!document.getElementsByTagName) return;
	var i;
	var enlaces = document.getElementsByTagName("a");
	var cuantos = enlaces.length;
	for (i = 0; i < cuantos; i++) {
		if (enlaces[i].getAttribute("href") && enlaces[i].getAttribute("rel") == "external") enlaces[i].target = "_blank";
	}
}

function reset_form() {
	if (!document.getElementsByTagName) return;
	var i, cuantos;
	var los_inputs = document.getElementsByTagName("input");
	var los_textareas = document.getElementsByTagName("textarea");
	cuantos = los_inputs.length;
	for (i = 0; i < cuantos; i++) {
		if (los_inputs[i].getAttribute("type").toLowerCase() == "text" || los_inputs[i].getAttribute("type").toLowerCase() == "password") {
			los_inputs[i].onfocus = function() {
				if ((this.value.charCodeAt(0) == 32 || this.value.charCodeAt(0) == 160) && this.value.length == 1) this.value = "";
			};
		}
	}
	cuantos = los_textareas.length;
	for (i = 0; i < cuantos; i++) {
		los_textareas[i].onfocus = function() {
			if (this.value.charCodeAt(0) == 32 || this.value.charCodeAt(0) == 160 && this.value.length == 1) this.value = "";
		};
	}
}

function mmn() {
	if (!document.getElementById) return;
	if (!document.getElementsByTagName) return;
	var elementos, cuantos, i;
	
	// Array con cada uno de los menús multinivel.
	var elementos = getElementsByClass("mmn",document,"ul");
	// Si hay menús multinivel, procesamos cada uno.
	if (elementos) {
		cuantos = elementos.length;
		for (i = 0; i < cuantos; i++) {
			mmn_procesar_menu(elementos[i]);
		}
	}
}

function mmn_procesar_menu(menu) {
	if (!document.getElementById) return;
	if (!document.getElementsByTagName) return;
	var lis, submenu, enlaces, cuantos, i;

	// Inicializar el menú (abrir los submenús que procedan).
	lis = menu.getElementsByTagName("li");
	cuantos = lis.length;
	for (i = 0; i < cuantos; i++) {
		if (lis[i].className.indexOf("seleccionado") >= 0) {
			submenus = lis[i].getElementsByTagName("ul");
			if (submenus.length > 0) {
				submenus[0].style.display = "block";
			}
		}
	}

	// Aplicar comportamiento a todos los enlaces (abrir/cerrar submenú).
	enlaces = menu.getElementsByTagName("a");
	cuantos = enlaces.length;
	for (i = 0; i < cuantos; i++) {
		enlaces[i].onclick = function() {
			mmn_procesar_enlace(this);
			return false;
		}
	}
}

function mmn_procesar_enlace(enlace) {
	if (!document.getElementById) return;
	if (!document.getElementsByTagName) return;
	var padre_enlace, submenus, cuantos, i, sublis;
	
	padre_enlace = enlace.parentNode;
	submenus = padre_enlace.getElementsByTagName("ul");
	cuantos = submenus.length;
	if (padre_enlace.className.indexOf("seleccionado") >= 0) {
		if (cuantos == 0) {
			// Por no tener submenú y estar seleccionado no se hace nada.
		} else {
			if (padre_enlace.className == "") {
				padre_enlace.className = "";
			} else {
				padre_enlace.className = padre_enlace.className.replace("seleccionado","");
			}
			
		}
		for (i = 0; i < cuantos; i++) {
			submenus[i].style.display = "none";
		}
		sublis = padre_enlace.getElementsByTagName("li");
		cuantos = sublis.length;
		for (i = 0; i < cuantos; i++) {
			sublis[i].className = "";
		}
	} else {
		if (padre_enlace.className == "") {
			padre_enlace.className = "seleccionado";
		} else {
			padre_enlace.className = "seleccionado " + padre_enlace.className;
		}
		if (cuantos > 0) {
			submenus = padre_enlace.getElementsByTagName("ul");
			submenus[0].style.display = "block";
		}
	}
}

attachEventListener(window, "load", target_blank, false);
attachEventListener(window, "load", reset_form, false);
attachEventListener(window, "load", mmn, false);
