/*
copyright by
F.H.U. Slawomir Kaniecki
e-mail: kontakt (at) www-editor (dot) net
/**/


var OnLoadPage = new Object();
// OnLoadPage.example = function() { alert('hej'); } // tak mozna uzyc :)
//  OnLoad="on_load()"
window.onload = function() {
	for (el in OnLoadPage) {
		if (typeof OnLoadPage[el] == "function") OnLoadPage[el]();
	}
}


OnLoadPage.advAJAX_init = function() {
	// alert(advAJAX)
	if (typeof advAJAX != "undefined" && advAJAX) { // inicjalizacja ustawien advAJAX
		advAJAX.setDefaultParameters({
			mimeType: 'text/plain', 
	
			onError : function(obj)			{ alert( (obj.status == 302) ? "zobacz logi" : "Error: " + obj.status); },
			onLoading : function(obj)		{
				var o = oid('ajax_layer');
				if (!o) {
					o = document.createElement("div");
					o.id = 'ajax_layer';
					o.innerHTML = '<img src="/images/ajax-loader.gif" style="">';
					document.body.appendChild(o);
				}
				if (o) {
					o.style.width = document.body.scrollWidth;
					o.style.height = document.body.scrollHeight - (document.body.scrollTop + 200);
					o.style.paddingTop = document.body.scrollTop + 200;
					o.style.visibility = 'visible';
					//	set_opacity(o, 30); //	fade_opacity(o, 0, 30, 10, 2);
				}
			},
			onComplete : function()	{ o = oid('ajax_layer'); if (o) { o.style.visibility = "hidden"; o.style.top = 0; o.style.width = 100; o.style.height = 100; } }
			//	onFinalization : function()	{ o = oid('ajax_layer'); if (o) { o.style.visibility = "hidden"; o.style.top = 0; o.style.width = 100; o.style.height = 100; } }
		});
	}
}

//	number_format($num, 2, '.', '')
function number_format(sum) {
	/* formatowanie do 2 miejsc po przecinku ... */
	sum = Math.round(parseFloat(sum) * 100)
	sum = sum + '';
	if (sum.length == 1) sum = '00' + sum;
	if (sum.length == 2) sum = '0'  + sum;
	sum = sum.replace(/([0-9]+)([0-9]{2})/, "$1.$2");
	/* END formatowanie do 2 miejsc po przecinku ... */
	return sum
}

function oid(id) {
	if (id) return document.getElementById(id);
}

function oname(id) {
	if (id) return document.getElementsByName(id);
}

function trim(txt) {
	for (r=txt.length-1; (txt.charAt(r) == ' ' || txt.charAt(r) == '\t' || txt.charAt(r) == '\n' || txt.charAt(r) == '\r'); r--) {}
	if (r > -1) for (l=0; (txt.charAt(l) == ' ' || txt.charAt(r) == '\t' || txt.charAt(r) == '\n' || txt.charAt(r) == '\r'); l++) {}
	return (r > -1) ? txt.substring(l, r+1) : '';
}

function element_position(el) {
	var ret = new Object();
	ret.left = 0;
	ret.top  = 0;
	var o = el;
	ret.left = o.offsetLeft;
	ret.top  = o.offsetTop;
	//	while (o = o.parentNode) {
	while (o = o.offsetParent) {
		ret.left = ret.left + o.offsetLeft;
		ret.top  = ret.top  + o.offsetTop;
	}
	return ret;
}

function find_o(targ, class_name) {
	if (class_name == '') return null;
	var o = targ;
	do {
		if (o.className == class_name) {
			return o;
		}
	//	} while (o = o.offsetParent)
	} while (o = o.parentNode)
	return null;
}

function set_opacity(o, value) {
	o.style.opacity = value/100;
	o.style.filter = 'alpha(opacity=' + value + ')';
}

function fade_opacity_to(o, f, t, step) {
	if (o.fade) clearTimeout(o.fade);

	if (o.fade_state == undefined) o.fade_state = 0;
	s = o.fade_state;
	//	alert(o.fade_state + ' - ' + f)
	set_opacity(o, s);
	p = element_position(o); // ???????????????????????????????/
	if (abs(s - f) >= step) o.fade = setTimeout(function() {
		o.fade_state = s + ( (s < f)? step : -1 * step )
		fade_opacity_to(o, f, t, step);
	}, t);
	else {
		if (o.fade) clearTimeout(o.fade);
		o.fade = null;
	}
}

function fade_opacity(o, s, f, t, step) {
	if (o.fade) clearTimeout(o.fade);

	set_opacity(o, s);
	p = element_position(o); // ???????????????????????????????/
	if (abs(s - f) >= step) o.fade = setTimeout(function() {
		fade_opacity(o, s + ( (s < f)? step : -1 * step ), f, t, step);
	}, t);
	else {
		if (o.fade) clearTimeout(o.fade);
		o.fade = null;
	}
}

// sw_cl: switch class
function sw_cl(id, st1, st2) {
	var o = id;
	if (typeof id == "string") o = oid(o);
	if (o) {
		if (o.className == st2) o.className = st1;
		else o.className = st2;
	}
}

//*
function radio_sel(r) {
	if (typeof r == "string") r = oname(r);
	if (r) {
		if (r.length) { for (var i=0; i<r.length; i++) if (r[i].type == 'radio' && r[i].checked) return i; return -1; }
		else return (r.type == 'radio' && r.checked)?0:-1;
	}
	else return -2;
}

function radio_val(r, id) {
	if (typeof r == "string") r = oname(r);
	if (r) {
		if (r.length) return r[id].value;
		else return r.value;
	}
	else return false;
}

function radio_sel_val(r) {
	if (typeof r == "string") r = oname(r);
	var id = radio_sel(r);
	if (id > -1) return radio_val(r, id)
	return '';
}
/**/

function check_e_mail(e) {
	if (typeof e != 'string') return -3; // blad duzy
	if (e == '') return -2; // pusty string
	return e.search(/^[a-zA-Z0-9]+[a-zA-Z0-9\._-]*@[a-zA-Z0-9]+[a-zA-Z0-9\._-]*\.[a-zA-Z0-9]{2,3}$/i); // -1 jak nie pasuje, 0 jak ok.
}

function debug_show(txt) {
	if (typeof $ == 'function') {
		$("#debug").remove()
		if (txt != '') {
			$('body').append('<div id="debug" style="position: absolute; top: 0px; background: yellow; z-index: 100; padding: 10px; "></div>');
			$("#debug").text(txt);
		}
	}
	else {
		alert(txt);
	}
}

function pos_rb(div_id, rel_l, rel_w, rel_h, ml, mt) {
// 	debug_show(rel_l);
	if (typeof ml == 'undefined') ml = 20;
	if (typeof mt == 'undefined') mt = 0;
	w = $(div_id).width();
	c_w = $(div_id).outerWidth();
	c_h = $(div_id).outerHeight();
	sleft = $(window).width() - c_w - ml;
	stop  = $(window).height() - c_h;
	if (sleft + c_w < rel_l + rel_w)	{ sleft = rel_l + rel_w - c_w - 0; }
	if (stop < rel_h + mt)			{ stop = rel_h + mt; }
// 	alert(c_w + " = " + $(div_id).css('width'))
	$(div_id).css('width', w);
	$(div_id).css('left', sleft);
	$(div_id).css('top', stop);
}

