﻿function ShowPassKeyboardTerminal()
{

$('#keyboard').css('z-index','20000');

	
	obj=document.getElementById('showkeyboard');

	document.getElementById('search_inp').value='';
	fireEvent(obj,'click');
	
	

}

function ShowPassKeyboard()
{

$('#keyboard').css('z-index','20000');

	
	obj=document.getElementById('showkeyboard');
	fireEvent(obj,'click');

}

function VirtKeyBoard(obj_name)
{
	document.getElementById('pwd').value=obj_name;
}





$(document).ready(function(){
    
	var shifton = false;
	var langon = false;
	
	// toggles the keyboard to show or hide when link is clicked
	$("#showkeyboard").click(function(e) {
		var height = $('#keyboard').height();
		var width = $('#keyboard').width();
		leftVal=e.pageX-40+"px";
		topVal=e.pageY+20+"px";
		$('#keyboard').css({left:leftVal,top:topVal}).toggle();
	});
	
	// makes the keyboard draggable
	$("#keyboard").draggable();	
	
	// toggles between the normal and the "SHIFT keys" on the keyboard
	function onShift(e) {
		var i;
		if(e==1) {
			for(i=0;i<4;i++) {
				var rowid = "#row" + i;
				$(rowid).hide();
				$(rowid+"_rus").hide();
				$(rowid+"_rus_shift").hide();
				$(rowid+"_shift").show();
			}
		}
		else {
			for(i=0;i<4;i++) {
				var rowid = "#row" + i;
				$(rowid).show();
				$(rowid+"_shift").hide();
				$(rowid+"_rus").hide();
				$(rowid+"_rus_shift").hide();

			}
		}
	 }

	// toggles between the normal and the "SHIFT keys" on the keyboard
	function onRus(e) {
		var i;
		if(e==1) {
			for(i=0;i<4;i++) {
				var rowid = "#row" + i;
				$(rowid).hide();
				$(rowid+"_shift").hide();
				$(rowid+"_rus_shift").show();
				$(rowid+"_rus").hide();

			}
		}
		else {
			for(i=0;i<4;i++) {
				var rowid = "#row" + i;
				$(rowid).hide();
				$(rowid+"_shift").hide();

				$(rowid+"_rus_shift").hide();
				$(rowid+"_rus").show();

			}
		}
	 }
	
	// function thats called when any of the keys on the keyboard are pressed
	$("#keyboard input").bind("click", function(e) 
	{
		var obj=document.getElementById('pwd').value;
		var object=document.getElementById(obj);
		var o_val=object.value;
		
		//alert('click='+o_val);
		//alert(o_val.indexOf('Введите название товара'));
		if(o_val.indexOf('Введите название товара')>=0)
			object.value=o_val.replace('Введите название товара','');
		
	
		if(o_val.indexOf('Введите P/N товара')>=0)	
			object.value=o_val.replace('Введите P/N товара','');
		
		if(o_val.indexOf('Введите код товара')>=0)
			object.value=o_val.replace('Введите код товара','');
		
		//alert('obj='+object.value);
							//alert('here');		   
		if( $(this).val() == 'Backspace' ) 
		{

			$('#'+obj).replaceSelection("", true);
		}
		
		else if(  ($(this).val() == "LATIN") || ($(this).val() == "Latin") ) 
		{
			if(shifton == true) 
			{
				onShift(0);	
				shifton = false;
			}
			
			else 
			{
				onShift(1);
				shifton = true;
			} 
		}
		else if( ( $(this).val() == "Русский" ) || ($(this).val() == "РУССКИЙ" ) )
		{
			if(langon == false) 
			{
				//alert('1'+$(this).val());
				onRus(1);	
				shifton = false;
				langon = true;
			}
			
			else 
			{
				//alert('0'+$(this).val());
				onRus(0);
				shifton = false;
				langon = false;
			} 
		}
		else if($(this).val() == "закрыть" )
		{
			ShowPassKeyboard();
		}
		
		else {

				
			$('#'+obj).replaceSelection($(this).val(), true);
			/*
			if(shifton == true) {
				onShift(0);
				shifton = false;
			}
			*/
		}

		//$('#'+obj).trigger('keyup');
		//alert('obj='+obj);
		//alert(document.getElementById(obj));
		
		//alert('value='+object.value);
		fireEvent(object,'keyup');	
	});
	
});




