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);
                };
				return false;
        },

        createButtons: function(form, classname) {

                // Button 1 - check all
                var chkPrompt1 = document.createTextNode("Select: ");
				var chkPrompt2 = document.createTextNode("Select: ");
				var chkText = "All";
				var unChkText = "None";
				var spacer1 = document.createTextNode(" | ");
				var spacer2 = document.createTextNode(" | ");

				var but1 = document.createElement('a');
                but1.className = "fd-but checkall-" + classname;
                but1.onclick = checkBoxMaster.checkOrUncheckAll;
				but1.setAttribute("href","#");
				but1.appendChild(document.createTextNode(chkText));
				
				var but2 = document.createElement('a');
                but2.className = "fd-but uncheckall-" + classname;
                but2.onclick = checkBoxMaster.checkOrUncheckAll;
				but2.setAttribute("href","#");
				but2.appendChild(document.createTextNode(unChkText));
				
				var but3 = document.createElement('a');
                but3.className = "fd-but checkall-" + classname;
                but3.onclick = checkBoxMaster.checkOrUncheckAll;
				but3.setAttribute("href","#");
				but3.appendChild(document.createTextNode(chkText));
				
				var but4 = document.createElement('a');
                but4.className = "fd-but uncheckall-" + classname;
                but4.onclick = checkBoxMaster.checkOrUncheckAll;
				but4.setAttribute("href","#");
				but4.appendChild(document.createTextNode(unChkText));
				
				var selhead = document.getElementById("sel1");
				var selfoot = document.getElementById("sel2");
				
                // DOM inject
				selhead.appendChild(chkPrompt1);
                selhead.appendChild(but1);
				selhead.appendChild(spacer1);
               	selhead.appendChild(but2);
				
				selfoot.appendChild(chkPrompt2);
                selfoot.appendChild(but3);
				selfoot.appendChild(spacer2);
               	selfoot.appendChild(but4); 
	
        },

        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;
addLoadEvent(checkBoxMaster.init);