/*
 * Author       : sampleapps
 * Generated on : 23-Nov-2009 01:48:48
 * Version      : 3.0
 */
application "Rank Calculation"
{
    type = public
    date format = "dd-MMM-yyyy"
    time zone = "America/Los_Angeles"
    section Home
    {
        form  Score_Details
        {
            displayname  =  "Score Details"
            
            Rank
            (
                type  =  number
                width  =  20
            )

            Name
            (
                type  =  text
            )

            Score
            (
                type  =  number
                width  =  20
                on update
                {
                    for each a in Score_Details
                    {
                        r = 1;
                        for each b in Score_Details
                        {
                            c = 0;
                            d = 0;
                            if (a.Score  !=  null)
                            {
                                c = a.Score;
                            }
                            if (b.Score  !=  null)
                            {
                                d = b.Score;
                            }
                            if (c  <  d)
                            {
                                r = (r  +  1);
                            }
                        }
                        a.Rank = r;
                    }
                }
            )

            actions
            {
                on add
                {
                    on load
                    {
                        hide Rank;
                    }
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                        on success
                        {
                            for each a in Score_Details
                            {
                                r = 1;
                                for each b in Score_Details
                                {
                                    c = 0;
                                    d = 0;
                                    if (a.Score  !=  null)
                                    {
                                        c = a.Score;
                                    }
                                    if (b.Score  !=  null)
                                    {
                                        d = b.Score;
                                    }
                                    if (c  <  d)
                                    {
                                        r = (r  +  1);
                                    }
                                }
                                a.Rank = r;
                            }
                        }
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    on load
                    {
                        hide Rank;
                        input.Score = 0;
                    }
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                        on validate
                        {
                            oldScore = 0;
                            if (old.Score  !=  null)
                            {
                                oldScore = old.Score;
                            }
                            input.Score = (input.Score  +  oldScore);
                        }
                        on success
                        {
                            for each a in Score_Details
                            {
                                r = 1;
                                for each b in Score_Details
                                {
                                    c = 0;
                                    d = 0;
                                    if (a.Score  !=  null)
                                    {
                                        c = a.Score;
                                    }
                                    if (b.Score  !=  null)
                                    {
                                        d = b.Score;
                                    }
                                    if (c  <  d)
                                    {
                                        r = (r  +  1);
                                    }
                                }
                                a.Rank = r;
                            }
                        }
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
                on delete
                {
                    on success
                    {
                        for each a in Score_Details
                        {
                            r = 1;
                            for each b in Score_Details
                            {
                                c = 0;
                                d = 0;
                                if (a.Score  !=  null)
                                {
                                    c = a.Score;
                                }
                                if (b.Score  !=  null)
                                {
                                    d = b.Score;
                                }
                                if (c  <  d)
                                {
                                    r = (r  +  1);
                                }
                            }
                            a.Rank = r;
                        }
                    }
                }
            }
        }

        list  Score_Details_View
        {
            displayname = "Score Details View"
            show  all  rows  from  Score_Details 
            (
                Rank
                Name
                Score
            )
            group by
            (
                Score   ascending
                Rank   descending
            )
            options
            (
                display rows = 100
            )
            permission
            (
                add = true
                edit = true
                delete = true
            )
        }

    }

}
