﻿/*!
* CodeForce LLC
* jQuery disableonclick plugin
* Version 2.33 (29-MAR-2010)
* @requires jQuery v1.2.3 or later
*
* Copyright (c) 2007-2008 M. Alsup
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* This plugin can disable and apply a disable class to any type of html control that can support a click event from it. 
* It has built in support for micorsoft validation testing, showing a waitui and clearing watermarks on click.
* 
*   OPTIONS available for overriding
*   disableAll - allows all controls with the disableonclick= true to be disabled regardless of group or location
*   clearWatermarksCallout - allows for a function callout to a clearwatermarks method. to be used with other jquery watermark plugins
*   waituiCallout - allows for a function callout to a waitui method or event which can do what ever you like between the client click and postback
*   activeClass - active class used on control if any
*   inactiveClass: - inactive class applied on disable
*   resetTimer - time in milliseconds i wait to reset the form controls if validation fails
*   useDisableAttribute - flag to determine if i should also apply the  disable attribute or just the classes
*   groupName - you can apply groupname based on a container level call to ensure all controls with a disableonclick are fired together. optionally you can assign the
*   groupname on the control itset as a custom attribute. which will allow for more specific control of the groupings
*
* Example Usage
    <script type="text/javascript">
        $(document).ready(function () {
            $(".ff").disableonclick({waituiCallout:showWatUI, clearWatermarksCallout: clearMarks, );            
        });
        function showWatUI() {		
        }
        function clearMarks() {
        }
    </script>
    <div class="ff">
        <asp:Button ID="Button1" runat="server" CssClass="testbtn" disableonclick="true" groupname="g"
        OnClientClick=" return false;" Text="Button" CausesValidation="true"  ValidationGroup="ff	/>
			
        <asp:Button ID="Button1" runat="server" CssClass="testbtn" disableonclick="true" groupname="g"
        OnClientClick=" return false;" Text="Button" CausesValidation="true"  ValidationGroup="ff" />
			
        <asp:TextBox ID="TextBox1" ValidationGroup="ff" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"  ValidationGroup="ff" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    </div>
*
*

*/

(function ($) {

    if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
        alert('disableonclick requires jQuery v1.2.3 or later!  You are using v' + $.fn.jquery);
        return;
    }

    $.fn.canceldisableonclick = function (obj, options) { _enableControl(obj, options); }

    // plugin definition
    $.fn.disableonclick = function (options) {
        // Extend our default options with those provided.
        // Note that the first arg to extend is an empty object -
        // this is to keep from overriding our "defaults" object.
        var opts = $.extend({}, $.fn.disableonclick.defaults, options);
        // Our plugin implementation code goes here.

        return this.each(function () {
            $this = $(this);
            // build element specific options
            disableControls($this, opts);
        });
    };

    // plugin defaults - added as a property on our plugin function
    $.fn.disableonclick.defaults = {
        disableAll: false,
        clearWatermarksCallout: null,
        waituiCallout: null,
        activeClass: 'cntrlactive',
        inactiveClass: 'cntrlinactive',
        resetTimer: 1000,
        useDisableAttribute: true,
        groupName: null
    };

    function disableControls(obj, options) {
        options = $.extend({}, $.fn.disableonclick.defaults, options || {});

        $(obj + "[disableonclick='true']" || obj + " > *[disableonclick='true']").each(function (i) {
            var $obj = $(this);

            if (options.groupName != null) {
                $obj.attr("groupname", options.groupName);
            }

            $obj.click(function () {

                if (options.clearWatermarksCallout != null && options.clearWatermarksCallout != undefined
                        && typeof options.clearWatermarksCallout != undefined && typeof options.clearWatermarksCallout == "function") {
                    options.clearWatermarksCallout();
                }
                if ((typeof Page_ClientValidate == "function" && typeof Page_IsValid != 'undefined' && Page_IsValid) || typeof Page_ClientValidate == "undefined") {
                    _tryWaitUiCallout(options);

                    if (options.disableAll) {
                        $("*[disableonclick='true']").each(function (i) {
                            _disableControl($(this), options);
                        });
                    } else {
                        var group = $(this).attr("groupname");
                        if (group != null && group != undefined && group.length > 0) {
                            //get all elements with that groupname
                            $("[disableonclick='true'][groupname='" + group + "']").each(function (i) {
                                _disableControl(this, options);
                            });
                        } else {
                            //just disable this element
                            _disableControl(this, options);
                        }
                    }

                    //debugger;
                    setTimeout(function () {
                        _checkForReset(this, options);
                    }, options.resetTimer);
                }
            });
        });


    };

    function _tryWaitUiCallout(options) {
        if (options.waituiCallout != null && options.waituiCallout != undefined
                        && typeof options.waituiCallout != undefined && typeof options.waituiCallout == "function") {
            options.waituiCallout();
        }
    }

    function _disableControl(obj, options) {
        options = $.extend({}, $.fn.disableonclick.defaults, options || {});
        var $obj = $(obj);
        $obj.addClass(options.inactiveClass);
        $obj.removeClass(options.activeClass);
        if (options.useDisableAttribute) {
            $obj.attr("disabled", "true");
        }
    };

    function _enableControl(obj, options) {
        options = $.extend({}, $.fn.disableonclick.defaults, options || {});
        var $obj = $(obj);
        $obj.addClass(options.inactiveClass);
        $obj.removeClass(options.activeClass);
        if (options.useDisableAttribute) {
            $obj.removeAttr("disabled");
        }
    };

    function _checkForReset(obj, options) {
        options = $.extend({}, $.fn.disableonclick.defaults, options || {});

        if ((typeof Page_ClientValidate == "undefined") || (typeof Page_IsValid != 'undefined' && !Page_IsValid)) {

            //this will retrieve the table which contains all sub elements
            $(obj + "[disableonclick='true'][class*='" + options.inactiveClass + "']" || obj + " > [disableonclick='true'][class*='" + options.inactiveClass + "']").each(function (i) {
                $(this).addClass(options.activeClass);
                $(this).removeClass(options.inactiveClass);
                if (options.useDisableAttribute) {
                    $(this).removeAttr("disabled");
                }
            });
            return false;
        }
        return true;
    };


})(jQuery);
