var isIE = (document.all && document.getElementById) ? true: false;

var brname=navigator.appName, BrVer='';
if(brname.substring(0,2)=="Mi")
	BrVer='E';
var timer = 0;
lastid = -1;

function show(id)
{
	if(!((document.all)?document.all['menu'+id]:document.getElementById('menu'+id)))
		return;
	clearTimeout(timer);
	if((id != lastid) && (lastid!=-1))
		((document.all)?document.all['menu'+lastid]:document.getElementById('menu'+lastid)).style.visibility = 'hidden';
	hideElement("SELECT", document.getElementById('menu'+lastid));
	lastid = id;
	((document.all)?document.all['menu'+lastid]:document.getElementById('menu'+lastid)).style.visibility = 'visible';
}

function hidden(id)
{
	if(!((document.all)?document.all['menu'+id]:document.getElementById('menu'+id)))
		return;
	showElement("SELECT");
	timer = setTimeout("if('"+id+"' == '"+lastid+"'){((document.all)?document.all['menu"+lastid+"']:document.getElementById('menu"+lastid+"')).style.visibility = 'hidden';}", 500)
}


function GetPos(el)
{
	if (!el || !el.offsetParent)return false;
	var res=Array()
	res["left"] = el.offsetLeft;
	res["top"] = el.offsetTop;
	var objParent = el.offsetParent;
	while (objParent.tagName.toUpperCase()!="BODY")
	{
		res["left"] += objParent.offsetLeft;
		res["top"] += objParent.offsetTop;
		objParent = objParent.offsetParent;
	}
	res["right"]=res["left"]+el.offsetWidth;
	res["bottom"]=res["top"]+el.offsetHeight;
	return res;
}

function hideElement(elName, Menu)
{
	if(BrVer!='E') return;
	for (i = 0; i < document.all.tags(elName).length; i++)
	{
		Obj = document.all.tags(elName)[i];
		if(!(pMenu=GetPos(Menu)))continue;
		if(!(pObj=GetPos(Obj)))continue;

		if(pObj["left"]<pMenu["right"] && pMenu["left"]<pObj["right"] && pObj["top"]<pMenu["bottom"] && pMenu["top"]<pObj["bottom"])
			Obj.style.visibility = "hidden";
	}
}

function OnW(url, name, scr, w, h)
{
name=window.open(url,name,'toolbar=no,location=no,status=no,menubar=no,resizable=yes,directories=no,scrollbars=yes,width='+w+',height='+h+'');
}

function showElement(elName)
{
	if(BrVer!='E') return;
	for (i = 0; i < document.all.tags(elName).length; i++)
	{
		obj = document.all.tags(elName)[i];
		if (!obj || !obj.offsetParent)continue;
		if(obj.style.visibility=="hidden")
			obj.style.visibility = "visible";
	}
}

function show_hide(obj) 
      { 
          if (document.getElementById(obj).style.display == 'none') 
              document.getElementById(obj).style.display = ''; 
          else 
              document.getElementById(obj).style.display = 'none'; 
      } 

function isValidEmail(email)
        {
                var not_valid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
                var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

                if (!not_valid.test(email) && valid.test(email)) return true;
                return false;
        }
        
function checkEMail(theForm)
        {
        	if (theForm.sf_EMAIL.value.length < 1)
        		{
                alert("Ââåäèòå àäðåñ e-mail!");
                theForm.sf_EMAIL.focus();
                return false;
        		}
            if (theForm.sf_EMAIL.value.length < 6)
            	{
                	alert("Ââåäåíûé e-mail íåêîððåêòåí!");
                   	theForm.sf_EMAIL.focus();
                	return false;
            	}
            if (!isValidEmail(theForm.sf_EMAIL.value))
            	{
                    alert("Ââåäåííûé e-mail íåêîððåêòåí!");
                   	theForm.sf_EMAIL.focus();
                	return false;
            	}
        	return true;
        }
        
function checkFields(theForm)
        {
        	if (theForm.USER_LOGIN.value.length < 1)
                {
                        alert("Ââåäèòå ëîãèí!");
                        theForm.USER_LOGIN.focus();
                        return false;
                }
            if (theForm.USER_PASSWORD.value.length < 1)
                {
                        alert("Ââåäèòå ïàðîëü!");
                        theForm.USER_PASSWORD.focus();
                        return false;
                }
        	return true;
        }


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


function preloadImages() {
  MM_preloadImages('/bitrix/templates/top-corporate/images/topline/nav_topkniga_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_litera_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_lasknigas_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_knigomir_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_kv_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_opt_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_inet_act.jpg','/bitrix/templates/top-corporate/images/topline/nav_forum_act.jpg');
}



Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};


var CurrentMonth = new Date().getMonth() + 1;

//if(Array(6,7,8).inArray(CurrentMonth)) CurrentSeason = "summer";
if(Array(9,10,11).inArray(CurrentMonth)) CurrentSeason = "autumn";
if(Array(12,1,2).inArray(CurrentMonth)) CurrentSeason = "winter";
if(Array(3,4,5,6,7,8).inArray(CurrentMonth)) CurrentSeason = "spring";


// screen keyboard

function show_keyboard(form_name, element_name, w,h) {
    if(!isIE) {
	window.open("/keyboard.php?targ="+window.name+"&fname="+form_name+"&ename="+element_name,'keyboard',"width="+w+",height="+h+",copyhistory=no,directories=no,menubar=no,resizable=yes,status=no,toolbar=no,scrollbars=no,always_raised=yes,dependent=yes");
    }
    else {
 	kbd_layer_control();
    }
}

var kbd_layer_ready = false;
function kbd_layer() {
    var rows = new Array();
    rows[0] = new Array('1','2','3','4','5','6','7','8','9','0','-','(',')');
    rows[1] = new Array('É','Ö','Ó','Ê','Å','Í','Ã','Ø','Ù','Ç','Õ','Ú');
    rows[2] = new Array('Ô','Û','Â','À','Ï','Ð','Î','Ë','Ä','Æ','Ý');
    rows[3] = new Array('ß','×','Ñ','Ì','È','Ò','Ü','Á','Þ','¨');
    var str = '';
    for( var i=0; i<4;i++) {
	row = rows[i];
	len = row.length;
	str +='<tr><td valign="top" align=center><table cellpadding="1" cellspacing="1" border="0"><tr>';
	for(var j=0 ;j <= len; j++) {
	    if(j != len) {
		str+='<td><button onClick=\'addChar("'+row[j]+'")\' class=btn>'+row[j]+'</button></td>';
	    }
	    else {
		if(!i) {
		    str+='<td><button onClick="backspace()" class=btn><img src=/images/larr10x10.gif width=10 height=10 border=0></button></td>';
		}
	    }
	}		
	str += '</tr></table></td>';
    }
    l.insertAdjacentHTML('BeforeEnd', '<table cellpadding=0 cellspacing=0 style="background-color:transparent" border=0><tr><td bgcolor="#004262"><img src=/images/c.gif width=1 height=15></td><td bgcolor="#004262" align=right><a style="text-decoration:none;color:white;font-family:Arial, Geneva, Helvetica, Sans-serif;font-weight:bold" href="javascript:kbd_layer_control()">x</a></td><td bgcolor="#004262"><img src=/images/c.gif width=1 height=15></td></tr><tr><td bgcolor="#004262"><img src=/images/c.gif width=1></td><td><table cellpadding="1" cellspacing="1" border="0" style="background-color:white" align=center valign=middle>'+str+'<tr><td valign="top" align=center><table cellpadding="1" cellspacing="1" border=0><tr><td><button onClick="addChar(\' \')" class=btn_big>SPACE</button></td></tr></table></td></tr></table><td bgcolor="#004262"><img src=/images/c.gif width=1></td></tr><tr><td bgcolor="#004262" colspan=3><img src=/images/c.gif width=height=1></td></tr></table>');
    str ='';	
    kbd_layer_ready = true;
}

function kbd_layer_control() {
	if(!kbd_layer_ready) {
	    kbd_layer();
	}
	
	if(l.style.visibility == 'hidden') {
	    l.style.visibility = 'visible';
	}
	else {
	    l.style.visibility = 'hidden'
	}
	document.onmousedown = MouseDown;
        document.onmouseup = MouseUp;
    }

function addChar(c) {
    document.forms['searchform'].elements['query'].value+=c;
}
    
function backspace(c) {
    var cur_element = document.forms['searchform'].elements['query'];
    cur_element.value=cur_element.value.substring(0, cur_element.value.length - 1);
}

// end of screen keyboard

var oldX, oldY;
function MouseDown() {
    if(l.style.visibility=='visible') {
	var myLeft = parseInt(l.style.left);
	var myRight = myLeft + l.offsetWidth;
	var myTop = parseInt(l.style.top);
    }
    if((event.clientX >= myLeft && event.clientX <= myRight)&& (event.clientY >= myTop && event.clientY <= myTop + 20))
    {
	oldX = event.offsetX;
	oldY = event.offsetY;
	document.onmousemove=MouseMove;
    }
}

function MouseUp() {
    document.onmousemove=new Function("event.returnValue = false;");
}

function MouseMove() {
    var startX = event.clientX + document.body.scrollLeft;
    var startY = event.clientY + document.body.scrollTop;
    var moveX  = 0;
    var moveY = 0;
    var parent = l.offsetParent;
    while (parent.offsetParent != null) {
	intLessTop += parent.offsetTop;
	intLessLeft += parent.offsetLeft;
	parent = parent.offsetParent;
    }
    l.style.pixelTop = startY - moveY - oldY;
    l.style.pixelLeft = startX - moveX - oldX;
    event.returnValue = false;
}
