var ns4=(document.layers)?true:false;
var $a=(document.all)?true:false;
var $b=true
//(document.getElementById&&!$a)?true:false;
var $c=navigator.userAgent.toLowerCase();
var $d=($c.indexOf("mac")!=-1);
var $e;
var vkWin;
var bShown=false;
var $f=false;
var $g;
var sLastKbd;
var $h=null;
var $i=false;
var $j;
var elLastFocus;


function drag_dropns(name)
{	
	if(!ns4)return;
	$o=eval(name);
	$o.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
	$o.onmousedown=gons;
	$o.onmousemove=dragns;
	$o.onmouseup=stopns;
};

function gons(e)
{	
	$o.captureEvents(Event.MOUSEMOVE);
	$l=e.x;
	$m=e.y;
};

function dragns(e)
{	
	if(dragswitch==1)
	{	
		$o.moveBy(e.x-$l,e.y-$m);
		return false;
	}
};

function stopns()
{	
	$o.releaseEvents(Event.MOUSEMOVE);
	$o.releaseEvents(Event.MOUSEDOWN|Event.MOUSEUP);
};



function startDrag(cx,cy)
{
	$p=true;
	var el=document.getElementById("kwin");
	if(!el.style.left)el.style.left=el.offsetLeft;
	if(!el.style.top)el.style.top=el.offsetTop;
	$q=parseInt(el.style.left);
	$r=parseInt(el.style.top);
	$s=cx;
	$t=cy;
};

function stopDrag()
{	
	$p=false;
	regainFocus();
};

function dragBox($u)
{
	e=$u||event;
	if($p==true)
	{
		$v=$q+e.clientX-$s;
        $w=$r+e.clientY-$t;
        document.getElementById("kwin").style.left=$v;
        document.getElementById("kwin").style.top=$w;
        return false;
	};
	if(document.ddonmousemove)document.ddonmousemove($u);
};

if(!document.ddonmousemove)document.ddonmousemove=document.onmousemove;
document.onmousemove=dragBox;

function isEditBox($x)
{
	if($a&&($x.isContentEditable!=null))return $x.isContentEditable;
	else
	{
		if(($x.type=="text")||($x.type=="textarea"))return true;
	};
return false;
};

function HookElement($x)
{
    if(($x.vkhooked==null))
    {
    	$x.oldonblur=$x.onblur;
	    $x.oldonfocus=$x.onfocus;
	    $x.oldonchange=$x.onchange;
	    $x.oldonkeydown=$x.onkeydown;
	    $x.oldonkeyup=$x.onkeyup;
	    $x.oldonclick=$x.onclick;
	};
	if(!$i||($x.vkhooked==null))
	{
		$x.onfocus=onFocusChange;
		$x.onblur=onBlur;
		$x.onchange=onFocusChange;
		$x.onkeydown=onKeyDown;
		$x.onkeyup=onKeyUp;
		$x.onclick=onClick;
		$x.vkhooked=true;
	}
};

function UnHookElement($x)
{
	$x.onfocus=$x.oldonfocus;
	$x.onchange=$x.oldonchange;
	$x.onkeydown=$x.oldonkeydown;
	$x.onkeyup=$x.oldonkeyup;
	$x.onclick=$x.oldonclick;
};

function initFocus()
{
	var $x;
	var i,j,k;
	var $y=window.parent.frames;
	$e=($y.length>0);
	if($e)
	{
		for(k=0;k<$y.length;k++)
		{
			try
			{
				if($y[k].document!=null)
				{
					for(i=0;i<$y[k].document.forms.length;i++)
					{
						for(j=0;j<$y[k].document.forms[i].elements.length;j++)
						{
							$x=$y[k].document.forms[i].elements[j];
							if(isEditBox($x))
							{
								if($h==null)$h=$x;
								HookElement($x);
							}
						}
					}
				}
			} 
			catch($z)
			{
			}
		}
	};
	for(i=0;i<document.forms.length;i++)
	{
		for(j=0;j<document.forms[i].elements.length;j++)
		{
			$x=document.forms[i].elements[j];
			if(isEditBox($x))
			{
				if($h==null)$h=$x;
				HookElement($x);
			}
		}
	};
	$i=true;
	if(document.vkonload)
	{
		document.vkonload();
		document.onload=document.vkonload;
		document.vkonload=null;
	}
};

function getX(e)
{
	var X;
	if(ns4)return e.pageX;
	else
	{
		if($a)
		{
			x=0;
			while(e.offsetParent!=null)
			{
				x+=e.offsetLeft;
				e=e.offsetParent;
			};
			x+=e.offsetLeft;
			return x;
		}
		else
		{
			return parseInt(e.style.left);
		}
	}
};

function getY(e)
{
	var y;
	if(ns4)return e.pageY;
	else
	{
		y=0;
		while(e.offsetParent!=null)
		{
			y+=e.offsetTop;
			e=e.offsetParent;
		};
		y+=e.offsetTop;
		return y;
	}
};

function getLayer()
{
	var $A;
	if($b)
	{
		var n=getLayer.arguments[getLayer.arguments.length-1];
		$A=document.getElementById(n);
		if($A)return $A;
		$A=document.getElementsByName(n);
		if($A)return $A[0];
	}
	else if(ns4)
	{
		$A=document.layers[getLayer.arguments[0]];
		for(var i=1;i<getLayer.arguments.length&&$A;i++)
		{
			$A=$A.document.layers[getLayer.arguments[i]];
		};
		return $A;
	}
	else
	{
		$A=eval('document.all.'+getLayer.arguments[getLayer.arguments.length-1]);
		return $A;
	}
};

function kL(n)
{
	var s="kbd"+n+".html";
	var kw=getLayer('kwin','kshell','kframe');
	if(ns4&&!kw)kw=getLayer('kshell','kframe');
	if(bDialog)
	{
		if($a)
		{
			kw=vkWin.document.all.kframe;
		}
		else if(ns4)
		{
			kw=vkWin.document.layers.kshell.document.layers.kframe;
		}
		else
		{
			kw=vkWin.document.getElementById('kframe');
		}
	}
	else
	{
		s=$k+s;
	};
	if(!kw)return;
	if(ns4)
	{
		kw.load(s,300);
		hideKbdMenu();
		if($f)
		{
			document.location.reload();
		}
	}
	else
	{
		if(!$a)
		{
			var t=location.href.substring(0,location.href.lastIndexOf("//")+1);
			if(t=="http://")t=location.href.substring(0,location.href.lastIndexOf("/")+1);
			t=t+$k;
			t="";
			s=t+s;
		};
		kw.src=s;
	};
	if((s=="kbdbl.html")||(s=="kbdce.html")||(s=="kbdws.html"))
	{
		$g=true;
	}
	else
	{
		sLastKbd=n;
		$g=false;
	};
	if(!ns4)
	{	
		var el;
		if(bDialog)
		{
			el=document.getElementById('kllst');
			//el=vkWin.frames.kbdmenu.document.getElementById('kllst');
		}
		else
		{
			el=document.getElementById('kllst');
		};
		if(el)el.value=n;
	};
	if(bDialog)initFocus();
	regainFocus();
	saveSettings();
};

function onVirkKeyPress(s)
{
	var $B;
	if(!$i)
	{
		initFocus();
		return;
	};
	if(s=="k1")
	{
		setTimeout("kL('ru');",1000);
		return;
	}
	else if(s=="k2")
	{
		setTimeout("kL('us');",1000);
		return;
	}
	else if(s=="k3")
	{
		setTimeout("kL('rt');",1000);
		return;
	};
	if(top.curselection==null)return;
	if($a&&!$d)
	{
		if(top.curselection.parentElement().name>'')
		{
			if(s=="enter")
			{
				top.curselection.text='\15\12';
			}
			else if(s=="bs")
			{
				if(!top.curselection.text>'')top.curselection.moveStart('character',-1);
				if(top.curselection.htmlText.substring(0,1)=='<')return;
				top.curselection.text='';
				top.curselection.collapse(false);
				top.curselection.select();
			}
			else if(s=="del")
			{
				if(!top.curselection.text>'')top.curselection.moveEnd('character',1);
				top.curselection.text='';
				top.curselection.collapse(false);
				top.curselection.select();
			}
			else if((s!="-1")&&(s!="-2"))
			{
				top.curselection.text=s;
				top.curselection.collapse(false);
				top.curselection.select();
				if((s.charCodeAt(0)>0x580)&&(s.charCodeAt(0)<0x6ff))
				{
					top.curselection.parentElement().dir="rtl";
				};
				if($g)
				{
					kL(sLastKbd);
				}
			}
		}
	}
	else
	{
		if(s=="enter")
		{
			top.curselection.value=top.curselection.value+'\15\12';
		}
		else if(s=="bs")
		{
			top.curselection.value=top.curselection.value.slice(0,top.curselection.value.length-1);
		}
		else if(s=="del")
		{
			top.curselection.value=top.curselection.value.slice(0,top.curselection.value.length-1);
		}
		else if((s!="-1")&&(s!="-2"))
		{
			if(top.curseloffs)
			{
				var $C=top.curselection.value;
				top.curselection.value=$C+s;
			}
			else
			{
				top.curselection.value=top.curselection.value+s;
			};
			return false;
		}
		else
		{
			top.curselection.focus();
		}
	}
};

function updateCurSel($x,$D)
{
	if($a)
	{
		if($e)
		{
			var $E=$x.parentElement;
			if($E==null)$E=$x;
			while($E.parentElement!=null)$E=$E.parentElement;
			top.curdoc=$E.document;
		}
		else
		{
			top.curdoc=document;
		};
		if($d)
		{
			top.curselection=event.srcElement
		}
		else
		{
			top.curselection=top.curdoc.selection.createRange();
		}
	}
	else if($b)
	{
		top.curselection=$D.currentTarget;
	}
	else
	{
		top.curselection=$D.target;
	}
};

function onClick($D)
{
	updateCurSel(this,$D);
	if(this.oldonclick!=null)return this.oldonclick();
};

function onFocusChange($D)
{
	if($a)
	{
		if(!event)
		{
			if(this.oldonfocus!=null)return this.oldonfocus();
			else return;
		};
		if(event.type=="change")event.srcElement.focus();
	};
	updateCurSel(this,$D);
	if(this.oldonfocus!=null)return this.oldonfocus();
	return true;
};

function onBlur($D)
{
	if($a)
	{
		if(!event)
		{	
			if(this.oldonblur!=null)return this.oldonblur();
			else return;
		}
	}
	else
	{
		elLastFocus=$D.target;
	};
	if(this.oldonblur!=null)return this.oldonblur();
};

function findFramedEvent()
{
	var $y=window.parent.frames;
	for(k=0;k<$y.length;k++)
	{
		try
		{
			if($y(k).event!=null)return $y(k).event;
		}
		catch($z)
		{
			;
		}
	};
	return event;
};

function onKeyUp($D)
{
	if(!bShown||ns4||!top.curdoc||$d)return;
	if($a)
	{
		var ev;
		if($e)
		{
			ev=findFramedEvent();
		}
		else
		{
			ev=window.event;
		};
		kc=ev.keyCode;
		s=vkWin.frames.kframe.kbdOnKeyUp(kc);
		{
			ev.returnValue=true;
			if(this.oldonkeyup!=null)return this.oldonkeyup();
			else
			{
				return;
			}
		}
	}
};

function onKeyDown($D)
{
	if(ns4);
	if(!bShown||!top.curdoc||$d)return true;
	if($a)
	{
		var s;
		top.curselection=top.curdoc.selection.createRange();
		var ev;
		if($e)
		{
			ev=findFramedEvent();
		}
		else
		{
			ev=window.event;
		};
		kc=ev.keyCode;
		s=vkWin.frames.kframe.kbdOnKeyDown(kc);
		if((s.charCodeAt(0)>0x580)&&(s.charCodeAt(0)<0x6ff))
		{
			top.curselection.parentElement().dir="rtl";
		};
		if((s!="-1")&&(s!="")&&(s!="-2"))
		{
			top.curselection.text=s;
			top.curselection.collapse(false);
			top.curselection.select();
			ev.returnValue=false;
		};
		if(s=="-2")ev.returnValue=false;
	}
	else
	{
		$D.preventDefault();
		return false;
		s=vkWin.frames.kframe.kbdOnKeyDown($D.which);
		if(s=="enter")
		{
			top.curselection.value=top.curselection.value+'\15\12';
		}
		else if(s=="bs")
		{
			top.curselection.value=top.curselection.value.slice(0,top.curselection.value.length-1);
		}
		else if(s=="del")
		{
			top.curselection.value=top.curselection.value.slice(0,top.curselection.value.length-1);
		}
		else if((s!="-1")&&(s!="-2"))
		{
			if(top.curselection!=null)top.curselection.value=top.curselection.value+s;
			$D.cancelBubble=true;
			$D.stopPropagation();
			$D.preventDefault();
			return false;
		}
		else
		{
			top.curselection.focus();
		}
	};
	if(this.oldonkeydown!=null)this.oldonkeydown();
};

function onCopyText(s)
{
	if(top.curselection)top.curselection.value=s;
};

function onVKClose()
{
	bShown=false;
	vkWin=null;
	if(!ns4)
	{
		setTimeout("el=document.getElementById('vkbtn');if(el)el.checked=bShown;",100);
	}
	else
	{
		el=document.forms['vkfrm'];
		if(el)el=el.elements['vkbtn'];
		if(el)el.checked=bShown;
	};
	saveSettings();
	Show_vkb=false;
	window.parent.document.vkb_bt.src = "vbk/vkb_off.gif";
};

function getElHeight(e)
{
	if(ns4)return e.document.height;
	else return e.offsetHeight;
};

function moveTo(e,x,y)
{
	if(ns4)e.moveTo(x,y);
	else if($a)
	{
		e.style.pixelLeft=x;
		e.style.pixelTop=y;
	}
	else
	{
		e.style.left=x;
		e.style.top=y;
	}
};

function setVisible(el,st)
{
	if(ns4)el.visibility=st;
	else
	{
		el.style.visibility=st;
	}
};



function showKbdMenu($G)
{
	if(bDialog)
	{
		if($a)
		{
			$j=vkWin.document.all.kbdmenu;
		}
		else if(ns4)
		{
			$j=vkWin.document.layers.kbdmenu;
		}
		else
		{
			$j=vkWin.document.getElementById('kbdmenu');
		}
	}
	else
	{
	$j=getLayer("kbdmenu");
	};
	if(!$j)return;
	if($j.bMenuShown)
	{
		hideKbdMenu();
	}
	else
	{
		if(!bDialog)
		{
			moveTo($j,20+getX($F),getY($F)+64);
		};
		setVisible($j,"visible");
		$j.bMenuShown=true;
	}
};

function hideKbdMenu(e)
{
	if(bDialog)
	{
		if(!vkWin)return;
		$j=vkWin.document.getElementById('kbdmenu');
	};
	if($j)
	{
		if($a)
		{
			if(window.event&&$j.bMenuShown)if(window.event.srcElement.id=="kllst")return true;
		}
		else
		{
			if(e&&$j.bMenuShown)if(e.target.id=="kllst")return true;
		};
		setVisible($j,"hidden");
		$j.bMenuShown=false;
	};
	if(window.event!=null)
	{
		if(document.olonmousedown!=null)
		{
			return document.olonmousedown(e);
		};
		return true;
	}
};

function kbdShowHide($G,$H)
{
	if($H==null)$H=sLastKbd;
	if($G)
	{
		if(bDialog)
		{
			if(bShown)
			{
				if($H!=null)
				{
					kL($H);
				};
				return;
			};
			if($a&&!$d)
			{
			    var t=location.href.substring(0,location.href.lastIndexOf("//")+1);
				if(t=="http://")t=location.href.substring(0,location.href.lastIndexOf("/")+1);
				t="";
				var $I=t+$k+"vkdlg.html";

				vkWin=parent.showModelessDialog($k+"vkdlg.html",null,"dialogWidth:304px;dialogHeight:128px;scroll:no;status:no;center:yes");
			}
			else
			{
				var t=location.href.substring(0,location.href.lastIndexOf("//")+1);
				if(t=="http://")t=location.href.substring(0,location.href.lastIndexOf("/")+1);
				t="";
				var $I=t+$k+"vkdlg.html";
				vkWin=window.open($I,"VirtualKeyboard","width=300,height=100,scroll=no,help=no,status=no,unadorned=yes,directories=no,menubar=no,resizable=no");
			};
			if($d)
			{
				vkWin.opener=window;
				kL($H);
			}
			else
			{
				setTimeout("vkWin.opener=window;kL ('"+$H+"');",500);
			}
		}
		else
		{
			setVisible($F,"visible");
			if(!ns4)
			{
				$F.style.display="inline";
			};
			if(!$f)
			{
				moveTo($F,sX,sY);
				setTimeout("kL ('"+$H+"');",500);
			}
		}
	}
	else
	{
		if(bDialog)
		{
			if(vkWin)vkWin.close();
			$f=false;
			vkWin=null;
		}
		else
		{
			setVisible($F,"hidden");
			if(!ns4)$F.style.display="none";
		}
	};
	bShown=$G;
	if(!ns4)
	{
		setTimeout("el=document.getElementById('vkbtn');if(el)el.checked=bShown;",100);
	}
	else
	{
		el=document.forms['vkfrm'];
		if(el)el=el.elements['vkbtn'];
		if(el)el.checked=bShown;
	};
	saveSettings();
};

function regainFocus()
{
	if(!$a)return;
	if(elLastFocus)setTimeout("elLastFocus.blur();elLastFocus.focus();",100);
	if(top.curselection)
	{
		if($d)
		{
			setTimeout("top.curselection.focus();",100);
			return;
		};
		setTimeout("top.curselection.collapse(false);top.curselection.select();top.curselection.parentElement().focus();",100);
	}
};

function getSettings()
{
	return;
	var $J=document.cookie;
	if($J=="")return false;
	var $K=$J.indexOf('vkbd=');
	if($K==-1)return false;
	$K+='vkbd'.length+1;
	var end=$J.indexOf(';vkend',$K);
	if(end==-1)end=$J.length;
	var $L=$J.substring($K,end);
	end=$L.indexOf('!',1);
	if(end==-1)end=$L.length;
	bShown=eval($L.substring(0,end));
	sLastKbd=$L.substring(end+1,$L.length);
	if(sLastKbd.length<2)sLastKbd=sKbd;
};

function saveSettings()
{
	return;
	var $J=document.cookie;
	if($J=="")return false;
	var $K=$J.indexOf('vkbd=');
	if($K==-1)return false;
	$K+='vkbd'.length+1;
	var end=$J.indexOf(';vkend',$K);
	if(end==-1)end=$J.length;
	var $L=$J.substring($K,end);
	var dt=new Date((new Date()).getTime()+24000*3600000);
	var $M="vkbd="+bShown+"!"+sLastKbd+";vkend;expires="+dt.toGMTString()+";path=/;";
};

function initVirk()
{
	if(!window.sKbd)sKbd='ru';
	sLastKbd=sKbd;
	if(bDialog)
	{
		vkWin=null;
		$j=null;
	}
	else
	{
		vkWin=window;
		if($j)$j.bMenuShown=false;
	};
	document.olonmousedown=document.onmousedown;
	document.onmousedown=hideKbdMenu;
	getSettings();
	if(bShown)
	{
		bShown=false;
		kbdShowHide(true);
	};
	initFocus();
};

function adjustIFrameSize($N)
{
	if(ns4)return;
	if($N.document.height)
	{
		var w=$N.document.getElementById('kllst');
		if(w)w=w.offsetWidth;
		var $O=parent.document.getElementById($N.name);
		$O.style.height=$N.document.height+'px';
		$O.style.width=w+'px';
	}
	else if(document.all)
	{
		var $O=parent.document.all[$N.name];
		if($N.document.compatMode&&$N.document.compatMode!='BackCompat')
		{
			$O.style.height=$N.document.documentElement.scrollHeight+'px';$O.style.width=$N.document.documentElement.scrollWidth+'px';
		}
		else
		{
			var w=$N.document.getElementById('kllst');
			if(w)w=w.offsetWidth;
			$O.style.height=$N.document.body.scrollHeight+'px';
			$O.style.width=w+'px';
		}
	}
};


if(window.bDialog==null)bDialog=true;
if(window.sVkDir==null)$k="vbk/";

var dragswitch=0;
var $l;
var $m;
var $n;

$p=false;

var $F=$F=getLayer('kwin');

;

if(ns4&&!$F)
{
	$F=getLayer('kshell');
};

initVirk();



