var ayIe=document.all;
var ayKeyPressSpecWork=false;

String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g,'');
}
String.prototype.ltrim = function(){
	return this.replace(/^\s+/,'');
}
String.prototype.rtrim = function(){
	return this.replace(/\s+$/,'');
}
String.prototype.replaceAll = function(pcFrom,pcTo){
	var i = this.indexOf(pcFrom);
	var c = this; 
	while (i > -1){
		c = c.replace(pcFrom, pcTo); 
		i = c.indexOf(pcFrom);
	}
	return c;
}

function ayAnimY(idobj,y,fnk){
  this.end=y;
  this.fnk=fnk;
  this.obj=ayId(idobj);
  this.obj.style.zIndex=1000;
  this.y=ayObjStyleI(this.obj,'top');
  this.req=5;
  ayAnimYh();
}
function ayAnimYh(){
  if (this.y<this.end){
    this.obj.style.zIndex=1;
    return this.fnk();
  }
  this.req++;
  this.y=this.y-this.req;
  this.obj.style.top=this.y+'px';
  setTimeout('ayAnimYh()',10);
}

function ayBrowserCss(url){
	var t=new Array('moz,webkit');
	var n=(navigator.userAgent).toLowerCase();
	for(var i=0;i<t.length;i++)
		if (n.indexOf(t[i]) != -1)
			return document.write('<link href="'+url+t[i]+'.css" rel="stylesheet" type="text/css" />');
	return document.write('<link href="'+url+'opera-ie.css" rel="stylesheet" type="text/css" />');
}

//
//wspolrzedne
//

//zwraca left dla elementu o podanej dlugosci tak aby byl wycentrowany 
function ayCenterX(w){
  return  (document.documentElement.clientWidth-w)/2+ayScrollX();
}
//zwraca top dla elementu o podanej wysokości tak aby byl wyśrodkowany w pionie
function ayCenterY(h){
  return ((document.documentElement.clientHeight-h)/2)+ayScrollY();
}

//
//checkboxy
//

//sparawdza czy chociaz jeden z checkboxow zostal zaznaczony
function ayChecksChecked(obj){
  var ret=0;
  var tab=obj.getElementsByTagName('input');
  for(i = 0; i < tab.length; i++)
    if ((tab[i].type=='checkbox')&&(tab[i].checked==true))
    return true;
  return false; 
}
//sparawdza czy chociaz jeden z checkboxow zostal zaznaczony, jezeli nie wyswietla komunikat 
function ayChecksCheckedMsg(obj){
  if (ayChecksChecked(obj)<1){
    alert('Nie zaznaczono zadnej pozycji...!');
    return false;
  } 
  return true;
}
//submituje formularz z checkboxami (obj - obiekt formularza)
function ayChecksFormSubmit(obj,action,target){
  if (ayChecksCheckedMsg(obj)){  
    obj.target=target;  
    obj.action=action;
    obj.submit();
  }
}
//submituje formularz z checkboxami (obj - obiekt formularza) do nowego okna o podanych wymiarach
function ayChecksFormSubmitOnClick(obj,width,height){
if (ayChecksChecked(obj))
  ayWindow('',width,height,obj.name); 
}

//
//Cookie
//

//ustawianie i pobieranie ciasteczek
function ayCookieSet(name,value,expires,path,domain,secure){
	var today = new Date();
	today.setTime(today.getTime());
	(expires) ? expires = expires * 1000 * 60 * 60 * 24:0;
	var expires_date = new Date(today.getTime()+(expires));
	document.cookie = name+'='+escape(value) +
		((expires) ? ';expires='+expires_date.toGMTString() : '') + //expires.toGMTString()
		((path) ? ';path=' + path : '') +
		((domain) ? ';domain=' + domain : '') +
		((secure) ? ';secure' : '');
}
function ayCookieGet(c_name){
	if (document.cookie.length<1) return '';
	c_start=document.cookie.indexOf(c_name+'=');
	if (c_start==-1) return '';
	c_start=c_start + c_name.length+1;
	c_end=document.cookie.indexOf(';',c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
}

//
// dateSel - to element z trzema selectorkami (rok, miesiac, dzien)
//

// zeruje
function ayDateSelClear(name){
  ayDateSelVal(name,'','','');     
}
// ustawia aktualna date 
function ayDateSelSetNow(name){
  n = new Date();
  ayDateSelVal(name,n.getFullYear(),n.getMonth()+1,n.getDate());  
}
// ustawia rok,miesiac dzien
function ayDateSelVal(name,y,m,d){
  if (document.getElementById(name+'_d'))
    document.getElementById(name+'_d').value=d;
  if (document.getElementById(name+'_m'))
    document.getElementById(name+'_m').value=m;
  if (document.getElementById(name+'_y'))
    document.getElementById(name+'_y').value=y;        
}

//
//Formularze
//
var ayFormValid=true;

function ayFormValBeg(){
	ayFormValid=true;
}
function ayFormVal(e,valid){
	ayFormElementSel(e,!valid);
	(valid) ? 0:ayFormValid=false;
}
function ayFormValEnd(){
	(ayFormValid) ? 0 : alert("Popraw zaznaczone pola");
	return ayFormValid;
}
function ayFormValObligs(ids){
	if (ids=='')
		return;
	var t=ids.split(',');
	for(var i=0;i<t.length;i++){		
		var v=ayFormElement2value(t[i]);
		ayFormVal(ayId(t[i]),(v!=''));
	}
}

//uniwersalna funkcja do zwracania wartosci kazdego elementu formularza
function ayFormElement2value(id){
	var e=document.getElementById(id);
	var t=e.type;
	if(t=='checkbox')
		if (e.checked) return '1'; else return '';
	if(t=='textarea')
		return e.value;
	if(t=='select-multiple')
		return aySelectedChecked2Coma(e.options);
	if(e.value)
		return e.value;
	return aySelectedChecked2Coma(e.getElementsByTagName('input'));
}

function ayFormElementSel(e,bool){
	(bool) ? e.className='tsel' : e.className='t';
}

function ayFunctionExists(fnk){
	if (typeof fnk == 'string')
		return (typeof this.window[fnk] == 'function');
	return false;
}

//parsuje do inta, w przypadku bledu zwraca 0
function ayInt(val){
  var v=parseInt(val);
  (v) ? 0:v=0;
  return v;
}

//
//Zwraca element o podanym id
//
function ayId(id){
  return document.getElementById(id);  
}
function ayIdDisplaySet(id,bool){
  (bool) ? document.getElementById(id).style.display=''   : document.getElementById(id).style.display='none';   
}
function ayIdInner(id){
  return document.getElementById(id).innerHTML;  
}
function ayIdInnerSet(id,val){
  document.getElementById(id).innerHTML=val;  
}
function ayIdStyleSet(id,style){
  var e=ayId(id);
  e.setAttribute('style',style);
}

function ayIdVal(id){
  return document.getElementById(id).value;  
}
function ayIdValSet(id,val){
  document.getElementById(id).value=val;  
}

function ayIdNodes(id,tag){
  var e=document.getElementById(id);
  return e.getElementsByTagName(tag);
}
function ayIdNodesClass(id,tag,className){
  var e=document.getElementById(id);
  var all=e.getElementsByTagName(tag);
  var res=[];
  for(var i=all.length;i>0;i--)
    (all[i-1].className==className) ?  res.push(all[i-1]) :0;
  return res;
}


function ayIndexOf(tab,search){
  if (!ayIe)
    return tab.indexOf(search);
  var res=false;
  for(var i=0;i<tab.length;i++)
    if (tab[i]==search)
      res=i;
  return res;
}

//
// Pomaga rozwiazac problem z odczytem przycisniecia klawiszy gora/dol na zdarzenie keypress
// W Niektórych przegladarkach zdarzenie kp nie jest generowane dla klawiszy specjalnych gora i dol
// Trzeba wtedy rowniez podlaczyc zdarzenie onkeyup.
// np:
// <input onkeypress="resize(event,1)" onkeyup="resize(event,0)"
// function resize(e,keypress) { var key=ayKeyPressSpec(e,keypress) ... }
//
function ayKeyPressSpec(e,keypress){
	(e) ? 0 : e=event;   	
	var key=e.keyCode;
	var spc=((key==38)||(key==40));
	((keypress)&&(spc)) ? ayKeyPressSpecWork=true:0;
	
	//na keypress puszczamy tylko specjalne, jezeli przegladarka obsluguje "specjalne na keypress"
	//na keyup zwykle, i specjalne, chyba ze specjalne puszczono juz za pomoca keypress
	if ((keypress==1)&&(spc))
		return key;
	if ((keypress==0)&&((!spc)||(!ayKeyPressSpecWork)))
		return key;
	return false;
} 

//
// Wsparcie dla phpowej klasy ayList
//

//akcja na klikniecie wiersza
function ayListClick(obj){
  ayListSelectRow(obj,obj.className!='selected');
}
//pyta czy usunac zaznaczone rekordy i submituje formularz
function ayListDelConfirm(form,name){
	if ((ayChecksCheckedMsg(form))&&(confirm('Czy na pewno usunąć?'))){
		document.getElementById('aylistdel_'+name).value=1;
		form.target='_self';
		form.method='post';
		form.submit();
	}
}

//zaznacza/odznacza wiersz 
function ayListLastEditSet(list,id){
	ayCookieSet('ayListId'+list,id,3600,'/');
}

//zaznacza/odznacza wiersz 
function ayListSelectRow(obj,selected){
	var inputs=obj.getElementsByTagName('input');
	(inputs[0]) ? inputs[0].checked=selected:0;
	if (!obj.getAttribute('classbak')){
		var attr = document.createAttribute('classbak');
		attr.value = obj.className;
		obj.setAttributeNode(attr);
	}  
  (selected) ? obj.className='selected' : obj.className=obj.getAttribute('classbak');
}
//zaznacza/odznacza wszystkie wiersze na liscie (fromRow ustawiamy na inne niz zero kiedy mamy kilka wierszy naglowka) 
function ayListSelectAll(checked,fromRow,id){
	(id) ? 0 :id='ayFlist';
	if (document.getElementById(id)){
		obj=document.getElementById(id);
		var tab=obj.getElementsByTagName('tr');
		var len=tab.length;
		(fromRow) ? 0 : fromRow=3;
		(fromRow==1) ? len=len-1:0;
		for(var i = fromRow; i < len; i++)
			(tab[i].className!='sum') ? ayListSelectRow(tab[i],checked):0;
	}
	else
		alert('Brak obiektu o id:'+name)
}

//
//Rozne
//

//narysuj menu w htmlu, a ta funkcja zaznaczy link pasujacy do bierzacego url-a.
// (ustawi mu i elementowi nadrzednemu classe o nazwie "sel")
function ayMenuByLocation(id){
	var l=document.location+'';
	var e=document.getElementById(id);
	var all=e.getElementsByTagName('a');
	var selNr=0;
	var selLen=0;  
	for(var i=0;i<all.length;i++){
		var s=all[i].href;
		if ((l.indexOf(s)>-1)&&(s.length>selLen)){
		  selNr=i;
		  selLen=s.length;    
		}
	}
	if(selLen>0){
		all[selNr].className='sel';
		all[selNr].parentNode.className='sel';
	}
}

function ayObjStyle(obj,key){
	if (obj.currentStyle)
		return obj.currentStyle[key];    
	return document.defaultView.getComputedStyle(obj,null)[key];
}
function ayObjStyleI(obj,key){
	return ayInt(ayObjStyle(obj,key));
}

//obliczaja laczna dlugosc(wysokosc) dodatkowa obiektu na ktora skladaja sie: paddingi,marginesy i bordery
//uwaga zerowy border to medium border
function ayObjPMBWidth(o){
	return ayObjStyleI(o,'paddingLeft')+ayObjStyleI(o,'paddingRight')+ayObjStyleI(o,'marginLeft')+ayObjStyleI(o,'marginRight')+ayObjStyleI(o,'borderLeftWidth')+ayObjStyleI(o,'borderRightWidth');
}
function ayObjPMBHeight(o){  
	return ayObjStyleI(o,'paddingTop')+ayObjStyleI(o,'paddingBottom')+ayObjStyleI(o,'marginTop')+ayObjStyleI(o,'marginBottom')+ayObjStyleI(o,'borderTopWidth')+ayObjStyleI(o,'borderBottomWidth');  
}

//ustawia przezroczystosc dla podanego elementu
function ayOpacity(id,value){
	if (ayIe) 
		document.getElementById(id).style.filter='alpha(opacity=' + value*100 + ')';
	else {
		document.getElementById(id).style.opacity=value;
		document.getElementById(id).style.zIndex=10;    
	}
}

function ayPostCode2City(idpostcode){
	var s=ayIdVal(idpostcode)+'';
	if ((s.substring(2,3)=='-')&&(s.substring(6,7)==' ')){
		ayIdValSet(idpostcode,s.substring(0,6));
		return s.substring(7,s.length);
	}
	return '';
}

function ayPostVal(url,key,val){
	var e=ayId('ayPostFormHidden');
	e.name=key;
	e.value=val;
	e.parentNode.action=url;
	e.parentNode.submit();
}

//Losuje znak (A-z,0-9)
function ayRandomChar(){
	while (true){
		var r=Math.random();
		r=parseInt(r*1000);
		r=(r%74)+48;
		if ((r<58)||((r>64)&&(r<91))||(r>96)) 
			return String.fromCharCode(r);
	}
	return false;
}

//Losuje ciag znakow o podanej dligosci (A-z,0-9)
function ayRandomString(length){
	var s='';
	for (i=0;i<length;i++)
		s=s+ayRandomChar();
	return s;
}

//okresla zescrolowanie strony przez uzytkownika w x i y
function ayScrollX(){
	return (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;		
}
function ayScrollY(){
	return (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;		
}

//Zwraca zaznaczenia z selectow i chceboxow do postaci textu po przecinku
function aySelectedChecked2Coma(t){
	var r='';
	for(var i=0;i<t.length;i++)
		if ((t[i].selected)||(t[i].checked))
			r=r+t[i].value+',';
	return r.substring(0,r.length-1);
}

function ayNodeOffset(cur,n,tlength){
  var s=(n+'').substring(0,1);                               
  (s=='=') ? n=parseInt(n) : n=cur+parseInt(n);  
  (n<0) ? n=(tlength-1):0;  
  (n>=tlength) ? n=0:0;
  return n;
}

//Ustawia classe n-tego elementu na selected
//n: 1 kolejny, -1 poprzedni, =4 dokładnie 4.
function ayNodeSelect(id,tag,n){
	var t=ayIdNodes(id,tag);
	var cur=0;                                    
	for(var i=0;i<t.length;i++){  
		(t[i].className=='selected') ? cur=i:0;
		t[i].className='';
	}
	var nr=ayNodeOffset(cur,n,t.length);
	t[nr].className='selected';
	return t[nr];
}

//Pokazuje n-ty element i ukrywa pozostale
//n: 1 kolejny, -1 poprzedni, =4 dokładnie 4.
function ayNodeShow(id,tag,n,prevId,nextId){
	var t=ayIdNodes(id,tag);
	var cur=0;                                    
	for(var i=0;i<t.length;i++){  
		(t[i].style.display!='none') ? cur=i:0;
		t[i].style.display='none';
	}
	var nc=ayNodeOffset(cur,n,t.length);	
	(prevId) ? ayIdDisplaySet(prevId,(nc!=0)) :0;
	(nextId) ? ayIdDisplaySet(nextId,(nc!=(t.length-1))) :0;	
	t[nc].style.display='';
  return nc;    
}

//
// pobiera wspolrzedne X i Y okienka
//
function ayScreenX(obj){
	if (obj.screenX)
		return obj.screenX;
	return obj.screenLeft;
}
function ayScreenY(obj){
	if (obj.screenY)
		return obj.screenY;
	return obj.screenTop-50;
}

//Pokazuje i ukrywa element o podanym id (Na przemian)
//dodatkowo zwraca +/- jako pokaz/ukryj
function ayShowHide(id){
	obj=document.getElementById(id);
	if (obj.style.display!='none'){
		obj.style.display='none'
		return '+';
	}
	obj.style.display=''  
	return '-';
}

//substring bez potrzeby konwersji do stringa wczesniej
function ayStrLeft(val,to){
  var s=val+'';
  (to<0) ? to=s.length+to+1:0;  
  return s.substring(0,to-1);
}
function ayStrRight(val,len){
  var s=val+'';
  (len<0) ? len=s.length+len:0;
  return s.substring(s.length-len,s.length);  
}

//funkcje do obslugi uploadu tinymce
/*
ayTinyUploadImgUrl=',klubokazji,web,uploads,tiny'
ayTinyUploadImgDir='web,uploads,tiny'
width : "100%",    
editor_selector : "mceAdvanced",
convert_urls : false,  
file_browser_callback : 'ayTinyUpload',
*/
function ayTinyUploadBack(win,value){    
	win.document.getElementById('src').value=value;
	win.document.getElementById('alt').value='obrazek';
	win.ImageDialog.showPreviewImage(value);
}  
function ayTinyUpload(field_name,url,type,win){
	var d=win.document;
	if (!d.getElementById('aytinyuload')){
		d.getElementById('srclabel').parentNode.style.verticalAlign='top';
		d.getElementById('srcbrowsercontainer').parentNode.style.verticalAlign='top';
		var tdir=ayTinyUploadDir.replace(/\//g,",");
		var turl=ayTinyUploadUrl.replace(/\//g,",");
		d.getElementById('src').parentNode.innerHTML+='<div><iframe id="aytinyuload" frameborder="0" style="width:265px;height:20px;overflow:hidden" src="'+ayAjaxUrl+'upload:ayTinyUploadBack;'+tdir+';'+turl+'"></iframe></div>';
	}
}  

//otwiera nowe okno o podanych wymiarach laduje podanego url-a
//jezeli chcesz zasubmitowac formularz do nowego okna uzyj skladni ayWopen('',800,600,form_id) 
function ayWindow(url,w,h,ref,sub){
	(!ref) ? ref='ayRef':0;	
	var x=ayCenterX(w);
	var y=ayCenterY(h);	
	(sub) ? x+=ayScreenX(self):0;
	(sub) ? y+=ayScreenY(self):0;
	win = window.open(url,ref,'height='+h+',width='+w+',left='+x+',top='+y+',resizable,scrollbars');
}
function ayWindowAjax(url,w,h,ref){
	(!ref) ? ref='ayRefAjax':0;
	ayWindow(ayAjaxUrl+(url.replace(/\//g,",")),w,h,ref,true);
}
function ayWindowSub(url,w,h,ref){
	(!ref) ? ref='ayRefSub':0;	
	ayWindow(url,w,h,ref,true);
}

function ayListWindow(id,width,height,listname,url){
	var burl=e=ayId(listname+'_url').innerHTML;
	ayListLastEditSet(listname,id);
	url=url+'';
	var b=url.substring(url.length-1,url.length);
	(b=='@') ? url=url.substring(0,url.length-1)+'/'+id+'/'+burl:0;
	ayWindow(url,width,height);
}

