// JavaScript Document
window.onload = registruj_funkce;

var lengthAbout = 5000;


function kontrolPassword(e){
  if (document.getElementById("passwordInput2").value!="")
    if (document.getElementById("passwordInput2").value.length==document.getElementById("passwordInput1").value.length)  
      if (document.getElementById("passwordInput2").value==document.getElementById("passwordInput1").value){
        document.getElementById("passwordSubmit").disabled='';
        document.getElementById("notSamePassword").style.display="none";
      }
      else{
        document.getElementById("passwordSubmit").disabled='disabled';
        document.getElementById("notSamePassword").style.display="block";
      }
    else{
      document.getElementById("passwordSubmit").disabled='disabled';
    document.getElementById("notSamePassword").style.display="none";
    }
  else
  {
    document.getElementById("passwordSubmit").disabled='disabled';
    document.getElementById("notSamePassword").style.display="none";
  }
}

function countSpacesCz (e){
  if (document.getElementById("textProfileAboutCz").value.length>=lengthAbout){
    document.getElementById("countTextProfileAboutCz").style.color="red"
    document.getElementById("textProfileAboutCz").value = document.getElementById("textProfileAboutCz").value.substring(0,lengthAbout);
    document.getElementById("countTextProfileAboutCz").innerHTML=0;
  }
  else{
    document.getElementById("countTextProfileAboutCz").style.color="white";
    document.getElementById("countTextProfileAboutCz").innerHTML=(lengthAbout-document.getElementById("textProfileAboutCz").value.length);
  }
}

function countSpacesEn (e){
  
  if (document.getElementById("textProfileAboutEn").value.length>=lengthAbout){
    document.getElementById("countTextProfileAboutEn").style.color="red"
    document.getElementById("textProfileAboutEn").value = document.getElementById("textProfileAboutEn").value.substring(0,lengthAbout);
    document.getElementById("countTextProfileAboutEn").innerHTML=0;
  }
  else{
    document.getElementById("countTextProfileAboutEn").style.color="white"; 
    document.getElementById("countTextProfileAboutEn").innerHTML=(lengthAbout-document.getElementById("textProfileAboutEn").value.length);
  }
}

function countCharsProfileName(e){
  if (document.getElementById("profileName").value=="")
    document.getElementById("profileNameKec").style.color="red";
  else
    document.getElementById("profileNameKec").style.color="white";
  
  if (document.getElementById("profileEmail").value=="" || !checkMail(document.getElementById("profileEmail").value))
    document.getElementById("profileEmailKec").style.color="red";
  else
    document.getElementById("profileEmailKec").style.color="white";  
  
  if (document.getElementById("profileName").value=="" || document.getElementById("profileEmail").value=="" || !checkMail(document.getElementById("profileEmail").value))
    document.getElementById("editUserProfileSubmit").disabled="disabled";
  else
    document.getElementById("editUserProfileSubmit").disabled="";
}

function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

function generateHash(e){
  document.getElementById("hashedPassword").value = hex_sha1(document.getElementById("passwordInput2").value);
  document.getElementById("passwordInput2").value = "";
  document.getElementById("passwordInput1").value = "";
  //document.getElementById("hashedPassword").value = hex_sha1(document.getElementById("hashedPassword").value);
}

function form_submit (e) {
  	alert("Pokuks o odeslani formular, nepovoluji");
  	e.preventDefault();
  }

function registruj_funkce (){
  var passwordSubmitForm = document.getElementById("passwordChangeForm");
  var passwordSubmit = document.getElementById("passwordSubmit");
  var passwordInput1 = document.getElementById("passwordInput1");
  var passwordInput2 = document.getElementById("passwordInput2");
  var textProfileAboutCz = document.getElementById("textProfileAboutCz");
  var textProfileAboutEn = document.getElementById("textProfileAboutEn");
  var profileName = document.getElementById("profileName");
  var profileEmail = document.getElementById("profileEmail");
  

  
  
  
  profileName.onkeyup = countCharsProfileName;
  profileEmail.onkeyup = countCharsProfileName;
  
  textProfileAboutCz.onkeyup = countSpacesCz;
  textProfileAboutEn.onkeyup = countSpacesEn;

  passwordInput2.onkeyup = kontrolPassword;
  passwordInput1.onkeyup = kontrolPassword;
  
  passwordSubmitForm.onsubmit = generateHash;
  //nutný věci k provedení;
  document.getElementById("passwordSubmit").disabled='disabled';
  document.getElementById("jsOnOf").value="on";
  
  countSpacesCz (null);
  countSpacesEn (null);
  countCharsProfileName(null);
  //passwordInput1.addEventListener("keyup",kontrol,false);
  //passwordSubmitForm.addEventListener("submit",form_submit,false);
  //passwordSubmitForm.onsubmit = form_submit;
}
