// restituisce il valore del cookie sNome
function GetCookie_Spy(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) {
      return (unescape(asCookie[1]));
    }
  }

  // SE non esiste il cookie richiesto
  return("");
}

/*Ajax*/
var http_request = false;

function makePOSTRequest_Spy(url, parameters, handler)
{
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	}
	else if (window.ActiveXObject)
	{ // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = handler;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}


/* Funzioni che effettuano la chiamata http*/
function makepost_Spy ()
{
	var form = document.userlog;

	Stringa = "&id_utente="+GetCookie_Spy('ChatForumUID');
	makePOSTRequest_Spy("/include/general/exe_spy_monitor.php",Stringa,myElaboraCerca_Spy);
	Timer_generico = setTimeout('makepost_Spy()',(30*1000));
}


timer = new Array();
function myElaboraCerca_Spy ()
{
	//strform=return_str_form();

	// Se ho avuto risposta dal server
	if (http_request.readyState == 4 && http_request.status == 200)
	{
		//Converto la stringa Json in vettore Javascript
		var myJSONText = eval('json = ' + http_request.responseText);
		var Testo = myJSONText[0];

		if ((Testo != "NO")&&(Testo != "nologged")) {
			document.getElementById("AjaxSpyMonitor").innerHTML= Testo;
			//window.setTimeout("AAA(0)",1);
		}
	}
	else
	{
		//non ho ancora effettuato il ritorno dalla pagina phph in ajax


	}
}

//FUNZIONI PHP.JS
function explode( delimiter, string, limit ) {
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function str_replace(search, replace, subject) {
    
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
    };
     
    return sa ? s : s[0];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

function AAA (G) {


	K = getScrollXY()
	A = K-G;
	B = substr(document.getElementById('IFRAME_AJAX').style.top,0,-2);
	C = parseInt(A) + parseInt(B);
	
	document.getElementById('IFRAME_AJAX').style.top =  C+'px' ;
	document.getElementById('IFRAME_AJAX').style.left = '-420px' ;
	window.setTimeout("AAA("+K+")",1);
}



function substr( f_string, f_start, f_length ) {
 
    f_string = f_string+'';
    
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}

//chiudo la finestra di avviso
function close(ID) {
	document.getElementById('SpyMonitor'+ID).style.visibility='hidden';
}

//funzioni per il timer
function stop_time(ID) {
	NomeTimer = 'Timer' + ID;
	clearInterval(window[NomeTimer]);
	
}	
function start_time(ID,TIME) {
	if (!event.fromElement.contains(event.toElement) &&!document.getElementById('SpyMonitor'+ID).contains(event.toElement))  {
		NomeTimer = 'Timer' + ID;
		window[NomeTimer] = setTimeout('close('+ID+')', TIME);
	}
}	

makepost_Spy();

