var ie4=(document.all)?1:0;
var elFirstEdit=null;
var bInited=false;


var MainApplet;


function showVirk()
{
if (document.Virk)
{
initFocus();
  if (document.all)
    {
     if (document.Virk.readyState==4)
       document.Virk.showKeyboard();
    }
  else
    document.Virk.showKeyboard();
}
else
{
  alert ("Virtual Keyboard is not loaded."); 
}
//if (document.all) {
//    document.body.style.cursor = 'auto';
//    document.all("imgvk").style.cursor = 'auto';
//}
}
var bTrial=true;

function initVirk()
{
if (ie4)
{
  sBrver="ie4";
}
else
{
  if (document.getElementById!=null)
    sBrver="ns6";
  else
    sBrver="ns4";
}
if (sMode=="ns")
  {
  sBrver="ns4";
  }
if (sBrver =="ie4")
  {
  sJar="vk";
  }                                                 
else
  {
  sJar="vkns";
  }                                                 
sJar=sJar+sSkin+sReg+"t.jar";
if (!navigator.javaEnabled())
  {
  alert ("Java-support is disabled in your browser. Virtual Keyboard cannot be loaded without Java."); 
  return;
  }
window.oldonload=window.onload;
//window.onload=initFocus;
document.writeln ("<APPLET MAYSCRIPT CODE=\"Virk.class\" ARCHIVE = \"",sJar,"\"  WIDTH=\"2\" HEIGHT=\"1\" NAME=\"Virk\" ID=\"Virk\">");
document.writeln ("  <PARAM NAME=\"MAYSCRIPT\" VALUE=\"true\"></PARAM>");
document.writeln ("  <PARAM NAME=\"onkeytyped\" VALUE=\"onVirkKeyPress\"></PARAM>");
document.writeln ("  <PARAM NAME=\"onapply\" VALUE=\"onCopyText\"></PARAM>");
document.writeln ("  <PARAM NAME=\"showlayouts\" VALUE=\""+sLayouts+"\"></PARAM>");
document.writeln ("  <PARAM NAME=\"brver\" VALUE=\""+sBrver+"\"></PARAM>");
document.writeln ("  <PARAM NAME=\"x\" VALUE=\""+sX+"\"></PARAM>");
document.writeln ("  <PARAM NAME=\"y\" VALUE=\""+sY+"\"></PARAM>");
document.writeln ("</APPLET>");
}

function isEditBox (ell)
{
if (ie4 && (ell.isContentEditable != null))
  return ell.isContentEditable;
else
  {
  if ((ell.type=="text") || (ell.type=="textarea"))
    return true;
  else
    return false;
  }
}

function HookElement (ell)
{
if (ell.oldonfocus == null)
  {
	ell.oldonfocus= ell.onfocus;
	ell.oldonchange= ell.onchange;
	ell.oldonkeydown=ell.onkeydown; 
	ell.oldonkeyup=ell.onkeyup;
	ell.oldonclick=ell.onclick;
  }
	
	ell.onfocus= onFocusChange;
	ell.onchange=onFocusChange ;
	ell.onkeydown=onKeyDown; 
	ell.onkeyup=onKeyUp;
	ell.onclick=onClick;
}


function UnHookElement (ell)
{
	ell.onfocus= ell.oldonfocus;
	ell.onchange= ell.oldonchange;
	ell.onkeydown=ell.oldonkeydown; 
	ell.onkeyup=ell.oldonkeyup;
	ell.onclick=ell.oldonclick;
}


function initFocus()
{
if (bInited==true)
  {
  return;
  }
	var ell;
	var i,j,k;
	var frm = window.parent.frames;

	if (self != top)
	{
	// FRAMES
		for (k=0; k<frm.length ; k++) 
		{
		    for (i=0; i<frm[k].document.forms.length ;i++)
			{
				for (j=0; j<frm[k].document.forms[i].elements.length; j++)
				{
					ell = frm[k].document.forms[i].elements[j];
					if (isEditBox (ell))
					{
						if (elFirstEdit == null)
						                  elFirstEdit = ell;
						HookElement (ell);
					}
				}
			}
		}
	}
	else
	{
	// NO FRAMES
	    for (i=0; i<document.forms.length; i++)
		{
			for (j=0; j<document.forms[i].elements.length; j++)
			{
				ell = document.forms[i].elements[j];
				if (isEditBox (ell))
				{

					if (elFirstEdit == null)
				                elFirstEdit = ell;
				    HookElement (ell);
				}
			}
		}
	}
	if (elFirstEdit)
		elFirstEdit.focus();
//alert ("inited");
bInited = true;
if (window.oldonload != null)
  return window.oldonload();
}

// ******************************************************
// ONVIRKKEYPRESS
// 

function onVirkKeyPress(s)
{
var doc;
if (!bInited)
  {
  initFocus();
  return ;
  }

if (top.curselection==null)
  return;
if (ie4)
{
if (self != top)
{
  doc = top.curdoc;
//  alert (doc);
}
else
  doc = document;
  
if (top.curselection != null)
{
	if (top.curselection.parentElement().name>'')
	{
		if (s == "enter") // ENTER
		{
			top.curselection = doc.selection.createRange();
			top.curselection.text = '\15\12';
		} 
		else if(s == "bs") // BACKSPACE
		{
			top.curselection = doc.selection.createRange();
			if (doc.activeElement.isTextEdit)
			{
				if (!(top.curselection.text>''))
				{
					top.curselection.moveStart('character',-1);
				}
				top.curselection.text='';
			} 
		}
		else if(s == "del") // DEL
		{
		var selend;
			top.curselection = doc.selection.createRange();
					
			if (doc.activeElement.isTextEdit)
				{
					if (!(top.curselection.text>'')
					&& top.curselection.parentElement().innerText>'')
					{
						selend = top.curselection.duplicate();
							top.curselection.moveEnd('character',1);
							if (!(top.curselection.text>''))
							  top.curselection=selend;
					}
					if (top.curselection.text>'')
					  top.curselection.text='';
				} 
		}
		else  if ((s != "-1") && (s != "-2")) // CHARACTER
		{
			//top.curselection = doc.selection.createRange();
if (!bTrial || (top.curselection.parentElement().value.length <128))
  {
			top.curselection.text = s;
			top.curselection.collapse(false);
			top.curselection.select();
  }
		}
	}
    if (top.curselection != null)
	{
        top.curselection.parentElement().focus();
		top.curselection.select();
	}
					
	}
}
else
	{
	if (s == "enter") // ENTER
	{
		top.curselection.value = top.curselection.value+'\15\12';
	} 
	else if(s == "bs") // BACKSPACE
	{
		top.curselection.value=top.curselection.value.slice (0,top.curselection.value.length-1);
	}
	else if(s == "del") // DEL
	{
		top.curselection.value=top.curselection.value.slice (0,top.curselection.value.length-1);
	}
	else  if ((s != "-1") && (s != "-2")) // CHARACTER
	{
          if (top.curselection!=null)
if (!bTrial || (top.curselection.parentElement().value.length <128))
  {
            top.curselection.value=top.curselection.value+s;
	          return false;
  }
	}
    }
}


// ******************************************************
// updateCurSel
// 

function updateCurSel (ell, nsEv)
{
if (ie4)
{
  var frm = window.parent.frames;
  if (self != top)
  {
	var par = ell.parentElement;
	if (par==null)
		par=ell;
	while (par.parentElement != null)
	  par = par.parentElement;

	top.curdoc=par.document;
  }
  else
    top.curdoc=document;
  if (sBrver=="ns4") 
    {
//Netscape style virk on IE
    top.curselection = window.event.srcElement;
    }
  else
    {
    top.curselection = top.curdoc.selection.createRange();
    }
}
else
{
  top.curselection= nsEv.target;
}
}


// ******************************************************
// ONCLICK
// 
function onClick(nsEv) 
{
updateCurSel(this, nsEv);
if (this.oldonclick != null)
  return this.oldonclick();
}


// ******************************************************
// ONFOCUSCHANGE
// 
function onFocusChange(nsEv) 
{
updateCurSel(this, nsEv);
if (this.oldonfocus != null)
  return this.oldonfocus();
}


// ******************************************************
// FINDFRAMEDEVENT
// 
function findFramedEvent() 
{
  var frm = window.parent.frames;
  for (k=0; k < frm.length; k++) 
    {
    if (frm(k).event != null)
      return frm(k).event;
    }
  return null;
}

// ******************************************************
// ONKEYUP
// 

function onKeyUp(nsEv)
{
if (sBrver=="ns4") // forced Netscape behaviour
  return;

if (!MainApplet)
	MainApplet=document.Virk;

if (ie4)
  {
  var ev;
  if (self != top)
    {
    ev = findFramedEvent();
    }
  else
	ev = window.event;
  kc=ev.keyCode;
  if (MainApplet)
	MainApplet.keyJSReleased(kc);
  else
  	{
  	ev.returnValue=true;
        if (this.oldonkeyup!=null)
    	  return this.oldonkeyup()
	else
	  return;
  	}
  }
}

// ******************************************************
// ONKEYDOWN
// 

function onKeyDown(nsEv)
{
if (sBrver=="ns4") // forced Netscape behaviour
  return;
if (!MainApplet)
	MainApplet=document.Virk;
if (ie4 && MainApplet)
  {
  var s;
  top.curselection = top.curdoc.selection.createRange();
  var ev;
  if (self != top)
    {
    ev = findFramedEvent();
    }
  else
    ev = window.event;
  kc=ev.keyCode;
  s = MainApplet.translateKeyStroke(kc);
  if ((s != "-1") && (s != "") && (s != "-2")) // not a dead key
	{
if (!bTrial || (top.curselection.parentElement().value.length <128))
  {
	top.curselection.text = s ;
	top.curselection.collapse(false);
	top.curselection.select();
}
	ev.returnValue = false;
	}
  else if (s == "-2") // dead key or no translate.
    {
//      alert(s);
	ev.returnValue = false;
    }
  }
if (this.oldonkeydown!=null)
	this.oldonkeydown();
}


// ******************************************************
// ONCOPYTEXT
// 
function onCopyText (s)
{
if (top.curselection)
	top.curselection.value=s;
}

