////////////////////////////////////////////////////////////////////////////////////
// Dean Vios
// Microsan
// 631.244.9200
// dvios@microsanpos.com
// 
// (This should work on v3.0 sp5 and above, this was tested on 3.2 sp1)
//
// This sim...
// Inq 1 - Starts a check with a Credit Card
// Inq 2 - Starts a check with a Credit Card & automatically performs an Init Auth
// Inq 3 - Picks up a check with a Credit Card
// Inq 4 - Renames check with a Credit Card

// Set configurable variables below

// enable or disable guest count, 0 to disable
var enable_gst_cnt	: n1 = 0

// set the next touchscreen to display after Inq 2 is performed
// set to 0 if this is not needed
var next_screen		: n99 = 0


// End configurable variables

////////////////////////////////////////////////////////////////////////////////////

var search_char		: a1 = "^"
var charpos		: n3 = 0
var charpos2		: n3 = 0
var CC_Track1 		: a78
var CC_Track2 		: a78
var fCC_Track1 		: a78
var fCC_Track2 		: a78
var cc_auth_Key		: key = key(1,458787)
var init_auth_key	: key = key(1,458786)
var begin_id_key	: key = key(1,327683)
var begin_check		: key = key(1,327681)
var guests_key		: key = key(1,458769)
var check_id_key	: key = key(1,393222)
var pickup_key		: key = key(1,327686)
var cardholder_name 	: a78
var account_num 	: a78
var guest_num	 	: a99

////////////////////////////////////////////////////////////////////////////////////
// Start a check with a CC

Event Inq : 1

  input CC_Track1 {m1, *}, "Swipe Credit Card"

call format_magdata

// search for the characters

call search_char
call search_char2
call format_strings 

if enable_gst_cnt = 1 then

  input guest_num, "Enter Number of Guests"

 loadkybdmacro begin_check, makekeys(guest_num), guests_key, makekeys (cardholder_name), check_id_key

else

 loadkybdmacro makekeys (cardholder_name), begin_id_key

endif

EndEvent

////////////////////////////////////////////////////////////////////////////////////
// Start a check with a CC & auto Init Auth

Event Inq : 2

   window 1, 1
   displaymsinput 0, 0, cc_track1 {m1, *}, "Swipe Credit Card", \
		  0, 0, cc_track2 {m2, *}, ""
   windowinput

// the input statement below doesn't work in v3.2 sp1 but should?, track 2 data is not read
//  input cc_track1 {m1, *}, cc_track2 {m2, *}, "Swipe Credit Card"

call format_magdata

// search for the characters

call search_char
call search_char2
call format_strings

if enable_gst_cnt = 1 then

  input guest_num, "Enter Number of Guests"

 loadkybdmacro begin_check, makekeys(guest_num), guests_key, makekeys (cardholder_name), check_id_key, init_auth_key
     LoadMagInfo fCC_Track1,fCC_Track2
     LoadKybdMacro key(1,278528)
	if next_screen <> 0 then
	 displaytouchscreen next_screen
	endif
else

 loadkybdmacro makekeys (cardholder_name), begin_id_key, init_auth_key
     LoadMagInfo fCC_Track1,fCC_Track2
     LoadKybdMacro key(1,278528)
	if next_screen <> 0 then
	 displaytouchscreen next_screen
	endif

endif

EndEvent

////////////////////////////////////////////////////////////////////////////////////
// Pickup a check with a CC

Event Inq : 3

  input CC_Track1 {m1, *}, "Swipe Credit Card"

call format_magdata

// search for the characters

call search_char
call search_char2
call format_strings 

loadkybdmacro makekeys (cardholder_name), pickup_key

EndEvent

////////////////////////////////////////////////////////////////////////////////////


// Name an existing check from a CC  - dlarocque, 7/15/08

Event Inq : 4

  input CC_Track1 {m1, *}, "Swipe Credit Card"

call format_magdata

// search for the characters

call search_char
call search_char2
call format_strings 



 loadkybdmacro makekeys (cardholder_name), check_id_key


EndEvent

////////////////////////////////////////////////////////////////////////////////////

sub format_magdata

  If (@MAGSTATUS = "Y")
    format fCC_Track1 as "%", CC_Track1, "?" 	// Format is "%" + Track 1 mag Info + "?"
    format fCC_Track2 as ";", CC_Track2, "?" 	// Format is ";" + Track 2 Mag Info + "?"
  EndIf

endsub

sub search_char

   for charpos = 1 to len(cc_track1)

       if search_char = mid(cc_track1, charpos, 1)
		   return
	endif
   endfor                                                 

   //If not found, error & exit

	errormessage "Invalid Data Entered"
	exitcancel
endsub

sub search_char2

  for charpos2 = 1 to len(cc_track1)

   //If we find search_char and its not the same position as before, return charpos2

       if search_char = mid(cc_track1, charpos2, 1) and charpos <> charpos2
		   return
       endif
   endfor

   //If not found, error & exit

	errormessage "Invalid Data Entered"
	exitcancel
endsub

sub format_strings

   // bump the position up a notch to make the formulas neater
	charpos = charpos + 1

	cardholder_name = trim(mid(cc_track1,charpos,charpos2-charpos))
	account_num = mid(cc_track1,2,charpos-3)

  // trim the cardholder data if its too long

	if len(cardholder_name) > 16 then
		cardholder_name = mid(cardholder_name,1,16)
	endif
endsub
