// JavaScript Document
var coreVersion="4";
var buff = new Array();
var lodedJavaScript=null;
function loadFile(filename, filetype){
	 if (filetype=="js"){ //if filename is a external JavaScript file
		 lodedJavaScript=filename;
	  var fileref=document.createElement('script')
	  fileref.setAttribute("type","text/javascript")
	  fileref.setAttribute("src", filename);
	  fileref.setAttribute("id", filename);
	 }
	 else if (filetype=="css"){ //if filename is an external CSS file
	  var fileref=document.createElement("link")
	  fileref.setAttribute("rel", "stylesheet")
	  fileref.setAttribute("type", "text/css")
	  fileref.setAttribute("href", filename)
	 }
	 if (typeof fileref!="undefined")
	  document.getElementsByTagName("head")[0].appendChild(fileref)
	}

function removeJavascript()
{
	if(lodedJavaScript!=null)
	{
		var obj=document.getElementsByTagName('script');
		for(var i=0;i<obj.length;i++)
		{
			var o=obj[i];
			if(o.id==lodedJavaScript)
			{
				document.getElementsByTagName("head")[0].removeChild(o);
			}
		}
	}
}
function initFonScript()
{
	//removeJavascript();
	var type=document.getElementById("fontType").value;
	var keyboard=document.getElementById("keyboard").value;
	var font=document.getElementById("fontList").value;
	window.location='?t='+type+'&k='+keyboard+'&f='+font;
	
}
function initBuff()
{
	for(var i=0;i<5;i++)
	{
		buff[i]="";
	}
}
function eraseChar(obj,nChar)
{
	var val=obj.value;
	val = val.substr(0,val.length-nChar);
	obj.value=val;
	eraseBuff(nChar);
}
function eraseBuff(char)
{
	for(var i=char;i<5;i++)
	{
		buff[i-char]=buff[i];
		buff[i]="";
	}
}
function addBuff(code)
{
	for(var i=4;i>=0;i--)
	{
		buff[i+1]=buff[i];
	}
	buff[0]=code;
}
function setVersion()
{
	if(k!='u')
	{
		document.getElementById('version').innerHTML=coreVersion+"."+libVersion;	
	}
	else
	{
		document.getElementById('version').innerHTML=coreVersion+".01.U";	
	}
}
function loadLibrary()
{
	initLibrary();
	setVersion();
}
var k,t,f;
function initApp()
{
	initBuff();
	//initFonScript();
	t=gup("t");
	k=gup("k");
	f=gup("f");
	if(t==""){t=document.getElementById('fontType').value;}
	if(k==""){k=document.getElementById('keyboard').value;}
	if(f==""){f=document.getElementById('fontList').value;}
	document.getElementById('fontType').value=t;
	document.getElementById('keyboard').value=k;
	document.getElementById('fontList').value=f;
	var filename=k+"_"+f+"_"+t+".js";
	loadFile("fontScript/"+filename,"js");
	if(k!="u")
	{
		
		document.onkeypress=checkKeycode
		document.onkeyup = checkKeyUp;
		document.onkeydown = checkKeyDown;		
		setTimeout("loadLibrary()",1000);
	}
	else
	{
		setTimeout("initUnicodeTyping()",1000);

	}
	
	if(downloadFilename!='')
	{
		document.getElementById('downloadLink').href="download/"+downloadFilename;
		document.getElementById('downloadLink').innerHTML="Download "+downloadFilename;
	}
	else
	{
		document.getElementById('downloadLinkTD').removeChild(document.getElementById('downloadLink'));
	}
	var obj = document.getElementById("data");
	obj.style.width=document.body.clientWidth+"px";
	obj.style.height=(document.body.clientHeight-220)+"px";
	obj.className=f;
	obj.focus();
}
function initLib()
{
	for(var i=0;i<255;i++)
	{	
		charLib[i]="";
	}
}
function makeCombination()
{
	var str1=combination[buff[3]+"."+buff[2]+"."+buff[1]+"."+buff[0]];
	var str2=combination[buff[2]+"."+buff[1]+"."+buff[0]];
	var str3=combination[buff[1]+"."+buff[0]];
	if(typeof(str1)=="object")
	{
		return str1;		
	}
	else if(typeof(str2)=="object")
	{
		return str2;
	}
	else if(typeof(str3)=="object")
	{
		return str3;
	}	
	else
	{
		return null;
	}
}
function changeFonrSize()
{
	var obj = document.getElementById("data");
	obj.style.fontSize=document.getElementById("fontsize").value;
}
function changeFontName()
{
	var obj = document.getElementById("data");
	obj.className=document.getElementById("fontList").value;
}








function writeChar(code) 
{
	var ch=String.fromCharCode(code);
	var obj=document.getElementById("data");

	obj.value += (ch);
}
function writeNum(code)
{
	var obj=document.getElementById("data");

	obj.value += (code);
	obj.value += "\n";
}

function writeCharFromLib(code)
{
	var obj=document.getElementById("data");
	//obj.focus();
	var char=charLib[code];

	if(typeof(char)!="object")
	{
		addBuff(code);
		obj.value += String.fromCharCode(code);		
		return;
	}
	if(char[0]>0)
	{
		eraseChar(obj,char[0]);
	}

	for(var i=1;i<char.length;i++)
	{
		addBuff(charLib[code][i]);
		obj.value += String.fromCharCode(charLib[code][i]);
	}	
	var cmb=makeCombination();
	if(cmb!=null)
	{
		eraseChar(obj,cmb[0]);
		for(var j=1;j<cmb.length;j++)
		{
			char = cmb[j];
			addBuff(char);
			obj.value += String.fromCharCode(char);
		}
	}
	validateConditions();
	if(obj.clientHeight<obj.scrollHeight)
	{
		obj.style.height = obj.scrollHeight+"px"; 
	}
}
var altKeyVal=0;
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
function checkKeycode(e) 
{
	var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var keycode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var key=String.fromCharCode(keycode);
	if(evtobj.ctrlKey==1)
	{
		if(keycode=="32")
		{
			if(document.getElementById('scriptStatus').checked==true)
			{
				document.getElementById('scriptStatus').checked=false;
			}
			else
			{
				document.getElementById('scriptStatus').checked=true;
			}
		}
		return;
	}	
	if(document.getElementById('scriptStatus').checked!=true)
	{
		return;
	}
	if(keycode==8)
	{
		eraseBuff(1);
	}

	if(gup("mode")=="debug")
	{
		alert(keycode);
	}
	if(keycode==8 
	|| keycode==18 
	|| keycode == 17 
	|| keycode == 16 
	|| keycode == 20 
	|| keycode == 148 	
	|| keycode == 9)
	{
		
	}
	else
	{

		if(evtobj.shiftKey==0 && keycode >= 65 && keycode <=90)
		{
			keycode=keycode+32;
		}

		
		if (evtobj.preventDefault) 
		{
	        evtobj.preventDefault();
	    } 
		else 
		{
	        evtobj.returnValue = false;
	    }
   		writeCharFromLib(keycode);
    }
	document.body.scrollTop = document.body.scrollHeight; 
}
function altRelease()
{

}
var altStatus=0;
function checkKeyUp(e)
{
	var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var keycode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var key=String.fromCharCode(keycode)
	if(evtobj.altKey==0 && altStatus==1)
	{
		altStatus=0;
		altRelease();
	}
}
function checkKeyDown(e)
{
	var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var keycode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var key=String.fromCharCode(keycode)
	if(evtobj.altKey==1)
	{
		altStatus=1;
	}	
}
function transliterate(id) {
	var len = arguments.length;
	for(var i=0;i<len; i++)
	{
		var element = document.getElementById(arguments[i]);
		if(element)
		{
			trasliteration_fields[arguments[i]] = true;
			previous_sequence[arguments[i]] = '';
			//element.onkeypress = tiKeyPressed;
			if (element.addEventListener){
				element.addEventListener('keypress', tiKeyPressed, false);
			} else if (element.attachEvent){  
				element.attachEvent("onkeypress", tiKeyPressed);  
			}  
		}
	}
}
function initUnicodeTyping()
{
	transliterate('searchInput', 'data', 'wpSummary', 'wpFilterDescription', 'wpFilterTags', 'wpFilterRules', 'wpFilterNotes', 'wikieditor-toolbar-reference-text', 'mw-upload-form', 'wpDestFile', 'wpUploadDescription', 'emailuser', 'wpSubject', 'searchText', 'powerSearchText', 'wpNewTitle', 'wpReason', 'nsfrom', 'username', 'mwProtect-reason', 'nsto','wpText');
	setVersion();
	 
}
function openCredit()
{
	window.open("thanks.html","Credits","width=400, height=300");
}
