/**
 * JS Class for the Rule Type Length 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 RuleTypeLengthWidgetType(id, value, operator, length)
{
    this.widgetType = 'RuleTypeLengthWidgetType';

    this.value    = value;
    this.length   = null;
    this.operator = null;
    this.plain    = false;
    this.widgetid = '';

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

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

}

RuleTypeLengthWidgetType.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;

    },

    setLength: function(len)
    {
        this.length = len;

    },

    setPlain: function(plain)
    {
        // Set this to true, if value = length to check instead of a str.
        this.plain = plain;

    },

    setOperator: function(op)
    {
        this.operator = op;
        switch (op) {
            case '==':
                this.opDesc = 'equal to';
            break;

            case '!=':
                this.opDesc = 'no equal to';
            break;

            case '>':
                this.opDesc = 'larger than';
            break;

            case '<':
                this.opDesc = 'smaller than';
            break;

            case '>=':
                this.opDesc = 'equal or larger than';
            break;

            case '<=':
                this.opDesc = 'equal or smaller than';
            break;

            default:
                this.opDesc = '';
            break;
        }//end switch

    },


    validate: function()
    {
        this.errors = [];
        var result  = false;
        var length  = this.value.length;
        if (this.plain === true) {
            length = this.value;
        }

        var evalStr = 'result = (' + length + ' ' + this.operator + ' ' + this.length + ');';
        eval(evalStr);
        if (result === true) {
            return true;
        } else {
            var msg = 'The length of "' + this.value + '" is not ' + this.opDesc + ' ' + this.length + '.';
            this.errors.push(msg);
            return false;
        }

    },

    getErrors: function()
    {
        return this.errors;

    }


};

