//dhtml funcs
var agt=navigator.userAgent.toLowerCase();
var isGecko = (agt.indexOf('gecko') != -1);
var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 &&
                parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;
var isOpera = (agt.indexOf('opera')>=0) ? 1 : 0;
//-----------------------------------------------------------------------
// Finding Layers
//-----------------------------------------------------------------------
function getLayer(name) {
	if (isGecko || isOpera) {
		return document.getElementById(name);
	} else if (isMinNS4)
		return findLayer(name, document);
	else if (isMinIE4)
		return eval('document.all.' + name);
	return null;
}
function findLayer(name, doc) {
	var i, layer;
	if (doc.layers[name])
		return doc.layers[name];
	else 
		for (i = 0; i < doc.layers.length; i++) {
			layer = doc.layers[i];
			if (layer.document.layers.length > 0)
				if ((layer = findLayer(name, layer.document)) != null)
					return layer;
		}
	return null;
}
function getLink(name, parent) {
	if (isGecko || isOpera) {
		pippo = document.getElementsByName(name);
		// alert("pippo: " + pippo + "," + pippo.length);
		return pippo[0];
	} else if (isMinNS4) {
		if (parent==null)
			parent=self;
		return parent.document.anchors[name];
	} else if (isMinIE4) {
		if (parent==null)
			parent=document;
		return parent.all[name];
	}
}
function getImg(name, parent) {
	if (parent==null)
		parent=window;
	if (isMinNS4) {
		return parent.document.images[name];
	} else if (isMinIE4) {
		return parent.all[name];
	}
}

//-----------------------------------------------------------------------------
// Layer clipping.
//-----------------------------------------------------------------------------
function clipLayer(layer, cliptop, clipright, clipbottom, clipleft) {
  if (isGecko) {
	// alert("clip gecko");
	layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	return;
  }
  if (isMinNS4) { 
	if (layer.clip==null) 
	   layer.clip=new Rect();
	layer.clip.left   = clipleft;    
	layer.clip.top    = cliptop;
	layer.clip.right  = clipright;
	layer.clip.bottom = clipbottom;
  }
  if (isMinIE4 || isOpera)
	layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function getClipLeft(layer) {
  if (isGecko) {
	var str =  layer.style.clip;
	if (!str)
	  return 0;
	var clip = getIEClipValues(layer.style.clip);
	return(clip[3]);
  }
  if (isMinNS4)
	return layer.clip.left;
  if (isMinIE4 || isOpera) {
	var str =  layer.style.clip;
	if (!str)
	  return 0;
	var clip = getIEClipValues(layer.style.clip);
	return(clip[3]);
  }
  return -1;
}

function getClipTop(layer) {
  if (isGecko) {
	var str =  layer.style.clip;
	if (!str)
	  return 0;
	var clip = getIEClipValues(layer.style.clip);
	return clip[0];
  }
  if (isMinNS4)
	return layer.clip.top;
  if (isMinIE4 || isOpera) {
	var str =  layer.style.clip;
	if (!str)
	  return 0;
	var clip = getIEClipValues(layer.style.clip);
	return clip[0];
  }
  return -1;
}

function getClipRight(layer) {
  if (isGecko) {
	var str =  layer.style.clip;
	if (!str)
	  return parseInt(layer.style.width);
	var clip = getIEClipValues(layer.style.clip);
	return clip[1];
  }
  if (isMinNS4)
	return layer.clip.right;
  if (isMinIE4 || isOpera) {
	var str =  layer.style.clip;
	if (!str)
	  return parseInt(layer.style.width);
	var clip = getIEClipValues(layer.style.clip);
	return clip[1];
  }
  return -1;
}

function getClipBottom(layer) {
  if (isGecko) {
	var str =  layer.style.clip;
	if (!str)
	  return parseInt(layer.style.height);
	var clip = getIEClipValues(layer.style.clip);
	return clip[2];
  }
  if (isMinNS4)
	return layer.clip.bottom;
  if (isMinIE4 || isOpera) {
	var str =  layer.style.clip;
	if (!str)
	  return parseInt(layer.style.height);
	var clip = getIEClipValues(layer.style.clip);
	return clip[2];
  }
  return -1;
}

function getClipWidth(layer) {
  if (isGecko) {
	var str = layer.style.clip;
	if (!str)
	  return parseInt(layer.style.width);
	var clip = getIEClipValues(layer.style.clip);
	return clip[1] - clip[3];
  }
  if (isMinNS4)
	return layer.clip.width;
  if (isMinIE4 || isOpera) {
	var str = layer.style.clip;
	if (!str)
	  return parseInt(layer.style.width);
	var clip = getIEClipValues(layer.style.clip);
	return clip[1] - clip[3];
  }
  return -1;
}

function getClipHeight(layer) {
  if (isGecko) {
	var str =  layer.style.clip;
	if (!str)
	  return parseInt(layer.style.height);
	var clip = getIEClipValues(layer.style.clip);
	return clip[2] - clip[0];
  }
  if (isMinNS4)
	return layer.clip.height;
  if (isMinIE4 || isOpera) {
	var str =  layer.style.clip;
	if (!str)
	  return parseInt(layer.style.height);
	var clip = getIEClipValues(layer.style.clip);
	return clip[2] - clip[0];
  }
  return -1;
}

function getIEClipValues(str) {
  var clip = new Array();
  var i;

  // Parse out the clipping values for IE layers.
  i = str.indexOf("(");
  clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  return clip;
}

//-----------------------------------------------------------------------------
// Layer positioning.
//-----------------------------------------------------------------------------
function moveLayerTo(layer, x, y) {
  if (isGecko) {
	layer.style.left = x;
	layer.style.top  = y;
	return;
  }
  if (isMinNS4)
	layer.moveTo(x, y);
  if (isMinIE4 || isOpera) {
	layer.style.left = x;
	layer.style.top  = y;
  }
}

function moveLayerBy(layer, dx, dy) {
  // alert("movelayerby dx,dy: " + dx + "," + dy);
  if (isGecko) {
	layer.style.left = parseInt(layer.style.left)+dx;
	layer.style.top  = parseInt(layer.style.top)+dy;
	return;
  }
  if (isMinNS4)
	layer.moveBy(dx, dy);
  if (isMinIE4 || isOpera) {
	// alert("lstl: " + layer.style.left + "," + layer.style.top);
	if(!isMinIE4 || !isNaN(parseInt(layer.style.left)))
		layer.style.left = parseInt(layer.style.left)+dx;
	layer.style.top  = parseInt(layer.style.top)+dy;
  }
  // alert("mlbend");
}

function getVisWidth(layer) {
  if (isGecko) {
	if (layer.style.width)
	  return parseInt(layer.style.width);
	else
	  return 0;
  }
  if (isMinNS4) {
	  return layer.clip.right-layer.clip.left;
  }
  if (isMinIE4 || isOpera) {
	if (layer.style.width)
	  return parseInt(layer.style.width);
	else
	  return 0;
  }
  return -1;
}

function getContWidth(layer) {
  if (isGecko) {
	if (layer.scrollWidth)
	  return layer.scrollWidth;
	else
	  return 0;
  }
  if (isMinNS4) {
	if (layer.document.width)
	  return layer.document.width;
	else
	  return 0;
  }
  if (isMinIE4 || isOpera) {
	if (layer.scrollWidth)
	  return layer.scrollWidth;
	else
	  return 0;
  }
  return -1;
}

function getVisHeight(layer) {
  if (isGecko) {
	if (layer.style.height)
	  return parseInt(layer.style.height);
	else
	  return 0;
  }
  if (isMinNS4) {
	  return layer.clip.bottom-layer.clip.top;
  }
  if (isMinIE4 || isOpera) {
	if (layer.style.height)
	  return parseInt(layer.style.height);
	else
	  return 0;
  }
  return -1;
}

function getContHeight (layer) {
  if (isGecko) {
	if (layer.scrollHeight)
	  return layer.scrollHeight;
	else
	  return 0;
  }
  if (isMinNS4) {
	if (layer.document.height)
	  return layer.document.height;
	else
	  return 0;
  }
  if (isMinIE4 || isOpera) {
	if (layer.scrollHeight)
	  return layer.scrollHeight;
	else
	  return 0;
  }
  return -1;
}

//-----------------------------------------------------------------------------
// Layer scrolling.
//-----------------------------------------------------------------------------

function scrollLayerTo(layer, x, y, bound) {
  var dx = getClipLeft(layer) - x;
  var dy = getClipTop(layer) - y;
  scrollLayerBy(layer, -dx, -dy, bound);
}

function scrollLayerBy(layer, dx, dy, bound) {
  var cl = getClipLeft(layer);
  var ct = getClipTop(layer);
  var cr = getClipRight(layer);
  var cb = getClipBottom(layer);
  // alert("slb" + cl + "," + ct + "," + cr + "," + cb + "," +dx + "," + dy + "," + bound);

  if (bound) {
	if (cr-cl >= getContWidth(layer))
	  dx=0;
	else if (cl + dx < 0)
	  dx = -cl;
	else if (cr + dx > getContWidth(layer))
	  dx = getContWidth(layer) - cr;
	if (cb-ct >= getContHeight(layer))
	  dy = 0;
	else if (ct + dy < 0)
	  dy = -ct;
	else if (cb + dy > getContHeight(layer))
	  dy = getContHeight(layer) - cb;
  }
  if (isMinIE4 || isGecko || isOpera) {
	layer.style.width = parseInt(layer.style.width)+dx;
	layer.style.height = parseInt(layer.style.height)+dy;
  }
  clipLayer(layer, ct + dy, cr + dx, cb + dy, cl + dx);
  moveLayerBy(layer, -dx, -dy);
}

//layer positioning
function getPageTop(lyr, limit) {
	if (isMinIE4 || isGecko || isOpera) {
		var top=0;
		for (var curr=lyr; curr!=null && curr!=limit; curr=curr.offsetParent) {
			top+=curr.offsetTop;
		}
		return top;
	} else if (isMinNS4) {
		if (lyr.pageY)
			return lyr.pageY;
		else
			return lyr.y;
	}
}
function getPageLeft(lyr, limit) {
	if (isMinIE4 || isGecko || isOpera) {
		var left=0;
		for (var curr=lyr; curr!=null && curr!=limit; curr=curr.offsetParent) {
			left+=curr.offsetLeft;
		}
		return left;
	} else if (isMinNS4) {
		if (lyr.pageX)
			return lyr.pageX;
		else
			return lyr.x;
	}
}
function getPageRight(lyr, limit) {
	var left= getPageLeft(lyr, limit);
	if (isMinIE4 || isGecko || isOpera) {
		return left+parseInt(lyr.style.width);
	} else if (isMinNS4) {
			return left + parseInt(lyr.width);
	}
}
function getLeft(lyr) {
	if (isMinIE4 || isGecko || isOpera) {
		return parseInt(lyr.style.left);
	} else if (isMinNS4) {
		return lyr.left;
	}
}
function getTop(lyr) {
	if (isMinIE4 || isGecko || isOpera) {
		return parseInt(lyr.style.top);
	} else if (isMinNS4) {
		return lyr.top;
	}
}

//window positioning
function getDocumentTop() {
	 if (isGecko) {
		return window.screen.availTop;
	} else if (isMinNS4) {
		return window.screenY+(window.outerHeight-window.innerHeight-24);
	} else if (isMinIE4 || isOpera) {
		return window.screenTop;
	}
}
function getDocumentLeft() {
	 if (isGecko) {
		return window.screen.availLeft;
	} else if (isMinNS4) {
		return window.screenX+4;
	} else if (isMinIE4 || isOpera) {
		return window.screenLeft;
	}
}
function getDocumentHeight() {
	 if (isGecko) {
		return window.innerHeight;
	} else if (isMinNS4) {
		return window.innerHeight;
	} else if (isMinIE4 || isOpera) {
		return document.body.clientHeight;
	}	
}
function getDocumentWidth() {
	 if (isGecko) {
		return window.innerWidth;
	} else if (isMinNS4) {
		return window.innerWidth;
	} else if (isMinIE4 || isOpera) {
		return document.body.clientWidth;
	}	
}

//layer visibility
function hideLayer(lyr) {
	lyr.style.display='none';
	/*if (isGecko) {
		var id1 = lyr.getAttribute("id");
		document.getElementById(id1).style.visibility="hidden";
	}
	else if (isMinNS4)
		lyr.visibility="hidden";
	else if (isMinIE4)
		lyr.style.visibility="hidden";*/
}

function showLayer(lyr) {
	lyr.style.display='block';
	/*if (isGecko) {
		var id1 = lyr.getAttribute("id");
		document.getElementById(id1).style.visibility="visible";
	}else if (isMinNS4){
		lyr.visibility="visible";
	}else if (isMinIE4){
		lyr.style.visibility="visible";
	}else{
		var id1 = lyr.getAttribute("id");
		document.getElementById(id1).style.visibility="visible";
	}*/
}

//layer writing
function normalize_api (lyr) {
	if (isGecko) {
		lyr.open = function() {
			this.temporaryHTML="";
		};
		lyr.writeln = function(str) {
			this.temporaryHTML+=str;
			this.temporaryHTML+="\n";
		};
		lyr.write = function(str) {
			this.temporaryHTML+=str;
		};
		lyr.close = function() {
			this.innerHTML="";
			this.innerHTML=this.temporaryHTML;
			this.temporaryHTML="";
		};
	} else if (isMinNS4) {
		lyr.open = function () {
			this.temporaryHTML = "";
		}
		lyr.writeln = function (str) {
			this.temporaryHTML += str;
			this.temporaryHTML += "\n";
		}
		lyr.write = function (str) {
			this.temporaryHTML+= str;
		}
		lyr.close = function () {
			this.document.open();
			this.document.write(this.temporaryHTML);
			this.document.close();
			this.temporaryHTML="";
		}
	} else if (isMinIE4 || isOpera) {
		lyr.open = function() {
			this.temporaryHTML="";
		};
		lyr.writeln = function(str) {
			this.temporaryHTML+=str;
			this.temporaryHTML+="\n";
		};
		lyr.write = function(str) {
			this.temporaryHTML+=str;
		};
		lyr.close = function() {
			this.innerHTML="";
			this.innerHTML=this.temporaryHTML;
			this.temporaryHTML="";
		};
	}
}

//style wrappers
function findStyleVal(search, str) {
	var tokens = str.split(";");
	for (var i=0; i < tokens.length; i++) {
		var keyval=tokens[i].split(":");
		if (keyval[0]==search)
			return keyval[1];
	}
	return null;
}
function getStyleValPairs(str) {
	var tokens = str.split(";");
	var hashtable = new Array();
	for (var i=0; i<tokens.length; i++) {
		var keyval=tokens[i].split(":");
		hashtable[keyval[0]] = keyval[1];
	}
	return hashtable;
}
function setTop(lyr, y) {
	y = parseInt(y);
	if (isGecko) {
		var id1 = lyr.getAttribute("id");
		// alert("setTop id: " + id1);
		// alert("setTop: " + y);
		lyr.style.top=y;
	} else if (isMinNS4) {
		lyr.top=y;
	} else if (isMinIE4 || isOpera) {
		lyr.style.top=y;
	}
}
function setLeft(lyr, x) {
	x = parseInt(x);
	// alert("setLeft: " + x);
	if (isGecko) {
		lyr.style.left=x;
	} else if (isMinNS4) {
		lyr.left=x;
	} else if (isMinIE4 || isOpera) {
		lyr.style.left=x;
	}
}
function setWidth(lyr, width) {
	width = parseInt(width);
	if (isGecko) {
		lyr.style.width=width;
	} else if (isMinNS4) {
		var curr_width=lyr.clip.right-lyr.clip.left;	
		var delta=width-curr_width;
		lyr.clip.right+=delta;
	} else if (isMinIE4 || isOpera) {
		lyr.style.width=width;
	}
}
function setHeight(lyr, height) {
	height = parseInt(height);
	if (isGecko) {
		lyr.style.height=height;
	} else if (isMinNS4) {
		var curr_height=lyr.clip.bottom-lyr.clip.top;	
		var delta=height-curr_height;
		lyr.clip.bottom+=delta;
	} else if (isMinIE4 || isOpera) {
		lyr.style.height=height;
	}
}
function setClip(lyr, clip) {
	if (isGecko) {
		lyr.style.clip = clip;
	} else if (isMinNS4) {
		clip=clip.substring(clip.indexOf('(')+1, clip.lastIndexOf(')'));
		var clipvals=clip.split(",");
		for (var i=0; i<4; i++)
			clipvals[i]=parseInt(clipvals[i]);	
		clipLayer(lyr, clipvals[0], clipvals[1], clipvals[2], clipvals[3]);
	} else if (isMinIE4 || isOpera) {
		lyr.style.clip = clip;
	}
}
function setVisibility(lyr, vis) {
	if (isGecko) {
		lyr.style.visibility=vis;
	} else if (isMinNS4) {
		lyr.visibility=vis;
	} else {
		lyr.style.visibility=vis;
	}
}
function setBgImage(lyr, img) {
	if (img.search(/url\([^\)]*\)/)!=-1)
		img = img.substring(4,img.length);
	if (isMinNS4) {
		lyr.background.src=img;
	} else if (isMinIE4 || isOpera) {
		lyr.style.backgroundImage="url("+img+")";
	}
}
function setBgColor(lyr, col) {
	if (isGecko) {
		lyr.style.backgroundColor=col;
	} else if (isMinNS4) {
		lyr.bgcolor=color;
	} else if (isMinIE4 || isOpera) {
		lyr.style.backgroundColor=col;
	}
}
function setZIndex(lyr, zindex) {
	zindex = parseInt(zindex);
	if (isMinNS4) {
		lyr.zIndex = zindex;
	} else if (isMinIE4 || isOpera) {
		lyr.style.zIndex = zindex;
	}
}

var style_mapping = new Array();
style_mapping["top"] = setTop;
style_mapping["left"] = setLeft;
style_mapping["width"] = setWidth;
style_mapping["height"] = setHeight;
style_mapping["clip"] = setClip;
style_mapping["visibility"] = setVisibility;
style_mapping["background-image"] = setBgImage;
style_mapping["backgound-color"] = setBgColor;
style_mapping["z-index"] = setZIndex;

function setStyle(lyr, style) {
	valpairs = getStyleValPairs(style);
	for (var prop in valpairs)
		if (style_mapping[prop])
			style_mapping[prop](lyr, valpairs[prop]);
}

//layer creation
function createLayer(name, style, parent, keepHidden) {
	var lyr=null;
	if (isGecko) 
	{
		if (parent==null || parent==document || parent==window)
			parent=document.body;
		parent.innerHTML += '<div id="'+name+'" style="'+style+'"></div>';
		lyr=document.getElementById(name);
	} else if (isMinNS4) {
		if (parent==null)
			parent=window;
		lyr=new Layer(1, parent);
		if (!keepHidden)
			lyr.hidden=false;
		parent.document[name]=lyr;
		parent.document.layers[name]=lyr;
		setStyle(lyr, style);
	} else if (isMinIE4 || isOpera) {
		if (parent==null || parent==document || parent==window)
			parent=document.body;
		parent.insertAdjacentHTML('BeforeEnd','<div id="'+name+'" style="'+style+'"></div>');
		lyr=parent.all[name];
	}
	normalize_api(lyr);
	return lyr;
}


//external files
function load_page(url) {
	if (isGecko) {
		var my_ifr = document.getElementById("page_loader");
		if (!my_ifr) {
			var bodies = document.getElementsByTagName("body");
			if (bodies.length == 1) {
				var frm = document.createElement("iframe");
				frm.id = "page_loader";
				frm.height = "1";
				frm.width = "1";
				my_ifr = bodies[0].appendChild(frm);
			}
			else {
				alert("Questa pagina html contiene un numero anomalo di body: (" + bodies.length + ")");
			}
		}
		my_ifr.src=url;
	}
	else if (isMinNS4) {
		if (!window.page_loader)
			window.page_loader=new Layer(1, window);
		window.page_loader.load("http://"+document.location.host+url, 1);
	} else if (isMinIE4 || isOpera) {
		if (!document.page_loader)
			document.body.insertAdjacentHTML('BeforeEnd', '<iframe name="page_loader" width=1 height=1 style="display:none" src=""></iframe>'); 
		parent.page_loader.onunload=function(){return false;};
		parent.page_loader.document.location=url;
	}
}

//proxy function (standard interface for request/response model + good for debug)
var ProxyTable = new Array();
function MessageProxy(destination, msg) {
	//notify ("dest: "+ destination + " msg: "+ msg);
	if (arguments.length>=2) {
		ProxyTable[destination](msg);
	} else {
		ProxyTable[destination]();
	}
}

//unescape string passed from java or some other language
function custom_unescape(str) {
	index=0;
	while((index=str.indexOf("$10"))!=-1)
		str = str.substring(0, index) + "\n" + str.substr(index+3);
	while((index=str.indexOf("$"))!=-1)
		str = str.substring(0, index) + "%" + str.substr(index+1);
	return unescape(str);
}
