////////////////////////////////////////////////////////////////////////////////////
//  Librerie di funzioni per la gestione delle finestre popup 
//  dei siti del Gruppo Athesis
//  
//  Realizzazione dei popup a partire dalle librerie overLIB 
//  ( http://www.bosrup.com/web/overlib/ ) by Erik Bosrup
//
//  Realizzazione della mappa: Riccardo De Mattei - Unisys Italia 
//
////////////////////////////////////////////////////////////////////////////////////

onError = null;


//
// Popup configuration
//

var popwidth   = 150;
var border     = 1;
var offsetx    = -5;
var offsety    = 10;
var minpopx    = 0;
var maxpopx    = 362;
var minpopy    = 0;
var maxpopy    = 392;

var xToppdf	= 325;
var yToppdf	= 370;
var maxarchive = 7

//
// Marker configuration
//
var markborder = 1;
//var markcolor  = "#ACA949";
var markcolor  = "red";
//var markcolor  = "#FF5500";
var markimage  = "";


//
// Markup configuration
//
var mark_fg  = ['black','black','white','white','white','white','black','black'];
var mark_bg  = ['#99ff99','#ff66ff','#00aa00','#886800','#004699','#990099','#ffff66','#A0FFFF'];
var mark_par = 'mark';
var mark_tag = 'marked';
var mark_cnt = 0;


//
// Public popup functions
//


// PopUp con posizionamento automatico
// Visualizza una finestra popup con differenti elementi che vengono passati come argomento 
// della funzione: ad. es. lst('1 PIPPO 0 PLUTO 2 PAPERINO')
// dove: 
//	0=prima riga di testo
//	1=titolo in negativo
// 	2=titoletto in alto a destra fuori dal popup
//      3,n=terza, n-esima riga di testo
function auto_pup(text) { return dtl(3, text) };
// PopUp con posizionamento a destra
function right_pup(text) { return dtl(1, text) };
// PopUp con posizionamento a sinistra
function left_pup(text) { return dtl(0, text) };
// PopUp con posizionamento centrato
function center_pup(text,ax1,ay1,ax2,ay2) { return dtl(2, text,ax1,ay1,ax2,ay2) };
// PopUp con posizionamento fisso
function fix_pup(text, x, y) { return dtlf(x, y, text) };



// Clear popup
function nd() { return dt_hide() };


//
// Public marker functions
//
function colora(x1,y1,x2,y2){
  if (markok){
    moveObj(tline, markx1+1, marky1+y1);
    moveObj(bline, markx1+1, marky1+y2+markborder-1);
    moveObj(lline, markx1+1+x1, marky1);
    moveObj(rline, markx1+x2+markborder, marky1);
    clipObj(tline, x1+markborder, 0, x2+markborder*2-1, markborder);
    clipObj(bline, x1, 0, x2+markborder-1, markborder);
    clipObj(lline, 0, y1, markborder, y2+markborder-1);
    clipObj(rline, 0, y1+markborder, markborder, y2+markborder*2-1);
    showObj(tline);
    showObj(bline);
    showObj(lline);
    showObj(rline);
  };
  return false;
};

function ncl(){
  if (markok){
    hideObj(tline);
    hideObj(bline);
    hideObj(lline);
    hideObj(rline);
  };
  return false;
};

function click(){
  return true;
};


//
// Public markup functions
//

function markup(marks){
	if (!document.createDocumentFragment) return false;

	var result = 0;
	var bodynode = document.getElementsByTagName('body')[0];
	for (var c=0, mc=marks.length; c<mc; c++)
		if (marks[c].length){
		  var hits = mark_traverse(bodynode, eval('/\\b('+marks[c]+')\\b/i'))
			result += hits;
			if (hits) mark_cnt++;
		};

  return result;
};

function unmark(){
	if (!document.createDocumentFragment) return false;

	var spans = new Array();
	for (var s=document.getElementsByTagName('span'), sc=s.length, c=0; c<sc; c++)
		if (s[c].id == mark_tag)
			spans.push(s[c])

	while (spans.length){
		var span = spans.pop();
		var text = span.firstChild.data;
		if (span.previousSibling && span.previousSibling.nodeType == 3){
			text = span.previousSibling.data + text;
			span.parentNode.removeChild(span.previousSibling);
		};
		if (span.nextSibling && span.nextSibling.nodeType == 3){
			text += span.nextSibling.data;
			span.parentNode.removeChild(span.nextSibling);
		};
		span.parentNode.replaceChild(document.createTextNode(text), span);
	};

	mark_cnt = 0;
  return false;
};


//
// Other public functions
//

var extopen=false;

// Function external - Richiama una pagina HTML in una finestra esterna
//		       name: Name of html-file		
//                     width, height: Width / Height of external window
function external(name,width,height,scroll){
  if(extopen){extwin.shutdown=true;extwin.close();};
  if(screen){
    var maxw = screen.availWidth-10;
    var maxh = screen.availHeight-50;
  }else{
    var maxw = 800;
    var maxh = 550;
  };
  if (width > maxw){
    extwin=window.open(name+'.html?external',name,'dependent=yes,scrollbars=yes,height='+(Math.min(height+30,maxh))+',width='+Math.min(width,maxw)+',screenX=0,screenY=0,locationbar=no,menubar=no,resizable=yes,status=no');
    if (!ns4) extwin.moveTo(0,0);
  }else if (height > maxh){
    extwin=window.open(name+'.html?external',name,'dependent=yes,scrollbars=yes,height='+Math.min(height,maxh)+',width='+(Math.min(width+30,maxw))+',screenX=0,screenY=0,locationbar=no,menubar=no,resizable=yes,status=no');
    if (!ns4) extwin.moveTo(0,0);
  }else{
    extwin=window.open(name+'.html?external',name,'dependent=yes,scrollbars=no,height='+height+',width='+width+',locationbar=no,menubar=no,resizable=yes,status=no');
  };
  return false;
};

// Function extclose: should be called onUnload to close external window
function extclose(){
  if (extopen) {
    extwin.shutdown=true;
    extwin.close();
  };
};



// Refresh function
function load(header, left, page, side){
  var me = window.name;
  if (header && me!='header' && header != parent.frames.header.location.pathname) parent.frames.header.location.href = header;
  if (left && me!='left' && left != parent.frames.left.location.pathname) parent.frames.left.location.href = left;
  if (side && me!='side' && side != parent.frames.side.location.pathname) parent.frames.side.location.href = side;
  if (page && me!='page' && page != parent.frames.page.location.pathname) parent.frames.page.location.href = page;
  if (header && me=='header' && header != location.pathname) location.href = header;
  if (left && me=='left' && left != location.pathname) location.href = left;
  if (side && me=='side' && side != location.pathname) location.href = side;
  if (page && me=='page' && page != location.pathname) location.href = page;
  return false;
};


// Select Option
function doselect(select, target){
  for (var sc=0, sl=select.length; sc<sl; sc++)
    if (select[sc].value == target){
      select[sc].selected = true;
      break;
    };
};


// Go to top Function
function totop(){
  window.scrollTo(0,0);
  return false;
};


//
// Non public common functions
//

// Get object by ID
function getObj(name) {
  if (ns4) return document[name]
  else if (ie4) return document.all[name]
  else if (dom) return document.getElementById(name);
};

// Make an object visible
function showObj(obj) {
  if (ns4) obj.visibility = "show"
  else obj.style.visibility = "visible";
};

// Hide an object
function hideObj(obj) {
  if (ns4) obj.visibility = "hide"
  else obj.style.visibility = "hidden";
};

// Move an object
function moveObj(obj,x,y) {
  if (ns4) {
    obj.left = x;
    obj.top = y;
  } else {
    obj.style.left = x;
    obj.style.top = y;
  };
};

// Resize an object
function sizeObj(obj,w,h) {
  if (ns4) {
    obj.width = w;
    obj.height = h;
  } else {
    obj.style.width = w;
    obj.style.height = h;
  };
};

// Clip an object
function clipObj(obj,x1,y1,x2,y2) {
  if (ns4) {
    obj.clip.left = x1;
    obj.clip.top = y1;
    obj.clip.right = x2;
    obj.clip.bottom = y2;
  } else {
    obj.style.clip = 'rect('+y1+','+x2+','+y2+','+x1+')';
  };
};

// Write to an object
function writeObj(obj,text) {
  if (ns4) {
    var doc = obj.document;
		doc.open();
    doc.write(text);
    doc.close();
  } else {
    obj.innerHTML = text;
  };
};

function getheightObj(obj){
  if (ns4) {
    return obj.clip.bottom;
  } else {
    return obj.offsetHeight;
  };
};


//
// Non public popup functions
//

// Simple popup
function dts(direction, text) {
  if (!popok) return false;
	offsety = 20;
  txt = '<TABLE WIDTH="'+popwidth+'" BORDER="0" CELLPADDING="'+(border+2)+'" CELLSPACING="0" CLASS="outer"><TR><TD CLASS="caption">'+unescape(text)+'</TD></TR></TABLE>'
  popdir = direction;
  writeObj(popobj, txt);
  return dt_show();
};
function dtsf(x, y, text) {
  popxf = x;
  popyf = y;
  dts(4, text);
};


// Caption popup
function dtc(direction, title, text) {
  if (!popok) return false;
	offsety = 20;
  txt = '<TABLE WIDTH="'+popwidth+'" BORDER="0" CELLPADDING="'+border+'" CELLSPACING="0" CLASS="outer"><TR><TD><TABLE WIDTH="100%" BORDER="0" CELLPADDING="2" CELLSPACING="0"><TR><TD CLASS="caption">'+unescape(title)+'</TD></TR></TABLE><TABLE WIDTH="100%" BORDER="0" CELLPADDING="2" CELLSPACING="0"><TR><TD CLASS="text">'+unescape(text)+'</TD></TR></TABLE></TD></TR></TABLE>'
  popdir = direction;
  writeObj(popobj, txt);
  return dt_show();
};
function dtcf(x, y, title, text) {
  popxf = x;
  popyf = y;
  dts(4, title, text);
};


// List popup
function dtl(direction, text,aax1,aay1,aax2,aay2) {
 xx1 = aax1;
 yy1 = aay1;
 xx2 = aax2;
 yy2 = aay2;
  if (!popok) return false;

	//offsety = 10;

	var tag = '';
	var txt = '<TABLE WIDTH="'+popwidth+'" BORDER="0" CELLPADDING="'+border+'" CELLSPACING="0" CLASS="outer"><TR><TD>';
	var txtlst = text.split(' ');
	var txtlen = txtlst.length;

  for (var cnt=0, mode=0, dot=''; cnt<txtlen; cnt+=2){
		if (txtlst[cnt] == '2'){
			if (tag) tag += ', ';
			tag += unescape(txtlst[cnt+1]);
			//offsety = 10;
		} else if (mode == txtlst[cnt]){
			txt += '<BR>'+dot+unescape(txtlst[cnt+1]);
		}else{
			if (mode) txt += '</TD></TR></TABLE>';
			mode = txtlst[cnt];
			dot = (cnt+2 < txtlen && txtlst[cnt+2] == mode)?'&middot;&nbsp;':'';
			txt += '<TABLE WIDTH="100%" BORDER="0" CELLPADDING="2" CELLSPACING="0"><TR><TD CLASS="'+((mode=='1')?'caption':'text')+'">'+dot+unescape(txtlst[cnt+1]);
		};
	};
	if (mode) txt += '</TD></TR></TABLE>';
	txt += '</TD></TR></TABLE>';

	if (tag) tag = '<TABLE WIDTH="'+popwidth+'" BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD ALIGN="right"><TABLE BORDER="0" CELLPADDING="'+border+'" CELLSPACING="0" CLASS="outer"><TR><TD><TABLE BORDER="0" CELLPADDING="1" CELLSPACING="0"><TR><TD CLASS="city">'+tag+'&nbsp;</TD></TR></TABLE></TD></TR></TABLE></TD></TR></TABLE>';
  popdir = direction;
  writeObj(popobj, tag+txt);
  return dt_show();
};
function dtlf(x, y, text) {
  popxf = x;
  popyf = y;
  dtl(4, text);
};


// Clear popup
function dt_hide() {
  if (bok) {
    if (popact) {
      popact = 0;
      hideObj(popobj);
    };
  };
  return false;
};

function dt_show() {
  if (bok) {
    if (!popact) {
      move();
      popact = 1;
      showObj(popobj);
    };
  };
  return false;
};

// Popup move handler
function mouseMove(e) {
  if (ns4) {popx=e.pageX; popy=e.pageY;}
  else if (ie4) {popx=event.x; popy=event.y;}
  else if (ie5 || (dom&&ie)) {popx=event.x+document.body.scrollLeft; popy=event.y+document.body.scrollTop;}
  else if (dom) {popx=e.pageX; popy=e.pageY;}
  if (popact) move();
  return true;
};

function move() {
  if (bok) {
    if (popdir == 4){
      moveObj(popobj, popxf, popyf);
    } else if (popdir == 3){
      newx = popx+offsetx;
      if (newx < minpopx) newx = minpopx;
      if (newx+popwidth > maxpopx) newx = maxpopx-popwidth;
      newy = popy+offsety;
      if (newy < minpopy) newy = minpopy;
      if (newy+getheightObj(popobj) > maxpopy) newy = popy-(offsety/2)-getheightObj(popobj);
      moveObj(popobj, newx, newy);

//modifica 9/7/02*/
} else if (popdir == 2) {

	// se non sta sotto
	
	if (yy2 + getheightObj(popobj)+offsety > maxpopy){
		// se sta sopra	
		 if (yy1 - getheightObj(popobj)+offsety> 0) {
		 	//cordy = yy1 - getheightObj(popobj)+offsety-markborder;
			cordy = yy1 - getheightObj(popobj)+offsety;
		 	if ((xx1+offsetx+(xx2-xx1)/2)< (maxpopx/2)){moveObj(popobj, 0, cordy);}
		 	else{moveObj(popobj, maxpopx-popwidth, cordy);}

		 }
		 // se non sta ne sopra ne sotto
		 else { 
		 	// se non sta a destra  
			if (xx2 + popwidth +offsetx > maxpopx){
				 // se sta a sinistra
				 if (xx1 - popwidth +offsetx> 0) {
				 	//cordx = xx1 - popwidth+offsetx-markborder;
					cordx = xx1 - popwidth+offsetx;
		 			if ((yy1+offsety+(yy2-yy1)/2)< (maxpopy/2)){moveObj(popobj, cordx, 0);}
		 			else{moveObj(popobj, cordx, maxpopy-getheightObj(popobj));}				 	
				 	
				 	}
				 // se non sta da nessuna parte
				 else {
				 	//moveObj(popobj, popx-(popwidth/2), popy+offsety+10);
					newx = popx-(popwidth/2);
					if (newx < minpopx) newx = minpopx;
					if (newx+popwidth > maxpopx) newx = maxpopx-popwidth;
					newy = popy+offsety+10;
					if (newy < minpopy) newy = minpopy;
					if (newy+getheightObj(popobj) > maxpopy) newy = popy-(offsety/2)-getheightObj(popobj);
					moveObj(popobj, newx, newy);
				 }
			}else{
		 			if ((yy1+offsety+(yy2-yy1)/2)< (maxpopy/2)){moveObj(popobj, xx2+offsetx+markborder, 0);}
		 			else{moveObj(popobj, xx2+offsetx+markborder, maxpopy-getheightObj(popobj));}				
	 	
				}		
				 	
		 }
	}else{
			// se sta sotto
			
		 	if ((xx1+offsetx+(xx2-xx1)/2)< (maxpopx/2)){moveObj(popobj, 0, yy2+offsety+markborder);}
		 	else{moveObj(popobj, maxpopx-popwidth, yy2+offsety+markborder);}		

	}

	

	
  }


/////////
    else if (popdir == 1) moveObj(popobj, popx+offsetx, popy+offsety);
    else if (popdir == 0) moveObj(popobj, popx-(offsetx/2)-popwidth, popy+offsety);
  };
};


//
// Non public marker functions
//
function initM(obj, x, y, w, h){
  moveObj(obj, x, y);
  sizeObj(obj, w, h);
writeObj(obj, '<TABLE WIDTH="'+w+'" HEIGHT="'+h+'" BORDER="0" CELLSPACING="0" CELLPADDING="0" BGCOLOR="'+markcolor+'"'+(moz?'':' BACKGROUND="'+markimage+'"')+'><TR><TD WIDTH="'+w+'" HEIGHT="'+h+'">&nbsp;</TD></TR></TABLE>');
};  



//
// Non public markup functions
//

function mark_traverse(obj, regex){
	var childs = new Array();
	for (var t=obj.childNodes, tc=t.length, c=0; c<tc; c++)
		childs.push(t[c])
	var hits = 0;
	while (childs.length){
		var child = childs.pop();
    if (child.nodeType == 1 && child.id != mark_tag) hits += mark_traverse(child, regex);
    if (child.nodeType == 3) hits += mark_colorize(child, regex);
	};
	return hits;
};

function mark_colorize(obj, regex){
	var hits = 0;
  var text = obj.data;
	if (regex.test(text)){
		var fragment = document.createDocumentFragment();
		if (moz){
			var chunks = text.split(regex);
		}else{
			var chunks = splitter(text, regex);
		};
		var clength  = chunks.length;
		for (var count=0; count<clength; count++){
			var chunk = chunks[count];
			if (chunk.length) 
				if (count%2){
					hits++;
					var col = document.createElement('span');
					col.id = mark_tag;
					col.style.color = mark_fg[mark_cnt%mark_fg.length];
					col.style.background = mark_bg[mark_cnt%mark_bg.length];
					col.appendChild(document.createTextNode(chunk));
				  fragment.appendChild(col);
				}else{
					fragment.appendChild(document.createTextNode(chunk))
				};
			};
	  obj.parentNode.replaceChild(fragment, obj);
  };
	return hits;
};

function splitter(text, regex){
	var result = new Array();
	while (1){
		var pos = text.search(regex);
		if (pos >= 0){
			var match = RegExp.$1;
			result.push(text.substr(0,pos));
			result.push(match);
			text = text.substr(pos+match.length);
		}else{
			if (text.length) result.push(text);
			break;
		};
	};
	return result;
};


//
// Popup initialization
//
var popx   = 0;
var popy   = 0;
var popact = 0;
var popdir = 1; // 0:left  1:right  2:center  3:auto  4:fixed
var popxf  = 0;
var popyf  = 0;

var popobj = null;
var popok  = false;


//
// Marker initialization
//
var markx1 = 0;
var marky1 = 0;
var markx2 = 340;
var marky2 = 501;

var tline = null;
var bline = null;
var lline = null;
var rline = null;

var markok = false;

function initmarker(){

  tline = getObj('tline');
  bline = getObj('bline');
  lline = getObj('lline');
  rline = getObj('rline');
  pdflinkns4 = getObj('pdflink');

	var ppos = getObj('pagepos');
	var ppic = getObj('pagepic');

  if (ns4) {
    markx1 = ppos.pageX;
    marky1 = ppos.pageY+1;
  } else {
    for (obj = ppic, markx1=0, marky1=1; obj.tagName != 'BODY'; obj = obj.offsetParent){
      markx1 += obj.offsetLeft;
      marky1 += obj.offsetTop;
    };
  };

  markx2 = ppic.width + markx1;
  marky2 = ppic.height + marky1;

  markx1 -= markborder;
  marky1 -= markborder;
  markx2 += markborder;
  marky2 += markborder;

  initM(tline, markx1, marky1, markx2-markx1, markborder);
  initM(bline, markx1, marky1, markx2-markx1, markborder);
  initM(lline, markx1, marky1, markborder, marky2-marky1);
  initM(rline, markx1, marky1, markborder, marky2-marky1);

  markok = true;
};



//
// Browser Detection
//
var ie  = 0;
var ns4 = 0;
var dom = 1;
var ie4 = 0;
var ie5 = 0;
var ope = 0;
var moz = 0;
var bok = 1;

function init(){
  ie  = (document.all)?true:false;
  ns4 = (document.layers)?true:false;
  dom = (document.getElementById)?true:false;
  ie4 = (document.all && !dom)?true:false;
  ie5 = (dom && navigator.userAgent.indexOf('MSIE 5')>0);
  ope = (dom && navigator.userAgent.indexOf('Opera')>0);
  moz = (dom && navigator.userAgent.indexOf('Gecko')>0);
  bok = ns4 || ie4 || dom;

  if (bok){
    popobj = getObj('popDiv');
	popok  = true;
    document.onmousemove = mouseMove;
    if (ns4) document.captureEvents(Event.MOUSEMOVE);
  };

 

};

// Move Pdf link object
function PosPdfLink() {
  if (ns4) {
    pdflinkns4.left = xToppdf;
    pdflinkns4.top = yToppdf;
    showObj(pdflinkns4); 
  }else if(moz){ 
        document.getElementById("pdflink").style.left = xToppdf;
        document.getElementById("pdflink").style.top = yToppdf;
        showObj(document.getElementById("pdflink"));
  }else {
    pdflink.style.left = xToppdf;
    pdflink.style.top = yToppdf;
    showObj(pdflink);
  };
};

// Init layers
function initLines() {

init();

var offxx = offsetx;
var offyy = offsety;

  if (ns4) {
  	
	for(var i=0; i<document.layers.length; i++){

	 	var temp = document.layers[i].name;

	 	if (temp.indexOf("line")==0){
		 	nomeobj = getObj(temp) ;
		 	nomeobj.left = nomeobj.left + offxx ;
		 	nomeobj.top = nomeobj.top + offyy ;
	 	}
	}   
  } else if(moz){
  
	for(var i=0; i<document.getElementsByTagName("div").length; i++){
	 	var temp = document.getElementsByTagName("div").item(i).id;
	 	if (temp.indexOf("line")==0){
		 	nomeobj = getObj(temp) ;
		 	var leftval = nomeobj.style.left
		 	var toptval = nomeobj.style.top
		 	nomeobj.style.left = eval(leftval.substring(0,leftval.indexOf("px"))) + offxx;
		 	nomeobj.style.top = eval(toptval.substring(0,toptval.indexOf("px"))) + offyy; 
	 	}
	}   
  } else {
  	
	for(var i=0; i<document.all.length; i++){
	 	var temp = document.all.item(i).id;
	 	if (temp.indexOf("line")==0){
		 	nomeobj = getObj(temp) ;
		 	var leftval = nomeobj.style.left
		 	var toptval = nomeobj.style.top
		 	nomeobj.style.left = eval(leftval.substring(0,leftval.indexOf("px"))) + offxx;
		 	nomeobj.style.top = eval(toptval.substring(0,toptval.indexOf("px"))) + offyy; 
	 	}
	}   
  };
};

function creaLink(giorno){
	Today = new Date();
	day = giorno.substr(6,2)
	month = giorno.substr(4,2)
	year = giorno.substr(0,4)
	mese = MonthReverse(month)
	data = new Date(mese+" "+day+", "+year)

	timeB = (Today - data)/86400000;
	
	if (timeB > maxarchive){
		for (var i=0; i< document.links.length; i++){
	 		var stringa =  document.links[i].href;
	 		document.links[i].href = stringa+"&archivio=yes";
		}
	 }
}
function MonthReverse(x) {
	if (x=='01') { return 'Jan'; }
	if (x=='02') { return 'Feb'; }
	if (x=='03') { return 'Mar'; }
	if (x=='04') { return 'Apr'; }
	if (x=='05') { return 'May'; }
	if (x=='06') { return 'Jun'; }
	if (x=='07') { return 'Jul'; }
	if (x=='08') { return 'Aug'; }
	if (x=='09') { return 'Sep'; }
	if (x=='10') { return 'Oct'; }
	if (x=='11') { return 'Nov'; }
	if (x=='12') { return 'Dec'; }
}
