// boekkat: cuecat support for javascript
// copyright 2005 jared jennings. all rights reserved.
// licensed under a BSD-style license. See LICENSE.txt.

// howto:
//
// in the head of your html file include the scripts...
// <script language="JavaScript1.2" src="capture.js"></script>
// <script language="JavaScript1.2" src="decode.js"></script>
// <script language="JavaScript1.2" src="interpret.js"></script>
// <script language="JavaScript1.2" src="act.js"></script>
//
// now make another script tag
// in the body, not the head,
// AFTER the form whose field you want the barcode to go in (if any)
// and in this script tag, put...
//

// --------------------------------------------------------------------
// event handlers
// can't be members of an object, because they need 'this'

// for netscape/moz we catch alt+f10 here
function Capture_keyPressed(e) {
	if(!document.all) {
		if(e.keyCode == 121 && e.altKey) {
			Capture.catchIt();
			return false;
		}
	}
	// IE, or not alt+f10
	return true;
}

// for IE we catch alt+f10 here: its keypressed only catches letters.
function Capture_keyDown() {
	if(event.keyCode == 121 && event.altKey) {
		// it's the barcode reader!
		Capture.catchIt();
		return false;
	}
}

// -------------------------------------------------------------------
// BookCat API


function CaptureObject() {
	this.actor   = new Actor();
}

	CaptureObject.prototype.catchIt = function() {
		var rawCueCatOutput;
		var wantedOutput;
		rawCueCatOutput = prompt("CueCat gibberish goes here");
		// presto we've got it.
		this.runWithIt(rawCueCatOutput);
	};
	
	// division between catchIt and runWithIt is for unit-testability
	CaptureObject.prototype.runWithIt = function(rawCueCatOutput) {
		var triple = CueCat.decodeAll(rawCueCatOutput);
		this.actor.onBarcode(triple);
	};

	CaptureObject.prototype.handle = function(codeType, handler) {
		// if codeType is noHandler or noInterpreter, no problem!
		// there's no method in the Interpreter by that name, so attempts
		// to interpret using it will fail. so it will have no influence on
		// the interpretation process, only coming into play when an error
		// needs handling.
		this.actor.order.push(codeType);
		this.actor.handlers[codeType] = handler;
		this.hookUpEvents();
	};

	CaptureObject.prototype.hookUpEvents = function() {
		// document.all is only defined in IE - not Netscape or Mozilla.
		
		if(!document.all) {
			window.captureEvents(Event.KEYPRESS);
		}
		
		document.onkeypress=Capture_keyPressed;
		
		if(document.all) {
			document.onkeydown=Capture_keyDown;
		}
	};

Capture = new CaptureObject();


// ----------------------------------------------------------------------
// IE lessons:
// 0. As any javascript site can tell you, ie doesn't use the 
//    captureEvents method of the document/window object, and 
//    it has a global event variable, not a passed-in local one.
// 1. The keyPressed event does not catch function keys. Use keyDown.
// 3. if you don't have a JScript reference available - use Access.
//    make a database, add a form, add a Microsoft Web Browser object
//    view the form in normal view. in the Code window (alt+f11)
//    Tools/References... and pick Microsoft Internet Controls
//    (...\shdocvw.dll) and Microsoft HTML Library (...\mshtml.tlb).
//    in the Immediate window (ctrl+g) do 
//    Forms(0).Controls(0).Object.navigate2("c:\your\html\file.htm").
//    use the Debug/Watch thing to navigate the hierarchy, and the 
//    Object Browser to check on what methods you can call on objects.


