
#import "cardCheck.h"

@implementation cardCheck

- appDidInit:sender
{

    ouvert = 1;
    ferme = 0;
    windowState = ferme;

    [TheWindow makeKeyAndOrderFront:self];
    [CCnumber setStringValue:""];
    [CCnumber selectText:self];
    [Status setStringValue:"Welcome to cardCheck!!"];
    [crcStat setStringValue:""];
    [attentionTexte setStringValue:"Read the warnings in the info menu..."];


    return self;
}

- textDidChange:sender
{

    [Status setStringValue:""];
    [crcStat setStringValue:""];
    [attentionTexte setStringValue:""];


    return self;
}



- CheckIt:sender
{
    // Local Variables
    char cc[120];
    int check, len, prod, j;

    	// Assign the value in the panel to the cc variable.
        strcpy(cc, [CCnumber stringValue]);
    
        if (cc[0] != '3' && cc[0] != '4' && cc[0] != '5' && cc[0] != '6') {
            [Status setStringValue:"Card number must start         with 3, 4, 5, or 6."];
            [CCnumber selectText:self]; 
            return self;
        }

        // Counts the string length of the cc variable.
        len = strlen(cc);

        
        if (cc[0] == '5' && len != 16) {
            [Status setStringValue:"Mastercard must be 16 digits."];
            [CCnumber selectText:self]; 
            return self;
        }

        
        if (cc[0] == '4' && (len != 13 && len != 16)) {
            [Status setStringValue:"Visa numbers must                     be 13 or 16 digits."];
            [CCnumber selectText:self]; 
            return self;
        }

        
        if (cc[0] == '3' && len != 15) {
            [Status setStringValue:"American Express numbers must be 15 digits."];
            [CCnumber selectText:self]; 
            return self;
        }

        
        if (cc[0] == '6' && len != 16) {
            [Status setStringValue:"Discover numbers                   must be 16 digits."];
            [CCnumber selectText:self]; 
            return self;
        }

        
        //  Perform checksum - Weighing list 212121212121...
        check = 0;
        for (j = 0; j < len; ++j) {
            prod = cc[j] - '0';
            if (((len - (j + 1)) %2)) {
                prod *= 2;
            }
            if (prod >= 10) {
                prod = prod - 9;
            }
            check += prod;
        }
        
        if ((check % 10) == 0) {
            [crcStat setStringValue:"GOOD"];
            [CCnumber selectText:self]; 
            [attentionTexte setStringValue:"NOTE: Valid card number but is it a valid account? Careful..."];
        } else {
            [crcStat setStringValue:"BAD"];
            [attentionTexte setStringValue:"This is not a valid card number..."];

            [CCnumber selectText:self]; 
        }
        


    return self;
}

- ClearStatus:sender
{
    [Status setStringValue:"Ummmmmm...."];

    return self;
}

- InfoPanel:sender
{
    [NXApp loadNibSection:"InfoPanel.nib" owner:self]; // load nib

    return self;
}

- IntroAndHelp:sender
{

    [NXApp loadNibSection:"HelpIntro.nib" owner:self]; // load nib

    return self;
}

- MoreInfo:sender
{
    [NXApp loadNibSection:"MoreInfo.nib" owner:self]; // load nib

    return self;
}

- warning:sender
{
    [NXApp loadNibSection:"warning.nib" owner:self]; // load nib

    return self;
}

- setWindowSize:sender
{

    if (windowState == ferme)  {
        windowState = ouvert;
        NX_HEIGHT(&cadre) = 336;
        NX_WIDTH(&cadre) = 660; }
    else  {
        windowState = ferme;
        NX_HEIGHT(&cadre) = 336;
        NX_WIDTH(&cadre) = 392;
    };

    [TheWindow sizeWindow : NX_WIDTH(&cadre) : NX_HEIGHT(&cadre) ];
    [TheWindow display];

    return self;
}

- showMainWindow:sender
{

    [TheWindow makeKeyAndOrderFront:self];

    return self;

}

@end
