/* Minified: jquery.loginAsForm.js, jquery.manageRoles.js, jquery.selectClientCompanyManager.js, jquery.selectSystemPermissions.js, jquery.selectUsersInline.js, widgets.RolePermissionValue.js, widgets.SelectUsers.js, widgets.SelectUsersPopup.js */
jQuery.fn.loginAsForm=function(s){var settings=jQuery.extend({href:null,title:null,error:null,error_event:"async_operation_error"},s);if(settings.width){if(parseInt(settings.width)==settings.width){settings.width=settings.width}else{settings.width_class=settings.width;settings.width=1000}}else{settings.width=1000}return this.each(function(){var element=$(this);element.click(function(){var dialog=App.widgets.FlyoutDialog.show({title:settings.title?settings.title:element.attr("title"),data:settings.href?settings.href:element.attr("href"),data_type:"url",min_height:"auto",width:settings.width,width_class:settings.width_class,success:function(){var dialog_content=$(this);if(dialog_content.is("form")){var form=dialog_content}else{var form=dialog_content.find("form:first")}var handle_success=function(response){if(typeof(response)=="object"&&response&&response.ajax_error){handle_error.apply(this,[response]);return false}App.widgets.FlyoutDialog.stopProcessing(dialog);App.widgets.FlyoutDialog.close(dialog);App.Wireframe.Flash.success("You have been logged in as :name. Please wait until interface is reloaded",{name:response.display_name});location.href=App.Config.get("homepage_url")};var handle_error=function(response){App.widgets.FlyoutDialog.stopProcessing(dialog);if(typeof(response)=="object"&&response){if(response.ajax_message){var response_message=response.ajax_message}else{var response_message=App.Wireframe.Utils.responseToErrorMessage(response)}}else{var response_message=App.lang("Unknown error occurred. Please try again later")}if(typeof(settings.error)=="function"){settings.error.apply(element[0],[response])}if(settings.error_event){App.Wireframe.Events.trigger(settings.error_event,[response_message,response])}};form.submit(function(){if(form.is(".disabled")){form.removeClass("disabled");return false}App.widgets.FlyoutDialog.startProcessing(dialog);var options={url:App.extendUrl(form.attr("action"),{async:1}),type:"post",dataType:"json",success:function(response){handle_success.apply(this,[response])},error:function(response){handle_error.apply(this,[response])}};$.ajax({url:App.extendUrl(form.attr("action"),{async:1}),type:"post",dataType:"json",data:form.serialize(),success:function(response){handle_success.apply(this,[response])},error:function(response){handle_error.apply(this,[response])}});return false})}});return false})})};
(function($){var methods={init:function(s){var settings=jQuery.extend({roles:null},s);return this.each(function(){var wrapper=$(this);var table=$('<table class="common" cellspacing="0"><thead><tr><th class="is_default"></th><th class="name" colspan="2">'+App.lang("Role Name")+"</th></tr></thead><tbody></tbody></table>").appendTo(wrapper);var empty=$('<p class="empty_page">'+App.lang("There are no system roles defined")+"</p>").appendTo(wrapper);if(typeof(settings.roles)=="object"&&settings.roles){$.each(settings.roles,function(index,role){wrapper.manageRoles("add_role",role,true)})}App.Wireframe.Content.bind("role_created.content",function(e,role){wrapper.manageRoles("add_role",role)});App.Wireframe.Content.bind("role_updated.content",function(e,role){wrapper.manageRoles("update_role",role)});App.Wireframe.Content.bind("role_deleted.content",function(e,role){wrapper.manageRoles("remove_role",role)});wrapper.manageRoles("refresh");table.delegate("td input[type=radio]","click",function(){var radio=$(this);var cell=radio.parent();var row=cell.parent();if(!row.is("tr.is_default")){table.find("td.is_default input[type=radio]").hide();cell.append('<img src="'+App.Wireframe.Utils.indicatorUrl()+'">');$.ajax({url:App.extendUrl(radio.attr("set_as_default_url"),{async:1}),type:"post",data:{submitted:"submitted"},success:function(){cell.find("img").remove();table.find("tr.is_default").removeClass("is_default");row.addClass("is_default");table.find("td.is_default input[type=radio]").show();App.Wireframe.Flash.success("Selected role has been set as default")},error:function(){table.find("tr.is_default td.is_default input[type=radio]").prop("checked",true);table.find("td.is_default input[type=radio]").show();App.Wireframe.Flash.error("Failed to select selected role as default")}})}})})},add_role:function(role,bulk){var wrapper=$(this);var table=wrapper.find("table");var row=$('<tr class="system_role" role_id="'+role.id+'"><td class="is_default"><input type="radio" name="default_role" value="'+role.id+'" set_as_default_url='+role.urls["set_as_default"].clean()+'></td><td class="name"><a href="'+role.urls["view"]+'">'+role.name.clean()+'</td><td class="options"></td></tr>').appendTo(table.find("tbody"));if(role.is_default){row.addClass("is_default").find("td.is_default input[type=radio]").prop("checked",true)}var options_cell=row.find("td.options");var options_id_prefix=wrapper.attr("id")+"_role_"+role.id;if(typeof(role.options)=="object"){for(var option_name in role.options){var option=$('<a href="'+role.options[option_name]["url"]+'"><img src="'+role.options[option_name]["icon"]+'"></a>').attr({id:options_id_prefix+"_"+option_name,title:role.options[option_name]["text"]}).appendTo(options_cell);if(typeof(role.options[option_name]["attributes"])=="object"&&role.options[option_name]["attributes"]){option.attr(role.options[option_name]["attributes"])}if(typeof(role.options[option_name]["classes"])=="object"&&role.options[option_name]["classes"]){for(var i in role.options[option_name]["classes"]){option.addClass(role.options[option_name]["classes"][i])}}if(typeof(role.options[option_name]["onclick"])=="string"&&role.options[option_name]["onclick"]){var onclick=eval(role.options[option_name]["onclick"]);if(typeof(onclick)=="function"){onclick.apply(option[0])}}}}if(!bulk){row.find("td").highlightFade();wrapper.manageRoles("refresh")}},update_role:function(role){var wrapper=$(this);var role_row=wrapper.find("tr[role_id="+role.id+"]");if(role_row.length){role_row.find("td.name a").text(role.name)}role_row.find("td").highlightFade()},remove_role:function(role){var wrapper=$(this);if(typeof(role)=="number"){var role_id=role}else{if(typeof(role)=="object"&&typeof(role.id)!="undefined"&&role.id){var role_id=role.id}else{var role_id=false}}if(role_id){wrapper.find("tr[role_id="+role_id+"]").remove();wrapper.manageRoles("refresh");App.Wireframe.Flash.success("Selected role has been successfully removed")}},refresh:function(){var wrapper=$(this);var table=wrapper.find("table");if(table.find("tr.system_role").length>0){table.oddEven({selector:"tr.system_role"}).show();wrapper.find("p.empty_page").hide()}else{table.hide();wrapper.find("p.empty_page").show()}}};$.fn.manageRoles=function(method){if(methods[method]){return methods[method].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof method==="object"||!method){return methods.init.apply(this,arguments)}else{throw"Method "+method+" does not exist on jQuery.manageHomescreenWidgets"}}}})(jQuery);
jQuery.fn.selectClientCompanyManager=function(s){var settings=jQuery.extend({client_company_managers_url:null,value:null,optional:null,company_select_id:null,skip_owners_without_finances:null},s);return this.each(function(){var wrapper=$(this);var wrapper_id=wrapper.attr("id");var company_select=$("#"+wrapper.attr("company_select_id"));var init=function(){populate_select();company_select.change(function(){populate_select()})};var populate_select=function(){var company_id=company_select.val();$.ajax({url:App.extendUrl(settings.client_company_managers_url,{async:1}),type:"get",data:{company_id:company_id,submitted:"submitted",skip_owners_without_finances:settings.skip_owners_without_finances?1:0},success:function(response){if(settings.optional==true){wrapper.empty().append(new Option(App.lang("-- None --"),""));wrapper.append(new Option(" "," "))}else{wrapper.empty()}if(response){var str;$.each(response,function(key,value){$.each(value,function(user_id,user_name){if(user_id==settings.value){selected="selected=selected"}else{selected=""}str+='<option value="'+user_id+'" '+selected+">"+user_name+"</option>"})});wrapper.append(str)}},error:function(){App.Wireframe.Flash.error("Failed to populate select box")}})};init()})};
jQuery.fn.selectSystemPermissions=function(s){var settings=jQuery.extend({name:"permissions",value:null,permissions:null,protect:null},s);if(typeof(settings.value)!="object"||settings.value==null){settings.value={}}return this.each(function(){var wrapper=$(this);var wrapper_id=wrapper.attr("id");var list=$('<div class="select_system_permissions_list"></div>').attr("id",wrapper_id+"_list").appendTo(wrapper);var description=$('<div class="select_system_permissions_permission_description" style="display: none"></div>').attr("id",wrapper_id+"_description").appendTo(wrapper);var add=function(name,info,add_to){var permission_wrapper_id=add_to.attr("id")+"_"+name;var permission_wrapper=$('<div id="'+permission_wrapper_id+'" class="select_system_permissions_permission_wrapper"><label for="'+permission_wrapper_id+'_value"><input type="checkbox" name="'+settings.name+"["+name+']" id="'+permission_wrapper_id+'_value" value="1" /> <span>'+info.name.clean()+"</span></label></div>").appendTo(add_to);var permission_checkbox=permission_wrapper.find("input[type=checkbox]");if(typeof(settings.protect)=="object"&&settings.protect){for(var i in settings.protect){if(settings.protect[i]==name){permission_checkbox.attr("protected",true);break}}}var show_permission_description_timer=false;permission_wrapper.find("label").hover(function(e){var text_text=typeof(info.description)=="string"&&info.description?info.description:App.lang("Sorry, no description available");description.append("<b>"+info.name.clean()+"</b>").append("<p>"+text_text.clean()+"</p>");show_permission_description_timer=setTimeout(function(){description.fadeIn()},200)},function(e){if(show_permission_description_timer){clearTimeout(show_permission_description_timer)}description.empty().hide()});if(permission_checkbox.attr("protected")==true||(typeof(settings.value[name])!="undefined"&&settings.value[name])){permission_checkbox.attr("checked","checked")}var subpermissions_wrapper=$('<div id="'+permission_wrapper_id+'_subpermissions" class="select_system_permissions_permission_subpermissions"></div>');for(var subpermission_name in settings.permissions){if(settings.permissions[subpermission_name]["depends_on"]==name){add(subpermission_name,settings.permissions[subpermission_name],subpermissions_wrapper)}}if(subpermissions_wrapper.find("div.select_system_permissions_permission_wrapper").length>0){subpermissions_wrapper.appendTo(permission_wrapper);if(!permission_checkbox[0].checked){subpermissions_wrapper.hide()}}};for(var permission_name in settings.permissions){if(typeof(settings.permissions[permission_name]["depends_on"])=="undefined"){add(permission_name,settings.permissions[permission_name],list)}}$("input[type=checkbox]").each(function(){$(this).click(function(){if($(this).attr("protected")){App.Wireframe.Flash.success("Selected permission is protected in this role and it cannot be turned Off");return false}else{var subpermissions_wrapper=$(this).parent().parent().find(".select_system_permissions_permission_subpermissions");if(subpermissions_wrapper!=undefined){if($(this).attr("checked")){subpermissions_wrapper.show()}else{subpermissions_wrapper.find("input[type=checkbox]").removeAttr("checked");subpermissions_wrapper.find("div.select_system_permissions_permission_subpermissions").hide();subpermissions_wrapper.hide()}}}})})})};
jQuery.fn.selectUsersInline=function(s){return this.each(function(){$(this).find("div.user_group").each(function(){var group=$(this);var group_checkbox=group.find("input.group_checkbox");var user_checkboxes=group.find("input.user_checkbox");if(user_checkboxes.length>0&&group.find("input.user_checkbox:checked").length==user_checkboxes.length){group_checkbox[0].checked=true}group_checkbox.click(function(){if(this.checked){group.find("ul.group_users input[type=checkbox]").each(function(){this.checked=true})}else{group.find("ul.group_users input[type=checkbox]").each(function(){this.checked=false})}});user_checkboxes.click(function(){if(group.find("input.user_checkbox:checked").length==user_checkboxes.length){group.find("input.group_checkbox")[0].checked=true}else{group.find("input.group_checkbox")[0].checked=false}})})})};
App.widgets.RolePermissionValue=function(){return{init:function(checkbox_id){var checkbox=$("#"+checkbox_id);var cell=checkbox.parent();checkbox.click(function(){var new_value=this.checked;checkbox.hide();cell.append('<img src="'+App.Wireframe.Utils.indicatorUrl()+'" />');$.ajax({url:App.extendUrl((new_value?checkbox.attr("set_permission_value_url"):checkbox.attr("unset_permission_value_url")),{async:1}),type:"POST",data:{submitted:"submitted",value:this.checked?1:0},success:function(){cell.find("img").remove();checkbox.show();App.Wireframe.Flash.success("Permission value changed")},error:function(){cell.find("img").remove();checkbox.show()[0].checked=!new_value;App.Wireframe.Flash.error("Failed to set selected permission value")}})})}}}();
App.widgets.SelectUsers=function(){return{init:function(widget_id,control_name,all_users){var widget=$("#"+widget_id);var users_list=widget.find("ul.users_list");var no_selected_users=widget.find("p.no_users_selected");widget.find(".assignees_button").click(function(){var selected_users={};widget.find("input[type=hidden]").each(function(){var input=$(this);selected_users[parseInt(input.attr("value"))]=input.attr("user_display_name")});App.widgets.SelectUsersPopup.open(all_users,selected_users,function(response){users_list.empty();widget.find("input[type=hidden]").remove();for(var user_id in response){users_list.append("<li>"+response[user_id].clean()+"</li>");users_list.after('<input type="hidden" name="'+control_name+'[]" value="'+user_id+'" user_display_name="'+response[user_id].clean()+'" />')}if(users_list.find("li").length>0){no_selected_users.hide();users_list.show()}else{users_list.hide();no_selected_users.show()}});return false})}}}();window.select_users_value_present=function(field,caption){if(field.find("ul.users_list li").length<1){return App.lang("Required")}};
App.widgets.SelectUsersPopup=function(){return{open:function(all_users,selected_users,on_ok){var popup_content=$('<div class="select_users_widget_popup"><table class="select_users_layout"><tr><td class="users_list"></td><td class="divider"><img src="'+App.Wireframe.Utils.imageUrl("/icons/12x12/proceed.png","environment")+'" alt="" /></td><td class="selected_users"><div class="selected_users_list"><p>'+App.lang("Selected users")+':</p><div class="selected_users_list_container"><table></table></div></div><p class="no_users_selected">'+App.lang("No users selected")+"<br /><br /><span>"+App.lang("Select users from the list on the left and click the arrow button to mark them as selected")+"</span></p></td></tr></table></div>");var users_list_row=popup_content.find("td.users_list");var selected_users_list=popup_content.find("td.selected_users div.selected_users_list");var selected_users_table=popup_content.find("td.selected_users div.selected_users_list_container table");var no_users_selected=popup_content.find("td.selected_users p.no_users_selected");var available_users_select=$('<select multiple="multiple"></select>');for(var company_name in all_users){var company_group=$("<optgroup></optgroup>").attr("label",company_name).appendTo(available_users_select);for(var user_id in all_users[company_name]){$('<option value="'+user_id+'">'+all_users[company_name][user_id].clean()+"</option>").appendTo(company_group)}}if(available_users_select.find("optgroup").length>0){users_list_row.append("<p>"+App.lang("Available users")+"</p>").append(available_users_select)}else{users_list_row.append('<p class="details">'+App.lang("No users here")+"</p>")}var select_user=function(user_id,user_name){var existing_row=selected_users_table.find("tr[user_id="+user_id+"]");if(existing_row.length>0){existing_row.find("td").highlightFade()}else{var user_row=$('<tr user_id="'+user_id+'"><td class="display_name">'+user_name.clean()+'</td><td class="remove"><img src="'+App.Wireframe.Utils.imageUrl("/icons/12x12/delete.png","environment")+'" alt="" title="'+App.lang("Remove from the list")+'" /></td></tr>').appendTo(selected_users_table);user_row.find("td.remove img").click(function(){user_row.remove();if(selected_users_table.find("tr").length>0){selected_users_table.oddEven("tr")}else{selected_users_list.hide();no_users_selected.show()}});selected_users_list.show();no_users_selected.hide()}};for(var selected_user_id in selected_users){select_user(selected_user_id,selected_users[selected_user_id])}if(selected_users_table.find("tr").length>0){selected_users_table.oddEven("tr");no_users_selected.hide()}else{selected_users_list.hide()}var available_to_selected=function(){available_users_select.find("option:selected").each(function(){var option=$(this);select_user(option.attr("value"),option.text())});selected_users_table.oddEven("tr")};popup_content.find("td.divider img").click(available_to_selected);available_users_select.dblclick(available_to_selected);App.Wireframe.ModalDialog.show("select_users_popup",App.lang("Select Users"),popup_content,{buttons:[{label:App.lang("Ok"),callback:function(){if(typeof(on_ok)=="function"){var selected_users={};selected_users_table.find("tr").each(function(){var row=$(this);selected_users[parseInt(row.attr("user_id"))]=row.text()});on_ok(selected_users)}}},{label:App.lang("Cancel"),callback:null}],width:630})}}}();
