/*******************************
*   COMMON functions
********************************/

var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
var isNetscape = (navigator.appName == "Netscape");


// Change object size
// cmd (1 - Increase, 2 - Decrease)
// direction (1 - width, 2 - height, 3 - both)
// pref - preffix for the cookies
// obj - object
function FrmChangeSize(cmd, direction, pref, obj, step, minw, maxw, minh, maxh)
{
	if(!(cmd==1 || cmd==2)) return false;
	if(!(direction==1 || direction==2 || direction==3) ) return false;
	if(!step) step=50;
	if(!minh || !minw || !maxh || !maxw) return false;
	var obj = document.getElementById(obj);
	if(!obj) return false;
	if(!pref) pref = "pref_";

	var w=parseInt(obj.style.width);
	var h=parseInt(obj.style.height);

	if(cmd==1)
	{
		if( (direction == 1) || (direction == 3) )
		{
			if( (w + step) <= maxw ) w = w + step;
		}
		if( (direction == 2) || (direction == 3) )
		{
			if( (h + step) <= maxh ) h = h + step;
		}
	}
	if(cmd==2)
	{
		if( (direction == 1) || (direction == 3) )
		{
			if( (w - step) >= minw ) w = w - step;
		}
		if( (direction == 2) || (direction == 3) )
		{
			if( (h - step) >= minh ) h = h - step;
		}
	}

	obj.style.width = w+'px';
	obj.style.height = h+'px';
	var expDate = new Date;
	expDate.setTime (expDate.getTime() + (600*24*60*60*1000));
	SetCookie(pref + "w", w, expDate, "/");
	SetCookie(pref + "h", h, expDate, "/");
}

/**
* Устанавливаем Cookie
*/
function SetCookie(name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape(value)
  + ((expires) ? "; expires=" + expires.toGMTString() : "")
  + ((path) ? "; path=" + path : "")
  + ((domain) ? "; domain=" + domain : "")
  + ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

/**
* Открываем всплывающее окно
* @param
* url - ссылка
* imgname - название окна
* w, h - ширина, высота
*/
function OpenWindow(url, imgname, w, h){
	var features='left=0,top=0,resizable=1,menubar=0,location=0,status=0,toolbar=0';
	wm=window.screen.width-10;hm=window.screen.height-60;
	if(w>wm || h>hm){features=features+',scrollbars=1';}else{features=features+',scrollbars=0';}
	if(w>wm){features=features+',width='+wm;}else{features=features+',width='+w;}
	if(h>hm){features=features+',height='+hm;}else{features=features+',height='+h;}
	window.open(url, imgname, features, true);
}

/**
* найти объект по ID
*/
function getElem(div) {
	return document.getElementById ? document.getElementById(div) : document.all[div]
}


/**
* Проверка Email
*/
function isEmail(email)
{
	var arr1 = email.split("@");
	if (arr1.length != 2) return false;
	else if (arr1[0].length < 1) return false;
	var arr2 = arr1[1].split(".");
	if (arr2.length < 2) return false;
	else if (arr2[0].length < 1) return false;
	return true;
}


/**
* Проверка строки на пустоту
*/
function isEmpty (txt)
{
	var ch;
	if (txt == "") return true;
	for (var i=0; i<txt.length; i++){
		ch = txt.charAt(i);
		if (ch!=" " && ch!="\n" && ch!="\t" && ch!="\r") return false;
	}
	return true;
}

/**
* Показать/Спрятать элемент
* @param
* name - name of object
* value - show/hide/
*/
function ShowHideElement(name, value) {
	var obj = getElem(name);
	if(typeof(obj)!='undefined')
	{
		if(obj.style.display == 'block' || value == 'hide')
			obj.style.display = 'none';
		else if(obj.style.display == 'none' || value == 'show')
			obj.style.display = 'block';
	}
}

function delaybanner(){
	if (timerID) clearTimeout(timerID);
	self.focus();
}

function pup(Url,width,height){
	if (width!=''){
		test_width=parseInt(width);
		if (isNaN(test_width)) width=240;
	}else{
		width=screen.width - 10;
	}
	if (height!=''){
		test_height=parseInt(height);
		if (isNaN(test_height)) height=400;
	}else{
		height=screen.height - 60;
	}
	open(Url, 'popup_autochel', 'titlebar=no,height='+height+',width='+width+',scrollbars=no');
	timerID=setTimeout('delaybanner()',8000);
}


var block_width = 200; //в CSS для блока (стиль footnote_popup) должна быть указана такая же длина (width)!

// получение координат курсора мыши
function GetCoords(event) {
	var x = y = 0;
	
	if (document.attachEvent != null) // Internet Explorer & Opera
	{
		x = window.event.clientX +
			(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		y = window.event.clientY + 
			(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}
	else if (!document.attachEvent && document.addEventListener) // Gecko
	{
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	} else {}
	
	return {x:x, y:y};
}

// показ блока в месте клика (по полученным координатам)
function ShowFootnote(event, id) {
	var event = event || window.event;
	
	var ss = ScreenSize(); // получение размеров клиентской части окна - координаты блока рассчитываются таким образом, чтоб блок не выползал за рабочую область
	var x = GetCoords(event).x;
	var x_delta = ss.w - block_width;
	var dx = 30; // дизайнерский отступ "для красоты" :) по абсциссе
	var dy = 5;  // дизайнерский отступ "для красоты" :) по ординате
	
	document.getElementById(id).style.left = (x > x_delta ? x_delta - dx : (x < 0 ? dx : x));
	document.getElementById(id).style.top = GetCoords(event).y + dy;
	document.getElementById(id).style.display = 'block';
}

// получение размера клиентской части окна
function ScreenSize() {
	var w, h; // w - длина клинетской области, h - высота клинетской области
	
	w = (window.innerWidth ? window.innerWidth :
			(document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth)
		);
	h = (window.innerHeight ? window.innerHeight :
			(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight)
		);
			
	return {w:w, h:h};
}

// установить/снять все флажки checkbox
// checks	- имя набора чекбоксов (например, ids[])
// val		- значение, которое нужно установить (1 || 0)
function SetChecks(checks, val, id_div)
{
	elts = document.getElementsByName(checks);
	for(i=0; i<elts.length; i++)
	{
		elts[i].checked = val;
	}

	// скрытие всех объектов класса
	divs = document.getElementsByTagName('div');
	for(i=0; i<divs.length; i++)
	{
		if(divs[i].className == 's_divs')
			divs[i].style.display = 'none';
	}
		
	// показ нужного объекта
	document.getElementById(id_div).style.display = 'block';
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
};
/**
 * Copyright (c) "IG 74", LTD
 * Content: Functions to make money
 */

/**
 * Money postloader
 */

function sb_loader(){
	this.domain = '';
	this.place_tag = 'div'; // тэг для баннерных мест
	this.place_class = 'surbis_banner'; // опознавательный класс для баннерных мест
	this.place_params_tag = 'p'; // тэг для параметров баннерных мест
	this.default_b_type = 'multi'; // как запрашивать (multi - пачками, single - по одному)
	this.default_prog = 'http://www.surbis.ru/cgi-bin/banjs_inner.cgi';
	this.default_multi_prog = 'http://www.surbis.ru/cgi-bin/banjs_inner.cgi';
	this.multi_max = 32;
	this.rule = [[3, 1, 1, 3, ''],[3, 3, 1, 3, ''],[3, 2, 2, 4, ''],[2, 0, 0, 1, "-/."],[3, 2, 1, 2, ''],[3, 3, 1, 4, ''],[3, 1, 2, 3, ''],[3, 2, 1, 3, ''],[2, 0, 1, 1, "/"],[2, 0, 1, 1, "strx"],[3, 3, 3, 5, ''],[2, 0, 1, 1, "/"]];
	this.rule_multi = [[3, 1, 1, 3, ''],[3, 3, 1, 3, ''],[3, 2, 2, 4, ''],[2, 0, 0, 1, "-/."],[3, 2, 1, 2, ''],[3, 3, 1, 4, ''],[3, 1, 2, 3, ''],[3, 2, 1, 3, ''],[2, 0, 1, 1, "/"],[2, 0, 1, 1, "imva"],[3, 3, 3, 5, ''],[2, 0, 1, 1, "/"]];
	this.t = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
}

sb_loader.prototype.rand = function(a, b){
	if(a > b){var t = a;a = b;b = t;}
	return Math.floor(Math.random() * (b - a + 1) + a);
}

sb_loader.prototype.randstr = function(rule){
	var scnt, cnt, buf = '', symb = '';
	for(var i in rule){
		switch(rule[i][0]){
		case 1: break;
		case 2: 
			if(rule[i][3] != rule[i][2]) cnt = this.rand(rule[i][2], rule[i][3]);
			else cnt = rule[i][3];
			scnt = rule[i][4].length - 1;
			for(var j = 0; j < cnt; j++) buf+= rule[i][4].charAt(this.rand(0, scnt));
			break;
		case 3:
			if(rule[i][3] != rule[i][2]) cnt = this.rand(rule[i][2], rule[i][3]);
			else cnt = rule[i][3];
			scnt = -1;
			if(rule[i][1] & 1) scnt+= 26;
			if(rule[i][1] & 2) scnt+= 10;
			if(scnt == 0 || cnt == 0) break;
			for(var j = 0; j < cnt; j++){
				symb = this.rand(0, scnt) + 48;
				if(!(rule[i][1] & 2) && rule[i][1] & 1) symb+= 49;
				else if(rule[i][1] & 2 && rule[i][1] & 1 && symb > 57) symb+= 39;
				buf+= String.fromCharCode(symb);
			}
			break;
		}
	}
	return buf;
}

sb_loader.prototype.init = function(f_load){
	this.default_prog = '/'+this.randstr(this.rule);
	this.default_multi_prog = '/'+this.randstr(this.rule_multi);

	this.addEvent(window, 'load', f_load, false);
	this.getDomain();
};

sb_loader.prototype.getElementsByClass = function(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
};

sb_loader.prototype.addEvent = function(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
};

sb_loader.prototype.getDomain = function(){
	this.domain = window.location.hostname;
	if(this.domain.substr(0, 4) == 'www.') this.domain = this.domain.substr(4);
};

sb_loader.prototype.show = function(){
	var multi_banners = {};
	var surbis_banners = this.getElementsByClass(this.place_class, null, this.place_tag);
	if(surbis_banners.length==0) return false;
	var surbis_bannersLen = surbis_banners.length;
	for (var i = 0; i < surbis_bannersLen; i++) {
		var prog = this.default_prog;
		var multi_prog = this.default_multi_prog;
		var b_type = this.default_b_type;
		var params = {
			'p': this.domain
		};
		var els = surbis_banners[i].getElementsByTagName(this.place_params_tag);
		var elsLen = els.length;
		for(var j = 0; j < elsLen; j++) {
			if( els[j].className == '' ) continue;
			if( els[j].className == 'prog' ){
				prog = els[j].innerHTML;
				multi_prog = els[j].innerHTML;
				continue;
			}
			if( els[j].className == 'type' ){
				b_type = els[j].innerHTML;
				continue;
			}
			params[els[j].className] = els[j].innerHTML;
		}
		var query = '';
		for(var par in params){
			if(typeof params[par] == 'string'){
				query += '&' + par + '=' + escape(params[par]);
			}
		}
		if(b_type == 'single')
		{
			this.putScript(prog, surbis_banners[i].id.substr(3), params.p);
		}
		else
		{
			if(typeof multi_banners[multi_prog] == 'undefined'){ multi_banners[multi_prog] = {};}
			if(typeof multi_banners[multi_prog][query] == 'undefined'){ multi_banners[multi_prog][query] = new Array;}
			multi_banners[multi_prog][query].push(surbis_banners[i].id.substr(3));
		}
	}
	// выгребаем баннеры пачками
	for(var bans in multi_banners){
		if(typeof multi_banners[bans] == 'object'){
			for(var bans2 in multi_banners[bans]){
				if(typeof multi_banners[bans][bans2] == "object" )
				{
					while( multi_banners[bans][bans2].length > this.multi_max ){
						var bbb = multi_banners[bans][bans2].slice(0, this.multi_max);
						this.putScript(bans, bbb.join(","), params.p);
						multi_banners[bans][bans2] = multi_banners[bans][bans2].slice(this.multi_max)
					}
					if(multi_banners[bans][bans2].length > 0){
						this.putScript(bans, multi_banners[bans][bans2].join(","), params.p);
					}
				}
			}
		}
	}
};

sb_loader.prototype.putScript = function(prog, id, query){
	var js = document.createElement('SCRIPT');
	js.type = 'text/javascript';
	js.language = 'javascript';
	js.src = prog + this.encode(id) + '/' + this.encode(query) + '/';
	document.body.appendChild(js);
};

sb_loader.prototype.encode = function(s){
	var p = 0;var l = s.length;var d = '';
	while(true){
		d+= this.t.charAt(s.charCodeAt(p) >> 2);
		if(l == 1){
			d+= this.t.charAt((s.charCodeAt(p) & 3) << 4);
			break;}
		d+= this.t.charAt((s.charCodeAt(p + 1) >> 4) + ((s.charCodeAt(p) & 3) << 4));
		if(l == 2){
			d+= this.t.charAt((s.charCodeAt(p + 1) & 15) << 2);
			break;}
		d+= this.t.charAt((s.charCodeAt(p + 2) >> 6) + ((s.charCodeAt(p + 1) & 15) << 2));
		d+= this.t.charAt(s.charCodeAt(p + 2) & 63);

		if(l <= 3)break;
		l-= 3;p+= 3;}
	return d;
};

sb_loader.prototype.decode = function(s){
	var p = 0;var l = s.length;var d = '';var c0,c1,c2,c3;
	while(true){
		if(l >= 1) c0 = this.t.indexOf(s.charAt(p));		else c0 = 0;
		if(l >= 2) c1 = this.t.indexOf(s.charAt(p + 1));	else c1 = 0;
		if(l >= 3) c2 = this.t.indexOf(s.charAt(p + 2));	else c2 = 0;
		if(l >= 4) c3 = this.t.indexOf(s.charAt(p + 3));	else c3 = 0;
		if(l == 1){
			d+= String.fromCharCode((c0 << 2) & 255);
			break;}
		d+= String.fromCharCode(((c1 >> 4) + (c0 << 2)) & 255);
		if(l == 2){
			d+= String.fromCharCode((c1 << 4) & 255);
			break;}
		d+= String.fromCharCode(((c1 << 4) + (c2 >> 2)) & 255);
		if(l == 3){
			d+= String.fromCharCode((c2 << 6) & 255);
			break;}
		d+= String.fromCharCode(((c2 << 6) + c3) & 255);

		if(l <= 4)break;
		l-= 4;p+= 4;}
	return d;
};

var sb_loader_ex = new sb_loader();
sb_loader_ex.init(function(){sb_loader_ex.show();});


/**
 * Richmedia functions
 */

function sc_rich_loader(name){
	this.banners = new Array();
	this.bans = null;
	this.showed = new Array();
	this.tohide = new Array();
	this.is_popupscript=1;
}

sc_rich_loader.prototype.showPopupBunner = function(a)
{
	obj = (a) ? a.target : event.srcElement;
	sc_rich_loader_ex.bans = obj;
	setTimeout('sc_rich_loader_ex.showPopupBunner_()', 200);
};

sc_rich_loader.prototype.showPopupBunner_ = function()
{
	if(this.bans != null)
	{
		obj = document.getElementById(this.bans.id+'_big');
		obj.style.display='block';
		this.showed[this.bans.id] = true;
		this.tohide[this.bans.id] = false;
	}
};

sc_rich_loader.prototype.hidePopupBunner = function(a)
{
	obj = (a) ? a.target : event.srcElement;
	sc_rich_loader_ex.bans = sc_rich_loader_ex.banners[obj.id];
	sc_rich_loader_ex.tohide[sc_rich_loader_ex.bans] = true;
	setTimeout('sc_rich_loader_ex.hidePopupBunner_(\''+sc_rich_loader_ex.bans+'\')', 1000);
};

sc_rich_loader.prototype.hidePopupBunner_ = function(name)
{
	if(this.showed[name] == true && this.tohide[name] == true)
	{
		obj = document.getElementById(name+'_big');
		obj.style.display='none';
		this.bans = null;
		this.showed[name] = false;
		this.tohide[name] = false;
	}
};

sc_rich_loader.prototype.popupMouseMove = function(a)
{
	obj = (a) ? a.target : event.srcElement;
	if(sc_rich_loader_ex.bans != null && sc_rich_loader_ex.showed[sc_rich_loader_ex.bans.id] == false)
	{
		if(obj != sc_rich_loader_ex.bans)
			sc_rich_loader_ex.bans = null;
	}
	if(sc_rich_loader_ex.bans != null && sc_rich_loader_ex.showed[sc_rich_loader_ex.bans.id] == true && sc_rich_loader_ex.tohide[sc_rich_loader_ex.bans.id] == false)
	{
		if(obj.id != sc_rich_loader_ex.bans.id+'_hide')
		{
			sc_rich_loader_ex.tohide[sc_rich_loader_ex.bans.id] = true;
			sc_rich_loader_ex.bans = sc_rich_loader_ex.bans.id;
			setTimeout('sc_rich_loader_ex.hidePopupBunner_(\''+sc_rich_loader_ex.bans+'\')', 1000);
		}			
	}
};

sc_rich_loader.prototype.addPopupBanner = function(name,doc)
{
	this.banners[name+'_hide'] = name;
	doc.getElementById(name).onmouseover = this.showPopupBunner;
	doc.getElementById(name+'_hide').onmouseout = this.hidePopupBunner;
	doc.body.onmousemove = this.popupMouseMove;
	this.showed[name] = false;
	this.tohide[name] = false;
};

var sc_rich_loader_ex = new sc_rich_loader();;
