﻿// ga_validation makes my life easier for doing Ticket #45 http://ms-004-svn02/trac/BrightHouse/ticket/45
// ga_validation requires the small Library I have written previously (validatorXtAPI)
// params:
//  Required:
//      submitButton: the button elem that triggers validation and invalid fields tracking when clicked
//      validationGroup: the validation group to validate
//      formName: the name of the form
//      labelFallback: callback function(fieldElem) for the label just incase no label is found, this must return a string
//      onPostback: callback function() handler to attach to endRequest postback
//      onValid: callback function() callback when all fields are valid
var ga_validation = function (submitButton, validationGroup, formName, labelFallback, onPostback, onValid) {
    if (submitButton && validationGroup && formName) {
        //lets do our magic
        var gaValidation = function () {
            var v = validatorXtAPI;
            v.invalidFields(validationGroup).each(
			    function () {
			        var label = (v.labelText(v.label(this)) == "" || !v.labelText(v.label(this))) ? undefined : v.labelText(v.label(this)); //label name
			        if (!label && labelFallback) { label = labelFallback(); } //The fallback callback function for label
			        if (!label) { label = $(this).attr('id'); } //Just get the ID of the field
			        _gaq.push(['_trackEvent', 'Form Validation Errors', formName, label]);
			        //console.log(label);
			    }
		    );
            //console.log('validated');
        };

        this.init = function () {
            $(function () {
                //execute on postback
                onEndRequest = function (sender, e) {
                    // Update complete...
                    $(document.body).css('cursor', 'default');
                    if (e.get_error()) {
                        // Something went wrong
                        if (e.get_error()) {
                            // Show an error message
                            alert(e.get_error().message);
                            e.set_errorHandled(true);
                        }
                    }
                    else {
                        // Run post update script
                        gaValidation();
                        if (onPostback)
                            onPostback();
                    }
                };

                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest);

                gaValidation(); //execute validation on pageload
                $(submitButton).click(function (e) {
                    if (Page_ClientValidate(validationGroup) && onValid) {
                        onValid(); //onValid callback
                    }
                    gaValidation();
                    //console.log('clicked');
                });

                //console.log("initialised");
            });
        }
    };
    this.init(); //initialise..
}
