/*******************************
*   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];}
}