4 // Behind the scenes method deals with browser for setting cursor position
5 $.caretTo = function (el
, index
) {
6 if (el
.createTextRange
) {
7 var range
= el
.createTextRange();
8 range
.move("character", index
);
11 else if (el
.selectionStart
!= null) {
13 el
.setSelectionRange(index
, index
);
17 //The following methods are queued under fx for more
18 //flexibility when combining with $.fn.delay() and
21 //Set caret to a particular index
22 $.fn
.caretTo = function (index
, offset
) {
23 return this.queue(function (next
) {
25 var i
= $(this).val().indexOf(index
);
26 if (offset
=== true) {
35 $.caretTo(this, index
);
42 * Display a personalized message containing the contact's name
43 * and a variable from the server
45 function assignAutoComplete(select_field
, id_field
, url
, varmax
, profileids
, autocomplete
) {
46 if(varmax
=== undefined) {varmax
= 10;}
47 if(profileids
=== undefined) {profileids
= [];}
49 if(url
=== undefined) {
50 url
= CRM
.url('civicrm/ajax/rest', 'className=CRM_Contact_Page_AJAX&fnName=getContactList&json=1');
53 var customObj
= $('#' + select_field
);
54 var customIdObj
= $('#' + id_field
);
56 if (!customObj
.hasClass('ac_input')) {
57 customObj
.autocomplete(url
,
58 { width
: 250, selectFirst
: false, matchContains
: true, max
: varmax
}).result(
59 function (event
, data
) {
60 customIdObj
.val(data
[1]);
62 var namefields
= ['first_name', 'last_name', 'middle_name'];
63 CRM
.api('profile', 'get', {'profile_id' : profileids
, 'contact_id' : data
[1]}, {
64 success: function(result
) {
65 $.each(result
.values
, function (id
, values
){
66 $.each(values
, function (fieldname
, fieldvalue
) {
67 $('#' + fieldname
).val(fieldvalue
);
74 customObj
.click(function () {
79 if(autocomplete
.show_hide
) {
81 showHideAutoComplete(select_field
, id_field
,
82 autocomplete
.show_text
,
83 autocomplete
.hide_text
,
90 * Show or hide the autocomplete and change the text
92 function showHideAutoComplete(name_field
, id_field
, hidden_text
, shown_text
, profileids
) {
93 $('#crm-contact-toggle-' + id_field
).on('click', function(event
) {
94 event
.preventDefault();
95 $('#' + name_field
).toggle();
96 if($('#' + name_field
).is(":visible")) {
97 $('#crm-contact-toggle-text-' + id_field
).text(shown_text
);
100 $('#crm-contact-toggle-text-' + id_field
).text(hidden_text
);
101 $('#' + id_field
).val('');
102 $('#' + name_field
).val('');
103 CRM
.api('profile', 'get', {'profile_id' : profileids
}, {
104 success: function(result
) {
105 $.each(result
.values
, function (id
, values
){
106 $.each(values
, function (fieldname
, fieldvalue
) {
107 $('#' + fieldname
).val(fieldvalue
);
117 var autocompletes
= CRM
.form
.autocompletes
;
118 var url
= CRM
.url(autocompletes
.url
[0], autocompletes
.url
[1]);
120 $(autocompletes
).each(function (index
, autocomplete
) {
121 assignAutoComplete(autocomplete
.name_field
, autocomplete
.id_field
, url
, autocomplete
.max
, CRM
.ids
.profile
, autocomplete
);