
document.write( "<style>.keyButton{height:18px;font-family:tahoma;font-size:11px;text-align:center;border-top:solid 1px white;border-left:solid 1px white;border-bottom:solid 1px gray;border-right:solid 1px gray;width:20px;cursor:hand;}</style>" );
document.write( "<style>.keyPressedButton{height:18px;font-family:tahoma;font-size:11px;text-align:center;border-top:solid 1px gray;border-left:solid 1px gray;border-bottom:solid 1px white;border-right:solid 1px white;width:20px;cursor:hand;}</style>" );

var gKeyboardBag = new ActiveXObject("Scripting.Dictionary");


var arabic_keys = new Array (
	new Array("Ð","1","2","3","4","5","6","7","8","9","0","-","="),
	new Array("Ö","Õ","Ë","Þ","Ý","Û","Ú","å","Î","Í","Ì","Ï" ),
	new Array("Ô","Ó","í","È","á","Ç","Ê","ä","ã","ß","Ø"),
	new Array("Æ","Á","Ä","Ñ","áÇ","ì","É","æ","Ò","Ù"),
	
	new Array("ø","!","@","#","$","%","^","&","*","(",")","_","+"),
	new Array("ó","ð","õ","ñ","áÅ","Å","‘","÷","×","º","<",">"),
	new Array("ö","ò","[","]","áÃ","Ã","Ü","¡","/",":","\""),
	new Array("~","ú","{","}","áÂ","Â","’",",",".","¿")
);

var english_keys = new Array (
	"`1234567890-=", "qwertyuiop[]", "asdfghjkl;'", "zxcvbnm,./", 
	"~!@#$%^&*()_+", "QWERTYUIOP{}", "ASDFGHJKL:\"", "ZXCVBNM<>?"
);

//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function createKeys( arKeys, sKeyboardID )
{
	var sHTML = "";
	var sHooverEffect= " onmouseover=\"this.style.background='#FFFFAA'\" onmouseout=\"this.style.background=''\" ";
	var shiftButtonClass = "keyButton";

	if (gKeyboardBag(sKeyboardID).shift)
	{
		shiftButtonClass = "keyPressedButton";
	}

	for (var row= 0; row<4; row++)
	{
		var rowIndex = row;
		
		if (gKeyboardBag(sKeyboardID).shift)
		{
			 rowIndex += 4;
		}

		switch(row)
		{
		case 0:
			break;
		case 1:
			sHTML += "<span class=keyButton style='width:30px'" +  sHooverEffect +
					 " onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar(' ')\">Tab</span>";
			break;
		case 2:
			sHTML += "<span class=keyButton style='width:35px'>&nbsp;</span>";
			break;
		case 3:
			sHTML += "<span class=" + shiftButtonClass + " style='width:47px'" +  sHooverEffect +
					 " onclick=\"javascript:gKeyboardBag('" + sKeyboardID + "').toggleShift()\">Shift</span>";
			
			break;
		}


		for( var keyIndex=0; keyIndex < arKeys[rowIndex].length; keyIndex ++ )
		{
			var sChar, sDispChar;

			sChar = arKeys[rowIndex][ keyIndex ];
			sDispChar = sChar;

			if (sChar == "\"")
			{
				sChar = "\\x22";
			}

			sHTML += "<span class=keyButton " + sHooverEffect +
					 " ondblclick=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('" + sChar + "')\" onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('" + sChar + "')\">" + sDispChar + "</span>";
		}
		
		switch(row)
		{
		case 0:
			sHTML += "<span class=keyButton style='width:55px'" +  sHooverEffect +
					 " onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('BS')\" ondblclick=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('BS')\">BkSpace</span>";
			break;
		case 1:
			if( gKeyboardBag(sKeyboardID).shift ) {
				sHTML += "<span class=keyButton style='width:45px'" +  sHooverEffect +
						 " onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('|')\">|</span>";
			}
			else{
				sHTML += "<span class=keyButton style='width:45px'" +  sHooverEffect +
						 " onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('\\\\')\">\\</span>";
			}
			break;
		case 2:
			if (gKeyboardBag(sKeyboardID).sOnEnter.length)
			{
				sHTML += "<span class=keyButton style='width:60px'" + 
					 " =\"javascript:" + gKeyboardBag(sKeyboardID).sOnEnter + ";HideElement('" + sKeyboardID + "_keyboard',0);\"></span>";
			}
			else
			{
				sHTML += "<span class=keyButton style='width:60px'" + 
					 " =\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar(' ')\">&nbsp;</span>";
			}
			break;
		case 3:
			sHTML += "<span class=" + shiftButtonClass + " style='width:68px'" +  sHooverEffect +
					 " onclick=\"javascript:gKeyboardBag('" + sKeyboardID + "').toggleShift()\">Shift</span>";
			break;
		}


		sHTML +=  "<br>";
	}


	for(i=0;i<3;i++)
	{
		sHTML += "<span class=keyButton style='width:25px'>&nbsp</span>";
	}

	sHTML += "<span class=keyButton style='width:140px' " +  sHooverEffect + " onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar(' ')\">Space</span>";
	
	for(i=0;i<4;i++)
	{
		sHTML += "<span class=keyButton style='width:25px'>&nbsp</span>";
	}
	
	return sHTML;
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function xcreateKeys( arKeys, sKeyboardID )
{
	var sHTML = "";
	
	for (var i=0; i<arKeys.length; i++)
	{
		var sChar;

		if(i%5 == 0 && i>0)
			sHTML +=  "<br>";

		sChar = arKeys[i];

		if (sChar == " ")
		{
			sChar = "&nbsp;";
		}

		sHTML += "<span style='font-family:tahoma;font-size:11px;text-align:center;border-top:solid 1px white;border-left:solid 1px white;border-bottom:solid 1px gray;border-right:solid 1px gray;width:20px;cursor:hand;background:#efefde;' " + 
				 " onmousedown=\"javascript:gKeyboardBag('" + sKeyboardID + "').addChar('" + arKeys[i] + "')\">" + sChar + "</span>";
	}
	
	return sHTML;
} 
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function CKeyboard(sTextBoxID, arKeys, sOnEnter)
{
	var oTextBox = document.getElementById( sTextBoxID );
	
	if( null == oTextBox )
	{
		alert("not found");
		return null;
	}

	this.text_box = oTextBox;
	this.shift = false;
	this.addChar   = CKeyboard_addChar;
	this.clearChar = CKeyboard_clearChar;
	this.toggleShift = CKeyboard_toggleShift;
	this.keys = arKeys;
	this.sOnEnter = "";

	if (sOnEnter)
	{
		this.sOnEnter = sOnEnter;
	}
	
	gKeyboardBag(sTextBoxID) = this; // create a reference to the object

	var mySpan = document.createElement("div");

	mySpan.sTextBoxID  = sTextBoxID;
	mySpan.onclick = showKeyboard;
	//mySpan.onmousemove = showKeyboard;
	mySpan.onmouseout  = hideKeyboard;
	//mySpan.style.border = "solid 0px yellow";
	mySpan.style.position = "absolute";
	
	//apply the frame on the target element
	oTextBox.applyElement(mySpan) ;
	
	mySpan.insertAdjacentHTML(
					"beforeEnd",
					"<img src=images/keyboard.gif align='left' style='border:1px  padding:0; position:relative; top:-32px; left:-52px; cursor:hand;' alt='&#1604;&#1608;&#1581;&#1577; &#1575;&#1604;&#1605;&#1601;&#1575;&#1578;&#1610;&#1581; &#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;'>" +
				      "<br>" +
				    "<div dir=LTR onselectstart=\"javascript:window.event.returnValue=false\" NOWRAP " + 
					" id=" + sTextBoxID + "_keyboard " +
					" style='filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=85);top:12px;left:-189px;padding:0px;z-index:0;position:absolute;display:none;border:1px solid maroon;background:#faf6db;color:maroon;font-size:10px;' >" + 
						createKeys( arKeys, sTextBoxID ) + 
					"</div>" 
					);
	
	return this;
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function showKeyboard()
{
	var sTextBoxID;
	var oElement = event.srcElement ;
	
	while( oElement && !sTextBoxID )
	{ 
		sTextBoxID = oElement.sTextBoxID;
		oElement = oElement.parentElement;
	}
	if( sTextBoxID )
		ShowElement(sTextBoxID + "_keyboard", 300);
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function hideKeyboard()
{
	var sTextBoxID;
	var oElement = event.srcElement ;
	
	while( oElement &&	!sTextBoxID )
	{ 
		sTextBoxID = oElement.sTextBoxID;
		oElement = oElement.parentElement;
	}
	if( sTextBoxID )
		HideElement(sTextBoxID + "_keyboard", 1500);
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function CKeyboard_toggleShift()
{
	this.shift = (this.shift == true)? false:true;
	document.getElementById(this.text_box.id + "_keyboard").innerHTML = createKeys(this.keys, this.text_box.id);
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function CKeyboard_clearChar()
{
	this.text_box.value = this.text_box.value.substring(0,this.text_box.value.length-1);
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function CKeyboard_addChar( sChar )
{
	//alert(this.text_box.id);
	if (sChar == "BS")
	{
		this.text_box.value = this.text_box.value.substring(0,this.text_box.value.length-1);
	}
	else if (sChar == "&nbsp;")
	{
		this.text_box.value += " ";
	}
	else if (sChar == "CL")
	{
		this.text_box.value = "";
	}
	else
		this.text_box.value += sChar;

	
	var sFocusScript = "if( document.getElementById(\"" + this.text_box.id + "\") ) {" +
		"document.getElementById(\"" + this.text_box.id + "\").focus() }";
	
	window.setTimeout( sFocusScript, 10 );
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function ShowElement( sID, delay )
{
	var idElement = document.getElementById(sID);

	if( null == idElement )
		return;
	
	if( idElement.idDelayedShowHide )
	{
		window.clearTimeout( idElement.idDelayedShowHide );
		idElement.idDelayedShowHide = null;
	}

	if( 0 == delay )
	{
		idElement.style.zIndex  = 1;
		idElement.style.display = "";
	}
	else
	{
		idElement.idDelayedShowHide = window.setTimeout("ShowElement('" + sID + "', 0)", delay);
	}
}
//////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////
function HideElement( sID, delay )
{
	var idElement = document.getElementById(sID);
	
	if( null == idElement )
		return;

	if( idElement.idDelayedShowHide )
	{
		window.clearTimeout( idElement.idDelayedShowHide );
		idElement.idDelayedShowHide = null;
	}

	if( delay == 0 )
	{
		idElement.style.display = "none";
	}
	else
	{
		idElement.idDelayedShowHide = window.setTimeout("HideElement('" + sID + "', 0)", delay);
	}
}