var checkBoxMaster = {
        butCnt: 0,

        checkOrUncheckAll: function() {

                // Step 1: Get the checkbox className from the button className
                var cname = this.className;
                
                cname = cname.replace(/fd-but/,"");
                cname = cname.replace(/uncheckall-/,"");
                cname = cname.replace(/checkall-/,"");
                cname = cname.replace(' ','');

                // Step 2: Find the form element
                var form = this.parentNode;
                while(form.nodeName.toLowerCase() != "form") {
                        if(!form.parentNode) break;
                        form = form.parentNode;
                };


                if(form.nodeName.toLowerCase() == 'form') {
                        var checkboxs = form.getElementsByTagName('input');
                        // Step 3: Loop through all the checkboxs and check/uncheck the ones whose className matches the classname located in Step 1.
                        for(var i = 0, inp; inp = checkboxs[i]; i++) if(inp.type.toLowerCase() == 'checkbox' && inp.className.search(cname) != -1) inp.checked = (this.className.search('uncheckall') == -1);
                };
        },

        createButtons: function(form, classname) {

                // Get all of the forms child divs
                var placeholder = form.getElementsByTagName('div');
                var elem = form;

                // Try to find the button placeholder div
                for(var i = 0, ph; ph = placeholder[i]; i++) {
                        if(ph.className && ph.className.search('button-placeholder-'+classname) != -1) {
                                elem = ph;
                                break;
                        };
                };

                // Button 1 - check all
                var but1 = document.createElement('input');
                but1.type = "button";
                but1.className = "fd-but checkall-" + classname;
                but1.name = "button" + checkBoxMaster.butCnt++;
                but1.value = "Check All";
                but1.onclick = checkBoxMaster.checkOrUncheckAll;

                // Button 2 - uncheck all
                var but2 = document.createElement('input');
                but2.type = "button";
                but2.className = "fd-but uncheckall-" + classname;
                but2.name = "button" + checkBoxMaster.butCnt++;
                but2.value = "Uncheck All";
                but2.onclick = checkBoxMaster.checkOrUncheckAll;

                // DOM inject
                elem.appendChild(but1);
                elem.appendChild(but2);
        },

        init: function() {
                // Get all the forms
                var forms = document.getElementsByTagName('form');

                // Loop through the forms
                for(var i = 0, form; form = forms[i]; i++) {

                        // Make sure the form has at least one required classname
                        if(!form.className || form.className.search(/fdCheckbox-[^\s]+/) == -1) continue;

                        // Get all child input tags
                        var inplist = form.getElementsByTagName('input');

                        // Create an array of relevant classNames
                        var cboxnames = form.className.match(/fdCheckbox-[^\s]+/g);

                        // Loop through the classname array
                        for(var k = 0, cname; cname = cboxnames[k]; k++) {

                                // Get the name of the checkbox group by removing the string "fdCheckbox-" from the current classname
                                cname = cname.replace("fdCheckbox-","");

                                // Initiate the checkbox counter
                                var cbox = 0;

                                // Loop through all the inputs
                                for(var j = 0, inp; inp = inplist[j]; j++) {
                                        // If the input is of type checkbox and has the correct name and is not disabled then increment the counter
                                        if(inp.type=='checkbox' && inp.disabled == false && inp.className.search(cname) != -1) cbox++;
                                }
                                // If two or more checkboxs have been located then create the buttons (it would be daft to create the buttons for a single checkbox)
                                if(cbox > 1) checkBoxMaster.createButtons(form, cname);
                        }
                };
        }
};

// You know the window.onload score.. use the addEvent method of your choice..
window.onload = checkBoxMaster.init;