function validateForm(form) {
  with (form) {
    if (!checkString(name, email, message)) return showAlert(0);
    if (!checkEmail(email.value)) return showAlert(1);
  }
  return true;
}

function showAlert(i) {
  var messages = new Array(
    'Wszystkie pola powinny byc wypelnione.',
    'Niepoprawny adres email.'
  );

  alert(messages[i]);
  return false;
}




function trim(t) {
  var i = 0;
  while ((i < t.length) && (t.charAt(i) == ' ')) i++;
  t = t.substring(i, t.length);

  i = t.length;
  while ((i > 0) && (t.charAt(i - 1) == ' ')) i--;
  return t.substring(0, i);
}



function trimZeroes(t) {
  var i = 0;

  while ((i < t.length) && (t.charAt(i) == '0')) i++;
  return t.substring(i, t.length);
}



function checkCheckboxAny() {
  for (var i = 0; i < arguments.length; i++)
    if (arguments[i].checked) return true;
  return false;
}



function checkString() {
  var result = true;
  for (var i = 0; i < arguments.length; i++)
    with (arguments[i]) {
      value = trim(value);
      if (value.length == 0) result = false;
    }
  return result;
}



function checkStringAny() {
  var result = 0;
  for (var i = 0; i < arguments.length; i++)
    with (arguments[i]) {
      value = trim(value);
      if (value.length > 0) result++;
    }
  return result;
}



function checkInt() {
  var result = true;
  for (var i = 0; i < arguments.length; i++)
    with (arguments[i]) {
      var n = parseInt(value, 10);
      if (isNaN(n)) {
        value = '';
        result = false;
      }
      else
        value = n;
    }
  return result;
}



function checkDate() {
  var result = true;
  for (var i = 0; i < arguments.length; i++)
    with (arguments[i]) {
      value = trim(value);

      var spl = value.split('-');
      if (spl.length == 3) {
        var yy = parseInt(trimZeroes(spl[0]));
        var mm = parseInt(trimZeroes(spl[1]));
        var dd = parseInt(trimZeroes(spl[2]));
        if (!(isNaN(yy) || isNaN(mm) || isNaN(dd))) {
          if (yy < 100) yy = yy + 2000;
          var day = new Date(yy, mm - 1, dd);
          if ((day.getYear() == yy) && (day.getMonth() == mm - 1) && (day.getDate() == dd)) {
            if (mm < 10) mm = '0' + mm;
            if (dd < 10) dd = '0' + dd;
            value = yy + '-' + mm + '-' + dd;
            continue;
          }
        }
      }
      result = false;
    }
  return result;
}



function checkEmail(t) {
  if ((t.indexOf(' ') != -1) || (t.indexOf(',') != -1) || (t.indexOf(';') != -1))
    return false;

  var ts = t.split('@');
  if (ts.length != 2) return false;

  var name = ts[0];
  var host = ts[1];

  if ((name.length == 0) || (host.length == 0)) return false;
  var hs = host.split('.');
  if (hs.length < 2) return false;

  for (var i = 0; i < hs.length; i++)
    if (hs[i].length == 0) return false;

  return true;
}