jQuery(document).ready(function() {
   getUrlVars();
  jQuery('#edit-recipient').on('change', function() {
     jQuery('.field--label').text(getBoard(jQuery('#edit-recipient option:selected').text()) + ' Contact Form');
     jQuery('input[name="send_to_email"]').val(getEmail(jQuery('#edit-recipient option:selected').text()));
  });
});

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        if (hash[0] === 'contact-id') {
          vars.push(hash[1]);
          break;
        }
    }
  
    if (vars.length > 0) {
      jQuery('#edit-recipient').val(vars[0]); 

      jQuery('.field--label').text(getBoard(jQuery('#edit-recipient option:selected').text()) + ' Contact Form');
      jQuery('input[name="send_to_email"]').val(getEmail(jQuery('#edit-recipient option:selected').text()));
    } else {
      jQuery('input[name="send_to_email"]').val('webcoordinator@dpor.virginia.gov');
    }
    return vars[0];
}

function getBoard(selectedValue) {
  var valAry = selectedValue.split('-');
  return jQuery.trim(valAry[0]);
}

function getEmail(selectedValue) {
  var valAry = selectedValue.split('-');
  return jQuery.trim(valAry[1]);
}