
function getWidth() {
var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function getHeight() {
var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function cleanUp() {
	sendRequest(inAdmin+"cmdChat.php?cmd=6",doNothing);
}

function giveSupport(id) {
	sendRequest(inAdmin+"cmdChat.php?cmd=1&id="+id,refrescar);
}

function leerMensajes() {
	var horaUltimoMensaje = document.getElementById("horaUltimoMensaje").value;
	var idUser = document.getElementById("idUser").value;
	//if (idChat != "?") {
		sendRequest(inAdmin+"cmdChat.php?cmd=3&idUser="+idUser,handleNewMensajes,"horaUltimoMensaje="+horaUltimoMensaje);
		
	//}
}

function checarEnLinea() {
	sendRequest(inAdmin+"cmdChat.php?cmd=13",handleEnLinea);
}

function leerAsistencia() {
	var idUser = document.getElementById("idUser").value;
	sendRequest(inAdmin+"cmdChat.php?cmd=11&id="+idUser,handleAsistido);
}

function clientHeartbeat() {
	var idUser = document.getElementById("idUser").value;
//	alert("hearbeat "+idUser+" "+inAdmin);
	sendRequest(inAdmin+"cmdChat.php?cmd=12&id="+idUser,doNothing);
}

function enviarMensaje() {
	var elmMensaje = document.getElementById("mensaje");
	var mensaje = elmMensaje.value;
	var idUser = document.getElementById("idUserAdmin").value;
	var username = document.getElementById("username").value;
	if (idUser != "#NONE") {
		sendRequest(inAdmin+"cmdChat.php?cmd=2&idUser="+idUser,doNothing,"mensaje="+mensaje+"&username="+username);
		elmMensaje.value = "";
		elmMensaje.focus();
	}
}

function doNothing(req) {
}

function asistir(id) {
	popUpAdmin(id);
	sendRequest(inAdmin+"cmdChat.php?cmd=10&id="+id,doNothing);
}

function rejectSupport(id) {
	sendRequest(inAdmin+"cmdChat.php?cmd=8&id="+id,doNothing);
	document.getElementById("backScreen").style.display = "none";
	document.getElementById("askForSupport").style.display = "none";
}

function acceptSupport(id) {
	var obj = document.getElementById("elusername");
	if (obj.value == "") {
		alert("Por favor ingresa un nombre de usuario.");
	}else {
		sendRequest(inAdmin+"cmdChat.php?cmd=9&username="+obj.value+"&id="+id,doNothing);
		popUpSoporte(id);
		document.getElementById("backScreen").style.display = "none";
		document.getElementById("askForSupport").style.display = "none";
	}
}

function acceptSupport2(id) {
	var obj = document.getElementById("elotrousername");
	if (obj.value == "") {
		alert("Por favor ingresa un nombre de usuario.");
	}else {
		sendRequest(inAdmin+"cmdChat.php?cmd=9&username="+obj.value+"&id="+id,doNothing);
		popUpSoporte(id);
		document.getElementById("backScreen").style.display = "none";
		document.getElementById("askForSupport").style.display = "none";
	}
}

function refrescar() {
	document.location.reload();
}

function nuevoChat() {
	var nombre = prompt("Ingresa tu nombre para el chat con soporte técnico");
	document.getElementById("username").value = nombre;
	sendRequest(inAdmin+"cmdChat.php?cmd=1&username="+nombre,handleNuevoChat);
}

function latido() {
	sendRequest(inAdmin+"cmdChat.php?cmd=7",doNothing);
}

function checarNuevosChats() {
	sendRequest(inAdmin+"cmdChat.php?cmd=4",handleChecarChats);
	sendRequest(inAdmin+"cmdChat.php?cmd=7",doNothing);
}

function handleNewMensajes(req) {
	if (req.responseText != "") {
		var listado = req.responseText.split("|");
		document.getElementById("mensajes").value += listado[0];
		document.getElementById("horaUltimoMensaje").value = listado[1];
	}
}

function handleEnLinea(req) {
	if (req.responseText != "") {
		if (req.responseText == "online") {
			if (document.getElementById('loginWindow').style.display == 'none') {
				document.getElementById("conSoporte").style.display = "block";
			}
			document.getElementById("sinSoporte").style.display = "none";
		}else if (req.responseText == "offline") {
			document.getElementById("conSoporte").style.display = "none";
			document.getElementById('loginWindow').style.display = "none";
			document.getElementById("sinSoporte").style.display = "block";
		}

	}
}

function handleAsistido(req) {
	if (req.responseText != "") {
		var listado = req.responseText.split("|");
		//document.getElementById("mensajes").value += listado[0];
		document.getElementById("idUserAdmin").value = listado[0];
		//document.getElementById("mensajes").value += "Ahora estas siendo asistido por: "+listado[1];
	}
}

function handleChecarChats(req) {
	if (req.responseText != "") {
		//var nuevosChats = req.responseText.split("|");
		//for (i=0;i<nuevosChats.length;i++) {
			//if (confirm("Un usuario ha solicitado soporte técnico, atender?"))
				//popUpAdmin(nuevosChats[i]);
		//}
		//sendRequest(inAdmin+"cmdChat.php?cmd=5",doNothing);
		if (req.responseText == "ImAsked!") {
			//sendRequest(inAdmin+"cmdChat.php?cmd=",doNothing);
			//popUpSoporte();
			var obj = document.getElementById("backScreen");
			obj.style.display = "block";
			obj.style.width = getWidth()+"px";
			obj.style.height = getHeight()+"px";
			obj = document.getElementById("askForSupport");
			obj.style.display = "block";
			obj.style.left = (getWidth()/2 - parseInt(obj.style.width)/2)+"px";
			obj.style.top = (getHeight()/2 - parseInt(obj.style.height)/2)+"px";
		}
	}
}

function handleNuevoChat(req) {
	document.getElementById("idChat").value = req.responseText;
}

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) {
		return -1;
	}
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
