  /***************************************************************************
    * SecureHash(s): Calculates the message digest for the given string using *
    * the SHA-1 algorithm.                                                    *
    **************************************************************************/

   function SecureHash(s) {
     var bitLen, n, w;
     var k, buffer, f;
     var h0, h1, h2, h3, h4;
     var a, b, c, d, e, temp;
     var i, j;

     // Pad the input message per the SHA-1 specification.

     bitLen = s.length * 8;
     n = 512 - (bitLen % 512);
     if (n <= 65)
       n += 512;
     s += String.fromCharCode(0x80);
     n -= 8;
     for (i = 0; i < n / 8; i++)
       s += String.fromCharCode(0x00);

     // Convert padded message to an array of 32-bit integers, note conversion
     // to little endian.

     w = new Array();
     for (i = 0; i < s.length; i += 4) {
       n = 0;
       for (j = 0; j < 4; j++)
         n = (n << 8) + s.charCodeAt(i + j);
       w[w.length] = n;
     }

     // Set last word to the original bit length of the message.

     w[w.length - 1] = bitLen;

     // Initialize hash values, constants and buffer.

     h0 = 0x67452301;
     h1 = 0xEFCDAB89;
     h2 = 0x98BADCFE;
     h3 = 0x10325476;
     h4 = 0xC3D2E1F0;

     k = new Array(80);
     for (i = 0; i < 80; i++)
      if (i < 20)
        k[i] = 0x5A827999;
      else if (i < 40)
        k[i] = 0x6ED9EBA1;
      else if (i < 60)
        k[i] = 0x8F1BBCDC;
      else
        k[i] = 0xCA62C1D6;

     buffer = new Array(80);

     // Process word array in 512-bit (16-word) blocks.

     n = w.length / 16;
     for (i = 0; i < n; i ++) {

       // Initialize 80-word buffer using the current block.

       for (j = 0; j < 80; j++)
         if (j < 16)
           buffer[j] = w[i * 16 + j];
         else
           buffer[j] = rol(buffer[j - 3] ^ buffer[j - 8] ^ buffer[j - 14] ^
                           buffer[j - 16], 1);

       a = h0; b = h1; c = h2; d = h3; e = h4;

       // Hash the block.

       for (j = 0; j < 80; j++) {
         temp = rol(a, 5);
         if (j < 20)
           f = (b & c) | ((~b) & d);
         else if (j < 40)
           f = b ^ c ^ d;
         else if (j < 60)
           f = (b & c) | (b & d) | (c & d);
         else
           f = b ^ c ^ d;
         temp = add(temp, f);
         temp = add(temp, e);
         temp = add(temp, buffer[j]);
         temp = add(temp, k[j]);
         e = d; d = c; c = rol(b, 30); b = a; a = temp;
       }

       // Update hash values.

       h0 = add(h0, a);
       h1 = add(h1, b);
       h2 = add(h2, c);
       h3 = add(h3, d);
       h4 = add(h4, e);
     }

     // Format hash values and return as message digest.

     return hex(h0) + hex(h1) + hex(h2) + hex(h3) + hex(h4);
   }

   // Helper functions for the Secure Hash function.

   function rol(x, n) {

     // Left circular shift for a 32-bit integer.

     return (x << n) | (x >>> (32 - n));
   }

   function add(x, y) {

     // Add two 32-bit integers, wrapping at 2^32.

     return ((x & 0x7FFFFFFF) + (y & 0x7FFFFFFF)) ^
             (x & 0x80000000) ^ (y & 0x80000000)
   }

   function hex(n) {

     var hexDigits = "0123456789ABCDEF";
     var i, s;

     // Format a 32-bit integer as a hexadecimal string.

     s = "";
     for (i = 7; i >= 0; i--)
       s += hexDigits.charAt((n >> (i * 4)) & 0x0F);

     return s;
   }

//function to validate by length
function ValidLength(item, len) 
{
   return (item.length >= len);
}

function error(elem, text) 
{
// abort if we already found an error
   if (errfound) return;
   errfound = true;
   window.alert(text);
   elem.select();
   elem.focus();
}

function SaveCookies() {
//  window.alert("Save Cookie");
  setCookie("coUsr", myForm.ctlUsr.value, "365");
  setCookie("coPWD", MyPWD, "365"); 
}

function setCookie(coName,coVal,nDays) { 
var today = new Date();
var expire = new Date();
//window.alert("Set cookie"+today.toGMTString());
 if (nDays==null) nDays=1;
 if (nDays==0) nDays=1;
// window.alert("ndays= " + nDays);
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = coName+"="+escape(coVal)+"; expires="+expire.toGMTString();
}


function CookiesEintragen() {
   	myForm = document.frmLogon;
	if (myForm.ctlUsr.value == "") {myForm.ctlUsr.value = getCookie("coUsr");}
	if (myForm.ctlPWD.value == "") {myForm.ctlPWD.value = getCookie("coPWD");}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf(prefix);
  if (begin == -1)
//    return null;
    return "";
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookies() {
// window.alert("Clear");
  deleteCookie("coUsr");
  deleteCookie("coPWD");
}

