function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);

  if (reason != "") {
    alert("Chýbajúce údaje:\n" + reason);
    return false;
  }

  return true;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Red';
        error = "Nezadali ste e-mailovú adresu!\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Red';
        error = "Nezadali ste e-mail v tvare meno@doména!\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Red';
        error = "E-mailová adresa obsahuje nedovolené znaky!\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Nezadali ste telefónne číslo!\n";
        fld.style.background = 'Red';
    } else if (isNaN(parseInt(stripped))) {
        error = "Telefónne číslo obsahuje nedovolené znaky!\n";
        fld.style.background = 'Red';
    } else if (!(stripped.length == 10)) {
        error = "Telefónne číslo má obsahovať 10 znakov!\n";
        fld.style.background = 'Red';
    }
    return error;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Red';
        error = "Je nutné zadať všetky údaje!\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
