#version Smart_Script 1.10
#timeout 1200
#linewait 0
#error_continue

// Outlook2000 Mail Downloading.ssc (MCHK Version) for Smart Script
// Copyright 1999,2000 Sony Corp.
// VASVersion=1.0.00.11260
function  main( $argc , @argv )
{
    $check = 0;
    if ( $argc == 1 ) {
        if ( @argv[ 0 ] eq "c" ) {
            //0x0002 Check by MAPI
            //0x0004 Check by normal exit
            //0x0008 Check by Filesize
            //0x0010 Don't Dialup before exec script
            exit 6;
        }
    }
    if ( $argc > 0) {
        $check = @argv[ 0 ];
    }
    if ( $argc == 2 ) {
        :$limit = @argv[ 1 ];
    } else {
        :$limit = 15;
    }
    Initialize(  );
    if ( $check == 2 ) {
        $before = ExecuteFileWait( :$OMCHKpath );
    }
    $_result = IsWindowExisting( :$WndNameMain , :$ClassNameMain );
    if ( ! $_result ) {
        $_result = IsFileExisting( :$path );
        if ( $_result ) {
            $_result = ExecuteFile( :$path );
        } else {
            $_result = TimerMessage( :$errmsgFileNotFound , 3 );
            exit 1;
        }
    }
    %outlook = WaitWindowOpen( :$WndNameMain , :$ClassNameMain , 30 );
    $_result = IsWindowValid( %outlook );
    if ( $_result ) {
        $_result = Sleep( 10 );
        %baloon = WaitWindowOpen( :$WndNameBalloon , :$ClassNameBalloon , 5 );
        $_result = IsWindowValid( %baloon );
        if ( $_result ) {
            $_result = DialogPushButton( %baloon , :$BnOK , "s" );
            $_result = Sleep( 5 );
        }
        $_result = RestoreWindow( %outlook );
        $_result = ActivateWindow( %outlook );
        $_result = PressKey( %outlook , "I" , "sc" );
        $_result = SetTimer(  );
        while( ! IsWindowValid( %state ) && GetTimer( ) < 10 ) {
            $_result = PressKey( %outlook , "M" , "c" );
            $_result = Sleep( 1 );
            %state = GetWindowHandle( :$WndNameState , :$ClassNameDlg );
        }
        $_result = IsWindowValid( %state );
        if ( $_result ) {
            $_result = SetTimer(  );
            $NotConnect = 0;
            $Error = 0;
            while( ! $NotConnect && ! $Error && GetTimer( ) < :$limit * 60 ) {
                $NotConnect = IsWindowExisting( :$WndNameFinish , :$ClassNameStatic );
                $Error = IsWindowExisting( :$WndNameError , :$ClassNameStatic );
            }
            if ( $NotConnect ) {
                if ( $check == 2 ) {
                    $after = ExecuteFileWait( :$OMCHKpath );
                    $result = MAPICheck( $before , $after );
                } else {
                    $result = 0;
                }
                exit $result;
            } else {
                if ( $Error ) {
                    $_result = TimerMessage( :$errmsgTaskError , 3 );
                } else {
                    $_result = TimerMessage( :$errmsgEnd , 3 );
                }
                exit 1;
            }
        } else {
            $_result = TimerMessage( :$errmsgStatusDlgNotFound , 3 );
            exit 1;
        }
    } else {
        $_result = TimerMessage( :$errmsgCantOpen , 3 );
        exit 1;
    }
}
//
function $ MAPICheck( $before , $after )
{
    if ( $before != -1 && $after != 1 ) {
        if ( $before < $after ) {
            return -1;
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}
//
function  Initialize(  )
{
    :$path = LoadRegistry( "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Office\\9.0\\Common\\InstallRoot\\" , "Path" );
    :$path &= "\\Outlook.exe ";
    //Window
    :$WndNameMain = "* - Microsoft Outlook";
    :$ClassNameMain = "rctrl_renwnd32";
    :$WndNameBalloon = "* - Microsoft Outlook";
    :$ClassNameBalloon = "MSOBALLOON";
    :$WndNameState = "Microsoft Outlook";
    :$WndNameFinish = "The requested tasks were completed successfully.";
    :$WndNameError = "Some errors occurred while processing the requested tasks.  Please review the list of errors below for more details.";
    :$ClassNameDlg = "#32770";
    :$ClassNameStatic = "Static";
    //Control
    :$BnOK = "OK";
    //Error Messages
    :$errmsgFileNotFound = "Outlook.exe not found.";
    :$errmsgEnd = "Terminated by timeout or by pressing cancel button.";
    :$errmsgStatusDlgNotFound = "Cannot find the Download state dialog.";
    :$errmsgCantOpen = "Cannot find Outlook98.";
    :$errmsgTaskError = "Some errors occurred while processing the requested tasks.";
    //for Mail Check
    :$OMCHKpath = "bin\\SEOMCHK.exe";
}
