 
var Browser = new Object();
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("Browser.isSafari")!=-1);
Browser.isOpera = (typeof window.opera != 'undefined'); 

function str_replace(search, replace, subject) {
	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
	while (j = 0, i--) {
		if (s[i]) {
			while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
		}
	};
	return sa ? s : s[0];
}

function in_array(myvalue, myarray) {
    var found = false, i;
 
    for (i in myarray) {
        if (myarray[i] == myvalue) {
            found = true;
            break;
        }
    }
 
    return found;
}

function array_search( myvalue, myarray ) {
    var i = '';
 
    for (i in myarray) {
        if ( myarray[i] == myvalue ){
            return i;
        }
    }
 
    return false;
}

function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (value instanceof Array) {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

function strpos(mystr, findtxt) {
    return (mystr+'').indexOf(findtxt, 0);
}

function URLDecode( encoded )
{
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	}
   return plaintext;
};

function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}


function SendAsyncRequest(method, url, mycallback, myform, retrynum, IsFileUpload, allowedExtensionsRegExp, allowedExtensionsErrMsg ) 
{
	if (IsFileUpload) {
		SendAsyncRequestAdvanced(method, url, mycallback, myform, retrynum, allowedExtensionsRegExp, allowedExtensionsErrMsg );
	} else {
		SendAsyncRequestBasic(method, url, mycallback, myform, retrynum, allowedExtensionsRegExp, allowedExtensionsErrMsg );
	}
}

function SendAsyncRequestBasic(method, url, mycallback, myform, retrynum, allowedExtensionsRegExp, allowedExtensionsErrMsg ) {
	var http = null;
	var data = "";
	var IsOKToSubmit = true;
	http = createRequest();
	
	var myRetryNum;
	if (retrynum === undefined) {
		myRetryNum = 0;
	} else {
		myRetryNum = parseInt(retrynum);
	}
	myRetryNum++;

	var curMethod = method;
	var curUrl = url; 
	var curCallback = mycallback; 
	var curForm = myform;
	var callback = function(){
		if (http.readyState == 4) 
		{
			var status = "";
			try {
			  status = http.status;
			}
			catch(e) { // firefox 2.0 workaround
				if (myRetryNum <= 3) {
					status = -1;
					setTimeout(function() {
						SendAsyncRequest(curMethod, curUrl, curCallback, curForm, myRetryNum, allowedExtensionsRegExp, allowedExtensionsErrMsg );
					}, 100);
				} else { // give-up
					status = 200;
				}
			}
		
			if (status < 200) { 		// 1xx Informational --> do nothing
				alert("Warning: status code is " + status);
			} else if (status < 300) {	// 2xx Success
				if (strpos(http.responseText, "parent.location='index.php'") == -1) {
					mycallback(http);
				} else {
					var mydocument = top.document;
					while (mydocument.parent) {
						mydocument = mydocument.parent;
					}
					mydocument.location = "index.php";
					alert("Sessione Scaduta!");
				}
			} else {						// 3xx Redirection
											// 4xx Client Error
											// 5xx Server Error			
				alert("Error: status code is " + status);
			}
		}
	}
	
	if (http) {
		http.open(method, url, true);
		http.onreadystatechange = callback;
		var mysep = "";
		if (method == "GET") {
			http.send(data);
		} else if (method == "POST") {
			var theform = document.getElementById(myform);
			if (!theform) {
				alert("Errore #65432: il Form '"+myform+"' non esiste!");
			} else {
				var elem = theform.elements;
				for(var i = 0; i < elem.length; i++)
				{
					switch(elem[i].type)
					{
						case 'file':
						case 'hidden':
						case 'text':
						case 'password':                                                
						case 'button':
						case 'reset':
						case 'submit':
						case 'textarea':
							if (data) data += "&";
							data += "" + encodeURIComponent(elem[i].name) + "=" + encodeURIComponent(elem[i].value);
							break;
						case 'select-one':
                            if (elem[i].options.length > 0){
                                if (data) data += "&";
                                data += "" + encodeURIComponent(elem[i].name) + "=" + encodeURIComponent(elem[i].options[elem[i].selectedIndex].value);
                            }
							break;
						case 'select-multiple':
							if (data) data += "&" + encodeURIComponent(elem[i].name) + "=";
							mysep = "";
							for(var j=0;j<elem[i].options.length;j++) {
								if(elem[i].options[j].selected) {
									data += encodeURIComponent(mysep + elem[i].options[j].value);
									if (!mysep) mysep = ",";
								}
							}
							break;
						case 'radio':
						case 'checkbox':
							if(elem[i].checked){
								if (data) data += "&";
								data += "" + encodeURIComponent(elem[i].name) + "=" + encodeURIComponent(elem[i].value);
							}
							break;
						default:
							if (elem[i].type!=undefined)
                                                           alert("ERRORE TIPO NON IMPLEMENTATO: tipo="+elem[i].type);
					}
				}
				if (IsOKToSubmit) {
					http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					http.send(data);
				}
			}
		} else {
			alert("Errore: Method non supportato!");
		}
	} else {
		alert("Errore: createRequest");
	}
}

function GetRandomStr() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz";
	var string_length = 24;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}


var SendAsyncRequestAdvanced_Arr = new Array();

function fileSubmissionReturnVal(retCode)
{
	if (SendAsyncRequestAdvanced_Arr[retCode]) {
		var myObj = SendAsyncRequestAdvanced_Arr[retCode];
		//var myUrl = ((strpos(myObj.url, "?")==-1)?(myObj.url+"?FileTransactionId="+retCode):(myObj.url+"&FileTransactionId="+retCode));
		
		var TheFormEl = document.getElementById(myObj.myform);
		if (TheFormEl) 
		{
			if (TheFormEl.elements["FileTransactionId"]) {
				TheFormEl.elements["FileTransactionId"].value = retCode;
			} else {
				var myInput = document.createElement("input");
				myInput.name = "FileTransactionId";
				myInput.id = "FileTransactionId";
				myInput.type = "hidden";
				myInput.value = retCode;
				TheFormEl.appendChild(myInput);
			}
			SendAsyncRequestBasic(myObj.method, myObj.url, myObj.mycallback, myObj.myform, myObj.retrynum, myObj.allowedExtensionsRegExp, myObj.allowedExtensionsErrMsg );
		}
		SendAsyncRequestAdvanced_Arr.splice(retCode,1); 
	} else {
		alert("return code not defined ["+retCode+"]");
	}
}

function SendAsyncRequestAdvanced(method, url, mycallback, myform, retrynum, allowedExtensionsRegExp, allowedExtensionsErrMsg ) 
{
	var IsOKToSubmit = true;
	var theform = document.getElementById(myform);

	if (!theform) {
		alert("Errore #65433: il Form '"+myform+"' non esiste!");
		IsOKToSubmit = false;
	} else {
		var elem = theform.elements;
		for(var i = 0; i < elem.length; i++) {
			if (elem[i].type == 'file') {
				var filename = elem[i].value;
				if (allowedExtensionsRegExp) {
					if (filename.search(allowedExtensionsRegExp) == -1)	{
						alert(allowedExtensionsErrMsg);
						IsOKToSubmit = false;
					}
				}
			}
		}
	}

	if (IsOKToSubmit) 
	{
		if (document.getElementById("__upload_iframe__")) {
			myIFrame = document.getElementById("__upload_iframe__");
		} else {
			myIFrame = document.createElement("iframe");
			myIFrame.id="__upload_iframe__";
			myIFrame.name="__upload_iframe__";
			myIFrame.style.width="600px";
			myIFrame.style.height="200px";
			myIFrame.style.display="inline";

			//document.getElementById("CheckEdImportaTDIFrame").appendChild(myIFrame); // DEBUG TEMPORANEO
			document.body.appendChild(myIFrame);
		}

		var TId = GetRandomStr();
		SendAsyncRequestAdvanced_Arr[TId] = new Object;
		SendAsyncRequestAdvanced_Arr[TId].method = method;
		SendAsyncRequestAdvanced_Arr[TId].url = url
		SendAsyncRequestAdvanced_Arr[TId].mycallback = mycallback;
		SendAsyncRequestAdvanced_Arr[TId].myform = myform;
		SendAsyncRequestAdvanced_Arr[TId].retrynum = retrynum;
		SendAsyncRequestAdvanced_Arr[TId].allowedExtensionsRegExp = allowedExtensionsRegExp;
		SendAsyncRequestAdvanced_Arr[TId].allowedExtensionsErrMsg = allowedExtensionsErrMsg;

		var theform_action = theform.action;
		var theform_method = theform.method;
		var theform_enctype= theform.enctype;
		var theform_target = theform.target;
		theform.action = "AssoloAjax.php?tid="+TId;
		theform.method = "POST";
		theform.enctype="multipart/form-data";
		theform.target = "__upload_iframe__";
		theform.submit();
		theform.action = theform_action;
		theform.method = theform_method;
		theform.enctype= theform_enctype;
		theform.target = theform_target;
	}
}



function createRequest()
{
	var MSXMLProgId = [ 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP' ];

	var http = false;
	try {
		http = new XMLHttpRequest(); // Instantiates XMLHttpRequest in non-IE browsers and assigns to http.
	} catch(e) {
		for(var i=0; i<MSXMLProgId.length; ++i){
			try {
				http = new ActiveXObject(MSXMLProgId[i]); // Instantiates XMLHttpRequest for IE and assign to http
				break;
			} catch(e2){}
		}
	} finally {
		return http;
	}
}


function SendAsyncRequestFast(url) 
{
/*	var callback = function(){
		if (http.readyState == 4) {
			if (http.status == 200) 
			{
				alert("Got: " + http.responseText);
			} else {
				alert("Error: status code is " + http.status);
			}
		}
	}*/
	
	if (http = createRequest()) {
		http.open('GET', url, true);
		//http.onreadystatechange = callback;
		http.send("");
	}
}

function ShowDivInfoImmagine(event,id,myConfigs)
{
	var mydiv = document.getElementById("__DivInfoImmagine__"+id);
	var OnlyShow = (mydiv)?true:false;
	
	var mydivs = document.getElementsByTagName("div");
	for (i=0; i< mydivs.length; i++) 
	{
		if (mydivs[i] && mydivs[i].id) 
		{
			if ((mydivs[i].id).indexOf("__DivInfoImmagine__") == 0)
			{
				if (mydivs[i].style.display != "none") {
					mydivs[i].style.display = "none";
				}
			}
		}
	}
	
	var DeltaX = 20,
		DeltaY = -100,
		DivWidth = 480,
		DivHeight = 290,
		Padding = 2,
		ClassName = "",
		GetScript = "GetInfoImmagine.php?id="+id,
		mousex = 0,
		mousey = 0;

	if (myConfigs) {
		if (!(myConfigs.DeltaX === undefined ))    DeltaX  = myConfigs.DeltaX;
		if (!(myConfigs.DeltaY === undefined ))    DeltaY  = myConfigs.DeltaY;
		if (!(myConfigs.DivWidth === undefined ))  DivWidth  = myConfigs.DivWidth;
		if (!(myConfigs.DivHeight === undefined )) DivHeight  = myConfigs.DivHeight;
		if (!(myConfigs.Padding === undefined ))   Padding  = myConfigs.Padding;
		if (!(myConfigs.ClassName === undefined ))   ClassName  = myConfigs.ClassName;
		if (!(myConfigs.GetScript === undefined ))   GetScript  = myConfigs.GetScript;
	}

	ev = event || window.event;

	if (ev.pageX || ev.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
	  mousex = ev.pageX;
	  mousey = ev.pageY;
	} else if (ev.clientX || ev.clientY) { // works on IE6,FF,Moz,Opera7
	  mousex = ev.clientX;
	  mousey = ev.clientY;
	}
	
	var myWidth = 0, 
		myHeight = 0;

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

	if (!OnlyShow) 
	{
		var mydiv = document.createElement("DIV");
		mydiv.id = "__DivInfoImmagine__"+id;

		if (ClassName) {
			mydiv.className = ClassName;
		} else {
			mydiv.style.background = "#FFFFFF";
			mydiv.style.border = "1px solid black";
			mydiv.style.padding = Padding+"px";
			mydiv.style.fontFamily = "Arial";
			mydiv.style.fontSize = "12px";
		}
		
		mydiv.style.position = "absolute";
		mydiv.style.width = DivWidth+"px";
		mydiv.style.height = DivHeight+"px";
		mydiv.style.overflow = "hidden";
	}
	
	if  (myWidth < (mousex+DeltaX+DivWidth+2*Padding)) {
		mydiv.style.left = ""+(mousex-DeltaX-DivWidth-2*Padding)+"px";
	} else {
		mydiv.style.left = ""+(mousex+DeltaX)+"px";
	}
	if  ((mousey+DeltaY) < (document.body.scrollTop + document.documentElement.scrollTop)) {
		mydiv.style.top = ""+(document.body.scrollTop + document.documentElement.scrollTop)+"px";
	} else if  (myHeight < (mousey+DeltaY+DivHeight+2*Padding)) {
		mydiv.style.top = ""+(myHeight-DivHeight-2*Padding)+"px";
	} else {
		mydiv.style.top = ""+(mousey+DeltaY)+"px";
	}

	/* begin fix position */
	/* ################################################################# */
	var coordX1 = parseInt(mydiv.style.left);
	var coordY1 = parseInt(mydiv.style.top);
	var coordX2 = parseInt(mydiv.style.left)+DivWidth;
	var coordY2 = parseInt(mydiv.style.top)+DivHeight;

	if ((mousex >= coordX1) && (mousex <= coordX2) && (mousey >= coordY1) && (mousey <= coordY2))  
	{
		//alert(""+mousey+"-"+DeltaY+"-"+DivHeight+" > ("+document.body.scrollTop+" + "+document.documentElement.scrollTop+"");
		
		if (mousey-Math.abs(DeltaY)-DivHeight > (document.body.scrollTop + document.documentElement.scrollTop)) {
			mydiv.style.top = ""+(mousey-Math.abs(DeltaY)-DivHeight)+"px";
		} else {
			mydiv.style.top = ""+(mousey+Math.abs(DeltaY)-DivHeight)+"px";
		}
		
		coordX1 = parseInt(mydiv.style.left);
		coordY1 = parseInt(mydiv.style.top);
		coordX2 = parseInt(mydiv.style.left)+DivWidth;
		coordY2 = parseInt(mydiv.style.top)+DivHeight;

		if ((mousex >= coordX1) && (mousex <= coordX2) && (mousey >= coordY1) && (mousey <= coordY2))  
		{
			if (mousex-Math.abs(DeltaX)-DivWidth > (document.body.scrollLeft + document.documentElement.scrollLeft)) {
				mydiv.style.left = ""+(mousex-Math.abs(DeltaX)-DivWidth)+"px";
			} else {
				mydiv.style.left = ""+(mousex+Math.abs(DeltaX))+"px";
			}
		
			coordX1 = parseInt(mydiv.style.left);
			coordY1 = parseInt(mydiv.style.top);
			coordX2 = parseInt(mydiv.style.left)+DivWidth;
			coordY2 = parseInt(mydiv.style.top)+DivHeight;

			if ((mousex >= coordX1) && (mousex <= coordX2) && (mousey >= coordY1) && (mousey <= coordY2))  
			{
				mydiv.style.top = ""+(mousey+1)+"px";
				mydiv.style.left = ""+(mousex+1)+"px";
			}
		}
	}
	
	if (parseInt(mydiv.style.left) < (document.body.scrollLeft + document.documentElement.scrollLeft)) {
		mydiv.style.left = ""+(document.body.scrollLeft + document.documentElement.scrollLeft)+"px";
	} else {
		if (parseInt(mydiv.style.left) > (myWidth-DivWidth)) {
			mydiv.style.left = ""+(myWidth-DivWidth)+"px";
		}
	}
	/* ################################################################# */
	/* end fix position */
	
//top.document.getElementById("div_righttab_Stili").innerHTML = "show<br>";
	
	if (OnlyShow) {
		mydiv.style.display = "inline";	
//top.document.getElementById("div_righttab_Stili").innerHTML = "onlyshow<br>";
	} else {
//top.document.getElementById("div_righttab_Stili").innerHTML = "create<br>";
		var myCallback = function(o) { 
			mydiv.innerHTML = o.responseText;
		}
		SendAsyncRequest('GET', ""+GetScript+"", myCallback);
		document.body.appendChild(mydiv);
		
		var CurId = id;
		function CheckDivInfoImmagine(evt)
		{
			if (mydiv.style.display != "none") 
			{
				var targ = document.getElementById("InfoImmagine_"+CurId);
				if (targ) {
					var box = targ.getBoundingClientRect(), 
			
					ev = evt || window.event;

					if (ev.pageX || ev.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
					  mousex = ev.pageX;
					  mousey = ev.pageY;
					} else if (ev.clientX || ev.clientY) { // works on IE6,FF,Moz,Opera7
					  mousex = ev.clientX;
					  mousey = ev.clientY;
					}

					if ((box.left || box.right) && (box.top || box.bottom)) {
						if ((mousex < box.left) || (mousex > box.right) || (mousey < box.top) || (mousey > box.bottom) ) 
						{
							mydiv.style.display = "none";
							//top.document.getElementById("div_righttab_Stili").innerHTML = "da cancellare<br>";
							//top.document.getElementById("div_righttab_Stili").innerHTML = "("+mousex+" < "+box.left+") || ("+mousex+" > "+box.right+") || ("+mousey+" < "+box.top+") || ("+mousey+" > "+box.bottom+")";
						}
					} else {
						alert("ERRORE targ IS ZERO!");
						mydiv.style.display = "none";
					}
				} else {
					//alert("ERRORE targ NOT FOUND!");
					mydiv.style.display = "none";
				}
			}
		}
		
		try {
			top.document.addEventListener('mousemove',CheckDivInfoImmagine,true);
		} catch(err) {
			top.document.attachEvent('onmousemove', CheckDivInfoImmagine);
		}
		
		function unloadIt(evt) {
			try {
				top.document.removeEventListener('mousemove',CheckDivInfoImmagine,true);
			} catch(err) {
				top.document.detachEvent('onmousemove', CheckDivInfoImmagine);
			}
		}
		try {
			document.addEventListener('unload',unloadIt,true);
		} catch(err) {
			document.attachEvent('onunload', unloadIt);
		}		
	}
}
function HideDivInfoImmagine(id) {
	var mydiv = document.getElementById("__DivInfoImmagine__"+id);
	if (mydiv) {
		//document.body.removeChild(mydiv);
		mydiv.style.display = "none";
	}
}

function MoveDivInfoImmagine(event,id,myConfigs)
{
	var DeltaX = 20,
		DeltaY = -100,
		DivWidth = 480,
		DivHeight = 290,
		Padding = 2,
		mousex = 0,
		mousey = 0,
		ScrollerHeight = 0,
		ScrollerWidth = 0;

	
	if (myConfigs) {
		if (!(myConfigs.DeltaX === undefined ))    DeltaX  = myConfigs.DeltaX;
		if (!(myConfigs.DeltaY === undefined ))    DeltaY  = myConfigs.DeltaY;
		if (!(myConfigs.DivWidth === undefined ))  DivWidth  = myConfigs.DivWidth;
		if (!(myConfigs.DivHeight === undefined )) DivHeight  = myConfigs.DivHeight;
		if (!(myConfigs.Padding === undefined ))   Padding  = myConfigs.Padding;
	}

	ev = event || window.event;
	
	ScrollerHeight = 0;
	ScrollerWidth = 0;
	if (ev.pageX || ev.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = ev.pageX;
      mousey = ev.pageY;
	  ScrollerHeight = getScrollerWidth();
	  ScrollerWidth = getScrollerWidth();
    } else if (ev.clientX || ev.clientY) { // works on IE6,FF,Moz,Opera7
      mousex = ev.clientX;
      mousey = ev.clientY;
	  mousex += (document.body.scrollLeft + document.documentElement.scrollLeft);
	  mousey += (document.body.scrollTop + document.documentElement.scrollTop);
	} else {
      mousex = 0;
      mousey = 0;
	}

	var myWidth = 0, 
		myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {	//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myWidth += (document.body.scrollLeft + document.documentElement.scrollLeft - ScrollerWidth);
		myHeight += (document.body.scrollTop + document.documentElement.scrollTop - ScrollerHeight);
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		myWidth += (document.body.scrollLeft + document.documentElement.scrollLeft);
		myHeight += (document.body.scrollTop + document.documentElement.scrollTop);
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	} else {
		myWidth = 0;
		myHeight = 0;
	}
		
	var mydiv = document.getElementById("__DivInfoImmagine__"+id);
	if (mydiv) {
		if  (myWidth < (mousex+DeltaX+DivWidth+2*Padding)) {
			mydiv.style.left = ""+(mousex-DeltaX-DivWidth-2*Padding)+"px";
		} else {
			mydiv.style.left = ""+(mousex+DeltaX)+"px";
		}
		if  ((mousey+DeltaY) < (document.body.scrollTop + document.documentElement.scrollTop)) {
			mydiv.style.top = ""+(document.body.scrollTop + document.documentElement.scrollTop)+"px";
		} else if  (myHeight < (mousey+DeltaY+DivHeight+2*Padding)) {
			mydiv.style.top = ""+(myHeight-DivHeight-2*Padding)+"px";
		} else {
			mydiv.style.top = ""+(mousey+DeltaY)+"px";
		}
		/*mydiv.innerHTML = "mousex="+mousex+"; mousey="+mousey+"";
		mydiv.innerHTML += "myWidth="+myWidth+"; myHeight="+myHeight+";";
		mydiv.innerHTML += "getScrollerWidth="+getScrollerWidth()+";";*/
		
		
		/* begin fix position */
		/* ################################################################# */
		var coordX1 = parseInt(mydiv.style.left);
		var coordY1 = parseInt(mydiv.style.top);
		var coordX2 = parseInt(mydiv.style.left)+DivWidth;
		var coordY2 = parseInt(mydiv.style.top)+DivHeight;

		if ((mousex >= coordX1) && (mousex <= coordX2) && (mousey >= coordY1) && (mousey <= coordY2))  
		{
			//alert(""+mousey+"-"+DeltaY+"-"+DivHeight+" > ("+document.body.scrollTop+" + "+document.documentElement.scrollTop+"");
			
			if (mousey-Math.abs(DeltaY)-DivHeight > (document.body.scrollTop + document.documentElement.scrollTop)) {
				mydiv.style.top = ""+(mousey-Math.abs(DeltaY)-DivHeight)+"px";
			} else {
				mydiv.style.top = ""+(mousey+Math.abs(DeltaY)-DivHeight)+"px";
			}
			
			coordX1 = parseInt(mydiv.style.left);
			coordY1 = parseInt(mydiv.style.top);
			coordX2 = parseInt(mydiv.style.left)+DivWidth;
			coordY2 = parseInt(mydiv.style.top)+DivHeight;

			if ((mousex >= coordX1) && (mousex <= coordX2) && (mousey >= coordY1) && (mousey <= coordY2))  
			{
				if (mousex-Math.abs(DeltaX)-DivWidth > (document.body.scrollLeft + document.documentElement.scrollLeft)) {
					mydiv.style.left = ""+(mousex-Math.abs(DeltaX)-DivWidth)+"px";
				} else {
					mydiv.style.left = ""+(mousex+Math.abs(DeltaX))+"px";
				}
			
				coordX1 = parseInt(mydiv.style.left);
				coordY1 = parseInt(mydiv.style.top);
				coordX2 = parseInt(mydiv.style.left)+DivWidth;
				coordY2 = parseInt(mydiv.style.top)+DivHeight;

				if ((mousex >= coordX1) && (mousex <= coordX2) && (mousey >= coordY1) && (mousey <= coordY2))  
				{
					mydiv.style.top = ""+(mousey+1)+"px";
					mydiv.style.left = ""+(mousex+1)+"px";
				}
			}
		}
		
		if (parseInt(mydiv.style.left) < (document.body.scrollLeft + document.documentElement.scrollLeft)) {
			mydiv.style.left = ""+(document.body.scrollLeft + document.documentElement.scrollLeft)+"px";
		} else {
			if (parseInt(mydiv.style.left) > (myWidth-DivWidth)) {
				mydiv.style.left = ""+(myWidth-DivWidth)+"px";
			}
		}
		/* ################################################################# */
		/* end fix position */
		
	}
}


function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(
        document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}


function GetMouseXY(event)
{
	ev = event || window.event;

	if (ev) {
		if (ev.pageX || ev.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mousex = ev.pageX;
			mousey = ev.pageY;
		} else { // works on IE6,FF,Moz,Opera7
			mousex = ev.clientX;
			mousey = ev.clientY;
		}
	} else {
		mousex = 0;
		mousey = 0;	
	}	
	return {X:mousex, Y:mousey};
}

function makeAnimation(el, StartX, StartY, StopX, StopY, DeltaX, DeltaY, LeftCount)
{
	if ((StopX == StartX) || 
		(StopY == StartY) || 
		(LeftCount == 0) ||
		((DeltaX == 0) && (DeltaY == 0))) {
		el.style.top  = StopY;
		el.style.left = StopX;
	} else {
		el.style.top  = StartY + DeltaY;
		el.style.left = StartX + DeltaX;
		LeftCount--;
	
		setTimeout(function() {
			makeAnimation(el, StartX + DeltaX, StartY + DeltaY, StopX, StopY, DeltaX, DeltaY, LeftCount);
		}, 100);
	}
}

function Animate(id, coord)
{
	NumMovements = 10;

	el = document.getElementById(id);
	posStart = GetXY(el);
	
	el.style.position = 'absolute';
	el.style.top = posStart[1];
	el.style.left = posStart[0];
	
	var deltaX = Math.round((parseInt(coord.X) - posStart[0])/(NumMovements+1));
	var deltaY = Math.round((parseInt(coord.Y) - posStart[1])/(NumMovements+1));
	
	//alert("go ["+posStart[0]+"] ["+posStart[1]+"] ["+coord.X+"] ["+coord.Y+"] ["+deltaX+"] ["+deltaY+"]");
	
	makeAnimation(el, posStart[0], posStart[1], coord.X, coord.Y, deltaX, deltaY, NumMovements);
}

function getDocumentScrollLeft(doc)
{
	doc = doc || document;
	return Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
}

function getDocumentScrollTop(doc) 
{
	doc = doc || document;
	return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
}

function GetXY(el)
{
    var patterns = {
        HYPHEN: /(-[a-z])/i, // to normalize get/setStyle
        ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
        OP_SCROLL:/^(?:inline|table-row)$/i
    };

	if (document.documentElement.getBoundingClientRect) { // IE
		var box = el.getBoundingClientRect(), 
			rootNode = el.ownerDocument;
		
		return [Math.round(box.left + getDocumentScrollLeft(rootNode)), 
				Math.round(box.top  + getDocumentScrollTop(rootNode))];
	} else { // manually calculate by crawling up offsetParents
		var pos = [el.offsetLeft, el.offsetTop];
		var parentNode = el.offsetParent;

		// safari: subtract body offsets if el is abs (or any offsetParent), unless body is offsetParent
		var accountForBody = (Browser.isSafari &&
				el.style.position == 'absolute' &&
				el.offsetParent == el.ownerDocument.body);

		if (parentNode != el) {
			while (parentNode) {
				pos[0] += parentNode.offsetLeft;
				pos[1] += parentNode.offsetTop;
				if (!accountForBody && Browser.isSafari && parentNode.style.position == 'absolute' ) {
					accountForBody = true;
				}
				parentNode = parentNode.offsetParent;
			}
		}

		if (accountForBody) { //safari doubles in this case
			pos[0] -= el.ownerDocument.body.offsetLeft;
			pos[1] -= el.ownerDocument.body.offsetTop;
		} 
		parentNode = el.parentNode;

		// account for any scrolled ancestors
		while ( parentNode.tagName && !patterns.ROOT_TAG.test(parentNode.tagName) ) 
		{
			if (parentNode.scrollTop || parentNode.scrollLeft) {
				pos[0] -= parentNode.scrollLeft;
				pos[1] -= parentNode.scrollTop;
			}
			
			parentNode = parentNode.parentNode; 
		}

		return pos;
	}
}

function GetXYWH(el)
{
	var pos = GetXY(el);
	try {
		var box = el.getBoundingClientRect();
		if (!box.width || !box.height) {
			return [parseInt(pos[0]), parseInt(pos[1]), parseInt(el.offsetWidth), parseInt(el.offsetHeight)];
		} else {
			return [parseInt(pos[0]), parseInt(pos[1]), parseInt(box.width), parseInt(box.height)];
		}
	} catch (e) {
		return [parseInt(pos[0]), parseInt(pos[1]), parseInt(el.offsetWidth), parseInt(el.offsetHeight)];
	}
	if (!box.width || !box.height) {
		return [parseInt(pos[0]), parseInt(pos[1]), parseInt(el.offsetWidth), parseInt(el.offsetHeight)];
	} else {
		return [parseInt(pos[0]), parseInt(pos[1]), parseInt(box.width), parseInt(box.height)];
	}
}

function addMapToImg(gruppo, OrigW, OrigH, link, link_target, X, Y, icon, iconDivClass, iconAttiva, iconAttivaDivClass, testoAccanto, iconId)
{

	var myImg = document.getElementById(gruppo);
	var myImgClientRect = myImg.getBoundingClientRect();
	var myImgPos = GetXY(myImg);

	//var DeltaX = parseInt(X*(myImgClientRect.right - myImgClientRect.left)/OrigW);
	//var DeltaY = parseInt(Y*(myImgClientRect.bottom - myImgClientRect.top)/OrigH);
	var DeltaX = parseInt(X);
	var DeltaY = parseInt(Y);


	//alert("X=["+X+"]*(["+myImgClientRect.right+"]-["+myImgClientRect.left+"])/["+OrigW+"]");

	var myOnclick = function (e) {
		if (link_target ==	'_blank') {
			mypop=window.open(link,'_blank','resizable=yes,width=800,height=600,scrollbars=yes,status=no,location=no');
		} else if (link_target ==	'_parent') {
			mypop=window.open(link,'_parent','resizable=yes,width=800,height=600,scrollbars=yes,status=no,location=no');
		} else if (link_target ==	'_top') {
			mypop=window.open(link,'_top','resizable=yes,width=800,height=600,scrollbars=yes,status=no,location=no');
		} else {
			document.location.href= link;
		}

		if (!e) var e = window.event
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	};

	link = str_replace("&amp;", "&", link);

	var mydiv = document.createElement("div");
	mydiv.style.position = 'absolute';
	mydiv.style.left = parseInt(myImgPos[0]+DeltaX)+'px';
	mydiv.style.top  = parseInt(myImgPos[1]+DeltaY)+'px';
	//alert("left=["+myImgPos[0]+"]+["+DeltaX+"]=["+mydiv.style.left+"]; top=["+myImgPos[1]+"]+["+DeltaY+"]=["+mydiv.style.top+"]");

	mydiv.style.cursor='pointer';
        if (icon.cursor)
  	  mydiv.style.cursor=icon.cursor;
          
	mydiv.style.overflow='visible';
	mydiv.className  = iconDivClass;
	mydiv.onclick = myOnclick;

	var myicon = document.createElement("img");
	myicon.src = icon.src;
	myicon.style.border = "0";
	myicon.style.width = icon.width+"px";
	myicon.style.height = icon.height+"px";
	myicon.alt = icon.alt;
	myicon.title = icon.title;
        if (iconId!=null){
          myicon.id=iconId;
        }

	mydiv.appendChild(myicon);

        if (testoAccanto!=null){
          var curText = document.createTextNode(' '+testoAccanto);
          mydiv.appendChild(curText);
        }

        if (iconAttiva.src){
            mydiv.onmouseover = function () {
                    mydiv.className  = iconAttivaDivClass;
                    myicon.src = iconAttiva.src;
                    myicon.style.width = iconAttiva.width+"px";
                    myicon.style.height = iconAttiva.height+"px";
                    myicon.alt = iconAttiva.alt;
                    myicon.title = iconAttiva.title;
            };
            mydiv.onmouseout = function () {
                    mydiv.className  = iconDivClass;
                    myicon.src = icon.src;
                    myicon.style.width = icon.width+"px";
                    myicon.style.height = icon.height+"px";
                    myicon.alt = icon.alt;
                    myicon.title = icon.title;
            };
        }
        
	document.body.appendChild(mydiv);

	var myResize = function (e) {
		var myImg = document.getElementById(gruppo);
		var myImgClientRect = myImg.getBoundingClientRect();
		var myImgPos = GetXY(myImg);

		//var DeltaX = parseInt(X*(myImgClientRect.right - myImgClientRect.left)/OrigW);
		//var DeltaY = parseInt(Y*(myImgClientRect.bottom - myImgClientRect.top)/OrigH);
		mydiv.style.left = parseInt(myImgPos[0]+DeltaX)+'px';
		mydiv.style.top  = parseInt(myImgPos[1]+DeltaY)+'px';
	};	
	try {
		window.addEventListener('resize',myResize,true);
	} catch(err) {
		window.attachEvent('onresize', myResize);
	}			
	setTimeout(myResize,100);
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
function isdefined(variable) {
    return (typeof(variable) == "undefined")? false: true;
}

function getMaxZIndex(fromElement,curElement)
{
    var curZIndex = 0;
    var curChild = fromElement.firstChild;
    while (curChild) {
        if (curChild != curElement) {
            if ((curChild.style) && (curZIndex < parseInt(curChild.style.zIndex))) curZIndex = parseInt(curChild.style.zIndex);
        }
        curChild = curChild.nextSibling;
    }
    return curZIndex;
}

