                                       var ajax_method = typeof XMLHttpRequest == "undefined"?new ActiveXObject('Microsoft.XMLHttp'):new XMLHttpRequest();

function get_cennik() {

  var password = element('cennik_pass').value;
  if(password == "") {
    element('cennik_pass').style.background = "#ffc6c6";
    alert('Proszę podać hasło!');
    return;
  }

  ajax_method.open( "GET", "get_price.php?id=0&pass="+password, false );
  ajax_method.send(null);

  if (ajax_method.readyState == 4) {
    if (ajax_method.status == 200) {
      //akcja gdy wynik zaladowany
      var res = (Trim(ajax_method.responseText));
      if(res=="full_ok") {
        window.location.href = "./cennik.php";
      } else if(res.substr(0,4)=="pass") {
        element('cennik_pass').style.background = "#ffc6c6";
        alert('Niepoprawne hasło!');
      } else {
        alert('Wystąpił nieoczekiwany błąd!' + res);
      }
    } else {
      alert('Errors detected...');
    }

  }

}

function get_price(product_id) {

  var password = element('price_pass'+product_id).value;
  if(password == "") {
    element('price_pass'+product_id).style.background = "#ffc6c6";
    alert('Proszę podać hasło!');
    return;
  }

  ajax_method.open( "GET", "get_price.php?id="+product_id+"&pass="+password, false );
  ajax_method.send(null);

  if (ajax_method.readyState == 4) {
    if (ajax_method.status == 200) {
      //akcja gdy wynik zaladowany
     // element('progress').style.display = 'none';
      var res = (Trim(ajax_method.responseText));
      if(res.substr(0,4)=="Cena") {
        element('cena'+product_id).innerHTML = res;
      } else if(res.substr(0,4)=="pass") {
        element('price_pass'+product_id).style.background = "#ffc6c6";
        alert('Niepoprawne hasło!');
      } else {
        alert('Wystąpił nieoczekiwany błąd!' + res);
      }
    } else {
      alert('Errors detected...');
    }

  }

}

function element(el_id) {
  return document.getElementById(el_id);
}

function show_hide(el_id) {
  element(el_id).style.display = (element(el_id).style.display == 'none') ? 'block' : 'none';
}

function check_q (form_obj) {
  var mail = element('user_email').value;
  var pytanie = element('pytanie').value;
  var error = false;
  var error_msg = '';
  
  if( !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(mail) ) {
    error = true;
    error_msg += "Niepoprawny adres E-Mail!\n";
    element('user_email').style.background = "#ffc6c6";
  } else {
    element('user_email').style.background = "#fff";
  }
  
  if( pytanie.length < 20 ) {
    error = true;
    error_msg += "Zbyt krótkie pytanie! (wymagane jest co najmniej 20 znaków)";
    element('pytanie').style.background = "#ffc6c6";
  } else {
    element('pytanie').style.background = "#fff";
  }
  
  if( error == true ) {
    alert("Wystąpiły błędy:\n\n"+error_msg);
    return false;
  } else {
    return true;
  }
}

function reg_check(form_obj) {
  var mail = element('email');
  var imie_nazwisko = element('imie_nazwisko');
  var error = false;
  var error_msg = '';
  
  if( !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(mail.value) ) {
    error = true;
    error_msg += "Niepoprawny adres E-Mail!\n";
    mail.style.background = "#ffc6c6";
  } else {
    mail.style.background = "#fff";
  }
  
  if( imie_nazwisko.value.length < 5 ) {
    error = true;
    error_msg += "W polu \"Imię i nazwisko\" wymagane jest co najmniej 5 znaków";
    imie_nazwisko.style.background = "#ffc6c6";
  } else {
    imie_nazwisko.style.background = "#fff";
  }
  
  if( error == true ) {
    alert("Wystąpiły błędy:\n\n"+error_msg);
    return false;
  } else {
    return true;
  }
}

function LTrim(str) {
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
    var j=0, i = s.length;
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
      j++;
    s = s.substring(j, i);
  }
  return s;
}

function RTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    var i = s.length - 1;
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;
    s = s.substring(0, i+1);
  }
  return s;
}

function Trim(str) {
  return RTrim(LTrim(str));
}


