// Basisfunktionen
// (c) 2007-2010 schukai GmbH
// www.alvine.eu

// Funktions- und Environment-Variablen
if(typeof _ALVINE_BACKEND == 'undefined') var _ALVINE_BACKEND = false;
_ALVINE_BACKEND = (_ALVINE_BACKEND === true)?true:false;
var _ALVINE_DHTML = false;
var _ALVINE_DOM = false;
var _ALVINE_COMPAT_MODE = document.compatMode;
var _ALVINE_QUIRKS_MODE = true;
if(typeof _ALVINE_DEBUG_MODE == 'undefined') var _ALVINE_DEBUG_MODE = false;
_ALVINE_DEBUG_MODE = (_ALVINE_DEBUG_MODE === true)?true:false;
var _ALVINE_ERROR_HANDLER = 'none';  //file,message,etc.
var _ALVINE_IE = false;
var _ALVINE_IE6 = false;
var _ALVINE_IE7 = false;
var _ALVINE_IE8 = false;
var _ALVINE_NS = false;
var _ALVINE_OP = false;
var _ALVINE_MAC = false;
var _ALVINE_NETSCAPE = false;
var _ALVINE_FIREFOX  = false;
var _ALVINE_FIREFOX2 = false;
var _ALVINE_FIREFOX3 = false;
var _ALVINE_FIREFOX4 = false;
var _ALVINE_SAFARI  = false;
var _ALVINE_SAFARI1 = false;
var _ALVINE_SAFARI2 = false;
var _ALVINE_SAFARI3 = false;
var _ALVINE_SAFARI4 = false;
var _ALVINE_CHROME  = false;  //new
var _ALVINE_CHROME1  = false;  //new
var _ALVINE_CHROME2  = false;  //new
var _ALVINE_CHROME3  = false;  //new
var _ALVINE_CLIENT_OS = null;

// Initialisieren der Environmet-Variablen
function initLib() {

  if(!_ALVINE_DEBUG_MODE && _ALVINE_ERROR_HANDLER=='none'){
    window.onerror = function(){
      return false;
    }
  }

  // Testen ob Javascript eingebunden
  setCookie("AlvineJavascript", true, 0, '/');

  // Render Modus ermitteln
  var m = document.compatMode.toString().toLowerCase();
  _ALVINE_COMPAT_MODE = m;
  _ALVINE_QUIRKS_MODE = (m == 'backcompat')?true:false;

  if(navigator.appName=="Netscape") {
    _ALVINE_NETSCAPE = true;
  }

  if(/Windows/.test(navigator.userAgent)){
    _ALVINE_CLIENT_OS = 'win';
  } else if(/Macintosh/.test(navigator.userAgent)){
    _ALVINE_CLIENT_OS = 'mac';
  } else if(/Linux/.test(navigator.userAgent)){
    _ALVINE_CLIENT_OS = 'lin';
  }

  if(navigator.userAgent.indexOf('Chrome')<0){
    if (/AppleWebKit\/(\S+)\s[\s|\S]+/.test(navigator.userAgent)) {
      _ALVINE_SAFARI = true;

      var safariBuild;
      //var webKitBuild = RegExp.$1;
      var fullVersion;

      if(navigator.userAgent.indexOf('Version')<0){

        if (/Safari\/(\S+)/.test(navigator.userAgent)) {
          safariBuild = RegExp.$1;
          fullVersion = parseInt(safariBuild);
        }

        if(safariBuild<=419){
          version = 1;
          _ALVINE_SAFARI1 = true;
        } else {
          version = 2;
          _ALVINE_SAFARI2 = true;
        }

      } else {

        if (/Version\/(\S+)\sSafari\/(\S+)/.test(navigator.userAgent)) {

          fullVersion = RegExp.$1;
          var version = parseInt(fullVersion); // capture x.x portion and store as a number
          safariBuild = RegExp.$2;

          if(version==4) _ALVINE_SAFARI4 = true;
          else if (version==3) _ALVINE_SAFARI3 = true;

        }
      }
    }
  }

  if(navigator.userAgent.indexOf('Chrome')>0){
    if (/AppleWebKit\/(\S+)\s[\s|\S]+/.test(navigator.userAgent)) {
      _ALVINE_CHROME = true;

      var chromeBuild;
      var webKitBuild = RegExp.$1;
      var fullVersion;

      if (/Chrome\/(\S+)/.test(navigator.userAgent)) {
        chromeBuild = RegExp.$1;
        fullVersion = parseInt(chromeBuild);
      }

      
      if(chromeBuild<530 || fullVersion == 1){
        version = 1;
        _ALVINE_CHROME1 = true;
      } else if(chromeBuild<532 || fullVersion == 2) {
        version = 2;
        _ALVINE_CHROME2 = true;
      } else if(chromeBuild>=532 || fullVersion == 3) {
        version = 3;
        _ALVINE_CHROME3 = true;
      }

    }
  }

  if (/Firefox[\/\s](\d+)(\.(\d+)?)/.test(navigator.userAgent)) {
    var version=new Number(RegExp.$1); // capture x.x portion and store as a number
    _ALVINE_FIREFOX = true;

    if(version==4) _ALVINE_FIREFOX4 = true;
    else if (version==3) _ALVINE_FIREFOX3 = true;
    else if (version==2) _ALVINE_FIREFOX2 = true;
		
  }

  if(navigator.appName=="Netscape") {
    _ALVINE_NETSCAPE = true;
  }

  if(document.getElementById) {
    _ALVINE_DHTML = true;
    _ALVINE_DOM = true;
  }
	
  if(window.opera) {
    _ALVINE_OP = true;
  }

  if(document.all && !_ALVINE_OP) {
		 
    var $versionpattern = /MSIE\s(.*?)\;/i;
    var version = navigator.appVersion.match($versionpattern);
    version = version[1];
					
    if(version.substring(0, 1)=='6') _ALVINE_IE6 = true;
    if(version.substring(0, 1)=='7') _ALVINE_IE7 = true;
    if(version.substring(0, 1)=='8') _ALVINE_IE8 = true;
		 
    _ALVINE_DHTML = true;
    _ALVINE_IE = true;
		
  };
 
  if(document.layers && !_ALVINE_OP) {
    _ALVINE_DHTML = true;
    _ALVINE_NS = true;
  };
	
  if(navigator.userAgent.indexOf("Mac") != -1) {
    _ALVINE_MAC = true;
  };

  if(_ALVINE_DEBUG_MODE && _ALVINE_ERROR_HANDLER!='none' && _ALVINE_IE6){
    window.onerror = handleErrors;
  }

};




function getElementID(id) {
  return getElement('id', id);
};

// System- und browserunabhägige Funktion um ein
// Element (Objekt) zu holen
// @param type Auswahl aus: id, name, tagname
// @param ptr ID oder Name des Elements
// @param index Nur bei Verwendung des Typ tagname
function getElement(type, ptr, index) {

  if(_ALVINE_DOM) {
    if(type.toLowerCase()=="id") {
      if(typeof document.getElementById(ptr) == "object") {
        return document.getElementById(ptr);
      };
      return void(0);

    } else if(type.toLowerCase()=="name") {
      if(typeof document.getElementsByName(ptr) == "object") {
        return document.getElementsByName(ptr)[index];
      };
      return void(0);

    } else if(type.toLowerCase()=="tagname") {
      if(typeof document.getElementsByTagName(ptr) == "object" || (_ALVINE_OP && typeof document.getElementsByTagName(ptr) == "function")) {
        return document.getElementsByTagName(ptr)[index];
      };
      return void(0);
    } else {
      return void(0);
    };

  } else if(_ALVINE_IE) {

    if(type.toLowerCase()=="id") {
      if (typeof document.all[ptr] == "object") {
        return document.all[ptr];
      };
      return void(0);

    } else if(type.toLowerCase()=="tagname") {
      if(typeof document.all.tags(ptr) == "object") {
        return document.all.tags(ptr)[index];
      };
      return void(0);

    } else if(type.toLowerCase()=="name") {
      if(typeof document[ptr] == "object") {
        return document[ptr];
      };
      return void(0);
    } else {
      return void(0);
    };

  } else if(_ALVINE_NS) {

    if(type.toLowerCase()=="id" || type.toLowerCase()=="name") {
      if (typeof document[ptr] == "object") {
        return document[ptr];
      };
      return void(0);

    } else if(type.toLowerCase()=="index") {
      if (typeof document.layers[ptr] == "object") {
        return document.layers[ptr];
      };
      return void(0);
    } else {
      return void(0);
    };
  };
  return void(0);
}

function alvine_datagrid_changeColor(obj, color) {
  if(!obj) return;
  if(color) {
    obj.setAttribute("alvineChangeColor", obj.style.backgroundColor);
  } else {
    color = obj.getAttribute("alvineChangeColor");
  };
  obj.style.backgroundColor = color;
}

// Browserunabhängig Inhalte in die Seite einfügen
// @param obj Zeiger auf Objekt
// @param content Zu schreibender inhalt 
function writeHTML(obj, content) {

  if(!obj) return void(0);
  if(_ALVINE_OP) {
    obj.innerHTML = content;
  } else if(_ALVINE_DOM) {   // IE7
    if(typeof(obj.innerHTML)!="undefined") {
      obj.innerHTML = content;
    } else {
  // nicht implementiert
  };
  } else if(_ALVINE_IE) {
  //obj.innerHTML = content;
  } else if(_ALVINE_NS) {
    obj.document.open();
    obj.document.write(content);
    obj.document.close();
  };
	 
  return void(0);
};

function changeSource(obj, source) {
  if(!obj) return;
  if(!source) return;
  obj.src=source;
}

// Browserunabhängig Inhalte in die Seite einfügen
// @param obj Zeiger auf Objekt
// @param content Zu schreibender inhalt (nur Text)
function writeContent(obj, content) {

  if(!obj) return void(0);
  if(_ALVINE_OP) {

  // nicht implementiert
	 
  } else if(_ALVINE_DOM) {
    if(obj.firstChild) {
      obj.firstChild.nodeValue = content;
    };

  } else if(_ALVINE_IE) {
    obj.innerText = content;

  } else if(_ALVINE_NS) {
    obj.document.open();
    obj.document.write(content);
    obj.document.close();
  };
  return void(0);
};

// Diese Funktion verschiebt ein Objekt 
// an die angegebenen koordinaten
function moveElementTo(obj, x, y) {

  if(_ALVINE_DOM || _ALVINE_OP) {
    obj.style.left = x+'px';
    obj.style.top = y+'px';
  } else if(_ALVINE_NS) {
    obj.moveTo(x, y);
  };

};
 
function setVisibilityMode(obj, flag) {

  if(!obj) return void(0);

  if(_ALVINE_DOM || _ALVINE_OP) {
    if(flag) {
      obj.style.visibility = "visible";
      obj.style.display = "block";
    } else {
      obj.style.visibility = "hidden";
      obj.style.display = "none";
    };

  } else if(_ALVINE_IE) {
    if(flag) {
      obj.style.visibility = "visible";
      obj.style.display = "block";
    } else {
      obj.style.visibility = "hidden";
      obj.style.display = "none";
    };
	 
  } else if(_ALVINE_NS) {
    if(flag) {
      obj.visibility="show";
      obj.display="block";
    } else {
      obj.visibility="hide";
      obj.display="none";
    };
  };
  return void(0);
};

// Sichtbarkeit von Elementen ändern.
// @param obj Zeiger auf Element
// @param flag true: sichtbar, false: unsichtbar
function setVisibility(obj, flag) {

  if(!obj) return void(0);

  if(_ALVINE_DOM || _ALVINE_OP) {
    if(flag) {
      obj.style.visibility = "visible";
      obj.style.display = "block";
    } else {
      obj.style.visibility = "hidden";
      obj.style.display = "none";
    };

  } else if(_ALVINE_IE) {
    if(flag) {
      obj.style.visibility = "visible";
      obj.style.display = "block";
    } else {
      obj.style.visibility = "hidden";
      obj.style.display = "none";
    };
	 
  } else if(_ALVINE_NS) {
    if(flag) {
      obj.visibility="show";
      obj.style.display = "block";
    } else {
      obj.visibility="hide";
      obj.style.display = "none";
    };
  };
  return void(0);
};

// Position eines Elements ermitteln
// Diese Funktion arbeitet mit dem Offset eines Elements
// und arbeitet sich rekursiv durch das DOM
// Ist eine AbbruchsID angegeben und trift
// die Funktion auf dieses Element so wird der
// Wert zurückgegeben
// @param obj Zeiger auf Element
// @param id AbbruchsID
function getLeftOfElement(obj, id) {

  if(_ALVINE_NS) return obj.pageX;

  if(!id) id = "noid";
  var left = obj.offsetLeft;
  if(obj.id==id) return left;

  if(obj.offsetParent!=null && obj.offsetParent.tagName!="HTML") {
    left += getLeftOfElement(obj.offsetParent);
  };
  return left;
}

function toggleFoldTableRow(id) {
	
  var tbl, row;
  row = getElementID(id);
  if(!row) return false;

  if(row) {

    if(row.style.display=='none' || !row.style.display) {
			
      foldTableRow(row, true);
			
    } else {

      foldTableRow(row, false);
			
    }
  }
	
  return true;
}

function foldTableRow(obj, forcedStatus) {
  obj = element_isObject(obj);

  if(!obj) return;
	
  if(forcedStatus) {
    // conditional compilation to hide the try-catch blocks from IE4
    /*@cc_on @if(!@_jscript || (@_jscript_version >= 5)) @*/
    try {
      obj.style.display = 'table-row';
    } catch(e) {
      obj.style.display = 'block';
    }
  /*@elif(@_jscript_version < 5)
			row.style.display = 'block'; // for IE4
		@end @*/
  } else {
    obj.style.display = 'none';
  }
	
  return true;
}

function getScrollOffsetOfElement(obj) {
  var diff = 0;
  if(obj.scrollTop) diff = obj.scrollTop;
  if(obj.tagName=="BODY") return diff;
  if(obj.parentNode) diff += getScrollOffsetOfElement(obj.parentNode);
  return diff;

}

// Identische Funktion für die Y-Koordinate
// @param obj Zeiger auf Element
// @param id AbbruchsID
function getTopOfElement(obj, id) {

  if(_ALVINE_NS) return obj.pageY;
  if(!id) id = "noid"; // dummywert!
  var top = obj.offsetTop;

  if(obj.id==id) return top;
  if(obj.offsetParent!=null && obj.offsetParent.tagName!="HTML") {
    top += getTopOfElement(obj.offsetParent);
  };
	
  // to: add padding
	
  return top;
}

// Höhe eines Objektes zurückgeben
function getHeightOfElement(obj) {
  if(_ALVINE_NS) return obj.height;
  return obj.offsetHeight;
};

// Breite eines Objektes zurückgeben
function getWidthOfElement(obj) {
  if(_ALVINE_NS) return obj.width;
  return obj.offsetWidth;
};

function getCookie(name) {

  name = name.replace(/^\s+|\s+$/g,"");

  values = document.cookie;
  values = values.split(";");
 
  for(var i=0;i<values.length;i++) {
    var p = values[i].split("=");
    p[0] = p[0].replace(/^\s+|\s+$/g,"");
    if(name==p[0]) {
      return p[1];
    }
  }
  return "";
};

// Cookie aufbauen
function setCookie(name, value, expires, path, domain, secure) {
  var today = new Date();
  today.setTime(today.getTime());
  if(expires) {
    expires = expires*86400000;
  };
  var expireDate = new Date(today.getTime()+(expires));
  document.cookie = name+"="+escape(value)+
  ((expires)?";expires="+expireDate.toGMTString():"")+
  ((path)?";path="+path:"")+
  ((domain)?";domain="+domain:"")+
  ((secure)?";secure":"");
} 

// Größen

function alvine_getClientWidth() {
  return alvine_getRectStruct (window.innerWidth ? window.innerWidth : 0,
    document.documentElement ? document.documentElement.clientWidth : 0,
    document.body ? document.body.clientWidth : 0
    );
}

function alvine_getClientHeight() {
  return alvine_getRectStruct (window.innerHeight ? window.innerHeight : 0,
    document.documentElement ? document.documentElement.clientHeight : 0,
    document.body ? document.body.clientHeight : 0
    );
}

function alvine_getScrollLeft() {
  return alvine_getRectStruct (window.pageXOffset ? window.pageXOffset : 0,
    document.documentElement ? document.documentElement.scrollLeft : 0,
    document.body ? document.body.scrollLeft : 0
    );
}

function alvine_getScrollTop() {
  return alvine_getRectStruct (window.pageYOffset ? window.pageYOffset : 0,
    document.documentElement ? document.documentElement.scrollTop : 0,
    document.body ? document.body.scrollTop : 0
    );
}

function alvine_getRectStruct(n_win, n_docel, n_body) {
  var n_result = 0;
  if(n_win) n_result = n_win;
  if(n_docel && (!n_result || (n_result > n_docel))) n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
};


// Simple AJAX - Object 

function alvineSimpleAjaxRequest(url, id, errorID, appendInputFlag) {
  var url = url;
  var containerID = (typeof id != 'object')?id:id.id;
  var errorContainerID = (typeof errorID != 'object')?errorID:errorID.id;
  var requestObj = false;
	
  var urlRequestValues = {};
  var urlData = new Array();
  var callBacks = new Array();
  var finalCallBacks = new Array();

  var urlControlData = new Array();
  var urlControlID = new Array();

  var urlStaticParameter = "";
  var urlOneWayParameter = "";

  var appendInput = (appendInputFlag===true)?true:false;

  this.addToUrl = function(urlPart) {
    url+= ','+urlPart;
  }

  this.addParameter = function(part) {
    if(urlStaticParameter.length>0) urlStaticParameter += '&';
    urlStaticParameter+= part;
  }

  this.addOnWayParameter = function(part) {
    if(urlOneWayParameter.length>0) urlOneWayParameter += '&';
    urlOneWayParameter+= part;
  }

  function checkState() {
    // 0 (nicht initialisiert)
    // 1 (lade)
    // 2 (geladen)
    // 3 (interaktiv)
    // 4 (vollständig)
    if(requestObj.readyState!=4) return false;

    if(requestObj.status==200) {
      return requestObj.responseText;
    } else {
		
      errorMsg = 'Error: '+requestObj.status; // @TODO: sprachversionen

      if(errorContainerID!=null) {
        obj = getElement('id', errorContainerID);
        writeHTML(obj,errorMsg);
        return false;
      };

      return errorMsg;
    }
  }
	
  this.sendRequest = function() {
			 
    requestObj = null;
    if(window.XMLHttpRequest) { // Mozilla, Firefox, Safari, ...
      requestObj = new XMLHttpRequest();
      if (requestObj.overrideMimeType) {
        requestObj.overrideMimeType('text/html'); // BugFix für fehlerhaften Response
      };
    } else if(window.ActiveXObject) { // IE
      try {
        requestObj = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          requestObj = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
        };
      }
    };

    if(!requestObj) {
		
      errorMsg = 'no ajax'; // @TODO: sprachversionen

      if(errorContainerID!=null) {
        obj = getElement('id', errorContainerID);
        writeHTML(obj,errorMsg);
        return false;
      };

      return false;
    }

    var parameters = urlStaticParameter;
    if(parameters.length>0) parameters += '&';
    parameters += urlOneWayParameter;
    urlOneWayParameter = "";
    for(i=0; i<urlData.length;i++) {
      if(parameters.length>0) parameters += '&';
      value = eval(urlData[i]);
      parameters += 'sxx_ajax['+urlData[i]+']='+value;
    };

    for(var stKey in urlRequestValues) {
      if(parameters.length>0) parameters += '&';
      value = urlRequestValues[stKey];
      parameters += 'sxx_ajax['+stKey+']='+value;
    };

    for(i=0; i<urlControlID.length;i++) {
      if(parameters.length>0) parameters += '&';
      value = eval(urlControlData[i]);
      parameters += 'sxx_ajax['+urlControlID[i]+']='+value;
    };

    if(parameters.length>0) parameters += '&';
    parameters += 'r='+(1 + 10000*(Math.random()));
		
    try {
      requestObj.onreadystatechange = eventHandler;
      requestObj.open('POST', url, true);
      requestObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');   // wenn data
      requestObj.send(parameters); // WENN DATA hier übergeben
    } catch (e) {
      alert(e);
    };
  }

  this.bindControl = function(key, id) {
    handler = 'o=getElementID(\''+id+'\'); if(o) o.value;';
    urlControlData[urlControlData.length] = handler;
    urlControlID[urlControlID.length] = key;
  }
	
  this.bindVariable = function(key) {
    urlData.push(key);
  }

  this.setResponseValue = function(boxID, key, value) {
    var stNewKey = boxID+'_'+key;    
    if(typeof urlRequestValues[stNewKey] != 'object') urlRequestValues[stNewKey] = {};
    urlRequestValues[stNewKey] = value;
  }

  this.bindCallback = function(callBack, forceExecution) {
    var newEntry = new Object();
    newEntry.forcedExecution = (forceExecution===true)?true:false;
    newEntry.cbfunction = callBack;
    finalCallBacks.push(newEntry);
  };
	
  this.bindObjectCallback = function(callBack) {
    callBacks.push(callBack);
  };

  function eventHandler() {
    var result = checkState();

    if(result!=false){
      var obj = getElement('id', containerID);
      resultContent = result;

      scriptStartIndex = resultContent.indexOf('<json>');
      if(scriptStartIndex>=0) {
        scriptEndIndex = resultContent.indexOf('</json>');
        skript = resultContent.substr(scriptStartIndex+6, scriptEndIndex-scriptStartIndex-6);
        data=alvine_getParsedJSONObject(skript);
        for(j=0;j<callBacks.length;j++) {
          callBacks[j]('json',data);
        };
        resultContent = resultContent.substr(0,scriptStartIndex)+resultContent.substr(scriptEndIndex+6+1,resultContent.length);
      };


      outScript = "";

      var collectorObj = document.createElement('DIV');
      writeHTML(collectorObj, resultContent);

      var scriptSegments = collectorObj.getElementsByTagName('SCRIPT');
      var scriptSegmentSeek = /\<script\s\S+type\=\"text\/javascript\"\S*\>(.*)\<\/script\>/;
      scriptSegmentSeek.exec(resultContent, 'g');

      if(scriptSegments){
        for(i=0; i<scriptSegments.length; i++) {
          if(typeof scriptSegments[i] != 'object') continue;
          var newContent = scriptSegments[i].innerHTML;
          newContent    += (newContent.substring(-1)!=';')?';':'';
          outScript     += newContent;

          if(typeof scriptSegments[i].parentNode == 'undefined') continue;
          if(typeof scriptSegments[i].parentNode.removeChild != 'function') continue;
          scriptSegments[i].parentNode.removeChild(scriptSegments[i]);
        }
        resultContent = collectorObj.innerHTML;
      }
	      
      scriptStartIndex = resultContent.indexOf('<array>');
      if(scriptStartIndex>=0) {
        scriptEndIndex = resultContent.indexOf('</array>');
        skript = resultContent.substr(scriptStartIndex+7, scriptEndIndex-scriptStartIndex-7);

        data=eval(skript);
        for(j=0;j<callBacks.length;j++) {
          callBacks[j]('array',data);
        };
        resultContent = resultContent.substr(0,scriptStartIndex)+resultContent.substr(scriptEndIndex+7+1,resultContent.length);
      };

      scriptStartIndex = resultContent.indexOf('<object>');
      if(scriptStartIndex>=0) {
        scriptEndIndex = resultContent.indexOf('</object>');
        skript = resultContent.substr(scriptStartIndex+8, scriptEndIndex-scriptStartIndex-8);
        data=eval(skript);
        for(j=0;j<callBacks.length;j++) {
          callBacks[j]('object',data);
        };
        resultContent = resultContent.substr(0,scriptStartIndex)+resultContent.substr(scriptEndIndex+8+1,resultContent.length);
      };

      if(appendInput) {
        obj.innerHTML+= resultContent;
      } else {
        writeHTML(obj, resultContent);
      }

      if(outScript) eval(outScript);

      if(finalCallBacks.length>0){ //execute callback functions
        for(j=0;j<finalCallBacks.length;j++) {
          eval(finalCallBacks[j].cbfunction);
        }
      }

      return;
    }

    if(finalCallBacks.length>0){
      for(j=0;j<finalCallBacks.length;j++) {  //execute only forced functions
        if(!finalCallBacks[j].forcedExecution) continue;
        eval(finalCallBacks[j].cbfunction);
      }
    }

    return;
  };
	
};

function getViewportHeight() {

  vpHeight = null;
  if(document.documentElement.clientHeight) {  // FIREFOX
    vpHeight = document.documentElement.clientHeight
  } else if(window.innerHeight) { // Netscape & Co
    vpHeight = window.innerHeight;
  } else if(document.clientHeight) {
    vpHeight = document.clientHeight;
  } else if(document.body && document.body.clientHeight) { // IE
    vpHeight = document.body.clientHeight;
  } else if(document.documentElement.clientHeight) {  // IE6 im standardMode
    vpHeight = document.documentElement.clientHeigt;
  } else { // Unbekannt
  }
  return vpHeight;
}

function getViewportWidth() {

  vpWidth = null;
  if(document.documentElement.clientWidth) {  // FIREFOX
    vpWidth = document.documentElement.clientWidth
  } else if(window.innerWidth) { // Netscape & Co
    vpWidth = window.innerWidth;
  } else if(document.clientWidth) {
    vpWidth = document.clientWidth;
  } else if(document.body && document.body.clientWidth) { // IE
    vpWidth = document.body.clientWidth;
  } else if(document.documentElement.clientWidth) {  // IE6 im standardMode
    vpWidth = document.documentElement.clientWidth;
  } else { // Unbekannt
  }
	
  return vpWidth;
}

function handleErrors(msg,file,line){
  var eMesg = null;
  
  eMesg = 'Error: '+msg+"\n"+"\n"+'File:'+file+"\n"+"\n"+'Line:'+line+"\n";
  alert(eMesg);

  return true;
}

/*IE6 ActiveX Controls Visibility*/
function ie6ToggleActiveXControls(state){

  if(!_ALVINE_IE6) return true;

  state = (state===false)?false:true;

  var objectList = {};
  var controlList = new Array('select');
  var i;

  for(i=0; i<controlList.length; i++){
    objectList[controlList[i]] = document.getElementsByTagName(controlList[i]);
  }

  for(var k in objectList){
    var fieldList = objectList[k];
    for(i=0; i<fieldList.length; i++){
      if(typeof fieldList[i] != 'object') continue;
      if(typeof fieldList[i].className == 'undefined') continue;      
      if(state==true){
        
        if(fieldList[i].style.visibility != 'hidden') continue; //object is already visible
        var newDisplay = (typeof fieldList[i].alvine_displayInitValue != 'undefined')?fieldList[i].alvine_displayInitValue:'visible';
        element_setVisibility(fieldList[i], newDisplay);

      } else {

        if(typeof fieldList[i].alvine_displayInitValue == 'undefined') fieldList[i].alvine_displayInitValue = fieldList[i].style.visibility;
        element_setVisibility(fieldList[i], 'hidden');

      }
    }
  }

  return true;
}



function alvine_getParsedJSONObject(jsonData) {

  try{
    if(typeof JSON=='undefined') {
      JSON = {};
      JSON.parse = function(expr) {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(expr.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + expr + ')');
      }
    }

  } catch(event) {};
  return JSON.parse(jsonData);
}



// JSON


function uniqueArray(arr) {
  var o = {};
  var tmp = new Array();
  for(var i = 0 ; i < arr.length; i++) o[arr[i]] = true;
  for(var i in o) tmp[tmp.length] = i;
  return tmp;
};

function removeFromArray(arr, entry) {
  var o = {};
  var tmp = new Array();
  for(var i = 0 ; i < arr.length; i++) {
    if(arr[i]==entry) continue;
    o[i] = arr[i];
  }
  for(var i in o) tmp[tmp.length] = o[i];
  return tmp;
};


// Umgebungsvariablen initialisieren
initLib();

// Testwerte
//alert('FLAGS \nIE ' + _ALVINE_IE + " \nDOM " + _ALVINE_DOM + " \nNS "+ _ALVINE_NS + " \nOP " +_ALVINE_OP + " \nMAC " + _ALVINE_MAC);
