//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// MAKL: 2007-03-01   
// Capture a keypress by implementing 
// the function OnKeyPress(keyCode)
// Dont forget to call InitKeyPress to start 
// catching...
// POMU: 2007-09-19
// Modified for jQuery
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

var _keyCap_init = false;

// *** Element key hooks ***
function InitKeyHook(id) {
	$(document).ready(function() {
		$("#"+id).keypress(HandleKeyPress);
	});
}

function HandleKeyPress(e) {
	var id = e.target.id;
    var callback = "if(window.OnKeyPress_"+id+") window.OnKeyPress_"+id+"(e.target, e.which);";
    
    // Callback will return true if it handled the event
	var handled = eval(callback);
	if(handled) {
		e.preventDefault();
		e.stopPropagation();
	}
}

// *** Global key hooks ***
function InitGlobalKeyHook() {
	if(_keyCap_init)
		return;

	$(document).ready(function() {
		$(document).keypress(HandleGlobalKeyPress);
	});

	_keyCap_init = true;
}

function HandleGlobalKeyPress(e) {
		
	var handled = false;
	if(window.OnKeyPress)
		handled = window.OnKeyPress(e.which);

	if(handled) {
		e.preventDefault();
		e.stopPropagation();
	}
	
}
