/* Copyright(C) 2005,2006 Salvatore Sanfilippo <antirez@gmail.com>
 * All Rights Reserved.
 * This code is releaed under the GPL license version 2.0 */

var agt = navigator.userAgent.toLowerCase();
var is_ie5 = (agt.indexOf('msie 5') != -1);
var is_ie = document.all?true:false

// Create the XML HTTP request object. We try to be
// more cross-browser as possible.
function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}

// An handler that does nothing, used for AJAX requests that
// don't require a reply and are non-critical about error conditions.
function DummyHandler() {
    return true;
}

// Shortcut for creating a GET request and get the reply
// This few lines of code can make Ajax stuff much more trivial
// to write, and... to avoid patterns in programs is sane!
var ajax_req = null;


function ajaxGet(url,handler) {
    var a = new Array("placeholder");
    for (var j=2; j<arguments.length; j++) {
        a[a.length] = arguments[j];
    }
    var myhandler = function() {
        var content = ajaxOk();
        if (content != false) {
            a[0] = content;
            try {
                return handler.apply(this, a);
            } catch(e) {
                return myDummyApply(handler, a);
            }
        }
    }
    url+='&rectype=AJAX';
    ajax_req = CreateXmlHttpReq(myhandler);
    ajax_req.open("GET",url);
    ajax_req.setRequestHeader( 
	    'Content-Type', 
	    'application/x-www-form-urlencoded; charset=UTF-8' 
	);
    ajax_req.send(null);
}

function ajaxPost(url,formname,handler) {
	url += (url.indexOf("?") == -1) ? "?" : "&amp;";
    url += "rand="+escape(Math.random());
    var a = new Array("placeholder");
    for (var j=3; j<arguments.length; j++) {
        a[a.length] = arguments[j];
    }
    var myhandler = function() {
        var content = ajaxOk();
        if (content != false) {
            a[0] = content;
            try {
                return handler.apply(this, a);
            } catch(e) {
                return myDummyApply(handler, a);
            }
        }
    }
	var fields = new Array();
	//loop through form elements and retrieve field NAMEs and Values
	for (var x = 0; x < eval("document."+formname+".elements.length"); x++){
		// join them into a string.
		if (encodeURIComponent) {
    		eval("fields.push(document."+formname+".elements[x].name+'='+encodeURIComponent(document."+formname+".elements[x].value))");
		} else {
			eval("fields.push(document."+formname+".elements[x].name+'='+escape(document."+formname+".elements[x].value))");
		}
		
	}
	var sendf = fields.join('&');
    ajax_req = CreateXmlHttpReq(myhandler);
    ajax_req.open("POST",url,true);
    ajax_req.setRequestHeader( 
	    'Content-Type', 
	    'application/x-www-form-urlencoded; charset=UTF-8' 
	);
    ajax_req.send(sendf);
}

function ajaxXML(url,handler){
   var a = new Array("placeholder");
    for (var j=2; j<arguments.length; j++) {
        a[a.length] = arguments[j];
    }
    var myhandler = function() {
        var content = ajax_req.responseXML;
        if (content != null) {
            a[0] = content;
            try {
                return handler.apply(this, a);
            } catch(e) {
                return myDummyApply(handler, a);
            }
        }
    }
    ajax_req = CreateXmlHttpReq(myhandler);
    ajax_req.open("GET",url);
    ajax_req.setRequestHeader( 
	    'Content-Type', 
	    'application/x-www-form-urlencoded; charset=UTF-8' 
	);
    ajax_req.send(null);
    
}

// IE 5.0 does not support the apply() method of the function object,
// we resort to this eval-based solution that sucks because it is not
// capable of preserving 'this' and is ugly as hell, but it works for us.
function myDummyApply(funcname,args) {
    var e = "funcname(";
    for (var i = 0; i < args.length; i++) {
        e += "args["+i+"]";
        if (i+1 != args.length) {
            e += ",";
        }
    }
    e += ");"
    return eval(e);
}

// Add a random parameter to the get request to avoid
// IE caching madness.
function ajaxGetRand(url,handler) {
    url += (url.indexOf("?") == -1) ? "?" : "&amp;";
    url += "rand="+escape(Math.random());
    arguments[0] = url;
    try {
        return ajaxGet.apply(this,arguments);
    } catch(e) {
        return myDummyApply(ajaxGet,arguments);
    }
}

function ajaxPostRand(url,handler) {
    url += (url.indexOf("?") == -1) ? "?" : "&amp;";
    url += "rand="+escape(Math.random());
    arguments[0] = url;
    try {
        return ajaxPost.apply(this,arguments);
    } catch(e) {
        return myDummyApply(ajaxPost,arguments);
    }
}

function ajaxOk() {
    if (ajax_req.readyState == 4 && ajax_req.status == 200) {
        return ajax_req.responseText;
    } else {
        return false;
    }
}
function Ajax_Replace(content,elemntID){
	var e = document.getElementById(elemntID);
	if (content!="EMPTY"){
		e.innerHTML = content;
   	}else{
   		e.innerHTML = "";
   	}
}
function Ajax_ReplacePage(content){
	if (content!="EMPTY")
   		document.innerHTML = content;
   	else
   		document.innerHTML = "";
}
function Ajax_Append(content,elemntID){
	var e = document.getElementById(elemntID);
	if (content!="EMPTY")
   		e.innerHTML += content;
   	else
   		e.innerHTML += "";
}

function Ajax_Allert(content){
	if (content!="OK"){
		alert(content);
	}
}

function Ajax_Redirect(content){
	location.href=content;
}

function Ajax_Reload(content){
	window.location.reload();
}

function Ajax_Noop(content){
	return true;
}

/* Tooltip */

function Ajax_Tooltip(content,id){
	//Elimino lo scroll nella pagina
	document.body.style.overflow = "hidden";
	//Creo il div di sfondo
	var div_sf = document.createElement('div');
	div_sf.setAttribute('id',id+'_sf');
	div_sf.className = 'tooltip_sf';
	//Creo il div in primo piano
	var div = document.createElement('div');
	div.setAttribute('id',id);
	div.className = 'tooltip';
	div.innerHTML+=content;
	//Creo la X per chiudere il div
	var close = document.createElement('img');
	close.setAttribute('id',id+'_close')
	close.className = 'tooltip_close';
	close.src='img/chiudi.png';
	var onC = 'Ajax_Tooltip_close("'+id+'")';
	close.onclick = new Function(onC);
	document.body.appendChild(div_sf);
	document.body.appendChild(div);
	document.body.appendChild(close);
}

function Ajax_Tooltip_close_helper(content,id){
	Ajax_Tooltip_close(id);
}

function Ajax_Tooltip_close(id){
	//Riabilito lo scroll nella pagina
	document.body.style.overflow = "";
	var div_sf = document.getElementById(id+'_sf');
	var div = document.getElementById(id);
	var close = document.getElementById(id+'_close');
	div_sf.parentNode.removeChild(div_sf);
	div.parentNode.removeChild(div);
	close.parentNode.removeChild(close);
}
