// es: title:Titolo|body:Body|details:Dettagli|options:Opzioni
function IwTabs(conf,autoTab,index,name) {
  var tabsConf=conf.split("|");
  this.ids=new Array();
  this.tabIdToIndex=new Array();
  this.yavRules=new Array();
  this.current=0;
  this.index=index==null ? '' : index;
  for (var i=0;i<tabsConf.length;i++) {
    var tabConf=tabsConf[i].split(":");
    var id=tabConf[0];
    this.ids[i]=id;
    this.tabIdToIndex[id+this.index+"Tab"]=i;
    if (id==autoTab) {
      this.current=i;
    }
    this.yavRules[i]=null;
  }
  this.yavAlertType='classic';
  this.yavForm=null;
  this.listeners=new Array();
  this.validateOnTabClick=false;
  this.validateOnNext=true;
  this.validateOnPrevious=false;
  if (name!=null) {
    this.name=name;
    tabsManager.setTabs(name,this);
  }
}
IwTabs.prototype.setValidateOnTabClick = function(b) {
  this.validateOnTabClick=b;
}
IwTabs.prototype.setValidateOnNext = function(b) {
  this.validateOnNext=b;
}
IwTabs.prototype.setValidateOnPrevious = function(b) {
  this.validateOnPrevious=b;
}
IwTabs.prototype.setYavRules = function(i,rules) {
  this.yavRules[i]=rules;
}
IwTabs.prototype.setYavFormName = function(formName) {
  this.formName=formName;
}
IwTabs.prototype.setYavAlertType = function(alertType) {
  this.alertType=alertType;
}
IwTabs.prototype.toString = function() {
  return ""+this.ids;
}
IwTabs.prototype.init = function() {
  this.hideAll();
  this.show(this.current);
}
IwTabs.prototype.setCurrentById = function(id) {
  var i=this.tabIdToIndex[id+this.index+"Tab"];
  if (i!=null && i!=this.current) {
    this.hide(this.current);
    this.current=i;
    this.show(i);
  }
}
IwTabs.prototype.setCurrent = function(i) {
  if (i!=null && i!=this.current) {
    this.hide(this.current);
    this.current=i;
    this.show(i);
  }
}
IwTabs.prototype.notifyListeners = function(id,selected) {
  if (id==null) {
    id=this.ids[this.current];
    selected=true;
  }
  var listeners=this.listeners[id];
  if (listeners!=null) {
    for (var i=0;i<listeners.length;i++) {
      selected ? listeners[i].tabSelected(this,id) : listeners[i].tabUnSelected(this,id);
    }
  }
}
IwTabs.prototype.validate = function(i) {
  var valid;
  var rules=this.yavRules[i];
  if (rules!=null) {
    valid=performCheck(this.formName, rules, this.alertType)
  } else {
    valid=true;
  }
  return valid;
}
IwTabs.prototype.validateCurrent = function() {
  var valid;
  if (this.current!=null) {
    valid=this.validate(this.current);
  } else {
    valid=true;
  }
  return valid;
}
IwTabs.prototype.validateAll = function() {
  var valid=true;
  for (var i=0;i<this.yavRules.length;i++) {
    if (!this.validate(i)) {
      this.setCurrent(i);
      valid=false;
      break;
    }
  }
  return valid;
}
IwTabs.prototype.click = function(comp) {
  if (!this.validateOnTabClick || this.validateCurrent()) {
    var id=comp.id;
    var i=this.tabIdToIndex[id];
    this.setCurrent(i);
  }
}
IwTabs.prototype.clickById = function(id) {
  if (!this.validateOnTabClick || this.validateCurrent()) {
    this.setCurrentById(id);
  }
}
IwTabs.prototype.mouseOver = function(comp) {
  var id=comp.id;
  var i=this.tabIdToIndex[id];
  if (i!=null && i!=this.current) {
    comp.className="tab";
    var tds=comp.getElementsByTagName('td');
    tds[0].className="tabOverSx";
    for (var j=1;j<=tds.length-2;j++) {
      tds[j].className="tabOverCx";
    }
    tds[tds.length-1].className="tabOverDx";
  }
}
IwTabs.prototype.mouseOut = function(comp) {
  var id=comp.id;
  var i=this.tabIdToIndex[id];
  if (i!=null && i!=this.current) {
    comp.className="tab";
    var tds=comp.getElementsByTagName('td');
    tds[0].className="tabSx";
    for (var j=1;j<=tds.length-2;j++) {
      tds[j].className="tabCx";
    }
    tds[tds.length-1].className="tabDx";
  }
}
IwTabs.prototype.next = function() {
  if (!this.validateOnNext || this.validateCurrent()) {
    var previous=this.current;
    if (this.current==null) {
      this.current=0;
    } else {
      this.current++;
      if (this.current>=this.ids.length) {
        this.current=0;
      }
    }
    if (previous!=null) {
      this.hide(previous);
    }
    this.show(this.current);
  }
}
IwTabs.prototype.previous = function() {
  if (!this.validateOnPrevious || this.validateCurrent()) {
    var next=this.current;
    if (this.current==null) {
      this.current=this.ids.length-1;
    } else {
      this.current--;
      if (this.current<0) {
        this.current=this.ids.length-1;
      }
    }
    if (next!=null) {
      this.hide(next);
    }
    this.show(this.current);
  }
}
IwTabs.prototype.hideAll = function() {
  for (var i=0;i<this.ids.length;i++) {
    this.hide(i);
  }
}
IwTabs.prototype.hide = function(i) {
  var comp=getObj(this.ids[i]+this.index+"Tab");
  comp.className="tab";
  var tds=comp.getElementsByTagName('td');
  tds[0].className="tabSx";
  for (var j=1;j<=tds.length-2;j++) {
    tds[j].className="tabCx";
  }
  tds[tds.length-1].className="tabDx";
  hideCompById(this.ids[i]+this.index+"Content");
  this.notifyListeners(this.ids[i],false);
}
IwTabs.prototype.show = function(i) {
  var comp=getObj(this.ids[i]+this.index+"Tab");
  comp.className="tabSel";
  var tds=comp.getElementsByTagName('td');
  tds[0].className="tabSelSx";
  for (var j=1;j<=tds.length-2;j++) {
    tds[j].className="tabSelCx";
  }
  tds[tds.length-1].className="tabSelDx";
  showCompById(this.ids[i]+this.index+"Content");
  this.notifyListeners(this.ids[i],true);
}
IwTabs.prototype.addTabListener = function(tabId,listener) {
  var listeners=this.listeners[tabId];
  if (listeners==null) {
    this.listeners[tabId]=new Array(listener);
  } else {
    listeners.push(listener);
  }
}
IwTabs.prototype.newFieldTabListener = function(field) {
  return new IwFieldTabListener(field);
}

// tabs manager
function IwTabsManager() {
  this.tabs=new Array();
}
IwTabsManager.prototype.getTabs = function(key) {
  var t=this.tabs[key];
  return t;
}
IwTabsManager.prototype.setTabs = function(key,tabs) {
  this.tabs[key]=tabs;
}
var tabsManager=new IwTabsManager();

// FieldTabListener
function IwFieldTabListener(field) {
  this.field=field;
}
IwFieldTabListener.prototype.tabSelected = function(tabs,tabId) {
  this.field.value="true";
}
IwFieldTabListener.prototype.tabUnSelected = function(tabs,tabId) {
  // nothing todo
}

