Option Explicit

' Retrieve locking code from user's PC and create email to send it to HCG

' =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =
' Get locking code
' =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =
' Uses SLM echoid utility to get locking code from user's PC
' For info on running DOS commands:
'    http://www.computerperformance.co.uk/ezine/ezine80.htm

Dim sRootDrive, sAppDir				' retrieve these properties from session object
Dim objShell, sCommand, objExec 	' used to run DOS commands and retrieve results
Dim sLockingCode, iPos				' stores working version of locking code and position of "4-"

' sRootDrive = Session.Property("ROOTDRIVE")
' sAppDir    = Session.Property("APPDIR")
sRootDrive = "C:"
sAppDir    = "c:\program files\taskmap\"

' We need to run echoid to get the locking code, but the context must be set to 
' the directory containing echoid.exe and echoid.dat before running it. Therefore,
' we need to create DOS commands that will 
' 1) open DOS box and set to root drive
' 2) change directory to application install directory
' 3) run echoid in silent mode
' NOTE: we use exec method, not run, because exec makes command output
' available via StdOut property
' Set objShell = CreateObject("Wscript.Shell")
' sCommand = "%comspec% /c " & sRootDrive & _
' 		   " & cd " & Chr(34) & sAppDir & Chr(34) & _
' 		   " & echoid -s"
' Set objExec = objShell.exec (sCommand)

' get output from echoid and isolate locking code by copying from "4-";
' only take 8 characters: 2 for 4-, 5 for locking code, 1 extra just in case
'sLockingCode = objExec.StdOut.readall()
sLockingCode = "  4-21CD4    "
If Len(slockingcode) = 0 Then
	MsgBox "LOCKING CODE LENGTH = ZERO"
End If

iPos = InStr(sLockingCode, "4-")
If iPos = 0 Then
	MsgBox "LOCKING CODE STRING STARTING WITH 4- NOT FOUND"
End If

'remove characters before "4-" and after locking code
sLockingCode = Mid(sLockingCode, ipos, Len(slockingcode)-iPos-3)
MsgBox "X" & sLockingCode & "X"

' return locking code to MSI even though we'll be using it below
'Session.Property("LOCKING_CODE") = sLockingCode

' =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =
' Create mailto URL for sending locking code to HCG
' =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =

'NOTE: 2009-10-01 switched from using Windows username to using Office username; 
'continuing to use Windows company name because Office doesn't let you enter one
'Dim sUsername      : sUsername       = Session.Property("USERNAME")
' Dim sUsername      : sUsername       = Session.Property("OFFICE_USERNAME")
' Dim sCompany       : sCompany        = Session.Property("COMPANYNAME")
' Dim sTMVersion     : sTMVersion      = Session.Property("ProductVersion")
' Dim sTMInstallType : sTMInstallType  = Session.Property("INSTALL_TYPE")
' Dim sManufacturer  : sManufacturer   = Session.Property("Manufacturer")
' 
Dim sUsername      : sUsername       = "Scott Helmers"
Dim sCompany       : sCompany        = "asdfasfasf"
Dim sTMVersion     : sTMVersion      = "3.x.x"
Dim sTMInstallType : sTMInstallType  = "Professional"
Dim sManufacturer  : sManufacturer   = "Harvard Computing Group"

Dim sURL

' Don't know exact upper limit on length of mailto URL that MSI can 
' handle, but the one below is very close
sURL = "mailto:support@TaskMap.com?Subject=TaskMap Locking Code&Body="
sURL = sURL & "Use this message to send us your locking code, or use the phone or fax numbers below. "
sURL = sURL & "We will email your TaskMap license to you." & "%0D%0A" & "%0D%0A"
sURL = sURL & "CUSTOMER INFORMATION"  & "%0D%0A" & "%0D%0A"
sURL = sURL & "TaskMap User: " & sUsername & "%0D%0A"
sURL = sURL & "Organization: " & sCompany & "%0D%0A" & "%0D%0A"
sURL = sURL & "TaskMap Version: " & sTMVersion & "%0D%0A"
sURL = sURL & "TaskMap Edition: " & sTMInstallType & "%0D%0A"
sURL = sURL & "Locking Code: " & sLockingCode & "%0D%0A" & "%0D%0A"
sURL = sURL & "VENDOR INFORMATION"  & "%0D%0A" & "%0D%0A"
sURL = sURL & sManufacturer & "%0D%0A"
sURL = sURL & "Phone: +1 978-800-4590 option 8" & "%0D%0A"
sURL = sURL & "Fax  : +1 978-428-0707" & "%0D%0A"

sURL = Replace(sURL, " ", "%20")
' replace ":" but only after "mailto:"
sURL = Left(sURL, 7) & Replace(Right(sURL, Len(sURL)-7), ":", "%3A")
sURL = Replace(sURL, "+", "%2B")

'MsgBox Left(sURL,581)
'Session.Property("MAILTO_URL") = Left(sURL, 581)
MsgBox sURL
'Session.Property("MAILTO_URL") = sURL

MsgBox "End of VBscript"

