function showPopup(url) {
    var wx;
	var wy;
    wx = (window.screen.width/2) - (200 + 10);
    wy = (window.screen.height/2) - (130 + 30);

    var w = window.open(url,"popup","status=no,height=320,width=400,resizable=no,left=" + wx + ",top=" + wy + ",screenX=" + wx + ",screenY=" + wy + ",scrollbars=yes");
    w.focus();
}

function $(id) {
	if (typeof id != "object") 
		id = document.getElementById(id);
	return id;
}

function Ajax() {
	this.request = null;

	this.initialize = function() {
		this.response = null;
		
		if (this.request == null) {
			if (typeof window.ActiveXObject != 'undefined' )
				this.request = new ActiveXObject("Microsoft.XMLHTTP");
			else 
				this.request = new XMLHttpRequest();
		}
		return this.request;
	}
	
	this.get = function(url,funkcja_wynikowa) {
		if (this.initialize() == null) return false;
		this.request.onreadystatechange = this.wynik(this,funkcja_wynikowa);
		this.request.open('GET',url,true);
		this.request.send(null);	
	}
	
	this.post = function(url,funkcja_wynikowa,tresc) {
		if (this.initialize() == null) return false;
		this.request.onreadystatechange = this.wynik(this,funkcja_wynikowa);
		this.request.open('POST',url,true);
		this.request.send(tresc);	
	}
	
	this.wynik = function(obAjax, funkcja_wynikowa) {
		return function() {
			if (obAjax.request.readyState == 4 && obAjax.request.status == 200) {
				funkcja_wynikowa(obAjax.request.responseText);
			}
		}
	}
}

function attachImg() {
	return function(wynik) {
		window.opener.location.reload();
	}
}

function wybierzObrazek(aktu,plik) {
	a = new Ajax();
	a.get('/adm/pzdj.php?aktu='+aktu+'&plik='+plik,attachImg());
}

function ml(usr,dmn) {
    document.write(
        '<a href="mailto:'+usr+'@'+dmn+'">'+usr+'@'+dmn+'</a>'
    );
}

function markdownEditResult(b) {
	return function(wynik) {
		document.getElementById(b).innerHTML = wynik;
	}
}

var _stara_wartosc_md = '';
function markdownEditEventTimeoutGen(a,b) {
	return function() {
		t = document.getElementById(a);
		if (t.value != _stara_wartosc_md) {
			_stara_wartosc_md = t.value;
			req = new Ajax();
			req.post('/mkdn.php',markdownEditResult(b),document.getElementById(a).value);
		}
	}
}

var _markdown_ta_id;
function markdownEditEventGen(a,b) {
	return function(evt) {
		window.clearTimeout(_markdown_ta_id);
		_markdown_ta_id = window.setTimeout(markdownEditEventTimeoutGen(a,b),500);
	}
}

function markdownEdit(a,b) {
	ta = document.getElementById(a);
	_markdown_ta_id = window.setTimeout(markdownEditEventTimeoutGen(a,b),1000);
	ta.onkeyup = markdownEditEventGen(a,b);
}

function dateUpdateDays(id) {
	var dni=new Array;
	dni=[[31,28,31,30,31,30,31,31,30,31,30,31],[31,29,31,30,31,30,31,31,30,31,30,31]];
	
	var rok=document.getElementById(id+'_y').value;
	var miesiac=document.getElementById(id+'_m').value;

	var p;
	if (rok % 400 == 0 || (rok % 4==0 && rok % 100 != 0)) p=1; else p=0;
	var d=dni[p][miesiac-1];

	var l=document.getElementById(id+'_d');
	var s = l.selectedIndex;
	l.options.length=0;
	var j=0;
	var i;
	for (i=1;i<=d;i++) l.options[j++]=new Option(i,i);
	if (s < l.options.length) l.selectedIndex = s; else l.selectedIndex = l.options.length-1;
}

function dateUpdateChange(id) {
	dateUpdateDays(id)
	return true;
}

function fillYear(id,rod,rdo) {
	var l=document.getElementById(id+'_y');
	l.options.length=0;
	var j=0;
	var i;
	for (i=rdo;i>=rod;i--) l.options[j++]=new Option(i,i);
	l.selectedIndex=0;
}

function fillDate(id,rod,rdo) {
	fillYear(id,rod,rdo);
	var l=document.getElementById(id+'_m');	
	var n;
	l.options.length=0;
	var j=0;
	var i;
	for (i=1;i<=12;i++) {
		n=i+''
		if (i < 10) n='0'+n;
		l.options[j++]=new Option(n,i);
	}
	l.selectedIndex=0;
	dateUpdateDays(id);
}
//z defaultami
function fillDate2(id) {
	var d = new Date();
	fillDate(id, d.getFullYear()-100, d.getFullYear());
}

function dateSet(id,r,m,d) {
	document.getElementById(id+'_y').value=r
	document.getElementById(id+'_m').value=m
	dateUpdateDays(id)
	document.getElementById(id+'_d').value=d
}

//z defaultami
function dateSet2(id) {
	var d = new Date();
	dateSet(id, d.getFullYear(), d.getMonth()+1, d.getDate());
}

function zmien_status(pole, klient, status) {
	if (status == undefined) return;
	var req = new Ajax();
	req.get('/adm/status.php?pole='+pole+'&klient='+klient+'&status='+status, function() {});
}
