﻿
if (typeof(scroobius) == 'undefined') scroobius = {};
scroobius.forms = function () { this.init(); }

scroobius.forms.prototype = {
    fields: null,
    isValid: true,
    message: '',
    hasFocussed: true,

    init: function () {
        this.fields = $('div.field input, div.field select, div.field textarea');
        $('form').bind('submit', $.proxy(this.formSubmit, this));

        this.fields.bind('blur', $.proxy(this.validate, this));
        this.fields.filter('[type=checkbox]').bind('click', $.proxy(this.validate, this));
    },

    formSubmit: function () {
        this.isValid = true;
        this.message = '';
        this.hasFocussed = false;
        this.fields.each($.proxy(this.validate, this));

        if (this.message != '') alert(this.message);

        return this.isValid;
    },

    validate: function (e, field) {
        if (typeof (field) == 'undefined') field = e.currentTarget;
        var j = $(field);
        j.parents('.field').removeClass('fdet-invalid');
        var type = j.attr('type'), tag = j.attr('tagName').toLowerCase();
        if (tag != 'input') type = tag;
        if (type == 'textarea') type = 'text';
        type = type.toLowerCase();
        if (this['validate_' + type]) this['validate_' + type](j);
    },

    validate_text: function (element) {
        var valid = true, doCheck = true;
        if (element.is('.fdet_mand') && element.val() == '') {
            this.notValid('Please enter a value for "#N"', element);
            valid = false;
        } else if (!element.is('.fdet_mand') && element.val() == '') {
            doCheck = false;
        }
        if (valid && doCheck) {
            var type = '', m = element.attr('class').match(/\bfdettype_[a-zA-Z0-0]+\b/i);
            if (m) {
                type = m[0].substring(9);
                if (this['validate_' + type]) this['validate_' + type](element);
            }
        }
    },

    validate_checkbox: function (element) {
        if (element.is('.fdet_mand') && element.attr('checked') == false) {
            this.notValid('You must check the box "#N" before you can continue', element);
        }
    },

    validate_select: function (element) {
        if (element.is('.fdet_mand') && element.attr('selectedIndex') == 0) {
            this.notValid('Please select a value from the list "#N"', element);
        }
    },

    validate_email: function (element) {
        var valid = /^.+@.+\..+$/.test(element.val());
        if (!valid) this.notValid('"#N" must be a valid email address', element);
    },

    validate_postcode: function (element) {
        var valid = /^[ ]*[a-z]{1,2}[0-9]{1,2}[a-z]{0,1}[ ]{0,}[0-9]{1,2}[a-z]{2}[ ]*$/i.test(element.val());
        if (!valid) this.notValid('"#N" must be a valid UK postcode', element);
    },

    validate_number: function (element) {
        var valid = /^[ ]*[0-9]+[\.]?[0-9]*[ ]*$/.test(element.val());
        if (!valid) this.notValid('"#N" must be a number', element);
    },

    notValid: function (message, element) {
        this.message += message.replace(/#N/, element.attr('name').replace(/_/g, ' ')) + '\n';
        this.isValid = false;
        element.parents('.field').addClass('fdet-invalid');
        if (!this.hasFocussed) {
            this.hasFocussed = true;
            element.focus();
        }
    }
}

$(function () {
    if (window.top == window) {
        new scroobius.forms();
    }
});
