var isValid;

//------ funkce pro získání hodnoty v inputu s id='element'
function getValue(element) {
  return document.getElementById(element).value;
}
//------ funkce pro získání délky řetezce v inputu s id='element'
function getLength(element) {
  return document.getElementById(element).value.length;
}
//------ funkce pro náhradu mezer v řetězci 'text' symbolem ''
function replaceWhiteSpaces(text) {
  return text.replace(/\s/g, '');
}
//------ funkce pro testování řetězce 'text' zda odpovídá šabloně 'pattern' regulárního výrazu
function testRegExp(pattern, text) {
  if(pattern.test(text)) return true;
    else return false;
}
//------ funkce pro kontrolu zda je input 'element' prázdný
function isEmpty(text) {
  if(text.replace(/\s/g, '').length < 3) return true;
    return false;     
}
//------ funkce pro nastavení inputu s id='element' jako špatně vyplněného
function setAsWrong(elementId) {
  document.getElementById(elementId).className = "inpWrong";
}
//------ funkce pro nastavení inputu s id='element' jako správně vyplněného
function setAsPass(elementId) {
  document.getElementById(elementId).className = "inp";
}

/*---------------------------------------------------------------------------*/

//------ funkce pro kontrolu jmena
function checkName(elementId) {
  var form_name = getValue(elementId);
  if(isEmpty(form_name) || form_name == "jméno" || form_name == "name") {setAsWrong("form_name");isValid = false;}
  else {setAsPass("form_name");}
}
//------ funkce pro kontrolu emailu
function checkEmail(elementId) {
  var email = getValue(elementId);
  mailPattern = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/);
  if(!testRegExp(mailPattern, email)) {setAsWrong("form_email");isValid = false;}
  else {setAsPass("form_email");}
}
//------ funkce pro kontrolu telefonu
function checkPhone(elementId) {
  var tel = getValue(elementId);
  telPattern = new RegExp(/^[1-9]{1}[0-9]{8,}$/);
  if(!testRegExp(telPattern, replaceWhiteSpaces(tel))) {setAsWrong("form_phone");isValid = false;}
  else {setAsPass("form_phone");}
}

function registerKeyUp() {
    document.getElementById("form_name").onkeyup = function() { checkName("form_name");}
    //document.getElementById("form_name").onchange = function() { checkName("form_name");}
    document.getElementById("form_email").onkeyup = function() { checkEmail("form_email");}
    //document.getElementById("form_email").onchange = function() { checkEmail("form_email");}
    document.getElementById("form_phone").onkeyup = function() { checkPhone("form_phone");}
    //document.getElementById("form_email").onchange = function() { checkEmail("form_phone");}
}

function checkForm() {
  isValid = true;

  checkName("form_name");
  checkEmail("form_email");
  checkPhone("form_phone");
  
    
if(isValid) return true;
registerKeyUp();
return false;
}


