function validateSendMailForm(theForm){
  var reason="";
  reason += validateEmpty(theForm.name);
  reason += validatePhone(theForm.phone);
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.message);
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true;
}
function validateGoToForm(theForm){
  var reason="";
  reason += validatePositive(theForm.page);
  
  if (reason != "") {
    alert("Formulář není správně vyplněn:\n" + reason);
    return false;
  }

  return true; 
}
function validatePositive(fld){
  var error = "";
  if(fld.value == "") {
    error = "Zadejte číslo\n";
    fld.style.background = 'Yellow';
  } else if (isNaN(parseInt(fld.value))) {
    error = "Číslo obsahuje neplatné znaky\n";
    fld.style.background = 'Yellow';
  }else if (parseInt(fld.value) <= 0){
    error = "Zadejte kladné číslo.\n";
    fld.style.background = 'Yellow';
  } else{
    fld.style.background = 'White';
  }
  return error;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Požadované pole nebylo vyplněno.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

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 = 'Yellow';
        error = "Vyplňte emailovou adresu.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Zadejte validní emailovou adresu.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Emailová adresa obsahuje neplatné znaky.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateNumber(fld) {
  var error = "";
  if(fld.value == "") {
    error = "Zadejte číslo\n";
    fld.style.background = 'Yellow';
  } else if (isNaN(parseInt(fld.value))) {
    error = "Číslo obsahuje neplatné znaky\n";
    fld.style.background = 'Yellow';
  }else{
    fld.style.background = 'White';
  }
  return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "";
        fld.style.background = 'White';
    } else if (isNaN(parseInt(stripped))) {
        error = "Telefonní číslo obsahuje neplatné znaky.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validatePhoneReq(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "Zadejte telefonní číslo";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "Telefonní číslo obsahuje neplatné znaky.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

