/*
 * Copyright 2008 Onyx Sp. z o.o. All rights reserved. Wszelkie prawa
 * zastrzeżone. Jakiekolwiek wykorzystywanie bez pisemnego zezwolenia,
 * całości lub fragmentów kodu jest zabronione i będzie ścigane z mocy
 * polskich i międzynarodowych przepisów o ochronie praw autorskich.
 */

// plik w kodowaniu utf-8
function val_radiogrupa(pole, etykieta, wymagane) {
	if (wymagane) {
		checked=0;
		if (document.forms[0].elements[pole].length) {
			for (i=0;i<document.forms[0].elements[pole].length;i++) {
				if (document.forms[0].elements[pole][i].checked) {
					checked = 1;
					break;
				}
			}

		}
		else {
			if (document.forms[0].elements[pole].checked) checked=1;
		}
		if (!checked) {
			//document.forms[0].elements[pole][0].focus();
			alert ("Musisz wybrać jedną z pozycji pola \"" + etykieta + "\".");
			return false;
		}
	}
	return true;
}

function val_str(pole, etykieta, maximum, minimum) {
  trim(pole);
  if (document.forms[0].elements[pole].value==''){
    if(minimum > 0){
      document.forms[0].elements[pole].focus();
      alert("Pole  \"" + etykieta + "\" musi zostać wypełnione.");
           return false;
        } else return true;
  }
  if (!minimum) minimum = 0;
  if (document.forms[0].elements[pole].value.length < minimum || document.forms[0].elements[pole].value.length > maximum){
      document.forms[0].elements[pole].focus();
                if (minimum != maximum) alert ("Pole \"" + etykieta + "\" musi zawierać od " + minimum + " do " + maximum + " znaków.");
                 else alert ("Pole \"" + etykieta + "\" musi mieć dokładnie " + maximum + " znaków.");
                return false;
        }
  return true;
}

// SPRAWDZENIE LICZB NATURALNYCH
// minimum: wartosc minimalna; maximum: wartosc maksymalna; wymagane: czy pole musi być wypełnione
function val_int(pole, etykieta, maximum, minimum, wymagane){
  trim(pole);
  if(document.forms[0].elements[pole].value=='') {
    if(wymagane) {
      document.forms[0].elements[pole].focus();
      alert("Pole \""+etykieta+"\" musi zostać wypełnione.");
           return false;
    } else return true;
  }
  if(!onlydigits(document.forms[0].elements[pole].value)) {
    document.forms[0].elements[pole].focus();
    alert ("Pole \"" + etykieta + "\" musi zawierać same cyfry.");
         return false;
  }
  if (maximum && (document.forms[0].elements[pole].value > maximum)){
    document.forms[0].elements[pole].focus();
    alert ("Pole \"" + etykieta + "\" musi zawierać liczbę nie większą niż " + maximum + ".");
    return false;
  }
  if (minimum && (document.forms[0].elements[pole].value < minimum)){
    document.forms[0].elements[pole].focus();
    alert ("Pole \"" + etykieta + "\" musi zawierać liczbę nie mniejszą niż " + minimum + ".");
    return false;
  }
  return true;
}

// SPRAWDZENIE LICZB DZIESIÄTNYCH
// minimum: wartosc minimalna; maximum: wartosc maksymalna;
// wymagane: czy pole musi być wypełnione, lb_miejsc_dzies: dopuszczalna liczba cyfr po znaku dziesiętnym
function val_dec(pole, etykieta, maximum, minimum, wymagane, lb_miejsc_dzies){
  trim(pole);
  if(document.forms[0].elements[pole].value=='') {
    if(wymagane) {
      document.forms[0].elements[pole].focus();
      alert("Pole \"" + etykieta + "\" musi zostać wypełnione.");
           return false;
    } else return true;
  }
  skladniki = split(document.forms[0].elements[pole].value,'.,');
  if (!skladniki[1]) skladniki[1] = '';

  if ((skladniki.lenght > 2) || (!onlydigits(skladniki[0])) || (!onlydigits(skladniki[1])) || ((lb_miejsc_dzies > 0) && (skladniki[1].length > lb_miejsc_dzies))){
    document.forms[0].elements[pole].focus();
         pom = "Pole \"" + etykieta + "\" musi zawierać liczbę rzeczywistą.";
         if (lb_miejsc_dzies) pom = pom + "(liczba miejsc dziesiętnych: " + lb_miejsc_dzies + ")";
    alert (pom);
         return false;
  }
  pom = skladniki[0] + "." + skladniki[1];

  if (maximum && (pom > maximum)){
    document.forms[0].elements[pole].focus();
    alert ("Pole \"" + etykieta + "\" musi zawierać liczbę nie wiekszą niż " + maximum + ".");
    return false;
  }
  if (minimum && (pom < minimum)){
    document.forms[0].elements[pole].focus();
    alert ("Pole \"" + etykieta + "\" musi zawierać liczbę nie mniejszą niż " + minimum + ".");
    return false;
  }
  return true;
}

// SPRAWDZENIE KODU POCZTOWEGO
// wymagane: czy pole musi być wypełnione
function val_kod(pole, etykieta, wymagane){
  trim(pole);
  if(document.forms[0].elements[pole].value=='') {
    if(wymagane) {
      document.forms[0].elements[pole].focus();
      alert("Pole \"" + etykieta + "\" musi zostać wypełnione.");
           return false;
    } else return true;
  }
  skladniki = split(document.forms[0].elements[pole].value,'-');
  if ((skladniki[0].length != 2) || (skladniki[1].length != 3) || (skladniki.length != 2) || (!onlydigits(skladniki[0])) || (!onlydigits(skladniki[1])) || (!between(skladniki[0],0,99)) || (!between(skladniki[1],0,999))){
    document.forms[0].elements[pole].focus();
         pom = "Pole \"" + etykieta + "\" musi zawierać dwie liczby oddzielone łącznikiem (np. 12-345 )";
    alert (pom);
         return false;
  }
  return true;
}

// SPRAWDZENIE Numeru identyfikacji podatkowej NIP
// wymagane: czy pole musi być wypełnione
function val_nip(pole, etykieta, wymagane){
  trim(pole);
  if(document.forms[0].elements[pole].value=='') {
    if(wymagane) {
      document.forms[0].elements[pole].focus();
      alert("Pole \"" + etykieta + "\" musi zostać wypełnione.");
           return false;
    } else return true;
  }
  skladniki = split(document.forms[0].elements[pole].value,'-');
  if (!skladniki[1]) skladniki[1] = '';
  if (!skladniki[2]) skladniki[2] = '';
  if (!skladniki[3]) skladniki[3] = '';

  if ((skladniki[0].length != 3) || (!between(skladniki[1].length,2,3)) || (skladniki[0].length != 3) || (!between(skladniki[3].length,2,3))|| ((skladniki[1].length + skladniki[3].length) != 5) || (skladniki.length != 4) || (!onlydigits(skladniki[0])) || (!onlydigits(skladniki[1])) || (!onlydigits(skladniki[2])) || (!onlydigits(skladniki[3])) || (!between(skladniki[0],0,999)) || (!between(skladniki[1],0,999)) || (!between(skladniki[2],0,999)) || (!between(skladniki[3],0,999))){
    document.forms[0].elements[pole].focus();
         pom = "Pole \"" + etykieta + "\" musi zawierać cztery liczby oddzielone łącznikiem\n (np. 888-888-88-88 lub 888-88-88-888 )";
    alert (pom);
         return false;
  }
  return true;
}

// SPRAWDZENIE Numeru REGON
// wymagane: czy pole musi być wypełnione
function val_regon(pole, etykieta, wymagane){
  trim(pole);
  if(document.forms[0].elements[pole].value=='') {
    if(wymagane) {
      document.forms[0].elements[pole].focus();
      alert("Pole \"" + etykieta + "\" musi zostać wypełnione.");
           return false;
    } else return true;
  }
  if (document.forms[0].elements[pole].value.length != 9){
    document.forms[0].elements[pole].focus();
    alert("Pole z \"" + etykieta + "\" musi zawierać 9 znaków.(a nie " + document.forms[0].elements[pole].value.length + ")");
    return false;
  }
  return true;
}

// SPRAWDZENIE czy wybrano wartość w polu select
// minimum: najmniejsze id jakie można wybrać
function val_select(pole, etykieta, minimum){
   if (document.forms[0].elements[pole].value < minimum){
         document.forms[0].elements[pole].focus();
         alert ("Musisz wybrać jedną z pozycji pola \"" + etykieta + "\"."); // zmiana WP
         return false;
   }
   return true;
}

// SPRAWDZENIE adresu E-MAIL
// max: maksymalna liczba znakow; wymagane: czy pole musi być wypełnione;
function val_email(pole, etykieta, maximum, wymagane) {
  trim(pole);
  if (document.forms[0].elements[pole].value==''){
    if(wymagane){
      document.forms[0].elements[pole].focus();
      alert("Pole \"" + etykieta + "\" musi zostać wypełnione.");
           return false;
        } else return true;
  }
  if (maximum && (document.forms[0].elements[pole].value.length > maximum)){
      document.forms[0].elements[pole].focus();
                alert("Pole \"" + etykieta + "\" nie może mieć więcej niż " + maximum + " znaków.");
                return false;
  }
  skladniki  = split(document.forms[0].elements[pole].value,'@');
  if (!skladniki[0]) skladniki[0] = '';
  if (!skladniki[1]) {
     skladniki[1] = '';
          skladniki2   = '';
  } else  skladniki2 = split(skladniki[1],'.');

  if ((skladniki.length != 2) || (skladniki2.length < 2)){
    document.forms[0].elements[pole].focus();
         pom = "Pole \"" + etykieta + "\" musi zawierać poprawny adres poczty elektronicznej\n (np. jkowal@abc.pl )";
    alert (pom);
         return false;
  }
  return true;
}


// SPRAWDZENIE DATY  (dzien-miesiac-rok "dd-mm-rrrr")
// sep: separator, np kropka, kreska itp., wymagane: czy pole musi być wypełnione
function val_date(pole, etykieta, sep, wymagane) {
	trim(pole);
	if (document.forms[0].elements[pole].value==''){
		if(wymagane){
			document.forms[0].elements[pole].focus();
			alert("Pole \"" + etykieta + "\" musi zostać wypełnione.");
			return false;
		} else return true;
	}
	skladniki = split(document.forms[0].elements[pole].value,'.-'+sep);
	if (!(skladniki.length == 3) || !onlydigits(skladniki[0]) || !onlydigits(skladniki[1]) || !onlydigits(skladniki[2])
		|| (	!(between(skladniki[0].length,1,2) && between(skladniki[1].length,1,2) && skladniki[2].length == 4)
			&& !(skladniki[0].length == 4 && between(skladniki[1].length,1,2) && between(skladniki[2].length,1,2)))) {
				document.forms[0].elements[pole].focus();
				alert("Pole \"" + etykieta + "\" musi zawierać datę w formacie dd.mm.rrrr lub rrrr-mm-dd");
				return false;
	}
	return true;
}

function format_dec(x,lb_miejsc,sep)
{
  if (!sep) sep=','
  if (!lb_miejsc) lb_miejsc = 2;

  if (x < 10) t = "00" + x;
   else  t = "" + x; /* ach ten brak typow */

  for (i=0; i <= (lb_miejsc - t.length); i++) t = "0" + t;

  return t.substring(0,t.length - lb_miejsc) + sep + t.substring(t.length - lb_miejsc,t.length);
}

function val_file(pole, etykieta, wymagane) {
	if (typeof(FileUploader) == 'undefined' || FileUploader.mode == 'input') {
		document.forms[0].encoding = 'multipart/form-data';
		if (typeof(document.forms[0].elements[pole]) == 'undefined' || !wymagane || document.forms[0].elements[pole].value) return true;
		document.forms[0].elements[pole].focus();
		alert("Pole z plikiem \"" + etykieta + "\" musi zostać wypełnione.");
		return false;
	}
	var uploadername = document.forms[0].elements[pole].value;
	var flashData = FileUploader.getData(uploadername);
	switch(flashData.state) {
		case 1:
			if(wymagane) {
				alert("Pole z plikiem \"" + etykieta + "\" musi zostać wypełnione.");
				return false;
			}
			break;
		case 2:
		case 3:
		case 5:
			alert("Nie można przesłać formularza ponieważ trwa przesyłanie danych w polu \"" + etykieta + "\".");
			return false;
			break;
		case 4:
			break;
	}
	return true;
}


// funkcje do listy
function settargets(action,target) {
   document.forms[0].action=action;
   document.forms[0].target=target;
}

function invalid(field,what) {
   if (document.forms[0].elements[field].value==''){
         alert ('Musisz wybrac ' + what + '.');
         return true;
   }
   return false;
}

// FUNKCJE POMOCNICZE
function onlydigits(s) {
//  if (!s) return true;
  for (i = 0; i < s.length; i++)
        if (s.charCodeAt(i) < 48 || s.charCodeAt(i) > 57) return false;
  return true;
}

function between(n,minimum,maximum) {
  if (n < minimum || n > maximum) return false;
  return true;
}

function trim(pole){
   str = document.forms[0].elements[pole].value;
        p1 = 0;
        while (str.charAt(p1) == ' ') p1++;
        p2 = str.length - 1;
        while (str.charAt(p2) == ' ') p2--;
        nowy = '';
        for (i = p1; i <= p2; i++) nowy = nowy + str.charAt(i);
        document.forms[0].elements[pole].value = nowy;

   return true;
}

function include(znak,str){
  for (i = 0; i < str.length; i++)
    if (str.charAt(i) == znak) return true;

  return false;
}

function split(str,separatory){
   wynik = new Array();
        wynik[0] = '';
        p = 0;
        w = false;
   while (!w && p < separatory.length) w = include(separatory.charAt(p++),str);
        if (!w){ wynik[0] = str; return wynik; }

        sep = separatory.charAt(--p);
        p = 0;
        wynik = new Array();
        wynik[p] = '';
   for (i=0; i < str.length; i++)
         if (str.charAt(i) == sep) wynik[++p] = ''; else wynik[p] = wynik[p] + str.charAt(i);

        return wynik
}

function get_dec(x){
   pom = split(x,'.,');
	if (pom[1]) y = (pom[1].length==2)? pom[1] : eval('pom[1]*10'); else y=0;

	return pom[0]*100 + y*1;
}

// SPRAWDZENIE ZAZNACZENIA POLA WYBORU
// alert: komunikat w przypadku braku zaznaczenia, wymagane: czy pole musi być wypełnione
function val_checkbox(pole, etykieta, wymagane) {
  trim(pole);
  if (!(document.forms[0].elements[pole].checked)){
    if(wymagane){
      document.forms[0].elements[pole].focus();
      alert("Pole wyboru \"" + etykieta + "\" musi zostać zaznaczone.");
          return false;
        } else return true;
  }
  return true;
}

