text: /*
   2017 (c) Sahana Software Foundation
   @license MIT
  
   requires jQuery 1.9.1+
   requires jQuery UI 1.10 widget factory
  */
  var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.owns=function(b,k){return Object.prototype.hasOwnProperty.call(b,k)};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(b,k,m){b!=Array.prototype&&b!=Object.prototype&&(b[k]=m.value)};
  $jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global&&null!=global?global:b};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(b,k,m,l){if(k){m=$jscomp.global;b=b.split(".");for(l=0;l<b.length-1;l++){var a=b[l];a in m||(m[a]={});m=m[a]}b=b[b.length-1];l=m[b];k=k(l);k!=l&&null!=k&&$jscomp.defineProperty(m,b,{configurable:!0,writable:!0,value:k})}};
  $jscomp.polyfill("Object.values",function(b){return b?b:function(b){var k=[],l;for(l in b)$jscomp.owns(b,l)&&k.push(b[l]);return k}},"es8","es3");
  (function(b,k){var m=function(a,b){var d=function(a,b){return b?encodeURIComponent(a)+"="+encodeURIComponent(b):""};a=a.split("?");a=[a[0]].concat(a[1].split("#"));var c=a[1],f,g={},h;c=c?c.split("&").map(function(a){f=decodeURIComponent(a.split("=")[0]);return b.hasOwnProperty(f)?(g[f]=!0,d(f,b[f])):a}).filter(function(a){return a}):[];for(f in b)g[f]||(h=d(f,b[f]))&&c.push(h);h=a[0];c.length&&(h+="?"+c.join("&"));a[2]&&(h+="#"+a[2]);return h};S3.addPersonWidgetReady=function(a){var c=new jQuery.Deferred,
  d=b("#"+a);if(d.data("lookup_contact")!==k)c.resolve(d);else d.on("addPersonReady",function(){c.resolve(d)});return c.promise()};var l=0;b.widget("s3.addPerson",{options:{lookupDuplicates:!1,separateNameFields:!1,trigger:"full_name",c:"pr",f:"person",chars:2,delay:800,downIcon:"fa fa-caret-down",yesIcon:"fa fa-check",noIcon:"fa fa-remove"},_create:function(){this.id=l;l+=1;this.eventNamespace=".addPerson"},_init:function(){var a=b(this.element),c=this.options;this.fieldName=a=a.attr("id");this.selector=
  "#"+a;this.data={};a=b(this.selector+"__row").hide();a.hasClass("form-row")||a.hasClass("control-group")?this.formStyle="div":this.formStyle="table";this.topFields=["organisation_id","pe_label"];a=b(this.selector+"_pe_label");a.length&&(this.idLabel=a);a=c.trigger;if(c.separateNameFields)if("first_name"==a)var d=["first_name","middle_name","last_name"];else a="last_name",d=["last_name","middle_name","first_name"];else a="full_name",d=["full_name"];c.trigger=a;this.triggerRow=b(this._rowSelector(a));
  this.trigger=b(this.selector+"_"+a);this.nameFields=d;this.personFields="father_name grandfather_name year_of_birth date_of_birth gender occupation mobile_phone home_phone email".split(" ");this._arrangeFormRows();this.refresh()},_destroy:function(){b.Widget.prototype.destroy.call(this)},refresh:function(){var a=b(this.element),c=this.selector,d=this.options;this._unbindEvents();this.pendingAC=!1;var e=this.throbber;e&&e.remove();e=b('<div class="throbber input_throbber"></div>');e.hide().insertAfter(this.trigger);
  this.throbber=e;var f=this.idLabel;f&&(e=b('<div class="throbber input_throbber"></div>'),e.hide().insertAfter(f),this.idLabelThrobber=e);(e=a.val())?(isNaN(e-0)||(this.data={id:e},this._serialize()),this.data.id?(this._disableInputs(),this._toggleActions({cancel:!1})):(this._enableAutocomplete(),this._toggleActions({edit:!1}))):(this._serialize(),this._enableAutocomplete(),this._toggleActions({edit:!1}));b(c+"_edit_bar").removeClass("hide").show();d.lookupDuplicates&&(d=this.fieldName+"_duplicates",
  b("#"+d).length||(d='<div id="'+d+'" class="req"></div>',"div"==this.formStyle?b(c+"_box_bottom").before(d):b(c+"_box_bottom1").before(d)));this._bindEvents();a.trigger("addPersonReady")},_arrangeFormRows:function(){var a=b(this.element),c=this.selector,d=b(c+"__row").hide();if("div"==this.formStyle){b(c+"_box_bottom").removeClass("hide").show().insertAfter(d);var e=this,f=this.topFields.concat(this.nameFields).concat(this.personFields).reverse();f.forEach(function(a){d.after(b(e._rowSelector(a)))});
  b(f.map(this._rowSelector,this).join(",")).removeClass("hide").show()}else b(c+"__row1").hide();c=b(c+"_title__row");a=a.next(".error_wrapper");d.after(a).after(c.removeClass("hide").show())},_allFields:function(){return this.topFields.concat(this.nameFields).concat(this.personFields)},_fieldMap:function(){var a={pe_label:"pe_label",email:"email",mobile_phone:"mphone",home_phone:"hphone",gender:"sex",date_of_birth:"dob",year_of_birth:"year_of_birth",father_name:"father_name",grandfather_name:"grandfather_name",
  occupation:"occupation",organisation_id:"org_id"};this.options.separateNameFields&&(a.first_name="first_name",a.middle_name="middle_name",a.last_name="last_name");return a},_rowSelector:function(a){return this.selector+"_"+a+"__row"},_readInputs:function(){var a=this.selector,c={},d;this._allFields().forEach(function(e){d=b(a+"_"+e);d.length&&(c[e]=d.val())});this.data=c},_updateInputs:function(){var a=this.selector,c=this.data,d;this._allFields().forEach(function(e){d=b(a+"_"+e);d.length&&c.hasOwnProperty(e)&&
  d.val(c[e])})},_serialize:function(){b(this.element).val(JSON.stringify(this.data))},_deserialize:function(){var a=b(this.element).val(),c=a-0;this.data=isNaN(c)?JSON.parse(a):{id:c}},_toggleActions:function(a){var c={edit:".edit-action",cancel:".cancel-action"},d=this.selector,e;for(e in a){var f=b(d+"_edit_bar "+c[e]);a[e]?f.removeClass("hide").show():f.hide()}},_toggleDatePickerTrigger:function(a){var c=b(this.selector+"_date_of_birth__row .calendar-clear-btn"),d=b(this.selector+"_date_of_birth__row .ui-datepicker-trigger");
  a?(d.removeClass("hide").show(),c.removeClass("hide").show()):(d.hide(),c.hide())},_enableInputs:function(){var a=this.selector;this._allFields().forEach(function(c){b(a+"_"+c).prop("disabled",!1)});this._toggleDatePickerTrigger(!0)},_disableInputs:function(){var a=this.selector;this._allFields().forEach(function(c){b(a+"_"+c).prop("disabled",!0)});this._toggleDatePickerTrigger(!1);this._toggleActions({edit:!0,cancel:!1})},_reset:function(a){var c=this.topFields.concat(this.personFields);a||(c=c.concat(this.nameFields));
  var d=this.selector;c.forEach(function(a){b(d+"_"+a).prop("disabled",!1).val("")});this._toggleDatePickerTrigger(!0);a?this._readInputs():this.data={};this._serialize()},_edit:function(){this.previousSelection=b.extend({},this.data);this._reset();this._enableAutocomplete();this._toggleActions({edit:!1,cancel:!0})},_cancel:function(){var a=this.previousSelection;this.pendingAC&&(this.pendingAC.abort(),this.pendingAC=!1,this.throbber.hide());a&&Object.values(a).length?(this.data=b.extend({},this.previousSelection),
  this._serialize(),this._updateInputs(),this._disableInputs()):this._reset();this._clearDuplicates()},_clearError:function(){b(this.selector+"_title__row").next(".error_wrapper").fadeOut("slow",function(){S3.hideAlerts()})},_fullName:function(a){return a.label!==k?a.label:a.name},_autocompleteLabel:function(a){if(a.label!==k)return a.label;var b=a.name,d=[a.job,a.org].filter(function(a){return a});d.length&&(b+=" ("+d.join(",")+")");this.idLabel&&(a=a.pe_label)&&(b+=' <span class="pe-label">['+a+"]</span> ");
  return b},_enableAutocomplete:function(){var a=this.trigger;if(!a.attr("autocomplete")){var c=this.options,d=this;this.acURL=S3.Ap.concat("/"+c.c+"/"+c.f+"/search_ac");this.idLabel&&(this.acURL+="?label=1");var e=a.autocomplete({delay:c.delay,minLength:c.chars,source:function(a,c){d.pendingAC&&d.pendingAC.abort();d.pendingAC=b.ajax({url:d.acURL,data:{term:a.term}}).done(function(a){0===a.length?delete d.data.id:a.push({id:0,value:"",label:i18n.none_of_the_above});c(a)})},search:function(){d.throbber.show();
  return!0},response:function(a,b,c){d.throbber.hide();return c},focus:function(){return!1},select:function(b,e){b=e.item;b.id?(c.separateNameFields||a.val(d._fullName(b)),d._selectPerson(b.id)):delete d.data.id;return!1}}).data("ui-autocomplete");e&&(e._renderItem=function(a,c){var e=d._autocompleteLabel(c);return b("<li>").data("item.autocomplete",c).append("<a>"+e+"</a>").appendTo(a)})}},_selectPerson:function(a){var c=this.options,d=this.selector,e=c.trigger,f;this.nameFields.forEach(function(a){f=
  b(d+"_"+a).prop("disabled",!1);a!=e&&f.val("")});this._reset(!0);var g=this,h=this.trigger,n=S3.Ap.concat("/"+c.c+"/"+c.f+"/"+a+"/lookup.json");this.idLabel&&(n+="?label=1");this.throbber.show();h.val()||h.attr("placeholder",i18n.loading+"...");b.getJSONS3(n,function(b){try{g.data.id=a,c.separateNameFields||(g.data.full_name=h.val()),g._processReponse(b)}catch(p){g._reset(!0)}g.throbber.hide();h.removeAttr("placeholder")})},_processReponse:function(a){var c=this._fieldMap(),d=this.data,e;for(e in c){var f=
  c[e]||e;a.hasOwnProperty(f)&&(d[e]=a[f])}this._serialize();this.previousSelection=b.extend({},this.data);this._updateInputs();this._disableInputs()},lookupIDLabel:function(){var a=this.idLabel;if(a){var c=a.val();if(c){var d=a.data("lookup");d&&(d.abort(),a.data("lookup",null));var e=this.options,f=this,g=this.idLabelThrobber.show();c=S3.Ap.concat("/"+e.c+"/"+e.f+"/lookup.json?search=1&label=1&~.pe_label="+c);d=b.getJSONS3(c,function(b){b.pe_label&&(f.data.id=b.id,e.separateNameFields||(f.data.full_name=
  b.name),f._processReponse(b));a.data("lookup",null);g.hide()});a.data("lookup",d)}}},lookupContact:function(a){this._reset();var c=this.options,d=this.trigger,e=this;a=S3.Ap.concat("/org/site/"+a+"/site_contact_person");this.throbber.show();d.val()||d.attr("placeholder",i18n.loading+"...");b.getJSONS3(a,function(a){try{e.data.id=a.id;if(!c.separateNameFields){var b=a.name;d.val(b);e.data.full_name=b}e._processReponse(a)}catch(h){e._reset()}e.throbber.hide();d.removeAttr("placeholder")})},_checkDuplicates:function(){var a=
  this.options,c=this._fieldMap(),d=this.selector,e=this.data,f={};if(!e.id){if(a.separateNameFields){if(!e.first_name)return;this.nameFields.forEach(function(a){(g=e[a])&&(f[c[a]||a]=g)})}else{var g=e.full_name;if(!g)return;f.name=g}this.personFields.forEach(function(a){(g=e[a])&&(f[c[a]||a]=g)});var h=S3.Ap.concat("/pr/person/check_duplicates");b.ajaxS3({url:h,data:f,type:"POST",dataType:"json",success:function(c){b(d+"_results").remove();if(c.length){var e=i18n.dupes_found.replace("_NUM_",c.length)+
  ' <i class="'+a.downIcon+'"> </i>';b(d+"_duplicates").html(e).data("results",c).show()}else b(d+"_duplicates").data("results",[]).hide()}})}},_displayDuplicates:function(){var a=this.options,c=this.selector;if(!b(c+"_results").length){var d=function(a,c){var d=b("<div>").addClass("card_1_line");c&&d.append(b("<label>").html(c));return d.append(a)},e=function(a,c){var d=b('<button type="button" class="fright">');c&&d.append(b('<i class="'+c+'">'));return d.append(a)};c=b(c+"_duplicates");var f=c.data("results"),
  g=b('<div id="'+this.fieldName+'_results">'),h,k,l,m;f.forEach(function(c){k=c.name;h=b('<div class="dl-item">').data({id:c.id,name:k});l=b('<img width="50" height="50" class="media-object">');m=c.image?"/default/download/"+c.image:"/static/img/blank-user.gif";l.attr("src",S3.Ap.concat(m));h.append(b('<a class="fleft">').append(l));c.org&&h.append(d(c.org));h.append(d(k));c.father_name&&i18n.father_name_label&&h.append(d(c.father_name,i18n.father_name_label));c.grandfather_name&&i18n.grandfather_name_label&&
  h.append(d(c.grandfather_name,i18n.grandfather_name_label));["year_of_birth","dob","email","phone"].forEach(function(a){(a=c[a])&&h.append(d(a))});h.append(e(i18n.Yes,a.yesIcon));g.append(h)});f=b('<div class="dl-item">').append(e(i18n.No,a.noIcon));g.append(f);c.after(g);b("html,body").animate({scrollTop:c.offset().top},250)}},_selectDuplicate:function(a,c){var d=this.selector,e=b(d+"_duplicates");a&&c?(b(d+"_full_name").val(c),this._clearDuplicates(),e.data("submit")?(this.data={id:a},this._serialize(),
  e.closest("form").off(this.eventNamespace).submit()):(this.options.separateNameFields&&this.trigger.val(""),this._selectPerson(a))):(this._clearDuplicates(),e.data("submit")&&e.closest("form").off(this.eventNamespace).submit())},_clearDuplicates:function(){var a=this.selector;b(a+"_duplicates").data("results",[]).empty();b(a+"_results").remove()},_onFormSubmit:function(a){var c=this.selector;if(this.options.lookupDuplicates&&(c=b(c+"_duplicates"),c.data("results").length))return c.data("submit",!0),
  this._displayDuplicates(),!1;this.data.id?this.data={id:this.data.id}:this._readInputs();this._serialize();b(a).off(this.eventNamespace).submit();return!0},_bindEvents:function(){var a=b(this.element),c=this.options,d=this.selector,e=this.eventNamespace,f=this;a.closest("form").on("submit"+e,function(a){a.preventDefault();return f._onFormSubmit(this)});b(d+"_edit_bar .edit-action").on("click"+e,function(){f._clearError();f._edit()});b(d+"_edit_bar .cancel-action").on("click"+e,function(){f._clearError();
  f._cancel()});b(d+"_organisation_id").change(function(){f.acURL=m(f.acURL,{"~.organisation_id":b(this).val()})});b(d+"_title__row").next(".error_wrapper").one("click"+e,function(){f._clearError();return!1});var g=this.nameFields.concat(this.personFields);b(g.map(function(a){return d+"_"+a}).join(",")).on("change"+e,function(){f._readInputs();f._serialize();c.lookupDuplicates&&f._checkDuplicates()});c.lookupDuplicates&&(b(d+"_duplicates").data("results",[]).on("click"+e,function(){f._displayDuplicates()}),
  a.closest("form").on("click"+e,d+"_results .dl-item",function(){var a=b(this),c=a.data("id");a=a.data("name");f._selectDuplicate(c,a)}),a.on("change"+e,function(){a.val()&&f._clearDuplicates()}));if(g=this.idLabel)g.on("input"+e,function(){var a=b(this),d=a.data("lookupTimeout");d&&clearTimeout(d);d=setTimeout(function(){f.lookupIDLabel()},c.delay);a.data("lookupTimeout",d)}),g.on("blur"+this.eventNamespace,function(){var a=b(this),c=a.data("lookup"),d=a.data("lookupTimeout");c&&(c.abort(),a.data("lookup",
  null),f.idLabelThrobber.hide());d&&clearTimeout(d)});return!0},_unbindEvents:function(){var a=b(this.element),c=this.selector,d=this.eventNamespace;a.off(d);a.closest("form").off(d);b(c+"_edit_bar .edit-action").off(d);b(c+"_edit_bar .cancel-action").off(d);b(c+"_duplicates").off(d);a=this.nameFields.concat(this.personFields);b(a.map(function(a){return c+"_"+a}).join(",")).off(d);(a=this.idLabel)&&a.off(d);return!0}})})(jQuery);
  