
(function($)
{$.fn.fieldSwitch=function(options)
{var opts=$.extend({},$.fn.fieldSwitch.defaults,options);return this.each(function()
{var success;$this=$(this);var o=$.meta?$.extend({},opts,$this.data()):opts;if(o.before)
{o.before($this)}
if(typeof(pbx.obj.present_form)!='undefined'&&typeof(pbx.obj.present_form.switches)!='undefined'&&typeof(pbx.obj.present_form.switches[$this.attr('name')])!='undefined')
{var eventToBind=$this.is('input:checkbox')||$this.is('input:radio')?'click':'change';$this.bind(eventToBind,function()
{var input=$(this);enforce(input,o);});}})};function enforce(input,o)
{var value=fieldValue(input,o);var active_switch=pbx.obj.present_form.switches[input.attr('name')][value];if(typeof(active_switch)!='undefined')
{applySwitch(input,o,active_switch);}
else
{if(pbx.obj.present_form.switches[input.attr('name')]&&pbx.obj.present_form.switches[input.attr('name')]['javascript_operators'])
{$.each(pbx.obj.present_form.switches[input.attr('name')]['javascript_operators'],function(operator,operator_conditions)
{$.each(operator_conditions,function(operator_value,operator_switch)
{if(eval('fieldValue '+operator+'operator_value'))
{applySwitch(input,o,operator_switch);return false;}});});}}}
function fieldValue(field,o)
{var ignore_disabled=o.ignore_disabled;return o.getValue(field,ignore_disabled);}
function applySwitch(input,o,active_switch)
{var new_rules=active_switch.rules;var new_values=active_switch.values;var form=input.parents('form');$.each(new_rules,function(field_name,rule){var field=form.find("[name='"+field_name+"']");o[rule](field);if(field.is('.pbx_field_switch')&&rule!='forbidden'){enforce(field,o);}});$.each(new_values,function(field_name,value){var field=form.find("[name='"+field_name+"']");o.setValue(field,value);if(field.is('.pbx_field_switch')){enforce(field,o);}});}
$.fn.fieldSwitch.defaults={ignore_disabled:1,mandatory:function(input)
{var label=input.removeAttr('disabled').parents('.pbx_field').show().removeClass('pbx_hidden').find('label').addClass('pbx_mandatory');if(label.find('span.pbx_mandatory_star').length==0)
{label.append("<span class='pbx_mandatory_star'>*</span>");}},optional:function(input)
{input.removeAttr('disabled').parents('.pbx_field').show().removeClass('pbx_hidden').find('label').removeClass('pbx_mandatory');},forbidden:function(input)
{input.attr('disabled','disabled').parents('.pbx_field').hide();},setValue:function(input,value)
{if(value=='_null_')
{value='';}
input.val(value);},getValue:function(input,ignore_disabled)
{var field_value='';if(input.is('input:checkbox'))
{field_value=input.filter(':checked').length+'';}
else if(input.is('input:radio'))
{field_value=input.filter(':checked').val()||'';}
else if(ignore_disabled&&(input.is('.pbx_inactive')||input.is(':disabled')))
{field_value='';}
else
{field_value=input.val();}
if(!field_value||field_value=='0')
{field_value='_null_'}
return field_value;}};})(jQuery);