/**
 * Provides conditional logic for the main contact form.
 *
 * https://nistwww.localhost/about-nist/contact-us
 * Sets the special hidden fields based on what the user selected in
 * the radio buttons.
 *
 * This logic used to be provided by the WebForms Conditional module
 * in D7. Currently there is no mature module solution. We decided
 * to nip it in the bud and set the values here.
 */
document.getElementById('edit-submit').onclick = function() {
  var nwfclo = {};
  // Name of the hidden fields.
  nwfclo.caseTypeFieldName = '00Nt0000000iEsE';
  nwfclo.respondingOfficeFieldName = '00Nt0000000iEsI';
  nwfclo.map = {
    'product_orders': {
      contactRouting: 'product_orders',
      respondingOffice: 'SRM',
      caseType: 'Product Orders (Standard Reference Materials)'
    },
    'computer_sec': {
      contactRouting: 'computer_sec',
      respondingOffice: 'ITL',
      caseType: 'Computer and IT Security'
    },
    'nist_pub': {
      contactRouting: 'nist_pub',
      respondingOffice: 'Publications',
      caseType: 'NIST Publications'
    },
    'cal_service': {
      contactRouting: 'cal_service',
      respondingOffice: 'Calibrations',
      caseType: 'Calibration Services'
    },
    'srd_orders': {
      contactRouting: 'srd_orders',
      respondingOffice: 'SRD',
      caseType: 'Technical Support'
    },
    'standards_info': {
      contactRouting: 'standards_info',
      respondingOffice: 'SCO',
      caseType: 'Standards Information'
    },
    'weights_measures': {
      contactRouting: 'weights_measures',
      respondingOffice: 'OWM',
      caseType: 'Weights and Measures'
    },
    'time_clocks': {
      contactRouting: 'time_clocks',
      respondingOffice: 'Time',
      caseType: 'Time and Clocks'
    },
    'other_questions': {
      contactRouting: 'other_questions',
      respondingOffice: 'PAO',
      caseType: 'Other'
    }
  };

  // Get the checked value.
  nwfclo.routingValue = document.querySelector('input[name="contact_routing"]:checked');

  if (nwfclo.routingValue !== null) {

    // Get the two hidden fields dom objects.
    nwfclo.caseTypeDom = document.getElementsByName(nwfclo.caseTypeFieldName)[0];
    nwfclo.respondingOfficeDom = document.getElementsByName(nwfclo.respondingOfficeFieldName)[0];

    // If we get a match on the map from the radio button set the
    // hidden fields.
    if (nwfclo.map[nwfclo.routingValue.value]) {
      nwfclo.caseTypeDom.value = nwfclo.map[nwfclo.routingValue.value].caseType;
      nwfclo.respondingOfficeDom.value = nwfclo.map[nwfclo.routingValue.value].respondingOffice;
    }
  }
}
