/*
	JS Extending Library
	Must be loaded before any other JS script
	v 1.0.3
*/

function rand(min, max) {
	return Math.floor(Math.random() * ((max + 1) - min)) + min;
}

function time() {
	return new Date().getTime();
}

function IDgen() {
	return time().toString() + rand(10000, 99999).toString();
}

function isset(v) {
	return (typeof(v) == 'undefined' ? false : true);
}

function empty(v) {
	return (isset(v) ? (v == '0'   ||   v == 0   ||   v == null   ||   v == '' ? true : false) : true);
	//return (isset(v) ? (in_array(v, ['0', 0, null, '']) ? true : false) : true);
}

function $(id) {
	return document.getElementById(id);
}

function trim(s) {
	return s.replace('/ /g', '');
}

function in_array(value, array) {
	var v;
	for (key in array) {
		if (array[key] == value) return true;
	}
	return false;
}

function array_kick(array, key) {
	var el, newArr = {};
	
	for (el in array) {
		if (key != el) newArr[el] = array[el];
	}
	return newArr;
}

function addEvent(ev, o, func, useCapture) {
	// For compatability with older version
	// useCapture
	if (typeof(ev) == 'object'   &&   typeof(o) != 'string') {
		var tmp = ev;
		var ev = o;
		var o = tmp;
	}
	// ---
	
	if (isIE()) o.attachEvent("on"+ ev, function (){eval(func)});
	else o.addEventListener(ev, function (e){eval(func);}, false);
}

function isIE() {
	return (window.navigator.appName == 'Microsoft Internet Explorer');
}

function isSAFARI() {
	return (navigator.platform == 'MacPPC'   &&   window.navigator.appName == 'Netscape');
}

function isIE7() {
	return (window.navigator.userAgent.indexOf('MSIE 7') != -1 ? true : false);
}


function dump(v) {
	var res = '';
	
	switch (typeof(v)) {
		case 'object':
			var el;
			for (el in v) res += el +' => '+ v[el] +'\n';
		break;
	}
	
	alert(res);
}

function popup(url, name, width, height, x, y, attr, content, insertHTML) {
	/*
		Usage
		All of the paramaters are not obligatory. It means that you can pass none of them and the popup is going to open anyway.
		
		@url [STRING] - URI of the document to open.
		@name [STRING] - Name of the new window, which can be used in target for A and FORM tags.
		@width [STRING] - Width of the window.
		@height [STRING] - Height of the window.
		@x [STRING] - Location of the window for x axis.
		@y [STRING] - Location of the window for y axis.
		@attr [STRING] - attributes of the window:
			copyhistory   Копировать историю просмотра текущего окна.
			dependent     Создать окно, зависимое от родительского окна. Зависимые окна закрываются при закрытии родительского окна и не показываются в панели задач Windows.
			directories   Показывать панель каталогов обозревателя.
			height        Высота окна в пикселях.
			location      Показывать адресную строку обозревателя.
			menubar       Показывать меню обозревателя.
			resizable     Пользователь может изменять размеры окна.
			screenX       Расстояние в пикселях от левого края экрана по горизонтали.
			screenY       Расстояние в пикселях от верхнего края экрана по вертикали.
			left          Расстояние в пикселях от левого края экрана по горизонтали.
			top           Расстояние в пикселях от верхнего края экрана по вертикали.
			scrollbars    Показывать полосы прокрутки окна.
			status        Показывать строку состояния обозревателя.
			toolbar       Показывать панель кнопок обозревателя.
			width         Ширина окна в пикселях.
		@content [STRING] - Some HTML content that will be written to the window. Pass some HTML or an empty string or "auto" for auto filling with HTML. Additionaly view @insertHTML
		@insertHTML [STRING] - if you pass auto for @content, than this will be placed between BODY tags.
	*/
	
	if (empty(url)) var url = '';
	
	if (! isset(name)) var name = '';
	
	if (empty(width)) var width = '100';
	else if (width == 'fullscreen') var width = screen.availWidth;
	
	if (empty(height)) var height = '100';
	else if (height == 'fullscreen') var height = screen.availHeight;
	
	if (empty(x)   &&   width != 'fullscreen') var x = (Math.round(screen.availWidth / 2 - parseInt(width) / 2)).toString();
	else if (width == 'fullscreen') var x = '0';
	
	if (empty(y)   &&   height != 'fullscreen') var y = (Math.round(screen.availHeight / 2 - parseInt(height) / 2)).toString();
	else if (height == 'fullscreen') var y = '0';
	
	if (! isset(insertHTML)) var insertHTML = '';
	
	if (empty(attr)) {
		var attr = 'dependent=1,width='+ width +',height='+ height;
		if (x != '') attr += ',screenX='+ x +',left='+ x;
		if (y != '') attr += ',screenY='+ y +',top='+ y;
	} else {
		if (! isset(attr)) var attr = '';
		if (x != ''   &&   attr.indexOf('left=') == -1) attr += (attr != '' ? ',' : '') +'left='+ x;
		if (x != ''   &&   attr.indexOf('screenX=') == -1) attr += (attr != '' ? ',' : '') +'screenX='+ x;
		if (y != ''   &&   attr.indexOf('top=') == -1) attr += (attr != '' ? ',' : '') +'top='+ y;
		if (y != ''   &&   attr.indexOf('screenY=') == -1) attr += (attr != '' ? ',' : '') +'screenY='+ y;
		if (attr.indexOf('width=') == -1) attr += (attr != '' ? ',' : '') +'width='+ width;
		if (attr.indexOf('height=') == -1) attr += (attr != '' ? ',' : '') +'height='+ height;
	}
	
	if (empty(content)) var content = '';
	else if (content = 'auto') var content = '<html><head><style>BODY{overflow:hidden;padding:0;margin:0;}</style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="imagetoolbar" content="no" /></head><body onLoad="window.moveTo('+ x +', '+ y +');self.focus()" bgColor="#000000">%content%</body></html>';
	
	var win = window.open(url, name, attr);
	if (content != '') {
		win.document.write(content.replace('%content%', insertHTML));
		win.document.close();
	}
	return win;
}

function moveObj2Event(e, id, offsetX, offsetY) {
	var x, y, o = $(id);
	if (o) {
		if (! e) var e = window.event;
		
		if (! isset(offsetX)) var offsetX = 0;
		if (! isset(offsetY)) var offsetY = 0;
		
		x = parseInt(e.clientX) + parseInt(document.body.scrollLeft) + offsetX;
		y = parseInt(e.clientY) + parseInt(document.body.scrollTop) + offsetY;
		
		if (document.body.clientHeight < o.offsetHeight + y) y = y - o.offsetHeight;
		if (document.body.clientWidth < o.offsetWidth + x) x = x - o.offsetWidth;
		
		o.style.left = x +'px';
		o.style.top = y +'px';
	}
}

function winStatus() {
	var e = window.event;
	
	if (isIE()) {
		if (isset(window.statusbar)) {
			if (! window.statusbar) {
				e.returnValue = false;
				return false;
			}
		}
		window.status = e.srcElement['innerText'];
		e.returnValue = true;
		return true;
	}
}

function copyright(o, from) {
	var y = new Date();
	y = parseInt(y.getFullYear());
	if (empty(from)) {
		o.innerHTML = y;
		return;
	}
	var from = parseInt(from);
	
	o.innerHTML = (y - from > 0 ? from +'-'+ y : y);
}

function checkData(s, type) {
	switch (type) {
		
		case 'date':
			if (/^(0|1|2|3)?[0-9]{1}\.(0|1|2)?[0-9]{1}\.20[0-9]{2}$/.test(s)) return true;
		break;
		
		case 'email':
			if (/^[a-z0-9_\-\.\^]+@[a-z0-9_\-\.]+\.[a-z]{2,4}$/.test(s)) return true;
		break;
		
		case 'int':
		case 'integer':
		case 'number':
			if (/^[0-9]+$/.test(s)) return true;
		break;
		
		case 'httpLink':
			if (/^http(s)?:\/\/[a-z0-9_\.-]+\.[a-z]{2,5}/i.test(s)) return true;
		break;
		
		case 'ftpLink':
			if (/^ftp:\/\/[a-z0-9_\.-]+\.[a-z]{2,5}/i.test(s)) return true;
		break;
		
	}
	
	return false;
}

function overlay(id, content) {
	var o = $(id);
	if (o) {
		o.innerHTML = content;
		toggle(id);
	}
}


function elrkjt(x, y, z) {
	// mailto: protection from bots
	document.write('<'+'a hr'+'ef="mai'+'lto:'+ x +'@'+ y +'.'+ z +'">'+ x +'@'+ y +'.'+ z +'</a>');
}

function toggle(id) {
	var o = $(id);
	if (o) {
		if (o.style.display == 'none') o.style.display = 'block';
		else o.style.display = 'none';
	}
	return id;
}

function toggleOn(id) {
	var o = $(id);
	if (o) o.style.display = 'block';
	return id;
}

function toggleOff(id) {
	var o = $(id);
	if (o) o.style.display = 'none';
	return id;
}