
// funkcja sprawdzająca wszyskie wymagane pola typu select
function checkAllSelect(isGood){
  var selectList = document.getElementsByTagName('select');
  for(i=0; i <selectList.length; i++){
    if (selectList[i].title =='wymagany' && selectList[i].value=="w"){
        temp = selectList[i].name+"IncorectDate";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu text i checkbox
function checkAllInputText(isGood){
  var inputList = document.getElementsByTagName('input');
  for(i=0; i <inputList.length; i++){
    if (inputList[i].title =='wymagany' && inputList[i].value=="" && inputList[i].type=="text"){
        temp = inputList[i].name+"IncorectDate";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        document.getElementById(temp).style.display = "block";
        isGood = false;
    }
    if (inputList[i].title =='wymagany' && !inputList[i].checked && inputList[i].type=="checkbox"){
        temp = inputList[i].name+"IncorectDate";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        document.getElementById(temp).style.display = "block";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu textarea
function checkAllTextarea(isGood){
  var inputList = document.getElementsByTagName('textarea');
  for(i=0; i <inputList.length; i++){
    if (inputList[i].title =='wymagany' && inputList[i].value==""){
        temp = inputList[i].name+"IncorectDate";
        document.getElementById(temp).innerHTML = "Pole wymagane";
        isGood = false;
    }
  }
  return isGood;
}

// funkcja sprawdzająca wszyskie wymagane pola typu textarea
function checkPassword(isGood){
  var inputList = document.getElementsByTagName('input');
  for(i=0; i <inputList.length; i++){
    if (inputList[i].value=="" && inputList[i].type=="password" && inputList[i].title =='wymagany'){
      temp = inputList[i].name+"IncorectDate";
      document.getElementById(temp).innerHTML = "Pole wymagane";
      document.getElementById(temp).style.display = "block";
      isGood = false;
    }
  }
  if(isGood && document.getElementById('formPassword').value!=document.getElementById('formPasswordRepeat').value){
      document.getElementById('formPasswordIncorectDate').innerHTML = "Wprowadzone hasła nie zgadzają się";
      document.getElementById('formPasswordIncorectDate').style.display = "block";
      isGood = false;
  }
  return isGood;
}
function clearAllError(){
  var spanList = document.getElementsByTagName('span');
  for(i=0; i <spanList.length; i++){
    if (spanList[i].className =='incorectDate'){
      spanList[i].innerHTML = "";
      spanList[i].style.display = "none";
    }
  }
}
// sprawdza poprawność formularz rejestracji
function checkFormRegristed(){
  
  clearAllError();
  
  var isGood = true;
  
  isGood = checkAllInputText(isGood);
  isGood = checkPassword(isGood);
  
  if(isGood && document.getElementById('formEmail').value!=document.getElementById('formEmailRepeat').value){
      document.getElementById('formEmailIncorectDate').innerHTML = "Wprowadzone adresy email nie zgadzają się";
      document.getElementById('formEmailIncorectDate').style.display = "block";
      
      isGood = false;
  }
  if(isGood){
      isGood = isEmailAddress(isGood, 'formEmail');
  }
  
  
  if (isGood == true){
    return true;
  }
  else{
    alert('Popraw informacje wprowadzone do formularza');
    return false;
  }
}

// sprawdza poprawność uaktualnienia konta
function checkFormUpdateAccount(){
  
  clearAllError();
  
  var isGood = true;
  
  isGood = checkAllInputText(isGood);
  
  if(isGood && document.getElementById('formPassword').value!=document.getElementById('formPasswordRepeat').value){
      document.getElementById('formPasswordIncorectDate').innerHTML = "Wprowadzone hasła nie zgadzają się";
      document.getElementById('formPasswordIncorectDate').style.display = "block";
      isGood = false;
  }
  
  if(isGood && document.getElementById('formEmail').value!=document.getElementById('formEmailRepeat').value){
      document.getElementById('formEmailIncorectDate').innerHTML = "Wprowadzone adresy email nie zgadzają się";
      document.getElementById('formEmailIncorectDate').style.display = "block";
      
      isGood = false;
  }
  if(isGood){
      isGood = isEmailAddress(isGood, 'formEmail');
  }
  
  
  if (isGood == true){
    return true;
  }
  else{
    alert('Popraw informacje wprowadzone do formularza');
    return false;
  }
}

// sprawdza poprawność zgłaszania promozycji
function checkFormRemindPassword(){

  var isGood = true;
  
  isGood = checkAllInputText(isGood);
  if(isGood){
      isGood = isEmailAddress(isGood, 'formEmail');
  }

  if (isGood == true){
    return true;
  }
  else{
    alert('Popraw informacje wprowadzone do formularza');
    return false;
  }
}
  // sprawdza, czy użytkownik wprowadził poprawny adres email
function isEmailAddress(isGood, formName){
  var dobryEmail=/^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;
  
  val = document.getElementById(formName).value;
  
  if (dobryEmail.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorectDate";
    document.getElementById(temp).innerHTML = "Niepoprawny adres E-mail";
    document.getElementById(temp).style.display="block";
    return false;
  }
}

  // sprawdza, czy użytkownik wprowadził poprawny telefon
  
function isPhoneNumber(isGood, formName){
  var dobryNumer=/[0-9\b]+$/;
  
  val = document.getElementById(formName).value;
  
  if (dobryNumer.test(val)){
    return isGood;
  }
  else{
    temp = formName+"IncorectDate";
    document.getElementById(temp).innerHTML = "Niepoprawny nr telefonu";
    return false;
  }
}








