//funkcja pozwala na otwieranie linkow
//"zewnetrznych" w nowym oknie
function external()
{
	//jezeli mozna pobrac elementy
	if (document.getElementsByTagName)
	{
		var a;
		//pobranie elementow a
		a = document.getElementsByTagName('a');
		for (var i=0; i < a.length; i++)
		{
			//jezeli jest atrybut href oraz rel="external" (link zewnetrzny)
			if (a[i].getAttribute('href') && a[i].getAttribute('rel') == 'external')
				//wtedy target bedzie _blank
				a[i].target = '_blank';
		}
	}
	else return;
}
//uruchomienie funkcji przy zaladowaniu strony
window.onload = external;


//funkcja ograniczna ilosc wpisywanych znakow w formularzu
function limit(what,howmuch)
{
	var string = what.value;
	
	//znaki ' " \ NULL beda zajmowaly dwa znaki
	//poniewaz trzeba do nich dodac backslash.
	//tutaj zamieniane sa na dwa dowolne znaki (22)
	//poniewaz dalej liczona jest tylko dlugosc stringa.
	string=string.replace(/\'/g,'22');
	string=string.replace(/\"/g,'22');
	string=string.replace(/\\/g,'22');
	string=string.replace(/\0/g,'22');
	
	//dlugosc nie moze byc wieksza niz HOWMUCH-1.
	//-1 podany jest na wypadek gdyby ostatni wpisany znak
	//byl znakiem podwojnym.
	if (string.length >= howmuch-1)
		return false;
}


//funkcja sprawdza czy poprawnie wypelniono pola formularza logowania
function check_login_form()
{
	//jezeli pole login jest puste
	if (document.login.user.value.length < 1)
	{
  		alert("Username not supplied");
  		document.login.user.focus();
  		return false;
 	}
 	
 	//jezeli pole pass jest puste
 	if (document.login.pass.value.length < 1)
 	{
  		alert("Password not supplied");
  		document.login.pass.focus();
  		return false;
 	}
}


//funkcja sprawdza czy poprawnie wypelniono pola formularza logowania
function check_toplogin_form()
{
	//jezeli pole login jest puste
	if (document.toplogin.user.value.length < 1)
	{
  		alert("Username not supplied");
  		document.toplogin.user.focus();
  		return false;
 	}
 	
 	//jezeli pole pass jest puste
 	if (document.toplogin.pass.value.length < 1)
 	{
  		alert("Password not supplied");
  		document.toplogin.pass.focus();
  		return false;
 	}
}

//funkcja sprawdza czy poprawnie wypelniono pola formularza prostego wyszukiwania
function check_simple_search_form()
{
	//jezeli pole search jest puste
	if (document.ssearch.search.value.length < 1)
	{
  		alert("You didn't specify what to search for");
  		document.ssearch.search.focus();
  		return false;
 	}
}


//funkcja sprawdza czy poprawnie wypelniono pola formularza zaawansowanego wyszukiwania
function check_advanced_search_form()
{
	//jezeli pole search jest puste
	if (document.asearch.search.value.length < 1)
	{
  		alert("You didn't specify what to search for");
  		document.asearch.search.focus();
  		return false;
 	}
 	
 	//elementy formularza
	var el = document.asearch.elements;
	//liczba wszystkich pol, z ktorych conajmniej jedno musi byc zaznaczone
	var count = 0;
	//liczba niezaznaczonych pol
	var empty = 0;
	
	//dla kazdego elementu...
	for(i=0; i < el.length; i++)
	{
		//...ktory jest checkboxem i nie okresla dokladnosci wyszukiwania
		if(el[i].type=="checkbox" && el[i].name!="wholewords" && el[i].name!="casesensitive")
		{
			//liczba takich elementow
			count++;
			//liczba pustych elementow
			if(el[i].checked==false) empty++;
		}
	}
	
	//jezeli wszystkie elementy sa puste
	if(count==empty)
	{
		alert("You didn't specify where to search");
		return false;
	}
}


//funkcja sprawdza czy poprawnie wypelniono pola formularza rejestracyjnego
function check_register_form()
{
	//jezeli pole username jest puste
	if (document.register.username.value.length < 1)
	{
  		alert("Username not supplied");
  		document.register.username.focus();
  		return false;
 	}
 	//jezeli pole pass jest puste
	if (document.register.pass.value.length < 1)
	{
  		alert("Password not supplied");
  		document.register.pass.focus();
  		return false;
 	}
 	//jezeli pole pass2 jest puste
	if (document.register.pass2.value.length < 1)
	{
  		alert("Password not repeated");
  		document.register.pass2.focus();
  		return false;
 	}
 	//jezeli pole email jest puste
	if (document.register.email.value.length < 1)
	{
  		alert("E-mail not supplied");
  		document.register.email.focus();
  		return false;
 	}
}


//funkcja sprawdza czy poprawnie wypelniono pola formularza w profilu
function check_profile_form()
{
	//jezeli wszystkie pola sa puste
	if (document.profile.email.value.length < 1 &&
		document.profile.pass.value.length < 1 &&
		document.profile.newpass.value.length < 1 &&
		document.profile.newpass2.value.length < 1)
	{
  		alert("Nothing supplied");
  		document.profile.email.focus();
  		return false;
 	}
 	
 	//przy probie zmiany email'a
	if (document.profile.email.value.length > 1 &&
		document.profile.pass.value.length < 1)
	{
  		alert("To change the e-mail please supply the password");
  		document.profile.pass.focus();
  		return false;
 	}
 	
 	//przy probie zmiany hasla
	if (document.profile.email.value.length < 1 &&
		document.profile.pass.value.length > 1 &&
		(document.profile.newpass.value.length < 1 ||
		 document.profile.newpass2.value.length < 1))
	{
  		alert("To change the password please supply and repeat the new one");
  		document.profile.newpass.focus();
  		return false;
 	}
 	
 	//przy probie zmiany hasla
	if (document.profile.pass.value.length < 1 &&
		(document.profile.newpass.value.length > 1 ||
		 document.profile.newpass2.value.length > 1))
	{
  		alert("To change the password please supply the current one");
  		document.profile.newpass.focus();
  		return false;
 	}
}


//funkcja sprawdza czy poprawnie wypelniono pola logowania admina
function check_admin_form()
{
	//nazwa admina
	if (document.adminlogin.adminuser.value.length < 1)
	{
  		alert("Username not supplied");
  		document.adminlogin.adminuser.focus();
  		return false;
 	}
 	
 	//haslo admina
	if (document.adminlogin.adminpass.value.length < 1)
	{
  		alert("Password not supplied");
  		document.adminlogin.adminpass.focus();
  		return false;
 	}
}

//funkcja sprawdza czy poprawnie wypelniono pola logowania admina
function check_client_app_form()
{
	//nazwa admina
	if (document.bananyapp.banname.value.length < 1)
	{
  		alert("Client name not supplied");
  		document.bananyapp.banname.focus();
  		return false;
 	}
 	
 	//haslo admina
	if (document.bananyapp.banversion.value.length < 1)
	{
  		alert("Client version not supplied");
  		document.bananyapp.banversion.focus();
  		return false;
 	}
}