 var checkBoxMaster = 
 {
 	 butCnt: 0,

 	 checkOrUncheckAll: function() 
	 {
		// Step 1: Get the checkbox group name from the button classname
		var cname = this.className;
		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 name matches the name located in Step 1.
      		for(var i = 0, inp; inp = checkboxs[i]; i++) if(inp.type.toLowerCase() == 'checkbox' && inp.id.indexOf(cname) == 0) 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 = "checkall " + classname;
		but1.name = "button" + checkBoxMaster.butCnt++;
		but1.value = "Select All";
		but1.onclick = checkBoxMaster.checkOrUncheckAll;
	
		// Button 2 - uncheck all
		var but2 = document.createElement('input');
		but2.type = "button";
		but2.className = "uncheckall " + classname;
		but2.name = "button" + checkBoxMaster.butCnt++;
		but2.value = "Deselect All";
		but2.onclick = checkBoxMaster.checkOrUncheckAll;
	
		// DOM inject
		elem.appendChild(but1);
		//elem.appendChild('&nbsp; &nbsp;');
		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.id.indexOf(cname) == 0) 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);
			}
    	}; // end for 
  	}// end init
  
};



function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
//window.onload = checkBoxMaster.init;