/*
 * 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 checkPassField(field, encfield) {
  if (field.value.trim().length < 6) {
    alert("Das eingegebene Passwort ist zu kurz.");
		field.value = "";
		field.focus();
		return false;
	} else if (field.value.trim().length > 12) {
		alert("Das eingegebene Passwort ist zu kurz.");
		field.value = "";
		field.focus();
		return false;
	}
	return true;
}

function checkPassFrm(frm) {
  if (typeof frm.actpass != 'undefined') { 
	  if (!checkPassField(frm.actpass)) {
		  return false;
		}
	}
	if (!checkPassField(frm.newpass1) || !checkPassField(frm.newpass2)) {
	  return false;
	} else if (frm.newpass1.value.trim() != frm.newpass2.value.trim()) {
		alert("Die Passwörter stimmen nicht überein.");
		frm.newpass1.value = "";
		frm.newpass2.value = "";
		frm.newpass1.focus();
		return false;
	}
  if (typeof frm.actpass != 'undefined') { 
	  sha1(frm.actpass, frm.encactpass);
	}
	sha1(frm.newpass1, frm.encnewpass1);
	sha1(frm.newpass2, frm.encnewpass2);
	frm.jsactive.value = 1; 
  return true;
}

