
function isIE()
{
  if (navigator.appName == 'Microsoft Internet Explorer') return true;
  return false;
}

function manualCloneNodeEdit(destDocument,node)
{
  if ((destDocument == null) || (node == null)) return null;
  
  var clone = null;
  
  if (node.nodeType == 3) {
    clone = destDocument.createTextNode(node.data);
    }
  else if (node.nodeType == 8) {
    clone = destDocument.createComment(node.data);
    }
  else {
    if ((isIE()) && (node.nodeName == 'input')) {
      switch (node.getAttribute('type')) {
        case 'checkbox':
          if (node.getAttribute('checked')) clone = destDocument.createElement('<input type=checkbox checked>');
          else clone = destDocument.createElement('<input type=checkbox>');
          break;
        case 'radio':
          if (node.getAttribute('checked')) clone = destDocument.createElement('<input type=radio checked>');
          else clone = destDocument.createElement('<input type=radio>');
          break;
        default:
          clone = destDocument.createElement(node.nodeName);
          break;
        }
      }
    else if ((isIE()) && (node.nodeName == 'option')) {
      if (node.getAttribute('selected')) clone = destDocument.createElement('<option selected>');
      else clone = destDocument.createElement('<option>');
      }
    else {    
      clone = destDocument.createElement(node.nodeName);
      }
  
    for (var i=0; i < node.attributes.length; i++) {
/*
      if (node.attributes.item(i).nodeName == 'onclick') {
        clone.setAttribute('onclickurl',node.attributes.item(i).nodeValue);
        clone.onclick = function() {fctredir(this.getAttribute('onclickurl'));};
        }
      else
*/
      if (node.attributes.item(i).nodeName == 'onchange') {
        clone.setAttribute('onchangeurl',node.attributes.item(i).nodeValue);
        clone.onchange = function() {fctredir(this.getAttribute('onchangeurl'));};
        }
      else if (node.attributes.item(i).nodeName == 'class') {
        clone.className = node.attributes.item(i).nodeValue;
        }
      else if (node.attributes.item(i).nodeName == 'id') { 
        if (node.nodeName == 'input') { 
          if (node.attributes.item(i).nodeValue == 'html') { 
            var val = node.getAttribute('value');
            val = val.replace(/&amp;/g,"&").replace(/&quot;/g,"\"").replace(/&lt;/g,"<").replace(/&gt;/g,">");
            node.setAttribute('value',val);
        // who comes first, id or value? -> set always both    
            clone.setAttribute('value',val);
            }
          }
        clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
        }
      else if (node.attributes.item(i).nodeName == 'src') { 
        clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue.replace(/&amp;/g,"&"));
        }
      else if (node.attributes.item(i).nodeName == 'type') {
        if (isIE()) {
          switch (node.nodeName) {
            case 'checkbox':
            case 'radio':
              break;
            default:
              clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
              break;
            }
          }
        else {
          clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
          }
        }
      else if (node.attributes.item(i).nodeName == 'selected') {
        if (isIE()) {
          switch (node.nodeName) {
            case 'option':
              break;
            default:
              clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
              break;
            }
          }
        else {
          clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
          }
        }
      else {
        clone.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
        }
      }
      
    for (var i=0; i < node.childNodes.length; i++) {
      clone.appendChild(manualCloneNodeEdit(destDocument,node.childNodes.item(i)));
      }  
    }
    
  return clone;
};

function fctredir(fct)
{
  eval(fct);
};

function cloneNodeTo(destNode,node)
{
  if ((destNode == null) || (node == null)) return null;
  if (destNode.nodeType != node.nodeType) return null;
  if (destNode.nodeName.toLowerCase() != node.nodeName.toLowerCase()) return null;
  if (node.nodeType == 3) return null;
  
  var children = destNode.childNodes.length;
  for (var i=children-1; i >= 0; i--) {
    destNode.removeChild(destNode.childNodes.item(i));
    }
  
  for (var i=0; i < node.attributes.length; i++) {
    if (node.attributes.item(i).nodeName == 'onclick') {
      destNode.setAttribute('onclickurl',node.attributes.item(i).nodeValue);
      destNode.onclick = function() {tl2(this,this.getAttribute('onclickurl'));};
      }
    else if (node.attributes.item(i).nodeName == 'onblur') {
      destNode.setAttribute('onblururl',node.attributes.item(i).nodeValue);
      destNode.onclick = function() {tl2(this,this.getAttribute('onblururl'));};
      }
    else if (node.attributes.item(i).nodeName == 'class') {
      destNode.className = node.attributes.item(i).nodeValue;
      }
    else {
      destNode.setAttribute(node.attributes.item(i).nodeName,node.attributes.item(i).nodeValue);
      }
    }
      
  for (var i=0; i < node.childNodes.length; i++) {
    destNode.appendChild(manualCloneNodeEdit(destNode.ownerDocument,node.childNodes.item(i)));
    }  
    
  return destNode;  
};

function rebuildElementFromXml(baseElem,editHref,postData) // baseElem is empty div
{
//  var indexElementObj = new indexXml(editHref);
  var indexElementObj = new clientXml(encodeURI(editHref),postData);
  var parsed = indexElementObj.getParsed();
  if (parsed == false) {
    alert ('not parsed: '+editHref);
    return;
    }
  var indexElement = indexElementObj.getIndexElement();
  if (indexElement == null) {
    alert ('indexElement == null: '+editHref);
    return;
    }
    
//  alert('rebuildElementFromXml '+baseElem);
  cloneNodeTo(baseElem,indexElement);
};

function goEdit(idname, idval, parentId, onexitfunc)
{
  var editBase;
  if (parentId) {
    var parentElement = findByNodeNameAndId(null,'div',parentId);
    editBase = parentElement.appendChild(document.createElement('div'));
    }
  else {
    editBase = document.getElementsByTagName('body')[0].appendChild(document.createElement('div'));
    }
  editBase.className = 'editdiv';
  editBase.setAttribute('id','editdiv'+idname);

  if (!onexitfunc) onexitfunc = '';
  rebuildElementFromXml(editBase,'?editname='+idname+'&editval='+idval+'&onexitfunc='+onexitfunc,null);
  
  if (idname == 'qid') {
    var editPreview = findByNodeNameAndId(editBase,'td','editpreview');
    editPreview.onclick = function () {showMenu(false);};
    editPreview.ondblclick = function (event) {globalDblClick(event);};
    }
}

function goDelete(idname, idval)
{
  if (confirm('Sollen '+idname+'='+idval+' und alle Abhaengigkeiten wirklich geloescht werden?')) {
//    alert('Löschen, wie geht das?');
    window.location.href = '?delname='+idname+'&delval='+idval;
    }
};

function saveEdit(editdivid,query)
{
  var body = document.getElementsByTagName('body')[0];
  var editBase = findByNodeNameAndId(body,'div',editdivid);
  
  if (!editBase) return;
  
  var poststr = '';
  var form = editBase.getElementsByTagName('form')[0];
  var amp = '';
  
  for (var i=0; i < form.elements.length; i++) {
    if (form.elements[i].name) {
      poststr += amp + form.elements[i].name + '=';
      if (form.elements[i].nodeName.toLowerCase() == 'input') {
        if (form.elements[i].getAttribute('type') == 'checkbox') {
          poststr += ((form.elements[i].checked) ? '-1' : '0');
          }
        else {  
          poststr += encodeURIComponent(form.elements[i].value);
          }
        }
      else {  
        poststr += encodeURIComponent(form.elements[i].value);
        }
      amp = '&';
      }
    }

//alert(query+' / '+poststr);
  rebuildElementFromXml(editBase,query,poststr);
};

function closeEdit(elemId,onexitfunc)
{
  var editDiv = findByNodeNameAndId(null,'div',elemId);

  if (editDiv == null) return;

  var editParent = editDiv.parentNode;
  editParent.removeChild(editDiv);

  if (onexitfunc) {
    fctredir(onexitfunc);
    }
  else {
    location.reload();
    }
};

function dateExpand()
{
//  alert('dateExpand');
  var iFriendlySyntax = findByNodeNameAndId(null,'input','friendlysyntax');
  var iDay = findByNodeNameAndId(null,'input','day');
  var iMonth = findByNodeNameAndId(null,'input','month');
  var iYear = findByNodeNameAndId(null,'input','year');
  var iOriginalSyntax = findByNodeNameAndId(null,'input','originalsyntax');
  var iNormalizedSyntax = findByNodeNameAndId(null,'input','normalizedsyntax');
  
  var dYear = '0020';
  var dDM = '00';
  
  var friendly = iFriendlySyntax.value;
  var parts = friendly.split('.');
  iDay.value = dDM;
  iMonth.value = dDM;
  var tmp;
  for (var i=parts.length-1; i >= 0 ; i--) {
    if (i == parts.length-1) {
      tmp = dYear+parts[i];
      iYear.value = tmp.substr(tmp.length-4,4);
      }
    else if (i == parts.length-2) {
      tmp = dDM+parts[i];
      iMonth.value = tmp.substr(tmp.length-2,2);
      }
    else if (i == parts.length-3) {
      tmp = dDM+parts[i];
      iDay.value = tmp.substr(tmp.length-2,2);
      }
    }
  iOriginalSyntax.value = friendly;
  iNormalizedSyntax.value = iYear.value + iMonth.value + iDay.value;
}

function propChangeExec2(query,noSelection)
{
  var indexElementObj = null;

  if (noSelection) {
    alert('Keine Auswahl getroffen');
    return;
    }
    
  indexElementObj = new clientXml(query,null);
  var indexElement = indexElementObj.getIndexElement();
  if (indexElement.textContent) {
    if (indexElement.textContent != 'done') alert('update control textContent: '+indexElement.textContent);
    }
  else {
    if (indexElement.text != 'done') alert('update control text: '+indexElement.text);
    }
}

function handleImageSelect(inputElem,basepath)
{
  var td = inputElem.parentNode;
  var img = td.getElementsByTagName('img')[0];
  if (inputElem.value == '-------') {
    img.setAttribute('src','img/placebo.gif');
    }
  else {
    img.setAttribute('src',basepath+inputElem.value);
    }
};

function globalDblClick(event) {
  var sel;
  var txt;
  var x;
  var y;
  var cx;
  var cy;
  var parElem;
  var parId;
  
  if (!event) event = window.event;
  
  var movX,movY;
  if (event.pageX) {
    movX = event.layerX;
    movY = event.layerY;
    }
  else {  
    movX = event.x-10;    
    movY = event.y-10;
//    movX = event.offsetX;    
//    movY = event.offsetY;
    }

/*
  sel = document.selection.createRange();
  parElem = sel.parentElement();
  parId = parElem.id;
*/  
  if (window.getSelection) {
    txt = window.getSelection();
    } 
  else if (document.getSelection) {
    txt = document.getSelection();
    } 
  else if (document.selection) {
    txt = document.selection.createRange().text;
    }  

  var clickValue = findByNodeNameAndId(null,'input','clickvalue');
  clickValue.value = txt;
  
  var cMenu = findByNodeNameAndId(null,'div','contextMenu');
  cMenu.style.left = movX+'px';
  cMenu.style.top = movY+'px';
  showMenu(true);

  return true;
};

function showMenu(visible) {
  var cMenu = findByNodeNameAndId(null,'div','contextMenu');
  if (!visible) {
    cMenu.style.display = 'none';
    }
  else {  
    cMenu.style.display = 'inline';
    }
  return true;
};

function processMenuItem(menuItem) {
  var clickName = findByNodeNameAndId(null,'select','clickname');
  clickName.value = menuItem;

  showMenu(false);
  
  clickName.onchange();
  }
  
function process(idName,itemId) {
  if (idName == 'town') idName = 'lid';

  var clickName = findByNodeNameAndId(null,'select','clickname');
//  clickName.value = idName;

  var clickValue = findByNodeNameAndId(clickName.parentNode,'input','clickvalue');
  var item = findByNodeNameAndId(null,'a',itemId);
  clickValue.value = item.childNodes.item(0).nodeValue;

  clickName.value = idName;
  clickName.onchange();
};

function enableTab(tabIdx) {
  for (var i=1; i < 5; i++) {
    var tab = findByNodeNameAndId(null,'tr','edittab'+i);
    var tabhead = findByNodeNameAndId(null,'td','edittabhead'+i);
    if (i == tabIdx) {
      tab.className = 'edittrvisible';
      tabhead.className = 'editactivetab';
      }
    else {
      tab.className = 'edittrhidden';
      tabhead.className = 'editinactivetab';
      }
    } 
};
