[% PROCESS tt/callforward_head.tt %]
[% META title = 'Call forward' %]
[% 
  add_text = Catalyst.loc('Add'); 
  rem_text = Catalyst.loc('Remove');
  through_text = Catalyst.loc('through');
  any_text = Catalyst.loc('any');
%]

<div id="main">

    <div id="topmsg">
        <p> [% messages.topmsg %] </p>
    </div>
    <div id="toperr">
        <p> [% messages.toperr %] </p>
        <p> [% messages.prov_error %] </p>
    </div>
    
<div class="sperrehead">
    <h3>[% Catalyst.loc('Time sets') %]</h2>
    <h2 class="umleitungfuer">[% Catalyst.loc('Settings for [_1]', subscriber.active_number) %]</h2>
</div>

<div id="spalten31">
    <div class="spalte1">
        
    [% FOREACH tset IN time_sets %]
        <div class="set-container span-15 last">
        <div class="set-item span-15 last">
            [% IF tset_id == tset.id %]
                [% PROCESS set action = 'edit' %]
            [% ELSE %]
                [% PROCESS set action = 'view' %]
            [% END %]
        </div>

        [% priority = 0 %]
        [% FOREACH period IN tset.periods %]

            <div class="sub-container span-15 last">
            
            [% IF period.id == -1 %]
                [% PROCESS period action = 'add' %]
            [% ELSIF tperiod_id == period.id %]
                [% PROCESS period action = 'edit' %]
            [% ELSE %]
                [% PROCESS period action = 'view' %]
            [% END %]

            [% priority = priority + 1 %]
            </div> <!--period-item-->
        [% END %]
        
        [% IF priority == 0 or tset_id == tset.id %]
            [% period = undef %]
            <div class="sub-container span-15">
                [% PROCESS period action = 'add' %]
            </div>
        [% END %]
        
        </div> <!--set-container-->
    [% END %]
        
    <div class="set-container span-15 last">
      <div class="set-item span-15 last">
        [% PROCESS set action = 'add' %]
      </div>
    </div>

    </div> <!-- spalte1 -->
    
    <div class="spalte2">
        [% PROCESS tt/callforward_navi.tt %]
    </div> <!-- spalte2 -->

    </div> <!-- spalten31 -->

</div> <!-- main -->
<br clear="all" />

[%# ================================= %]
[% BLOCK set %]
    [% IF action == 'edit' or action == 'add' %]
        <form action="/callforward/time/set/save" method="post">
        [% IF action == 'edit' %]
            <div class="span-10"> <input type="text" size="20" name="tsetname" value="[% tset.name %]" title="enter a name for the time set" /> </div>
        [% ELSE %]
            <div class="span-10"> <input type="text" size="20" name="tsetname" value="" title="enter a name for the time set" /> </div>
        [% END %]
    [% ELSE %] 
        <form action="/callforward/time/set/delete" method="post">
        <div class="span-10"> [% tset.name %] </div>
    [% END %]

    <input type="hidden" name="subscriber_id" value="[% subscriber.subscriber_id %]"/>
    [% IF action == 'edit' or action == 'view' %]
        <input type="hidden" name="tset_id" value="[% tset.id %]"/>
    [% END %]

    [% IF action == 'edit' %]
        <div class="span-2"><p class="link_button">
            <label for="save_tset_[% tset.id %]">[% Catalyst.loc('Save') %]</label>
            <input type="image" class="hidden" src="/grafik/dot_trans.gif" alt="" id="save_tset_[% tset.id %]">
        </p></div>
        <div class="span-1"><a class="link" href="/callforward/time"><span class="button-cancel">[% Catalyst.loc('Cancel') %]</span></a></div>
    [% ELSIF action == 'add' %]
        <div class="span-2"><p class="link_button">
            <label for="add_tset_[% tset.id %]">[% Catalyst.loc('Add timeset') %]</label>
            <input type="image" class="hidden" src="/grafik/dot_trans.gif" alt="" id="add_tset_[% tset.id %]">
        </p></div>
        <div class="span-1">&nbsp;</div>
    [% ELSIF action == 'view' %]
        <div class="span-2"><a class="link" href="/callforward/time/set/[% tset.id %]/edit#tset-[% tset.id %]"><span class="button-edit">[% Catalyst.loc('Edit') %]</span></a></div>
        <div class="span-2">
        [% IF mapped.${tset.id} %]
            <p>[% mapped.${tset.id} %] [% Catalyst.loc('times(s) mapped') %]</p>
        [% ELSE %]
            <p class="link_button">
                <label for="delete_tset_[% tset.id %]">[% Catalyst.loc('Delete') %]</label>
                <input type="image" class="hidden" src="/grafik/dot_trans.gif" alt="" id="delete_tset_[% tset.id %]">
            </p>
        [% END %]
        </div>
    [% END %]
    
    </form><!-- eoset -->
[% END %]

[%# ================================= %]
[% BLOCK period %]
    
    [% IF action == 'edit' or action == 'add' %]
        <form action="/callforward/time/period/save" method="post">
    [% ELSIF action == 'view' %]
        <form action="/callforward/time/period/delete" method="post">
    [% END %]

    <input type="hidden" name="subscriber_id" value="[% subscriber.subscriber_id %]"/>
    <input type="hidden" name="tset_id" value="[% tset.id %]"/>

    [% IF action == 'edit' or action == 'view' %]
        <input type="hidden" name="tperiod_id" value="[% period.id %]"/>
    [% END %]

    [% IF 0 %]
      <!-- dummy for auto-generate translation stubs
      Catalyst.loc('year'); Catalyst.loc('month'); Catalyst.loc('mday');
      Catalyst.loc('wday'); Catalyst.loc('hour'); Catalyst.loc('minute');
      -->
    [% END %]
    [% FOREACH part IN ['year', 'month', 'mday', 'wday', 'hour', 'minute'] %]
        <div class="span-2">
            <div class="ui-widget-header drophead" style="text-transform:capitalize"><p class="ui-widget-header drophead">[% Catalyst.loc(part) %]</p></div>
            [% IF action == 'add' %]
                <div class="ui-widget-header dateform-elem"     id="[% part %]-[% tset.id %]--1"></div> 
                <div class="ui-widget-header dropfoot"          id="[% part %]-[% tset.id %]--1-foot"></div>
            [% ELSE %]
                <div class="ui-widget-header dateform-elem"     id="[% part %]-[% tset.id %]-[% period.id %]"></div>
                <div class="ui-widget-header dropfoot"          id="[% part %]-[% tset.id %]-[% period.id %]-foot"></div>
            [% END %]
            
            <div class="dateform-errormsg" id="[% part %]-[% tset.id %]-[% period.id %]-errormsg">
            [% IF (action == 'edit' or action == 'add') and tperiod_id == period.id %]
                [% error = 'err_' _ part %]
                [% IF messages.$error %]
                    <p>[% messages.$error %] </p>
                [% END %]
            [% END %]
            </div>
                
        </div>
    [% END %]

    [% IF action == 'edit' %]
        <div class="span-2" style="padding:20px 0 0 10px;">
          <p class="link_button">
            <label for="save_period_[% period.id %]">[% Catalyst.loc('Save') %]</label>
            <input type="image" class="hidden" src="/grafik/dot_trans.gif" alt="" id="save_period_[% period.id %]">
          </p>
          <br/>
          <a class="link" href="/callforward/time"><span class="button-cancel">[% Catalyst.loc('Cancel') %]</span></a>
        </div>
    [% ELSIF action == 'add' %]
        <div class="span-1" style="padding:20px 0 0 10px;"><p class="link_button">
            <label for="add_period_[% tset.id %]">[% Catalyst.loc('Add period') %]</label>
            <input type="image" class="hidden" src="/grafik/dot_trans.gif" alt="" id="add_period_[% tset.id %]">
        </p></div>
        <div class="span-1"></div>
    [% ELSIF action == 'view' %]
        <div class="span-2" style="padding:20px 0 0 10px;">
          <a class="link" href="/callforward/time/period/[% period.id %]/edit#tperiod-[% period.id %]"><span class="button-edit">[% Catalyst.loc('Edit') %]</span></a>
          <br/>
          <p class="link_button">
            <label for="add_period_[% period.id %]">[% Catalyst.loc('Delete') %]</label>
            <input type="image" class="hidden" src="/grafik/dot_trans.gif" alt="" id="add_period_[% period.id %]">
          </p>
        </div>
    [% END %] 
    </form><!-- eoperiod -->
[% END %]


[%#
    get_months() and get_wdays() provide translated month and weekdays

    document.ready() (create_period_parts) prints needed period-parts (year, month, ...). this
    should only be done in one place and it needs to be done at least
    in .js therefore only there.
%]
<script type="text/javascript">
    
    $(document).ready(function(){
        [% PROCESS create_period_parts | trim %]
    });

    function get_months () {

        var stuff = Array ();
        var i = 1;

        stuff.push ({ value: i++, label: '[% Catalyst.loc("Jan") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Feb") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Mar") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Apr") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("May") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Jun") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Jul") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Aug") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Sep") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Oct") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Nov") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Dec") %]' });

        return stuff;
    }

    function get_wdays () {

        var stuff = Array ();
        var i = 1;

        stuff.push ({ value: i++, label: '[% Catalyst.loc("Sun") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Mon") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Tue") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Wed") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Thu") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Fri") %]' });
        stuff.push ({ value: i++, label: '[% Catalyst.loc("Sat") %]' });

        return stuff;
    }

</script>

[% BLOCK create_period_parts %]
        [% html = '' %]
        [% FOREACH tset IN time_sets %]
            [% FOREACH period IN tset.periods %]
               
                [% disabled = ((period.id == tperiod_id) or (period.id == 0)) ? 0 : 1 %]
                
                [% FOREACH part IN ['year', 'month', 'mday', 'wday', 'hour', 'minute'] %]
                    
                    [% target = part _ '-' _ tset.id _ '-' _ period.id %]
                    [% from = 'from_' _ part %] 
                    [% to = 'to_' _ part %] 

                    [% selected_from = (period.$from.defined) ? period.$from : -1 %]
                    [% selected_to = (period.$to.defined) ? period.$to : -1 %]

                    [% IF (selected_from >= 0) or (selected_to >= 0) %]
                        [% f = "create_period_part ('from', $disabled, $selected_from , '$part') " %]
                        [% t = "create_period_part ('to', $disabled, $selected_to, '$part') " %]
                        [% add_text = Catalyst.loc('Add'); rem_text = Catalyst.loc('Remove') %]
                        [% html = html _ "print_html ('$target', $f + ' $through_text<br/> ' + $t, $disabled, '$any_text', '$add_text', '$rem_text', '$through_text');\n" %]
                    [% ELSE %]
                        [% html = html _ "remove_html ('$target', '$disabled', '$any_text', '$add_text', '$rem_text', '$through_text');\n" %]
                    [% END %]

                [% END %]
                
            [% END %]
                
            [% IF (tset_id == tset.id) or (tset.periods.size == 0) %]
                [% FOREACH part IN ['year', 'month', 'mday', 'wday', 'hour', 'minute'] %]
                    [% target = part _ '-' _ tset.id _ '--1' %]
                    [% html = html _ "remove_html ('$target', null, '$any_text', '$add_text', '$rem_text', '$through_text');" %]
                [% END %]
            [% END %]

        [% END %]

        [% html %]
[% END %]
