// 2use popup() function: <a href="javascript:;" onClick="javascript:popup('name', '#link', 0, 0, 300, 150, 0, 0, 1, 1, 1); return false;"></a>
function popup(name, url, left, top, width, height, toolbar, menubar, statusbar, scrollbar, resizable){
	var wint = (screen.height - height) / 2;
	var winl = (screen.width - width) / 2;
	toolbar_str = toolbar ? 'yes' : 'no';
	menubar_str = menubar ? 'yes' : 'no';
	statusbar_str = statusbar ? 'yes' : 'no';
	scrollbar_str = scrollbar ? 'yes' : 'no';
	resizable_str = resizable ? 'yes' : 'no';
	window.open(url, name, 'left='+winl+',top='+wint+',width='+width+',height='+height+',toolbar='+toolbar_str+',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
}

function simplepopup(url,width,height) {
	winl = (screen.width) ? (screen.width - width) / 2 : 0;
	wint = (screen.height) ? (screen.height - height) / 2 : 0;
	var settings = 'left='+winl+',top='+wint+',width='+(width+40)+',height='+(height+40)+',toolbar=no,menubar=no,status=yes,scrollbars=yes,resizable=yes';
	window.open(url,'imgpoint',settings);
}

function emptypopup (name,width,height) {
	winl = (screen.width) ? (screen.width - width) / 2 : 0;
	wint = (screen.height) ? (screen.height - height) / 2 : 0;
	var settings = 'left='+winl+',top='+wint+',width='+width+',height='+height+',toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=no';
	window.open('about:blank',name,settings);
}

function preloadImgs() {
	var d = document;
	if(d.images) {
		if(!d.IMGs) d.IMGs=new Array();
		var i,j=d.IMGs.length,a=preloadImgs.arguments;
		for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){
		d.IMGs[j]=new Image; d.IMGs[j++].src = base_path + 'img/' + a[i];
		}
	}
}
/*
function addEvent(obj, eventType,fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(eventType, fn, useCapture);
		return true;
	} else {
		if (obj.attachEvent) {
			var r = obj.attachEvent("on"+eventType, fn);
			return r;
		}
	}
}
*/


function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");

document.cookie = curCookie;
}

// name - имя считываемого cookie

function getCookie(name) {
var prefix = name + "=";
var cookieStartIndex = document.cookie.indexOf(prefix);
if (cookieStartIndex == -1) return null;
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)cookieEndIndex = document.cookie.length;
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}


// name - имя cookie
// [path] - путь, для которого cookie действительно
// [domain] - домен, для которого cookie действительно

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}


var __eventListeners = [];

function addEvent(instance, eventName, listener) {
    var listenerFn = listener;
    if (instance.addEventListener) {
        instance.addEventListener(eventName, listenerFn, false);
    } else if (instance.attachEvent) {
        listenerFn = function() {
            listener(window.event);
        }
        instance.attachEvent("on" + eventName, listenerFn);
    } else {
        throw new Error("Event registration not supported");
    }
    var event = {
        instance: instance,
        name: eventName,
        listener: listenerFn
    };
    __eventListeners.push(event);
    return event;
}
/* * * * * * * * * * * * * * * * */
function removeEvent(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}
/* * * * * * * * * * * * * * * * */
function unregisterAllEvents() {
    while (__eventListeners.length > 0) {
        removeEventListener(__eventListeners[0]);
    }
}
/* * * * * * * * * * * * * * * * */
function getTargetElement(evt) {
    var elem;
    if (evt.target) {
        elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
    } else {
        elem = evt.srcElement;
    }
    return elem;

}
/* * * * * * * * * * * * * * * * */
function popupimg(name, url, width, height){
	var imgss;
	winl = (screen.width) ? (screen.width - width) / 2 : 0;
	wint = (screen.height) ? (screen.height - height) / 2 : 0;
	//var winname = urlencode(name);
	var winname = name;
	var settings = 'left='+winl+',top='+wint+',width='+(width+40)+',height='+(height+40)+',toolbar=no,menubar=no,status=yes,scrollbars=yes,resizable=yes';
	myimgss = window.open('','imgpoint',settings);
	myimgss.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"><title>'+urldecode(winname)+'</title></head><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><table height="100%" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><a href="javascript:self.close();"><img src="'+url+'" name="fullbimg" alt="Click to close the window..." width="'+width+'" height="'+height+'" border="0"></a></td></tr></table></body></html>');
	myimgss.document.close();
	if(myimgss.window.focus) {
		myimgss.window.focus();
	}
}
/* * * * * * * * * * * * * * * * */
function urlencode(ch) {
	ch = ch.replace(/[ ]/g,"%20")
	return escape(ch)
}
function urldecode(ch) {
   ch = ch.replace(/[+]/g," ")
   return unescape(ch)
}
/* * * * * * * * * * * * * * * * */
function cPanels (id,active) {
	this.active = active;
	this.activeLink = null;
	this.ID = id;
	this.root = document.getElementById(id);
	this.childs = [];
	this.bookmarks = null;
	this.init = function () {
		var i,j;
		if (this.root) {
			var elements = this.root.getElementsByTagName('div');
			if (elements) {
				j = 0;
				for (i in elements) {
					if (elements[i].className && (elements[i].className.indexOf('ptab') != -1)) {
						if (j == this.active) {
							elements[i].style.display = 'block';
						}
						this.childs[j] = elements[i];
						j++;

					} else if(elements[i].className && (elements[i].className.indexOf('bookmarks') != -1)) {
						this.bookmarks = new cBookmarks(elements[i].id,this.active);
						this.bookmarks.init();
					}
				}
				this.total = j;
			}
		}
	}
	this.activate = function (pIndex,link) {
		if (this.childs[this.active]) {
			this.childs[this.active].style.display = 'none';
			if (this.bookmarks && this.bookmarks[this.active]) {
				this.bookmarks[this.active].id = '';
			}
			if (this.activeLink) {
				this.activeLink.className = '';
			}

		}
		if (this.childs[pIndex]) {
		    this.childs[pIndex].style.display = 'block';
		    this.active = pIndex;
		    this.activeLink = link;
		    if (this.activeLink) {
		    	this.activeLink.className = 'selected';
		    }
		    /*
		    if (this.bookmarks[pIndex]) {
		    	this.bookmarks[pIndex].id = 'bmsel';
		    }
		    */
		    if (this.bookmarks) this.bookmarks.activate(pIndex);
		}
	}

	this.initRoot = function (obj) {
		this.root = obj;
		this.init();
	}
}

/* * * * * * * * * * * * * * * * */
function getOffsetLeft(el) {
	var left = el.offsetLeft;
	if (el.offsetParent) left += getOffsetLeft(el.offsetParent);
	return left;
}
/* * * * * * * * * * * * * * * * */
function getOffsetTop(el) {
	var top = el.offsetTop;
	//alert(el.nodeName + ' class:' + el.className + ' offset:' + el.offsetTop);
	if (el.offsetParent) top += getOffsetTop(el.offsetParent);
	return top;
}

/* * * * * * * * * * * * * * * * */
function validateform(fi,errormsg) {
	var el,noerr;
	noerr = true;
	for (var i in fi) {
		el = document.onfrm[fi[i]];
		if (el) {
			if (el.value == '') {
				noerr = false;
				el.style.background = '#ffdddd';
			} else {
				el.style.background = '#ffffff';
			}
		}
	}
	if (!noerr) alert(errormsg);
	return noerr;
}

/* * * * * * * * * * * * * * * * */
function setLang(lang) {
	var link = location.href;
	var preg = /^http:\/\/(\w{2,3}\.)?(.+)/;
	var rez;
	if (rez = link.match(preg)) {
		location.href = 'http://' + lang + '.' + rez[2];
	}


}
/* * * * * * * * * * * * * * * * */
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

/* * * * * * * * * * * * * * * * */
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}
/* * * * * * * * * * * * * * * * */
function printPage(mode) {
	if ((mode == undefined) || (mode != 1)) {
		var wint = (screen.height - 600) / 2;
		var winl = (screen.width - 800) / 2;
		var url = location.href + '?print';
		window.open(url, 'Print', 'left='+winl+',top='+wint+',width=800,height=600,toolbar=no,menubar=no,status=yes,scrollbars=yes,resizable=no,location=no');
	} else {
		window.print();
	}
	return false;
}

/* * * * * * * * * * * * * * * * */
var dd_u = "undefined";

function WZDD()
{
	this.elements = new Array(0);
	this.obj = null;
	this.n = navigator.userAgent.toLowerCase();
	this.db = (document.compatMode && document.compatMode.toLowerCase() != "backcompat")?
		document.documentElement
		: (document.body || null);
	this.op = !!(window.opera && document.getElementById);
	if(this.op) document.onmousedown = new Function('e',
		'if(((e = e || window.event).target || e.srcElement).tagName == "IMAGE") return false;');
	this.ie = !!(this.n.indexOf("msie") >= 0 && document.all && this.db && !this.op);
	this.iemac = !!(this.ie && this.n.indexOf("mac") >= 0);
	this.ie4 = !!(this.ie && !document.getElementById);
	this.n4 = !!(document.layers && typeof document.classes != dd_u);
	this.n6 = !!(typeof window.getComputedStyle != dd_u && typeof document.createRange != dd_u);
	this.w3c = !!(!this.op && !this.ie && !this.n6 && document.getElementById);
	this.ce = !!(document.captureEvents && document.releaseEvents && !this.n6);
	this.px = this.n4? '' : 'px';
	this.tiv = this.w3c? 40 : 10;
}

var dd = new WZDD();

dd.Int = function(d_x, d_y)
{
	return isNaN(d_y = parseInt(d_x))? 0 : d_y;
};

dd.getScrollX = function()
{
	return dd.Int(window.pageXOffset || (dd.db? dd.db.scrollLeft : 0));
};
dd.getScrollY = function()
{
	return dd.Int(window.pageYOffset || (dd.db? dd.db.scrollTop : 0));
};

dd.evt = function(d_e)
{
	this.but = (this.e = d_e || window.event).which || this.e.button || 0;
	this.button = (this.e.type == 'mousedown')? this.but
		: (dd.e && dd.e.button)? dd.e.button
		: 0;
	this.src = this.e.target || this.e.srcElement || null;
	this.src.tag = ("" + (this.src.tagName || this.src)).toLowerCase();
	this.x = dd.Int(this.e.pageX || this.e.clientX || 0);
	this.y = dd.Int(this.e.pageY || this.e.clientY || 0);
	if(dd.ie)
	{
		this.x += dd.getScrollX() - (dd.ie && !dd.iemac)*1;
		this.y += dd.getScrollY() - (dd.ie && !dd.iemac)*1;
	}
	this.modifKey = this.e.modifiers? this.e.modifiers&Event.SHIFT_MASK : (this.e.shiftKey || false);
};
/* * * * * * * * * * * * * * * * */
function cScroller () {//states: 1 - down , -1 - up , -2 - right , 2 - left
	this.objects = {};
	this.speed = 10;

	this.init = function (objID,state) {
		var obj = {};
		obj.obj = document.getElementById(objID);
		obj.state = state;
		obj.timer = null;
		obj.maxVertScroll = parseInt(obj.obj.scrollHeight) - parseInt(obj.obj.offsetHeight);
		obj.maxHorizScroll = (parseInt(obj.obj.scrollWidth) - parseInt(obj.obj.offsetWidth));
		this.objects[objID] = obj;
	}

	this.down = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,1);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = 1;
		}

		if ((obj.maxVertScroll > obj.obj.scrollTop) && (obj.state == 1)) {
			obj.obj.scrollTop = (obj.obj.scrollTop + this.speed);
			obj.timer = setTimeout(this.down.bind(this,id,true),100);
		}

	}

	this.right = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,-2);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = -2;
		}

		if ((obj.maxHorizScroll > obj.obj.scrollLeft) && (obj.state == -2)) {
			obj.obj.scrollLeft = (parseInt(obj.obj.scrollLeft) + this.speed);
			obj.timer = setTimeout(this.right.bind(this,id,true),100);
		}

	}


	this.up = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,-1);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = -1;
		}

		if ((obj.obj.scrollTop > 0) && (obj.state == -1)) {
			obj.obj.scrollTop = (parseInt(obj.obj.scrollTop) - this.speed);
			if (obj.obj.scrollTop < 0) obj.obj.scrollTop = 0;
			obj.timer = setTimeout(this.up.bind(this,id,true),100);
		}
	}


	this.left = function (id,timer) {
		if (this.objects[id] == undefined) {
			this.init(id,2);
		}
		var obj = this.objects[id];
		if (timer == undefined) {
			obj.state = 2;
		}

		if ((obj.obj.scrollLeft > 0) && (obj.state == 2)) {
			obj.obj.scrollLeft = (parseInt(obj.obj.scrollLeft) - this.speed);
			if (obj.obj.scrollLeft < 0) obj.obj.scrollLeft = 0;
			obj.timer = setTimeout(this.left.bind(this,id,true),100);
		}
	}

	this.stop = function (id) {
		if (this.objects[id]) {
			clearTimeout(this.objects[id].timer);
			this.objects[id].state = 0;
		}
	}



}
/* * * * * * * * * * * * * * * * */
var hiddenBlocks = {};

function setBlock(id,mode) {
	if (hiddenBlocks[id] == undefined) {
		hiddenBlocks[id] = document.getElementById(id);
	}
	var disp = mode ? 'block' : 'none';
	if (hiddenBlocks[id]) hiddenBlocks[id].style.display = disp;
}

/* * * * * * * * * * * * * * * * */
function clearTable(table) {
	if (table && table.tBodies) {
		while(table.tBodies.length > 0) {
			table.removeChild(table.tBodies[0]);
		}
	}
}
/* * * * * * * * * * * * * * * * */
function clearNode(node) {
	if (node.childNodes) {
		while(node.childNodes.length > 0) {
			node.removeChild(node.childNodes[0]);
		}
	}
}
/* * * * * * * * * * * * * * * * */
function fmtNumberEx(n,decimals)
     {
     var number = new Number(n);
     var num = number.toFixed(decimals).split('.');
     var newNum = "";
     var result = num[0];
     var remainder;
     while (result > 0)
     {
          result = Math.floor(num[0]/1000);
          remainder = num[0]%1000;
          num[0] = result;
          if (remainder < 1000) {remainder = ' ' + remainder;}
          newNum = "" + remainder + newNum;
     }
     return newNum + '.' + num[1];
}
/* * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * */
function fmtNumber(number, decimals, point, separator)
{
   if(!isNaN(number))
   {
        point = point ? point : '.';
        number = number.toString().split('.');
        separator = separator ? separator : ' ';
        if(separator)
        {
             var tmp_number = new Array();
             for(var i = number[0].length, j = 0; i > 0; i -= 3)
             {
                  var pos = i > 0 ? i - 3 : i;
                  tmp_number[j++] = number[0].substring(i, pos);
             }
             number[0] = tmp_number.reverse().join(separator);
        }
        if(decimals && number[1]) {
             //number[1] = Math.round(parseFloat(number[1].substr(0, decimals) + '.' + number[1].substr(decimals, number[1].length), 10));
             //number[1] = Math.round(parseFloat(number[1].substr(0, decimals) + '.' + number[1].substr(decimals, number[1].length), 10));
        } else {
        	number.push('00');
        }
        return(number.join(point));
   }
   else return(null);
}
/* * * * * * * * * * * * * * * * */
function cBubbleBlock () {
	this.blocks = {};
	this.timers = {};
	this.inited = false;
	this.init = function () {
		var elems = document.getElementsByTagName('div');
		for (var i = 0; i < elems.length; i++) {
			if (elems[i].className && (elems[i].className == 'bubble'))	{
				if (!elems[i].id) elems[i].id = 'bubble_' + i;
				addEvent(elems[i],'mouseover',this.over.bindAsEventListener(this,elems[i].id));
				addEvent(elems[i],'mouseout',this.out.bindAsEventListener(this,elems[i].id));
				this.blocks[elems[i].id] = elems[i];
			}
		}
		this.inited = true;
	}

	this.over = function (e,id) {
		if (!this.inited) this.init();
		if (this.blocks[id]) {
			if (this.timers[id]) {
				clearTimeout(this.timers[id]);
				this.timers[id] = false;
			}
			this.blocks[id].style.display = 'block';
		}
	}

	this.out = function (e,id,mode) {
		if (!this.inited) this.init();
		if (this.blocks[id]) {
			if (this.timers[id]) {
				clearTimeout(this.timers[id]);
				this.timers[id] = false;
			}
			if (mode == undefined)
			{
				this.timers[id] = setTimeout(this.out.bind(this,false,id,true),2000);
			} else {
				this.blocks[id].style.display = 'none';
			}
		}
	}

}
/* * * * * * * * * * * * * * * * */
function cLangMaster (lang,labels) {
	this.lang = lang||'ru';
	this.labels = labels||{};
	this.setLabel = function(label,value) {this.labels[label] = value};
	this.setLabels = function(labels) { this.labels = labels};
	this.getLabel = function(label) {return this.labels[label]||''};
}
/* * * * * * * * * * * * * * * * */
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}
/* * * * * * * * * * * * * * * * */
Function.prototype.bindAsEventListener = function(object) {
  var __method = this, args = $A(arguments), object = args.shift();
  return function(event) {
    return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments)));
  }
}
/* * * * * * * * * * * * * * * * * * * * * */
var $A = function(iterable) {
  if (!iterable) return [];

  var results = [];
  for (var i = 0, length = iterable.length; i < length; i++) results.push(iterable[i]);
  return results;

}
