// keycatcher.js

/*  **************************************************************
	added in 7.1.6 to handle trapping the enter key everywhere 
	and to do some other fancy stuff in particular pages
	<script language="JavaScript" src="keycatcher.js" type="text/javascript"></script>
	<script>KeyCatch(jsCurRun,'testpage',0);</script>
	*******************************************************************  */

var pageName = '';
var helpStr = 'Unknown page';
var tfAlerts = 0;
var codeLocation = 'UNKNOWN_CODE_LOCATION';
var serverUrl = 'UNKNOWN_SERVER';

function BrowserType () 
{
  var srchText = navigator.userAgent;
  var brwTypes = ("Opera,MSIE,Netscape,Firefox").split(',');
  for (var ix=0; ix < brwTypes.length; ix++) {
		if (srchText.toString().match(brwTypes[ix])) {
			  return brwTypes[ix];
		}
  }
  return null;
}

var brwType = BrowserType();

function GetServerUrl() 
{
	if (codeLocation == 'EW-PRO')
		return 'http://www.eventkeeper.com/';
 	else
	if (codeLocation == 'EW-DEV')
		return 'http://www.eventkeeper.com/74x/';
	else
	if (codeLocation == 'LOCAL-DEV')
		return 'http://localhost:8500/ek_74x/';
	else
	{
		alert('unknown code location = ' + codeLocation);
		return 'woofta';
	}
}


var pK = 0;

var loginASCII = '';
var loginChars = '';
var loginStr = '';
var curObj = '';

function keyHand(e) 
{
	evt = (e) ? e : window.event;
	pK = e ? e.which : window.event.keyCode;
	var theObj=(window.event)?window.event.srcElement:arguments.callee.arguments[0].target;
	if (theObj != curObj)
	{
		loginChars = '';
		curObj = theObj;
	}
	if (theObj.type == 'textarea' || theObj.type == 'submit')
	{
		return true;
	}
	
	if (pK == 13)
	{
		switch(pageName)
		{
			case (pageName = 'passlist'):
			{
				if (theObj.name == 'curPassSearchText')
				{
					// alert("keycatcher 78");
					document.SetDisplayCriteria.curNumDays.value = '-30';	
					return true;
				}
				else
					return false;
				break;
			}

			case (pageName = 'mvlc'):
			{
				if (theObj.name == 'fSearchNameStr')
				{
					GetNameList();	
					return false;
				}
				else
					return false;
				break;
			}
			case (pageName = 'eventlist'):
			case (pageName = 'passlist_old'):
			case (pageName = 'index'):
			case (pageName = 'editor'):
			{
				return false;
				break;
			}
			default:
			{
				return false;
				break;
			}
		}
	}
	
	loginASCII = pK;
	if 		(loginASCII == 186) loginChars += ';';
	else if (loginASCII == 191) loginChars += '/';
	else loginChars += String.fromCharCode(loginASCII);
	
	if (loginChars.toLowerCase().indexOf("lll") != -1)
	{		
		loginChars = '';
		window.location.href = serverUrl + 'login.cfm';
	}
	else
	if (loginChars.indexOf("??") != -1)
	{
		var str_end = loginChars.indexOf("??");
		var theChars = loginChars.substring(0,str_end);
		if (theChars == '')	alert(helpStr);
		else alert ("String = " + theChars );
		loginChars = '';
		return false;
	}
	else
	if (loginChars.indexOf(";;") != -1)
	{
		var newOrg = loginChars.substring(0,loginChars.indexOf(";;"));
		window.location.href= serverUrl + newURL + newOrg;
		loginChars = '';
	}
	else
	if (loginChars.indexOf("gowingnut") != -1)
	{
		window.location.href= serverUrl + 'login.cfm?curOrg=RPL&uid=rpl-wb&pwd=wb';
		loginChars = '';
	}
	return true;
}
		

function InstallKeyHandler (tfAlerts)
{
	if(tfAlerts)
		alert("Installing new key handler");
	
 	if (!brwType) { return; }

    if (brwType == 'MSIE') 
	{
    	document.onkeypress = keyHand;
    	// document.onkeydown = keyHand;
   	}
  	else if (brwType == 'Firefox') 
	{
    	document.onkeypress = keyHand;
  	}
 	else if (brwType == 'Netscape') 
	{
		/*
		document.onkeypress = keyHand;
	
		if (document.captureEvents) 
		{ document.captureEvents (Event.KEYPRESS);}
		*/
  	}
   	else 
	{
    	// alert ('Unsupported Browswer');
  	}
}

function KeyCatch(codeLoc,passInPageName,tfShowAlerts)
{
	codeLocation = codeLoc;
	pageName = passInPageName;
	tfAlerts = tfShowAlerts;//
	
	serverUrl = GetServerUrl();

	if (pageName == 'testpage')
	{
		helpStr = 'Testing Keystrokes:\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to EventKeeper for ORGCODE\n\n?? - This help message';
		helpStr += '\n\nCode Location = ' + codeLocation;
		newURL = 'events.cfm?curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
	if (pageName == 'eventlist')
	{
		helpStr = 'EventKeeper Keystrokes:\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to EventKeeper for ORGCODE\n\n?? - This help message';
		newURL = 'events.cfm?curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
	if (pageName == 'passlist')
	{
		helpStr = 'TixKeeper with Scanner:\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to TixKeeper for ORGCODE\n\n?? - This help message';
		newURL = 'events.cfm?curApp=passes&curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
	if (pageName == 'passlist_old')
	{
		helpStr = 'TixKeeper Standard Keystrokes:\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to TixKeeper for ORGCODE\n\n?? - This help message';
		newURL = 'events.cfm?curApp=passes&curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
	if (pageName == 'index')
	{
		helpStr = 'Plymouth Rocket Keystrokes:\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to EventKeeper for ORGCODE\n\n?? - This help message';
		newURL = 'code/events.cfm?curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
	if (pageName == 'editor')
	{
		helpStr = 'Plymouth Rocket Editor Keystrokes:\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to EventKeeper for ORGCODE\n\n?? - This help message';
		newURL = '../code/events.cfm?curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
	if (pageName == 'mvlc')
	{
		helpStr = 'TixKeeper Keystrokes (MVLC):\n\nlll - Go to Login Page\n\nORGCODE;;';
		helpStr += '- Go to TixKeeper for ORGCODE\n\n?? - This help message';
		newURL = '../code/events.cfm?curOrg=';
		if (tfAlerts)
			alert("Start KeyCatch for " + pageName);
	}
	else
		alert("BAD CODEDOG!\n\nCalled KeyCatch without specifying a valid page name.");
	
	InstallKeyHandler(tfAlerts);
}			

