CRM-14701 fixes - Form data not retained subsequent to contribution page on behalf...
[civicrm-core.git] / templates / CRM / Contribute / Form / Contribution / OnBehalfOf.tpl
index 8dbc5d970033305674b2be6ece7cbe2c38ca88a2..fbd78867b26b9e3e3889fed9ec39d47c0a5b6843 100644 (file)
         {else}
           <div class="label">{$form.onbehalf.$fieldName.label}</div>
           <div class="content">
-            {$form.onbehalf.$fieldName.html}
-            {if $fieldName eq 'organization_name'}
-              <div id="id-onbehalf-orgname-help" class="description">{ts}Start typing the name of an organization that you have saved previously to use it again. Otherwise click "Enter a new organization" above.{/ts}</div>
+            {if $fieldName eq 'organization_name' and !empty($form.onbehalfof_id)}
+              {$form.onbehalfof_id.html}
             {/if}
+            {$form.onbehalf.$fieldName.html}
             {if !empty($onBehalfOfFields.$fieldName.html_type)  && $onBehalfOfFields.$fieldName.html_type eq 'Autocomplete-Select'}
               {assign var=elementName value=onbehalf[$fieldName]}
             {include file="CRM/Custom/Form/AutoComplete.tpl" element_name=$elementName}
   {/foreach}
 </div>
 <div>{$form.mode.html}</div>
+</fieldset>
 {/if}
-
+{if empty($snippet)}
 {literal}
 <script type="text/javascript">
-  cj( "div#id-onbehalf-orgname-help").hide( );
 
   showOnBehalf({/literal}"{$onBehalfRequired}"{literal});
 
 
 function showOnBehalf(onBehalfRequired) {
   if ( cj( "#is_for_organization" ).attr( 'checked' ) || onBehalfRequired ) {
-    var urlPath = {/literal}"{crmURL p=$urlPath h=0 q='snippet=4&onbehalf=1'}";
-    urlPath += "{$urlParams}";
+    var urlPath = {/literal}"{crmURL p=$urlPath h=0 q="snippet=4&onbehalf=1&id=$contributionPageID&qfKey=$qfKey"}";
     {if $mode eq 'test'}
       urlPath += '&action=preview';
     {/if}
@@ -156,15 +155,8 @@ function showOnBehalf(onBehalfRequired) {
       urlPath += '&reset={$reset}';
     {/if}{literal}
     cj("#onBehalfOfOrg").show();
-    if (cj("#onBehalfOfOrg *").length < 1) {
-      cj.ajax({
-        url     : urlPath,
-        global  : false,
-        async   : false,
-        success : function ( content ) {
-          cj( "#onBehalfOfOrg" ).html( content );
-        }
-      });
+    if (cj("fieldset", '#onBehalfOfOrg').length < 1) {
+      cj('#onBehalfOfOrg').load(urlPath);
     }
   }
   else {
@@ -172,22 +164,9 @@ function showOnBehalf(onBehalfRequired) {
   }
 }
 
-function resetValues( filter ) {
-  if (filter) {
-    cj("#select_org div").find( 'input[type=text], select, textarea' ).each(function( ) {
-      if ( cj(this).attr('name') != 'onbehalf[organization_name]' ) {
-        cj(this).val('');
-      }
-    });
-  }
-  else {
-    cj("#select_org div").find( 'input[type=text], select, textarea' ).each(function( ) {
-      cj(this).val( '' );
-    });
-  }
-  cj("#select_org tr td").find( 'input[type=radio], input[type=checkbox]' ).each(function( ) {
-    cj(this).attr('checked', false);
-  });
+function resetValues() {
+  cj('input[type=text], select, textarea', "#select_org div").not('#onbehalfof_id').val('');
+  cj('input[type=radio], input[type=checkbox]', "#select_org tr td").prop('checked', false);
 }
 
 function createNew( ) {
@@ -195,7 +174,7 @@ function createNew( ) {
     var textMessage = ' {/literal}{ts escape="js"}Use existing organization{/ts}{literal} ';
     cj("#onbehalf_organization_name").removeAttr('readonly');
     cj("#mode").removeAttr('checked');
-    resetValues( false );
+    resetValues();
   }
   else {
     var textMessage = ' {/literal}{ts escape="js"}Enter a new organization{/ts}{literal} ';
@@ -214,8 +193,18 @@ function setOrgName( ) {
 }
 
 
-function setLocationDetails(contactID) {
-  resetValues(true);
+function setLocationDetails(contactID , reset) {
+  var submittedCID = {/literal}"{$submittedOnBehalf}"{literal};
+  var submittedOnBehalfInfo = {/literal}'{$submittedOnBehalfInfo}'{literal};
+  submittedOnBehalfInfo = cj.parseJSON(submittedOnBehalfInfo);
+  if (submittedCID == contactID) {
+    cj.each(submittedOnBehalfInfo, function(key, value) {
+      cj('#onbehalf_' + key ).val(value);
+    });
+    return;
+  }
+
+  resetValues();
   var locationUrl = {/literal}"{$locDataURL}"{literal} + contactID + "&ufId=" + {/literal}"{$profileId}"{literal};
   cj.ajax({
     url         : locationUrl,
@@ -265,34 +254,27 @@ function setLocationDetails(contactID) {
   });
 }
 
-var orgOption = '';
 cj("input:radio[name='org_option']").click( function( ) {
-  orgOption = cj("input:radio[name='org_option']:checked").val( );
+  var orgOption = cj(this).val();
   selectCreateOrg(orgOption, true);
 });
 
+cj('#onbehalfof_id').change(function() {
+  setLocationDetails(cj(this).val());
+}).change();
+
 function selectCreateOrg( orgOption, reset ) {
   if (orgOption == 0) {
-    cj("div#id-onbehalf-orgname-help").show( );
-    var dataUrl = {/literal}"{$employerDataURL}"{literal};
-    cj('#onbehalf_organization_name').autocomplete( dataUrl,
-      { width         : 180,
-        selectFirst   : false,
-        matchContains : true,
-        max: {/literal}{crmSetting name="search_autocomplete_count" group="Search Preferences"}{literal}
-      }).result( function( event, data, formatted ) {
-        cj('#onbehalf_organization_name').val( data[0] );
-        cj('#onbehalfof_id').val( data[1] );
-        setLocationDetails( data[1] );
-      });
+    cj("#onbehalfof_id").show().change();
+    cj("input#onbehalf_organization_name").hide()
   }
   else if ( orgOption == 1 ) {
-    cj("input#onbehalf_organization_name").removeClass( 'ac_input' ).unautocomplete( );
-    cj("div#id-onbehalf-orgname-help").hide( );
+    cj("input#onbehalf_organization_name").show();
+    cj("#onbehalfof_id").hide();
   }
 
   if ( reset ) {
-    resetValues( false );
+    resetValues();
   }
 }
 
@@ -318,4 +300,4 @@ function selectCreateOrg( orgOption, reset ) {
 {/if}
 
 </script>
-</fieldset>
+{/if}