/*
 * Remove leading and following spaces from strings
 */
String.prototype.trim=function(){
  var r=/^\s+|\s+$/,
  a=this.split(/\n/g),
  i=a.length;
  while(i-- > 0)
    a[i]=a[i].replace(r,'');
  return a.join('\n');
}
/*
 * Convert ISO-Chars to UTF-8
 */
 
function utf8_encode(isotext) {
	 isotext = isotext.replace(/\r\n/g,"\n");
	 var utf8text=[];
	 for(var n=0; n<isotext.length; n++) {
			 var chr=isotext.charCodeAt(n);
			 if (chr<128) {
					 utf8text[utf8text.length]= String.fromCharCode(chr);
	     } else if((chr>127) && (chr<2048)) {
					 utf8text[utf8text.length]= String.fromCharCode((chr>>6)|192);
					 utf8text[utf8text.length]= String.fromCharCode((chr&63)|128);
			 } else {
					 utf8text[utf8text.length]= String.fromCharCode((chr>>12)|224);
					 utf8text[utf8text.length]= String.fromCharCode(((chr>>6)&63)|128);
					 utf8text[utf8text.length]= String.fromCharCode((chr&63)|128);
			 }
	 }
	 return utf8text.join('');
}


/*
 * Encode visible field in SHA1-hidden Field and delete visible field.
 */
function sha1(srcfield, destfield) {
   destfield.value = hex_sha1(utf8_encode(srcfield.value));
   srcfield.value = "";
}
/* Check Data before Login, encode Pass (always) */

function checkLoginFrm(frm) {
  if (frm.name_key.value.trim().length < 3) {
      alert("Bitte geben Sie Ihren Benutzernamen oder einen Schlüssel ein."); 
      frm.name_key.focus();
      return false;
  } else if (!parseInt(frm.name_key.value.trim())) {
		 if (frm.pass.value.trim().length < 6) {
        alert("Das eingegebene Passwort ist zu kurz.");
        frm.pass.value = "";
        frm.pass.focus();
        return false;
     } else if (frm.pass.value.trim().length > 12) {
       alert("Das eingegebene Passwort ist zu lang."); 
       frm.pass.value = "";
       frm.pass.focus();
       return false;
     } else {
       sha1(frm.pass, frm.encpass);
       frm.pass.value = "";
    }
	}
	frm.jsactive.value = 1; //unbedingt ganz zuletzt setzen!
  return true;
}
