function form_change_action(form_id, newaction) {
	var f = document.getElementById(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)
{
	$('body').append('<div style="display:none;" id="aaa123123">' + msg + '</div>');
	$('#aaa123123').dialog({
			bgiframe: true,
			modal: true,
			closeOnEscape: false,
			beforeclose: function() {
					if (okfunc) {
						okfunc();
					}
					$('#aaa123123').remove();
				},
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					
				}
			}
		});
}

function pwcconfirm(msg,onokfunc)
{
	$('body').append('<div style="display:none;" id="aaa123123">' + msg + '</div>');
	$('#aaa123123').dialog({
			bgiframe: true,
			modal: true,
			closeOnEscape: true,
			beforeclose: function() {
					$('#aaa123123').remove();
				},
			buttons: {
				'Tak': function() {
					$(this).dialog('close');
					if (onokfunc) {
						onokfunc();
					}
				},
				'Nie': function() {
					$(this).dialog('close');
				}
			}
	});
}

function div_loading_show()
{
	var width = 300;
    var height = 300;
    var left = parseInt((screen.width/4) - (width/4));
    var top = parseInt((screen.height/4) - (height/4));
    top -= 70;
    if (window.pageYOffset) {
    	top += window.pageYOffset;
    } else if (document.documentElement.scrollTop) {
    	top += document.documentElement.scrollTop;
    }
    $('#content_loading').width(width);
    $('#content_loading').height(height);
    $('#content_loading').css('top', top + 'px');
    $('#content_loading').css('left', left + 'px');
 	$('#content_loading').show();
}

function div_loading_hide()
{
 	$('#content_loading').hide();
}

function ajax_deal_with_response(json) {
	var jar = '';
	try {
		jar = eval(json)
	} catch (e) {
		var rt = json.substr(0, json.lastIndexOf(']')+1);
		eval('jar = ' + rt);
	}
	var dkf = jar.length;
	div_loading_hide();
	for (var i = 0; i < dkf; i++) {
		if (jar[i].nstyleonly) {
			if (jar[i].nstyle) {
				try {
					eval("st = " + jar[i].nstyle);
				} catch (e) {
					continue;
				}
				$('#' + jar[i].div).css(st);
			}
		} else if (jar[i].jscode) {
			eval(jar[i].content);
		} else if (jar[i].append != 0) {
			if (jar[i].append == -1) {
				$('#' + jar[i].div).prepend(jar[i].content);
			} else {
				$('#' + jar[i].div).append(jar[i].content);
			}
			if (jar[i].nstyle) {
				try {
					eval("st = " + jar[i].nstyle);
				} catch (e) {
					continue;
				}
				$('#' + jar[i].div).css(st);
			}
		} else {
			$('#' + jar[i].div).html(jar[i].content);
			if (jar[i].nstyle) {
				try {
					eval("st = " + jar[i].nstyle);
				} catch (e) {
					continue;
				}
				$('#' + jar[i].div).css(st);
			}
		}
	 }
}

function ajax_get(url) {
	div_loading_show();
	$.get(url,{},ajax_deal_with_response);
}

function ajax_post(form_id)
{
	div_loading_show();
	var f = $('#' + form_id);
	var url = f.action;
	var data = f.serialize();
	$.post(url, data,ajax_deal_with_response);
}

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;
}

function prac_form() {
	// imie, nazwa, email, telefon
	if($('#imie').val() == '' || $('#nazwa').val() == '' || $('#email').val() == '' || $('#telefon').val() == '') {
		pwcalert('Proszę poprawnie wypełnić formularz') ;
		return false ;
	}
	
	if(!check_mail($('#email').val())) {
		pwcalert('Proszę wpisać poprawny adres E-mail') ;
		return false ;
	}
	
}

// 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;
}
