adding all weblabels from weblabels.fsf.org
[weblabels.fsf.org.git] / www.fsf.org / 20131028 / files / crm.fsf.org / Common.js
CommitLineData
5a920362 1/*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.2 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2012 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25*/
26
27/**
28 *
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2012
31 * $Id$
32 *
33 */
34
35/**
36 * This function can be used to clear default 'suggestive text' from an input field
37 * When the cursor is moved into the field.
38 *
39 * It is generally invoked by the input field's onFocus event. Use the reserved
40 * word 'this' to pass this object. EX: onFocus="clearFldVal(this);"
41 *
42 * @access public
43 * @param fld The form field object whose value is to be cleared
44 * @param hideBlocks Array of element Id's to be hidden
45 * @return none
46 */
47function clearFldVal(fld) {
48 if (fld.value == fld.defaultValue) {
49 fld.value = "";
50 }
51}
52
53/**
54 * This function is called by default at the bottom of template files which have forms that have
55 * conditionally displayed/hidden sections and elements. The PHP is responsible for generating
56 * a list of 'blocks to show' and 'blocks to hide' and the template passes these parameters to
57 * this function.
58 *
59 * @access public
60 * @param showBlocks Array of element Id's to be displayed
61 * @param hideBlocks Array of element Id's to be hidden
62 * @param elementType Value to set display style to for showBlocks (e.g. 'block' or 'table-row' or ...)
63 * @return none
64 */
65function on_load_init_blocks(showBlocks, hideBlocks, elementType)
66{
67 if ( elementType == null ) {
68 var elementType = 'block';
69 }
70
71 /* This loop is used to display the blocks whose IDs are present within the showBlocks array */
72 for ( var i = 0; i < showBlocks.length; i++ ) {
73 var myElement = document.getElementById(showBlocks[i]);
74 /* getElementById returns null if element id doesn't exist in the document */
75 if (myElement != null) {
76 myElement.style.display = elementType;
77 } else {
78 alert('showBlocks array item not in .tpl = ' + showBlocks[i]);
79 }
80 }
81
82 /* This loop is used to hide the blocks whose IDs are present within the hideBlocks array */
83 for ( var i = 0; i < hideBlocks.length; i++ ) {
84 var myElement = document.getElementById(hideBlocks[i]);
85 /* getElementById returns null if element id doesn't exist in the document */
86 if (myElement != null) {
87 myElement.style.display = 'none';
88 } else {
89 alert('showBlocks array item not in .tpl = ' + hideBlocks[i]);
90 }
91 }
92}
93
94/**
95 * This function is called when we need to show or hide a related form element (target_element)
96 * based on the value (trigger_value) of another form field (trigger_field).
97 *
98 * @access public
99 * @param trigger_field_id HTML id of field whose onchange is the trigger
100 * @param trigger_value List of integers - option value(s) which trigger show-element action for target_field
101 * @param target_element_id HTML id of element to be shown or hidden
102 * @param target_element_type Type of element to be shown or hidden ('block' or 'table-row')
103 * @param field_type Type of element radio/select
104 * @param invert Boolean - if true, we HIDE target on value match; if false, we SHOW target on value match
105 * @return none
106*/
107function showHideByValue(trigger_field_id, trigger_value, target_element_id, target_element_type, field_type, invert ) {
108 if ( target_element_type == null ) {
109 var target_element_type = 'block';
110 } else if ( target_element_type == 'table-row' ) {
111 var target_element_type = '';
112 }
113
114 if (field_type == 'select') {
115 var trigger = trigger_value.split("|");
116 var selectedOptionValue = document.getElementById(trigger_field_id).options[document.getElementById(trigger_field_id).selectedIndex].value;
117
118 var target = target_element_id.split("|");
119 for(var j = 0; j < target.length; j++) {
120 if ( invert ) {
121 show(target[j], target_element_type);
122 } else {
123 hide(target[j],target_element_type);
124 }
125 for(var i = 0; i < trigger.length; i++) {
126 if (selectedOptionValue == trigger[i]) {
127 if ( invert ) {
128 hide(target[j],target_element_type);
129 } else {
130 show(target[j],target_element_type);
131 }
132 }
133 }
134 }
135
136 } else if (field_type == 'radio') {
137 var target = target_element_id.split("|");
138 for(var j = 0; j < target.length; j++) {
139 if (document.getElementsByName(trigger_field_id)[0].checked) {
140 if ( invert ) {
141 hide(target[j], target_element_type);
142 } else {
143 show(target[j], target_element_type);
144 }
145 } else {
146 if ( invert ) {
147 show(target[j], target_element_type);
148 } else {
149 hide(target[j], target_element_type);
150 }
151 }
152 }
153 }
154}
155
156/**
157 * This function is used to display a page element (e.g. block or table row or...).
158 *
159 * This function is called by various links which handle requests to display the hidden blocks.
160 * An example is the <code>[+] another phone</code> link which expands an additional phone block.
161 * The parameter block_id must have the id of the block which has to be displayed.
162 *
163 *
164 * @access public
165 * @param block_id Id value of the block (or row) to be displayed.
166 * @param elementType Value to set display style to when showing the element (e.g. 'block' or 'table-row' or ...)
167 * @return none
168 */
169function show(block_id,elementType)
170{
171 if ( elementType == null ) {
172 var elementType = 'block';
173 } else if ( elementType == "table-row" && navigator.appName == 'Microsoft Internet Explorer' ) {
174 var elementType = "block";
175 }
176 var myElement = document.getElementById(block_id);
177 if (myElement != null) {
178 myElement.style.display = elementType;
179 } else {
180 alert('Request to show() function failed. Element id undefined = '+ block_id);
181 }
182}
183
184/**
185 * This function is used to hide a block.
186 *
187 * This function is called by various links which handle requests to hide the visible blocks.
188 * An example is the <code>[-] hide phone</code> link which hides the phone block.
189 * The parameter block_id must have the id of the block which has to be hidden.
190 *
191 * @access public
192 * @param block_id Id value of the block to be hidden.
193 * @return none
194 */
195function hide(block_id)
196{
197 var myElement = document.getElementById(block_id);
198 if (myElement != null) {
199 myElement.style.display = 'none';
200 } else {
201 alert('Request to hide() function failed. Element id undefined = ' + block_id);
202 }
203}
204
205/**
206 *
207 * Function for checking ALL or unchecking ALL check boxes in a resultset page.
208 *
209 * @access public
210 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
211 * belonging to the resultset's checkbox collection
212 * @param action - 'select' = set all to checked; 'deselect' = set all to unchecked
213 * @param form - name of form that checkboxes are part of
214 * Sample usage: onClick="javascript:changeCheckboxValues('chk_', 'select', myForm );"
215 *
216 * @return
217 */
218function toggleCheckboxVals(fldPrefix,object) {
219 if ( object.id == 'toggleSelect' && cj(object).is(':checked') ) {
220 cj( 'Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', true);
221 } else {
222 cj( 'Input[id*="' + fldPrefix + '"],Input[id*="toggleSelect"]').attr('checked', false);
223 }
224 /* function called to change the color of selected rows */
225 on_load_init_checkboxes(object.form.name);
226}
227
228function countSelectedCheckboxes(fldPrefix, form) {
229 fieldCount = 0;
230 for( i=0; i < form.elements.length; i++) {
231 fpLen = fldPrefix.length;
232 if (form.elements[i].type == 'checkbox' && form.elements[i].name.slice(0,fpLen) == fldPrefix && form.elements[i].checked == true) {
233 fieldCount++;
234 }
235 }
236 return fieldCount;
237}
238
239/**
240 * Function to enable task action select
241 */
242function toggleTaskAction( status ) {
243 var radio_ts = document.getElementsByName('radio_ts');
244 if (!radio_ts[1]) {
245 radio_ts[0].checked = true;
246 }
247 if ( radio_ts[0].checked || radio_ts[1].checked ) {
248 status = true;
249 }
250
251 var formElements = ['task', 'Go', 'Print'];
252 for(var i=0; i<formElements.length; i++ ) {
253 var element = document.getElementById( formElements[i] );
254 if ( element ) {
255 if ( status ) {
256 element.disabled = false;
257 } else {
258 element.disabled = true;
259 }
260 }
261 }
262}
263
264/**
265 * This function is used to check if any actio is selected and also to check if any contacts are checked.
266 *
267 * @access public
268 * @param fldPrefix - common string which precedes unique checkbox ID and identifies field as
269 * belonging to the resultset's checkbox collection
270 * @param form - name of form that checkboxes are part of
271 * Sample usage: onClick="javascript:checkPerformAction('chk_', myForm );"
272 *
273 */
274function checkPerformAction (fldPrefix, form, taskButton, selection) {
275 var cnt;
276 var gotTask = 0;
277
278 // taskButton TRUE means we don't need to check the 'task' field - it's a button-driven task
279 if (taskButton == 1) {
280 gotTask = 1;
281 } else if (document.forms[form].task.selectedIndex) {
282 //force user to select all search contacts, CRM-3711
283 if ( document.forms[form].task.value == 13 || document.forms[form].task.value == 14 ) {
284 var toggleSelect = document.getElementsByName('toggleSelect');
285 if ( toggleSelect[0].checked || document.forms[form].radio_ts[0].checked ) {
286 return true;
287 } else {
288 alert( "Please select all contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button." );
289 return false;
290 }
291 }
292 gotTask = 1;
293 }
294
295 if (gotTask == 1) {
296 // If user wants to perform action on ALL records and we have a task, return (no need to check further)
297 if (document.forms[form].radio_ts[0].checked) {
298 return true;
299 }
300
301 cnt = (selection == 1) ? countSelections() : countSelectedCheckboxes(fldPrefix, document.forms[form]);
302 if (!cnt) {
303 alert ("Please select one or more contacts for this action.\n\nTo use the entire set of search results, click the 'all records' radio button.");
304 return false;
305 }
306 } else {
307 alert ("Please select an action from the drop-down menu.");
308 return false;
309 }
310}
311
312/**
313 * This function changes the style for a checkbox block when it is selected.
314 *
315 * @access public
316 * @param chkName - it is name of the checkbox
317 * @return null
318 */
319function checkSelectedBox( chkName ) {
320 var checkElement = cj('#' + chkName );
321 if ( checkElement.attr('checked') ) {
322 cj('input[value=ts_sel]:radio').attr('checked',true );
323 checkElement.parents('tr').addClass('crm-row-selected');
324 } else {
325 checkElement.parents('tr').removeClass('crm-row-selected');
326 }
327}
328
329/**
330 * This function is to show the row with selected checkbox in different color
331 * @param form - name of form that checkboxes are part of
332 *
333 * @access public
334 * @return null
335 */
336function on_load_init_checkboxes(form)
337{
338 var formName = form;
339 var fldPrefix = 'mark_x';
340 for( i=0; i < document.forms[formName].elements.length; i++) {
341 fpLen = fldPrefix.length;
342 if (document.forms[formName].elements[i].type == 'checkbox' && document.forms[formName].elements[i].name.slice(0,fpLen) == fldPrefix ) {
343 checkSelectedBox (document.forms[formName].elements[i].name, formName);
344 }
345 }
346}
347
348/**
349 * Function to change the color of the class
350 *
351 * @param form - name of the form
352 * @param rowid - id of the <tr>, <div> you want to change
353 *
354 * @access public
355 * @return null
356 */
357function changeRowColor (rowid, form) {
358 switch (document.getElementById(rowid).className) {
359 case 'even-row' : document.getElementById(rowid).className = 'selected even-row';
360 break;
361 case 'odd-row' : document.getElementById(rowid).className = 'selected odd-row';
362 break;
363 case 'selected even-row' : document.getElementById(rowid).className = 'even-row';
364 break;
365 case 'selected odd-row' : document.getElementById(rowid).className = 'odd-row';
366 break;
367 case 'form-item' : document.getElementById(rowid).className = 'selected';
368 break;
369 case 'selected' : document.getElementById(rowid).className = 'form-item';
370 }
371}
372
373/**
374 * This function is to show the row with selected checkbox in different color
375 * @param form - name of form that checkboxes are part of
376 *
377 * @access public
378 * @return null
379 */
380function on_load_init_check(form)
381{
382 for( i=0; i < document.forms[form].elements.length; i++) {
383 if ( ( document.forms[form].elements[i].type == 'checkbox'
384 && document.forms[form].elements[i].checked == true )
385 || ( document.forms[form].elements[i].type == 'hidden'
386 && document.forms[form].elements[i].value == 1 ) ) {
387 var ss = document.forms[form].elements[i].id;
388 var row = 'rowid' + ss;
389 changeRowColor(row, form);
390 }
391 }
392}
393
394/**
395 * reset all the radio buttons with a given name
396 *
397 * @param string fieldName
398 * @param object form
399 * @return null
400 */
401function unselectRadio(fieldName, form) {
402 for( i=0; i < document.forms[form].elements.length; i++) {
403 if (document.forms[form].elements[i].name == fieldName) {
404 document.forms[form].elements[i].checked = false;
405 }
406 }
407 return;
408}
409
410/**
411 * Function to change button text and disable one it is clicked
412 *
413 * @param obj object - the button clicked
414 * @param formID string - the id of the form being submitted
415 * @param string procText - button text after user clicks it
416 * @return null
417 */
418var submitcount=0;
419/* Changes button label on submit, and disables button after submit for newer browsers.
420Puts up alert for older browsers. */
421function submitOnce(obj,formId,procText) {
422 // if named button clicked, change text
423 if (obj.value != null) {
424 obj.value = procText + " ...";
425 }
426 if (document.getElementById) { // disable submit button for newer browsers
427 obj.disabled = true;
428 document.getElementById(formId).submit();
429 return true;
430 } else { // for older browsers
431 if (submitcount == 0) {
432 submitcount++;
433 return true;
434 } else {
435 alert("Your request is currently being processed ... Please wait.");
436 return false;
437 }
438 }
439}
440
441/**
442 * Function submits referenced form on click of wizard nav link.
443 * Populates targetPage hidden field prior to POST.
444 *
445 * @param formID string - the id of the form being submitted
446 * @param targetPage - identifier of wizard section target
447 * @return null
448 */
449function submitCurrentForm(formId,targetPage) {
450 alert(formId + ' ' + targetPage);
451 document.getElementById(formId).targetPage.value = targetPage;
452 document.getElementById(formId).submit();
453}
454
455/**
456 * Function counts and controls maximum word count for textareas.
457 *
458 * @param essay_id string - the id of the essay (textarea) field
459 * @param wc - int - number of words allowed
460 * @return null
461 */
462function countit(essay_id,wc){
463 var text_area = document.getElementById("essay_" + essay_id);
464 var count_element = document.getElementById("word_count_" + essay_id);
465 var count = 0;
466 var text_area_value = text_area.value;
467 var regex = /\n/g;
468 var essay = text_area_value.replace(regex," ");
469 var words = essay.split(' ');
470
471 for (z=0; z<words.length; z++){
472 if (words[z].length>0){
473 count++;
474 }
475 }
476
477 count_element.value = count;
478 if (count>=wc) {
479 /*text_area.value = essay;*/
480
481 var dataString = '';
482 for (z=0; z<wc; z++){
483 if (words[z].length>0) {
484 dataString = dataString + words[z] + ' ';
485 }
486 }
487
488 text_area.value = dataString;
489 text_area.blur();
490 count = wc;
491 count_element.value = count;
492 alert("You have reached the "+ wc +" word limit.");
493 }
494}
495
496function popUp(URL) {
497 day = new Date();
498 id = day.getTime();
499 eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=640,height=420,left = 202,top = 184');");
500}
501
502function imagePopUp ( path ) {
503 window.open(path,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,screenX=150,screenY=150,top=150,left=150');
504}
505
506/**
507 * Function to show / hide the row in optionFields
508 *
509 * @param element name index, that whose innerHTML is to hide else will show the hidden row.
510 */
511function showHideRow( index ) {
512 if ( index ) {
513 cj( 'tr#optionField_' + index ).hide( );
514 if( cj( 'table#optionField tr:hidden:first' ).length ) cj( 'div#optionFieldLink' ).show( );
515 } else {
516 cj( 'table#optionField tr:hidden:first' ).show( );
517 if( ! cj( 'table#optionField tr:hidden:last' ).length ) cj( 'div#optionFieldLink' ).hide( );
518 }
519 return false;
520}
521
522/**
523 * Function to check activity status in relavent to activity date
524 *
525 * @param element message JSON object.
526 */
527function activityStatus( message ) {
528 var d = new Date(), time = [], i;
529 var currentDateTime = d.getTime()
530 var activityTime = cj("input#activity_date_time_time").val().replace(":", "");
531
532 //chunk the time in bunch of 2 (hours,minutes,ampm)
533 for(i=0; i<activityTime.length; i+=2 ) {
534 time.push( activityTime.slice( i, i+2 ) );
535 }
536 var activityDate = new Date( cj("input#activity_date_time_hidden").val() );
537
538 d.setFullYear(activityDate.getFullYear());
539 d.setMonth(activityDate.getMonth());
540 d.setDate(activityDate.getDate());
541 var hours = time['0'];
542 var ampm = time['2'];
543
544 if (ampm == "PM" && hours != 0 && hours != 12) {
545 // force arithmetic instead of string concatenation
546 hours = hours*1 + 12;
547 } else if (ampm == "AM" && hours == 12) {
548 hours = 0;
549 }
550 d.setHours(hours);
551 d.setMinutes(time['1']);
552
553 var activity_date_time = d.getTime();
554
555 var activityStatusId = cj('#status_id').val();
556
557 if ( activityStatusId == 2 && currentDateTime < activity_date_time ) {
558 if (! confirm( message.completed )) {
559 return false;
560 }
561 } else if ( activity_date_time && activityStatusId == 1 && currentDateTime >= activity_date_time ) {
562 if (! confirm( message.scheduled )) {
563 return false;
564 }
565 }
566}
567
568/**
569 * Function to make multiselect boxes behave as fields in small screens
570 */
571
572function advmultiselectResize() {
573 var amswidth = cj("#crm-container form:has(table.advmultiselect)").width();
574 if (amswidth < 700) {
575 cj("form table.advmultiselect td").each( function() {
576 cj(this).css('display', 'block');
577 });
578 } else {
579 cj("form table.advmultiselect td").each( function() {
580 cj(this).css('display', 'table-cell');
581 });
582 }
583 var contactwidth = cj('#crm-container #mainTabContainer').width();
584 if (contactwidth < 600) {
585 cj('#crm-container #mainTabContainer').addClass('narrowpage');
586 cj('#crm-container #mainTabContainer').addClass('narrowpage');
587 cj('#crm-container #mainTabContainer.narrowpage #contactTopBar td').each( function(index) {
588 if (index > 1) {
589 if (index%2 == 0) {
590 cj(this).parent().after('<tr class="narrowadded"></tr>');
591 }
592 var item = cj(this);
593 cj(this).parent().next().append(item);
594 }
595 });
596 } else {
597 cj('#crm-container #mainTabContainer.narrowpage').removeClass('narrowpage');
598 cj('#crm-container #mainTabContainer #contactTopBar tr.narrowadded td').each( function() {
599 var nitem = cj(this);
600 var parent = cj(this).parent();
601 cj(this).parent().prev().append(nitem);
602 if ( parent.children().size() == 0 ) {
603 parent.remove();
604 }
605 });
606 cj('#crm-container #mainTabContainer.narrowpage #contactTopBar tr.added').detach();
607 }
608 var cformwidth = cj('#crm-container #Contact .contact_basic_information-section').width();
609
610 if (cformwidth < 720) {
611 cj('#crm-container .contact_basic_information-section').addClass('narrowform');
612 cj('#crm-container .contact_basic_information-section table.form-layout-compressed td .helpicon').parent().addClass('hashelpicon');
613 if (cformwidth < 480) {
614 cj('#crm-container .contact_basic_information-section').addClass('xnarrowform');
615 } else {
616 cj('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform');
617 }
618 } else {
619 cj('#crm-container .contact_basic_information-section.narrowform').removeClass('narrowform');
620 cj('#crm-container .contact_basic_information-section.xnarrowform').removeClass('xnarrowform');
621 }
622}
623