function CustomFormAssetType(assetid)
{
    this.assetid = assetid;

}

CustomFormAssetType.prototype = {

    attachValidation: function(options)
    {
        for (var qid in options) {
            var tmp = {};
            if (options[qid].hasOwnProperty('rules') === true) {
                for (var type in options[qid].rules) {
                    if (type === 'len' || type === 'email') {
                        if (options[qid].type === 'text') {
                            tmp[type] = options[qid].rules[type];
                        }
                    } else {
                        var qnType = options[qid].type;
                        this.attach(qid, type, qnType, options[qid].rules[type]);
                    }
                }
            }

            if (tmp !== {}) {
                this.attachTextValidation(qid, tmp);
            }
        }

    },

    attachTextValidation: function(qid, options)
    {
        var self  = this;
        var id    = 'form' + this.assetid + '-q' + qid;
        var input = document.getElementById(id);
        if (typeof input === 'undefined' || input === null) {
            return;
        }

        if (options.hasOwnProperty('len') === true && options.hasOwnProperty('email') === true) {
            // Has length and email rule.
            input.onblur = function(evt) {
                var ret = self.lenValidation(id, this.value, options.len);
                if (ret === true) {
                    self.emailValidation(id, this.value, options.email);
                }
            };
        } else if (options.hasOwnProperty('len') === true) {
            input.onblur = function(evt) {
                self.lenValidation(id, this.value, options.len);
            };
        } else if (options.hasOwnProperty('email') === true) {
            input.onblur = function(evt) {
                self.emailValidation(id, this.value, options.email);
            };
        }

    },

    attach: function(qid, type, qnType, options)
    {
        var self  = this;
        var id    = 'form' + this.assetid + '-q' + qid;
        var input = null;

        if (type === 'number') {
            if (qnType !== 'number') {
                return;
            }

            input = document.getElementById(id);
            if (typeof input === 'undefined' || input === null) {
                return;
            }

            input.onblur = function(evt) {
                self.numberValidation(id, this.value, options);
            };
        } else if (type === 'selcount') {
            if (qnType === 'dropdown') {
                // Multi-dropdown.
                input = document.getElementById(id);
                if (typeof input === 'undefined' || input === null) {
                    return;
                }

                input.onblur = function(evt) {
                    var len   = this.length;
                    var count = 0;
                    for (var i = 0; i < len; i++) {
                        if (this.options[i].selected === true) {
                            count++;
                        }
                    }

                    self.dropdownCountValidation(id, count, options);
                };
            } else if (qnType === 'options') {
                // Multi-checkbox.
                var index    = 0;
                var count    = 0;
                var checkbox = document.getElementById(id + '-' + index);
                while (typeof checkbox !== 'undefined' && checkbox !== null) {
                    checkbox.onblur = function(evt) {
                        self.optionsCountValidation(id, options);
                    };

                    index++;
                    checkbox = document.getElementById(id + '-' + index);
                }
            }//end if
        } else if (qnType === 'passwordchange') {
            input = document.getElementById(id);
            if (typeof input === 'undefined' || input === null) {
                return;
            }

            input.onkeyup = function(evt) {
                self.passwordValidation(id, this.value);
            };

            confirm = document.getElementById(id + '-confirm');
            if (typeof confirm === 'undefined' || confirm === null) {
                return;
            }

            confirm.onkeyup = function(evt) {
                self.passwordMatch(id, this.value);
            };
        }//end if

    },

    getOption: function(options, key)
    {
        if (options.hasOwnProperty(key) === true) {
            return options[key];
        } else {
            return null;
        }

    },

    lenValidation: function(id, value, options)
    {
        var error       = document.getElementById(id + '-error');
        error.innerHTML = '';
        var ret         = false;

        error.style.display = 'none';
        if (value === '') {
            return true;
        }

        var len   = this.getOption(options, 'len');
        var lenOp = this.getOption(options, 'lenOp');
        if (len !== null && len !== '' && lenOp !== null) {
            var lenV = new RuleTypeLengthWidgetType(id, value, lenOp, len);
            ret      = lenV.validate();
            if (ret === false) {
                var lenMsg      = this.getOption(options, 'msg');
                error.innerHTML = lenMsg;

                error.style.display = 'block';
            }

            return ret;
        }

    },

    emailValidation: function(id, value, options)
    {
        var error       = document.getElementById(id + '-error');
        error.innerHTML = '';
        var ret         = false;

        error.style.display = 'none';
        if (value === '') {
            return true;
        }

        var emailV = new RuleTypeEmailWidgetType(id, value);
        ret        = emailV.validate();
        if (ret === false) {
            var emailMsg    = this.getOption(options, 'msg');
            error.innerHTML = emailMsg;

            error.style.display = 'block';
        }

        return ret;

    },

    numberValidation: function(id, value, options)
    {
        var msg         = this.getOption(options, 'msg');
        var error       = document.getElementById(id + '-error');
        error.innerHTML = '';
        var ret         = false;

        error.style.display = 'none';
        if (value === '') {
            return true;
        }

        var isNum = new RuleTypeNumberWidgetType(id, value);
        ret       = isNum.validate();
        if (ret === false) {
            error.innerHTML     = msg;
            error.style.display = 'block';
            return false;
        }

        var min       = this.getOption(options, 'min');
        var max       = this.getOption(options, 'max');
        var magnitude = new RuleTypeMagnitudeWidgetType(id, value, min, max);
        ret           = magnitude.validate();
        if (ret === false) {
            error.innerHTML     = msg;
            error.style.display = 'block';
            return false;
        }

        var intOnly = this.getOption(options, 'intOnly');
        if (intOnly === true) {
            var rule = new RuleTypeIntegerWidgetType(id, value, options.intOnly);
            ret      = rule.validate();
            if (ret === false) {
                error.innerHTML     = msg;
                error.style.display = 'block';
                return false;
            }
        }

        return true;

    },

    dropdownCountValidation: function(id, value, options)
    {
        var error       = document.getElementById(id + '-error');
        error.innerHTML = '';
        var ret         = false;

        error.style.display = 'none';
        if (value === '') {
            return;
        }

        var count   = this.getOption(options, 'countVal');
        var countOp = this.getOption(options, 'countOp');
        if (count !== null && count !== '' && countOp !== null) {
            var countV = new RuleTypeLengthWidgetType(id, value, countOp, count);
            countV.setPlain(true);
            ret = countV.validate();
            if (ret === false) {
                var msg         = this.getOption(options, 'msg');
                error.innerHTML = msg;

                error.style.display = 'block';
                return;
            }
        }

    },

    // Checkbox gets check count on the fly.
    optionsCountValidation: function(id, options)
    {
        var error       = document.getElementById(id + '-error');
        error.innerHTML = '';
        var ret         = false;

        error.style.display = 'none';

        var index    = 0;
        var x        = 0;
        var checkbox = document.getElementById(id + '-' + index);
        while (typeof checkbox !== 'undefined' && checkbox !== null) {
            if (checkbox.checked === true) {
                x++;
            }

            index++;
            checkbox = document.getElementById(id + '-' + index);
        }

        var count   = this.getOption(options, 'countVal');
        var countOp = this.getOption(options, 'countOp');
        if (count !== null && count !== '' && countOp !== null) {
            var countV = new RuleTypeLengthWidgetType(id, x, countOp, count);
            countV.setPlain(true);
            ret = countV.validate();
            if (ret === false) {
                var countMsg    = this.getOption(options, 'msg');
                error.innerHTML = countMsg;

                error.style.display = 'block';
                return;
            }
        }

    },

    passwordValidation: function(id, value)
    {
        var passwordV = new RuleTypePasswordWidgetType(id, value);
        passwordV.update();

    },

    passwordMatch: function(id, value)
    {
        var password = document.getElementById(id).value;
        var confirm  = document.getElementById(id + '-confirm').value;
        var msg      = document.getElementById(id + '-matched');
        if (password !== confirm) {
            msg.innerHTML = 'Not Matched';
        } else {
            msg.innerHTML = 'Matched';
        }

    }

};
