//reset a form with security check
function resetFunction () {
  var chk = window.confirm("Wollen Sie alle Eingaben l&ouml;schen?");
  return (chk);
}
function encode_utf8(rohtext) {
 // dient der Normalisierung des Zeilenumbruchs
   rohtext = rohtext.replace(/\r\n/g,"\n");
   var utftext = "";
   for(var n=0; n<rohtext.length; n++)
       {
       // ermitteln des Unicodes des  aktuellen Zeichens
       var c=rohtext.charCodeAt(n);
       // alle Zeichen von 0-127 => 1byte
       if (c<128)
           utftext += String.fromCharCode(c);
       // alle Zeichen von 127 bis 2047 => 2byte
       else if((c>127) && (c<2048)) {
           utftext += String.fromCharCode((c>>6)|192);
           utftext += String.fromCharCode((c&63)|128);}
       // alle Zeichen von 2048 bis 66536 => 3byte
       else {
           utftext += String.fromCharCode((c>>12)|224);
           utftext += String.fromCharCode(((c>>6)&63)|128);
           utftext += String.fromCharCode((c&63)|128);}
       }
   return utftext;
}
function decode_utf8(utftext) {
   var plaintext = ""; var i=0; var c=c1=c2=0;
   // while-Schleife, weil einige Zeichen uebersprungen werden
   while(i<utftext.length)
       {
       c = utftext.charCodeAt(i);
       if (c<128) {
          plaintext += String.fromCharCode(c);
           i++;}
       else if((c>191) && (c<224)) {
           c2 = utftext.charCodeAt(i+1);
           plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
           i+=2;}
       else {
           c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
          plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
           i+=3;}
       }
   return plaintext;
}
//focus an element by id
function focus(id){
	var obj_focus = document.getElementById(id);	
	if(obj_focus){
		obj_focus.focus();
	}
}
function cleanValue(obj){
  obj.value = "";
}
//open a new page by clicking the link
function popuplink(file) { 								    
   window.open(file,'popup','toolbar=yes,width=800px, height=600px,resizable=yes,top=40,scrollbars=yes');
   return false;
}
//ask for confirmation the action
function userConfirm(message){
 return window.confirm(message);
}
//return current timestamp in form: YYYY-mm-dd
function getCurrentdate(){
	var date=new Date(); 
	var dd = ((date.getDate()  < 10) ? "0" : "") + date.getDate();
	var mm = ((date.getMonth() < 10) ? "0" : "") + (date.getMonth() + 1);
	var yy = date. getFullYear();
	return yy + '-' + mm + '-' + dd;
}
// hidde and show an element by clicking
function ShowHide(id) {
    var obj = document.getElementById(id);
    if(obj.style.visibility){
      obj.style.visibility = (obj.style.visibility == 'hidden' ? 'visible' : 'hidden');
    }else{
      obj.style.visibility = 'visible';
    }
}
function displayHide(id) {
    var obj = document.getElementById(id);
    obj.style.display = (obj.style.display == 'none' ? 'block' : 'none');
}
//delete all empty chars from start and end of the string
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
//if focus not an input field block enter key else not
function blockEnter(event){	
  var f=document.getElementById("basicForm");  
  if(f.addEventListener)
  {  
    f.addEventListener("focus",cancel,true);
    if(event.target.nodeName.toLowerCase() == "input"){
    	return false;
    }else{
    	return true;
    }
  }
  else if(f.attachEvent)
  {
    for(i=0;i<f.childNodes.length;i++){
    	if(event.srcElement.nodeName.toLowerCase() == "input"){    	
    		return false;
    	}else{
    		return true;
    	}
    }
  }
}
function cancel(event)
{
  return false;
}
//return the keycode of pressed key		
function onKeyPress(e){
  var keyCode = window.event ? e.keyCode : e.which;
  if(keyCode == 13){
    return blockEnter(e);
  }else{
  	return keyCode;
  }
}
//check e-mail
function checkEmail(email) {
	var proto  = "(mailto:)?";
	var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
	var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
	var regex  = "^" + proto + "?" + usr + "\@" + domain + "$";
	
	var rgx    = new RegExp(regex);
	return rgx.exec(email) ? true : false;
}
function validateEmail(emailId){
	var email = document.getElementById(emailId).value;
	if(!checkEmail(email)){
		alert('Diese e-Mail Adresse ist leider ungültig!');
		return false;
	}
	return true;
}
