function IwPage() {
  this.initialize();
  this.okBtId="btOk";
  this.cancelBtId="btCancel";
  this.cancelUrl="/";
  this.bar=null;
  this.barName=null;
  this.initBarAsapCounter=0; // per debugging
  this.initBarAsapInterval=null;
  this.notifyPageLoadedInterval=null;
  this.notifyPageLoadedCounter=0; // per debugging
  this.currentTab=null;
  this.pageLoadedListeners=new Array();
  this.restoreFormListeners=new Array();
}
IwPage.prototype.initialize = function() {
  this.confirmBeforeUnload=false;
}
IwPage.prototype.checkUnload = function() {
  if (false && this.confirmBeforeUnload) {
    return "-----\nVuoi veramente uscire senza salvare?\n-----\n";
  }
}
IwPage.prototype.formUpdated = function(comp) {
  this.confirmBeforeUnload=true;
  var okBt=getObj(this.okBtId);
  okBt.disabled=false;
}
IwPage.prototype.ok = function(comp) {
  this.confirmBeforeUnload=false;
}
IwPage.prototype.cancel = function(comp) {
  this.confirmBeforeUnload=false;
  document.location=this.cancelUrl;
}
IwPage.prototype.getBar = function() {
  return this.bar;
}
IwPage.prototype.initBar = function() {
  if (this.bar==null) { // la barra e' ancora da inizializzare
    if (checkBar()) {
      this.bar=parent.iwShare.bar;
      if (this.bar!=null) {
        this.bar=setBar(this.bar);
      }
    }
  }
}
IwPage.prototype.setBarName = function(barName) {
  this.barName=barName;
  this.bar=null;
  this.initBarAsap();
}
IwPage.prototype.initBarAsap = function() {
  if (this.initBarAsapInterval!=null) {
    window.clearInterval(this.initBarAsapInterval);
  }
  if (this.bar==null && this.barName!=null) { // se e' ancora da inizializzare proviamo ad inizializzarla
    this.initBar();
  }
  if (this.bar==null) { // se initBar non e' riuscito a settare l'oggetto this.bar riproviamo piu' tardi
    this.initBarAsapInterval=window.setInterval("iwPage.initBarAsap()",200);
  }
}
IwPage.prototype.backupForm = function(form) {
  var share=parent.iwShare;
  if (share!=null) {
    var formValues=new Array();
    share["formBackup_"+form.name]=formValues;
    var elements=form.elements;
    for (var i=0;i<elements.length;i++) {
      var e=elements[i];
      if (e.name != null) {
        if (e.type!="radio" || e.checked) {
          formValues[e.name]=e.value;
        }
      }
    }
  }
}
IwPage.prototype.restoreForm = function(form) {
  var share=parent.iwShare;
  if (share!=null) {
    var formValues=share["formBackup_"+form.name];
    if (formValues!=null) {
      var elements=form.elements;
      for (var i=0;i<elements.length;i++) {
        var e=elements[i];
        if (e.name != null) {
          if (e.type=="radio") {
            e.checked=formValues[e.name]==e.value;
          } else if (e.type!="button" && e.type!="submit") {
            e.value=formValues[e.name];
          }
        }
      }
    }
  }
  this.notifyRestoreFormListeners(form);
}

IwPage.prototype.clearFormBackup = function(form) {
  var share=parent.iwShare;
  if (share!=null) {
    share["formBackup_"+form.name]=null;
  }
}

IwPage.prototype.clearFormBackup = function(form) {
  var share=parent.iwShare;
  if (share!=null) {
    share["formBackup_"+form.name]=null;
  }
}

IwPage.prototype.addPageLoadedListener = function(listener) {
  this.pageLoadedListeners.push(listener);
}

IwPage.prototype.pageLoadedNotified = function(listenerId) {
  // alert('pageLoadedNotified:'+listenerId);
  for (var i=0;i<this.pageLoadedListeners.length;i++) {
    if (this.pageLoadedListeners[i]!=null && this.pageLoadedListeners[i].listenerId==listenerId) {
      this.pageLoadedListeners[i]=null;
    }
  }
}
IwPage.prototype.notifyPageLoaded = function() {
  // alert('notifyPageLoaded');
}
IwPage.prototype.notifyPageLoaded_FROZEN = function() {
  var retry=false;
  var lastFailureCause=null;
  if (this.notifyPageLoadedInterval==null) {
    this.notifyPageLoadedInterval=window.setInterval("iwPage.notifyPageLoaded()",1000);
  } else {
    window.clearInterval(this.notifyPageLoadedInterval);
    this.notifyPageLoadedCounter++;
    for (var i=0;i<this.pageLoadedListeners.length;i++) {
      if (this.pageLoadedListeners[i]!=null) {
        if (!this.pageLoadedListeners[i].pageLoaded(this)) {
          //this.pageLoadedListeners[i]=null;
        //} else {
          retry=true;
          lastFailureCause=this.pageLoadedListeners[i].exception;
        }
      }
    }
    if (retry) {
      var s="iwPage.notifyPageLoaded ha fallito "+this.notifyPageLoadedCounter+" volte";
      s+="\nLa causa dell'ultimo fallimento e' stata: "+lastFailureCause;
      s+="\nVuoi riprovare?";
      if (true || this.notifyPageLoadedCounter <= 1 || confirm(s)) {
        this.notifyPageLoadedInterval=window.setInterval("iwPage.notifyPageLoaded()",5000);
      }
    }
  }
}
IwPage.prototype.addRestoreFormListener = function(listener) {
  this.restoreFormListeners.push(listener);
}
IwPage.prototype.notifyRestoreFormListeners = function(form) {
  while (this.restoreFormListeners.length > 0) {
    var e = this.restoreFormListeners.shift();
    e.restoreListener(form);
  }
}

function IwPageLoadedFlashListener(flashInstance,componentId) {
  this.flashInstance=flashInstance;
  this.componentId=componentId==null ? '_root' : componentId;
  this.exception=null;
  this.listenerId="swf:"+this.flashInstance.name;
}

IwPageLoadedFlashListener.prototype.pageLoaded = function(page) {
  var notified=false;
  var flash=getObj(this.flashInstance.name);
  if (flash!=null) {
    var s="";
    s+="notifying pageLoaded:";
    s+="\n- this.listenerId="+this.listenerId;
    s+="\n- this.flashInstance.name="+this.flashInstance.name;
    s+="\n- this.componentId="+this.componentId;
    // alert(s);
    try {
      flash.TGotoLabel(this.componentId+".jsActions","iwPageLoaded");
      notified=true;
    } catch (e) {
      notified=false;
      this.exception=e;
    }
  }
  return false; // riprova finche' il flash non si autocancella
}

var bar=null;
// definizioni javascript comuni a tutta l'amministrazione di instantweb
function confirmAndRemoveBlock(articleId,index,nextPage) {
  if (confirm("confermi la rimozione del blocco?")) {
    removeBlock(articleId,index,nextPage);
  }
}
function removeBlock(articleId,index,nextPage) {
  nextPageWithAction(nextPage,"/article/jsp/removeBlock.jsp?articleId="+articleId+"&index="+index);
}
function nextPageWithAction(nextPage,action,form) {
  var iwAction=null;
  if (form!=null) {
    iwAction=escape(action+"?"+encodeFormFields(form));
  } else {
    iwAction=escape(action);
  }
  var location=nextPage;
  if (location.indexOf('?')==-1) {
    location+="?iwAction="+iwAction;
  } else {
    location+="&iwAction="+iwAction;
  }
  document.location=location;
}
function fireNewMouseEvent(eventType,comp) {
  if (msie()) {
    var newEvent=document.createEventObject();
    if (comp.fireEvent) {
      comp.fireEvent("on"+eventType,newEvent);
    }
  } else {
    var newEvent=document.createEvent("MouseEvents");
    // rif: http://developer.mozilla.org/en/docs/DOM:event.initMouseEvent
    newEvent.initMouseEvent(eventType,true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    if (comp.dispatchEvent) {
      comp.dispatchEvent(newEvent);
    }
  }
}
function fireMouseOver(comp) {
  fireNewMouseEvent("mouseOver",comp);
}
function fireMouseOut(comp) {
  fireNewMouseEvent("mouseOut",comp);
}
function fillHeadTooltip(flashId) {
  // window.status="fillHeadTooltip: flashId="+flashId;
  var comp=getObject(flashId);
  if (typeof(comp)=='NodeList') {
    comp=comp[0];
  }
  if (comp!=null) {
    if (window.iw.i18n!=null) {
      comp.title=window.iw.i18n.get("/js/iwskin/common/editTooltip.html");
    }
    fireMouseOver(comp);
  }
}
function emptyHeadTooltip(flashId) {
  // window.status="emptyHeadTooltip: flashId="+flashId;
  var comp=getObject(flashId);
  if (comp!=null) {
    comp.title="";
    fireMouseOut(comp);
  }
}
// comunicazione con la barra
function setFlashId(x,flashId,flashComponentId) {
  if (bar!=null) {
    bar.setFlashId(x,flashId,flashComponentId);
  }
}
function iwClick(x,flashId,flashComponentId,blkFlashId,blkFlashComponentId) {
  if (bar!=null) {
    if (flashId!=null) {
      bar.iwClick(x,flashId,flashComponentId,blkFlashId,blkFlashComponentId);
    } else {
      bar.iwClick(x.id);
    }
  }
}
function iwDblClick(x,flashId,flashComponentId) {
  if (bar!=null) {
    if (flashId!=null) {
      bar.iwDblClick(x,flashId,flashComponentId);
    } else {
      bar.iwDblClick(x.id);
    }
  }
}
function iwMouseOver(x) {
  if (bar!=null) {
    bar.iwMouseOver(x.id);
  }
}
function iwMouseOut(x) {
  if (bar!=null) {
    bar.iwMouseOut(x.id);
  }
}
function checkBar() {
  if (!parent.iwShare) {
    document.location="/iw/adm/main/index.jsp?main="+escape(document.location);
    return false;
  } else {
    return true;
  }
}
function changeBar(barName) {
  if (barName=='empty' && parent.iwShare && parent.iwShare.bar) {
    parent.iwShare.bar.barName=barName;
    parent.iwShare.bar.update();
  } else if (checkBar()) {
    iwPage.setBarName(barName);
  }
}
function loginBarIfNeeded(nextPage) {
  if (parent.iwShare && parent.barFrame.bar) {
    var loc=nextPage==null ? document.location : nextPage;
    // if (window.iw.i18n) window.iw.i18n.alert("/js/adm/sessionExpired");
    parent.location="/iw/adm/main/index.jsp?main="+escape(loc);
  }
}
function menu1ButtonAreas(level) {
  return new Array("horizontal",new Object({from:0,to:103,name:"page"}),new Object({from:103,to:123,name:"openclose"}));
}

