var Site =
{
	validateForm: function(form, onErrorCallback, onHideCallback)
	{
//		Formcheck.setBirthdateHidden('birthdate-day', 'birthdate-month', 'birthdate-year', 'birthdate', '-');

		var form = form || document.forms[0];
		var success = (!window.Formcheck || Formcheck.checkForm(form));

		if (!success && window.Formcheck_Text)
		{
			var messages = [];

			for (var i = 0; i < Formcheck.invalid.length; i++)
			{
				messages.push(Formcheck_Text.get(Formcheck.invalid[i].name));
			}

			if (typeof onErrorCallback == 'function')
			{
				onErrorCallback();
			}

			$('#info-message-content').html('<p><strong>'+Formcheck_Text.get('error')+'</strong></p><p>'+messages.join('<br />')+'</p>');
			$('#info-message').lightbox_me({
				centered: true
			});
		}

		return success;
	},

	submitForm: function(form)
	{
		if (Site.validateForm(form))
		{
			form.submit();
		}
	},

    ajaxSubmit: function(form, messageTitle, message, onSuccess)
    {
        if (Site.validateForm(form))
        {
            $.post(
                form.action,
                $(form).serialize(),
                function(data)
                {
                    if (typeof onSuccess == 'function')
                    {
                        onSuccess();
                    }

                    if (messageTitle && message)
                    {
                        $('#info-message-content').html('<p><strong>'+messageTitle+'</strong></p><p>'+message+'</p>');
                        $('#info-message').lightbox_me({
                            centered: true
                        });
                    }
                }
            );
        }

        return false;
    },

	blur: function(element)
	{
        var validate = true;

        if(element.name.indexOf('birthdate') != -1)
        {
            element = $('[name*=birthdate]');
            element.name = 'birthdate';

            element.each(function()
            {
                if(!$(this).val())
                {
                   validate = false;
                }
            });
        }

        if(!validate)
        {
            return false;
        }

		$.post(basepath+'/index/ajax', $(element).serialize(), function(response) {
			if (true !== response) {
                $.each(response[element.name], function(key, value) {
                    $(element).addClass('failed');
                });
			}
            else
            {
                $(element).removeClass('failed');
            }
		});
	}
}

