var __wins = new Array();
var level = findLevel();

function findLevel() {
    var lev = 0;
    var obj = window.opener;
    while (obj != null) {
        var o = '.opener';
        for (var k = 0; k < lev+1; k++)
            o += '.opener';
        obj = eval("window"+o);
        lev++;
    }
    return lev;
}

function openWindowFromLink(e,w,h,message) {
    var name = e.attr("name");
    var url = e.attr("url");
    openWindow(url,name,message,w,h);
}

function openWindow(url,name,message,w,h) {
    Today = new Date();
    ms = Today.getMilliseconds();
    if (name == undefined || name == null) name = ms;
    if (w == undefined) w = 800;
    if (h == undefined) h = 600;
    var win = new xWindow(name,w,h);
    win.load(url,message);
    var ref = 'window';
    for (var k = 0; k < level; k++) ref += '.opener';
    __wins.push(win);
    eval(ref+".__wins.push(win)");
    return name;
}

function xWindow(name, w, h, x, y, loc, men, res, scr, sta, too)
{
  var ww = window.screen.width;
  var wh = window.screen.height;
  if (w!=null && (x == undefined || x == -1)) x = (ww-w)/2;
  if (h!=null && (y == undefined || y == -1)) y = (wh-h)/2;
  if (too==undefined) too=1;
  if (res == undefined) res = 1;
  if (scr == undefined) scr = 1;
  var e='',c=',',xf='left=',yf='top='; this.n = name;
  if (document.layers) {xf='screenX='; yf='screenY=';}
  this.f = (w?'width='+w+c:'width='+ww+'px,')+(h?'height='+h+c:'height='+(wh)+'px,')+(x>=0?xf+x+c:e)+
    (y>=0?yf+y+c:e)+'location='+loc+',menubar='+men+',resizable='+res+
    ',scrollbars='+scr+',status='+sta+',toolbar='+too;
  this.opened = function() {return this.w && !this.w.closed;};
  this.close = function() {if(this.opened()) this.w.close();};
  this.focus = function() {if(this.opened()) this.w.focus();};
  this.load = function(sUrl,msg) {
    if (msg == undefined) msg = 'caricamento ...';
    var code = winloading(msg);
    if (this.opened()) {
        this.w.document.write(code);
        this.w.location.href = sUrl;
    }
    else {
        this.w = window.open('','',this.f);
        this.w.document.write(code);
        this.w.location.href = sUrl;
    }
    this.focus();
    return false;
  };
  this.setContent = function(sUrl,code) {
    if (msg == undefined) msg = 'caricamento ...';
    var code = winloading(msg);
    if (this.opened()) {
        this.w.document.write(code);
        this.w.location.href = sUrl;
    }
    else {
        this.w = window.open('','',this.f);
        this.w.document.write(code);
        this.w.location.href = sUrl;
    }
    this.focus();
    return false;
  };
}

function winloading(messaggio) {
        var c = new Array();
        c[0] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
        c[1] = "<html>";
        c[2] = "<head>";
        c[3] = "<script type='text/javascript' language='javascript'>";
        c[4] = "var m = '"+messaggio+"';var c = 0;var ml = m.length;";
        c[5] = "function u() {var msg = new Array();for(var k = 0; k < m.length; k++) { var col='white'; if (k==c) col='red';msg[k] = '<span style=\"color:'+col+';\">'+m.substring(k,k+1)+'</span>';};document.getElementById('__l__').innerHTML = msg.join('');c=(c+1)%m.length;}";
        c[13] = "setInterval('u()',150);";
        c[14] = "</script>";
        c[15] = "</head>";
        c[16] = "<body style='background-color:#aed9bb'>";
        c[17] = "<div id='__l__' align='center' style='background-color:darkblue;color:white;font-weight:bold;font-size:0.8em'>"+messaggio+"</div>";
        c[18] = "</body>";
        c[19] = "</html";
	return c.join("");
}

