// this jquery code adds additional fields to the form
// to add sfdc integration email triggers and other fields
// which change value based on the values of member checkboxes
// Sam Dorman 2009

// works for signup pages, donations, questionnaires, petitions (old), campaigns (old), LTEs, event registrations, actions, create an event
// can optionally add to TAF and postcard if desired but currently it doesnt
// doesn't work on unsubscribes because dia only processes first email_trigger_KEYS input


// To require either the OEF and/or OIF checkbox be checked, include this code in the DIA page, like in the Prefooter box:
// <!-- require OEF or OIF -->
//      <input type="hidden" name="OEF_or_OIF_required" /> 

// To count the page as a member application, include this code in the DIA page, like in the Prefooter box:
// <!-- count as member application -->
//      <input type="hidden" name="counts_as_member_app" /> 


$(document).ready(function() {
        OEF_field = 'BOOL0';
        OIF_field = 'BOOL1';
        membersField = 'BOOL20';
        membersGroup = '17404';
        contactTrigger = '8303';
        donationTrigger = '8201';
        contactForm = false;
        formRef = 'form[name=data]';
		pathname = location.href;
		pagename = pathname.substring(pathname.lastIndexOf('/') + 1, pathname.length);
		submittedDIAMemberApp_field = 'BOOL4';
		OEF_or_OIF_required_field = 'OEF_or_OIF_required';
		counts_as_member_app_field = 'counts_as_member_app';


        if ($('form[name=data]').length) {contactForm=true; formRef='form[name=data]';}
        else if ($('form[name=letter_data]').length) {contactForm=true; formRef='form[name=letter_data]';}
        else if ($('form[name=event_registration]').length) {contactForm=true; formRef='form[name=event_registration]';}
        else if ($('form[name=unsubscribedata]').length) {contactForm=true; formRef='form[name=unsubscribedata]';}
        else if ($('form[name=actionForm]').length) {contactForm=true; formRef='form[name=actionForm]';}
        else if ($('form[name=eventForm]').length) {contactForm=true; formRef='form[name=eventForm]';}
     //   else if ($('#data').length) {contactForm=true; formRef='#data';}
	else { contactForm = false; }

        // if checkboxes are present, add additional fields that change based on whether either box is checked

     if (contactForm) {
        $(formRef).append('<input type="hidden" name="email_trigger_KEYS" value="'
                        + contactTrigger + '"/>');
	if ($('input[name=' + OEF_field + ']').length || $('input[name=' + OIF_field + ']').length) {
             $(formRef).append('<input type="hidden" name="'
                        + membersField + '"/>' +
                     '<input type="hidden" name="link" value="groups">' +
                     '<input type="hidden" name="linkKey" value="" id="dynamic-group-key">');

             function checkMemberStatus() {
                   if ($('input[name=' + OEF_field + ']').is(':checked')
                       || $('input[name=' + OIF_field + ']').is(':checked')) {
                             $('input[name=' + membersField + ']').val('1');
                             $('#dynamic-group-key').val(membersGroup);
                   } else {
                             $('input[name=' + membersField + ']').val('');
                             $('#dynamic-group-key').val('');
                   }
              }
              if ($('input[name=' + OEF_field + ']').length) {
                   $('input[name=' + OEF_field + ']').change( function() {checkMemberStatus();});
              }
              if ($('input[name=' + OIF_field + ']').length) {
                   $('input[name=' + OIF_field + ']').change( function() {checkMemberStatus();});
              }
        }

		// if this is a page where OEF/OIF are required
		// add javascript validation for at least one of those checkboxes
		if ($('input[name=' + OEF_or_OIF_required_field + ']').length) {
			$(formRef).submit(function() {
			   if ($('input[name="BOOL0"]')[0].checked || $('input[name="BOOL1"]')[0].checked) {
     			   return true;
			   } else {
     			  alert('Please indicate whether you are a veteran of Iraq and/or Afghanistan.\n\nIf you are neither, follow the link at the top of the form to the page for other supporters.');
     			  return false; // blocks the submit
 			   }
            });
		}
		
		// if this page is being treated as a member app		
		// and set the DIA Member App Submitted checkbox
		if ($('input[name=' + counts_as_member_app_field + ']').length) {
            $(formRef).append('<input type="hidden" name="'
                        + submittedDIAMemberApp_field + '" value="1"/>');
		}
		
		

     }

     if ($('form[name=donation_data]').length) {
        $('form[name=donation_data]').append('<input type="hidden" name="email_trigger_KEYS" value="'
                        + donationTrigger + '"/>');
     }

});



function membercheck(myfield)
{
    // Checks whether OEF or OIF checked, if so checks Veteran checkbox and adds to DIA group
    var OEFfield = "BOOL0";
    var OIFfield = "BOOL1";
    var memberField = "BOOL20";
    var memberGroup = "17404";

    if (myfield.form[OEFfield][0].checked || myfield.form[OIFfield][0].checked) {
        // alert("veteran");
        myfield.form[memberField].value = "1";
        document.getElementById('dynamic-group-key').value = memberGroup;
    } else {
        myfield.form[memberField].value = "";
        document.getElementById('dynamic-group-key').value = "";
    }
}

