function nuevoAjax(){
	var oHttp=false;
		var asParsers=["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
		for (var iCont=0; ((!oHttp) && (iCont<asParsers.length)); iCont++)	{
			try	{
				oHttp=new ActiveXObject(asParsers[iCont]);
			}
		    catch(e) {
				oHttp=false;
			}
		}

	if ((!oHttp) && (typeof XMLHttpRequest!='undefined')) {
		oHttp=new XMLHttpRequest();
	}
	return oHttp;
}

function linksFixer (links) {
	for(var i=0; i<links.length; i++) {
		var href = links[i].getAttribute("href");
		var rel = links[i].getAttribute("rel");
		if (rel != "formulario") {
			var hash = href.substr(href.indexOf("idmenu=")+7);
			links[i].setAttribute("href","#"+hash);
		}
	}
}

function refrescarMenus (idmenu) {
	/*Buscar info sobre arguments.length para saber si podemos enviar formularios a partir de saber cuantos parametros (argumentos) le
	 * enviamos a la funcion.*/
	if (idmenu == 0) {
		return;
	}

	var ajaxMenus;
	var respuestaXml;
	var navegacionPrincipal = document.getElementById('navegacionprincipal');
	var navegacionsecundaria = document.getElementById('navegacionsecundaria');
	var rastro = document.getElementById('rastro');
	var titulo = document.getElementById('titulo');
	var subtitulo = document.getElementById('subtitulo');
	var contenido = document.getElementById('contenido');
	var ajaxEnabled;

	ajaxMenus = nuevoAjax ();
	ajaxMenus.open("POST", "ajaxindexv2.php", false);
	ajaxMenus.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxMenus.send("tag=refrescarMenus&idmenu=" + idmenu);

//	ajaxMenus.onreadystatechange = function(){
//		if (ajaxMenus.readyState == 4) {
			//Ahora tengo que procesar el xml de respuesta...
//			if (ajaxMenus.status == 200) {
				respuestaXml = ajaxMenus.responseXML;
				navegacionPrincipal.innerHTML = respuestaXml.getElementsByTagName("navegacionPrincipal")[0].firstChild.nodeValue;
				if (respuestaXml.getElementsByTagName("navegacionsecundaria")[0].firstChild.nodeValue != '') {
					ver = '';
				} else {
					ver = 'none';
				}
				navegacionsecundaria.innerHTML = respuestaXml.getElementsByTagName("navegacionsecundaria")[0].firstChild.nodeValue;
				document.getElementById('bs').style.display = ver;
				rastro.innerHTML = respuestaXml.getElementsByTagName("rastro")[0].firstChild.nodeValue;
				titulo.innerHTML = respuestaXml.getElementsByTagName("titulo")[0].firstChild.nodeValue;
				document.title = respuestaXml.getElementsByTagName("tituloPagina")[0].firstChild.nodeValue;
				subtitulo.innerHTML = respuestaXml.getElementsByTagName("subtitulo")[0].firstChild.nodeValue;
				contenido.innerHTML = respuestaXml.getElementsByTagName("contenido")[0].firstChild.nodeValue;
				ajaxEnabled = respuestaXml.getElementsByTagName("ajaxEnabled")[0].firstChild.nodeValue;
				if (ajaxEnabled == 1) {
					links = navegacionPrincipal.getElementsByTagName("A");
					linksFixer(links);
					links = navegacionsecundaria.getElementsByTagName("A");
					linksFixer(links);
					links = rastro.getElementsByTagName("A");
					linksFixer(links);
					links = titulo.getElementsByTagName("A");
					linksFixer(links);
					links = subtitulo.getElementsByTagName("A");
					linksFixer(links);
					links = contenido.getElementsByTagName("A");
					linksFixer(links);
				}
//			} else {
//				alert("Estado: " + ajaxMenus.status + "\nMotivo: " + ajaxMenus.statusText);
//			}
//		}
//	}

}

//function chkPage(url) {
//	var req = new XMLHttpRequest();
//	try {
//	req.open('HEAD', url);
//	req.send(null);
//	}
//	catch (e) {
//	return false;
//	}
//	return true;
//}


function enviarform (idform, idmenu, accion, idelem) {
	var formulario;
	var longitudformulario;
	var cadenaformulario;
	var sepcampos;
	var ajaxMenus;
	var respuestaXml;
	var navegacionprincipal = document.getElementById('navegacionprincipal');
	var navegacionsecundaria = document.getElementById('navegacionsecundaria');
	var rastro = document.getElementById('rastro');
	var titulo = document.getElementById('titulo');
	var subtitulo = document.getElementById('subtitulo');
	var contenido = document.getElementById('contenido');
	var ajaxEnabled;
	var vinculosDelForm;

	if (idform != "noform") {
		//Serializamos el formulario.
		formulario = document.getElementById(idform);
		longitudformulario = formulario.elements.length;
		cadenaformulario = "";
		sepcampos = "";
		for (var i = 0; i <= formulario.elements.length - 1; i++) {
			if (formulario.elements[i].type == "radio"){
				if (formulario.elements[i].checked == true){
				cadenaformulario += sepcampos+formulario.elements[i].name+'='+encodeURI(formulario.elements[i].value);
				sepcampos = "&";
				}
			}else{
				cadenaformulario += sepcampos+formulario.elements[i].name+'='+encodeURI(formulario.elements[i].value);
				sepcampos = "&";
			}
		}
		vinculosDelForm = formulario.getElementsByTagName ("A");
	}

	//Creamos el objeto ajax y le enviamos los parametros.
	ajaxMenus = nuevoAjax ();

//if (chkPage('ajaxindexv2.php')== true){
//	ajaxMenus.open("POST", "ajaxindexv2.php",true);
//}else{
	ajaxMenus.open("POST", "../ajaxindexv2.php",true);
//}


	ajaxMenus.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxMenus.send("tag=refrescarMenus&idmenu=" + idmenu + "&accion=" + accion + "&" + cadenaformulario + "&idelem=" + idelem);
	var_estadoactual = null;
	if (document.getElementById('estadoactual')) {
		var_estadoactual = document.getElementById ('estadoactual');
		textito = document.getElementById ('textomensaje');
		var_estadoactual.style.display= '';
		textito.style.background='#FF9900';
		textito.innerHTML = "Procesando, por favor aguarde...";
	}
	contenido.style.cursor = "wait";
	if (idform != "noform") {
		bloquearEnlaces (vinculosDelForm);
	}

	ajaxMenus.onreadystatechange = function() {

		if (ajaxMenus.readyState == 4) {
			contenido.style.cursor = "default";
			if (idform != "noform") {
				habilitarEnlaces (vinculosDelForm);
			}
			//Ahora tengo que procesar el xml de respuesta...
			if (ajaxMenus.status == 200) {
				respuestaXml = ajaxMenus.responseXML;
				navegacionprincipal.innerHTML = respuestaXml.getElementsByTagName("navegacionprincipal")[0].firstChild.nodeValue;
				if (respuestaXml.getElementsByTagName("navegacionsecundaria")[0].firstChild.nodeValue != '') {
					ver = '';
				} else {
					ver = 'none';
				}
				navegacionsecundaria.innerHTML = respuestaXml.getElementsByTagName("navegacionsecundaria")[0].firstChild.nodeValue;
				document.getElementById('bs').style.display = ver;
				rastro.innerHTML = respuestaXml.getElementsByTagName("rastro")[0].firstChild.nodeValue;
				titulo.innerHTML = respuestaXml.getElementsByTagName("titulo")[0].firstChild.nodeValue;
				subtitulo.innerHTML = respuestaXml.getElementsByTagName("subtitulo")[0].firstChild.nodeValue;
				contenido.innerHTML = respuestaXml.getElementsByTagName("contenido")[0].firstChild.nodeValue;
				ajaxEnabled = respuestaXml.getElementsByTagName("ajaxEnabled")[0].firstChild.nodeValue;
				if (ajaxEnabled == 1) {
					links = navegacionprincipal.getElementsByTagName("A");
					linksFixer (links);
					links = navegacionsecundaria.getElementsByTagName("A");
					linksFixer (links);
					links = rastro.getElementsByTagName("A");
					linksFixer (links);
					links = titulo.getElementsByTagName("A");
					linksFixer (links);
					links = subtitulo.getElementsByTagName("A");
					linksFixer (links);
					links = contenido.getElementsByTagName("A");
					linksFixer (links);
				}
			} else {
				alert("Estado: " + ajaxMenus.status + "\nMotivo: " + ajaxMenus.statusText);
			}
		} else {
			if (var_estadoactual != null) {
				var_estadoactual.style.display= '';
				textito.style.background='#FF9900';
				textito.innerHTML = "Procesando, por favor aguarde!...";
			}
		}
	}
}

function bloquearEnlaces (elemento) {
    for (var i = 0; i < elemento.length; i++) {
		var href = elemento[i].getAttribute("href");
		elemento[i].setAttribute("name",href);
		elemento[i].setAttribute("href","#");
		elemento[i].disabled = true;
	}
}

function habilitarEnlaces (elemento) {
    for (var i = 0; i < elemento.length; i++) {
		if (elemento[i].getAttribute("href") == "#") {
			var hrefAnterior = elemento[i].getAttribute("name");
			elemento[i].setAttribute("href",hrefAnterior);
			elemento[i].setAttribute("name","");
		}
		elemento[i].disabled = false;
	}
}

function submitenter0 (myfield,e,form,idmenu,accion){
       var keycode;
       if (window.event) keycode = window.event.keyCode;
       else if (e) keycode = e.which;
       else return true;

       if (keycode == 13){
          enviarform(form,idmenu,accion);

          return false;
       }else{
          return true;
       }
}

