// JavaScript Document
function aleatorio()
{ 
num=Math.floor(Math.random()*100000000000000);
num=num.toString();
return num;
}
var che=0;
var sesionunica=0;
var nombreoperador="";
var nombreusuario="";



function nof5()
{
document.onkeydown = function(){  

    if(window .event && window.event .keyCode == 116){ 
     window. event.keyCode = 505;  
    } 
	 if(window .event && window.event .keyCode == 112){ 
     window. event.keyCode = 505;  
    } 
	 if(window .event && window.event .keyCode == 114){ 
     window. event.keyCode = 505;  
    } 
    if(window .event && window.event .keyCode == 505){  
     return false;     
    }  
   }  
   
   // boton derecho
 		
   			
}

function nuevoAjax()
{ 
	var contador=aleatorio();
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

function buscarusuarios(operador)
{
var contador=aleatorio();
var ajax=nuevoAjax();
ajax.open("get","operaciones.php?funcion=buscarusuario&operador="+operador+"&contador="+contador,true);
	ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
	ajax.send();
	
	ajax.onreadystatechange=function()
			{	
				if (ajax.readyState==4)
				{
				var id_sesion=ajax.responseText;
				aceptar(id_sesion); alert(id_sesion);
				}
			}

}




function aceptar(id_sesion)
{
	alert(1);
}


function encender_operador(operador)
{

		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=encenderoperador&operador="+operador+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{ 
						if(ajax.responseText=="1") operador_online();
						}
						
						
					}

}

function apagar_operador(operador)
{

		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=apagaroperador&operador="+operador+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
						if(ajax.responseText=="1") operador_offline();
						}
						
					}

}



function operador_online()
{
var obj=document.getElementById("op_offline");
var obj2=document.getElementById("op_online");
obj.style.display="none"; obj2.style.display="block";
}

function operador_offline()
{
var obj=document.getElementById("op_offline");
var obj2=document.getElementById("op_online");
obj.style.display="block"; obj2.style.display="none";
}


function operador_verificar_estado_operador(operador)
{
	
		
		
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=operador_verificar_estado_operador&operador="+operador+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
						if(ajax.responseText=="1")  operador_online(); else operador_offline();	
						}
						
					}
}


function usuario_verificar_estado_operador(operador)
{
	
		
		
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=usuario_verificar_estado_operador&operador="+operador+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
						if(ajax.responseText=="1")  operador_online(); else operador_offline();	
						}
						
					}
}

function consulta_nombre(operador)
{
window.open("chatusuario.php?operador="+operador,"chat_us","width=450, height=300, scrollbars=no, menubar=no, location=no, resizable=no") ;
}

function iniciar_sesion(operador,nombre)
{
	
	
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=iniciar_sesion&operador="+operador+"&nombre="+nombre+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
						//segunestado_usuario(ajax.responseText);
						var interval_us_1=setInterval("segunestado_usuario("+ajax.responseText+");",3000);								
						sesionunica=ajax.responseText;
						tomar("mensaje").disabled=false;
						}
						
					}
}




function verificar_solicitud(operador)
{
	
		ultimo_ingreso(operador);
			
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=verificar_solicitud&operador="+operador+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
						if (ajax.responseText>0)
						{
							
						window.open("chat_operador.php?operador="+operador+"&id="+ajax.responseText,"_blank","width=450, height=300, scrollbars=no, menubar=no, location=no, resizable=no") ;
						
						}
						}
						
					}
}

function ultimo_ingreso(operador)
{
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=ultimo_ingreso_operador&operador="+operador+"&contador="+contador,true);
		ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
		ajax.send();	
	
}

function tomar(id)
{
return document.getElementById(id);	
}



function acepta_conversacion(id)
{
	
	var nombre=tomar("nombre");
	if (nombre.value>"")
	{	
		nombreoperador=nombre.value;
		enviarnombre_operador(id,nombre.value);
		var chat=tomar("chat");
		var consulta=tomar("consulta");
		consulta.style.display="none";
		chat.style.display="block";
		cambiarestado(id,3);
		var interval_op_1=setInterval("segunestado_operador("+id+");",3000);
		che=1;		
	}
}


function enviarnombre_operador(id,nombre)
{
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=nombre_operador&id="+id+"&nombre="+nombre+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
						}
						
					}
}

function rechaza_conversacion(id)
{
	
	cambiarestado(id,7);
	window.close();
}



function cambiarestado(idsesion,estado)
{
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=cambiarestado&estado="+estado+"&id="+idsesion+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
						
}





function segunestado_usuario(idsesion)
{
	
		
		var contador2=aleatorio();
		var ajax2=nuevoAjax();
		ajax2.open("get","operaciones.php?funcion=operadoronline&id="+idsesion+"&contador="+contador2,true);
		ajax2.setRequestHeader("Content-Type", "text/html; charset=utf-8");
		ajax2.send();
		ajax2.onreadystatechange=function()
					{	
						if (ajax2.readyState==4)
						{ 
							if (ajax2.responseText==2) 
							{ 
							//alert("Cerro ! - "+ajax2.responseText);
							cambiarestado(idsesion,5);
							var tt=document.frames[0].document.getElementById("contenido")
							.innerHTML;	
							document.frames[0].document.getElementById("contenido").innerHTML+="<BR><b>[ "+nombreoperador+" ha salido del chat ]</b><br>";
							document.frames[0].document.getElementById("marca").scrollIntoView(true);						
							}
						}
					}
					
		
		
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=verestado&id="+idsesion+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
							//alert(idsesion+"---"+ajax2.responseText);
							var estado=ajax.responseText;
							if (estado==3)
							{
							//alert(1);
							var contenido=tomar("contenido");
							contenido.innerHTML=
							"<iframe name='iframeu' id='iframeu' src='i_usuario.php?id="+idsesion+"' frameborder='0' width='400' height='130' scrolling='yes'  class='lienafinagris'></iframe>";
							abrirchat_usuario(idsesion);
							usuario_enchat(idsesion);
							setInterval("usuario_enchat("+idsesion+");",3000);
							cambiarestado(idsesion,4);
							//buscarnombre(idsesion);							
							}
							
							
							
							
							if (estado==7)
							{
							tomar("aguarde").style.display="none";
							tomar("rechazo").style.display="block";
							setTimeout("window.close();",10000);
							}
							
							
						}
						
					}
	
}









function segunestado_operador(idsesion)
{
		var contador2=aleatorio();
		var ajax2=nuevoAjax();
		ajax2.open("get","operaciones.php?funcion=usuarioonline&id="+idsesion+"&contador="+contador2,true);
		ajax2.setRequestHeader("Content-Type", "text/html; charset=utf-8");
		ajax2.send();
		ajax2.onreadystatechange=function()
					{	
						if (ajax2.readyState==4)
						{ 
							if (ajax2.responseText==2) 
							{ 
							cambiarestado(idsesion,5);
							var tt=document.frames[0].document.getElementById("contenido")
							.innerHTML;	
							document.frames[0].document.getElementById("contenido").innerHTML+="<BR><b>[ "+nombreusuario+" ha salido del chat ]</b><br>";
							document.frames[0].document.getElementById("marca").scrollIntoView(true);					}
						}
					}
	
	
	
	
	
	
}










function abrirchat_usuario(idsesion)
{
var chat=tomar("chat");
var aguarde=tomar("aguarde");
chat.style.display="block";
aguarde.style.display="none";

			var contador=aleatorio();
					var ajax=nuevoAjax();
					ajax.open("get","operaciones.php?funcion=vernombre_operador&id="+idsesion+"&contador="+contador,true);
						ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
						ajax.send();
						
						ajax.onreadystatechange=function()
								{	
									if (ajax.readyState==4)
									{
									txt1=unescape(ajax.responseText);
									txt2=txt1.replace(/\+/gi," ");
									tomar("nombre_operador").innerHTML=txt2;
									nombreoperador=txt2;
									}
									
								}
}










function mensaje_usuario(nombre)
{
		//alert(sesionunica);
		var mensaje=tomar("mensaje");
		
		if (mensaje.value>"")
		{
		var txt5=mensaje.value; 
		var textomensaje=txt5.replace(/\s*[\r\n][\r\n \t]*/g, "<br>"); 
		var tt=document.frames[0].document.getElementById("contenido").innerHTML;
		tt+=textomensaje;
		document.frames[0].document.getElementById("contenido").innerHTML+="<b>"+nombre+"</b>: "+textomensaje+"<br>";
		document.frames[0].document.getElementById("marca").scrollIntoView(true);
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=enviomensaje_usuario&id="+sesionunica+"&mensaje=<b>"+nombre+"</b>: "+textomensaje+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
							
							
						}
						
					}
		}
		mensaje.value="";
		mensaje.focus();
		
		
	
}



function mensaje_operador(operador,idsesion)
{
		
		var mensaje=tomar("mensaje");
		
		if (mensaje.value>"")
		{
			var txt5=mensaje.value; 
			var textomensaje=txt5.replace(/\s*[\r\n][\r\n \t]*/g, "<br>"); 
			var tt=document.frames[0].document.getElementById("contenido").innerHTML;
		tt+=textomensaje;
		document.frames[0].document.getElementById("contenido").innerHTML+="<b>"+nombreoperador+"</b>: "+textomensaje+"<br>";
		document.frames[0].document.getElementById("marca").scrollIntoView(true);
			
			
			var contador=aleatorio();
			var ajax=nuevoAjax();
			ajax.open("get","operaciones.php?funcion=enviomensaje_operador&id="+idsesion+"&mensaje="+textomensaje+"&contador="+contador,true);
				ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
				ajax.send();
				
				ajax.onreadystatechange=function()
						{	
							if (ajax.readyState==4)
							{
															
							}
							
						}
		mensaje.value="";
		mensaje.focus();
		}
		
		
		
	
}



function ver_mensajes_usuario(idsesion)
{
	
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=vermensajes_usuario&id="+idsesion+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
					 	var contenido=tomar("contenido");
						if(ajax.responseText>"") 
						{
						txt1=unescape(ajax.responseText);
						txt2=txt1.replace(/\+/gi," ");
						contenido.innerHTML+=txt2;
						tomar("marca").scrollIntoView(true); }
						
						}
						
					}
	
}


function ver_mensajes_operador(idsesion)
{
	
		var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=vermensajes_operador&id="+idsesion+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
			ajax.onreadystatechange=function()
					{	
						if (ajax.readyState==4)
						{
					 	var contenido=tomar("contenido");
						if(ajax.responseText>"") 
						{
							
						txt1=unescape(ajax.responseText);
						txt2=txt1.replace(/\+/gi," ");
						contenido.innerHTML+=txt2;
						tomar("marca").scrollIntoView(true);
						}
						}
						
					}
	
}



function usuario_enchat(id)
{
	var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=usuario_enchat&id="+id+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
}

function operador_enchat(id)
{
	var contador=aleatorio();
		var ajax=nuevoAjax();
		ajax.open("get","operaciones.php?funcion=operador_enchat&id="+id+"&contador="+contador,true);
			ajax.setRequestHeader("Content-Type", "text/html; charset=utf-8");
			ajax.send();
			
}



function cierra_ventana_op(id)
{
/*	
clearInterval(interval_op_1);
clearInterval(interval_op_2);
cambiarestado(id,5);*/
}


function cierra_ventana_us(id)
{
	/*
cambiarestado(id,5);
clearInterval(interval_us_1);
clearInterval(interval_us_2);*/
}

