function browser(){
  ie = (    (document.all) 
         && (window.offscreenBuffering) ) ? true : false;
  nn = (    (document.captureEvents) 
         && (!document.getElementById)  ) ? true : false;
  mz = (    (document.getElementById) 
         && (!document.all) 
         && (document.documentElement)  ) ? true : false;
  op = (    (document.getElementById) 
         && (navigator.userAgent.indexOf('Opera') != -1) ) ? true : false;    
}
function isIE(){    
    return  (    (document.all) 
              && (window.offscreenBuffering)) ? true : false;
}

function isMozilla(){    
    return  (    (document.getElementById) 
              && (!document.all) 
              && (document.documentElement)  ) ? true : false;}

function UnCryptMailto(s, shift) {
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(shift)); 
	}
	return r;
}
function linkTo_UnCryptMailto(s, shift)	{
	location.href=UnCryptMailto(s, shift);
}

function getCookie(Name,Default){
    var search = Name + "="
    if (document.cookie.length > 0){
        offset = document.cookie.indexOf(search)
        if (offset != -1){
            offset += search.length
            end = document.cookie.indexOf(";", offset)
            if (end == -1)
                end = document.cookie.length
            return unescape(document.cookie.substring(offset, end))
        }
    }
    return Default;
}

// " " (ASCII 32 (0x20)), an ordinary space.
// "\t" (ASCII 9 (0x09)), a tab.
// "\n" (ASCII 10 (0x0A)), a new line (line feed).
// "\r" (ASCII 13 (0x0D)), a carriage return.
// "\0" (ASCII 0 (0x00)), the NUL-byte.
// "\x0B" (ASCII 11 (0x0B)), a vertical tab.
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function getCookie(Name,Default){
    var search = Name + "="
    if (document.cookie.length > 0){
        offset = document.cookie.indexOf(search)
        if (offset != -1){
            offset += search.length
            end = document.cookie.indexOf(";", offset)
            if (end == -1)
                end = document.cookie.length
            return unescape(document.cookie.substring(offset, end))
        }
    }
    return Default;
}

// automatische selektion in einem select element
function selectOption(element, value){
    var opt = element.options;
    for(i=0;i<opt.length;i++){
        if(opt[i].value==value){
            element.selectedIndex = i;
            return;
        }
    }
}

function selectRadio(form, fieldName, selectedValue){
    var field = form[fieldName];
    for(i=0;i<field.length;i++){
        if(field[i].value==selectedValue){
            field[i].checked=true;
            return;
        }
    }
}

function selectCheckbox(element, selectedValue){
    element.checked = element.value==selectedValue;
}

// noValue ist der nicht gesetzte Wert z.B. 0 oder -1
function checkSelected(previousState, element, noValue){
    var ret = previousState;
    if(element.options[element.selectedIndex].value == noValue ){
        element.style.backgroundColor = '#ffa0a0';
        element.onmousedown = new Function("this.style.backgroundColor='white'");
        ret = false;
    }
    return ret;
}

function checkNotEquals(previousState, element, value){
    var ret = previousState;
    if(element.value == value ){
        element.style.backgroundColor = '#ffa0a0';
        element.onkeypress= new Function("this.style.backgroundColor='white'");
        ret = false;
    }
    return ret;
}

function checkNotEmpty(previousState, element){
    var ret = previousState;
    if(element.value.length == 0 ){
        element.style.backgroundColor = '#ffa0a0';
        element.onkeypress= new Function("this.style.backgroundColor='white'");
        ret = false;
    }
    return ret;
}
function checkInteger(previousState, element){
    var ret = previousState;
    var regexp = /^[0-9]*$/;
    if(regexp.test(element.value)==false){
        element.style.backgroundColor = '#ffa0a0';
        element.onkeypress= new Function("this.style.backgroundColor='white'");
        ret = false;
    }
    return ret;
}
function checkValidProperty(previousState, element){
    var ret = previousState;
    if(element.valid != undefined){
        if(element.valid == false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkEmail(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        var regexp = /^([A-Za-z0-9_\-\+\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkWeb(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        if(element.value.indexOf('http://')!=0){
            element.value = 'http://' + element.value;
        }
        var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{2}/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkVorwahl(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        var regexp = /^0[1-9][0-9]+$/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkVerbindung(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        element.value = element.value.replace(/ /g, "");
        var regexp = /^[1-9][0-9]+[\-\/ ]?[0-9]+$/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkMobil(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        element.value = element.value.replace(/ /g, "");
        var regexp = /^0[1-9][0-9]+[\/\-]?[1-9][0-9]+$/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkJahr(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        var regexp = /^[12][09][0-9][0-9]$/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
    
}
function checkDatum(previousState, element){
    var ret = previousState;
    if(element.value!=''){
        var regexp = /^[0-9][0-9]\.[0-9][0-9]\.[12][09][0-9][0-9]$/;
        if(regexp.test(element.value)==false){
            element.style.backgroundColor = '#ffa0a0';
            element.onkeypress= new Function("this.style.backgroundColor='white'");
            ret = false;
        }
    }
    return ret;
}
function checkMaxLength(previousState, element, max_length){
    var ret = previousState;
    if(element.value.length >max_length){
        element.style.backgroundColor = '#ffa0a0';
        element.onkeypress= new Function("this.style.backgroundColor='white'");
        ret = false;
    }
    return ret;
}
function check(element, value){
    if(element.value == value)
        element.checked = true;
}
// division display or none ------------------------------------
function display(element,state){
    node = document.getElementById(element);
    if(node){
        node.style.display = state ? "block" : "none";
    }
}
function toggleDisplay(element){
    node = document.getElementById(element);
    if(node){
        if(node.style.display=="" || node.style.display=="none"){
          node.style.display = "block";
        } else {
          node.style.display = "none";
        }
    }
}

// division show hide ------------------------------------
function showDiv(divId){
    div = document.getElementById(divId);
    div.style.visibility = 'visible';
    openDiv = divId;
}
function hideDiv(divId){
    div = document.getElementById(divId);
    div.style.visibility = 'hidden';
}

// confirm link
function confirmAction(href,prompt){
    if(confirm(prompt))
        window.location.href=href;
}

// confirm submission of form
function confirmSubmit(prompt,form){
    if(confirm(prompt)){
        if(!form.onsubmit){
            form.submit();
            return true;
        }
        if(form.onsubmit && form.onsubmit()){
            form.submit();
            return true;
        }
    }
    return false;
}

function setInput(form, name, value){
    form.elements[name].value = value;
}
function copyDiv(from, to){
    document.getElementById(to).innerHTML = document.getElementById(from).innerHTML;
}
function emptyDiv(id){
    document.getElementById(id).innerHTML = '';
}

// fuegt zeichen in ein input field ein
// Bsp: <img onclick="insertTags(adresse.nachname, '[b]', '[/b]')" border="0" src="bold.png" title="Text als fett markieren">
function insertTags(input, aTag, eTag) {
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
}

/** 
 *   .popup { position: absolute; background-color: white; top: 100px; left: 100px; visibility: hidden; padding: 20px; border: 5px outset red; z-index: 999;}
 *   
 *   <div id="popup" class="popup">
 *       <p id="popup_msg"></p><br>
 *       <p align="center"><input id="popupOk" type=button value=OK onclick="closePopup()"></p>
 *   </div>
 *   
 *   popup(document.getElementById('gaga'), 'das geht so aber nicht');
 */
function popup(parentNode, msg){
    divover(true);
    var popup     = document.getElementById('popup');
    var popup_msg = document.getElementById('popup_msg');
    if(parentNode!=null){
      if(popup.parentNode){
        popup.parentNode.removeChild(popup);
        document.body.insertBefore(popup, document.body.firstChild);
      }
      
      zentrieren(popup, parentNode);
      //var dimDiv = getDimension(parentNode);
      //popup.style.top  = (dimDiv.y+dimDiv.height)/2 + 'px';
      //popup.style.left = dimDiv.x + 'px';
    } else {
      popup.style.top  = '300px';
      popup.style.left = '300px';
    }
    popup_msg.innerHTML = msg;
    showDiv('popup');
    document.getElementById("popupOk").focus();
}
function closePopup(){
    hideDiv('popup');
    divover(false);
}
function showDetails(id){
    var url = "http://"
             +"anwaltsuche.anwalt-suchservice.de"
             +"/anwaltsuche/standard/index.html"
             +"?method=detailsId&contactId="+id;
    window.open(url, "details");
}

function getDimension(element)
{
    var x = y = 0;
    var node = element;
    if(node.nodeName != 'IFRAME'){
      while (node) {
          x += node.offsetLeft;
          y += node.offsetTop;
          node = node.offsetParent;
      }
    }
    var dimension=new Object();
    dimension.x=x;
    dimension.y=y;
    dimension.width  = element.offsetWidth;
    dimension.height = element.offsetHeight;
    return dimension;
}

function divover(state){
    if(state){
      divover.div = document.createElement("div");
      divover.div.className = 'divover';
      document.body.insertBefore(divover.div, document.body.firstChild);
    } else {
        if(divover.div){
          document.body.removeChild(divover.div);
          divover.div = null;
        }
    }
}
function zentrieren(element, relativElement)
{
    var dimElement = getDimension(element);
    var dimRelativ = getDimension(relativElement);
    
    var top  = (dimRelativ.y + dimRelativ.height - dimElement.height)/2;
    var left = (dimRelativ.x + dimRelativ.width  - dimElement.width)/2;

    element.style.top  = "" + top + "px";
    element.style.left = "" + left + "px";
}

function getSource(evt){
         if(evt.srcElement) return evt.srcElement;
    else if(evt.target)     return evt.target;
    return null;
}

function Item(key,value){
    this.key        = key;
    this.value      = value;
    this.editable   = true;
    this.removable  = true;
    this.editItem   = null;   // spezielle Funktion moeglich
    this.removeItem = null;   // spezielle Funktion moeglich
    this.removeIcon = null;
    this.editIcon   = null;
}

function Liste(id, formElement) {
    this.id          = id;
    this.formElement = formElement;
    this.items       = new Object();
    this.length      = 0;
    this.maxlen      = 10;
    this.edit        = false;   // editieren ist erlaubt oder auch nicht, symbol anzeigen oder auch nicht
    this.insert      = false;   // hinzufuegen ist erlaubt oder auch nicht, symbol anzeigen oder auch nicht
    this.remove      = true;    // entfernen ist erlaubt oder auch nicht, symbol anzeigen oder auch nicht
    this.insertPosition = 'bottom';   // top oder bottom Position fuer Eintrag hinzufuegen
    this.insertLabel    = "Eintrag hinzuf&uuml;gen";
    this.removeConfirm  = "Eintrag wirklich entfernen?";
    this.emptytext      = null;   // Text, der Angezeigt werden soll, wenn liste leer ist
    this.toomanytext    = null;   // Text, der Angezeigt werden soll, wenn maxlen ueberschritten ist
    
    this.setEdit = function(edit){
        this.edit = edit;
    }
    this.setInsert = function(insert){
        this.insert = insert;
    }
    this.setRemove = function(remove){
        this.remove = remove;
    }
    this.setMaxlen = function(maxlen){
        this.maxlen = maxlen;
    }
    this.setEmptytext = function(emptytext){
        this.emptytext = emptytext;
    }
    this.setToomanytext = function(toomanytext){
        this.toomanytext = toomanytext;
    }
    this.setInsertPosition = function(insertPosition){ // top oder bottom Position fuer Eintrag hinzufuegen
      this.insertPosition = insertPosition;
    }
    this.setInsertLabel = function(insertLabel){
      this.insertLabel = insertLabel;
    }
    this.setRemoveConfirm = function(removeConfirm){
      this.removeConfirm = removeConfirm;
    }
    this.zeigeAnzahl = function(){
        var span = document.getElementById("anzahl_"+id);
        if(span){
            span.innerHTML = this.size() + "/" + this.maxlen;
        }
    }
    this.size = function(){
        var size = 0;
        for(key in this.items){
            size++;
        }
        return size;
    }
    this.add = function(key, value, refill){
        if(this.size()>=this.maxlen){
            if(refill){ // nur bei benutzerinteraktion melden
              if(this.toomanytext){
                popup(document.getElementById(this.id), this.toomanytext);
              } else {
                popup(document.getElementById(this.id), "Bitte maximal " + this.maxlen + " Eintr&auml;ge ausw&auml;hlen");
              }
            }
            return null;
        }

        var item = new Item(key,value)
        this.items[key] = item;
        if(refill){
            this.fill();
        }
        return item;
    }
    this.removeItem = function(key, refill){
        if(confirm(this.removeConfirm)){
            this.removeItemQuiet(key,refill);
            return true;
        }
        return false;
    }
    this.removeItemQuiet = function(key, refill){
          delete this.items[key];
          if(refill){
              this.fill();
          }
    }
    this.editItem = function(key){
        alert("funktion editItem ueberschreiben!");
    }
    this.addItem = function(key){
        alert("funktion addItem ueberschreiben!");
    }
    
    this.fillItem = function(key){
        var item = this.items[key];
        
        var div = document.getElementById(this.id);
        var table     = document.createElement('table');
        div.appendChild(table);
        
        var tableBody = document.createElement("tbody");
        table.appendChild(tableBody);
        
        var tr    = document.createElement('tr');
        tableBody.appendChild(tr);
        
        var tdL   = document.createElement('td');
        var tdR   = document.createElement('td');
        tr.appendChild(tdL);
        tr.appendChild(tdR);
        
        if(this.remove && item.removable){

            var removeIcon = item.removeIcon!=null ? item.removeIcon : '/vrn/media/pencil_delete.png';
            var span = document.createElement('span');
            span.innerHTML = "<img src='"+removeIcon+"' align=absmiddle alt='Eintrag loeschen'>";
            span.style.cursor = "pointer";
            span.style.marginTop = "3px";
            span.onclick   = new Function("liste['"+id+"'].removeItem('"+key+"');");
            if(item.removeItem!=null){
                span.onclick = item.removeItem;
                span.title   = "Eintrag permanent loeschen.";
            } else {
                span.onclick = new Function("liste['"+id+"'].removeItem('"+key+"',true);");
                span.title   = "Eintrag aus dieser Liste entfernen.";
            }            
            tdL.appendChild(span);
        }
        
        if(this.edit && item.editable){
            if(this.remove && item.removable){
                tdL.appendChild(document.createElement('br'));
            }
            var editIcon = item.editIcon!=null ? item.editIcon : '/vrn/media/pencil.png';
            var span = document.createElement('span');
            span.innerHTML = "<img src='"+editIcon+"' align=absmiddle alt='Eintrag bearbeiten' title='Eintrag bearbeiten'>";
            span.style.cursor = "pointer";
            span.style.marginTop = "3px";
            if(item.editItem!=null){
                span.onclick = item.editItem;
            } else {
                span.onclick = new Function("liste['"+id+"'].editItem('"+key+"');");
            }
            
            tdL.appendChild(span);
        }
        tdR.innerHTML = item.value;
        
        arr = key.split("|");
        if(arr.length==3){
            tdR.innerHTML = tdR.innerHTML + "<br>In der Kanzlei t&auml;tig als: <b>" + arr[2] + "</b>";
        }
    }
    this.addInsert = function(){
        var div = document.getElementById(this.id);
        var span = document.createElement('span');
        span.innerHTML = "<b><img src='/vrn/media/pencil_add.png' align=absmiddle alt='"+this.insertLabel+"' title='"+this.insertLabel+"'>"+this.insertLabel +"</b><br>";
        span.style.cursor = "pointer";
        span.onclick   = new Function("liste['"+id+"'].addItem();");
        div.appendChild(span)
    }
    
    this.fill = function(){
        var div = document.getElementById(this.id);
        while(div.childNodes.length>0){
            div.removeChild(div.childNodes[0]);
        }
        if(this.insert==true && this.insertPosition=='top'){
            this.addInsert();
        }
        for(key in this.items){
            this.fillItem(key);
        }
        this.zeigeAnzahl();
        if(this.insert==true && this.insertPosition=='bottom'){
            this.addInsert();
        }
        if(this.size()==0 && this.emptytext){
            var paragraph  = document.createElement('p');
            paragraph.innerHTML = this.emptytext;
            div.appendChild(paragraph);
        }
        var liste = "";
        for(key in this.items){
            if(liste.length>0){liste += ",";}
            liste += key;
        }
        this.formElement.value = liste;
    }
    this.dump = function(){
        var info = ''
        for(key in this.items){
            var item = this.items[key];
            info += item.key+"="+item.value+"\n";
        }
        alert(info);
    }
}
