//
function writeNewEmeilToNewsletter(){
  
  var email = document.getElementById('formNewsletterEmail').value;
  var dobryEmail=/^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;
  
  if(dobryEmail.test(email)){
    newsletter(email);
  }
  else{
    document.getElementById('formNewsletterEmail').value="Niepoprawny adres e-mail";
  }
  return false;
};

function newsleterMessage(dane){
  if(dane=="del"){
    document.getElementById('formNewsletterEmail').value="Adres e-mail został usunięty";
  }
  if(dane=="add"){
    document.getElementById('formNewsletterEmail').value="Adres e-mail został dodany";
  }
}
function newsletter(email){
  var ObiektXMLHttp = false;
    if (window.XMLHttpRequest) 
    { 
      ObiektXMLHttp = new XMLHttpRequest();        
    } 
    else if (window.ActiveXObject) 
    { 
      try
      {
        ObiektXMLHttp = new ActiveXObject("Microsoft.XMLHttp");                
      } 
      catch(e)
      {
        alert("Blad");
      }
    };
                
    if(ObiektXMLHttp) 
    {           
      ObiektXMLHttp.open("GET",'newsletter.php?email='+email);
      ObiektXMLHttp.onreadystatechange = function() 
      {
        if (ObiektXMLHttp.readyState == 4)
          {                 
            dane = ObiektXMLHttp.responseText;     
            newsleterMessage(dane);
          }
      }       
      
        // trzecia część kodu 
      ObiektXMLHttp.send(null); 
    } 
}

// 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=="wybierz"){
        temp = selectList[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 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 checkOrderForm(){
  
  clearAllError();
  
  var isGood = true;
  
  isGood = checkAllInputText(isGood);
  isGood = checkAllSelect(isGood);
  

  
  
  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, 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;
  }
}









