/**
 * JS Class for the Rule Type Magnitude Widget.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program as the file license.txt. If not, see
 * <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>
 *
 * @package    CMS
 * @subpackage Widget
 * @author     Squiz Pty Ltd <products@squiz.net>
 * @copyright  2010 Squiz Pty Ltd (ACN 084 670 600)
 * @license    http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPLv2
 */

function RuleTypeMagnitudeWidgetType(id, value, min, max)
{
    this.value    = null;
    this.min      = null;
    this.max      = null;
    this.widgetid = '';

    if (typeof value !== 'undefined' && value !== null) {
        this.value = value;
    }

    if (typeof min !== 'undefined' && min !== null && min !== '') {
        this.min = Number(min);
    }

    if (typeof max !== 'undefined' && max !== null && max !== '') {
        this.max = Number(max);
    }

}

RuleTypeMagnitudeWidgetType.prototype = {
    reset: function()
    {
        this.simpleResult = false;
        this.widget       = null;
        this.value        = null;
        this.server       = true;
        this.client       = true;
        this.errors       = [];

    },

    setValue: function(val)
    {
        this.value = val;

    },

    setWidgetid: function(wid)
    {
        this.widgetid = wid;

    },

    validate: function()
    {
        var val = Number(this.value);

        // Not greater than.
        if (this.min === null && this.max !== null) {
            if (val > this.max) {
                return false;
            }
        }

        // Not less than.
        if (this.min !== null && this.max === null) {
            if (val < this.min) {
                return false;
            }
        }

        // Check if value is within specified range.
        if (this.min !== null && this.max !== null) {
            if (this.min >= this.max) {
                // Incorrect logic, skip this check.
                return true;
            }

            if (val < this.min || val > this.max) {
                return false;
            }
        }

        return true;

    }

};

