function loadPage(url,params,idDiv,showLoading,showLoaded){
	var httpRequest = false;
	if(window.XMLHttpRequest){
		// Si es Mozilla, Safari etc
		httpRequest= new XMLHttpRequest();
	}else if(window.ActiveXObject){
		// pero si es IE
		try{
			httpRequest = new ActiveXObject ("Msxml2.XMLHTTP");
		}catch (e){
			// en caso que sea una versión antigua
			try{
				httpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
			}catch (e){
			}
		}
	}else{
		return false;
	}
	httpRequest.onreadystatechange = function(){
		contenedor=document.getElementById(idDiv);
		if(httpRequest.readyState==1 && showLoading){
			altura=contenedor.offsetHeight;
			contenedor.innerHTML=showLoading;
			try{
				contenedor.style.removeProperty('height');
				contenedor.style.height=altura;
			}catch(e){
			}
		}else if(httpRequest.readyState==4){
			try{
				contenedor.style.removeProperty('height');
			}catch(e){
			}
			txt=httpRequest.responseText;
			if(txt.indexOf("<script")!=-1 ||txt.indexOf("<SCRIPT")!=-1){
				//truco para que iexplore interprete los scripts incluidos
				contenedor.innerHTML="<div style=\"display: none\">&nbsp;</div>\n"+txt;
				executeJavascript(contenedor);
			}else{
				contenedor.innerHTML=txt;
			}
			if(showLoaded){
				mostrarDiv(contenedor);
			}else{
				ocultarDiv(contenedor);
			}
		}
	}
	httpRequest.open ('POST', url, true);		
	httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	httpRequest.send(params);
}
function executeJavascript(divElement){
	arrayScript=divElement.getElementsByTagName("script");
	jsCode="";
	for(x=0;x<arrayScript.length;x++){
		jsCode+=arrayScript[x].innerHTML+"\n";
	}
	eval(jsCode);
}
/*funcion que carga command+params dentro de un div especifico (id=idDiv)
toggleVisible=true : carga/oculta el div de manera alternada
toggleVisible=false: carga el div cada vez que se llama la funcion
 */
function runCommand(commandName,params,idDiv){
	loadPage('index.php?do='+commandName,params,idDiv,false,false);
}
/*funcion que carga command+params dentro de un div especifico (id=idDiv)
muestra mientras se carga y muestra cuando esta listo
 */
function loadCommand(commandName,params,idDiv,show){
	loadPage('index.php?do='+commandName,params,idDiv,false,true);
}
function loadTab(commandName,params,tab){
	if(typeof(tab)!='object'){
		tab=document.getElementById(tab);
	}
	tabSet=tab.parentNode;
	tabContentId='c_'+tabSet.id;
	elem=tabSet.childNodes;
	for(x=0;x<elem.length;x++){
		if(elem[x].className=="front"){
			elem[x].className="back";
			break;
		}
	}
	tab.className="front";
	loadPage('index.php?do='+commandName,params,tabContentId,false,true);
}
/*Funcion que envia el formulario indicado para ser procesado por el commandName.
El resultado se inserta en el div contenedor del formulario.
*/
function submitForm(formulario, commandName,outputDiv){
	stringParams="";
	for(x=0;x<formulario.elements.length;x++){
		if(formulario.elements[x].name==''){
			continue;
		}
		tipoInput=formulario.elements[x].type;
		if(tipoInput=="radio"||tipoInput=="checkbox"){
			if(!(formulario.elements[x].checked)){
				continue;
			}
		}
		if(x>0){
		stringParams+="&";
		}
		stringParams+=formulario.elements[x].name+"="+formulario.elements[x].value;
	}
	//por defecto el resultado del formulario reemplaza a este (si no se especifica outputDiv)
	idContenedor=formulario.parentNode.id;
	if(outputDiv){
		idContenedor=outputDiv;
	}
	loadPage('index.php?do='+commandName,stringParams,idContenedor,'Enviando información...',false);
	return false;
}
function ocultarDiv(divName){
	if(typeof(divName)!='object'){
		divName=document.getElementById(divName);
	}
	divName.style.display="none";
}
function mostrarDiv(divName){
	if(typeof(divName)!='object'){
		divName=document.getElementById(divName);
	}
	divName.style.display="block";
}
function toggleDiv(divName){
	if(typeof(divName)!='object'){
		divName=document.getElementById(divName);
	}
	if(divName.style.display=="block"){
		ocultarDiv(divName);
	}else{
		mostrarDiv(divName);
	}
}
/*metodo util para conocer propiedades y metodos de objetos*/
function getProp(obj,div){
	var txt="<pre>[["+obj+"]]\n\n";
	for(x in obj){
		try{
			txt+= x+"="+eval("obj."+x)+"\n";
		}catch(e){
			txt+= x+".()\n";//+eval("obj."+x)
		}
	}
	txt+="_______________________________________</pre>";
	document.getElementById(div).innerHTML=txt;
}

