$(document).ready( function() {
    if ($('#trial_form').length > 0)
    {
        var validateParams = {
            submitHandler: function(form) {
                form.submit();
            },
            rules: {
                product: "required",
                name: "required",
                email: {
                    required: true,
                    email: true
                },
                private_key: "required"
            },

            messages: {
                product: "<strong>Продукт: </strong>" + Messages.GenerateFreeSerial.ErrorProduct,
                name: "<strong>Имя: </strong>" + Messages.GenerateFreeSerial.ErrorName,
                email: "<strong>Email: </strong>" +  Messages.GenerateFreeSerial.ErrorEmail,
                private_key: Messages.GenerateFreeSerial.ErrorPrivateKey
            }
        };

        var TrialFormValidatorSettings = generateSettings( validateParams );
        var buttonDecorator = new FormSubmitButtonDecorator($("#trial_form"));
        buttonDecorator.initByValidateParams(validateParams);
        buttonDecorator.activateButtonDecorator();

        $('#trial_form').submit(function(){
            $(this).unbind()
                   .validate( TrialFormValidatorSettings );
            return $(this).valid();
        });
    }
    else
    {
	    trackTrialEvent();
    }
});


function trackTrialEvent()
{
    $("input[id^='trackTrial']").each(function(){
        var reg = /^trackTrial_(.+)_(.+)$/;
        var matches = reg.exec( $(this).attr('id') );
        if ( matches != null )
        {
            var productCode = matches[1];
            var productVersion = matches[2];
            Stats.trackEvent('Trial', productCode, productVersion);
        }
     });
}

