// JavaScript Document
var Rejestracja = {
	isEmpty:function(str){
		return (((/^(\s)+$/.test(str)))||(str==''));
	},
	isValidLogin:function(str){
		return (/^[0-9a-zA-Z_±ˇćĆęĘłŁńŃóÓ¶¦żŻĽ¬]+$/.test(str));
	},
	validate: function() {
		var login 			= $('rej_login');
		var haslo1 			= $('rej_haslo1');
		var haslo2 			= $('rej_haslo2');
		var imie 			= $('rej_imie');
		var nazwisko 		= $('rej_nazwisko');
		var telefon 		= $('rej_telefon');
		var email 			= $('rej_email');
		var przypomnienie 	= $('rej_przypomnienie');
		var ul 				= $('rej_ul');
		var nr_d 			= $('rej_nrd');
		var nr_m 			= $('rej_nrm');
		var kod 			= $('rej_kod');
		var miejscowosc 	= $('rej_miejscowosc');

		var r 	= {
			login: 				false,
			hasla_nie_puste: 	false,
			hasla_dlugosc: 		false,
			hasla_jednakowe: 	false,
			imie: 				false,
			nazwisko: 			false,
			telefon: 			false,
			email: 				false,
			rej_przypomnienie: 	false,
			ul: 				false,
			nr_d: 				false,
			nr_m: 				false,
			kod: 				false,
			miejscowosc: 		false
			};
		// login
		if(login){
			r.login 				= this.check_login($F(login));
			if(!r.login){
				alert('Nie wypełniłe¶ pola login.');
				Field.focus(login);
				return false;
			}
			r.login 				= this.isValidLogin($F(login));
			if(!r.login){
				alert('Pole login zawiera niedozwolone znaki.\nDozwolone s± duże i małe litery alfabetu, cyfry oraz znak "_".');
				Field.focus(login);
				return false;
			}
		} else {
			r.login 				= true;
		}
		// haslo
		if(haslo1&&haslo2){
			r.hasla_nie_puste 	= this.check_haslo_puste($F(haslo1),$F(haslo2));
			if(!r.hasla_nie_puste){
				alert('Nie podałe¶ jednego lub obu haseł.');
				Field.focus(haslo1);
				return false;
			}
			r.hasla_dlugosc 		= this.check_haslo_dlugosc($F(haslo1),$F(haslo2),4);
			if(!r.hasla_dlugosc){
				alert('Jedno lub oba hasła s± za krótkie.\nMinimalna długo¶ć hasła to 4 znaki.');
				Field.focus(haslo1);
				return false;
			}
			r.hasla_jednakowe 	= this.check_haslo_jednakowe($F(haslo1),$F(haslo2));
			if(!r.hasla_jednakowe){
				alert('Podane hasła nie s± jednakowe.');
				Field.focus(haslo1);
				return false;
			}
		} else {
			r.hasla_nie_puste 	= true;
			r.hasla_dlugosc 	= true;
			r.hasla_jednakowe 	= true;
		}
		// imie
		r.imie 				= this.check_imie($F(imie));
		if(!r.imie){
			alert('Nie wypełniłe¶ pola imię.');
			Field.focus(imie);
			return false;
		}
		// nazwisko
		r.nazwisko 			= this.check_nazwisko($F(nazwisko));
		if(!r.nazwisko){
			alert('Nie wypełniłe¶ pola nazwisko.');
			Field.focus(nazwisko);
			return false;
		}
		// telefon
		r.telefon 			= this.check_telefon($F(telefon));
		if(!r.telefon){
			alert('Podany numer telefonu jest nieprawidłowy.');
			Field.focus(telefon);
			return false;
		}
		// email
		r.email 				= this.check_email($F(email));
		if(!r.email){
			alert('Podany adres email jest nieprawidłowy.');
			Field.focus(email);
			return false;
		}
		// przypomnienie
		r.przypomnienie			= this.check_przypomnienie($F(przypomnienie));
		if(!r.przypomnienie){
			alert('Musisz podac odpowiedz na pytanie kontrolne.');
			Field.focus(przypomnienie);
			return false;
		}
		// ulica
		r.ul					= this.check_ulica($F(ul));
		if(!r.ul){
			alert('Musisz podac ulice w adresie dostawy.');
			Field.focus(ul);
			return false;
		}
		// nr domu
		r.nr_d					= this.check_nr_d($F(nr_d));
		if(!r.nr_d){
			alert('Musisz podac nr domu w adresie dostawy.');
			Field.focus(nr_d);
			return false;
		}
		// nr mieszkania
		r.nr_m					= this.check_nr_m($F(nr_m));
		if(!r.nr_m){
			alert('Musisz podac nr mieszkania w adresie dostawy. Jezeli nie posiadasz wpisz "-".');
			Field.focus(nr_m);
			return false;
		}
		// kod_pocztowy
		r.kod					= this.check_kod($F(kod));
		if(!r.kod){
			alert('Musisz podac poprawny kod pocztowy w adresie dostawy. (XX-YYY)');
			Field.focus(kod);
			return false;
		}
		// miejscowosc
		r.miejscowosc			= this.check_miejscowosc($F(miejscowosc));
		if(!r.miejscowosc){
			alert('Musisz podac miejscowosc w adresie dostawy.');
			Field.focus(miejscowosc);
			return false;
		}
		
		var wynik = (
			r.login && 
			r.hasla_nie_puste && 
			r.hasla_dlugosc && 
			r.hasla_jednakowe && 
			r.imie && 
			r.nazwisko && 
			r.telefon && 
			r.email && 
			r.przypomnienie &&
			r.ul &&
			r.nr_d &&
			r.nr_m &&
			r.kod &&
			r.miejscowosc
			);
		return wynik;
	},
	check_login: function(login){
		return (!this.isEmpty(login));
	},
	check_haslo_puste: function(haslo1, haslo2){
		return ((!this.isEmpty(haslo1))&&(!this.isEmpty(haslo2)));
	},
	check_haslo_dlugosc: function(haslo1, haslo2, dlugosc){
		return ((haslo1.length>=dlugosc)&&(haslo2.length>=dlugosc));
	},
	check_haslo_jednakowe: function(haslo1, haslo2){
		return (haslo1==haslo2);
	},
	check_imie: function(imie){
		return (!this.isEmpty(imie));
	},
	check_nazwisko: function(nazwisko){
		return (!this.isEmpty(nazwisko));
	},
	check_telefon: function(tel){
		return VALIDATOR.PHONE.verify(tel);
	},
	check_email: function(email){
		return VALIDATOR.MAIL.verify(email);
	},
	check_przypomnienie: function(przypomnienie){
		return (!this.isEmpty(przypomnienie));
	},
	check_ulica: function(ulica){
		return (!this.isEmpty(ulica));
	},
	check_nr_d: function(nr){
		return (!this.isEmpty(nr));
	},
	check_nr_m: function(nr){
		return (!this.isEmpty(nr));
	},
	check_kod: function(kod){
		return VALIDATOR.ZIP.verify(kod);
	},
	check_miejscowosc: function(miejscowosc){
		return (!this.isEmpty(miejscowosc));
	}
}

