var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
var isNetscape = (navigator.appName == "Netscape");

function ShowHideElement(name)
{
	var obj = document.getElementById(name);
	if(obj.style.display == "none")
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

function VarOpenImg(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);
}

// Change object size 
// cmd (1 - Increase, 2 - Decrease)
// direction (1 - width, 2 - height, 3 - both)
// obj - object
function FrmChangeSize(cmd, direction, 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=40;
	if(!minh || !minw || !maxh || !maxw) return false;
	var obj = document.getElementById(obj);
	if(!obj) return false;

	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("var_otziv_w", w, expDate, "/");
	SetCookie("var_otziv_h", h, expDate, "/");
}



//==================================
// COOKIES FUNCTION
//==================================
//"Внутренняя" функция для сохранения декодированного значения
function getCookieVal (offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
/*Эта функция необходима для исправления ошибок в дате на Mac 2.x
 *Вызывайте её для проверки объекта даты перед отправкой данных в функцию SetCookie
 *Эта функция должна вызываться только 1 раз для каждого полученного объекта даты
 */
function FixCookieDate(date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)date.setTime(date.getTime() - skew);
}
/*Эта функция предназначена для возвращения значения cookie
 *name - строковой объект, содержащий имя cookie
 *returns - строковой объект, содержащий либо значение cookie, либо 0, если запись не существует
 */
function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring( i , j ) == arg) return getCookieVal(j);
        i = document.cookie.indexOf(" ", i ) + 1;
        if (i==0) break;
    }
    return null;
}
/*Эта функция предназначена для создания или обновления cookie
 *name - строковой объект, содержащий имя cookie
 *value - строковой объект, содержащий значение cookie
 *[expires] - объект Date, содержащий срок годности cookie
 *[path] - Строковой объект, указывающий путь нужной cookie-записи.
 *[domen] - Строковой объект, указывающий домен нужной cookie-записи.
 *[secure] - Булево значение, определяет необходимость безопасного соединения 
 */
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 getElem(div) {
	return document.getElementById ? document.getElementById(div) : document.all[div]
}
function getElem2(div, obj) {
	if(isMSIE){
		return obj.Document.getElementById ? obj.Document.getElementById(div) : obj.Document.all[div];
	}
	if(isOpera){
		return obj.contentDocument.getElementById ? obj.contentDocument.getElementById(div) : obj.contentDocument.all[div];
	}
	if(isNetscape){// Mozilla
		return obj.contentDocument.getElementById ? obj.contentDocument.getElementById(div) : obj.contentDocument.all[div];
	}
}

function newImage(pimg)
{
  if (document.images)
  {
    rslt = new Image();
    rslt.src = pimg;
    return rslt;
  }
}

/**
* Проверка 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;
}


// добавляем элемент в строковый массив в куках
//=============================================
function array_cookie_add(id, name, expDate)
{
	var str = GetCookie(name);
	if(str == null) str = "";
	var newstr = str;
	if(str.length>0)
	{
		str = '_' + str + '_';
		if( str.indexOf('_' + id + '_') == -1 )
		{
			newstr = newstr + '_' + id;
		}
	}
	else
	{
		newstr = id;
	}
	SetCookie(name, newstr, expDate, "/");
}
// удаляем элемент из строкового массива в куках
//=============================================
function array_cookie_delete(id, name, expDate)
{
	var str = GetCookie(name);
	if(str == null) str = "";
	var newstr = str;
	if(str.length>0)
	{
		newstr = "";
		var arr = str.split('_');
		for(var i = 0; i<arr.length; i++ )
		{
			if(arr[i] != id) newstr = newstr + (newstr?"_":"") + arr[i];
		}
	}
	SetCookie(name, newstr, expDate, "/");
}
