//reset a form with security check
function resetFunction () {
  var chk = window.confirm("Wollen Sie alle Eingaben loeschen?");
  return chk;
}
//focus an element by id
function focus(id){
	var obj_focus = document.getElementById(id);
	if(obj_focus){
		obj_focus.focus();
	}
}
//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;
}
function filInput(id){
  document.getElementById('newsImage').value = document.getElementById(id).innerHTML;
}
//ask for confirmation the action
function userConfirm(message){
 return window.confirm(message);
}
function displayHide(id , obj_sender) {
    var obj = document.getElementById(id);
    if(obj.style.display){
    	obj.style.display = (obj.style.display == 'none' ? 'block' : 'none');
    }else{
   		if(obj_sender.className == 'open' || obj_sender.className == 'newsYear'){
   			obj.style.display = 'none';
   		}else{
   			obj.style.display = 'block';
   		}
    }
}
function changeClassName(obj){
	 obj.className = (obj.className == 'closed')? 'open' : 'closed'
}
//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;
}
// generate a random Password
function generatePassword(id) {
	var string_length = 8;
	var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var lower = "abcdefghijklmnopqrstuvwxyz";
	var num = "0123456789";
	var special = "!§$%&/=?*#+-";

	var chars = upper + lower + num + special;
	var all = new Array(upper, lower, num, special);

	var randomstring = new Array();

	var j = 0;
	var tmp = '';

	// add one of each types of digits
	for(i = 0; i < all.length; i++){
		j = Math.floor(Math.random() * all[i].length);
		randomstring.push(all[i].substring(j, j + 1));
	}

	// fill with totally random digits till string_length is reached
	while(randomstring.length < string_length){
		j = Math.floor(Math.random() * chars.length);
		randomstring.push(chars.substring(j, j + 1));
	}

	// shuffle result
	for (i = 0; i < randomstring.length; i++){
		j = Math.round(Math.random() * (randomstring.length - i - 1)) + i;
		tmp = randomstring[i];
		randomstring[i] = randomstring[j];
		randomstring[j] = tmp;
	}

	document.getElementById(id).value = randomstring.join("");
}
//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;
}
function isFirstNews(id){
	return (document.getElementById(id).checked)? 1 : 0;
}