// Copyright 2004 and onwards  Zucchetti Spa.

function LoadBodyImage(imageName, fixedHeight, topPosition) {
  var img=new Image()
  img.src=imageName
  document.images[document.images.length-1].src=img.src
  //SetRowHeightDiff(document.images[document.images.length-1].id,fixedHeight,topPosition,'immagine')
}

function PreviousZone(x1, x2, topPosition) {
  var z = new Zones(0,0,0,0)
  // cerco le zone di appartenenza
  var bSet = false
  for (var i in m_oZones) {
    //se l'elemento interseca in una zona di stretch
    if ((m_oZones[i].x1 >= x1 && m_oZones[i].x1 <= x2)
         || (m_oZones[i].x2 >= x1 && m_oZones[i].x2 <= x2)
         || (x1 > m_oZones[i].x1 && x2 < m_oZones[i].x2) ) {
      if ( m_oZones[i].y < topPosition && m_oZones[i].heightDiff > z.heightDiff/*&& m_oZones[i].y > z.y */) {
        //alert("interseca:"+i+" topPosition:"+topPosition+"; zone_y:"+m_oZones[i].y+"; result_y:"+z.y )
        z = m_oZones[i]
        bSet = true
      }
    }
  }
  if (bSet)     {
    return z
  }
}

function MyZone(x1, y1) {
  var z
  for (var i in m_oZones) {
    if (m_oZones[i].x1 == x1 && m_oZones[i].y == y1)   {
      z = m_oZones[i]
      //alert("mia zona:"+i)
    }
  }
  return z
}

function SetRowHeightDiff(id, fixedHeight, topPosition, type) {
  var ctrl = Ctrl(id)
  if (ctrl == null) { alert(type+"  "+id);return; }
  var i=0
  var difference = 0
  var tmp_h
  var x1 = Val(ctrl.currentStyle.left)
  var x2 = x1 + Val(ctrl.currentStyle.width)
  var z = PreviousZone(x1, x2, topPosition)

  switch (type) {
    case 'immagine':
      tmp_h = ctrl.height
      break;
    case 'box':
      tmp_h =Val(ctrl.currentStyle.height)
      break;
    default:
      tmp_h = ctrl.offsetHeight
      break;
  }

  if (z != null) {
    difference = z.heightDiff
    // verifico se bisogna spostare il top
    if (difference > 0) {
      //alert("sposto il top di "+ctrl.id+" di px:"+difference+". Fixed height:"+fixedHeight)
      ctrl.style.top = Val(ctrl.currentStyle.top) + difference
    }
  }
  // imposto la nuova differenza per tutte la zona dell'elemento
  z = MyZone(x1, topPosition)
  if (z != null)  {
    if (tmp_h > fixedHeight || difference > 0) {
      //alert("nuova altezza:"+tmp_h+" precedente:"+fixedHeight+" differenza prec.:"+difference)
      //alert("imposto indice "+i+" a "+m_oZones[idx[i]].heightDiff)
      z.heightDiff = difference + tmp_h - fixedHeight
      SetHeightInContainer(id, tmp_h - fixedHeight)
    }
  }
  if (type=='box') {
    SetBoxHeight(id, tmp_h)
  }
}

function SetBoxHeight(id, oldH) {
  var s, itmid
  var h = 0

  for (s in m_oContainer) {
    if (s == Left(id, 10)) {
      for (itmid in m_oContainer[s]) {
        h = Max(m_oContainer[s][itmid], h)
      }
    }
  }
  if (h > 0) {
    //alert("aumento altezzo box di "+h)
    Ctrl(id).style.height = oldH + h
  }
}

function SetHeightInContainer(id, h) {
  var s, itmid
  for (s in m_oContainer) {
    for (itmid in m_oContainer[s]) {
      if (itmid == Left(id, 10)) {
        m_oContainer[s][itmid] = m_oContainer[s][itmid] + h
      }
    }
  }
}

function SetIdHeight(id, h) {
  Ctrl(id).style.height = h + MaxRowDiff()
  ClearRowDiff()
}

function ClearRowDiff() {
  var z = 0
  for (z in m_oZones) {
    m_oZones[z].heightDiff = 0
  }

  var s, itmid
  for (s in m_oContainer) {
    for (itmid in m_oContainer[s]) {
      m_oContainer[s][itmid] = 0
    }
  }
}

function MaxRowDiff() {
  var m = 0, z = 0
  for (z in m_oZones) {
    m = Max(m, m_oZones[z].heightDiff)
  }
  //alert("diff:"+m)
  return m
}

function Zones() {
  this.x1 = arguments[0];
  this.x2 = arguments[1];
  this.heightDiff = arguments[2];
  this.y = arguments[3];
}

function ChangeRow(p_nRow) {
  if (m_nCurrentRow==-1) {
    m_nCurrentRow = p_nRow;
    TrsToWork();
    SetControlsValue();
  } else {
    if (Ne(m_nCurrentRow,p_nRow)) {
      if (CheckRow()) {
        WorkToTrs();
        m_nCurrentRow = p_nRow;
        TrsToWork();
        SetControlsValue();
      } else {
        DoUpdate(false);
      }
    }
  }
}

function FillFrmTrs() {
  var row,trsrow=0,p,name,prefix
  WorkToTrs()
  var a=TrsApplet()
  a.SetRow(0)
  for(trsrow=0;trsrow<m_oTrsFW.length;trsrow++) {
    a.SetRow(trsrow+1)
    row=m_oTrsFW[trsrow]
    p={}
    for(var prop in row) {
      p[prop]=1
    }
    prefix=''
    for(var i=0;i<arguments.length;i++) {
      name=arguments[i]
      if(Eq(row['w_'+name],row['t_'+name]) && 3!=row['m_nRowStatus']) {
        delete p['w_'+name]
        delete p['o_'+name]
        delete p['t_'+name]
        prefix+='|k_'+name+'_|'
      }
      if(Empty(row['w_'+name]))
        delete p['w_'+name]
      if(Empty(row['t_'+name]))
        delete p['t_'+name]
      if(Empty(row['o_'+name]))
        delete p['o_'+name]
    }
    for(var prop in p) {
      pos=prop.lastIndexOf('_')
      if(prefix.indexOf('|'+prop.substr(0,pos+1)+'|')!=-1) {
         delete p[prop]
      }
    }
    for(var prop in p) {
      a.setValue(prop,AsAppletValue(row[prop]))
    }
  }
}

function DoPrevious(prev) {
  document.FSender.m_bQuery.value='true'
  SubmitForm("previous")
}

function DoNext() {
  document.FSender.m_bQuery.value='true'
  SubmitForm("next")
}

function GetStretchedHeight(m_cDiv){
  var ctrl = Ctrl(m_cDiv)
  if (ctrl == null) { alert(type+"  "+m_cDiv);return; }
  var n
  if (typeof(ctrl.offsetHeight)=='undefined')
    n=ctrl.height
  else
    n=ctrl.offsetHeight
  return(n)
}

function SetStretchedHeight(m_cDiv,m_nHeight){
  var ctrl=Ctrl(m_cDiv)
  if (ctrl==null) return
  ctrl.style.height=m_nHeight
}

function DocumentInterceptor(){
  this.output=''
  this.write=function(s){
    this.output+=s
  }
  this.writeln=function(s){
    this.write(s+"\r\n")
  }
  this.getOutput=function(){
    return this.output
  }
  this.emptyOutput=function(){
    this.output=''
  }
}
