/*
This code is written by Baconbutty in response to my inquiry at the newsgroup com.lang.javascript.

onkeypress=KeyPress(); onkeydown=KeyDown(); 

*/

function KeyDown() 
{ 
    var key=window.event.keyCode; 


     if (key==8) 
     { 
        window.event.returnValue=false; 


        var r=document.selection.createRange(); 
        r.collapse(false); 
        r.move("character",-1); 
        r.collapse(false); 


        var p=r.parentElement(); 


        if (p.nodeName.toLowerCase()=="sup"){ 
            p.outerHTML=""; 
            return; 
        } 
        window.event.returnValue=true; 
     } 
} 


function KeyPress() 
{ 
    var key=window.event.keyCode; 
    window.event.returnValue=false; 

    if (key>=49 && key<=52) 
    { 
        var r=document.selection.createRange(); 
        r.collapse(false); 


        var d=r.duplicate(); 
        d.moveStart("character",-1); 
        var c=d.text; 


        var p=d.parentElement(); 


        var b=/[A-Za-z]/.test(c); 
        //b=b || p.nodeName.toLowerCase()=="sup"; 


        if (b) 
        { 
           d.collapse(false); 


           var s=String.fromCharCode(key); 
           s=(p.nodeName.toLowerCase()=="sup")? s:"<SUP>"+s+"</SUP>"; 


           d.pasteHTML(s); 
           d.collapse(false); 
           d.select(); 
           return; 
        } 
     } 


     if (!(key in {37:1,38:1,39:1,40:1})) 
     { 
        var r=document.selection.createRange(); 
        r.collapse(false); 
        p=r.parentElement(); 


        if (p.nodeName.toLowerCase()=="sup") 
        { 
           var s=p.outerHTML; 
           p.removeNode(true); 
           r.pasteHTML(s); 
           r.collapse(false); 
           r.select(); 
        } 
     } 
     window.event.returnValue=true; 
}

//Below for status line

myMsg = "  Happy Chinese typing !"
var i = 0;
	
function scrollMsg() 
{

window.status = myMsg.substring(i,myMsg.length) + myMsg.substring(0,i);
if (i < myMsg.length) 
{
i++
}
else 
{
i = 0
}
setTimeout("scrollMsg()",120)
}

function stoptimer()
{
clearTimeout(timer);
}

//Following function is for the appreciation window

function newWindow() {
 
catWindow = window.open('appr2.html', 'catwin','width=395,height=235, scrollbars=yes');
catWindow.focus();
}

//Typewriting code

message='Nin<sup>2</sup> hao<sup>3</sup>. Edit in this box.';
pos=0;
maxlength=message.length+1;

function writemsg()
{
if (pos<maxlength)
	{
	txt=message.substring(pos,0);
	document.getElementById('edit').innerHTML=txt;
	pos++;
	timer=setTimeout('writemsg()', 20);
	}
}
function stoptimer()
{
clearTimeout(timer);
}

//Following function clears the writing box when left-clicking the mouse.


/*
function keyClick() {

document.getElementById('edit').innerHTML='';
}

*/
