var nbsp = 160;
var node_text = 3;
var emptyString1 = /^\s*$/
var glb_vfld;

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};

function setFocusDelayed()
{
  glb_vfld.focus()
}

function setfocus(vfld)
{
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}

function msg(fld, msgtype, message) {
  var dispmessage;
  if (emptyString1.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;
  var elem = document.getElementById(fld);
  	elem.style.display = 'block';
  	elem.firstChild.nodeValue = dispmessage;
  //elem.className = msgtype;
};

var proceed = 2;  

function commonCheck    (vfld, ifld, reqd) {
  if (!document.getElementById) 
    return true;
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;
  if (elem.firstChild.nodeType != node_text)
    return true;
  if (emptyString1.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "ERROR: Pflichfeld bitte ausfüllen");  
      setfocus(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");
      return true;  
    }
  }
  return proceed;
}

function validatePresent(vfld, ifld ) {
  var stat = commonCheck (vfld, ifld, true);
  if (stat != proceed) return stat;

  msg (ifld, "warn", "");
  document.getElementById(ifld).style.height = '0px';
  return true;
};

function validateEmail  (vfld, ifld, reqd) {
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    msg (ifld, "error", "ERROR: keine gueltige email! bitte ueberpruefen");
    setfocus(vfld);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
  if (!email2.test(tfld)) {
    msg (ifld, "warn", "email adresse kann es nicht geben!! bitte ueberpruefen");
  } else {
    msg (ifld, "warn", "");
	document.getElementById(ifld).style.height = '0px';
	return true;
  }
  
};

function validateTelnr  (vfld, ifld, reqd) {
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var telnr = /^\+?[0-9 ()-]+[0-9]$/
  if (!telnr.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(vfld);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (ifld, "error", "ERROR: " + numdigits + " digits - too short");
    setfocus(vfld);
    return false;
  }

  if (numdigits>14)
    msg (ifld, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (ifld, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (ifld, "warn", "");
  }
  return true;
};

function validateAge (vfld, ifld, reqd) {
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var ageRE = /^[0-9]{1,3}$/
  if (!ageRE.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid age");
    setfocus(vfld);
    return false;
  }

  if (tfld>=200) {
    msg (ifld, "error", "ERROR: not a valid age");
    setfocus(vfld);
    return false;
  }

  if (tfld>110) msg (ifld, "warn", "Older than 110: check correct");
  else {
    if (tfld<7) msg (ifld, "warn", "Bit young for this, aren't you?");
    else        msg (ifld, "warn", "");
  }
  return true;
};
