function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

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


/*************************************************************************************************************************/
/*                                                         USUARIOS ONLINE                                               */
/*************************************************************************************************************************/
function mostrarUsersOnline(userId, cursoId){
	var divUsersOnline = document.getElementById('users_online');
	ajaxUsers=objetoAjax();
	ajaxUsers.open("POST", "inc/dpl_users_online.php", true);
	ajaxUsers.onreadystatechange=function() {
		if (ajaxUsers.readyState==4) {
			if(ajaxUsers.status == 200) {
				divUsersOnline.innerHTML = ajaxUsers.responseText;
			}
		}
	}
	ajaxUsers.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajaxUsers.send('user_id='+userId+"&curso_id="+cursoId);
	setTimeout ("mostrarUsersOnline('"+userId+"', '"+cursoId+"')", 10000);
}


/*************************************************************************************************************************/
/*                                                               CHAT                                                    */
/*************************************************************************************************************************/
idNuevoUltimoMensaje = '';
idViejoUltimoMensaje = '';
function inicializarChat(curso_id, user_id, idUltimoMensaje){
	divDebug = document.getElementById('debug');
	idUltimoMensaje = mostrarMensajes(curso_id, idUltimoMensaje);
	mostrarUsuariosOnline(curso_id, user_id);
	setTimeout ("inicializarChat('"+curso_id+"', '"+user_id+"', '"+idUltimoMensaje+"')", 1000);
}


/*************************************************************************************************************************/
function enviarMensaje(){
	//donde se mostrará lo resultados
	divChatContenido = document.getElementById('chat_contenido');
	divCuadroMensaje = document.getElementById('mensaje');

	var mensaje = document.frmMensaje.mensaje.value;
	var curso_id = document.frmMensaje.id_curso.value;
	var user_id = document.frmMensaje.id_usuario.value;
	
	ajax=objetoAjax();
	ajax.open("POST", "enviar_mensaje.php",true);
	ajax.onreadystatechange=function() {
		divCuadroMensaje.value = '';
		/*if (ajax.readyState==1) {
			divCuadroMensaje.value = '';
		}*/
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("mensaje="+mensaje+"&curso_id="+curso_id+"&user_id="+user_id);
}

/*************************************************************************************************************************/
function mostrarMensajes(curso_id, idUltimoMensaje){
	// VARIABLES
	var mensaje = '';
	idViejoUltimoMensaje = idUltimoMensaje;
	
	// DIVS
	divChatContenido = document.getElementById('chat_contenido');
	divFlashAudio = document.getElementById('chat_sonido');
	divDebug = document.getElementById('debug');
	
	// PROCESO
	ajax=objetoAjax();
	ajax.open("POST", "mostrar_mensajes.php", true);
	ajax.onreadystatechange=function() {
		// si ajax está listo...
		if (ajax.readyState==4) {
			// imprimo el mensaje de ok
			divDebug.innerHTML = 'MSG:'+ajax.readyState+'|';
			// si terminó de cargar la página...
			if(ajax.status == 200) {
				divChatContenido.style.cursor = '';
				var documento_xml = ajax.responseXML;
				idNuevoUltimoMensaje = documento_xml.getElementsByTagName('ultimo')[0].firstChild.nodeValue;
				if (idNuevoUltimoMensaje > idUltimoMensaje){
					var audio = documento_xml.getElementsByTagName('sonido')[0].firstChild.nodeValue;
					divFlashAudio.innerHTML = audio;
					var root = documento_xml.getElementsByTagName("listado_mensajes");
					var mensajes = documento_xml.getElementsByTagName("mensaje");
					for (i = 0; i < mensajes.length; i++){
						id = documento_xml.getElementsByTagName('id')[i].firstChild.nodeValue;
						color = documento_xml.getElementsByTagName('color')[i].firstChild.nodeValue;
						usuario = documento_xml.getElementsByTagName('usuario')[i].firstChild.nodeValue;
						hora = documento_xml.getElementsByTagName('hora')[i].firstChild.nodeValue;
						contenido = documento_xml.getElementsByTagName('contenido')[i].firstChild.nodeValue;
						mensaje = mensaje + '&raquo; '+'<span style="color: '+color+'"><em>'+usuario+' ('+hora+')</em> &raquo; <strong>'+contenido+'</strong></span><br />';
						
					}
					viejosDivs = document.getElementsByTagName('div');
					for (i = 0; i < viejosDivs.length; i++){
						viejosDivs[i].style.backgroundColor = '#FFFFFF';
					}
					nuevoDiv =  document.createElement("div");
					nuevoDiv.innerHTML = mensaje;
					nuevoDiv.style.backgroundColor = '#F1EFE2';
					nuevoDiv.style.margin = '1px 0';
					divChatContenido.appendChild(nuevoDiv);
					divChatContenido.scrollTop = divChatContenido.scrollHeight;
					idViejoUltimoMensaje = idNuevoUltimoMensaje;
				}
			}
		} else {
			divDebug.innerHTML = 'MSG:'+ajax.readyState+'|';
			divChatContenido.style.cursor = 'wait';
		}
	}
	idViejoUltimoMensaje = idNuevoUltimoMensaje;
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("curso_id="+curso_id+"&idUltimoMensaje="+idViejoUltimoMensaje);
	return idViejoUltimoMensaje;
}

/*************************************************************************************************************************/
function mostrarUsuariosOnline(curso_id, user_id, ajax){
	divChatUsuarios = document.getElementById('chat_usuarios');
	divDebugUsr = document.getElementById('debugUsr');
	ajax = objetoAjax();
	ajax.open("POST", "usuarios_online.php", true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divChatUsuarios.innerHTML = ajax.responseText;
			divChatUsuarios.style.cursor = '';
			divDebugUsr.innerHTML = 'USR:'+ajax.readyState+'|';
		} else {
			divChatUsuarios.style.cursor = 'wait';
			divDebugUsr.innerHTML = 'USR:'+ajax.readyState+'|';
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("curso_id="+curso_id+"&user_id="+user_id);
}
