function displayWindow(url, width, height) {
 var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=no' );
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

 function szukajsp() {
   if (document.form.szukajco.value=="") {
   alert("Wpisz szukan± frazę !");
   document.form.szukajco.focus();
  return false;
  }
 return true;
 }

  function checkemail(param) {
 	goodSigns = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@."
	 if ( param.length == 0 ) {
	 return true
	 } 
	 for (i=0; i<param.length; i++) {
	 sign=param.charAt(i)
	 if (goodSigns.indexOf(sign,0) == -1){ return false }
	 }
	 return true
 } 
 function sprawdz() {

   if (document.formularz3.login.value == "") {
 alert("Proszę podać adres E-mail!");
 document.formularz3.login.focus();
 return false;
 }
 
 if (!checkemail(document.formularz3.login.value)) {
	   alert("Pole 'E-mail' zawiera niedozwolone znaki!" + '\n\n' + "Proszę uzyć:" + '\n' + "litery ( A-Z a-z ), cyfry ( 0-9 ), znak 'minus' ( - ), znak 'podkre¶lenia' ( _ ), znak 'malpa' ( @ ), znak 'kropka' ( . )");
	   document.formularz3.login.focus();
	   document.formularz3.login.select();
	   return false;
	 }
	 if (document.formularz3.login.value != "" && (document.formularz3.login.value.indexOf('@', 0) < 1 || document.formularz3.login.value.indexOf('.') < 1)) {
	   alert("Pole 'E-mail' jest nie poprawne!");
	   document.formularz3.login.focus();
	   document.formularz3.login.select();
	   return false;
	 }
 
 
  if (document.formularz3.haslo_1.value == "") {
 alert("Proszę podać hasło!");
 document.formularz3.haslo_1.focus();
 return false;
 }
 
 
 if (document.formularz3.haslo_1.value.length < "6") {
	alert ("Podene hasło jest za krótkie");
	document.formularz3.haslo_1.focus();
	return false;
	}
 
 
    if (document.formularz3.haslo_2.value == "") {
 alert("Proszę powtórzyć hasło!");
 document.formularz3.haslo_2.focus();
 return false;
 }
 
  if (document.formularz3.haslo_2.value.length < "6") {
	alert ("Podene hasło jest za krótkie");
	document.formularz3.haslo_2.focus();
	return false;
	}
 
 
  if (document.formularz3.haslo_1.value != document.formularz3.haslo_2.value) {
	alert ("Hasła musz± być zgodne");
	document.formularz3.haslo_1.focus();
	 document.formularz3.haslo_2.value='';
	   document.formularz3.haslo_1.value='';
	
	
	return false;
	}
 
 
    if (document.formularz3.imie.value == "") {
 alert("Proszę podać Imię!");
 document.formularz3.imie.focus();
 return false;
 }
 
     if (document.formularz3.nazwisko.value == "") {
 alert("Proszę podać Nazwisko!");
 document.formularz3.nazwisko.focus();
 return false;
 }
 
     if (document.formularz3.tel_dom.value == "") {
 alert("Proszę podać Telefon!");
 document.formularz3.tel_dom.focus();
 return false;
 }
 

 
 return true;
 }

function sprawdz1() {
	if (document.formularz.email.value.length < "6") {
	alert ("Podany adres e-mail jest niepoprawny");
	return false;
	}
	i = document.formularz.email.value.length - 1;
	if (document.formularz.email.value.charAt(i) == ".") {
	alert ("Podany adres e-mail jest niepoprawny");
	return false;
	}
	if (document.formularz.email.value.charAt(i) == "@") {
	alert ("Podany adres e-mail jest niepoprawny");
	return false;
	}
	if (document.formularz.email.value.charAt(0) == ".") {
	alert ("Podany adres e-mail jest niepoprawny");
	return false;
	}
	if (document.formularz.email.value.charAt(0) == "@") {
	alert ("Podany adres e-mail jest niepoprawny");
	return false;
	}
	}
	
function selecturl(s) {
	var gourl = s.options[s.selectedIndex].value;	window.top.location.href = gourl;
}	
/////////////////////////////////////////////////
//
// 	Oblsugiwane typy weryfikacji:
// 	dla typow:
//	    text			- required
//								- minlength
//								- numerical
//								- NIP
// 								- PESEL
// 								- kodPocztowy
//								- email
//								- www
//
//		  password 	- required
//					      - minlength
//
//		  checkbox  - required
//
//////////////////////////////////////////////////


//////////////////////////////////////////////////
//
// Glowna funkcja sprawdzajaca
//
//////////////////////////////////////////////////
function verify(f) {
	// zmienne
	var error_msg = ""; // tresc bledu ktora zostawnie wyswietlona dla elementu
	var no_errors = true;
	var supported_type = false;	// sprawdza czy element formularza jest obslugiwany przez skrypt
	var focus_set = false;
	
	var form_elements = new Array();
	var j = 0;
	// stworz tablice wszystkich elementow formularza
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		
		if(e.id != null && e.id != "" || e.style.display == 'none') {
			form_elements[j] = e;
			j++;
		}
	}
	
	// znajdz wszystkie elementy formularza i wyslij je do sprawdzenia
	for(var i = 0; i < form_elements.length; i++) {		//for(var i = 0; i < f.length; i++) {
		var e = form_elements[i];							//var e = f.elements[i];		
					
		switch(e.type) {
			case "text":
				error_msg = verify_text(e);
				supported_type = true;	// element jest obslugiwany
				break;
			case "password":
				error_msg = verify_password(e);
				supported_type = true;	// element jest obslugiwany
				break;				
			case "radio":
				error_msg = verify_radio(e);
				supported_type = true;	// element jest obslugiwany
				break;
			case "checkbox":
				error_msg = verify_checkbox(e);
				supported_type = true;
				break;
			default:
				error_msg = "";
				supported_type = false;	// element nie jest obslugiwany
				break;
		} //switch
		
		// jesli element jest obslugiwany
		if(supported_type) {				
			var id = e.getAttribute('id');
			var error_id = id + '_error';				
			
			// sprawdz czy byly bledy
			if(error_msg != "" && error_msg != null) {
				// jesli blad nie byl wczesniej wyswietlony stworz go
				if(document.getElementById(error_id) == null) {
					var error_element = '<div id="' + error_id + '" class="error">' + error_msg + '</div>';
					error_element +='<br id="br_' + error_id + '" class="clearboth">';
					var r = e.parentNode;
					var html = r.innerHTML;
					html += error_element;
					r.innerHTML = html;				
				}
				else {					
					document.getElementById(error_id).innerHTML = error_msg;
				}
			
				error_msg = "";
				no_errors = false;
				/*
				if(!focus_set) {
					e.focus();
					focus_set = true;
				}
				*/
			}
			// jesli nie bylo bledow
			else {
				// ale wczesniej byl, skasuj element
				if(document.getElementById(error_id) != null) {
					var parent = e.parentNode;
					parent.removeChild(document.getElementById(error_id));
					parent.removeChild(document.getElementById('br_' + error_id));
				}
			}
		} // end if(supported_type)
	}// end for
	
	return no_errors;
}			

//////////////////////////////////////////////////
//
// Funkcje sprawdzajace poszegolne elementy
//
//////////////////////////////////////////////////

///////////////
// type:'text'
///////////////
function verify_text(e) {
	var errors = "";
	
	// attr: required
	if(e.getAttribute("required") == "true") {
		if(verify_text_empty(e.value)) {
			errors += "<span style='color: #FF0000'>pole wymagane</span>";
			return errors;
		}
	}
	
	// jesli parametr nie jest pusty zweryfikuj go
	if(!verify_text_empty(e.value)) {
		
		// attr: minlength
		if(e.getAttribute("minlength") != null && e.getAttribute("minlength") != "") {
			if(e.value.length < e.getAttribute("minlength")) {
				errors += "Ten element musi mieÄ‡ przynajmniej " + e.getAttribute("minlength") + " znakĂłw.";
				return errors;			
			}
		}
		
		// attr: numerical
		if(e.getAttribute("validate") == "numerical") {
			for(var i=0; i<e.value.length; i++) {
				if(e.value.charAt(i) < "0" || e.value.charAt(i) > "9") {
					errors += "Ten element moĹĽe zawieraÄ‡ tylko wartoĹ›ci numeryczne.";
					return errors;				
				}
			}		
		}
		
		// attr: kodPocztowy
		if(e.getAttribute("validate") == "kodPocztowy") {
			var kodPocztowy = e.value;
			
			//usun '-' z podanego kodu
			kodPocztowy = kodPocztowy.replace(/-/g, "");
			
			if(kodPocztowy.length != 6) {
				errors += "Wprowadzony kod pocztowy ma zĹ‚Ä… dĹ‚ugoĹ›Ä‡.";
				return errors;
			}
			
			//sprawdz czy zostaly tylko liczby
			for(var i=0; i<kodPocztowy.length; i++) {
				if(kodPocztowy.charAt(i) < "0" || kodPocztowy.charAt(i) > "9") {
					errors += "Wprowadzony kod pocztowy jest niepoprawny.";
					return errors;
				}
			}
			
		}
		// attr: NIP
		if(e.getAttribute("validate") == "NIP") {
			var nip = e.value;
			
			//usun '-' z podanego NIPU
			nip = nip.replace(/-/g, "");
			
			if(nip.length != 10) {
				errors += "Wprowadzony NIP jest niepoprawny.";
				return errors;
			}
			
			//sprawdz czy zostaly tylko liczby
			for(var i=0; i<nip.length; i++) {
				if(nip.charAt(i) < "0" || nip.charAt(i) > "9") {
					errors += "Wprowadzony NIP jest niepoprawny.";
					return errors;
				}
			}
			
			//sprawdz czy pierwsze 3 liczby sa z przedzialu {111,998}
			var urzad = nip.substring(0,3);
			if (urzad < "111" || urzad > "998") {
				errors += "Wprowadzony NIP jest niepoprawny.";
				return errors;
			}
			
			// sprawdz cyfre kontrolna
			var crc = nip.charAt(9);
			var nip2 = nip.substring(0,9);
			var wagi = new Array(6,5,7,2,3,4,5,6,7);
			var suma = 0;
			var wynik = 0;
			
			for(var i=0; i<nip2.length; i++) {
				suma += nip2.charAt(i)*wagi[i];
			}		
			
			wynik = suma % 11;
			
			if(wynik == 10)
				wynik = 0;
				
			if(crc!=wynik) {
				errors += "Wprowadzony NIP jest niepoprawny.";
				return errors;
			}		
		} // end attr: NIP
		
		// attr: PESEL
		if(e.getAttribute("validate") == "PESEL") {
			var pesel = e.value;
			
			// sprawdz dlugosc
			if(pesel.length != 11) {
				errors += "Wprowadzony PESEL jest niepoprawny.";
				return errors;
			}
			
			//sprawdz czy zostaly tylko liczby
			for(var i=0; i<pesel.length; i++) {
				if(pesel.charAt(i) < "0" || pesel.charAt(i) > "9") {
					errors += "Wprowadzony PESEL jest niepoprawny.";
					return errors;
				}
			}
						
			// sprawdz cyfre kontrolna
			var crc = pesel.charAt(10);
			var pesel2 = pesel.substring(0,10);
			var wagi = new Array(1,3,7,9,1,3,7,9,1,3);
			var suma = 0;
			var wynik = 0;
			
			for(var i=0; i<pesel2.length; i++) {
				suma += pesel2.charAt(i)*wagi[i];
			}		
			
			wynik = suma % 10;
			
			if(wynik == 10)
				wynik = 0;
			else
				wynik = 10 - wynik;
				
			if(crc!=wynik) {
				errors += "Wprowadzony PESEL jest niepoprawny.";
				return errors;
			}		
		} // end attr: PESEL
		
		// attr: email
		if(e.getAttribute("validate") == "email") {
			if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(e.value) ) {;}
			else {
				errors += "Wprowadzony adres email jest niepoprawny.";
				return errors;
			}				
		}		
	} //if(!verify...
		
	return errors;
}

///////////////
// type:'password'
///////////////
function verify_password(e) {
	var errors = "";
	
	// attr: required
	if(e.getAttribute("required") == "true") {
		if(verify_text_empty(e.value)) {
			errors += "Ten element jest wymagany.";
			return errors;
		}
	}
	
	// jesli parametr nie jest pusty zweryfikuj go
	if(!verify_text_empty(e.value)) {
		
		// attr: minlength
		if(e.getAttribute("minlength") != null && e.getAttribute("minlength") != "") {
			if(e.value.length < e.getAttribute("minlength")) {
				errors += "Ten element musi mieÄ‡ przynajmniej " + e.getAttribute("minlength") + " znakĂłw.";
				return errors;			
			}
		}
	}
}

///////////////
// type:'radio'
///////////////
function verify_radio(e) {
	var errors = "";
	var no_errors = false;
	
	// required
	if (e.required) {
		for(var i=0; i<e.length; i++) {
			if(e[i].checked){
				no_errors = true;
				break;
			}
		}
		
		if(!ok) {
			errors += "Ten element jest wymagany";
		}		
	}		
	
	return errors;
}

function verify_checkbox(e) {
	var errors = "";
		
	// required
	if(e.getAttribute("required") == "true") {
		if(!e.checked) {
			errors += "Ten element jest wymagany";
			return errors;
		}
	}
	
	return errors;
}

function verify_text_empty(t) {
	var is_empty = false;
	var spaces = true;
	
	// sprawdz czy same spacje
	for(var i=0; i<t.length; i++) {
		if(t[i]!=" ")
			spaces = false;
			break;
	}
	
	if(t=="" || t==" " || spaces)
		is_empty = true;
		
	return is_empty;
}
	
   
