function addListener(a,b,c,d){if(a.addEventListener){a.addEventListener(b,c,d);return true;}else if(a.attachEvent){var e=a.attachEvent("on"+b,c);return e;}else{alert("Handler could not be attached");}}

function bind(a,b,c,d){return window.addListener(a,b,function(){d.apply(c,arguments)});}

function handleKeystroke(evt) 
{  

	var msg='';
	// Grab the cross browser event
	if( !evt ) evt = window.event;
	// Garb the combo key press	

	if (evt.ctrlKey)
	{ 
		try
		{
			switch (evt.keyCode )
			{
			case 83:
				if(typeof this[83]=='function')
				{
					this[83]();
				}
				break;
			}
		}
		catch (e)
		{
			alert(e.description);
		}
		
	}

	if (evt.ctrlKey && evt.shiftKey)
	{
		var win;
		win=window.parent;
		if(typeof win.keylogger!='function')
		{
			win=GetKeyloggerAPI(window);
		}

		if(win!=null) // Check if the win object does not has the logger API i.e. It is a pop-up window.
		{
			switch (evt.keyCode)
			{
			case 83: 
				win.keylogger('HotkeyStudentMode');
				break;
			case 67: 
				win.keylogger('HotkeyCourseAdminMode');
				break;
			case 82: 
				win.keylogger('HotkeyResultAdminMode');
				break;
			case 84:
				win.keylogger('HotkeyStatisticAdminMode');
				break;
			case 85:
				win.keylogger('HotkeyUserAdminMode');
				break;
			case 71:
				win.keylogger('HotkeyUserAdvSearch');
				break;
			}
		}
		else
		{
			//It is not a Top menu Short key.. It is local call for "Save" or some other navigation..
			//handel it on local level not from default.asp page: function keylogger().
		}
		cancelEvent(evt); //To calcel the default action if any
	}
	/*
	// Character code of key pressed
	var asc = !evt.keyCode ? (!evt.which ? evt.charCode : evt.which) : evt.keyCode;
	// ASCII character of above code
	var chr = String.fromCharCode(asc).toLowerCase();
	for (var i in this)
	{
	  if (asc == i)
	  {
		this[i](evt);
		break;
	  }
	}
	*/
	
}
function cancelEvent(evt)
{
evt.cancelBubble = true;
evt.returnValue = false;
if (evt.preventDefault) evt.preventDefault();
if (evt.stopPropagation) evt.stopPropagation();
return false;
}
//
// KEY COMMANDS
var keyMap = new Array();
var PAGE_DOWN  = 34;
var PAGE_UP    = 33
var ARROW_DOWN = 40
var ARROW_UP   = 38

keyMap[PAGE_DOWN] = pageDown;
keyMap[ARROW_DOWN] = pageDown;
keyMap[PAGE_UP] = pageUp;
keyMap[ARROW_UP] = pageUp;

//

function pageDown(evt)
{
alert("You clicked DOWN and we are going down");
}


function pageUp(evt)
{
alert("You clicked UP but I am canceling that request");
cancelEvent(evt);
}
function CtrlShiftS(evt)
{
alert("You clicked Ctrl + Shift + S");
cancelEvent(evt);
}
		function HoykeyMode(strUrl,frameSide)
		{
			if (frameSide==undefined)
			{
				top.LeftFrame.document.location=strUrl;
			}
			else
			{
				top.MainFrame.location=strUrl;
			}
		}

		
	function GetKeyloggerAPI(win)
	{
		
	  if ((win.parent != null))
	  {
		  
		gAPI = _ScanForKeyloggerAPI(win.parent);
	  }
	  return gAPI;
	}

	function _ScanForKeyloggerAPI(win)
	{
	  // This function is called by GetKeyloggerAPI
	  var nFindAPITries = 10;
	  while ((win.keylogger == null)&&(win.parent != null))
	  {
		nFindAPITries--;
		if (nFindAPITries < 0) return null;
		win = win.parent;
	  }
	  return win;
	}


//Wraper function for custom functions.

function proxyCaller(fn)
{
	if(typeof fn=='function')
		fn();
}


// Add the keydown listner to the document object for global capture
bind(document, 'keydown', keyMap, handleKeystroke);

