/**
 * Gilmartinley Javascript
 * www.gilmartinley.co.uk
 * @copyright darren@cwebd.co.uk
 */


/**
 * Check/Uncheck Location boxes 
 *
 * @param object theElement 
 * @param string prefixId
 */         
function checkUncheckAll(theElement,prefixId) {
    var theForm = theElement.form, z = 0;
    
    // Ensure that selecting/deselecting is only done by the correct button
    
    if (theElement.name == prefixId + "[all-locations]") {
        
        for(z=0; z<theForm.length;z++){
            
            if(theForm[z].type == 'checkbox'){
                if (theForm[z].name == prefixId + "[location][]") {
                    theForm[z].checked = theElement.checked;
                }
            }
        }
    }
    
    else {
        
        // If the select all button is currently selected then make it deselect if
        // this item is unchecked
        
        if (document.getElementById) {
            
            var selectAll = document.getElementById("all-locations");
            
            if (!theElement.checked && selectAll.checked) {
                
                selectAll.checked = false;
            }
            
            else {
                
                // Have all the locations been checked, if so then ensure the select all is checked
                
                var notCheckCount = 0;
                
                for(z=0; z<theForm.length;z++){
                    
                    if(theForm[z].type == 'checkbox'){
                        if (theForm[z].name == prefixId + "[location][]") {
                            
                            // Is this checked
                            
                            if (!theForm[z].checked) {
                                notCheckCount++;
                            }
                        }
                    }
                }
                
                if (notCheckCount == 0) {
                    
                    selectAll.checked = true;
                    
                }
                
                else {
                    
                    selectAll.checked = false;
                }
                
            }
            
        }
    }
}
