function validate_registration() {
  var email = document.forms.reg_form.email;  
  
  var email_error = document.getElementById('email_error');
  
  var correct = 1;
    
  if (email.value) {
    if ((email.value.indexOf(".") >= 1) && (email.value.indexOf("@") > 0)) {
      email_error.style.display = "none";
    } else {
      email_error.innerHTML = "Niepoprawny e-mail";
      email_error.style.display = "inline";
      correct = 0;
    }
  } else {
    email_error.innerHTML = "Pole wymagane";
    email_error.style.display = "inline";
    correct = 0;
  }
  
  return correct;
}

//wrzucilem ta w osobnym pliku js zeby ukryc przed uzytkownikami konstrukcje linku do usuwania konta
//Co prawda i tak potem jest sprawdzane czy dany uzytkownik wczesniej sie zalogowal ale napewno cos takiego nie zaszkodzi
function remove_account(link, user_id) {
  if (confirm('Czy jesteś pewien, że chcesz usunąć konto użytkownika serwisu USBDacs.eu? Posiadanie konta umożliwia pełne korzystanie z serwisu w tym dostęp do treści udostępnionych jedynie zarejestrowanym użytkownikom. Pamiętaj, że możesz dokonać zmiany adresu e-mail służącego logowaniu do serwisu bez konieczności tworzenia nowego konta.')) {
    link.href='rejestracja.php?action=remove_account&id=' + user_id;
    return true;
  } else {
    return false;
  }
}

function validate_edit_profil() {

  var new_email = document.forms.reg_form.new_email;  
  var new_email_error = document.getElementById('new_email_error');
  
  var pass = document.forms.reg_form.pass;
  var pass2 = document.forms.reg_form.pass2;  
  var pass2_error = document.getElementById('pass2_error');
  
  var correct = 1;
  if (new_email.value) {
    if ((new_email.value.indexOf(".") > 2) && (new_email.value.indexOf("@") > 0)) {
      new_email_error.style.display = "none";
    } else {
      new_email_error.innerHTML = "Niepoprawny e-mail";
      new_email_error.style.display = "inline";
      correct = 0;
    }
  }
  
  if (pass.value) {
    if (pass.value == pass2.value) {
      pass2_error.style.display = "none";
    } else {
      pass2_error.innerHTML = "Żle powtórzone";
      pass2_error.style.display = "inline";
      correct = 0;
    }
  } else {
    pass2_error.style.display = "none";
  }
   
  return correct;
}

function showPic( pic, pid )
{
	window.open('showPic.php?pic=' + pic + '&pid=' + pid,'','menubar=no, personalbar=no, toolbar=no, width=675, height=505');
}

function clear_string(input) {
  var string = input.value;
  if (string.match(/[^a-zA-Z0-9_ąęółśżźćń ]/g)) {
    string = string.replace(/[^a-zA-Z0-9_ąęółśżźćń\-\.@\+ ]/g, '');
    input.value = string;
  }
}

function add_zapotrzebowanie(producent_id, producent_name, kategoria_id, kategoria_name, id_zapotrzebowania) {
  var producent_id = Number(producent_id);
  var kategoria_id = Number(kategoria_id);
  var opis = producent_name;
  if (kategoria_id) {
    opis = opis + ' - ' + kategoria_name;
  }
  
   
  
  var zapotrzebowanie = document.getElementById('zapotrzebowanie_body');
  var row_num = zapotrzebowanie.rows.length;
  
  //sprawdzamy czy nie ma juz podobnego wpisu
  for (i = 0; i < row_num; i++) {
    if (zapotrzebowanie.rows[i].cells[1].getElementsByTagName('a')[0].text == opis) {
      setTimeout("document.getElementById('zapotrzebowanie_body').rows[" + i + "].getElementsByTagName('a')[0].style.color = 'rgb(255, 0, 0)';", 100);
      setTimeout("document.getElementById('zapotrzebowanie_body').rows[" + i + "].getElementsByTagName('a')[0].style.color = 'rgb(210, 137, 1)';", 300);
      setTimeout("document.getElementById('zapotrzebowanie_body').rows[" + i + "].getElementsByTagName('a')[0].style.color = 'rgb(255, 0, 0)';", 500);
      setTimeout("document.getElementById('zapotrzebowanie_body').rows[" + i + "].getElementsByTagName('a')[0].style.color = 'rgb(210, 137, 1)';", 700);
      setTimeout("document.getElementById('zapotrzebowanie_body').rows[" + i + "].getElementsByTagName('a')[0].style.color = 'rgb(255, 0, 0)';", 900);
      setTimeout("document.getElementById('zapotrzebowanie_body').rows[" + i + "].getElementsByTagName('a')[0].style.color = 'rgb(210, 137, 1)';", 1100);
      return false;
    }
  }
  
  //tworzymy wszystkie kolumny nowego wiersza
  zapotrzebowanie.insertRow(row_num);
  zapotrzebowanie.rows[row_num].insertCell(0);
  zapotrzebowanie.rows[row_num].insertCell(1);
  zapotrzebowanie.rows[row_num].insertCell(2);
  zapotrzebowanie.rows[row_num].insertCell(3);
  
  var cell0 = zapotrzebowanie.rows[row_num].cells[0];
  var cell1 = zapotrzebowanie.rows[row_num].cells[1];
  var cell2 = zapotrzebowanie.rows[row_num].cells[2];
  var cell3 = zapotrzebowanie.rows[row_num].cells[3];
  
  cell0.style.backgroundColor = "#594f4e";
  cell1.style.backgroundColor = "#594f4e";
  cell2.style.backgroundColor = "#594f4e";
  cell2.style.padding = "0 0 0 10px";
  cell3.style.backgroundColor = "#594f4e";
  
  //tworzymy sobie linka
  var link = document.createElement('a');
  if (kategoria_id) {
    link.setAttribute('href', 'urzadzenia.php?k=' + kategoria_id + '&p=' + producent_id);
  } else {
    link.setAttribute('href', 'urzadzenia.php?p=' + producent_id);
  }
  link.setAttribute('target', '_blank');
  link.appendChild( document.createTextNode( opis ) );
  cell1.appendChild( link );
  
  var link = document.createElement('a');
  link.setAttribute('href', 'usun.html');
  //link.appendChild( document.createTextNode( ' usun' ) );
  var obrazek = document.createElement('img');
  obrazek.setAttribute('src', 'gfx/publish_x.png');
  obrazek.style.borderStyle = 'none';
  link.appendChild( obrazek );
  link.onclick = function() {remove_zapotrzebowanie( this.parentNode.parentNode, id_zapotrzebowania); return false;}
  cell2.appendChild( link );
  
  
  var new_input = document.createElement('input');
  new_input.setAttribute('name', 'zap_id[]');
  new_input.setAttribute('type', 'hidden');
  if (id_zapotrzebowania) {
    new_input.setAttribute('value', id_zapotrzebowania);
  } else {
    new_input.setAttribute('value', 0);
  }
  cell0.appendChild( new_input );
  
  var new_input = document.createElement('input');
  new_input.setAttribute('name', 'zap_producent_id[]');
  new_input.setAttribute('type', 'hidden');
  new_input.setAttribute('value', producent_id);
  cell0.appendChild( new_input );
  
  var new_input = document.createElement('input');
  new_input.setAttribute('name', 'zap_producent_name[]');
  new_input.setAttribute('type', 'hidden');
  new_input.setAttribute('value', producent_name);
  cell0.appendChild( new_input );
  
  var new_input = document.createElement('input');
  new_input.setAttribute('name', 'zap_kategoria_id[]');
  new_input.setAttribute('type', 'hidden');
  new_input.setAttribute('value', kategoria_id);
  cell0.appendChild( new_input );
  
  var new_input = document.createElement('input');
  new_input.setAttribute('name', 'zap_kategoria_name[]');
  new_input.setAttribute('type', 'hidden');
  new_input.setAttribute('value', kategoria_name);
  cell0.appendChild( new_input );
}

function remove_zapotrzebowanie( row, id) {
  row.parentNode.removeChild(row);
  
  if (id) {
    var new_input = document.createElement('input');
    new_input.setAttribute('name', 'del_zapotrzebowanie[]');
    new_input.setAttribute('type', 'hidden');
    new_input.setAttribute('value', id);
    document.getElementById('zapotrzebowanie_contener').appendChild( new_input );
  }
}

//funkcja zmienia selecta kategorii w zaleznosci od tego ktory producent zostal wybrany
function change_kategoria(producent_id) {
  ajaxRequest = getAjax();

  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('kategoria_div');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
  }
  document.getElementById('kategoria_div').innerHTML = "<img style='margin: 0 auto;' src='gfx/ajax_loader.gif' />";
  var producent_id = document.getElementById('producent_select').value;
  var queryString = "&producent_id=" + producent_id;
  ajaxRequest.open("GET", "get.php?task=set_kategoria" + queryString, true);
  ajaxRequest.send(null);
}

function getAjax(){
  var ajaxRequest;
  
  try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
  } catch (e){
    // Internet Explorer Browsers
    try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e){
        // Something went wrong
        alert("Your browser broke!");
        return false;
      }
    } 
  }
  return ajaxRequest;
}


