function ajax(parametros,nombrePrograma){
	if(window.XMLHttpRequest) {
		var xmlhttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	} else {
		return true;
	}
	xmlhttp.open('POST',nombrePrograma, true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); // sending it as encoded formdata
	xmlhttp.setRequestHeader('Content-length',parametros.length); // we need to specify the length of the contents
	xmlhttp.setRequestHeader('Connection','close'); // Connection is to be closed after transfer
	xmlhttp.send(parametros); 	
	return xmlhttp;
};
function checkSession(insideModule){
	//alert('checkSession');
	params = 'session_code='+sessionCode;
	if(insideModule == true){
		var xmlhttp = ajax(params,'../ajax/session.php');
	}else{
		var xmlhttp = ajax(params,'ajax/session.php');
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) { // Wait until everything is fetched!
			resp2 = xmlhttp.responseText;
			resp2 = resp2.split("::");
			if(resp2[0] == "ERROR"){
				top.document.getElementById('loginBox').style.display = "block";
				top.document.getElementById('loginMessage').style.display = "block";
				top.document.getElementById('Cargando').style.display = "block";
			}else if(resp2[0] == "SUCCESS"){
				top.document.getElementById('loginBox').style.display = "none";
				top.document.getElementById('loginMessage').style.display = "none";
				top.document.getElementById('Cargando').style.display = "none";
			}else{
				//alert('Falla de conexion:'+resp2);	
			}
		}
	}
	
};
function login(){
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	params = 'username='+username+'&password='+password;
	var xmlhttp = ajax(params,'ajax/login.php');
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) { // Wait until everything is fetched!
			resp = xmlhttp.responseText;
			resp = resp.split("::");
			if(resp[0] == "SUCCESS"){
				sessionCode = resp[1];
				top.document.getElementById("desktopObject").style.display = "block";
				top.document.getElementById("loginBox").style.display = "none";
				top.document.getElementById("Cargando").style.display = "none";
				var myIframe = top.document.createElement('iframe');
				if(top.document.getElementById("dashboardFrame")){
				//	top.document.getElementById("dashboardFrame").parentNode.removeChild(top.document.getElementById("dashboardFrame"));	
					myIframe.style.display = "none";
				}
				myIframe.setAttribute("id","dashboardFrame");
				myIframe.setAttribute("src","dashboard.php?sessionCode="+sessionCode);
				myIframe.setAttribute("width","100%");
				myIframe.setAttribute("height","100%");
				myIframe.frameBorder = "0";
				top.document.getElementById('container_0').appendChild(myIframe);
			}else if(resp[0] == "ERROR"){
				alert(resp[1]);
			}else{
				alert('Error de comunicación con el servidor:'+resp);
			}
		}
	}

};
function changeTab(tabNum){
	checkSession(false);

	if(top.document.getElementById('tabList')){
		var dlElement = top.document.getElementById('tabList');
	}else{
		var dlElement = document.getElementById('tabList');	
	}
	var dlChildren = dlElement.childNodes;
	for(i=0;i<dlChildren.length;i++) if(dlChildren[i].tagName=="DT") dlChildren[i].className='dt dt_inactive'; else dlChildren[i].className='dd dd_inactive';

	top.document.getElementById('container_'+tabNum).className = 'dd dd_active';
	top.document.getElementById('tab_'+tabNum).className = 'dt dt_active';
	
};
function openModule(moduleId,moduleName,modulePath){
	var tabList = top.document.getElementById('tabList');
	if(!top.document.getElementById("tab_"+moduleId)){
		var dtElement = top.document.createElement('dt');
		dtElement.setAttribute("id","tab_"+moduleId);
		dtElement.className = "dt dt_active";
		dtElement.onclick = function(){
			parent.changeTab(moduleId);
		};
		var textDT = top.document.createTextNode(moduleName);
		dtElement.appendChild(textDT);
		tabList.appendChild(dtElement);
		
		var ddElement = top.document.createElement('dd');
		ddElement.setAttribute("id","container_"+moduleId);
		ddElement.className = "dd dd_active";
		
		//PARA QUE DIGA [CERRAR] AL TOPE DE TODOS LOS MODULOS ACTIVOS
		var closingDiv = top.document.createElement('div');
		closingDiv.className = "closeLink";
		closingDiv.onclick = function (){
			closeModule(moduleId);	
		};
		var closingText = top.document.createTextNode('[x]');
		closingDiv.appendChild(closingText);
		ddElement.appendChild(closingDiv);
		
		//PARA QUE APAREZCA LA IMAGEN CERRAR AL TOPE DE TODOS LOS MODULOS ACTIVOS (PRESENTA BUGS)
		/*var closingImg = top.document.createElement('IMG');
		//closingImg.className = "closeLink";
		closingImg.onclick = function (){
			closeModule(moduleId);	
		};
		closingImg.setAttribute("id","imagenUno"); 
		closingImg.setAttribute("src","../images/cerrar.png");
		closingImg.setAttribute("width","52px");
		closingImg.setAttribute("height","16px");
		closingImg.setAttribute('align','left');
		ddElement.appendChild(closingImg);*/
		
		var moduleIframe = top.document.createElement('iframe');
		moduleIframe.setAttribute("src",modulePath+'?sessionCode='+sessionCode);
		moduleIframe.setAttribute("width","100%");
		moduleIframe.setAttribute("height","100%");
		moduleIframe.setAttribute("id",moduleId);
		moduleIframe.frameBorder = "0";
		ddElement.appendChild(moduleIframe);
					
		tabList.appendChild(ddElement);
	
		changeTab(moduleId);
	}else{
		changeTab(moduleId);
	}
};
function closeModule(tabNum){
	var dlElement = top.document.getElementById('tabList');
	dlElement.removeChild(top.document.getElementById('tab_'+tabNum));
	dlElement.removeChild(top.document.getElementById('container_'+tabNum));
	changeTab('0');
};

var timerID;

function ShowLayer(id)
{
  document.getElementById().style.display = "block"; 
}

function HideTimedLayer(id)
{  
    clearTimeout(timerID);
    document.getElementById(id).style.display = "none";
}

function timedLayer(id)
{
  setTimeout("HideTimedLayer(\"" + id + "\")", 5000);
}

