function form_change_action(form_id, newaction) {
	var f = $(form_id);
	var faction = f.action;
	var przec = faction.indexOf(',');
	if (przec == -1)
		return;
	var part1 = faction.substr(0, przec + 1);
	var part2 = faction.substr(przec + 1);
	przec = part2.indexOf(',');
	if (przec == -1) {
		f.action = part1 + newaction;
		return;
	}
	part2 = part2.substr(przec);
	f.action = part1 + newaction + part2;
}

function pwcalert(msg,okfunc)
{
	Windows.overlayShowEffectOptions = {duration: 0.2};
	Windows.overlayHideEffectOptions = {duration: 0.2};
	Dialog.alert("<span class=\"txt_bold_red_big\">" + msg + "</span>", {windowParameters: {className: "greylighting"},hideEffectOptions: {duration: 0.2},showEffectOptions: {duration: 0.2},width:300, height:100, okLabel: "OK",onOk:okfunc,showEffectOptions: {afterFinish: function() {$$(".ok_button")[0].focus();}} });
	WindowCloseKey.init();
}

function pwcconfirm(msg,onokfunc)
{
	Windows.overlayShowEffectOptions = {duration: 0.2};
	Windows.overlayHideEffectOptions = {duration: 0.2};
	Dialog.confirm("<span class=\"txt_bold_red_big\">" + msg + "</span>", {windowParameters: {className: "greylighting"},hideEffectOptions: {duration: 0.2},showEffectOptions: {duration: 0.2},width:300, height:100, okLabel: "Tak",cancelLabel: "Nie",onOk: onokfunc,showEffectOptions: {afterFinish: function() {$$(".ok_button")[0].focus();}} });
}

function ajax_post(form_id)
{
$(form_id).request({
  onSuccess: function(transport){
     var json = '';
	try {
		json = transport.responseText.evalJSON();
	} catch (e) {
		var rt = transport.responseText;
		rt = rt.substr(0, rt.lastIndexOf(']')+1);
		transport.responseText = rt;
		json = transport.responseText.evalJSON();
	}
	 ajax_deal_with_response(json);
	 div_loading_hide();
   },
   onLoading: div_loading_show,
   onComplete: div_loading_hide
});
}

function div_loading_show()
{
	var width = 300;
    var height = 300;
    var left = parseInt((screen.width/2) - (width/2));
    var top = parseInt((screen.height/2) - (height/2));
    top -= 70;
    if (window.pageYOffset) {
    	top += window.pageYOffset;
    } else if (document.documentElement.scrollTop) {
    	top += document.documentElement.scrollTop;
    }
    $('content_loading').style.width = width + 'px';
    $('content_loading').style.height = height + 'px';
    $('content_loading').style.top = top + 'px';
    $('content_loading').style.left = left + 'px';
 	$('content_loading').show();
}

function div_loading_hide()
{
 	$('content_loading').hide();
}

function ajax_deal_with_response(json) {
	var jar = json.toArray();
	var dkf = jar.length;
	div_loading_hide();
	for (var i = 0; i < dkf; i++) {
		if (jar[i].nstyleonly) {
			if (jar[i].nstyle) {
				var st = jar[i].nstyle;
				$(jar[i].div).setStyle(st);
			}
		} else if (jar[i].jscode) {
			eval(jar[i].content);
		} else if (jar[i].append != 0) {
			if (jar[i].append == -1) {
				$(jar[i].div).innerHTML = jar[i].content + $(jar[i].div).innerHTML;
			} else {
				$(jar[i].div).insert(jar[i].content);
			}
			if (jar[i].nstyle) {
				var st = jar[i].nstyle;
				$(jar[i].div).setStyle(st);
			}
		} else {
			$(jar[i].div).update(jar[i].content);
			if (jar[i].nstyle) {
				var st = jar[i].nstyle;
				$(jar[i].div).setStyle(st);
			}
		}
	 }
}

function ajax_get(file, params) {
	var url = file;
	if (params)
		url += '?act=' + params;
new Ajax.Request(url, {
  method: 'get',
  evalScripts : true,
  onSuccess: function(transport) {
  	var json = '';
	try {
		json = transport.responseText.evalJSON();
	} catch (e) {
		var rt = transport.responseText;
		rt = rt.substr(0, rt.lastIndexOf(']')+1);
		transport.responseText = rt;
		json = transport.responseText.evalJSON();
	}
	ajax_deal_with_response(json);
	div_loading_hide();
  }
});
}

function str_pad(str, len,padwith) {
	str = str + '';
	if (!padwith)
		padwith = '0';
	if (str.length >= len)
		return str;
	while(str.length < len)
		str = padwith + str;
	return str;
}


// onKeyPress="return is_num(event)"
function is_num(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode;
	return is_num_func(unicode);
}

function is_num_func(unicode)
{
	if (unicode != 8 && unicode != 9) {
	if (unicode < 48 || unicode > 57)
		return false;
	}
	return true;
}

// onBlur="is_num_check(this)"
function is_num_check(obj)
{
	var str = obj.value;
	var nstr = '';
	for (var i = 0; i < str.length; i++) {
		if (is_num_func(str.charCodeAt(i))) {
			nstr += str.charAt(i);
		}
	}
	obj.value = nstr;
}

// onKeyPress="return is_alpha(event)"
function is_alpha(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode;
	return is_alpha_func(unicode);
}

function is_alpha_func(unicode)
{
	if (unicode != 8 && unicode != 9) {
	if (!((unicode >= 48 && unicode <= 57) || (unicode >= 65 && unicode <= 90) || (unicode >= 97 && unicode <= 122)))
		return false;
	}
	return true;
}

// onKeyPress="return nospace(event)"
function nospace(e)
{
	var unicode = e.which ? e.which : e.keyCode;
	if (unicode == 32) {
		return false;
	}
	return true;
}

function check_mail(mail)
{
	var pattern = '^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'+'@'+
                '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'+
                '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$';
		return mail.search(pattern) == -1 ? false : true;
}

// onKeyPress="return is_num2(event, this)"
function is_num2(e, obj)
{
	var k = obj.value.indexOf('.');
	var prz = obj.value.indexOf(',');
	var check = (k != -1) ? true : false;
	if (!check)
		check = (prz != -1) ? true : false;
	var unicode = e.charCode ? e.charCode : e.keyCode;
	return is_num2_func(unicode, check);
}

function is_num2_func(unicode, check)
{
	if (unicode != 8 && unicode != 46 && unicode != 44 && unicode != 9) {
	if (unicode < 48 || unicode > 57)
		return false;
	} else if ((unicode == 46 || unicode == 44) && check) {
		return false;
	}
	return true;
}

// onKeyUp="digits(this, 2)" 
function digits(obj, dec)
{
	var k = obj.value.indexOf('.');
	var prz = obj.value.indexOf(',');
	var check = (k != -1) ? true : false;
	if (!check) {
		check = (prz != -1) ? true : false;
		if (check)
			k = prz;
	}
	if (check) {
		var le = obj.value.length - k - 1;
		if (le > dec) {
			obj.value = obj.value.substr(0, k + dec + 1);
			return true;
		}
	}
}

function is_num2_digits_check(obj, dec)
{
	var str = obj.value;
	var nstr = '';
	var kropka = false;
	for (var i = 0; i < str.length; i++) {
		if (is_num2_func(str.charCodeAt(i), kropka)) {
			nstr += str.charAt(i);
		}
		if (kropka == false && (str.charAt(i) == '.' || str.charAt(i) == ',')) {
			kropka = true;
		}
	}
	nstr = nstr.replace(',','.');
	obj.value = nstr;
	if (dec >= 0)
		digits(obj, dec);	
}

function del_item(id, file_name, action, msg)
{
	if (!msg) {
		msg = 'Czy na pewno usun±æ rekord?';
	}
	linkd = file_name+'?act='+action+'&' + 'id='+id;
	pwcconfirm(msg, del_go);
}

function del_go()
{
	document.location=linkd;
}

function del_item_ax(id, file_name, action, msg)
{
	if (!msg) {
		msg = 'Czy na pewno usun±æ rekord?';
	}
	act = '?act=' + action + '&id=' + id;
	file_name2 = file_name;
	pwcconfirm(msg, del_go_ax);
}

function del_go_ax()
{
	idzdo(file_name2, act);
	return true;
}

// uzywac tylko w formularzach gdzie na pewno nie wystapia znaki powodujace problemy takie jak /
function form_with_get_send(form_id)
{
	var f = $(form_id);
	var flink = f.action;
	for (var i = 0; i < f.length; i++) {
		var el = f[i];
		if (el.type == 'hidden' || el.type == 'text' || el.type == 'select-one') {
			flink += ',' + el.name + '=' + encodeURIComponent(el.value);
		}
	}
	location.href = flink;
}