commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / templates / CRM / Batch / Form / Entry.js
1 //@todo functions partially moved from tpl but still need an enclosure / cleanup
2 // jslinting etc
3 CRM.$(function($) {
4 $('.selector-rows').change(function () {
5 var options = {
6 'url': CRM.url('civicrm/ajax/batch')
7 };
8
9 $("#Entry").ajaxSubmit(options);
10
11 //avoid multiple click on submit button
12 $("#_qf_Entry_upload").on('click', function() {
13 $('#Entry').block();
14 });
15
16 // validate rows
17 checkColumns($(this));
18 });
19 cj('.pledge-adjust-option').click(function(){
20 var blockNo = cj(this).attr('id');
21 cj('select[id="option_type_' + blockNo + '"]').show();
22 cj('select[id="option_type_' + blockNo + '"]').removeAttr('disabled');
23 cj('#field_' + blockNo + '_total_amount').removeAttr('readonly');
24 });
25 $('input[name^="soft_credit_contact_"]').on('change', function(){
26 var rowNum = $(this).attr('id').replace('soft_credit_contact_id_','');
27 var totalAmount = $('#field_'+rowNum+'_total_amount').val();
28 //assign total amount as default soft credit amount
29 $('#soft_credit_amount_'+ rowNum).val(totalAmount);
30 //assign soft credit type default value if any
31 $('#soft_credit_type_'+ rowNum).val($('#sct_default_id').val());
32 });
33
34 // validate rows
35 validateRow();
36
37 //calculate the actual total for the batch
38 calculateActualTotal();
39
40 $('input[id*="_total_amount"]').bind('keyup change', function () {
41 calculateActualTotal();
42 });
43
44 if (CRM.batch.type_id == 1) {
45 // hide all dates if send receipt is checked
46 hideSendReceipt();
47
48 // hide the receipt date if send receipt is checked
49 $('input[id*="][send_receipt]"]').change(function () {
50 showHideReceipt($(this));
51 });
52
53 }
54 else if (CRM.batch.type_id == 2){
55 cj('select[id^="member_option_"]').each(function () {
56 if (cj(this).val() == 1) {
57 cj(this).attr('disabled', true);
58 }
59 });
60
61 // set payment info accord to membership type
62 $('select[id*="_membership_type_0"]').change(function () {
63 setPaymentBlock($(this), null);
64 });
65 $('select[id*="_membership_type_1"]').change(function () {
66 setPaymentBlock($(this), $(this).val());
67 });
68
69 }
70
71 // line breaks between radio buttons and checkboxes
72 $('input.form-radio').next().after('<br />');
73 $('input.form-checkbox').next().after('<br />');
74
75 //set the focus on first element
76 $('#primary_contact_1').focus();
77
78 });
79
80
81
82 function setPaymentBlock(form, memType) {
83 var rowID = form.closest('div.crm-grid-row').attr('entity_id');
84 var dataUrl = CRM.url('civicrm/ajax/memType');
85
86 if (!memType) {
87 memType = cj('select[id="field_' + rowID + '_membership_type_1"]').val();
88 }
89
90 cj.post(dataUrl, {mtype: memType}, function (data) {
91 cj('#field_' + rowID + '_financial_type').val(data.financial_type_id);
92 cj('#field_' + rowID + '_total_amount').val(data.total_amount).change();
93 }, 'json');
94 }
95
96 function hideSendReceipt() {
97 cj('input[id*="][send_receipt]"]').each(function () {
98 showHideReceipt(cj(this));
99 });
100 }
101
102 function showHideReceipt(elem) {
103 var rowID = elem.closest('div.crm-grid-row').attr('entity_id');
104 if (elem.prop('checked')) {
105 cj('.crm-batch-receipt_date-' + rowID).hide();
106 }
107 else {
108 cj('.crm-batch-receipt_date-' + rowID).show();
109 }
110 }
111
112 function validateRow() {
113 cj('.selector-rows').each(function () {
114 checkColumns(cj(this));
115 });
116 }
117
118 function checkColumns(parentRow) {
119 // show valid row icon if all required data is field
120 var validRow = 0;
121 var inValidRow = 0;
122 var errorExists = false;
123 var rowID = parentRow.closest('div.crm-grid-row').attr('entity_id');
124
125 parentRow.find('div .required').each(function () {
126 //special case to handle contact autocomplete select
127 var fieldId = cj(this).attr('id');
128 if (fieldId.substring(0, 16) == 'primary_contact_') {
129 // if display value is set then make sure we also check if contact id is set
130 if (!cj(this).val()) {
131 inValidRow++;
132 }
133 else {
134 if (cj(this).val() && !cj('input[name="primary_contact_select_id[' + rowID + ']"]').val()) {
135 inValidRow++;
136 errorExists = true;
137 }
138 }
139 }
140 else {
141 if (!cj(this).val()) {
142 inValidRow++;
143 }
144 else {
145 if (cj(this).hasClass('error') && !cj(this).hasClass('valid')) {
146 errorExists = true;
147 }
148 else {
149 validRow++;
150 }
151 }
152 }
153 });
154
155 // this means user has entered some data
156 if (errorExists) {
157 parentRow.find("div:first span").prop('class', 'batch-invalid');
158 }
159 else {
160 if (inValidRow === 0 && validRow > 0) {
161 parentRow.find("div:first span").prop('class', 'batch-valid');
162 }
163 else {
164 parentRow.find("div:first span").prop('class', 'batch-edit');
165 }
166 }
167 }
168
169 function calculateActualTotal() {
170 var total = 0;
171 cj('input[id*="_total_amount"]').each(function () {
172 if (cj(this).val()) {
173 total += parseFloat(cj(this).val());
174 }
175 });
176
177 cj('.batch-actual-total').html(formatMoney(total));
178 }
179
180 //money formatting/localization
181 function formatMoney(amount) {
182 var c = 2;
183 var t = CRM.setting.monetaryThousandSeparator;
184 var d = CRM.setting.monetaryDecimalPoint;
185
186 c = isNaN(c = Math.abs(c)) ? 2 : c;
187 t = t === undefined ? "." : t;
188 d = d === undefined ? "," : d;
189
190 var n = amount,
191 i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
192 s = n < 0 ? "-" : "",
193 j = (j = i.length) > 3 ? j % 3 : 0;
194
195 return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
196 }
197
198 /**
199 * This function is use to setdefault elements via ajax
200 *
201 * @param fname string field name
202 * @return void
203 */
204 function setFieldValue(fname, fieldValue, blockNo) {
205 var elementId = cj('[name="field[' + blockNo + '][' + fname + ']"]');
206
207 if (elementId.length === 0) {
208 elementId = cj('input[type=checkbox][name^="field[' + blockNo + '][' + fname + ']"][type!=hidden]');
209 }
210
211 // if element not found than return
212 if (elementId.length === 0) {
213 return;
214 }
215
216 //check if it is date element
217 var isDateElement = elementId.attr('format');
218
219 // check if it is wysiwyg element
220 var editor = elementId.attr('editor');
221
222 //get the element type
223 var elementType = elementId.attr('type');
224
225 // set the value for all the elements, elements needs to be handled are
226 // select, checkbox, radio, date fields, text, textarea, multi-select
227 // wysiwyg editor, advanced multi-select ( to do )
228 if (elementType == 'radio') {
229 if (fieldValue) {
230 elementId.filter("[value=" + fieldValue + "]").prop("checked", true);
231 }
232 else {
233 elementId.removeProp('checked');
234 }
235 }
236 else {
237 if (elementType == 'checkbox') {
238 // handle checkbox
239 elementId.removeProp('checked');
240 if (fieldValue) {
241 cj.each(fieldValue, function (key, value) {
242 cj('input[name="field[' + blockNo + '][' + fname + '][' + value + ']"]').prop('checked', true);
243 });
244 }
245 }
246 else {
247 if (editor) {
248 switch (editor) {
249 case 'ckeditor':
250 elemtId = elementId.attr('id');
251 oEditor = CKEDITOR.instances[elemtId];
252 oEditor.setData(htmlContent);
253 break;
254 case 'tinymce':
255 var elemtId = element.attr('id');
256 tinyMCE.get(elemtId).setContent(htmlContent);
257 break;
258 case 'joomlaeditor':
259 // TO DO
260 case 'drupalwysiwyg':
261 // TO DO
262 break;
263 default:
264 elementId.val(fieldValue);
265 }
266 }
267 else {
268 elementId.val(fieldValue);
269 }
270 }
271 }
272
273 // since we use different display field for date we also need to set it.
274 // also check for date time field and set the value correctly
275 if (isDateElement && fieldValue) {
276 setDateFieldValue(fname, fieldValue, blockNo);
277 }
278 }
279
280 function setDateFieldValue(fname, fieldValue, blockNo) {
281 var dateValues = fieldValue.split(' ');
282
283 var actualDateElement = cj('#field_' + blockNo + '_' + fname);
284 var date_format = actualDateElement.attr('format');
285 var altDateFormat = 'yy-mm-dd';
286
287 var actualDateValue = cj.datepicker.parseDate(altDateFormat, dateValues[0]);
288
289 // format date according to display field
290 var hiddenDateValue = cj.datepicker.formatDate('mm/dd/yy', actualDateValue);
291
292 actualDateElement.val(hiddenDateValue);
293
294 var displayDateValue = actualDateElement.val();
295 if (date_format != 'mm/dd/yy') {
296 displayDateValue = cj.datepicker.formatDate(date_format, actualDateValue);
297 }
298
299 cj('#field_' + blockNo + '_' + fname + '_display').val(displayDateValue);
300
301 // need to fix time formatting
302 if (dateValues[1]) {
303 cj('#field_' + blockNo + '_' + fname + '_time').val(dateValues[1].substr(0, 5));
304 }
305 }