function in_array( item, arr)
{
  if( !arr.length) {
    return false;
  }

  for(var i = 0; i < arr.length; i++) {
    if (item == arr[i]) {
      return i;
    }
  }

  return false;
}

function getId( id)
{
  if( document.getElementById( id)) {
    return document.getElementById( id);
  } else {
    return null;
  }
}

function getElement( id)
{
  if( document.getElementById( 'id_' + id)) {
    return document.getElementById( 'id_' + id);
  }

  return null;
}

function initializeObjects()
{
  objlist = document.getElementsByTagName("object");
  for ( var thisobj = 0; thisobj < objlist.length; thisobj++) {
    objlist[thisobj].outerHTML = objlist[thisobj].outerHTML;
  }
}

function getSelectedRadio( x)
{
  var value = '';

  // now get the new selection
  if( x && x.length) {
    // loop over all radios
    for( i = 0; i < x.length; i++) {
      if( x[i].checked) {
        value = x[i].value;
        // break;
      }
    }
  } else if( x) {
    // only single radio
    if( x.checked) {
      value = x.value;
    }
  }

  return value;
}

// returns the associative kex at index position x
function getArrayKey( arr, ix)
{

  var i = 0;
  for( key in arr) {
    if( i == ix) {
      return key;
    }
    i++;
  }

  return '';
}

// returns the key for searched value
function arraySearch( arr, val)
{

  for( key in arr) {
    if( val == arr[key]) {
      return key;
    }
  }
  return false;

}

// necessary for associative arrays
function arrayLength( arr)
{

  var i = 0;
  for( key in arr) {
    i++;
  }

  return i;
}

// currency conversion
function convertCurrency( amount, roe, rrule, rbase)
{
  var convamount = amount * roe;
  if( roe == 1) {
    return amount;
  }
  switch ( rrule) {
  case 'UP TO':
    convamount = convamount + rbase - 0.0001;
    convamount = parseInt( convamount / rbase) * rbase;
    break;
  case 'DOWN TO':
    convamount = parseInt( convamonut);
    break;
  case 'ACTUAL':
    break;
  case 'NEAREST':
  default:
    convamount = convamount + rbase / 2;
    convamount = parseInt( convamount / rbase) * rbase;
    break;
  }

  return convamount;
}

// get AJAX Object for HTTP Requests
function getXmlObj()
{
  var http;

  // Internet Explorer
  if( window.ActiveXOject) {
    http = new ActiveXObject( 'Microsoft.XMLHTTP');
  // Rest of the world
  } else if( window.XMLHttpRequest) {
    http = new XMLHttpRequest();
  } else {
    // old IEs
    http = new ActiveXObject("Msxml2.XMLHTTP.3.0");
  }

  if( !http) {
    return null;
  }

  return http;
}

// remove leading and trailing whitespaces
function trim (zeichenkette) {
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

// for the lazy guy
function g( id)
{
  return document.getElementById( id) || false;
}

// show hide slide
// IE
if( navigator.appName.indexOf("Microsoft") !=-1) {
  var is_ie = 1;
} else {
  var is_ie = 0;
}

// step = pixels per movement
var step = 12;
// intv = setTimeoutparameter 2
var intv = 10;
// stores item data for reuse and to support multiple toggles
var toggleitems = [];

// main function
// optional 2nd parameter: action
// possible values:
// 'open' to open the target
// 'close' to close the target
function toggle( targetid) {
  if( !g(targetid)) {
    return false;
  }

  var action = false;

  if( arguments.length > 1) {
    action = arguments[1];
    if( action == '' || (action != 'open' && action != 'close')) {
      action = false;
    }
  }

  if( !toggleitems[targetid]) {
    toggleitems[targetid] = [false,0,0];
  }

  var item = toggleitems[targetid];
  var active = item[0];
  var dir = item[1]
  var x = item[2];

  if( active &&
      (arguments.length < 3 ||
       arguments[2] != 'timeout')) {
    return false;
  }

  if( !active) {
    if( (!action && g( targetid).style.display == 'block') ||
        (action && action == 'close' && g( targetid).style.display == 'block' )) {
      dir = -1*step;
      active = true;
      ht = getRealHeight( g( targetid));
    } else if( (!action && g( targetid).style.display == 'none') ||
               (action && action == 'open' && g( targetid).style.display == 'none')) {
      active = true;
      dir = step;
      ht = 0;
      g( targetid).style.display = 'block';
    }
  } else {
    var scrht = g(targetid).scrollHeight;
    var ht = getRealHeight( g( targetid));
    if( isNaN( ht)) {
      ht = '0';
    }
  }

  if( active) {
    newht = ht + dir;
    if( newht <= 0) {
      newht = 0;
      active = false;
      g( targetid).style.display = 'none';
    } else if( newht >= scrht) {
      newht = scrht;
      active = false;
    } else {
      window.setTimeout( 'toggle( "'+ targetid +'","' +  action + '", "timeout")', intv);
    }

    g(targetid).style.height = newht + 'px';
    toggleitems[targetid] = [ active, dir, x ];
    return true;
  }
}

// get real height
function getRealHeight( element){
  if(is_ie){
    return parseInt( element.offsetHeight);
  } else {
    return parseInt( document.defaultView.getComputedStyle(element, "").getPropertyValue("height"));
  }
}

function toggleImage( targetid, imgopen, imgclose, myaction)
{
  var img = g( targetid);
  var src = img.src.split( '/');
  var imgo = imgopen.split( '/');
  var imgc = imgclose.split( '/');

  if( (!myaction && src[src.length - 1] == imgo[imgo.length -1]) ||
      (myaction && myaction == 'close')) {
    img.src = imgclose;
  } else if( !myaction || (myaction && myaction == 'open')) {
    img.src = imgopen;
  }
}

// set flag if combos are already replaced
var replaced = false;

// replaces all combos by spans (IE 6 issue)
// mode is either replace or restore
function replaceCombos( replacemode, excludelist)
{
  if( navigator.appVersion.indexOf( "MSIE 6") == -1) {
    return;
  }

  if( replacemode === 'restore') {
    replaced = false;
  } else {
    if( replaced) {
      return;
    }

    replaced = true;
  }

  // setup excludelist
  if( typeof excludelist !== 'undefined') {
    var useexcludes = true;
  } else {
    var useexcludes = false;
  }


  var arSelect = document.getElementsByTagName("select");
  var len=arSelect.length;
  var objSel=null;
  var strNodeValue="";
  var objSpan=null;
  var o=null;

  for(var i=0;i<len;i++) {
    objSel=arSelect[i];
    strNodeValue = ' ' + objSel.options[objSel.selectedIndex].text + '  ';

    if( replacemode == 'restore') {
      objSel.style.display = '';

      if( g( objSel.id + "_spanreplacement")) {
        o = g( objSel.id + "_spanreplacement");
        o.parentNode.removeChild(o);
      }
    } else {
      if( useexcludes && excludelist[objSel.name] && excludelist[objSel.name] === true) {
        continue;
      }

      objSel.style.display = 'none';
      objSpan = document.createElement("span");
      objSpan.id = objSel.id+"_spanreplacement";
      objSpan.appendChild(document.createTextNode(strNodeValue));
      objSpan.className = "modalWrap";
      objSel.parentNode.insertBefore(objSpan,objSel);
    }
  }
}

// reduces simple array to a new array without duplicates. keys are not
// preserved and the resulting array will always be an indexed array
// regardless of whether the input array was associative or indexed.
function array_unique( a)
{
  var tmpa = new Array();
  var newa = new Array();
  for( i in a) {
    if( !tmpa[a[i]]) {
      tmpa[a[i]] = a[i];
      newa.push( a[i]);
    }
  }
  return newa;
}

