<%-# Fedena
#Copyright 2010 Foradian Technologies Private Limited
#
#This product includes software developed at
#Project Fedena - http://www.projectfedena.org/
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied.  See the License for the
#specific language governing permissions and limitations
#under the License. -%>

<script>
  function add_recipient(recipient) {
    var recipient_list = new Array();
    if($('recipients').value != '')
      recipient_list = $('recipients').value.split(',');
    else
      recipient_list = [];

    var recipient_exists = false;

    for(i=0; i<recipient_list.length; i++)
      if(recipient_list[i] == recipient)
        recipient_exists = true;

    if(!recipient_exists) {
      recipient_list.push(recipient);
    }
    $('recipients').value = recipient_list.join();
    recipients = $('recipients').value;
<%= remote_function(:url => {:action => 'update_recipient_list'}, :with => "'recipients='+recipients" ) %>
  }

  function add_all_recipient(recipient) {
    var recipient_list = new Array();
    if($('recipients').value != '')
      recipient_list = $('recipients').value.split(',');
    else
      recipient_list = [];

    var new_list = recipient.split(',');

    for(i=0;i<new_list.length;i++)
    {
      var recipient_exists = false;
      for(j=0; j<recipient_list.length; j++)
        if(recipient_list[j] == new_list[i])
          recipient_exists = true;
      if(!recipient_exists) recipient_list.push(new_list[i]);
    }

    $('recipients').value = recipient_list.join();
    recipients = $('recipients').value;
<%= remote_function(:url => {:action => 'update_recipient_list'}, :with => "'recipients='+recipients" ) %>
  }


  function remove_recipient(recipient) {
    recipients = $('recipients').value;
    var recipient_list = new Array();
    recipient_list = $('recipients').value.split(',');

    for(i=0; i<recipient_list.length; i++)
      if (recipient_list[i] == recipient)
    {
      recipient_list.splice(i,1);
      break;
    }

    $('recipients').value = recipient_list.join();
    recipients = $('recipients').value;
<%= remote_function(:url => {:action => 'update_recipient_list'}, :with => "'recipients='+recipients" ) %>



  }
</script>

<div id="content-header">
  <%= image_tag("/images/reminder/reminder_small.png") %>
  <h1><%= t('messages') %></h1>
  <h3><%= t('create_text') %>/<%= t('new_text') %></h3>
  <div id="app-back-button">
    <%= link_to_function image_tag("/images/buttons/back.png",:border => 0), "history.back()" %>
  </div>
</div>

<div id="inner-tab-menu">
  <ul>
    <% unless @new_reminder_count.size == 0 %>
      <li><%= link_to "<b>#{t('inbox')} (#{@new_reminder_count.size})</b>", :controller=>"reminder", :action=>"index"%></li>
    <% else %>
      <li><%= link_to "#{t('inbox')}", :controller=>"reminder", :action=>"index"%></li>
    <% end %>

    <li><%= link_to "#{t('sent')}", :controller=>"reminder", :action=>"sent_reminder" %></li>
    <li><%= link_to "#{t('create_text')}", :controller=>"reminder", :action=>"create_reminder" %></li>
  </ul>
</div>

<div id="page-yield">

  <% unless flash[:notice].nil? %>
    <p class="flash-msg"> <%= flash[:notice] %> </p>
  <% end %>

  <div class="create-options">


    <% current_user = @current_user %>
  </div>
  <% form_for :reminder do |l| %>
    <div id="form">
      <%= error_messages_for :reminder %>
      <%= hidden_field_tag :recipients, '' %>
      <% @config = Configuration.available_modules  %>
      <% if @config.include?('HR') %>
        <div class="to-options">
          <%= link_to_remote "#{t('reminder_to_staff')}", :url=>{:controller=>"reminder",:action=>"select_employee_department"},:update=>"select-employee-department", :html=>{:class=>"themed_text"} %>
        </div>
      <% else %>
        <div class="to-options">
          <%= link_to_remote "#{t('reminder_to_staff')}", :url=>{:controller=>"reminder",:action=>"select_users"},:update=>"select-employee-department", :html=>{:class=>"themed_text"} %>
        </div>
      <% end %>
      <%= image_tag("loader.gif",
        :align => "absmiddle",
        :border => 0,
        :id => "loader",
        :style =>"display: none;" ) %>
      <div class="extender"></div>
      <% if @config.include?('HR') %>
        <div id="select-employee-department">
          <%     @user = current_user %>
          <%  @departments = EmployeeDepartment.find(:all, :conditions=>"status = true") %>
          <%=   render :partial=>"select_employee_department" %>
        </div>
      <% end %>

      <div class="label-field-to">
        <div id="to_users">

        </div>
      </div>
      <div class="extender"></div>


      <div class="to-options">
        <%= link_to_remote "#{t('reminder_to_student')}", :url=>{:controller=>"reminder",:action=>"select_student_course"},:update=>"select-student-course", :html=>{:class=>"themed_text"} %>
      </div>
      <div class="extender"></div>
      <div id="select-student-course">
        <%     @user = current_user %>
        <%  @batches = Batch.active %>
        <%=  render :partial=> "select_student_course" %>
      </div>
      <div class="label-field-to">
        <div id="to_users2"></div>
      </div>
    </div>
    <div class="message">
      <div id="recipient-list"></div>
      <div class="label-field-pair">
        <label for="subject"><%= t('subject') %></label>
        <div class="text-input-bg"><%= l.text_field :subject %></div>
      </div>
      <div class="label-field-pair">
        <label for="student_grade"><%= t('message') %></label>
        <div class="text-area-bg"><%= l.text_area :body, :cols=>25, :rows=>10 %>
        </div>
      </div>
      <div class="extender"></div>
      <div id="submit-button">
        <%=submit_tag "#{t('send')}", :class => 'button' %>
      </div>
    </div>


    <% unless params[:send_to].nil? %>
      <script type="text/javascript">
        send_to = "<%=  params[:send_to] %>";
        recipient_list = send_to.split(',');
        for(i=0; i<recipient_list.length; i++)
          add_recipient(recipient_list[i]);
      </script>
    <% end %>
  <% end %>
  <div class="extender"></div>

</div>