/**
 * Check all categories below the selected one on the email alerts page
 *
 * @param HTMLElement element The element checked
 */
function recallsCheckChildCategories (element) {
    // check or uncheck all the children of the supplied element
    $$('#categoryChildrenOf' + element.value + ' input').each(
        function(child) {
            child.checked = element.checked;
        }
    );
}
    
function recallsCanSubmitEmailAlerts (element) {
    // enable or disable the submit button based on whether categories are checked
    var myForm = $(element).form;
    var checkboxes = $(myForm).getInputs('checkbox');
    var checked = false;
    checkboxes.each(
        function(checkbox) {
            if (checkbox.checked) {
                checked = true;
            }
        }
    )
    if (!checked) {
        alert('You have not checked any categories. Click on the checkbox next to the categories you wish to subscribe to, and then click the Subscribe button, or click the back button in your browser to leave this page.');
    }
    return checked;
}

/**
 * Enable the "final progress report" controls if the passed in element is checked.
 *
 * Rather than listing a huge bunch of ids to enable disable, we just iterate through all the
 * inputs in the form, enabling/disabling the ones after the passed in element.
 *
 * @param HTMLElement element The element used to determine whether to enable or disable the fields.
 */
function recallsProgressReportEnableFinalControls (element) {
    var afterCheckbox = false;
    var inputs = $$('ol#recallProgressReportFinalInformation input', 'ol#recallProgressReportFinalInformation textarea');
    for (i = 0; i < inputs.length; i++) {
        if ((element.type == 'checkbox' && element.checked) || element.value == 'true') {
            sytadelEnableInputField(inputs[i], true);
        } else {
            sytadelEnableInputField(inputs[i], false);
        }
    }
    
    if ((element.type == 'checkbox' && !element.checked) || element.value == 'false') {
        // set these back to unchecked, otherwise we could get an inconsistant state when they uncheck
        // the final progress report checkbox.
        $('recallProgressReport.returnedProductActions.disposedOf').setValue(false);
        $('recallProgressReport.returnedProductActions.defectRectified').setValue(false);
    }
    
    recallsProgressReportUpdateEvidenceInputs('disposedOf');
    recallsProgressReportUpdateEvidenceInputs('defectRectified');
}

/**
 * DOM event handler for updates to the disposal evidence repeater on the progress report form.
 *
 * Passes the count of items in the repeater through to recallsProgressReportUpdateEvidenceInputs().
 *
 * @param DOMEvent event DOM event triggered by the repeater changing, contains all the repeated item DOM nodes
 */
function recallsProgressReportUpdateDisposalEvidence (event) {
    recallsProgressReportUpdateEvidenceInputs('disposedOf', event.memo.items.length);
}

/**
 * DOM event handler for updates to the defect rectified evidence repeater on the progress report form.
 *
 * Passes the count of items in the repeater through to recallsProgressReportUpdateEvidenceInputs().
 *
 * @param DOMEvent event DOM event triggered by the repeater changing, contains all the repeated item DOM nodes
 */
function recallsProgressReportUpdateDefectRectifiedEvidence (event) {
    recallsProgressReportUpdateEvidenceInputs('defectRectified', event.memo.items.length);
}


/**
 * Enables the nominated representative contact details input fields in the progress reporting form based on the value of the element.
 *
 * @param HTMLElement element the input to use to determine whether to enable/disable the contact fields
 */
function recallsEnableNominatedRepresentativeContactDetails (element) {
    element = $(element);
    var inputs = $$('div#recallNominatedRepresentativeContactDetails input, div#recallNominatedRepresentativeContactDetails select');
    if ((element.type == 'checkbox' && element.checked) || element.value == 'true') {
        inputs.each(function(item){
            sytadelEnableInputField(item, true);
        });
    } else {
        inputs.each(function(item){
            sytadelEnableInputField(item, false);
        });
    }
}

/**
 * Enables or disables disposal evidence and description fields on the progress report form.
 *
 * This is called with or without the count of items in the relevant repeater.
 * Without, we just enable or disable all the inputs based on the value of the disposedOf checkbox.
 * With, we also show or hide the mandatory field indicator on the description if the count is > 0.
 *
 * @param string fieldSetName either 'disposedOf or 'refectRectified', indicates which set of inputs we need to update
 * @param int repeaterCount the item count in the relevant repeater
 */
function recallsProgressReportUpdateEvidenceInputs (fieldSetName, repeaterCount) {
    if (fieldSetName == 'disposedOf') {
        var checkbox = $('recallProgressReport.returnedProductActions.disposedOf');
        var description = $('recallProgressReport.disposalDescription');
        var evidenceInputs = $$('div#recallProgressReportDisposalEvidence input');
    } else {
        var checkbox = $('recallProgressReport.returnedProductActions.defectRectified');
        var description = $('recallProgressReport.defectRectifiedDescription');
        var evidenceInputs = $$('div#recallProgressReportDefectRectifiedEvidence input');
    }
    if (checkbox.checked) {
        if (typeof repeaterCount != 'undefined') {
            if (repeaterCount > 0) {
                description.siblings().each(function(item){
                    if (item.className == 'sytadelMandatoryField') {
                        item.hide();
                    }
                });
            } else {
                description.siblings().each(function(item){
                    if (item.className == 'sytadelMandatoryField') {
                        item.show();
                    }
                });
            }
        }
        sytadelEnableInputField(description, true);
        evidenceInputs.each(function(item){
            sytadelEnableInputField(item, true);
        });
    } else {
        sytadelEnableInputField(description, false);
        evidenceInputs.each(function(item){
            sytadelEnableInputField(item, false);
        });
    }
}

/**
 * Shows or hides the mandatory field indicator for the supply chain notification description field on the progress report form.
 * 
 * This is called by an event observer on the supply chain evidence repeater.
 *
 * @param DOMEvent event DOM event triggered by the repeater changing, contains all the repeated item DOM nodes
 */
function recallsProgressReportUpdateSupplyChainNotification (event) {
    if (event.memo.items.length > 0) {
        $('recallProgressReport.supplyChainNotificationDescription').siblings().each(function(item){
            if (item.className == 'sytadelMandatoryField') {
                item.hide();
            }
        });
    } else {
        $('recallProgressReport.supplyChainNotificationDescription').siblings().each(function(item){
            if (item.className == 'sytadelMandatoryField') {
                item.show();
            }
        });
    }
}

/**
 * Enable the "other text" field for advertising mediums if the passed in element is checked.
 *
 * @param HTMLElement element If this checkbox is checked, enable the field, otherwise, disable it.
 */
function recallsAdvertisingMediumsEnableOtherText (element) {
    element = $(element);
    // grab the other text field by moving around the DOM
    var otherTextField = element.ancestors()[3].childElements()[2].childElements()[1];
    if (element.checked) {
        sytadelEnableInputField(otherTextField, true);
    } else {
        sytadelEnableInputField(otherTextField, false);
    }
}

/**
 * Enable or disable the manufacturer contact details if the passed in element is checked.
 *
 * @param mixed eventOrElement either an event when called by prototype observe() or an element when called directly
 */
function recallsEnableManufacturerDetails (eventOrElement) {
    if (typeof eventOrElement.element != 'undefined') {
        // if this is an event, "this" is the observed element
        element = this;
    } else {
        // otherwise they have passed in an element
        element = $(eventOrElement);
    }
    
    // grab the detail fields
    var manufacturerName = $('/recall/manufacturerName');
    var manufacturerContactDetails = $('/recall/manufacturerContactDetails');
    
    if (element.checked) {
        sytadelEnableInputField(manufacturerName, false);
        sytadelEnableInputField(manufacturerContactDetails, false);
    } else {
        sytadelEnableInputField(manufacturerName, true);
        sytadelEnableInputField(manufacturerContactDetails, true);
    }
}
