function register_event(elem, eventType, handler) {
	if (elem.attachEvent) elem.attachEvent('on' + eventType, handler);
	if (elem.addEventListener) elem.addEventListener(eventType, handler, false);
}

function str_replace(search, replace, string) {
	while (-1 != string.indexOf(search)) {
		string = string.replace(search, replace);
	}
	return string;
}

function popup_img(href,w,h,title) {
	var scrollbars = ((h > (screen.height - 50)) || (w > (screen.width - 50))) ? 1 : 0;
	var width = (w > (screen.width - 50)) ? (w - 50) : w;
	var height = (h > (screen.height - 50)) ? (h - 100) : h;
	var wo = window.open(href,'_blank','scrollbars=' + scrollbars + ',menubar=0,title=0,titlebar=0,directories=0,status=0,statusbar=0,resizable=0,width=' + (parseInt(width) + parseInt((1 == scrollbars) ? 16 : 0)) + ',height=' + (parseInt(height) + parseInt((1 == scrollbars) ? 16 : 0)));
	wo.document.write('<html><head><title>'+title+'</title></head><body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0"><a href="" onclick="window.close(); return false;"><img src="'+href+'" width="'+w+'" height="'+h+'" vspace="0" hspace="0" border="0" alt="'+str_replace('"', '&quot;', title)+'" /></a></body></html>');
	wo.focus();
	wo.moveTo(((screen.width - width) / 2), ((screen.height - height) / 2));
}

function pround(val, precision) {
	val = String(val);
	return parseFloat(val.substring(0,val.indexOf('.'))+'.'+val.substr(val.indexOf('.')+1,precision));
}

// get cookie by name
function get_cookie(name) {
	var c = document.cookie;
	if (1 > c.length) return false;

	var b = c.indexOf(name + '=');
	if (-1 == b) return false;

	b += (name.length + 1);
	var e = c.indexOf(';', b);

	return unescape((-1 == e) ? c.substring(b) : c.substring(b, e));
}

function set_cookie(name, value) {
	document.cookie = name + '=' + escape(value) + ';path=/';
}

function add_url_param(url, params) {
	var i = 0;
	var out = '';
	for (i = 0; i < params.length; i++) {
		out += ((url.lastIndexOf('/') == (url.length - 1)) ? '?' : '&') + params[i][0] + '=' + params[i][1];
	}
	return out;
}

function Logout() {
	if (confirm('Вы действительно хотите выйти?')) {
		self.location.href="logout.php";
		return true;
	} else return false;
}

function get_form_submit(form) {
	var i = form.elements.length - 1;
	var found = false;
	var sbmt = null;
	while ((i > 0) && (false == found)) {
		if (form.elements[i].type == "submit") {
			sbmt = form.elements[i];
			found = true;
		}
		i--;
	}

	return sbmt;
}

function property_exists(obj, name) {
	for (prop in obj) {
		if (prop == name) return true;
	}
	return false;
}
function set_property(obj, name, value, only_exists) {
	only_exists = (undefined != only_exists) ? only_exists : true;
	if (true == property_exists(obj, name)) obj['' + name + ''] = value;
	else {
		if (false == only_exists) obj['' + name + ''] = value;
	}
}
function get_property(obj, name) {
	if (undefined != obj['' + name + '']) return obj['' + name +''];
}

// object.property_exists
function object_property_exists(name) {
	return property_exists(this, name);
}
// object.set_property
function object_set_property(name, value) {
	set_property(this, name, value);
}