var EditorWindow;
var isIE = (navigator.appName.toLowerCase().indexOf("microsoft") != -1);
var isNS = (navigator.appName.toLowerCase().indexOf("netscape") != -1);
var isApple = (navigator.vendor && navigator.vendor.toLowerCase().indexOf("apple") != -1);
var isOpera = (navigator.userAgent && navigator.userAgent.toLowerCase().indexOf("opera") != -1);

var fullPage = false;
var inhalt="";
var adapted = false;

var scrollF = null;
var scrollC = null;
var scrollB = null;
var scrollH = 0;
var scrollK = 0;
var scrollP = 0;

var dObj = new Array;
var dBas = new Array;
var dAct = new Array;
var dPos = -1;
var ldPos = -1;
var diffW = 0;
var diffH = 0;
var wFaktor = 0;

var heute = new Date();
var tt;
var mm;
var jj;
var iDate = dateToString(heute);
var sTT=extractTT(iDate);
var sMM=extractMM(iDate);
var sJJ=extractJJ(iDate);

var title1="";
var title2="";
var pFrom ="";
var pTill ="";
var line=new Array();
var stack = new Array();
var ajaxRequest;
var ajaxTarget;
var obj;
var frameNo=0;
var waiting=false;
var protect = false;

var DayTitlesLg = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"];
var DayClasses  = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"];
var DayTitles   = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"];
var MonTitles   = ["Januar", "Februar", "März", "April", "Mai", "Juni",
"Juli", "August", "September", "Oktober", "November", "Dezember"];
var MonShTitles = ["Jan", "Feb", "Mrz", "Apr", "Mai", "Jun",
"Jul", "Aug", "Sep", "Okt", "Nov", "Dez"];
var MoTgNormalJ = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var MoTgSchaltJ = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

window.onerror = handleError;
if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;
document.onmouseup = dragstop;

function handleError(nachricht, adresse, zeile) {
  msgLine('<p style="color: red;">'+nachricht+'</p>');
  return true;
}

function presets() {
  if (document.getElementById("alles")) document.getElementById("alles").style.visibility = "visible";
  if (document.forms && document.forms[0]) placeCursor(document.forms[0]);
}

function placeCursor(formular) {
  for (i=0; i<9999; i++) {
    if (formular.elements && formular.elements[i]) {
      var el = formular.elements[i];
      if (el.type) {
        if (el.type == "checkbox") { 
          el.focus(); return;
        }
        if (el.type == "radio") { 
          el.focus(); return;
        }
        if (el.type == "textarea") { 
          el.focus(); return;
        }
        if (el.type == "select-one") { 
          el.focus(); return;
        }
        if (el.type == "select-multiple") { 
          el.focus(); return;
        }
        if (el.type == "text") { 
          el.focus(); return;
        }
      }
    }
  }
}

function adapt() {
  newWidth = getBreite();
  newHeight = getHoehe();
  if ((typeof(minWidth) != "undefined") && (minWidth > newWidth)) newWidth = minWidth;
  if ((typeof(minHeight) != "undefined") && (minHeight > newHeight)) newHeight = minHeight;
  if ((typeof(maxWidth) != "undefined") && (maxWidth < newWidth)) newWidth = maxWidth;
  if ((typeof(maxHeight) != "undefined") && (maxHeight < newHeight)) newHeight = maxHeight;
  diffW = newWidth - resizeWidth;
  diffH = newHeight - resizeHeight;
  resizeObjects(diffW,diffH);
  adapted = true;
}

function getDim(id, dim) {
  if (dim == "width") {
    if(document.getElementById(id).clientWidth) return parseFloat(document.getElementById(id).clientWidth);
    else if(document.getElementById(id).currentStyle) return parseFloat(document.getElementById(id).currentStyle.width);
    else if(window.getComputedStyle) return parseFloat(window.getComputedStyle(document.getElementById(id), null).getPropertyValue(dim));
  } else {
    if(document.getElementById(id).clientWidth) return parseFloat(document.getElementById(id).clientHeight);
    else if(document.getElementById(id).currentStyle) return parseFloat(document.getElementById(id).currentStyle.height);
    else if(window.getComputedStyle) return parseFloat(window.getComputedStyle(document.getElementById(id), null).getPropertyValue(dim));
  }
  return 0;
}

function getHoehe() {
  if (! isIE) { 
    return window.innerHeight;
  }
  else if (document.body && window.document.documentElement && window.document.documentElement.clientHeight) { 
    return window.document.documentElement.clientHeight;
  }
  else if (document.body && window.document.body.clientHeight) { 
    return window.document.body.clientHeight;
  }
  else { 
    return 0;
  }
}
function getBreite() {
  if (! isIE) { 
    return window.innerWidth;
  }
  else if (document.body && document.body.offsetWidth) { 
    return document.body.offsetWidth;
  }
  else { 
    return 0;
  }
}

function openEditor(page) {
  var wParms = "resizable=yes,"
  + ( isIE ? "width" : "innerWidth" ) + "=950,"
  + ( isIE ? "height" : "innerHeight" ) + "=500,"
  + "left=50,top=50";
  if (! isApple && EditorWindow && EditorWindow.close) EditorWindow.close();
  EditorWindow = window.open("/1x1TIME-1.01/xhtml/Editor.xhtml?editPage="+page,
    "EditorWindow", wParms);
  EditorWindow.focus();
}

function closeEditor() {
  EditorWindow.close();
  location.reload();
}

function checkScrolling() {
  var divs = document.getElementsByTagName("div");
  for (i=0; i<divs.length; i++) {
    var test = divs[i];
    if (test.className == "scrollBar") {
      if (visible(test)) {
        var cont = test.parentNode.firstChild;
        if (cont.className != "scrollCont") cont = cont.nextSibling;
        test.style.visibility = scrollable(cont) ? "visible" : "hidden";
      }
    }
  }
}
function visible(element) {
  while (element.parentNode) {
    var style = element.style.cssText;
    if ((style.indexOf ("display: none;") != -1) || (style.indexOf ("visibility: hidden;") != -1)) {
      return false;
    }
    element = element.parentNode;
  }
  return true;
}
function scrollable(element) {
  return (parseFloat(element.parentNode.style.height) < parseFloat(element.scrollHeight));
}

function setScrollObjects(element) {
  if (element.className == "scrollBar") {
    scrollB = element;
    scrollC = element.previousSibling;
    if (scrollC.className != "scrollCont") scrollC = scrollC.previousSibling;
  } else if (element.className == "scrollCont") {
    scrollC = element;
    scrollB = element.nextSibling;
    if (scrollB.className != "scrollBar") scrollB = scrollB.nextSibling;
  } else {
    scrollC = null;
    return;
  }
  scrollF = element.parentNode;
  scrollH = parseFloat(scrollF.style.height) - parseFloat(scrollC.scrollHeight);
  scrollP = parseFloat(scrollF.style.height) - parseFloat(scrollB.style.height);
}

function wheel(event){
  if (scrollC == null) return;
  if (! scrollable(scrollC)) return;
  var delta = 0;
  if (!event) event = window.event;
  if (!event) event = window.frames[frameNo].window.event;
  if (event.wheelDelta) {
    delta = -event.wheelDelta/120;
    if (isNS) delta = delta/3;
  } else if (event.detail) {
    delta = event.detail/3;
  }
  scrollToPos(delta * 16);
}

function dragstart(element) {
  setScrollObjects(element);
  msg("TOP = "+getOffset(element, "top"));
  scrollK = getOffset(element, "top") + parseFloat(scrollB.style.height) / 2;
  document.body.style.cursor = "pointer";
  protect = true;
  document.onmouseup = dragstop;
  document.onmousemove = drag;
}

function dragstop() {
  protect = false;
  document.onmousemove = null;
  document.body.style.cursor = "default";
}


function drag(ereignis) {
  var delta = (document.all ? window.event.clientY : ereignis.pageY) - scrollK;
  if(window.getSelection) window.getSelection().removeAllRanges();
  scrollK = scrollK + delta;
  delta = delta * scrollH / scrollP * -1;
  scrollToPos(delta);
}

function getOffset(element, dim) {
  obj = element;
  var l = t = 0;
  if (obj.offsetParent) {
    do {
      l += obj.offsetLeft;
      t += obj.offsetTop;
    } while ((obj = obj.offsetParent));
    return (dim == "top" ? t : l);
  }
  return 0;
}

function scrollToPos(delta) {
  tn = parseFloat(scrollC.style.top);
  if (isNaN(tn)) tn = 0;
  tn = tn - delta;
  if (tn > 0) tn = 0;
  if (tn < scrollH) tn = scrollH ;
  scrollC.style.top = tn + "px";
  tn = (tn / scrollH) * scrollP;
  scrollB.style.top = tn + "px";
}

function contentHeight(pObj) {
  sObj = null;
  if (isIE)         sObj = pObj.parentNode.previousSibling.firstChild.firstChild;
  else if (isOpera) sObj = pObj.parentNode.previousSibling.firstChild.nextSibling.firstChild.nextSibling;
  else if (pObj.parentNode.previousSibling.previousSibling.firstChild.nextSibling.firstChild)
    sObj = pObj.parentNode.previousSibling.previousSibling.firstChild.nextSibling.firstChild.nextSibling;
  if (sObj != null) return sObj.scrollHeight;
  checkScrollFrame(pObj);
  try {
    if (window.frames[frameNo].document.body.scrollHeight)
      return window.frames[frameNo].document.body.scrollHeight;
    else
      return window.frames[frameNo].document.height;
  } catch (error) {
  }
  return 0;
}


function submit(what, whatelse) {
  if(waiting) {
    alert('You have a open request to the server,\nthat is not completed yet.\n\n\Please wait for the answer,\nbefore clicking again.');
  } else {
    waiting=true;
    if (whatelse) {
      for (i=0; i< whatelse.elements.length; i++) {
        var obj = whatelse.elements[i];
        if ((obj.type == 'text') && ! obj.readOnly && ! obj.disabled) {
          if (obj.value == '') what.submit();
          else whatelse.submit();
          return false;
        }
      }
      what.submit();
    } else {
      what.submit();
    }
    popUpHide();
  }
  return false;
}

function filter(what) {
  if(waiting) {
    alert('You have a open request to the server,\nthat is not completed yet.\n\n\Please wait for the answer,\nbefore clicking again.');
  } else {
    var p = lastPageRechts.indexOf('&');
    if (p != -1) lastPageRechts = lastPageRechts.substr(0, p);
    for (i=0; i<9999; i++) {
      if (what.elements[i]) {
        var el = what.elements[i];
        if (el.type == 'text') lastPageRechts = lastPageRechts + '&' + el.name + "=" + el.value;
      } else {
        popUpHide();
        document.getElementById("mainFrame").src=lastPageRechts;
        waiting=true;
        document.getElementById("bar").className = "aBar";
        return;
      }
    }
  }
}

function menuLink(seite, attr) {
  if (attr == 'create') popUp(seite);
  if (attr == 'main') rechts(seite);
}

function links(seite) {
  if(waiting) {
    msgLine("loading - please wait ...");
  } else {
    waiting=true;
    msgLine("loading ...");
    document.getElementById("bar").className = "aBar";
    document.getElementById("frame"+activeBlock).src=seite;
  }
}

function rechts(seite) {
  if(waiting) {
    msgLine("loading - please wait ...");
  } else {
    waiting=true;
    msgLine("loading ...");
    lastPageRechts = seite;
    document.getElementById("mainFrame").src=seite;
  }
}

function popUp(seite) {
  if(waiting) {
    msgLine("loading - please wait ...");
  } else {
    waiting=true;
    document.getElementById("greyed").style.visibility = "visible";
    document.getElementById("popUpFrame").src=seite;
    document.getElementById("popUpDiv").style.visibility = "visible";
  }
}

function searchKey(seite) {
  if(waiting) {
    msgLine("loading - please wait ...");
  } else {
    waiting=true;
    if (document.getElementById("searchDiv").style.display == "none")
      document.getElementById("searchDiv").style.display = "block";
    document.getElementById("greyed").style.visibility = "visible";
    document.getElementById("searchFrame").src=seite;
    document.getElementById("searchDiv").style.visibility = "visible";
  }
}

function loaded(what) {
  msgLine("");
  if ((what == null) || (what.src == '') || ! what.id) return;
  waiting = false;
}

function popUpHide() {
  document.getElementById("popUpDiv").style.visibility = "hidden";
  document.getElementById("greyed").style.visibility = "hidden";
}

function searchHide() {
  if (isIE) document.getElementById("searchDiv").style.display = "none";
  else  document.getElementById("searchDiv").style.visibility = "hidden";
  if (document.getElementById("popUpDiv").style.visibility == "hidden")
    document.getElementById("greyed").style.visibility = "hidden";
}

function encode() {
  str = document.standard.usprf.value+"*"+document.standard.rawpw.value;
  document.standard.uspwd.value = MD5(str);
  document.standard.rawpw.value = "";
}

function sendLogin() {
  encode();
  document.standard.submit();
}


function setOpacity (element, value) {
  if (isIE) {
    opacity = Math.round( value * 100 ) ;
    element.style.filter = ( opacity > 100 ? '' : 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')' );
  } else element.style.opacity = value;
}

function setTitle(t1, t2, hist) {
  title1 = t1;
  title2 = ((t2=="*date") ? t2=iDate : ((t2=="*period") ? pFrom + "-" + pTill : t2));
  if (document.getElementById("title1")) document.getElementById("title1").innerHTML = title1;
  if (document.getElementById("title2")) document.getElementById("title2").innerHTML = title2;
  helms = hist.split("|");
  htext = "";
  for (h=0; h<helms.length; h++) {
    htext += ((h==0) ? '' : ' > ')
    + ((h==helms.length-1)
      ? '<span class="hactual">'
      : '<a class="blinks" href="/1x1TIME-1.01' + helms[h].split(";")[1] + '">')
    + helms[h].split(";")[0]
    + ((h==helms.length-1) ? '</span>' : '</a>')
  }
  if (document.getElementById("blinks")) document.getElementById("blinks").innerHTML = htext;
}

function msg(text) {
  var m = '' + text;
  window.status = m;
}

function msgLine(text) {
  if (document.getElementById("msgLine")) document.getElementById("msgLine").innerHTML = text;
}

function submitAJAX(link) {
  var lnk = "/1x1TIME-1.01" + link;
  lnk += ((lnk.indexOf("?") == -1) ? "?" : "&") + "fkt=submitAJAXresponse";
  lnk += ((lnk.indexOf("?") == -1) ? "?" : "&") + "ajax=true";
  loadXMLDoc(lnk);
}

function submitAJAXresponse(ajaxResponse) {
  type   = ajaxResponse.getElementsByTagName('type')[0].firstChild.data;
  result = ajaxResponse.getElementsByTagName('result')[0];
  if (type != 'ERR') {
    text = getContent(result);
    document.getElementById("msgLine").innerHTML = text;
  }
  else alert("error occured loading exchangeFrame");
}

function loadXMLDoc(url) {
  if (window.XMLHttpRequest) {
    stamp = new Date();
    ajaxRequest = new XMLHttpRequest();
    ajaxRequest.onreadystatechange = processReqChange;
    ajaxRequest.open("GET", url + (url.indexOf("?") == -1 ? "?" : "&") + "timestamp="+stamp, true);
    ajaxRequest.send(null);
  } else if (window.ActiveXObject) {
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    if (ajaxRequest) {
      ajaxRequest.onreadystatechange = processReqChange;
      ajaxRequest.open("GET", url, true);
      ajaxRequest.send();
    }
  }
}

function processReqChange() {
  if (ajaxRequest.readyState == 4) {
    if ((ajaxRequest.status == 200) && ajaxRequest.responseXML) {
      ajaxResponse = ajaxRequest.responseXML.documentElement;
      method = ajaxResponse.getElementsByTagName('method')[0].firstChild.data;
      eval(method+'(ajaxResponse);');
    } else {
      msg("There was a problem retrieving the XML data:\n" + ajaxRequest.statusText);
    }
  }
}

function changeMenu(rowTag, index) {
  titleNode = document.getElementById('LN'+line[index]);
  titleNode.style.color='#656565';
  document.getElementById('MB'+line[index]).style.display='none';
  line[index] = rowTag.id.substring(2);
  markMenuActive(index);
}

function markMenuActive(index) {
  ln = 0; if(! line[index]) line[index] = index + 1;
  while (!document.getElementById('LN'+line[index])) {
    ln++; line[index] = index + ln;
  }
  newStyle = isIE ? "block" : "table-row";
  titleNode = document.getElementById('LN'+line[index]);
  link = "/1x1TIME-1.01" + document.getElementById('LN'+line[index]).title;
  link += ((link.indexOf("?") == -1) ? "?" : "&") + "fkt=setLoadedContent";
  link += ((link.indexOf("?") == -1) ? "?" : "&") + "ajax=true";
  titleNode.style.color='#3399ff';
  document.getElementById('MB'+line[index]).style.display=newStyle;
  if (ajaxRequest && ajaxRequest != null) {
    stack.push("FR"+line[index] + ";" + link);
  } else {
    ajaxTarget = "FR"+line[index];
    loadXMLDoc(link);
  }
}

function setLoadedContent(ajaxResponse) {
  type   = ajaxResponse.getElementsByTagName('type')[0].firstChild.data;
  result = ajaxResponse.getElementsByTagName('result')[0];
  if (type != 'ERR') {
    text = getContent(result);
    document.getElementById(ajaxTarget).innerHTML=text;
    execScripts(document.getElementById(ajaxTarget));
    checkScrolling();
  }
  else msg("error occured loading exchangeFrame");
  if (stack.length > 0) {
    stackitem = stack.pop();
    ajaxTarget = stackitem.split(";")[0];
    link = stackitem.split(";")[1];
    loadXMLDoc(link);
  } else ajaxRequest = null;
}

function execScripts(node) {
  var fn = firstChild(node);
  while (fn != null) {
    if ((fn.nodeName == "SCRIPT") || (fn.nodeName == "script")) {
      if (fn.data) script = fn.data;
      else script = fn.text;
      eval(script);
    } else if (fn.nodeType == 1) {
      execScripts(fn);
    }
    fn = nextChild(fn);
  }
}

function getContent(xmlTag) {
  if(xmlTag.firstChild.textContent && xmlTag.normalize) {
    xmlTag.normalize(xmlTag.firstChild);
    content=xmlTag.firstChild.textContent;
  } else if(xmlTag.firstChild.nodeValue) {
    content=xmlTag.firstChild.nodeValue;
  } else {
    content=null;
  }
  return content;
}

function sortButton(fld,sFld) {
  var isDesc = ( sFld.indexOf('desc') != -1);
  if (isDesc) sFld = sFld.substring(0,sFld.length - 5);
  var isSel = (sFld == fld);
  document.write('<a href="javascript: changeSort(\'' + fld + '\', \'' + isSel + '\', \'' + isDesc + '\');">'
    + '<img src="/atpSQL-3.01/images/16x16/'+(isSel ? (isDesc ? 'SortUp' : 'SortDown') : 'sort')+'.gif"/></a>');
}

function changeSort(fld, se, de) {
  document.suche.sFld.value = fld + ((se == 'true') ? ((de != 'true') ? ' desc' : '' ) : '');
  document.suche.submit();
}

function findNode(node, path) {
  if (node == null) return null;
  tfld = "id";
  wp = path;
  if (wp.substring(0,1) == "/") wp = wp.substring(1);
  ends = wp.indexOf("/");
  wr = (ends == -1) ? "" : wp.substring(ends);
  wp = (ends == -1) ? wp : wp.substring(0,ends);
  wn = node;
  idx = "1";
  isID = false;
  if (wp.indexOf("[") != -1) {
    idx = wp.substring(wp.indexOf("[") + 1, wp.length() - 1);
    if (wp.indexOf(".") != -1) {
      tfld = wp.substring(wp.indexOf(".") + 1, wp.indexOf("["));
      wp = wp.substring(0, wp.indexOf("."));
    } else
      wp = wp.substring(0, wp.indexOf("["));
    isID = !(parseInt(idx) > 0);
  }
  cn = firstChild(wn);
  widx = isID ? "" : "0";
  if ((wp == "*") || ((cn != null) && (cn.nodeName == wp))) {
    widx = ((cn != null) && isID) ? eval('cn.'+tfld) : "1";
  }
  while ((cn != null) && (cn.nodeName != wp) && (wp != "*") && (widx != idx)) {
    cn = nextChild(cn);
  }
  if ((cn != null) && (cn.nodeName == wp)) {
    if (wr == "") return cn;
    else return findNode(cn, wr);
  }
  return null;
}
function firstChild(node) {
  if (node == null) return null;
  var erg = node.firstChild;
  while ((erg != null) && (erg.nodeType != 1))
    erg = erg.nextSibling;
  return erg;
}
function nextChild(node) {
  if (node == null) return null;
  var erg = node.nextSibling;
  while ((erg != null) && (erg.nodeType != 1))
    erg = erg.nextSibling;
  return erg;
}
function trim (str) {
  return str.replace (/^\s+/, '').replace (/\s+$/, '');
}

function pause(millis) {
  var lDate = new Date();
  var cDate = null;
  do {
    cDate = new Date();
  }
  while(cDate-lDate < millis);
  alert("wake up after "+millis);
}

function calShow() {
  document.getElementById("addCalTitle").innerHTML = MonTitles[mm-1] + " " + jj;
  document.getElementById("lJahr").innerHTML = '' + ( jj - 1) + '<img src="/1x1TIME-1.01/images/16x16/link.gif" style="vertical-align: top;" />';
  document.getElementById("nJahr").innerHTML = '' + ( jj + 1) + '<img src="/1x1TIME-1.01/images/16x16/link.gif" style="vertical-align: top;" />';
  if (document.getElementById("aDatum")) document.getElementById("aDatum").innerHTML = '' +  sTT + '.' + sMM + '.' + sJJ;
  if (document.getElementById("aDatumMA")) document.getElementById("aDatumMA").innerHTML = '1.' + sMM + '.' + sJJ;
  if (document.getElementById("aDatumME")) document.getElementById("aDatumME").innerHTML = anzTage(sMM, sJJ) +'.' + sMM + '.' + sJJ;
  if (document.getElementById("aDatumPA")) document.getElementById("aDatumPA").innerHTML = '1.1.' + sJJ;
  if (document.getElementById("aDatumPE")) document.getElementById("aDatumPE").innerHTML = '31.12.' + sJJ;
  if (document.getElementById("aDatumPA1")) document.getElementById("aDatumPA1").innerHTML = '1.' + sMM + '.' + sJJ;
  if (document.getElementById("aDatumPE1")) document.getElementById("aDatumPE1").innerHTML = '' +  sTT + '.' + sMM + '.' + sJJ;
  if (document.getElementById("aDatumPA2")) document.getElementById("aDatumPA2").innerHTML = '' +  sTT + '.' + sMM + '.' + sJJ;
  if (document.getElementById("aDatumPE2")) document.getElementById("aDatumPE2").innerHTML = '31.' + sMM + '.'  + sJJ;
  if (document.getElementById("aDatumIA")) document.getElementById("aDatumIA").innerHTML = pFrom;
  if (document.getElementById("aDatumIE")) document.getElementById("aDatumIE").innerHTML = pTill;
  var ersterTg = ersterTag(mm, jj);
  for (d=1; d<anzTage(mm,jj)+1; d++) {
    var y = intDiv(d+ersterTg-2, 7)+1;
    var x = d - 1 - (y-1) * 7 + ersterTg;
    var fName = "W"+y+"T"+x;
    var zName = "Z"+y+"T"+x;
    eval('document.getElementById("'+fName+'").innerHTML = "' + d + '"' );
    eval('document.getElementById("'+zName+'").className = "' + DayClasses[x-1] + '"');
    if (jj == heute.getFullYear() &&
      mm == heute.getMonth()+1 &&
      d  == heute.getDate())
      eval('document.getElementById("'+zName+'").className = "heute"');
    if (jj  == sJJ && mm  == sMM && d == sTT)
      eval('document.getElementById("'+zName+'").className = "inhalt"');
  }
}
function calShowHeute() {
  jj = heute.getFullYear();
  mm = heute.getMonth() + 1;
  tt = heute.getDate();
  var parms = '&aDatumMA=' + dateToString(getDate(1, mm, jj));
  parms += '&aDatumME=' + dateToString(getDate(anzTage(mm, jj), mm, jj));
  parms += '&aDatumJA=' + dateToString(getDate(1, 1, jj));
  parms += '&aDatumJE=' + dateToString(getDate(anzTage(12, jj), 12, jj));
  calRefresh();
}
function calChange(monat, jahr) {
  var tmp = adjustMonat(monat);
  mm = tmp[0];
  jj = tmp[1] + jahr;
  var anz = anzTage(mm, jj);
  var parms = '&aDatumMA=' + dateToString(getDate(1, mm, jj));
  parms += '&aDatumME=' + dateToString(getDate(anz, mm, jj));
  parms += '&aDatumJA=' + dateToString(getDate(1, 1, jj));
  parms += '&aDatumJE=' + dateToString(getDate(anzTage(12, jj), 12, jj));
  calRefresh();
}
function calSelect(y, x) {
  var fName = "W"+y+"T"+x;
  eval('tt = document.getElementById("'+fName+'").innerHTML' );
  sJJ = jj;
  sMM = mm;
  sTT = tt;
  aDatumN  = dateToString(getDate(tt, mm, jj));
  aDatumA = wochenAnfang(aDatumN);
  var parms = '&aDatum='+aDatumN+'&aDatumA='+aDatumA;
  parms += '&aDatumB=' + datumFrist(aDatumA, 1);
  parms += '&aDatumC=' + datumFrist(aDatumA, 2);
  parms += '&aDatumD=' + datumFrist(aDatumA, 3);
  parms += '&aDatumE=' + datumFrist(aDatumA, 4);
  parms += '&aDatumF=' + datumFrist(aDatumA, 5);
  parms += '&aDatumG=' + datumFrist(aDatumA, 6);
  parms += '&aDatumMA=' + dateToString(getDate(1, mm, jj));
  parms += '&aDatumME=' + dateToString(getDate(anzTage(mm, jj), mm, jj));
  parms += '&aDatumJA=' + dateToString(getDate(1, 1, jj));
  parms += '&aDatumJE=' + dateToString(getDate(anzTage(12, jj), 12, jj));
  calShow();
}

function calSetPeriod(what) {
  if (what == "from") pFrom = tt + '.' + mm + '.' + jj;
  else pTill = tt + '.' + mm + '.' + jj;
  document.getElementById("aDatumIA").innerHTML = pFrom;
  document.getElementById("aDatumIE").innerHTML = pTill;
}

function calRefresh() {
  for (y=1; y<7; y++) {
    for (x=1; x<8; x++) {
      var fName = "W"+y+"T"+x;
      var zName = "Z"+y+"T"+x;
      eval('document.getElementById("'+fName+'").innerHTML = ""' );
      eval('document.getElementById("'+zName+'").className = "noDisplay"');
    }
  }
  calShow();
}

function intDiv(divident, divisor) {
  var rest = divident % divisor;
  return ((divident - rest) / divisor);
}
function dateToString(Datum) {
  var sT = "" + Datum.getDate();
  var sM = "" + (Datum.getMonth()+1);
  var sJ = "" + Datum.getFullYear();
  if (sT.length < 2) sT = "0" + sT;
  if (sM.length < 2) sM = "0" + sM;
  return sJ + sM + sT;
}
function getDate(tag, monat, jahr) {
  var vDate = new Date();
  vDate.setDate(1);
  vDate.setMonth(monat-1);
  vDate.setFullYear(jahr);
  vDate.setDate(tag);
  return vDate;
}
function anzTage(monat, jahr) {
  if ((jahr % 4) == 0) {
    if ((jahr % 100) == 0 && (jahr % 400) != 0) return MoTgNormalJ[monat-1];
    return MoTgSchaltJ[monat-1];
  } else return MoTgNormalJ[monat-1];
}
function adjustMonat(wert) {
  var erg = new Array();
  erg[0] = mm + wert; erg[1] = jj;
  if (erg[0] < 1)  {
    erg[1]=erg[1]-1; erg[0]=12;
  }
  if (erg[0] > 12) {
    erg[1]++; erg[0]=1;
  }
  return erg;
}
function ersterTag(monat, jahr) {
  return wochenTag(1, monat, jahr);
}
function checkDate(datum, orgDatum) {
  var sDatum = "" + datum;
  var jj = extractJJ(datum);
  var mm = extractMM(datum);
  var tt = extractTT(datum);
  if ((jj == 0) || (mm == 0) || (tt == 0)) {
    if (! orgDatum == "") alert('Das Datum ->'+orgDatum+'<- ist ungültig.');
    return false;
  } else if (mm > 12) {
    if (! orgDatum == "") alert('Das Datum ->'+orgDatum+'<- ist ungültig.');
    return false;
  } else if (tt > anzTage(mm, jj)) {
    if (! orgDatum == "") alert('Das Datum ->'+orgDatum+'<- ist ungültig.');
    return false;
  }
  return true;
}
function extractTT(datum) {
  var sDatum = "" + datum;
  return parseFloat(sDatum.substr(6,2));
}
function extractMM(datum) {
  var sDatum = "" + datum;
  return parseFloat(sDatum.substr(4,2));
}
function extractJJ(datum) {
  var sDatum = "" + datum;
  return parseFloat(sDatum.substr(0,4));
}
function wochenAnfang(datum) {
  var wTg = wochenTag(extractTT(datum), extractMM(datum), extractJJ(datum));
  if (wTg == 1) return datum;
  return datumFrist(datum, (wTg -1) * -1);
}
function wochenTag(tag, monat, jahr) {
  var vDate = getDate(tag, monat, jahr);
  var tTag = vDate.getDay(); 
  if (tTag < 1) tTag = tTag + 7;
  return tTag;
}
function datumFrist(datum, offs) {
  var o = offs;
  var t = extractTT(datum); var m = extractMM(datum); var j = extractJJ(datum);
  while (o < 0) {
    t=t-1;
    if (t < 1) {
      m=m-1;
      if (m < 1) j=j-1;
      t = anzTage(m, j);
    }
    o++;
  }
  while (o > 0) {
    t++;
    if (t > anzTage(m, j)) {
      m++; t=1;
    }
    if (m > 12) {
      j++; m=1;
    }
    o=o-1;
  }
  return dateToString(getDate(t, m, j));
}

function array(n) {
  for(i=0;i<n;i++) this[i]=0;
  this.length=n;
}

function integer(n) {
  return n%(0xffffffff+1);
}

function shr(a,b) {
  a=integer(a);
  b=integer(b);
  if (a-0x80000000>=0) {
    a=a%0x80000000;
    a>>=b;
    a+=0x40000000>>(b-1);
  } else
    a>>=b;
  return a;
}

function shl1(a) {
  a=a%0x80000000;
  if (a&0x40000000==0x40000000)
  {
    a-=0x40000000;
    a*=2;
    a+=0x80000000;
  } else
    a*=2;
  return a;
}

function shl(a,b) {
  a=integer(a);
  b=integer(b);
  for (var i=0;i<b;i++) a=shl1(a);
  return a;
}

function and(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0)
    if (t2>=0)
      return ((t1&t2)+0x80000000);
    else
      return (t1&b);
  else
  if (t2>=0)
    return (a&t2);
  else
    return (a&b);
}

function or(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0)
    if (t2>=0)
      return ((t1|t2)+0x80000000);
    else
      return ((t1|b)+0x80000000);
  else
  if (t2>=0)
    return ((a|t2)+0x80000000);
  else
    return (a|b);
}

function xor(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0)
    if (t2>=0)
      return (t1^t2);
    else
      return ((t1^b)+0x80000000);
  else
  if (t2>=0)
    return ((a^t2)+0x80000000);
  else
    return (a^b);
}

function not(a) {
  a=integer(a);
  return (0xffffffff-a);
}

var state = new array(4);
var count = new array(2);
count[0] = 0;
count[1] = 0;
var buffer = new array(64);
var transformBuffer = new array(16);
var digestBits = new array(16);

var S11 = 7;
var S12 = 12;
var S13 = 17;
var S14 = 22;
var S21 = 5;
var S22 = 9;
var S23 = 14;
var S24 = 20;
var S31 = 4;
var S32 = 11;
var S33 = 16;
var S34 = 23;
var S41 = 6;
var S42 = 10;
var S43 = 15;
var S44 = 21;

function F(x,y,z) {
  return or(and(x,y),and(not(x),z));
}

function G(x,y,z) {
  return or(and(x,z),and(y,not(z)));
}

function H(x,y,z) {
  return xor(xor(x,y),z);
}

function I(x,y,z) {
  return xor(y ,or(x , not(z)));
}

function rotateLeft(a,n) {
  return or(shl(a, n),(shr(a,(32 - n))));
}

function FF(a,b,c,d,x,s,ac) {
  a = a+F(b, c, d) + x + ac;
  a = rotateLeft(a, s);
  a = a+b;
  return a;
}

function GG(a,b,c,d,x,s,ac) {
  a = a+G(b, c, d) +x + ac;
  a = rotateLeft(a, s);
  a = a+b;
  return a;
}

function HH(a,b,c,d,x,s,ac) {
  a = a+H(b, c, d) + x + ac;
  a = rotateLeft(a, s);
  a = a+b;
  return a;
}

function II(a,b,c,d,x,s,ac) {
  a = a+I(b, c, d) + x + ac;
  a = rotateLeft(a, s);
  a = a+b;
  return a;
}

function transform(buf,offset) {
  var a=0, b=0, c=0, d=0;
  var x = transformBuffer;

  a = state[0];
  b = state[1];
  c = state[2];
  d = state[3];

  for (i = 0; i < 16; i++) {
    x[i] = and(buf[i*4+offset],0xff);
    for (j = 1; j < 4; j++) {
      x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
    }
  }

  a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478);
  d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756);
  c = FF ( c, d, a, b, x[ 2], S13, 0x242070db);
  b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee);
  a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf);
  d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a);
  c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613);
  b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501);
  a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8);
  d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af);
  c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1);
  b = FF ( b, c, d, a, x[11], S14, 0x895cd7be);
  a = FF ( a, b, c, d, x[12], S11, 0x6b901122);
  d = FF ( d, a, b, c, x[13], S12, 0xfd987193);
  c = FF ( c, d, a, b, x[14], S13, 0xa679438e);
  b = FF ( b, c, d, a, x[15], S14, 0x49b40821);

  a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562);
  d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340);
  c = GG ( c, d, a, b, x[11], S23, 0x265e5a51);
  b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
  a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d);
  d = GG ( d, a, b, c, x[10], S22,  0x2441453);
  c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681);
  b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
  a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6);
  d = GG ( d, a, b, c, x[14], S22, 0xc33707d6);
  c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87);
  b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed);
  a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905);
  d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8);
  c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9);
  b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a);

  a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942);
  d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681);
  c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122);
  b = HH ( b, c, d, a, x[14], S34, 0xfde5380c);
  a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44);
  d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9);
  c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60);
  b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70);
  a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6);
  d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa);
  c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085);
  b = HH ( b, c, d, a, x[ 6], S34,  0x4881d05);
  a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039);
  d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5);
  c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8);
  b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665);

  a = II ( a, b, c, d, x[ 0], S41, 0xf4292244);
  d = II ( d, a, b, c, x[ 7], S42, 0x432aff97);
  c = II ( c, d, a, b, x[14], S43, 0xab9423a7);
  b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039);
  a = II ( a, b, c, d, x[12], S41, 0x655b59c3);
  d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92);
  c = II ( c, d, a, b, x[10], S43, 0xffeff47d);
  b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1);
  a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f);
  d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0);
  c = II ( c, d, a, b, x[ 6], S43, 0xa3014314);
  b = II ( b, c, d, a, x[13], S44, 0x4e0811a1);
  a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82);
  d = II ( d, a, b, c, x[11], S42, 0xbd3af235);
  c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
  b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391);

  state[0] +=a;
  state[1] +=b;
  state[2] +=c;
  state[3] +=d;

}
function init() {
  buffer = new array(64);
  count[0]=count[1] = 0;
  state[0] = 0x67452301;
  state[1] = 0xefcdab89;
  state[2] = 0x98badcfe;
  state[3] = 0x10325476;
  for (i = 0; i < digestBits.length; i++)
    digestBits[i] = 0;
}

function update(b) {
  var index,i;

  index = and(shr(count[0],3) , 0x3f);
  if (count[0]<0xffffffff-7)
    count[0] += 8;
  else {
    count[1]++;
    count[0]-=0xffffffff+1;
    count[0]+=8;
  }
  buffer[index] = and(b,0xff);
  if (index  >= 63) {
    transform(buffer, 0);
  }
}

function finish() {
  var bits = new array(8);
  var        padding;
  var        i=0, index=0, padLen=0;

  for (i = 0; i < 4; i++) {
    bits[i] = and(shr(count[0],(i * 8)), 0xff);
  }
  for (i = 0; i < 4; i++) {
    bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
  }
  index = and(shr(count[0], 3) ,0x3f);
  padLen = (index < 56) ? (56 - index) : (120 - index);
  padding = new array(64);
  padding[0] = 0x80;
  for (i=0;i<padLen;i++)
    update(padding[i]);
  for (i=0;i<8;i++)
    update(bits[i]);

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
    }
  }
}

function hexa(n) {
  var hexa_h = "0123456789abcdef";
  var hexa_c="";
  var hexa_m=n;
  for (hexa_i=0;hexa_i<8;hexa_i++) {
    hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
    hexa_m=Math.floor(hexa_m/16);
  }
  return hexa_c;
}


var ascii="01234567890123456789012345678901" +
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
"[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

function MD5(nachricht)
{
  var l,s,k,ka,kb,kc,kd;

  init();
  for (k=0;k<nachricht.length;k++) {
    l=nachricht.charAt(k);
    update(ascii.lastIndexOf(l));
  }
  finish();
  ka=kb=kc=kd=0;
  for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
  for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
  for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
  for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
  s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
  return s;
}




