jQuery(document).ready(function() {
	/* wyrzucenie alerta, ze strona sie laduje */
	jQuery(".saction,.action,.submitForm").each(function() {
		this.onclick = null;
	});

	jQuery(".action").click(doAction);
	jQuery(".saction").change(doAction);
	jQuery(".submitForm").click(function() { return submitForm(this); });

});

function clearAx(id) {
	var p = jQuery("#"+id);
	jQuery(p).find(".jqmar").html("");
	jQuery(p).find(".jqmpinfo").html("");
	jQuery(p).find(".jqmptitle").html("");
	jQuery(p).find(".jqmabefore").css("display", "none");
	jQuery(p).find(".jqmaafter").css("display", "none");
	jQuery(p).find(".jqmaclose").css("display", "none");
	jQuery(p).find(".jqmaimg-err").css("display", "none");
	jQuery(p).find(".jqmaimg-ok").css("display", "none");
}
function initAx(title, info, id) {
	var p = jQuery("#"+id);
	clearAx(id);
	jQuery(p).find(".jqmpinfo").html(info);
	jQuery(p).find(".jqmptitle").html(title);
	jQuery(p).find(".jqmabefore").css("display", "block");
	if (jQuery(p).css('display') == 'none')
		jQuery(p).jqm({modal: true}).jqmShow();
}

function msgAx(type, msg, id) {
	var p = jQuery("#"+id);
	jQuery(p).find(".jqmaimg-" + type).css("display", "block");
	jQuery(p).find(".jqmar").html('' + msg);
	jQuery(p).find(".jqmabefore").css("display", "none");
	jQuery(p).find(".jqmaafter").css("display", "block");
}

function doAction(t) {
		if (!t || typeof t == 'undefined')
			t = this;
	var arr = t.id.split('_');
	var a_mod = arr.shift();
	var a_cmd = arr.shift();
	var a_itm = "";
	if (jQuery(t).attr('nodeName') == "SELECT")
		a_itm = jQuery(t).val();
	else
		a_itm = arr.shift();
	var a_mod_cmd = a_mod + '_' + a_cmd;

	var data = "c=" + a_mod_cmd + "&id=" + a_itm;

	var title = jQuery("#" + a_mod_cmd + "Title").val();
	var info = jQuery("#" + a_mod_cmd + "Info").val();

	if (jQuery(t).hasClass('confirm')) {
		var confirmtext = jQuery("#" + a_mod_cmd + "Confirm").val();
		if (!confirm(confirmtext))
			return false;
	}

	initAx(title, info, 'processing');
	doAx(_ax, data, 'processing');

	return false;
}

function processForm(formname, title, info) {
	var formsel = "form#" + formname;
	var data = jQuery(formsel).serialize();
	var ftitle = jQuery(formsel).attr('title');
	if (!ftitle || ftitle.length <= 0 || typeof ftitle != 'string' || ftitle == 'undefined' || ftitle == '') {
		ftitle = 'processing';
	}	

	initAx(title, info, ftitle);
	return doAx(_ax, data, ftitle);
}

function doAx(action, data, id) {
	var p = jQuery("#"+id);
	jQuery.ajax({
		type: "POST",
		url: action,
		data: data+"&l="+_l,
		dataType: "json",
		success: function(data, status) {
			if (data.ndl && data.ndl == 1) {
				jQuery(p).jqm().jqmHide();
				return showLoginForm();
			}
			if (data.rdr && data.rdr != '') {
				window.location = data.rdr;
			}
			if (data.cls && data.cls == 1) {
				jQuery(p).jqm().jqmHide();
			}
			if (data.err == 0) {
				msgAx('ok', data.msg, id);
			}
			else {
				msgAx('err', data.msg, id);
			}
			if (data.url && data.url != '') {
				jQuery(p).find(".jqmClose").click(function() {
					if (data.newwin && data.newwin == 1) {
						var win = window.open(data.url, 'resellerwin');
					}
					else
						window.location = data.url;
				});
			}
			else if (data.rfr && data.rfr != '') {
				if (data.rfr == '1') {
					jQuery(p).find(".jqmClose").click(function() {
						window.location.reload(true);
					});
				}
				else {
						window.location.reload(true);
				}
			}
		},
		error: function(data, status) {
			msgAx('err', "-", id);
		},
		complete: function() {
			jQuery(p).find(".jqmaclose").css("display", "block");
		}
	});
}

function showLoginForm() {
	clearAx('loginwin');
	jQuery("#loginwin").jqm({modal: true}).jqmShow();
	return false;
}

function submitForm(t) {
		if (!t || typeof t == 'undefined')
			t = this;
		var submit = jQuery(t).attr('id');
		var form = jQuery(t).parents("form");
/*
		var submit = t.id;
		var form = jQuery('#' + t.id).parents("form");
*/
		var formid = jQuery(form[0]).attr('id');
		var sel = "#" + formid + " :input";
		var req = 0;

		var str = '_' + formid;
		if (window[str]) {
			/* jezeli istnieje funkcja _[id formularza](), to ja odpalamy przed wyslaniem */
			window[str]();
		}

		if (!jQuery(t).hasClass('skipreq')) {
			jQuery(sel).each(function() {
				if (jQuery(this).attr('class') && jQuery(this).attr('class').match('req')) {
					/* jezeli dany input ma klase req, znaczy, ze nie moze byc pusty i form w takim przypadku wyslany nie bedzie */
					if (jQuery(this).val() == '') {
						req++;
						return false;
					}
				}
			});
		}

		if (req != 0) {
			/* ktorys z wymaganych argumentow byl pusty, walimy alerta o tresci z inputa o id req_empty */
			alert(jQuery("#req_empty").val());
			return false;
		}

		if (submit && submit.length > 0 && submit != 'undefined') {
			jQuery("#asubmit").remove();
			var sb = jQuery("<input id='asubmit' type='hidden' name='submit' />");
			jQuery(sb).val(submit);
			jQuery('#' + formid).append(sb);
		}

		var title = jQuery('#'+formid+'Title').val();
		var info = jQuery('#'+formid+'Info').val();
		processForm(formid, title, info);
		return false;
}


