<script type="text/javascript"> 
attendanceAlertMsg = "<%= l(:text_are_you_sure) %>";
selectListAlertMsg = "<%=l(:actionview_instancetag_blank_option) %>";
issueExistsAlertMsg = "<%=l(:label_wk_leave_exists) %>";
issueUrl="<%= "#{url_for(:controller => 'wkattendance', :action => 'getIssuesByProject')}" %>";
projectUrl="<%= "#{url_for(:controller => 'wkattendance', :action => 'getProjectByIssue')}" %>";
lblInvalid= " <%= (CGI.unescapeHTML l('activerecord.errors.messages.invalid')).html_safe %>";
lblDaysPerMonth = "<%=l(:label_hours_per_month) %>";
lblAccrual = "<%=l(:label_wk_leave_accrual) %>";
lblAccrualAfter = "<%=l(:label_wk_leave_accrual_after) %>";
lblAccrualMultiplier = "<%=l(:label_accrual_multiplier) %>";
lblLeaveIssue = "<%=l(:label_wk_leave_issue) %>";
lblYear = "<%=l(:label_year) %>";
</script>
<div class="splitcontent">
<div class="box tabular settings">
<fieldset>
	<legend><%= l(:report_attendance) %></legend>
		
	<p>
	<%= check_box_tag('settings[wktime_enable_clock_in_out]', 1, @settings['wktime_enable_clock_in_out'].to_i == 1) %> <label><%= l(:label_wk_enable_clock_in_out) %></label>
	</p>
	<p>
		<%= check_box_tag('settings[att_save_geo_location]', 1, @settings['att_save_geo_location'].to_i == 1) %>
		<label for="settings_att_save_geo_location"><%= l(:label_save_geo_location) %></label>
	</p>
	<p>
	<%= check_box_tag('settings[wktime_work_time_header]', 1, @settings['wktime_work_time_header'].to_i == 1) %> <label><%= l(:label_wk_work_time_header) %></label>
	</p> 
	<p>
		<%
		breakTime = []
		if(@settings['wktime_break_time'].blank?)
			breakTime = ''
		else
			@settings['wktime_break_time'].each do |element|
			  listboxArr = element.split('|')
			  fromHr = listboxArr[0].to_i>9 ? listboxArr[0].strip  : "0"+listboxArr[0].strip
			  fromMin = listboxArr[1].to_i>9 ? listboxArr[1].strip : "0"+listboxArr[1].strip
			  toHr = listboxArr[2].to_i>9 ? listboxArr[2].strip : "0"+listboxArr[2].strip
			  toMin = listboxArr[3].to_i>9 ? listboxArr[3].strip : "0"+listboxArr[3].strip
			  optionText = fromHr + ":"  + fromMin + " - "  + toHr + ":"  + toMin
			  breakTime << [optionText,element]
			end
		end
		%>
	<label><%= l(:label_wk_break_time) %></label>
		<%=  select_tag('settings[wktime_break_time]',
			options_for_select(breakTime, :selected => 'settings[wktime_break_time]'), :multiple=> true, :style => "width:200px; height:100px;")%>	

	</p>			
	<p>
		<%= link_to l(:button_add), "javascript:showBreakTimeDialog('Add');", :class => "button"%>  |
		<%= link_to l(:button_edit), "javascript:showBreakTimeDialog('Edit');", :class => "button"%>  |
		<%= link_to l(:button_delete), "javascript:removeSelectedValue('settings_wktime_break_time');", :class => "button" %>
	</p>
	
	<div id="breaktime-dlg" title="<%= l(:label_wk_break_time) %>">
		<p>
			<label><%= l(:label_date_from) %></label><span style="margin-left:0.5em;"></span>
			<%
			hr = []
			min = []
			for i in 0..23
				if i < 10
					hr << ['0' + i.to_s,i]
				else
					hr << [i,i]
				end
			end
			i = 0
			num = 5

			until i > 55  do
				if i < 10
					min << ['0' + i.to_s,i]
				else
					min << [i,i]
				end
			
			   i +=5;
			end
			%>
			<%= select_tag('break_from_hr', options_for_select(hr))%>&nbsp<%= l(:field_hours) %>&nbsp;	
			<%= select_tag('break_from_min', options_for_select(min))%>&nbsp<%= l(:label_wk_minutes) %>								   	
		</p>
		<p>
			<span style="margin-left:1.1em;"></span><label><%= l(:label_date_to) %></label><span style="margin-left:0.5em;"></span>
			<%= select_tag('break_to_hr', options_for_select(hr))%>&nbsp<%= l(:field_hours) %>&nbsp;	
			<%= select_tag('break_to_min', options_for_select(min))%>&nbsp<%= l(:label_wk_minutes) %>								   	
		</p>
		<p>
			<%= hidden_field_tag :hdnAction, "" %>
		</p>
			<p><%= l(:label_hours_note) %></p>		
	</div>
	
		<%
		leave = []
		issue_list = Issue.order('subject')
		unless issue_list.blank?
			issueslist = issue_list.collect {|issue| [issue.subject, issue.id] }
			issuehash = Hash[issue_list.map { |u| [u.id, u.subject] }]
		else
			issueslist = Array.new
			issueslist << [ "", 0]
		end		
		projects = Project.where("#{Project.table_name}.status not in(#{Project::STATUS_CLOSED},#{Project::STATUS_ARCHIVED})").order('name')
		wktime_helper = Object.new.extend(WktimeHelper)
		projArr = wktime_helper.options_for_wktime_project(projects,false)

		issueIdArr =Array.new
	    if(wktime_helper.getLeaveSettings.blank?)
			leaveIssueslist = []
		else
			wktime_helper.getLeaveSettings.each do |element|
			   listboxArr = element.split('|')
			   issueIdArr << listboxArr[0]
			end
			issueIds = issueIdArr.join(',')
		    leaveIssues = Issue.select(:id, :subject).where("id in (#{issueIds})")
			leaveIssueslist = leaveIssues.collect {|issue| [issue.subject, issue.id]}
		end
		leaveIssueslist.unshift(["",0]) 
	%>
	
	<p><label><%= l(:label_public_holiday) %></label>
	 <%= select_tag 'settings[wktime_holiday]', options_for_select(leaveIssueslist, :selected =>  @settings['wktime_holiday']) %></p>
	
	<%
		noOfDays = []
		for i in 0..31
			if i == 0
				noOfDays << ['',i]
			else
				noOfDays << [i,i]
			end
		end
	%>
	<p>
		<label><%= l(:minimum_working_days_for_accrual) %></label>
		<%= select_tag('settings[wktime_minimum_working_days_for_accrual]',
				   options_for_select(noOfDays,									   
										:selected => @settings['wktime_minimum_working_days_for_accrual'].blank? ? 0 : @settings['wktime_minimum_working_days_for_accrual'])) %>
	</p>
	<%
		custom_fields = UserCustomField.order('name')
		allFields = Hash.new()
		fldInFiles = Hash.new()		
		allFields = {"user_id" => "#{l(:label_user_id)}", "start_time" => "#{l(:label_start_time)}", "end_time" => "#{l(:label_end_time)}", "hours" => "#{l(:field_hours)}", "id1" => "#{l(:field_id1)}", "id2" => "#{l(:field_id2)}", "id3" => "#{l(:field_id3)}" }
		unless custom_fields.blank?		
			cfdate = custom_fields.select {|cf| cf.field_format == 'date'}
			cfids = custom_fields.select {|cf| cf.field_format == 'string' || cf.field_format == 'int' }			
			unless cfdate.blank?
				cfd = cfdate.collect {|cf| [cf.name, cf.id] }
				cfd.unshift(["",0])
			else
				cfd = Array.new
				cfd << [ "", 0]
			end
			
			unless cfids.blank?
				 cfids.collect {|cf|
				 allFields.store("#{cf.id}", cf.name)}
			end
			
			cfs = custom_fields.collect {|cf| [cf.name, cf.id] }
			cfs.unshift(["",0])
		else
			cfd = Array.new
			cfd << [ "", 0]			
			cfs = Array.new
			cfs << [ "", 0]
		end
		available_fields = allFields
		selectedfld = Setting.plugin_redmine_wktime['wktime_fields_in_file']
		if !selectedfld.blank?
			selectedfld.each do |field|
				fldInFiles[field] = allFields[field]
			end
			available_fields = allFields.select {|key,value|  !selectedfld.include? key.to_s}
		end
	%>
	<p>
		<%= check_box_tag('settings[wktime_period_end_process]', 1, @settings['wktime_period_end_process'].to_i == 1) %>
		<label><%= l(:label_period_end_process) %></label>
	</p>
</fieldset>
</div>

 
<div class="box tabular settings">
<fieldset class="box">
       <legend><%= l(:button_import) %></legend>
		<p><%= l(:label_redmine_restart_warning) %></p>
	    <p>
			<%= check_box_tag('settings[wktime_auto_import]', 1, @settings['wktime_auto_import'].to_i == 1) %> <label><%= l(:label_auto_import) %></label>
		</p>
			<p>	
			<label><%= l(:label_auto_import_time) %></label>
			<%
			hr = []
			for i in 0..23
				if i < 10
					hr << ['0' + i.to_s,i]
				else
					hr << [i,i]
				end
			end
			%>
			<%= l(:field_hours) %>&nbsp;<%= select_tag('settings[wktime_auto_import_time_hr]', options_for_select(hr, 
			:selected => @settings['wktime_auto_import_time_hr'].blank? ? 23 : @settings['wktime_auto_import_time_hr']))%>
			<%= l(:label_wk_minutes) %>&nbsp;<%= select_tag('settings[wktime_auto_import_time_min]', options_for_select([['00',0],
										   [15, 15],
										   [30, 30],
										   [45, 45]], :selected => @settings['wktime_auto_import_time_min'].blank? ? 0 : @settings['wktime_auto_import_time_min']))%>								   
		</p>		
        <p>
			  <label><%= l(:label_path) %></label><%= text_field_tag 'settings[wktime_file_to_import]', @settings['wktime_file_to_import'], :size => 40 %> 
		</p> 
		 <p>
			<%= check_box_tag('settings[wktime_import_file_headers]', 1, @settings['wktime_import_file_headers'].to_i == 1) %> <label><%= l(:label_import_file_headers) %></label>
		</p>
	    <p>
		  <label><%= l(:label_fields_separator) %></label>
		  <%= select_tag 'settings[wktime_field_separator]', 
				options_for_select([[l(:label_comma_char), ','], [l(:label_semi_colon_char), ';']], :selected => @settings['wktime_field_separator'].blank? ? 0 : @settings['wktime_field_separator']) %>
		</p>
		 <p>
		  <label><%= l(:label_fields_wrapper) %></label>
		  <%= select_tag 'settings[wktime_field_wrapper]',
			  options_for_select([[l(:label_quote_char), "'"], [l(:label_double_quote_char), '"']], :selected => @settings['wktime_field_wrapper'].blank? ? 0 : @settings['wktime_field_wrapper']) %>
		</p>
		 <p>
		  <label><%= l(:label_encoding) %></label>
		  <%= select_tag 'settings[wktime_field_encoding]', options_for_select(Setting::ENCODINGS, :selected => @settings['wktime_field_encoding'].blank? ? 0 : @settings['wktime_field_encoding']) %>
		</p>
		<p>
		  <label><%= l(:setting_date_format) %></label>
		  <% wktime_helper = Object.new.extend(WktimeHelper) %>
		  <%= select_tag 'settings[wktime_field_datetime]', options_for_select(wktime_helper.date_format_options, :selected => @settings['wktime_field_datetime'].blank? ? 0 : @settings['wktime_field_datetime'] ) %>
		</p>	
		<table>
		<thead>
		<tr align='left'>
		<td align='left'><%= l(:label_available_fields) %></td>
		<td></td>
		<td align='left'><%= l(:label_fields_in_file) %></td>
		</tr>
		</thead>
		<tbody>
			<tr valign="top">
				<td>					
					<%= select_tag('settings[wktime_avialable_fields]', options_for_select(available_fields.invert), :multiple => true, :size=> 10, :style => "width:150px; ") %>
				</td>
				<td valign="center">
					<button type="button" onclick="listbox_moveacross('settings_wktime_avialable_fields', 'settings_wktime_fields_in_file')" id="rightmove" style="position:relative; top:40px;">&gt;&gt;</button>
					<br>
					<button type="button" onclick="listbox_moveacross('settings_wktime_fields_in_file', 'settings_wktime_avialable_fields')" id="rightmove" style="position:relative; top:50px;">&lt;&lt;</button>
				</td>
				<td>					
					<%= select_tag('settings[wktime_fields_in_file]', options_for_select(fldInFiles.invert, :selected =>  @settings['wktime_fields_in_file'] ), :multiple => true, :size => 10, :style => "width:150px;") %>
				</td>
			</tr>
		</tbody>
		</table>
 </fieldset>
 </div>
 </div>
 <%= render :partial => 'settings/tab_shift_scheduling' %>
<% if !projArr.blank? %>
<div style = "display:none">
	<%=h select_tag('template_projDD', options_for_select(projArr))%>
</div>
<% end %>