//############################################################
//# $Id: functions.js,v 1.9 2006-09-18 12:41:41 monecke Exp $
//#
//#  Copyright (c) 2005 Institute of Transport, 
//#                     Railway Construction and Operation, 
//#                     University of Hanover, Germany
//#
//# lm/060126 
//############################################################

// startup functions

init_form();

// add eventhandler to all inputs
function init_form() {

  oForm = document.forms[0];

  for (var i=0; i < oForm.elements.length; i++) {

    oForm.elements[i].onkeypress = disable_return_submit;

  }

}

// swallow all return key and try to focus next form element
function disable_return_submit(event) {

  event = (event) ? event : window.event;

  if ( event.keyCode != 13) {

    return true;

  } else {

    var oForm = null;
    var iElementId = -1;
    var iNextId = -1;
    var oTarget = (event.target) ? event.target : (event.srcElement) ? event.srcElement : null;

    if (oTarget) {

      oForm = oTarget.form;

      iElementId = getIndex(oTarget);
      iNextId = (typeof oForm.elements[iElementId + 1] != 'undefined') ? iElementId + 1 : 0;

      oForm.elements[iNextId].focus();

      return false;
    }
  }
}

// return the index of the given element in his form
function getIndex(oElement) {

  var oItr = null;
  var i = -1;
  var oForm = oElement.form;

  while (oItr = oForm.elements[++i]) {

    if ( oItr == oElement){
      return i;
    }
  }

  return null;
}


// global variable with the starting height
var g_iRealContentHeight = 0;

function setContentMinimumHeight(iIdContent, iIdHeader, iIdFooter) {

  var iHeaderHeight = document.getElementById(iIdHeader).offsetHeight;
  var iContentHeight = document.getElementById(iIdContent).offsetHeight;
  var iFooterHeight = document.getElementById(iIdFooter).offsetHeight;
  
  if (g_iRealContentHeight == 0) {
    g_iRealContentHeight = iContentHeight;
  } else {
    iContentHeight = g_iRealContentHeight;
  }
  

  var iWindowHeight = 0;
  
  if (self.innerHeight) {
    // all except Explorer
    iWindowHeight = self.innerHeight - 10;
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    // Explorer 6 Strict Mode
    iWindowHeight = document.documentElement.clientHeight;
  }
  else if (document.body) {
    // other Explorers
    iWindowHeight = document.body.clientHeight - 8;
  }

  var iOverall = iHeaderHeight + iContentHeight + iFooterHeight;
  var iDiff = iWindowHeight - iOverall;

//    alert("window=" + iWindowHeight + " "
//          + "header=" + iHeaderHeight + " "
//          + "content=" + iContentHeight + " "
//          + "footer=" + iFooterHeight + " "
//          + "overall=" + iOverall + " "
//          + "diff=" + iDiff + " ");

  if (iDiff > 0) {
    document.getElementById(iIdContent).style.height = (iContentHeight + iDiff) + 'px';
  }
}


function show_progress() {
  
  var stInput  = document.getElementById("lyInput");
  var stProgress  = document.getElementById("lyProgress");
  var stCalculate  = document.getElementById("eco_btn_calculate");

  if (!stCalculate) {
    stCalculate  = document.getElementById("eco_btn_recalculate");    
  }

  var cCoords = findPos(stInput);
  var cCoordsCalculate = findPos(stCalculate);

  stProgress.style.left = cCoords[0] + (cCoords[2] / 2 - stProgress.offsetWidth / 2) + 'px';
  stProgress.style.top =  cCoords[1] + (((cCoordsCalculate[1] - cCoords[1]) / 2) - stProgress.offsetWidth / 2) + 'px';
  stProgress.style.visibility = "visible";

  // we must hide all input elements because of bug in IE6
  stInput.style.visibility = "hidden";

}

function findPos(obj) {
        var curleft = curtop = curwidth = curheight = 0;
        if (obj.offsetParent) {
                curleft = obj.offsetLeft
                curtop = obj.offsetTop
                curwidth = obj.offsetWidth
                curheight = obj.offsetHeight
                while (obj = obj.offsetParent) {
                        curleft += obj.offsetLeft
                        curtop += obj.offsetTop
                }
        }
        return [curleft, curtop, curwidth, curheight];
}
