/**
 * Last change: jb, 02.02.06
 *
 * copied from vvv
 *
 */

var isIE =   navigator.userAgent.indexOf("MSIE") > -1
          && navigator.userAgent.indexOf("Opera") == -1;

var HeightCorrector = 50;
var WidthCorrector = 6;

function RemoveSpaces(inp) {
 var buffer = '';
 for (z=0; z < inp.length; z++)
  if(inp.charAt(z) != ' ') buffer = buffer + inp.charAt(z);
 return buffer;
}


function setFocus(element)
{
 element.focus();

 if(isIE)
 {
  if(element.oldClassName == null)
   element.oldClassName = element.className;
  element.className = "inputfocus";
 }

 if(element.select)
  element.select();
}

function PageLoaded()
{
 if(isIE)
  setFocusOnInputs()

 hideLoading();
}

function setFocusOnInputs()
{
 var inputs = document.getElementsByTagName("input");

 for(i = 0; i < inputs.length; i++)
 {
  var input = inputs[i];
  if(input.type == "text"
  || input.type == "password")
  {
   if(input.oldClassName == null)
    input.oldClassName = input.className;

   input.oldFocus = (input.onfocus)?input.onfocus:function(){};
   input.onfocus = function() { this.className = "inputfocus"; this.oldFocus(); };

   input.oldBlur = (input.onblur)?input.onblur:function(){};
   input.onblur = function() { this.className = this.oldClassName; this.oldBlur(); };
  }
 }

 inputs = document.getElementsByTagName("textarea");

 for(i = 0; i < inputs.length; i++)
 {
  var input = inputs[i];

  if(input.oldClassName == null)
   input.oldClassName = input.className;

  input.oldFocus = (input.onfocus)?input.onfocus:function(){};
  input.onfocus = function() { this.className = "inputfocus"; this.oldFocus(); };

  input.oldBlur = (input.onblur)?input.onblur:function(){};
  input.onblur = function() { this.className = this.oldClassName; this.oldBlur(); };
 }
}

function onInputFocus()
{
 doInputFocus(this);
}

function doInputFocus(element)
{
 element.oldClassName = element.className;
 element.className = "inputfocus";
}

function onInputBlur()
{
 this.className = this.oldClassName;
}

function hideLoading()
{
 document.body.style.cursor = 'auto';
 if(document.getElementById('Loading') != null)
  document.getElementById('Loading').style.visibility = 'hidden';
}

function showLoading()
{
 if(document.getElementById('Loading') != null)
  document.getElementById('Loading').style.visibility = 'visible';
}

function showHelp(Page)
{
 showHelpWindow('/Help?Page='+Page);
}

function showHelpWindow(URL)
{
 var width = Math.round(window.screen.availWidth*0.7);
 var height = Math.round(window.screen.availHeight*0.6);
 var left = Math.round((window.screen.width-width)/2);
 var top = Math.round((window.screen.height-height)/2 - (window.screen.height*0.1));

 window.open(URL,'_help','status=no,scrollbar=1,scrollbars=yes,resizable=yes,screenX='+left+',screenY='+top+',left='+left+',top='+top+',width='+width+',height='+height);
}

function showDownload(Page)
{
 var left = Math.round((window.screen.width-300)/2);
 var top  = Math.round((window.screen.height-400)/2);
 window.open(Page,'DlWin','width=300,height=200,status=no,scrollbar=0,scrollbars=No,screenX='+left+',screenY='+top+',left='+left+',top='+top+',resizable=no');
}

function showPrint(Page)
{
 var width = Math.round(window.screen.availWidth*0.7);
 var height = Math.round(window.screen.availHeight*0.9);
 var left = Math.round((window.screen.width-width)/2);
 var top = Math.round((window.screen.height-height)/2 - (window.screen.height*0.1));

 window.open(Page,'PrintWin','width='+width+',height='+height+',status=no,scrollbar=yes,scrollbars=yes,screenX='+left+',screenY='+top+',left='+left+',top='+top+',resizable=yes');
}

var thumb; // used to be able to recursively pass thumbnail to zoom function in setTimeout
function zoom(thumbnail)
{
 var img = new Image();

 var path = thumbnail.src.substring(0, thumbnail.src.lastIndexOf('/')+1);
 var name = thumbnail.src.substring(thumbnail.src.lastIndexOf('/')+1);

 img.src = path+'../'+name;

 if(!img.complete)
 {
  // if image has not been loaded, restart function after 100ms
  thumb = thumbnail;
  setTimeout('zoom(thumb)', 100);
  return;
 }

 var width = img.width + WidthCorrector;
 var height = img.height + HeightCorrector;
 var left = Math.round((window.screen.width-width)/2);
 var top  = Math.round((window.screen.height-height)/2);

 wnd = window.open('about:blank','_zoomed_image','status=no,scrollbars=no,resizable=no,screenX='+left+',screenY='+top+',left='+left+',top='+top+',width='+width+',height='+height);

 doc = wnd.document;
 doc.write('<html>');
 doc.write('<head>');
 doc.write('<title>'+document.title+' - '+name+'</title>');
 doc.write('</head>');
 doc.write('<body style="padding: 0; margin: 0;" onClick="window.close()">');
 doc.write('<table width=100% height=100% cellpadding=0 cellspacing=><tr><td align="center">');
 doc.write('<img src='+img.src+' width='+img.width+' height='+img.height+' align=middle ');
 doc.write('onClick="window.close()">');
 doc.write('</td></tr></table>');
 doc.write('</body>');
 doc.write('</html>');
 doc.close();
}

/**
 * Functions used by PrintWindow
 */

function resizePrintWindow()
{
 var Table = document.getElementById("PrintTable");

 var width = Table.offsetWidth+Table.offsetLeft+30;
 var height = Table.offsetHeight+Table.offsetTop+50;

 if(width > window.screen.availWidth-20)
  width = window.screen.availWidth-20;
 if(height > window.screen.availHeight-20)
  height = window.screen.availHeight-20;

 window.resizeTo(width, height);
}


/**
 * Functions used by DownloadWindow
 */

function resizeDownloadWindow()
{
 window.resizeTo(300, document.body.scrollHeight+HeightCorrector);
}

function doDownload(url)
{
 window.opener.location.replace(url);
 window.close();
}

/**
 * Functions used by NumberInput-Element
 */

function increase(input)
{
 if(input.value == '')
  input.value = 1;
 else
 if(!isNaN(input.value))
  input.value = (parseInt(input.value)+1);

 return false;
}

function decrease(input)
{
 if(input.value == '')
  input.value = 0;
 else
 if(!isNaN(input.value))
  input.value = (parseInt(input.value)-1);

 return false;
}

/**
 * Functions used by DBTable
 */

var AlterDirection = 0;
var MaxRows;
var TotalRows;
var Position;

function AlterMaxRows()
{
 if(AlterDirection != 0
 && (AlterDirection > 0 || MaxRows > 5)
 && Position+MaxRows < TotalRows)
 {
  MaxRows += AlterDirection;

  document.getElementById("DBTable_PositionIndicator").innerHTML =
    (Position+1)+"-"+(Position+MaxRows);

  setTimeout('AlterMaxRows()', 140);
 }
}

function AlterMaxRows_Start(direction)
{
 MaxRows = parseInt(document.DBTable_NavigationForm.DBTableNavigation_MaxRows.value);
 TotalRows = parseInt(document.DBTable_NavigationForm.DBTableNavigation_TotalRows.value);
 Position = parseInt(document.DBTable_NavigationForm.DBTableNavigation_Position.value);

 AlterDirection = direction;
 AlterMaxRows();
}

function AlterMaxRows_Finish()
{
 if(AlterDirection != 0)
 {
  AlterDirection = 0;

  showLoading();

  document.DBTable_NavigationForm.DBTableNavigation_MaxRows.value = MaxRows;
  document.DBTable_NavigationForm.submit();
 }
}

