/* Time-String in übliches Format umwandeln */
function FormTime(TimeStr) {
  var time = new Date(TimeStr);

  var Tag = time.getDate();
  if (Tag < 10) Tag = "0" + Tag;

  var Monat = time.getMonth() + 1;
  if (Monat < 10) Monat = "0" + Monat;

  var Stunde = time.getHours();
  if (Stunde < 10) Stunde = "0" + Stunde;

  var Minute = time.getMinutes();
  if (Minute < 10) Minute = "0" + Minute;

  var Sekunde = time.getSeconds();
  if (Sekunde < 10) Sekunde = "0" + Sekunde;

  var Jahr = time.getFullYear();

  return Tag + "." + Monat + "." + Jahr + ", " + Stunde + "." + Minute + "." + Sekunde;
}

/* Speicherdatum des Dokuments als Text liefern */
function getModified(mod) {
  if (mod != "") var lastmod = FormTime(mod);
  else var lastmod = FormTime(document.lastModified);

  return("Letzte Änderung: " + lastmod + " - ");
}

/* Speicherdatum des Dokuments schreiben */
function PrintModified(mod, mailTo) {
  document.writeln("<p>", getModified(mod), "<a href=\"mailto:" +
  mailTo + "\">" + mailTo + "</a></p>");
}

/* MailTo in Tag schreiben */
function setMailTo(user, domain, target)  {
  var text = "";
  var mailTo = user + "@" + domain;
  var node = document.getElementById(target);
  if (node.firstChild != null) {
    text = node.firstChild.nodeValue;
    node.removeChild(node.firstChild);
  }
  if (text == "") {
    text = mailTo;
  }
  var A = document.createElement("a");
  var AText = document.createTextNode(text);
  A.href = "mailto:" + mailTo;
  A.appendChild(AText);
  node.appendChild(A);
  return(false);
}

/* Speicherdatum des Dokuments in Tag schreiben */
function setModified(id, mod, user, domain) {
  var mailTo = user + "@" + domain;
  var node = document.getElementById(id);
  var nodeText = document.createTextNode(getModified(mod));
  var A = document.createElement("a");
  var AText = document.createTextNode(mailTo);
  A.href = "mailto:" + mailTo;
  A.appendChild(AText);
  node.appendChild(nodeText);
  node.appendChild(A);
  return(false);
}

/* Zwei Frames gleichzeitig ändern */
function LeftAndRight(Left, Right) {
  parent.frames['links'].location.href=Left;
  parent.frames['rechts'].location.href=Right;
}

/* Um einen Cookie zu löschen, ist die Lebensdauer auf -1 Tag zu setzen.
   Einen temporären Cookie erhält man mit 0 Tagen Lebensdauer. Die Pfadangabe
   ist optional. */
function SetCookie(Name, Value, LifeDays, Path) {
  var today = new Date();
  var expiry = new Date(today.getTime() + LifeDays * 24*60*60*1000);

  if (Name != null && Name != "" && Value != null) {
    var CookieString = Name + "=" + escape(Value);

    if (LifeDays) CookieString += "; expires=" + expiry.toGMTString();
    if (Path) CookieString += "; path=" + Path;
    document.cookie = CookieString;
  }
}

/* Parst den Cookiestring und liefert ein Array mit den einzelnen Cookies oder
   null zurück. */
function GetCookieArray() {
  if (!document.cookie || document.cookie == "") return null;

  var Cookies = document.cookie.split(";");
  var CookieArray = new Array();

  for (var i = 0; i < Cookies.length; i++) {
    var Cookie = Cookies[i].substring(Cookies[i].search(/\S/),
      Cookies[i].length).split("=");
    if (Cookie[1]) CookieArray[Cookie[0]] = unescape(Cookie[1]);
    else CookieArray[Cookie[0]] = "";
  }

  return CookieArray;
}

/* Liefert den Wert eines Cookies oder null. */
function GetCookie(Name) {
  var Cookies = new GetCookieArray();
  var Value = Cookies[Name];

  if (Value || Value == "") return Value;
  return null;
}

/* Zeigen, ob die Seite seit letztem Besuch geändert wurde */
function IsModifiedSinceLatterVisit(DocName, Days) {
  if (GetCookie(DocName) == null)
    SetCookie(DocName, document.lastModified, Days, "/");

  if (GetCookie(DocName) != document.lastModified) {
    alert("Diese Seite wurde seit Ihrem letzten Besuch ge&auml;ndert!");
    SetCookie(DocName, document.lastModified, Days, "/");
  }
}

/* Zeigen, ob die Seite seit letztem Besuch geändert wurde */
function IsDocModified() {
  return IsModifiedSinceLatterVisit(document.URL.substring(document.URL.lastIndexOf("/")
    + 1, document.URL.lastIndexOf(".")), 365);
}

/* Schreibt eine leere Zeichenkette in ein Element, wenn darin der Standardwert steht.*/
function valFocus(obj) {
  with (obj) if (value == defaultValue) value = '';
}

/* Schreibt den Standardwert in ein Element, wenn darin eine leere Zeichenkette steht.*/
function valBlur(obj) {
  with (obj) if (value == '') value = defaultValue;
}

/*
 * An URI datatype.  Based upon examples in RFC3986.
 *
 * TODO %-escaping
 * TODO split apart authority
 * TODO split apart query_string (on demand, anyway)
 *
 * @(#) $Id$
 *
 * copied from http://js-uri.googlecode.com/files/js-uri-0.1.zip
 * and modified
 *
 */

// Constructor for the URI object.  Parse a string into its components.
function URI(str) {
  if (!str) str = "";
  // Based on the regex in RFC2396 Appendix B.
  var parser = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;
  var result = str.match(parser);
  this.scheme    = result[1] || null;
  this.authority = result[2] || null;
  this.path      = result[3] || null;
  this.query     = result[4] || null;
  this.fragment  = result[5] || null;
  return(false);
}

// Restore the URI to it's stringy glory.
URI.prototype.toString = function() {
  var str = "";
  if (this.scheme) {
    str += this.scheme + ":";
  }
  if (this.authority) {
    str += "//" + this.authority;
  }
  if (this.path) {
    str += this.path;
  }
  if (this.query) {
    str += "?" + this.query;
  }
  if (this.fragment) {
    str += "#" + this.fragment;
  }
  return(str);
};

// Set default values
URI.prototype.defaults = function(defaultURI) {
  var deflt = new URI(defaultURI);
  if (!this.scheme) {
    this.scheme = deflt.scheme;
  }
  if (!this.authority) {
    this.authority = deflt.authority;
  }
  if (!this.path) {
    this.path = deflt.path;
  }
  if (!this.query) {
    this.query = deflt.query;
  }
  if (!this.fragment) {
    this.fragment = deflt.fragment;
  }
  return(false);
};

// Canonizes the path

URI.prototype.canonizePath = function() {
  if (this.path) {
    var aPath = this.path.split('/');
    var aPathCanon = new Array();
    for (var k in aPath) {
      if ((aPath[k] == '.') || (aPath[k] == '')) {
        continue;
      }
      if (aPath[k] == '..') {
        if (aPathCanon.length > 0) {
          aPathCanon.pop();
        }
      } else {
        aPathCanon.push(aPath[k]);
      }
    }
    this.path = '/' + aPathCanon.join('/');
  }
  return(this.path);
}

/* copied from http://www.11tmr.com/11tmr.nsf/D6Plinks/MWHE-695L9Z
   and modified */
function getURLParam(strParamName) {
  var objURI = new URI(location.href);
  var strReturn = '';
  if (objURI.query != null) {
    var strQueryString = objURI.query;
    if (strQueryString.length > 0){
      var aQueryString = strQueryString.split('&');
      for (var iParam = 0; iParam < aQueryString.length; iParam++ ) {
        if (aQueryString[iParam].indexOf(strParamName + '=') > -1) {
          var aParam = aQueryString[iParam].split('=');
          strReturn = aParam[1];
          break;
        }
      }
    }
  }
  return(unescape(strReturn));
}

