<div id="rightBox">
	<table border="0" cellpadding="0" cellspacing="0" class="rightBox_table">
		<tr>
			<td><img src="/files/contentBoxTop.jpg" alt="" width="690" height="16"/></td>
		</tr>
		<tr>
			<td valign="top" style="background-image: url(/files/contentBoxMiddle.jpg); width: 690px; height: 547px; background-position: center top; background-repeat: no-repeat;">
				<div id="rightBox_content">
					<?php
						if(isset($parent_ticket))
						{
							echo '<div style="border-bottom:1px solid #999999;width:100%">Wiadomość</div>';
							echo '<br/>';
							echo '<fieldset id="filters_0" class="collapsible">
									<legend onclick="toggleFieldset(this,0);">'.$parent_ticket->data_wyslania.' <b>OD</b> '.$parent_ticket->nadawca.' <b>DO</b> '.$parent_ticket->odbiorca.'</legend>
										<div id="fieldset_content_0">';
							
										echo '<table id="hor-minimalist-b" style="width:100%">';
										
											echo '<tr>';
												echo '<td width="140">Temat</td>';
												echo '<td><div class="info_field">'.$parent_ticket->temat.'</div></td>';
											echo '</tr>';
													
											echo '<tr>';
												echo '<td width="140">Wiadomość</td>';
												echo '<td><div class="info_field">'.nl2br($parent_ticket->wiadomosc).'</div></td>';
											echo '</tr>';
											echo '<tr>';
										echo '</table>';
										echo '<br/><br/>';
										
							echo	'</div>
								</fieldset>
								<br/>';										
								
							if(isset($child_tickets))
							{
									foreach($child_tickets as $key=>$value)
									{								
										if($key < (count($child_tickets)-1))
											echo '<fieldset id="filters_'.($key+1).'" class="collapsible collapsed">';
										else
											echo '<fieldset id="filters_'.($key+1).'" class="collapsible">';
											
		    									echo '<legend onclick="toggleFieldset(this,'.($key+1).');">'.$value->data_wyslania.' <b>OD</b> '.$value->nadawca.' <b>DO</b> '.$value->odbiorca.'</legend>';
												
													if($key < (count($child_tickets)-1))		
														echo '<div id="fieldset_content_'.($key+1).'" style="display:none;">';
													else
														echo '<div id="fieldset_content_'.($key+1).'">';	
													
								
													echo '<table id="hor-minimalist-b" style="width:100%">';
		
														echo '<tr>';
															echo '<td width="140">Wiadomość</td>';
															echo '<td><div class="info_field">'.nl2br($value->wiadomosc).'</div></td>';
														echo '</tr>';
														echo '<tr><td colspan="2" align="right" style="border:none;"><a href="#top">TOP</a></td></tr>';		
												echo '</table>';	
										echo	'</div>
		    								</fieldset>
											<br/>';																								
									}	
							}
							echo '<div style="border-bottom:1px solid #999999;width:100%">Odpowiedź</div>';
						}
						if(isset($form_success))
						{
							if($form_success)
								echo '<p class="info_box">Wiadomość została wysłana.<p>';
							else
								echo '<p class="info_box">Wystapił błąd.<p>';								
						}
					?>
					<?php
						if($can_response == true)
						{
									echo '<p class="info_box" id="counter"><p>';
										
									if(isset($parent_ticket))
										echo '<form action="/tickets/response/'.$parent_ticket->id.'" id="add_ticket" name="add_ticket" method="post">';
							?>	
								<script type="text/javascript">
								      $(document).ready(function() {
									  		getMinutes();
										 	setInterval("getMinutes();", 60000);
											
									     });	
										 
										 //przedluz sesje
										function prolongSession()
										{
											  $.get("<?php echo base_url(); ?>tickets/reset_session/<?php echo $parent_ticket->id; ?>", {},
											  function(data){
											  		if(data == 1)
														getMinutes();
													else	
														alert('Wystapił bład !');
											  	});											
										}
									  	//pobierz ilosc minut do konca sesji
										function getMinutes()
										{
												$.get("<?php echo base_url(); ?>tickets/get_minutes_to_end_session/<?php echo $parent_ticket->id; ?>", {},
												  function(data){
												  		if(data == 0)
															window.location = '<?php echo base_url()."tickets/show/".$parent_ticket->id;?>';
														$('#counter').html("Do końca sesji pozostało Ci : "+data+" minut.<div style='cursor:pointer' onclick='prolongSession();'><b>Przedłuż sesję</b></div>");
															
												  });								
										}									  
								</script>
								
								<input type="hidden" name="add_ticket" value="1"/>
								<table id="hor-minimalist-b" style="width:100%">
										<!-- ODBIORCA -->
										<?php
											echo '<input type="hidden" name="user_id" value="'.$user_account['id'].'">';
		
											if($user_account['id'] != $parent_ticket->nadawca_id)
											{
												echo '<input type="hidden" name="odbiorca" value="'.$parent_ticket->nadawca_id.'"/>';
												//echo '<input type="text" readonly="readonly" name="odbiorca_name" value="'.$parent_ticket->nadawca.'"/>';
											}
											else
											{
												echo '<input type="hidden" name="odbiorca" value="'.$parent_ticket->odbiorca_id.'"/>';
												//echo '<input type="text" readonly="readonly" name="odbiorca_name" value="'.$parent_ticket->odbiorca.'"/>';												
											}
											
											echo '<input type="hidden"  value="Re:'.$parent_ticket->temat.'" name="temat" />'; 
											echo '<input type="hidden" name="kategoria" value="'.$parent_ticket->kategoria_id.'"/>';
		
									?>		
										
										<!-- PRIORYTET -->
										<tr><td>Priorytet</td>
											<td>
												<select name="priorytet" style="width:150px;">
													<?php
														foreach($priorytety as $key=>$value)
														{
															echo '<option value="'.$value->id.'"';
																if(isset($form_data['priorytet']))
																{
																	if($form_data['priorytet'] == $value->id)
																		echo ' selected="selected" ';
																} 													
															echo '>'.$value->nazwa.'</option>';													
														} 
													?>											
												</select>	
											</td>
										</tr>
										
										<!-- STATUS -->
										<?php if($user_type == 'admin')
											{
										?>									
												<tr><td>Status</td>
													<td>
														<select name="status" style="width:150px;">
															<?php
																foreach($statusy as $key=>$value)
																{
																	if($value->id == 1)
																		continue;
																	echo '<option value="'.$value->id.'"';
																		if(isset($form_data['status']))
																		{
																			if($form_data['status'] == $value->id)
																				echo ' selected="selected" ';
																		}													
																	echo '>'.$value->nazwa.'</option>';													
																} 
															?>											
														</select>												
													</td>
												</tr>
										<?php		
											}
											else
												echo '<input type="hidden" name="status" value="'.($statusy[1]->id).'"/>';
										?>
										<!-- WIADOMOSC -->
										<tr><td valign="top">Wiadomość</td>
										<td><textarea id="wiadomosc" cols="1" rows="1" name="wiadomosc" style="border: 1px solid black; overflow: hidden; width: 100%; height: 142px; text-align: left;"><?php if(isset($form_data['wiadomosc'])) echo $form_data['wiadomosc']; ?></textarea>
										<br/>
											<span id="wiadomosc_error" class="error_info">
												<?php
													if(isset($errros['wiadomosc']))
													{
														echo $errros['wiadomosc'];										
													}
												?>
											</span>	
										</tr>
										
										<!-- ZALACZNIK -->
										<tr>
											<td valign="top">Załącznik</td>
											<td>
												<script type="text/javascript" src="/files/js/jquery-1.3.2.min.js"></script>
												<script type="text/javascript" src="/files/js/jquery.uploadify.v2.1.0.min.js"></script>
												<script type="text/javascript" src="/files/js/swfobject.js"></script>
												<script type="text/javascript">
													
													
													function validateWiadomosc()
													{
														isValid = true;
														if(jQuery.trim($("#wiadomosc").val()) == '')
															{
																$('#wiadomosc_error').text("Pole wiadomosc nie może być puste");
																isValid = false;
															}	
															else
															{
																$('#wiadomosc_error').text("");
															}											
														return isValid;
													}
													function validateTemat()
													{
														isValid = true;
														if(jQuery.trim($("#temat").val()) == '')
															{
																$('#temat_error').text("Pole temat nie może być puste");
																isValid = false;
															}
														else
															{
																$('#temat_error').text("");
															}																				
														return isValid;												
													}
													
									
													
													
													$(document).ready(function() {
														    var upload 		= false;
															var file_number = 0; 
															var currentDateTime = currentDateTime();
															$("#add_ticket").submit(function() {
																
																if(!validateWiadomosc())
																	return false;
																
																if (!upload) {
																	return true;
																}
																else {
																	$('#uploadify').uploadifyUpload();
																	return false;
																}
															})
														
															function addFileToList(id,filename)
															{
																elem = "<input type='hidden' id="+id+" name='files[]' value='"+currentDateTime+filename+"'/>";							
																var htmlStr = $("#files").html();
		      													$("#files").html(htmlStr+elem);
																file_number++;
			
															}
															
															$("#wiadomosc").focus(function () {
														         $('#temat_error').text("");
														    });
															$("#wiadomosc").blur(function () {
														       validateWiadomosc();
														    });	
																
															
																										
		
															
															function clearFileList()
															{
																var htmlStr = $("#files").html();
		      													$("#files").html('');
																file_number = 0;
															} 
															
															function currentDateTime()
															{
																var dteNow = new Date();
																
																var day = dteNow.getDate()
																var month = dteNow.getMonth()+1;
																var year = dteNow.getFullYear();
																var hour = dteNow.getHours();
																var minute= dteNow.getMinutes();
																var second = dteNow.getSeconds();
																return year+'.'+month+'.'+day+'_'+hour+':'+minute+':'+second+'__';
						
															}
															
														$("#uploadify").uploadify({
															'uploader'       : '/files/uploadify/uploadify.swf',
															'script'         : '/files/uploadify/uploadify.php',
															'checkScript'	 : '/files/uploadify/check.php',  
															'cancelImg'      : '/files/uploadify/cancel.png',
															'buttonText'	 : 'Select Files', 
															'folder'         : '/files/uploads',
															'queueID'        : 'fileQueue',
															'sizeLimit'		 : '1024',
												            'onError'   : 		function (a, b, c, d) {
																                 if (d.status == 404)
																                    alert('Could not find upload script');
																				 else if (d.type === "HTTP")
																                   alert('error '+d.type+": "+d.status);
																                 else if (d.type ==="File Size")
																                   alert(c.name+' '+d.type+' Limit:+Math.round(d.sizeLimit/1024)');
																                 else
																                   alert('error '+d.type+": "+d.text);             
																              },													
															'scriptData' 	 : {'filename_prefix':currentDateTime},
															'onClearQueue'	 : function(a) {clearFileList(); upload = false;},
															'onSelect'		 : function(a,b,c){upload = true;},
							         						'onComplete'	 : function(a, b, c, d, e){ addFileToList(b,c.name);},  
		         											'onAllComplete'  : function(event,data){  
																					upload = false;
		            							  									$("form#add_ticket").submit();
		         																},  													
															'auto'           : false,
															'multi'          : true
														});
													});
													</script>
													<div id="fileQueue"></div>
													<div id="files"></div>
													<input type="file" name="uploadify" id="uploadify" />
													<a class="anuluj_upload_form" href="javascript:jQuery('#uploadify').uploadifyClearQueue()">Anuluj</a>																				
											</td>
										</tr>
										<tr><td colspan="2" align="right" style="border:none;"><a href="#top">TOP</a></td></tr>	
										<tr><td colspan="2" style="border-bottom:0px;">
												<div class="ticket_menu_item"><a href="/tickets">Anuluj</a></div>
												<input class="ticket_menu_item" type="image" id="submit_add_ticket" value="Wyślij" />
											</td>
										</tr>
									</tbody>
								</table>	
							</form>
					<?php
						}
						else
						{
							echo '<p class="info_box">Wiadomość jest już obsługiwana przez jednego z pracowników.<br/>Możliwość odpowiedzi jest zablokowana.<p>';							
						}
					?>														
				</div>
			</td>
		</tr>
	</table>
</div>									