/**
 * JS Class for the Rule Type Integer 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 RuleTypeIntegerWidgetType(id, value, intOnly)
{
    this.value    = null;
    this.intOnly  = false;
    this.widgetid = '';

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

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

}

RuleTypeIntegerWidgetType.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()
    {
        // Check if this is an integer.
        if (this.intOnly === true) {
            // Make sure it does not end with '.0' or '.000' etc..
            var checkVal     = this.value;
            var val          = Number(this.value);
            var decimalCheck = (/\.0+$|\.$/.test(checkVal));
            checkVal         = parseInt(val, 10);
            if (val !== checkVal || decimalCheck === true) {
                return false;
            }
        }

        return true;

    }

};

