-- Indentation fixes
[civicrm-core.git] / js / Common.js
index e0b4d42c59ad114755816a637981cb942b80c3c5..437ecffd8bab383f074a3da952cfe98ec5994910 100644 (file)
@@ -1,6 +1,6 @@
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -618,7 +618,7 @@ CRM.validate = CRM.validate || {
   };
 
   var h;
-  CRM.help = function (title, params) {
+  CRM.help = function (title, params, url) {
     h && h.close && h.close();
     var options = {
       expires: 0
@@ -626,7 +626,7 @@ CRM.validate = CRM.validate || {
     h = CRM.alert('...', title, 'crm-help crm-msg-loading', options);
     params.class_name = 'CRM_Core_Page_Inline_Help';
     params.type = 'page';
-    $.ajax(CRM.url('civicrm/ajax/inline'),
+    $.ajax(url || CRM.url('civicrm/ajax/inline'),
       {
         data: params,
         dataType: 'html',
@@ -804,7 +804,12 @@ CRM.validate = CRM.validate || {
       CRM.alert(text, title, type, options);
     });
     // Handle qf form errors
-    $('form :input.error', this).one('blur', function () {
+    $('form :input.error', this).one('blur', function() {
+      // ignore autocomplete fields
+      if ($(this).is('.ac_input')) {
+        return;
+      }
+
       $('.ui-notify-message.error a.ui-notify-close').click();
       $(this).removeClass('error');
       $(this).next('span.crm-error').remove();
@@ -874,4 +879,33 @@ CRM.validate = CRM.validate || {
       $(this).toggleClass('collapsed');
     });
   };
+
+  /**
+   * Clientside currency formatting
+   * @param value
+   * @param format
+   * @return string
+   */
+  var currencyTemplate;
+  CRM.formatMoney = function(value, format) {
+    var decimal, separator, sign, i, j, result;
+    if (value === 'init' && format) {
+      currencyTemplate = format;
+      return;
+    }
+    format = format || currencyTemplate;
+    result = /1(.?)234(.?)56/.exec(format);
+    if (result === null) {
+      return 'Invalid format passed to CRM.formatMoney';
+    }
+    separator = result[1];
+    decimal = result[2];
+    sign = (value < 0) ? '-' : '';
+    //extracting the absolute value of the integer part of the number and converting to string
+    i = parseInt(value = Math.abs(value).toFixed(2)) + '';
+
+    j = ((j = i.length) > 3) ? j % 3 : 0;
+    result = sign + (j ? i.substr(0, j) + separator : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + separator) + (2 ? decimal + Math.abs(value - i).toFixed(2).slice(2) : '');
+    return format.replace(/1.*234.*56/, result);
+  };
 })(jQuery);