// JavaScript Document
//elemento script creato e distrutto dinamicamente
var jsel;
//elemento che permette di inviare un messaggio relativo alla chat
var boolAction = false;
//elemento che identifica il tipo di azione 0 = nessuna azione
var action = 0;
 
//impostazioni per il decay delle richieste
var defaultTimeout = 10000;
var defaultDecay = 1000;
var defaultCounter = 1;
var timeout = defaultTimeout;
var counter = defaultCounter;
 
//timer per la ritrasmissione se si perde un pacchetto
var verifyChat;
 
var chatPopup;
 
var xmlHttpPostChatFunc = new Function;
 
function chatInitOnload()
{
	chatPopup = document.createElement("div");
	chatPopup.setAttribute("id", "chatPopup");
	chatPopup.style.position = "absolute";
	chatPopup.style.display = 'none';
	chatPopup.style.zIndex = "100";
	chatPopup.innerHTML = document.getElementById('operatorChatRequest').innerHTML;
	window.document.body.appendChild(chatPopup);
	document.getElementById('operatorChatRequest').innerHTML = ''; 
	
	generateScript();
}
 
function xmlhttpPostChat(strURL, query_string) 
{
	var xmlHttpReq = false;
	var self = xmlHttpPostChatFunc;
	// Mozilla/Safari
	if(window.XMLHttpRequest) 
	{
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if(window.ActiveXObject) 
	{
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	self.xmlHttpReq.open('GET', strURL, true);
	//self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	self.xmlHttpReq.onreadystatechange = function() 
	{	
		if (self.xmlHttpReq.readyState == 4) 
		{
			eval(self.xmlHttpReq.responseText);
		}
	}
	self.xmlHttpReq.send(query_string);
}
 
function generateScript()
{
	var callSrc = '';
	if(boolAction == false)
	{
		callSrc = 'proxy_chat.php?id_attivita=' + idAttivita + '&id_chat=' + idChat + '&action=';
	}
	else
	{
		switch(action)
		{
			case 1://chat rifiutata
				callSrc = 'proxy_chat.php?id_attivita=' + idAttivita + '&id_chat=' + idChat + '&action=chat_refused';
			break;
			
			case 2://chat accettata, apertura finestra chat
				callSrc = 'proxy_chat.php?id_attivita=' + idAttivita + '&id_chat=' + idChat + '&action=chat_accepted';			
			break;
		}
			
		boolAction = false;
	}
	
	xmlhttpPostChat(callSrc, '');
	verifyChat = setTimeout("generateScript()", 22000);
}
 
function evalScript(value, message)
{
	switch(value)
	{
		case 0:
			document.getElementById('chatPopup').style.display = 'none';
			
			if(((counter % 12) == 0) && (timeout <= (2 * defaultTimeout)))
			{
				timeout += 	defaultDecay;
			}
			counter++;
		break;
		case 1:
			if(document.getElementById('chatPopup').style.display == 'none')
			{
				displayChatRequest(message);
			}
			
			timeout = defaultTimeout;
			counter = defaultCounter;
		break;
		case 2:
			window.open('http://www.abc.sm/instant_messaging/client/client_chat.php?id_attivita=' + idAttivita + '&id_chat=' + idChat + '&sessid=' + sessionId + '&lang=' + chatLanguage, 'chat', 'width=350, height=400');
		
			timeout = defaultTimeout;
			counter = defaultCounter;
		break;
	}
	
	clearTimeout(verifyChat);
	setTimeout("generateScript()", timeout);
}
 
function requestChat()
{
	window.open('http://www.abc.sm/instant_messaging/client/client_chat.php?id_attivita=' + idAttivita + '&id_chat=' + idChat + '&sessid=' + sessionId + '&lang=' + chatLanguage, 'chat', 'width=350, height=400');	
}
 
function acceptChat()
{
	boolAction = true;
	action = 2;
	window.open('http://www.abc.sm/instant_messaging/client/client_chat.php?id_attivita=' + idAttivita + '&id_chat=' + idChat + '&sessid=' + sessionId, 'chat', 'width=350, height=400');	
	document.getElementById('chatPopup').style.display = 'none';
	document.getElementById('operatorChatRequestMessage').innerHTML = '';
}
 
function refuseChat()
{
	boolAction = true;
	action = 1;
	document.getElementById('chatPopup').style.display = 'none';
	document.getElementById('operatorChatRequestMessage').innerHTML = '';
}
 
function displayChatRequest(message)
{
	var windowWidth = 0;
	var windowHeight = 0;
	
	if(typeof( window.innerWidth ) == 'number' ) 
	{
    	//Non-IE
    	windowWidth = window.innerWidth;
    	windowHeight = window.innerHeight;
  	} 
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
	{
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
  	} 
	else if(document.body && (document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
  	}
	
	var scrollOffsetY = 0;
	
	if(typeof(window.pageYOffset) == 'number') 
	{
    	//Netscape compliant
    	scrollOffsetY = window.pageYOffset;
	} 
	else if(document.body && document.body.scrollTop)
	{
    	//DOM compliant
    	scrollOffsetY = document.body.scrollTop;
	} 
	else if(document.documentElement && document.documentElement.scrollTop)
	{
    	//IE6 standards compliant mode
    	scrollOffsetY = document.documentElement.scrollTop;
  	}
	
	document.getElementById('operatorChatRequestMessage').innerHTML = message;
	
	document.getElementById('chatPopup').style.display = 'block';		
	
	var divWidth = document.getElementById('chatPopup').offsetWidth;
	var divHeight = document.getElementById('chatPopup').offsetHeight;
	var divLeft = Math.round(windowWidth / 2) - Math.round(divWidth / 2);
	var divTop = Math.round(windowHeight / 2) - Math.round(divHeight / 2) + scrollOffsetY;
	
	document.getElementById('chatPopup').style.left = divLeft + 'px';
	document.getElementById('chatPopup').style.top = divTop + 'px';
}
 
function addOnloadEvent(fnc)
{
	if(typeof window.addEventListener != "undefined")
		window.addEventListener( "load", fnc, false );
	else if(typeof window.attachEvent != "undefined") 
	{
		window.attachEvent("onload", fnc);
	}
	else 
	{
		if(window.onload != null) 
		{
			var oldOnload = window.onload;
			window.onload = function(e) 
							{
								oldOnload( e );
								window[fnc]();
							};
		}
		else
			window.onload = fnc;
	}
}
 
addOnloadEvent(chatInitOnload);// JavaScript Document
