// vim: set ts=4 sw=4 :
String.prototype.asInteger = function() { var l = parseInt(this, 10); return (isNaN(l)) ? 0 : l; };
Number.prototype.asInteger = function() { return (isNaN(this)) ? 0 : this; };

function e(id) {
	return document.getElementById(id);
}

function clearField(id, dflt) {
	var field = e(id);
	if ( field.value == dflt ) {
		field.value = '';
	}
}

/* setInner replaces innerHTML of element specified by "id" with "html" */
function setInner(html, id) {
//	setNotBusy(id); /* hide busy indicator */
	e(id).innerHTML = html;
}

function addclass(a, klass) {
	var a = e('t_'+a);
	a.className = a.className + ' ' + klass;
}

function removeclass(a, klass) {
	var re = new RegExp('\\s?'+klass, 'g');
	var a = e('t_'+a);
	a.className = a.className.replace(re, '');
}

function generateApikey(evnt) {
	evnt.returnValue = false;
	e('id_api_key').innerHTML = 'Generuję...';
	var a = Ajax('/ajax/generate-apikey.html', document.location);
	a.onSuccess = function () { setInner(this.HTTP.responseText, 'id_api_key'); }
	a.GET();
	return false;
}

function searchValidate(evnt) {
	var el = e('id_s').value;
	if (el.length > 3) {
		evnt.returnValue = true;
		return true;
	}

	evnt.returnValue = false;
	alert('Musisz podać co najmniej 4 znaki numeru seryjnego!');
	return false;
}

/* listy "ostatnio" oraz wyniki wyszukiwania */

var ostCnt = 0;
var ostPage = 1;
var ostPageCnt = 0;
var ostEl = null;
function ostNumerate(el) {
	ostEl = el;
	var list = el.getElementsByTagName('div');
	var pagelist = '';
	var pn = 0;
	ostCnt = 0;
	for (var i=0; i < list.length; i++) {
		if (list[i].className == 'page') {
			pn += 1;
			list[i].setAttribute('id', 'page_' + pn);
		}
		if (list[i].className.match(/col(\s|")/)) {
			ostCnt++;
		}
	}
	ostPage = 1;
	ostPageCnt = pn;
	ostPageSwitch(1, null);
}

function ostPageSwitch(num, evnt) {
	if (evnt) evnt.returnValue = false;
	var list = ostEl.getElementsByTagName('div');
	for (var i=0; i < list.length; i++) {
		if (list[i].className == 'page') {
			id = list[i].getAttribute('id');
			list[i].style.display = (id == ('page_'+num)) ? 'inline' : 'none';
		}
	}
	ostPage = num;
	if (ostPage > 1) { var a = 1; } // dummy for MSIE -- do any operation reading ostPage
	e('popr').style.display = (ostPage > 1) ? 'block' : 'none';
	e('nast').style.display = (ostPage < ostPageCnt) ? 'block' : 'none';

	return false;
}

function potwierdz(evnt, msg) {
	if (confirm('Czy na pewno chcesz ' + msg + '?\n\nTa operacja jest nieodwracalna!')) {
		evnt.returnValue = true;
		return true;
	}
	evnt.returnValue = false;
	return false;
}

function getPosition(e) {
	var pleft = 0; var ptop = 0;
	if (e.offsetParent) {
		do {
			pleft += e.offsetLeft
			if (e.currentStyle) pleft += e.currentStyle.borderLeftWidth.asInteger();
			ptop += e.offsetTop;
			if (e.currentStyle) ptop += e.currentStyle.borderTopWidth.asInteger();
		} while (e = e.offsetParent);
	}

	return { x: pleft, y: ptop };
}
function loadingShow(for_id, msg) {
	var div = e(for_id);
	if (div == null) return false;

	if (!div.loader) {
		// create loader for that div:
		div.loader = document.createElement('div');
		div.loader.id = for_id + '_loader';
		div.loader.className = 'loader';
		div.loader.style.display = 'none';
		e('content').insertBefore(div.loader, e('content').firstChild);
	}
	// show loader
	if (msg == null) msg = 'Wczytuję...';
	div.loader.innerHTML = '<img src="/gfxw/indicator.gif" alt="" /> ' + msg;
	var pos = getPosition(div);
	if (div.loader.style && (typeof(div.loader.style.left) == 'string')) {
		div.loader.style.left = (pos.x + 2) + 'px';
		div.loader.style.top = (pos.y + 2) + 'px';
	} else if (div.loader.style) {
		div.loader.style.left = pos.x + 2;
		div.loader.style.top = pos.y + 2;
	}
	div.loader.style.display = 'block';
}

function loadingHide(for_id) {
	var div = e(for_id);
	if (div == null) return false;
	if (div.loader) div.loader.style.display = 'none';
	return false;
}
