/* Filename:    J007_SimpleInv7.js
 * Description: Simple Inventory Application
 * Copyright:   2006 The Code Corporation. Please see http://www.codecorp.com/jsLicensing.html for information regarding copyright and grant of license.
 * $Revision: 118 $
 * $Date: 2006-12-05 16:26:51 -0700 (Tue, 05 Dec 2006) $
 */
 
//Add buttons to form
// (Also, set input modes on edit fields)


//constants
var sepHeight  = 8;


// helper function(s)

function focusQuantity()
{
    inventory.setActiveChild(quantityEdit);
    
    /*workaround setActiveChild repaint bug*/
    gui.showForm(inventory);
}


//form event handlers
 
function cancel()
{
    // return to default application
    
    reader.runScript(".default.js");
}

function ok()
{
    // write to inventory.dat file
    
    if( quantityEdit.text == "" )
    {
        gui.alert("No quantity");
        focusQuantity();
        return;
    }
    
    var s = "MF:" + manufacturerEdit.text + "\t" + "PN:" + partEdit.text + "\t" + "QT:" + quantityEdit.text + "\n";
    
    gui.statusText = "Saving...";
    
    if( storage.append("inventory.dat", s) )
    {
        gui.statusText = "Saved";
        sleep_ms(2000);
        gui.statusText = "Inventory";
        
        manufacturerEdit.text = "";
        partEdit.text         = "";
        quantityEdit.text     = "";
    }
    else
    {
        gui.statusText = "Save failed!";
    }
}


// reader interface function(s)

reader.onDecode = function(decode)
{
    //splitting decoded data into 2 edit fields
    
    manufacturerEdit.text = decode.data.substr(0, 3);
    partEdit.text         = decode.data.substr(3, 3);
    
    focusQuantity();
}


//variables
var inventory        = new gui.Form(ok , cancel);
var manufacturerEdit = new gui.Edit("", gui.inputMode.caps);
var partEdit         = new gui.Edit("", gui.inputMode.numeric);
var quantityEdit     = new gui.Edit("", gui.inputMode.numeric, gui.inputMode.numeric);

//Add caption
inventory.caption    = "Inventory";

// add Label and Edit boxes to the form
inventory.append(new gui.Label("Manuf. ID:"));
inventory.append(manufacturerEdit);
inventory.append(new gui.Label("Part #:"));
inventory.append(partEdit);
inventory.append(new gui.Label("Quantity:"));
inventory.append(quantityEdit);

// add buttons (and separator)
inventory.append(new gui.Separator(sepHeight, gui.separatorStyle.horizontalLine));
inventory.append(new gui.Button("Save", ok));
inventory.append(new gui.Button("Upload all", function() { storage.upload("inventory.dat"); }));

// show the form
gui.showForm(inventory);

//EOF
