/*
	adds an event to DOM object
		obj - object to add an event to
		evType - string event identificator
		fn - function to call
*/
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn); 
		return r;
	} else {
		return false;
	}
}

/*
	returns a DOM object which is the target of e event
*/
function get_event_target(e)
{
	var targ;
	if (!e)	
		var e = window.event;
	if (e.target)
		targ = e.target;
	else if (e.srcElement)
		targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function popup_window(url, width, height)
{
	window.open(url, 'popup', 'width=' + width + ',height=' + height + ',status=no,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,screenX=0,screenY=0');
}

function popup_picture(url, width, height)
{
	width = width + 50;
	height = height + 90;
	window.open(url, 'popup', 'width=' + width + ',height=' + height + ',status=no,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,screenX=0,screenY=0');
}

function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.trim = strtrim;

function validate_form()
{
	var ok = true;
	var the_elements = document.forms[0].elements;
	for (var i = 0; i < the_elements.length; i++) {
		var the_input = the_elements[i];
		var div_container = the_input;
		while (div_container.tagName.toLowerCase() != "div") {
			div_container = div_container.parentNode;
		}
		if (div_container.className == "required error") {
			div_container.className = "required";
		}
		if ((div_container.style.display != "none") && (div_container.className == "required") && (the_input.value.trim() == "")) {
			div_container.className = "required error";
			ok = false;
		}
	}
	if (!ok) {
		alert("Užpildykite reikalingus laukus.");
	}
	return ok;
}
