

function sprawdz_konto(formularz)
{
  for (i = 0; i < formularz.length; i++)
  {
    var pole = formularz.elements[i];
    if ((pole.type == "text" || pole.type == "text" || pole.type == "text" || pole.type == "text") && pole.value == "")
    {
      alert("Proszę wypełnić wszystkie pola!");
      return false;
    }
  }
  return true;
}



function ShowOrHide(div) {
var d = $(div);
if (document.getElementById(div).className.indexOf("hide") != -1) {
Element.removeClassName( d, "hide" );
Element.addClassName( d, "show" );
} else
{
Element.removeClassName( d, "show" );
Element.addClassName( d, "hide" );
}
}

function putYoutube(pid) {
var d = $(video_place);	
d.innerHTML = "<object width=\"400\" height=\"330\"><param name=\"movie\" value=\""+pid+"&hl=pl&fs=1&rel=0&border=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><embed src=\""+pid+"&hl=pl&fs=1&rel=0&border=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"400\" height=\"330\"></embed></object>";
	
}


/** @file

Wspólne funkcje wykorzystywane w wielu miejscach w systemie
@author Paweł Wnuk
@author Adam
@date 2008.01
*/

/*function $(id) {
	return document.getElementById(id);
}*/ 

/* Komunikat błędu - AJAX */ 
function ajaxErrorMessage(msg) 
{
	var mm = 'Nie udało się odebrać odpowiedzi z serwera. Prosimy o kontakt z administratorem, '; 
	if (msg != '') 
		mm += 'przekazując mu następujący komunikat: '+msg; 
	else 
		mm += 'opisując mu przy jakiej czynności ten błąd się pojawił'; 
	alert(mm); 
}


/* Sprawdzanie poprawności adresu e-mail */ 
function checkEMail(adres) {
	if (adres != "") {
		var re = new RegExp("[^@]{1,}[@]{1}[^@.]{1,}[.]{1}[^@]{1,}","gi");
		var wynik = re.test(adres);
		if (wynik == true) {
			return true;
		}
		if (wynik == false) {
			return false;
		}
	}
}

/* Sprawdzenie poprawności kodu pocztowego */ 
function checkPostCode(kod)
{
//  var regex = /^[0-9]{2}\-[0-9]{3}$/;
  var regex = /^[0-9]{5}$/;
  return regex.test(kod);
}

/* Sprawdzenie poprawności wprowadzenia liczby całkowitej */ 
function checkIntNumber(liczba)
{
  var regex = /^[\-]{0,1}[0-9]{1,8}$/;
  return regex.test(liczba);
}


/* Sprawdzenie poprawności wprowadzenia liczby zmiennoprzecinkowej */ 
function checkRealNumber(liczba)
{
  var regex = /^[\-]{0,1}[0-9]{1,8}[.]{0,1}[0-9]{0,6}$/;
  return regex.test(liczba);
}

/* Sprawdzenie poprawności godziny */ 
function checkHour(godz)
{
  var regex = /^[0-9]{2}\:[0-9]{2}$/;
  return regex.test(godz);
}

function changeSpecialChars(txt)
{
  var arg = txt.replace(/</g,'&lt;');
  arg = arg.replace(/>/g,'&gt;');
  return arg; 
}

function hl2Item(item,action) {
	color='#AEAEAE';
	if (action==0) 
		color='#EBEBEB';
	item.style.background=color;
}

function hlOn() {
	this.style.background='#f3f3f3';
}

function hlOff() {
	this.style.background='#ffffff';
}

function changeOrder(form,item) {
	if (document.getElementById(form).order.value==item-1)
		iSort=(parseInt(document.getElementById(form).sort.value)+1)%2;
	else
		iSort = 0;
	document.getElementById(form).order.value=item-1;
	document.getElementById(form).sort.value=iSort;
	document.getElementById(form).submit();
}

function autoSubmit(field,e,script) {
	var keycode;

	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;
	else
		return true;

	if (keycode == 13) {
	    if (script)
			eval(script);
		else
			field.form.submit();
		return false;
	} else
		return true;
}

function oknoLogin() { 
	
	if ($('okno_login').value.length < 1) {
		window.alert('Brakuje nazwy użytkownika!');
		$('okno_login').focus();
		return;
	}
	$('okno_login_form').submit();
	
}

function popup(v,t,e,p){
	if(!document.createElement) return;
	p=document.getElementById('popup');
	if(!p){
		p=document.createElement('div');
		p.id='popup';
		p.style.zIndex=100;
		if(document.all&&!window.opera)
		    p.style.position='absolute';
		else
		    p.style.position='fixed';
		p.style.padding='3px';
		p.style.border='1px solid #000000';
		p.style.backgroundColor='#ffffff';
		p.style.MozOpacity='0.9';
		p.style.align='left';

	}
	p.move=function(e){
		e=e||event;
		p.style.left=e.clientX+5+"px";
		if(document.all&&!window.opera)
		    p.style.top=e.clientY+document.body.scrollTop+20+"px";
		else
		    p.style.top=e.clientY+20+"px";
	}
	p.hide=function(x){
		if(x=document.getElementById('popup')) document.body.removeChild(x)
	}
	p.move(e);
	p.innerHTML=t;
	document.body.appendChild(p);
	p.onmousedown=p.hide;
	v.onmouseout=p.hide;
}

function ShowOrHide(div) {
var d = $(div);
if (document.getElementById(div).className.indexOf("hide") != -1) {
Element.removeClassName( d, "hide" );
Element.addClassName( d, "show" );
} else
{
Element.removeClassName( d, "show" );
Element.addClassName( d, "hide" );
}
}

function changeUserType(tryb) {
	
	if (tryb < 1) {
		alert('Nieznany typ'); die();
	}

	new Ajax.Request('./act/set_users_tools.act.php?act=ch_disp', {
		method: 'GET',
		parameters: {'tryb': tryb},
		onCreate: function() { 

		},
		onSuccess: function(transport) {
			if (transport.responseText != 'ok') {
				alert('Błąd, Spróbuj jeszcze raz, lub skontaktuj się z administratorem. \n\nKomunikat serwera: '+transport.responseText); 
				$('form_sg_add_msg').innerHTML = "Wysyłanie... (błąd)";  
			} else {
				$('user_typ_form').submit();
			};
		},
		onFailure: function() { 
			ajaxErrorMessage('Błąd przy wysyłaniu do serwera'); 

		} 	
	});
	return true; 	
}



function makeDonation(user_id, user_imie, user_nazwisko, amount) {
	if (amount<=0){ alert('Zła kwota!'); return;}

	new Ajax.Request('./act/skr_tools.act.php', {
		method: 'POST',
		parameters: {'user_id': user_id, 'amount': amount, 'user_imie': user_imie, 'user_nazwisko': user_nazwisko},
		onCreate: function() { 
		},
		onSuccess: function(transport) {
			if (transport.responseText != 'ok') {
				alert('Błąd, Spróbuj jeszcze raz, lub skontaktuj się z administratorem. \n\nKomunikat serwera: '+transport.responseText); 
				$('message').innerHTML = "Wysyłanie... (błąd)";  
			} else {
				//$('message').innerHTML = "Zapisano w bazie";  
					document.getElementById(user_id).style.display='none';
			};
		},
		onFailure: function() { 
			ajaxErrorMessage('Błąd przy wysyłaniu do serwera'); 

		} 	
	});
	return true; 	
}


function isNumeric(input) {
   var fine = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < input.length && IsNumber == true; i++) 
      { 
      Char = input.charAt(i); 
      if (fine.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function getElementById(id) {
    if (document.all) {
	return document.getElementById(id);
    }
    for (i=0;i<document.forms.length;i++) {
	if (document.forms[i].elements[id]) {return document.forms[i].elements[id]; }
    }
    return null;
}

function insertAt(elementId, replaceString) {
  //inserts given text at selection or cursor position
  textarea = getElementById(elementId);
  var toBeReplaced = /text|page|area_name/;//substrings in replaceString to be replaced by the selection if a selection was done
  if (textarea.setSelectionRange) {
    //Mozilla UserAgent Gecko-1.4
    var selectionStart = textarea.selectionStart;
    var selectionEnd = textarea.selectionEnd;
    if (selectionStart != selectionEnd) { // has there been a selection
	var newString = replaceString.replace(toBeReplaced, textarea.value.substring(selectionStart, selectionEnd));
    	textarea.value = textarea.value.substring(0, selectionStart)
                  + newString
                  + textarea.value.substring(selectionEnd);
   }
    else  {// set caret
       textarea.value = textarea.value.substring(0, selectionStart)
                  + replaceString
                  + textarea.value.substring(selectionEnd);
     
    }
  }
  else if (document.selection) {
    //UserAgent IE-6.0
    textarea.focus();
    var range = document.selection.createRange();
    if (range.parentElement() == textarea) {
      var isCollapsed = range.text == '';
      if (! isCollapsed)  {
        range.text = replaceString.replace(toBeReplaced, range.text);
        range.moveStart('character', -range.text.length);
        range.select();
      }
	else {
		range.text = replaceString;
	}
    }
  }
  else { //UserAgent Gecko-1.0.1 (NN7.0)
   //alert("don't know yet how to handle insert" + document);
	}
}

function Show(div) {
var d = $(div);
Element.removeClassName( d, "hide" );
Element.addClassName( d, "show" );
}

function Hide(div) {
var d = $(div);
Element.removeClassName( d, "show" );
Element.addClassName( d, "hide" );
}


function addLink(nazwa, sciezka, opis, typ) 
{ 	
	if (nazwa.length < 1) {
 	 alert('Brakuje tytułu'); return;
	}
	if (sciezka.length < 1) {
 	 alert('Brakuje wstępu'); return;
	}
	if (opis.length < 1) {
 	 alert('Brakuje opisu'); return;
	}
	
	new Ajax.Request('./act/admin_tools.act.php?act=6', {
		method: 'post',
		parameters: {'nazwa': nazwa, 'sciezka': sciezka, 'opis' : opis, 'typ': typ },
		onCreate: function() { 
			$('form_msg').innerHTML = "Trwa wysyłanie danych ...";  
		},
		onSuccess: function(transport) {
			if (transport.responseText != 'ok') {
				alert('Błąd, Spróbuj jeszcze raz, lub skontaktuj się z administratorem. \n\nKomunikat serwera: '+transport.responseText); 
				$('form_msg').innerHTML = "Wysyłanie... (błąd)";  
			} else {
				alert('Dodano link.'); 
				$('form_msg').innerHTML = "Dodano link.";  
				$('add_link').submit();
			};
		},
		onFailure: function() { 
			ajaxErrorMessage('Błąd przy wysyłaniu haseł do serwera'); 
			$('form_msg').innerHTML = "Wysyłanie... (błąd)";  
		} 	
	});
	return true; 
}	
