/* <![CDATA[ */

// Upon the DOM being ready
$(document).ready(function() {
    
    // Handle multiple checkbox selection
    $('table > thead > tr > td > input[type=checkbox].multicheck').each(function() {
        $(this).click(function(e) {
            var checkall = false;
            if ($(this).is(':checked')) {
                checkall = true;
            }
            $(this).closest('table').find('tbody > tr > td > input[type=checkbox].multisubject').each(function() {
                this.checked = checkall;
            });
        });
    });
    
    // Handle form submission
    $('form.handledel').submit(function(e) {
        
        // Define variables
        var checkboxSelectionCount = 0;
        var retval = true;
        
        // Check if any checkboxes inside have been checked
        $(this).find('input[type=checkbox].delitem:checked').each(function() { checkboxSelectionCount++; })
        
        // If checkboxes are selected
        if (checkboxSelectionCount > 0) {
            
            // If confirmation returns false (customer would not like to continue)
            if (!confirm('You have ' + checkboxSelectionCount + ' item' + (checkboxSelectionCount>1?'s':'') + ' marked for deletion, do you wish to continue?')) {
                
                // Set retval to false (do not submit)
                retval = false;
                
            }
            
        }
        
        // If retval is false
        if (!retval) {
            e.preventDefault();
        }
        
    });
    
    try {
        $('input.datetime_field').datepicker({
            duration: '',
            showTime: true,
            constrainInput: false,
            dateFormat: 'dd/mm/yy',
            time24h: true,
            showOn: 'button',
            buttonImage: '/static/images/ui/calendar.png',
            buttonImageOnly: true
        });
    } catch(err) {}
    
    try {
        $('input.date_field').datepicker({
            duration: '',
            showTime: false,
            constrainInput: false,
            dateFormat: 'dd/mm/yy',
            showOn: 'button',
            buttonImage: '/static/images/ui/calendar.png',
            buttonImageOnly: true
        });
    } catch(err) {}
    
    // Handle form submission
    $('form.multiaction').submit(function(e) {
        
        // Define variables
        var deletionOptionSelectionCount = 0;
        var deletionCheckboxSelectionCount = 0;
        var delretval = true;
        
        // Check if any checkboxes inside have been checked
        $(this).find('input[type=checkbox].multisubject:checked').each(function() { deletionCheckboxSelectionCount++; })
        $(this).find('option.deletion:selected').each(function() { deletionOptionSelectionCount++; })
        
        // If deletion option is selected
        if (deletionOptionSelectionCount > 0 && deletionCheckboxSelectionCount > 0) {
            
            // If confirmation returns false (customer would not like to continue)
            if (!confirm('The action you have chosen will remove ' + deletionCheckboxSelectionCount + ' item' + (deletionCheckboxSelectionCount>1?'s':'') + ', do you wish to continue?')) {
                
                // Set retval to false (do not submit)
                delretval = false;
                
            }
            
        }
        
        // If retval is false
        if (!delretval) {
            e.preventDefault();
        }
        
    });
    
    // Create form reset links
    $('form').find('span.reset_form').each(function() {
        if ($(this).is('.disabled')) {
            $(this).html('clear changes or&nbsp;');
        } else {
            $(this).html('<a href="#">clear changes</a> or&nbsp');
        }
    });
    
    // Handle form reset links
    $('form span.reset_form').click(function(e) {
        $(this).closest('form').each(function() {
            this.reset();
        });
        if (typeof(messageLengthCounter) == "function") {
            messageLengthCounter();
        }
        e.preventDefault();
    });
    
    // Handle form button disabling
    $('form').each(function() {
        $(this).submit(function(e) {
            if (!e.isDefaultPrevented()) {
                $(this).find(':submit').attr('disabled', true);
            }
        })
    });
    
});

/* ]]> */