ninja-forms-display.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. init_all_the_ninja_things();
  2. function init_all_the_ninja_things() {
  3. // Prevent the enter key from submitting the form.
  4. jQuery(".ninja-forms-form input").bind("keypress", function(e) {
  5. if (e.keyCode == 13) {
  6. var type = jQuery(this).attr("type");
  7. if( type != "textarea" ){
  8. // return false;
  9. }
  10. }
  11. });
  12. /* * * Begin Mask JS * * */
  13. jQuery( document ).ready( function() {
  14. jQuery("div.label-inside input, div.label-inside textarea").focus(function () {
  15. var label = jQuery("#" + this.id + "_label_hidden").val();
  16. if (this.value == label) {
  17. this.value = '';
  18. }
  19. });
  20. jQuery("div.label-inside input, div.label-inside textarea").blur(function () {
  21. var label = jQuery("#" + this.id + "_label_hidden").val();
  22. if (this.value == '') {
  23. this.value = label;
  24. }
  25. });
  26. if (jQuery.fn.mask) {
  27. jQuery(".ninja-forms-mask").each(function () {
  28. var mask = jQuery(this).data('mask');
  29. mask = mask.toString();
  30. jQuery(this).mask(mask);
  31. });
  32. var date_format_mask = ninja_forms_settings.date_format;
  33. date_format_mask = date_format_mask.replace(/m/g, 9);
  34. date_format_mask = date_format_mask.replace(/d/g, 9);
  35. date_format_mask = date_format_mask.replace(/y/g, 99);
  36. date_format_mask = date_format_mask.replace(/Y/g, 9999);
  37. jQuery(".ninja-forms-date").mask(date_format_mask);
  38. }
  39. if (jQuery.fn.datepicker) {
  40. jQuery(".ninja-forms-datepicker").datepicker(ninja_forms_settings.datepicker_args);
  41. }
  42. if (jQuery.fn.autoNumeric) {
  43. jQuery(".ninja-forms-currency").autoNumeric({
  44. aSign: ninja_forms_settings.currency_symbol,
  45. aSep: thousandsSeparator,
  46. aDec: decimalPoint
  47. });
  48. }
  49. });
  50. /* * * End Mask JS * * */
  51. /* * * Begin Help Hover JS * * */
  52. if( jQuery.fn.qtip ){
  53. jQuery(".ninja-forms-help-text").qtip({
  54. style: {
  55. classes: 'qtip-shadow qtip-dark'
  56. }
  57. });
  58. }
  59. /* * * End Help Hover JS * * */
  60. /* * * Begin Character/Word Limit JS * * */
  61. jQuery( document ).ready( function(){
  62. jQuery(".input-limit").each(function() {
  63. var input_limit = jQuery(this).data( 'input-limit' );
  64. var input_limit_type = jQuery(this).data( 'input-limit-type' );
  65. var input_limit_msg = jQuery(this).data( 'input-limit-msg' );
  66. jQuery(this).counter( {
  67. count: 'down',
  68. goal: input_limit,
  69. type: input_limit_type,
  70. msg: input_limit_msg
  71. } );
  72. });
  73. });
  74. /* * * Begin ajaxForms JS * * */
  75. /*
  76. * Attaching events to these elements can allow devs to mimic a priority system for event firing.
  77. * Priority is handled by the element that the event handlers are attached to:
  78. * 1) Event handlers attached to the form itself will be fired first.
  79. * 2) Event handlers attached to the 'body' element will be fired second.
  80. * 3) Event handlers attached to the document element will be fired last.
  81. * These lines give examples of the elements that can have events attached to them for beforeSubmit actions.
  82. jQuery("#ninja_forms_form_2").on('beforeSubmit.example', function(e, formData, jqForm, options){
  83. alert('hello world');
  84. return true;
  85. });
  86. jQuery(".ninja-forms-form").on('beforeSubmit.example', function(e, formData, jqForm, options){
  87. alert('hello world');
  88. return true;
  89. });
  90. jQuery('body').on('beforeSubmit.example', function(e, formData, jqForm, options ){
  91. alert('hello world');
  92. return true;
  93. });
  94. jQuery(document).on('beforeSubmit.example', function(e, formData, jqForm, options ){
  95. alert('world');
  96. return true;
  97. });
  98. * If you want to remove an event handler, you can use the jQuery .off() method.
  99. jQuery(document).off('beforeSubmit.example');
  100. */
  101. jQuery(".ninja-forms-form").each(function(){
  102. var form_id = this.id.replace("ninja_forms_form_", "");
  103. var settings = window['ninja_forms_form_' + form_id + '_settings'];
  104. if ( typeof settings != 'undefined' ) {
  105. ajax = settings.ajax
  106. } else {
  107. ajax = 0;
  108. }
  109. if(ajax == 1){
  110. var options = {
  111. beforeSerialize: function($form, add_product_form_options) {
  112. if ( typeof tinyMCE !== 'undefined' ) {
  113. tinyMCE.triggerSave();
  114. }
  115. },
  116. beforeSubmit: ninja_forms_before_submit,
  117. success: ninja_forms_response,
  118. dataType: 'json'
  119. };
  120. jQuery(this).ajaxForm(options);
  121. // Add our default response handler if "custom" hasn't been selected.
  122. jQuery(this).on('submitResponse.default', function(e, response){
  123. return ninja_forms_default_response( response );
  124. });
  125. // Add our default beforeSubmit handler.
  126. jQuery(this).on('beforeSubmit.default', function(e, formData, jqForm, options){
  127. return ninja_forms_default_before_submit( formData, jqForm, options );
  128. });
  129. } else {
  130. jQuery(this).submit( function(e){
  131. var formData = jQuery(this).serialize();
  132. var jqForm = this;
  133. var options = '';
  134. return ninja_forms_before_submit(formData, jqForm, options);
  135. });
  136. }
  137. });
  138. /* * * End ajaxForm JS * * */
  139. /*
  140. * Password Field JS
  141. */
  142. jQuery('.pass1').val('').keyup( function(){
  143. var pass1 = this.value;
  144. var pass2 = this.id.replace( "pass1", "pass2" );
  145. pass2 = jQuery( "#" + pass2 ).val();
  146. check_pass_strength( pass1, pass2 );
  147. });
  148. jQuery('.pass2').val('').keyup( function(){
  149. var pass2 = this.value;
  150. var pass1 = this.id.replace( "pass2", "pass1" );
  151. pass1 = jQuery( "#" + pass1 ).val();
  152. check_pass_strength( pass1, pass2 );
  153. });
  154. /*
  155. * Timer field JS
  156. */
  157. jQuery('.countdown-timer').each( function( index ) {
  158. jQuery(this).attr('disabled', 'disabled').parents('.field-wrap:first').find('input.no-js').remove();
  159. id = jQuery(this).attr('id');
  160. if ( window.ninja_forms_timer ) window.clearInterval( window.ninja_forms_timer.index );
  161. window.ninja_forms_timer = { index: window.setInterval( function(){ ninja_forms_countdown( id, index ) }, 1000 ) };
  162. });
  163. function ninja_forms_countdown( id, index ){
  164. $countdown = jQuery('#' + id);
  165. counter = parseInt($countdown.data('countdown')) - 1;
  166. $countdown.val(counter).data('countdown', counter).find('span').html(counter);
  167. if ( counter <= 0 ) {
  168. window.clearInterval( window.ninja_forms_timer.index );
  169. $countdown.removeAttr('disabled').html( $countdown.data('text') );
  170. }
  171. }
  172. /*
  173. * Calculation Field JS
  174. */
  175. var calc_fields = jQuery(".ninja-forms-field-calc-listen");
  176. calc_fields.each(function (i, element) {
  177. if ( this.type == 'checkbox' ) {
  178. if ( this.checked ) {
  179. var previousValue = 'checked';
  180. } else {
  181. var previousValue = 'unchecked';
  182. }
  183. } else {
  184. if ( typeof this.type === 'undefined' ) {
  185. var previousValue = jQuery(this).prop('innerHTML');
  186. } else {
  187. var previousValue = jQuery(this).val();
  188. }
  189. }
  190. var form_id = ninja_forms_get_form_id( this );
  191. var field_id = jQuery(this).attr("rel");
  192. var calc_settings = window['ninja_forms_form_' + form_id + '_calc_settings'];
  193. // Get our auto total field or fields.
  194. for ( calc_id in calc_settings.calc_fields ) {
  195. if ( calc_id != field_id ) {
  196. jQuery(element).data(calc_id + "-oldValue", previousValue);
  197. }
  198. }
  199. });
  200. jQuery('body').on( 'focus', '.ninja-forms-field-list-options-span-calc-listen', function(e){
  201. var field_id = jQuery(this).attr("rel");
  202. if ( jQuery("#ninja_forms_field_" + field_id + "_type").val() == 'list' && jQuery("#ninja_forms_field_" + field_id + "_list_type").val() == 'radio' ) {
  203. var form_id = ninja_forms_get_form_id( this );
  204. var calc_settings = window['ninja_forms_form_' + form_id + '_calc_settings'];
  205. // Get our auto total field or fields.
  206. for ( calc_id in calc_settings.calc_fields ) {
  207. if ( calc_id != field_id ) {
  208. jQuery(this).data(calc_id + "-oldValue", jQuery("input[name='ninja_forms_field_" + field_id +"']:checked").val());
  209. }
  210. }
  211. }
  212. });
  213. jQuery('body').on( 'mousedown', '.ninja-forms-field-list-options-span-calc-listen', function(e){
  214. var field_id = jQuery(this).attr("rel");
  215. if ( jQuery("#ninja_forms_field_" + field_id + "_type").val() == 'list' && jQuery("#ninja_forms_field_" + field_id + "_list_type").val() == 'radio' ) {
  216. var form_id = ninja_forms_get_form_id( this );
  217. var calc_settings = window['ninja_forms_form_' + form_id + '_calc_settings'];
  218. // Get our auto total field or fields.
  219. for ( calc_id in calc_settings.calc_fields ) {
  220. if ( calc_id != field_id ) {
  221. jQuery(this).data(calc_id + "-oldValue", jQuery("input[name='ninja_forms_field_" + field_id +"']:checked").val());
  222. }
  223. }
  224. }
  225. });
  226. // Listen to the input elements for our auto-calculation fields and change the total.
  227. jQuery('body').on( 'change', '.ninja-forms-field-calc-listen', function(event){
  228. if ( this == event.target ) {
  229. // Get our calc settings.
  230. var form_id = ninja_forms_get_form_id( this );
  231. var field_id = jQuery(this).attr("rel");
  232. var calc_settings = window['ninja_forms_form_' + form_id + '_calc_settings'];
  233. var visible = jQuery("#ninja_forms_field_" + field_id + "_div_wrap").data("visible");
  234. // Get our auto total field or fields.
  235. for ( calc_id in calc_settings.calc_fields ) {
  236. if ( calc_id != field_id ) {
  237. var calc_method = calc_settings.calc_fields[calc_id]['method'];
  238. var calc_places = calc_settings.calc_fields[calc_id]['places'];
  239. // Find out which calc_method we're using.
  240. // If we're using the 'fields' method, bail if the current field isn't in our field list.
  241. if ( calc_method == 'fields' ) {
  242. var change = false;
  243. for (var i = calc_settings.calc_fields[calc_id]['fields'].length - 1; i >= 0; i--) {
  244. if ( calc_settings.calc_fields[calc_id]['fields'][i]['field'] == field_id ) {
  245. change = true;
  246. break;
  247. }
  248. };
  249. } else if ( calc_method == 'eq' ) { // If we're using the 'eq' method, make sure that our current field is in our equation
  250. var change = false;
  251. if ( typeof calc_settings.calc_fields[calc_id]['fields'] !== 'undefined' ) {
  252. for (var i = calc_settings.calc_fields[calc_id]['fields'].length - 1; i >= 0; i--) {
  253. if ( calc_settings.calc_fields[calc_id]['fields'][i] == field_id ) {
  254. change = true;
  255. break;
  256. }
  257. };
  258. }
  259. }
  260. if ( ( ( calc_method == 'fields' || calc_method == 'eq' ) && change ) || calc_method == 'auto' ) {
  261. if ( calc_method == 'auto' || calc_method == 'fields' ) { // Method: auto or fields
  262. // Loop through our calc fields and check to see if they are set to auto. If they are, perform the auto totalling actions.
  263. var key = jQuery(this).val();
  264. var new_value = '';
  265. // Set our old_value to the previous one for this field.
  266. old_value = jQuery(this).data(calc_id + '-oldValue');
  267. // Check to see if we are in a list field. If we are, we can grab the calc values.
  268. if ( jQuery('#ninja_forms_field_' + field_id + '_type' ).val() == 'list' ) {
  269. var key = jQuery(this).val();
  270. // See if we have any old values. If we do, compare them to our current selection for this field and see if we need to subtract anything.
  271. if ( jQuery('#ninja_forms_field_' + field_id + '_list_type').val() == 'checkbox' ) {
  272. if ( this.checked ) {
  273. jQuery(this).data(calc_id + '-oldValue', 'checked' );
  274. } else {
  275. jQuery(this).data(calc_id + '-oldValue', 'unchecked' );
  276. }
  277. if ( this.checked && visible == 1 ) {
  278. // If this is checked AND visible, we don't want to do any old operations
  279. old_value = 0;
  280. } else if ( this.checked && visible != 1 ) {
  281. // If this is checked AND hidden, we want to perform an old operation on it.
  282. old_value = key;
  283. new_value = 0;
  284. } else if ( !this.checked && visible == 1 ) {
  285. if ( old_value == 'checked' ) {
  286. old_value = key;
  287. } else {
  288. old_value = 0;
  289. }
  290. new_value = 0;
  291. } else if ( !this.checked && visible != 1 ) {
  292. if ( old_value == 'checked' ) {
  293. old_value = key;
  294. } else {
  295. old_value = 0;
  296. }
  297. new_value = 0;
  298. }
  299. } else if ( jQuery('#ninja_forms_field_' + field_id + '_list_type').val() == 'radio' ) {
  300. // If this is a checkbox or a radio list, then we have to check the span parent for the oldValue.
  301. var span = jQuery(this).parent().parent().parent().parent();
  302. old_value = jQuery(span).data(calc_id + '-oldValue');
  303. if ( typeof old_value === 'undefined' ) {
  304. if ( this.checked ) {
  305. old_value = jQuery(this).val();
  306. }
  307. }
  308. if ( this.checked && visible == 1 ) {
  309. if ( old_value == key ) {
  310. old_value = 0;
  311. }
  312. } else if ( this.checked && visible != 1 ) {
  313. new_value = 0;
  314. } else if ( !this.checked ) {
  315. old_value = 0;
  316. new_value = 0;
  317. }
  318. if ( this.checked ) {
  319. jQuery(span).data(calc_id + '-oldValue', key );
  320. }
  321. } else if ( jQuery('#ninja_forms_field_' + field_id + '_list_type').val() == 'multi' ) {
  322. // This is a multi-select list. The value is in an array, so we need to add all the values together.
  323. if ( jQuery.isArray( key ) ) {
  324. var tmp = 0;
  325. for (var i = key.length - 1; i >= 0; i--) {
  326. if ( typeof calc_settings.calc_value[field_id][key[i]] !== 'undefined' ) {
  327. tmp += parseFloat( calc_settings.calc_value[field_id][key[i]] );
  328. }
  329. };
  330. new_value = tmp;
  331. }
  332. if ( jQuery.isArray( old_value ) ) {
  333. var tmp = 0;
  334. for (var i = old_value.length - 1; i >= 0; i--) {
  335. if ( typeof calc_settings.calc_value[field_id][old_value[i]] !== 'undefined' ) {
  336. tmp += parseFloat( calc_settings.calc_value[field_id][old_value[i]] );
  337. }
  338. };
  339. old_value = tmp;
  340. }
  341. } else {
  342. // This is a select list, so we can just grab the oldValue from this field.
  343. var old_value = jQuery(this).data(calc_id + '-oldValue');
  344. }
  345. // Check to see if we're in a checkbox field. If so, the key needs to be based on checked or unchecked, not value.
  346. } else if ( jQuery('#ninja_forms_field_' + field_id + '_type').val() == 'checkbox' ) {
  347. if ( this.checked && visible == 1 ) {
  348. // This field is checked, so set key to 'checked.'
  349. var key = 'checked';
  350. // Checkboxes only have two states, so if we are changing, the previous value must have been the opposite of this one.
  351. old_value = 'unchecked';
  352. } else if( this.checked && visible != 1 ) {
  353. var key = 'unchecked';
  354. if ( jQuery(this).data(calc_id + '-oldValue') == 'checked' || typeof jQuery(this).data(calc_id + '-oldValue') === 'undefined' ){
  355. old_value = 'checked';
  356. } else {
  357. old_value = 0;
  358. }
  359. } else if( !this.checked && visible != 1 ) {
  360. var key = 'unchecked';
  361. old_value = 0;
  362. } else {
  363. var key = 'unchecked';
  364. if ( jQuery(this).data(calc_id + '-oldValue') == 'checked' || typeof jQuery(this).data(calc_id + '-oldValue') === 'undefined' ){
  365. old_value = 'checked';
  366. } else {
  367. old_value = 0;
  368. }
  369. }
  370. } else if ( jQuery('#ninja_forms_field_' + field_id + '_type').val() == 'calc' ) {
  371. if ( key == '' ) {
  372. key = jQuery('#ninja_forms_field_' + field_id).prop('innerHTML');
  373. }
  374. }
  375. if ( new_value === '' ) {
  376. if ( typeof calc_settings.calc_value[field_id] !== 'undefined' && typeof calc_settings.calc_value[field_id][key] !== 'undefined' ) {
  377. // Get our calc value for this field from our previously defined JS object.
  378. var new_value = calc_settings.calc_value[field_id][key];
  379. } else {
  380. // This field doesn't exist in the calc value object. It's either a textbox or similar element.
  381. if ( typeof this.type === 'undefined' ) {
  382. var new_value = this.innerHTML;
  383. } else {
  384. var new_value = this.value;
  385. }
  386. if ( typeof ninja_forms_settings.currency_symbol !== 'undefined' ) {
  387. // Strip the Currency Symbol
  388. f_value = new_value.replace( ninja_forms_settings.currency_symbol, "" );
  389. // Strip the Thousands Separator
  390. f_value = f_value.replace( /thousandsSeparator/g, "" );
  391. // If the Decimal Point is not `.`
  392. if ( '.' != decimalPoint ) {
  393. // Replace the Decimal Point
  394. f_value = f_value.replace( decimalPoint, "." );
  395. }
  396. }
  397. if ( isNaN( new_value ) ) {
  398. new_value = 0;
  399. }
  400. }
  401. }
  402. //console.log(field_id + ':' + old_value);
  403. // Check to see if our old_value exists in the calc_value JS object.
  404. if ( typeof calc_settings.calc_value[field_id] !== 'undefined' && typeof calc_settings.calc_value[field_id][old_value] !== 'undefined' ) {
  405. // Grab our calc value form the calc_value JS object.
  406. old_value = calc_settings.calc_value[field_id][old_value];
  407. } else {
  408. // Our calc_value doesn't exist in the calc_value JS object.
  409. // Check to see if our old_value is an array. This would be the case if the field is a multi-select.
  410. if ( old_value == '' || typeof old_value === 'undefined' ) {
  411. // We aren't dealing with an old_value array and old_value isn't a number. Set it to 0.
  412. old_value = 0;
  413. } else {
  414. if ( isNaN( old_value ) ) {
  415. if ( typeof ninja_forms_settings.currency_symbol !== 'undefined' ) {
  416. old_value = old_value.replace( ninja_forms_settings.currency_symbol, "" );
  417. old_value = old_value.replace( /,/g, "" );
  418. }
  419. }
  420. }
  421. }
  422. // Find out what kind of element our total field is - either span or textbox.
  423. // Set our current value.
  424. if(jQuery("#ninja_forms_field_" + calc_id).attr("type") == 'text' ){
  425. var current_value = jQuery("#ninja_forms_field_" + calc_id).val();
  426. }else{
  427. var current_value = jQuery("#ninja_forms_field_" + calc_id).html();
  428. }
  429. // Strip the Thousands Separator
  430. current_value = current_value.replace( /thousandsSeparator/g, "" );
  431. // If the Decimal Point is not `.`
  432. if ( '.' != decimalPoint ) {
  433. // Replace the Decimal Point
  434. current_value = current_value.replace( decimalPoint, "." );
  435. }
  436. // Make sure that our current total is made up of numbers.
  437. if ( typeof ninja_forms_settings.currency_symbol !== 'undefined' && typeof current_value != 'undefined' ) {
  438. // Strip the Currency Symbol
  439. f_value = current_value.replace( ninja_forms_settings.currency_symbol, "" );
  440. // Strip the Thousands Separator
  441. f_value = f_value.replace( /thousandsSeparator/g, "" );
  442. // If the Decimal Point is not `.`
  443. if ( '.' != decimalPoint ) {
  444. // Replace the Decimal Point
  445. f_value = f_value.replace( decimalPoint, "." );
  446. }
  447. }
  448. if ( !isNaN( current_value ) ) {
  449. // Convert those string numbers into operable ones.
  450. current_value = parseFloat( current_value );
  451. } else {
  452. // Our current total isn't made up of numbers, so set the current total to 0.
  453. current_value = 0;
  454. }
  455. // Find out what calculation method our calc field is using and set our math operations appropriately.
  456. if ( calc_method == 'auto' ) { // Method: auto
  457. // If we are using the 'auto' method, then the calc is an auto-total field. We're always adding.
  458. var old_op = 'subtract';
  459. var new_op = 'add';
  460. if ( !jQuery(this).hasClass('ninja-forms-field-calc-auto') ) {
  461. old_value = '';
  462. new_value = '';
  463. }
  464. } else if( calc_method == 'fields' ) { // Method: fields
  465. // If we are using the 'fields' method, then figure out what the operators should be from the JS object.
  466. for (var i = calc_settings.calc_fields[calc_id]['fields'].length - 1; i >= 0; i--) {
  467. if ( calc_settings.calc_fields[calc_id]['fields'][i]['field'] == field_id ) {
  468. var old_op = ninja_forms_find_opposite_op( calc_settings.calc_fields[calc_id]['fields'][i]['op'] );
  469. var new_op = calc_settings.calc_fields[calc_id]['fields'][i]['op'];
  470. }
  471. };
  472. }
  473. // If our old value exists and isn't empty or 0, then carry out the old_op on it.
  474. if ( old_value && !isNaN( old_value ) && old_value != 0 && old_value != '' && !jQuery(this).hasClass('ninja-forms-field-calc-no-old-op') ) {
  475. old_value = parseFloat( old_value );
  476. var asdf = current_value;
  477. tmp = new ninja_forms_var_operator(old_op);
  478. current_value = tmp.evaluate( current_value, old_value );
  479. //console.log( this.id + ' - ' + asdf + ' ' + old_op + ' ' + old_value + ' = ' + current_value );
  480. }
  481. // If our new value exists and isn't empty or 0, then carry out the new_op on it.
  482. if ( new_value && !isNaN( new_value ) && new_value != 0 && new_value != '' && !jQuery(this).hasClass('ninja-forms-field-calc-no-new-op') ) {
  483. new_value = parseFloat( new_value );
  484. tmp = new ninja_forms_var_operator(new_op);
  485. var calc_value = tmp.evaluate( current_value, new_value );
  486. //console.log( this.id + ' - ' + current_value + ' ' + new_op + ' ' + new_value + ' = ' + calc_value );
  487. } else {
  488. // We don't have any calculations to do, so set calc_value to our current_value.
  489. var calc_value = current_value;
  490. }
  491. } else if ( calc_method == 'eq' ) { // Method: eq.
  492. var tmp_eq = calc_settings.calc_fields[calc_id]['eq'];
  493. // Loop through our fields getting their values and replacing their placeholders in the equation.
  494. for (var i = calc_settings.calc_fields[calc_id]['fields'].length - 1; i >= 0; i--) {
  495. // Make sure that the changed field is in the formula and that we should change the current value.
  496. var f_id = calc_settings.calc_fields[calc_id]['fields'][i];
  497. var key = jQuery("#ninja_forms_field_" + f_id).val();
  498. var f_value = '';
  499. if ( jQuery('#ninja_forms_field_' + f_id + '_type' ).val() == 'list' ) {
  500. if ( jQuery('#ninja_forms_field_' + f_id + '_list_type').val() == 'radio' ) {
  501. key = jQuery('.ninja-forms-field-' + f_id + '-options :checked').val();
  502. } else if ( jQuery('#ninja_forms_field_' + f_id + '_list_type').val() == 'multi' ) {
  503. // If we're working with a multi-select list, we need to add all of these values together before we continue our calculation.
  504. if ( jQuery.isArray( key ) ) {
  505. var tmp = 0;
  506. for (var x = key.length - 1; x >= 0; x--) {
  507. if ( typeof calc_settings.calc_value[f_id][key[x]] !== 'undefined' ) {
  508. tmp += parseFloat( calc_settings.calc_value[f_id][key[x]] );
  509. }
  510. };
  511. f_value = tmp;
  512. }
  513. } else if ( jQuery('#ninja_forms_field_' + f_id + '_list_type').val() == 'checkbox' ) {
  514. var tmp = 0;
  515. jQuery('.ninja-forms-field-' + f_id + '-options :checked').each(function(){
  516. if ( typeof calc_settings.calc_value[f_id][this.value] !== 'undefined' ) {
  517. tmp += parseFloat( calc_settings.calc_value[f_id][this.value] );
  518. }
  519. });
  520. f_value = tmp;
  521. }
  522. } else if ( jQuery('#ninja_forms_field_' + f_id + '_type').val() == 'checkbox' ) {
  523. if ( jQuery('#ninja_forms_field_' + f_id).attr('checked') ) {
  524. // This field is checked, so set key to 'checked.'
  525. var key = 'checked';
  526. } else {
  527. var key = 'unchecked';
  528. }
  529. } else if ( jQuery('#ninja_forms_field_' + f_id + '_type').val() == 'calc' ) {
  530. if ( key == '' ) {
  531. f_value = jQuery("#ninja_forms_field_" + f_id).prop('innerHTML');
  532. }
  533. }
  534. if ( f_value == '' ) {
  535. if ( typeof calc_settings.calc_value[f_id] !== 'undefined' && typeof calc_settings.calc_value[f_id][key] !== 'undefined' ) {
  536. f_value = calc_settings.calc_value[f_id][key];
  537. //console.log( 'here: ' + f_value );
  538. } else {
  539. f_value = key;
  540. }
  541. }
  542. // Check for a percentage sign in our f_value. If we find one, then convert it to a decimal.
  543. if ( typeof f_value !== 'undefined' && typeof f_value === 'string' ) {
  544. if ( f_value.indexOf("%") >= 0 ) {
  545. f_value = f_value.replace( "%", "" );
  546. if ( !isNaN( f_value ) ) {
  547. f_value = parseFloat( f_value ) / 100;
  548. }
  549. }
  550. }
  551. // This field doesn't exist in the calc value object. It's either a textbox or similar element.
  552. if ( typeof this.type === 'undefined' && key == '' ) {
  553. f_value = this.innerHTML;
  554. //console.log( 'undefined: ' + f_value );
  555. }
  556. if ( typeof ninja_forms_settings.currency_symbol !== 'undefined' && isNaN( f_value ) && typeof f_value != 'undefined' ) {
  557. // Strip the Currency Symbol
  558. f_value = f_value.replace( ninja_forms_settings.currency_symbol, "" );
  559. // Strip the Thousands Separator
  560. f_value = f_value.replace( /thousandsSeparator/g, "" );
  561. // If the Decimal Point is not `.`
  562. if ( '.' != decimalPoint ) {
  563. // Replace the Decimal Point
  564. f_value = f_value.replace( decimalPoint, "." );
  565. }
  566. }
  567. if ( isNaN( f_value ) || f_value == '' || !f_value || typeof f_value === 'undefined' ) {
  568. f_value = 0;
  569. }
  570. var find = 'field_' + f_id;
  571. var re = new RegExp(find, 'g');
  572. tmp_eq = tmp_eq.replace(re, f_value);
  573. }
  574. var calc_value = eval(tmp_eq);
  575. }
  576. // Find out what kind of element our total field is - either span or textbox.
  577. // Set our current value.
  578. if(jQuery("#ninja_forms_field_" + calc_id).attr("type") == 'text' ){
  579. var current_value = jQuery("#ninja_forms_field_" + calc_id).val();
  580. }else{
  581. var current_value = jQuery("#ninja_forms_field_" + calc_id).html();
  582. }
  583. // Make sure that our current total is made up of numbers.
  584. if ( typeof ninja_forms_settings.currency_symbol !== 'undefined' && typeof current_value != 'undefined' ) {
  585. current_value = current_value.replace( ninja_forms_settings.currency_symbol, "" );
  586. }
  587. if ( !isNaN( current_value ) ) {
  588. // Convert those string numbers into operable ones.
  589. current_value = parseFloat( current_value );
  590. } else {
  591. // Our current total isn't made up of numbers, so set the current total to 0.
  592. current_value = 0;
  593. }
  594. if ( current_value !== calc_value ) {
  595. if ( jQuery('#ninja_forms_field_' + field_id + '_list_type').val() != 'checkbox' ) {
  596. //console.log( 'set old value ' + field_id + ':' + key );
  597. jQuery(this).data(calc_id + '-oldValue', key);
  598. }
  599. if ( jQuery('#ninja_forms_field_' + field_id + '_list_type').val() == 'checkbox' || jQuery('#ninja_forms_field_' + field_id + '_list_type').val() == 'radio' ) {
  600. jQuery("#ninja_forms_field_" + field_id + "_div_wrap").find(".ninja-forms-field").each(function(){
  601. jQuery(this).removeClass('ninja-forms-field-calc-no-old-op');
  602. });
  603. } else {
  604. jQuery(this).removeClass('ninja-forms-field-calc-no-old-op');
  605. }
  606. calc_value = calc_value.toFixed(calc_places);
  607. // If the Decimal Point is not `.`
  608. if ( '.' != decimalPoint ) {
  609. // Replace the Decimal Point
  610. calc_value = calc_value.toString().replace( ".", decimalPoint );
  611. }
  612. // Set the value of our calculation field.
  613. if(jQuery("#ninja_forms_field_" + calc_id).attr("type") == 'text' ){
  614. jQuery("#ninja_forms_field_" + calc_id).val(calc_value);
  615. }else{
  616. jQuery("#ninja_forms_field_" + calc_id).html(calc_value);
  617. }
  618. //if( typeof calc_settings.calc_fields[field_id] === 'undefined' ) {
  619. jQuery("#ninja_forms_field_" + calc_id).trigger('change');
  620. //}
  621. }
  622. }
  623. }
  624. }
  625. }
  626. });
  627. }
  628. function ninja_forms_before_submit( formData, jqForm, options ){
  629. var form_id = jQuery( jqForm ).prop( 'id' ).replace( 'ninja_forms_form_', '' );
  630. jQuery( '#nf_submit_' + form_id ).hide();
  631. jQuery( '#nf_processing_' + form_id ).show();
  632. jQuery( document ).data( 'submit_action', 'submit' );
  633. var result = jQuery(jqForm).triggerHandler('beforeSubmit', [ formData, jqForm, options ]);
  634. if ( result !== false ) {
  635. result = jQuery('body').triggerHandler('beforeSubmit', [ formData, jqForm, options ]);
  636. }
  637. if( result !== false ) {
  638. result = jQuery(document).triggerHandler('beforeSubmit', [ formData, jqForm, options ]);
  639. }
  640. return result;
  641. }
  642. function ninja_forms_response(responseText, statusText, xhr, jQueryform){
  643. //alert(responseText);
  644. var form_id = responseText.form_id;
  645. jQuery( '#nf_processing_' + form_id ).hide();
  646. jQuery( '#nf_submit_' + form_id ).show();
  647. if( ninja_forms_settings.ajax_msg_format == 'inline' ){
  648. var result = jQuery(jQueryform).triggerHandler('submitResponse', [ responseText ]);
  649. if ( result !== false ) {
  650. result = jQuery('body').triggerHandler('submitResponse', [ responseText ]);
  651. }
  652. if( result !== false ) {
  653. result = jQuery(document).triggerHandler('submitResponse', [ responseText ]);
  654. }
  655. return result;
  656. }
  657. }
  658. function ninja_forms_default_before_submit(formData, jqForm, options){
  659. var form_id = jQuery(jqForm).prop("id").replace("ninja_forms_form_", "" );
  660. // Show the ajax spinner and processing message.
  661. //jQuery("#ninja_forms_form_" + form_id + "_process_msg").show();
  662. jQuery("#ninja_forms_form_" + form_id + "_response_msg").prop("innerHTML", "");
  663. jQuery("#ninja_forms_form_" + form_id + "_response_msg").removeClass("ninja-forms-error-msg");
  664. jQuery("#ninja_forms_form_" + form_id + "_response_msg").removeClass("ninja-forms-success-msg");
  665. jQuery(".ninja-forms-field-error").prop("innerHTML", "");
  666. jQuery(".ninja-forms-error").removeClass("ninja-forms-error");
  667. return true;
  668. }
  669. function ninja_forms_default_response(response){
  670. var form_id = response.form_id;
  671. ninja_forms_update_error_msgs(response);
  672. ninja_forms_update_success_msg(response);
  673. if ( response.errors == false && typeof response.form_settings['landing_page'] != 'undefined' && response.form_settings['landing_page'] != '' ) {
  674. window.location = response.form_settings['landing_page'];
  675. }
  676. return true;
  677. }
  678. function ninja_forms_update_success_msg(response){
  679. var innerHTML = '';
  680. var form_id = response.form_id;
  681. var success = response.success;
  682. //alert(success);
  683. var form_settings = response.form_settings;
  684. var hide_complete = form_settings.hide_complete;
  685. var clear_complete = form_settings.clear_complete;
  686. if(success != false){
  687. for( var propName in success ){
  688. innerHTML += '<p>' + success[propName] + '</p>';
  689. }
  690. if(innerHTML != ''){
  691. jQuery("#ninja_forms_form_" + form_id + "_response_msg").removeClass("ninja-forms-error-msg")
  692. jQuery("#ninja_forms_form_" + form_id + "_response_msg").addClass("ninja-forms-success-msg")
  693. jQuery("#ninja_forms_form_" + form_id + "_response_msg").prop("innerHTML", innerHTML);
  694. jQuery("#ninja_forms_form_" + form_id + "_response_msg").show();
  695. }
  696. if(hide_complete == 1 ){
  697. jQuery("#ninja_forms_form_" + form_id ).hide();
  698. }
  699. if(clear_complete == 1 ){
  700. jQuery("#ninja_forms_form_" + form_id ).clearForm();
  701. // Replace any inside labels.
  702. jQuery( 'div.label-inside input.ninja-forms-field, div.label-inside textarea.ninja-forms-field' ).each( function() {
  703. var label = jQuery("#" + this.id + "_label_hidden").val();
  704. this.value = label;
  705. });
  706. if( 'rating' in jQuery("input[type=radio].ninja-forms-star") ) {
  707. jQuery("input[type=radio].ninja-forms-star").rating("drain");
  708. }
  709. }
  710. }
  711. }
  712. function ninja_forms_update_error_msgs(response){
  713. var innerHTML = '';
  714. var form_id = response.form_id;
  715. var errors = response.errors;
  716. if(errors != false){
  717. for( var propName in errors ){
  718. if(errors[propName]['location'] == 'general' ){
  719. innerHTML += '<p>' + errors[propName]['msg'] + '</p>';
  720. }else{
  721. var field_id = errors[propName]['location'];
  722. jQuery("#ninja_forms_field_" + field_id + "_error").show();
  723. jQuery("#ninja_forms_field_" + field_id + "_error").prop("innerHTML", errors[propName]['msg']);
  724. jQuery("#ninja_forms_field_" + field_id + "_div_wrap").addClass("ninja-forms-error");
  725. }
  726. }
  727. if(innerHTML != ''){
  728. jQuery("#ninja_forms_form_" + form_id + "_response_msg").removeClass("ninja-forms-success-msg")
  729. jQuery("#ninja_forms_form_" + form_id + "_response_msg").addClass("ninja-forms-error-msg")
  730. jQuery("#ninja_forms_form_" + form_id + "_response_msg").prop("innerHTML", innerHTML);
  731. }
  732. }
  733. }
  734. function ninja_forms_html_decode(value) {
  735. if (value) {
  736. var decoded = jQuery('<div />').html(value).text();
  737. decoded = jQuery('<div />').html(decoded).text();
  738. return decoded;
  739. } else {
  740. return '';
  741. }
  742. }
  743. function ninja_forms_toggle_login_register(form_type, form_id) {
  744. var el_id = 'ninja_forms_form_' + form_id + '_' + form_type + '_form';
  745. if(form_type == 'login'){
  746. var opp_id = 'ninja_forms_form_' + form_id + '_register_form';
  747. }else{
  748. var opp_id = 'ninja_forms_form_' + form_id + '_login_form';
  749. }
  750. var ele = document.getElementById(el_id);
  751. var opp_ele = document.getElementById(opp_id);
  752. if(ele.style.display == "block") {
  753. ele.style.display = "none";
  754. }else{
  755. ele.style.display = "block";
  756. opp_ele.style.display = "none";
  757. }
  758. }
  759. function ninja_forms_get_form_id(element){
  760. var form_id = jQuery(element).closest('form').prop("id");
  761. if ( 'undefined' === typeof form_id ) {
  762. return false;
  763. }
  764. form_id = form_id.replace("ninja_forms_form_", "");
  765. if(form_id == '' || form_id == 'ninja_forms_admin'){
  766. form_id = jQuery("#_form_id").val();
  767. }
  768. return form_id;
  769. }
  770. function check_pass_strength(pass1, pass2) {
  771. jQuery('#pass-strength-result').removeClass('short bad good strong');
  772. if ( ! pass1 ) {
  773. jQuery('#pass-strength-result').html( ninja_forms_password_strength.empty );
  774. return;
  775. }
  776. strength = passwordStrength(pass1, pass2);
  777. switch ( strength ) {
  778. case 2:
  779. jQuery('#pass-strength-result').addClass('bad').html( ninja_forms_password_strength['bad'] );
  780. break;
  781. case 3:
  782. jQuery('#pass-strength-result').addClass('good').html( ninja_forms_password_strength['good'] );
  783. break;
  784. case 4:
  785. jQuery('#pass-strength-result').addClass('strong').html( ninja_forms_password_strength['strong'] );
  786. break;
  787. case 5:
  788. jQuery('#pass-strength-result').addClass('short').html( ninja_forms_password_strength['mismatch'] );
  789. break;
  790. default:
  791. jQuery('#pass-strength-result').addClass('short').html( ninja_forms_password_strength['short'] );
  792. }
  793. }
  794. function passwordStrength(password1, password2) {
  795. var shortPass = 1, badPass = 2, goodPass = 3, strongPass = 4, mismatch = 5, symbolSize = 0, natLog, score;
  796. // password 1 != password 2
  797. if ( (password1 != password2) && password2.length > 0)
  798. return mismatch
  799. //password < 4
  800. if ( password1.length < 4 )
  801. return shortPass
  802. //password1 == username
  803. if ( password1.match(/[0-9]/) )
  804. symbolSize +=10;
  805. if ( password1.match(/[a-z]/) )
  806. symbolSize +=26;
  807. if ( password1.match(/[A-Z]/) )
  808. symbolSize +=26;
  809. if ( password1.match(/[^a-zA-Z0-9]/) )
  810. symbolSize +=31;
  811. natLog = Math.log( Math.pow(symbolSize, password1.length) );
  812. score = natLog / Math.LN2;
  813. if (score < 40 )
  814. return badPass
  815. if (score < 56 )
  816. return goodPass
  817. return strongPass;
  818. }
  819. function ninja_forms_find_opposite_op(op) {
  820. switch(op){
  821. case "add":
  822. return "subtract";
  823. case "subtract":
  824. return "add";
  825. case "multiply":
  826. return "divide";
  827. case "divide":
  828. return "multiply";
  829. }
  830. }
  831. function ninja_forms_var_operator(op) {
  832. this.operation = op;
  833. this.evaluate = function evaluate(param1, param2) {
  834. switch(this.operation) {
  835. case "add":
  836. return param1 + param2;
  837. case "subtract":
  838. return param1 - param2;
  839. case "multiply":
  840. return param1 * param2;
  841. case "divide":
  842. return param1 / param2;
  843. }
  844. }
  845. }