jQuery.fn.center = function()
{
    this.css('position', 'absolute');
    this.css('top', ($(window).height() - this.height()) / 2 + $(window).scrollTop() + 'px');
    this.css('left', ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + 'px');

    return this;
}

var GJ = new function()
{
    this.cssDisplayError = 'displayError';

    this.getField = function(name)
    {
        this.jQueryIsLoaded();

        var field = $("input[ id $= '" + name + "' ]");

        if (field.length == 0)
        {
            alert(name + ' could not be found');
        }

        return field;
    };

    this.getSelect = function(name)
    {
        this.jQueryIsLoaded();

        var field = $("select[ id $= '" + name + "' ]");

        if (field.length == 0)
        {
            alert(name + ' could not be found');
        }

        return field;
    };

    this.fieldHasValue = function(name)
    {
        var isValid = false;
        var field = this.getField(name);

        if (this.hasValue(field.val()))
        {
            isValid = true;
        }

        this.fieldIsValid(isValid, field);

        return isValid;
    };

    this.selectHasValue = function(name, none)
    {
        var isValid = false;
        var field = this.getSelect(name);

        if (field.val() != none)
        {
            isValid = true;
        }

        this.fieldIsValid(isValid, field);

        return isValid;
    };

    this.fieldIsValid = function(isValid, field)
    {
        if (isValid)
        {
            isValid = true;

            field.removeClass(this.cssDisplayError);
        }
        else
        {
            isValid = false;

            field.addClass(this.cssDisplayError);
        }
    };

    this.fieldMatchesPattern = function(name, regex)
    {
        var field = this.getField(name);

        var isValid = regex.test(field.val());

        this.fieldIsValid(isValid, field);

        return isValid;
    };

    this.fieldHasEMail = function(name)
    {
        if (this.fieldHasValue(name))
        {
            var r = /^[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*$/i;

            return this.fieldMatchesPattern(name, r);
        }

        return false;
    };

    this.fieldHasPositiveInteger = function(name)
    {
        if (this.fieldHasValue(name))
        {
            var r = /^[0-9]+$/i;

            return this.fieldMatchesPattern(name, r);
        }

        return false;
    };

    this.fieldHasInteger = function(name)
    {
        if (this.fieldHasValue(name))
        {
            var r = /^(\-|\+)?[0-9]+$/i;

            return this.fieldMatchesPattern(name, r);
        }

        return false;
    };

    this.fieldHasPositiveDecimal = function(name)
    {
        if (this.fieldHasValue(name))
        {
            var r = /^[0-9]*(\.)?[0-9]+$/i;

            return this.fieldMatchesPattern(name, r);
        }

        return false;
    };

    this.fieldHasDecimal = function(name)
    {
        if (this.fieldHasValue(name))
        {
            var r = /^(\-|\+)?[0-9]*(\.)?[0-9]+$/i;

            return this.fieldMatchesPattern(name, r);
        }

        return false;
    };

    this.fieldHasFraction = function(name)
    {
        if (this.fieldHasValue(name))
        {
            if (this.fieldHasDecimal(name))
            {
                return true;
            }
            else
            {
                var r = /^(\-|\+)?(([0-9]+\-)?([0-9]+\/[0-9]+))$/i;

                return this.fieldMatchesPattern(name, r);
            }

            return isValid;
        }

        return false;
    };

    this.fieldHasTime = function(name)
    {
        if (this.fieldHasValue(name))
        {
            if (this.fieldHasDecimal(name))
            {
                return true;
            }
            else
            {
                var r = /^[0-9]{1,2}:[0-9]{1,2}$/i;

                return this.fieldMatchesPattern(name, r);
            }

            return isValid;
        }

        return false;
    };

    this.fieldHasDate = function(name)
    {
        if (this.fieldHasValue(name))
        {
            var r = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}$/i;

            return this.fieldMatchesPattern(name, r);
        }

        return false;
    };

    this.fieldHasHeight = function(name)
    {
        if (this.fieldHasPositiveDecimal(name))
        {
            return true;
        }

        var r = /^[0-9]+\-[0-9]+$/i;

        return this.fieldMatchesPattern(name, r);

        return isValid;
    };

    this.fieldHasWeightLiftingRoutinePattern = function(name)
    {
        var r = /^((,|\+)?\s*[0-9]+((\/|\@)[0-9]+(\%)?(\^|of1RM|\[1RM\])?)?((x|\*)[0-9]+)?)*$/i;

        return this.fieldMatchesPattern(name, r);
    };

    this.fieldHasWeightLiftingJournalPattern = function(name)
    {
        var r = /^((,|\+)?\s*[0-9]+((\/)[0-9]+)?((x|\*)[0-9]+)?)*$/i;

        return this.fieldMatchesPattern(name, r);
    };

    this.hasValue = function(value)
    {
        return value != '';
    };

    this.getTimeZone = function()
    {
        var curtime = new Date();
        var a = curtime.getTimezoneOffset();
        return -1 * a / 60;
    };

    this.jQueryIsLoaded = function()
    {
        if (jQuery)
        {
            return true;
        }

        alert('jQuery is NOT loaded');

        return false;
    };

    this.updateCheckBoxes = function(name, checked)
    {
        this.getField(name).each(function()
        {
            if (!this.disabled)
            {
                this.checked = checked;
            }
        });
    };

    this.countCheckBoxes = function(name, checked)
    {
        var count = 0;

        this.getField(name).each(function()
        {
            if (!this.disabled && this.checked)
            {
                count += 1;
            }
        });

        return count;
    };
};