/*
    SCL SOFTWARE Javascript library.
    Copyright (C) SCL SOFTWARE 1999/2009.  All rights reserved.
    Santiago Corredoira Lascaray
    version: 2.0
*/


if (typeof Scl == "undefined")
{
    var Scl = {};
}   

if (typeof Scl.Exception == "undefined")
{
    Scl.Exception = function(message)
    {
        this.Message = message;
        this.Name = "Exception";
    }; 
    
    Scl.Exception.prototype.toString = function()
    {
        return this.Name + ': "' + this.Message + '"';
    };
}
 
(function() {

    Scl.getElement = function(id) {
        if (typeof (id) == 'string') {
            return document.getElementById(id);
        }
        else {
            return id; // Not an id name, just return it.
        }
    };


    Scl.contains = function(array, element) {
        for (var i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    };

    /**
    * Required field validation
    */
    Scl.RequiredValidation = function(control, message) {
        this.control = control;
        this.message = message;
       
        this.ctrl1;

        this.validate = function() {
            this.ctrl1 = Scl.getElement(this.control);
            if (this.ctrl1 == null) {
                throw new Scl.Exception(this.control + " is undefined");
            }

            return (this.ctrl1.value != '');
        }
    };

    /**
    * Required field validation
    */
    Scl.TinyMceRequiredValidation = function(control, message) {
        this.control = control;
        this.message = message;
        this.ctrl1;

        this.validate = function() {
            var editor = tinyMCE.get(control);
            var valid = editor.getContent() != '';

            if (!valid) {
                var style = editor.getWin().document.body.style;
                style.border = '1px solid #ff0000';
                style.backgroundColor = '#ffeeee';
            }

            return (valid);
        }
    };

    /**
    * Regular expression validation
    */
    Scl.RegExValidation = function(control, expression, message) {
        this.control = control;
        this.expression = expression;
        this.message = message;
        this.ctrl1;

        this.validate = function() {
            this.ctrl1 = Scl.getElement(this.control);

            if (this.ctrl1 == null) {
                throw new Scl.Exception(this.control + " is undefined");
            }

            var value = this.ctrl1.value;

            if (value == null || value == '') {
                return true;
            }
            else {
                var reg = new RegExp(this.expression, "m");
                return (reg.test(value));
            }
        }
    };

    /**
    * CompareFieldsValidation validation
    */
    Scl.CompareFieldsValidation = function(control, control2, message) {
        this.control = control;
        this.control2 = control2;
        this.message = message;
        this.ctrl1;

        this.validate = function() {
            this.ctrl1 = Scl.getElement(this.control);

            if (this.ctrl1 == null) {
                throw new Scl.Exception(this.control + " is undefined");
            }

            var ctrl2 = Scl.getElement(this.control2);

            if (ctrl2 == null) {
                throw new Scl.Exception(this.control2 + " is undefined");
            }

            return this.ctrl1.value == ctrl2.value;
        }
    };

    /**
    * Requires the second field to have a value only if the first has a value also
    */
    Scl.ConditionalRequiredValidator = function(control, control2, value, message) {
        this.control = control;
        this.control2 = control2;
        this.value = value;
        this.message = message;
        this.ctrl1;

        this.validate = function() {
            this.ctrl1 = Scl.getElement(this.control);

            if (this.ctrl1 == null) {
                throw new Scl.Exception(this.control + " is undefined");
            }

            var ctrl2 = Scl.getElement(this.control2);

            if (ctrl2 == null) {
                throw new Scl.Exception(this.control2 + " is undefined");
            }

            // La validación es: si el valor de ctrl2 == value entonces ctrl1 
            // tiene que tener un valor.

            if (typeof ctrl2.checked != 'undefined') {
                // Si el control es un checkbox, en caso de que el valor sea 'on' es que 
                // tiene que estar marcado (el valor de un checkbox siempre da 'on', hay que comprobar
                // la propiedad checked.
                if (value == 'on') {
                    return !ctrl2.checked && this.ctrl1.value != '';
                }
                // En caso de que el valor sea distinto de on simplemente comprobar que no esta marcado.
                else {
                    return ctrl2.checked || this.ctrl1.value != '';
                }
            }
            else {
                return ctrl2.value != value || this.ctrl1.value != '';
            }
        }
    };

    /**
    * Forms validation
    */
    Scl.Validator = function() {
        var validators = new Array();

        this.addValidation = function(validation) {
            validators[validators.length] = validation;
        };

        /**
        * Call the validate method on all the validators and notify the user if fails.
        * @return Boolean
        */
        this.validate = function() {
            var invalidControls = new Array();
            var summary = "";
            var errorsFound = false;

            for (i = 0; i < validators.length; i++) {
                var validation = validators[i];

                if (!validation.validate()) {
                    if (errorsFound) {
                        summary += '\r\n- ' + validation.message;
                    }
                    else {
                        summary += '- ' + validation.message;
                        errorsFound = true;
                    }

                    // Check if the control is already invalid
                    if (!Scl.contains(invalidControls, validation.ctrl1)) {
                        invalidControls[invalidControls.length] = validation.ctrl1;
                    }

                    if (validation.ctrl1 != null) {
                        validation.ctrl1.style.border = '1px solid #ff0000';
                        validation.ctrl1.style.backgroundColor = '#ffeeee';
                    }
                }
                else {
                    // Check if the control is already invalid
                    if (!Scl.contains(invalidControls, validation.ctrl1)) {
                        if (validation.ctrl1 != null) {
                            // If the control has not been included as invalid, clear the error background
                            validation.ctrl1.style['backgroundColor'] = '';
                        }
                    }
                }
            }

            if (errorsFound) {
                alert(summary);
            }

            return !errorsFound;
        }
    };

})();