var printWindow = "";

function printArticle(aid)
{
	if (!aid) return false;
	if (printWindow) printWindow.close();
	printWindow = window.open(site_root+"/articles/?aid="+aid+"&print="+aid, "print", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,top=20,left=50,width=700,height=600");
}

function printArticleEng(aid)
{
	if (!aid) return false;
	if (printWindow) printWindow.close();
	printWindow = window.open(site_root+"/articles/?aid="+aid+"&print_e="+aid, "print", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,top=20,left=50,width=700,height=600");
}

var s2fWindow = "";

function send2friendEng(aid)
{
	if (!aid) return false;
	if (s2fWindow) s2fWindow.close();
	s2fWindow = window.open(site_root+"?send2friend_e="+aid, "s2f", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,top=20,left=50,width=700,height=500");
}

//---------------------------------------------------------------------
// Function to return the value of the cookie specified by "name".

// Parameter:
//     name     String object containing the cookie name.

// Return:      String object containing the cookie value, or null if
//              the cookie does not exist.
//---------------------------------------------------------------------
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;
}
//---------------------------------------------------------------------
// Function to get a cookie.
//---------------------------------------------------------------------
function getCookieVal( offset )
{
    var endstr = document.cookie.indexOf (";", offset);

    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
//---------------------------------------------------------------------
// Function to set a cookie.
//---------------------------------------------------------------------
function SetCookie( name, value )
{
    var argv    = SetCookie.arguments;
    var argc    = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path    = (argc > 3) ? argv[3] : null;
    var domain  = (argc > 4) ? argv[4] : null;
    var secure  = (argc > 5) ? argv[5] : false;

    document.cookie =
        name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}
//---------------------------------------------------------------------
// Function to delete a cookie. (Sets expiration date)
//    name - String object containing the cookie name
//---------------------------------------------------------------------
function DeleteCookie (name)
{
    var exp  = new Date();
    var cval = GetCookie (name);

    exp.setTime (exp.getTime() - 1);  // This cookie is history
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function clickItem(id)
{
//	alert(0);
	var i=$("i"+id);
	var d=$("div"+id);
	var dsd=d.visible();

	if (!d.innerHTML.length)
	{
		new Ajax.Request('/szi?menuexpand='+id,
		{
			method: 'get',
			onFailure: function(tr) { alert(tr.statusText) },
			onSuccess: function (tr) {
				d.update(tr.responseText)
			}
		});
	}

	if (dsd) {d.hide()} else {d.show()};
	i.src=dsd?"/v2/art/plusffffff.gif":"/v2/art/minusffffff.gif";
}

function checkEmail(frm)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	var error = "";
	if (!frm.email.value.length)
	{
		error = "Вы не ввели свой e-mail.";
	}
	else if (!filter.test(frm.email.value))
	{
		error = "Проверьте правильность ввода e-mail.";
	}

	if (error.length)
	{
		alert("Ошибка. "+error);
		frm.email.focus();
		return false;
	}
	return true;
}

function flipPanel(panel)
{
	var cookie = GetCookie("hide_" + panel);
	if (cookie)
	{
		DeleteCookie("hide_" + panel);
		if (document.getElementById)
		{
			document.getElementById("panel_" + panel).style.display = "block";
			document.getElementById("pm_" + panel).src = site_root + "/v2/art/minusffffff.gif";
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		SetCookie("hide_" + panel, 1);
		if (document.getElementById)
		{
			document.getElementById("panel_" + panel).style.display = "none";
			document.getElementById("pm_" + panel).src = site_root + "/v2/art/plusffffff.gif";
			return false;
		}
		else
		{
			return true;
		}
	}
}
