<script type="text/javascript">
$(document).ready(function() {
	
		$("#customers").multiSelect();
			
		function setCustomersSelectVisibility(){
			if ($('#active').val() == 1) {
				$('#customers_row').show();
				$('#managers_row').hide();
			}
			else
			{
				$('#customers_row').hide();
				$('#managers_row').show();				
			}
		}
		setCustomersSelectVisibility();
		
		$('#active').change(function() {
		  setCustomersSelectVisibility();
		  $("#typ").change();
		});
		
			
	
		//typ
		function change_typ($value)
		{
			
			//pracownik
			if ($value == 2)
			{				
				if($('#active').val() == 1)
				{
					$('.pracownik').show();
					$('.pracownik input').removeAttr("disabled");
					$('.pracownik select').removeAttr("disabled");
					$('#customers_row').show();
					$('#managers_row').hide();					
				}
				else
				{
					$('#customers_row').hide();
					$('#managers_row').show();				
				}				
			}//ksiegowa	
			else if($value == 3)
			{
				$('.pracownik input').attr("disabled", true);
				$('.pracownik select').attr("disabled", true);
				$('.pracownik').hide();
			}
			else
				alert('Wystapił błąd: nieprawidłowy typ afilianta');
		}
			
		$("#zwolniony").change(function(){
			if($(this).val() == 1);
				$("#active").val(0);
				
			$('#active').change();	
		});	
			
		$("#typ").change(function(){
				change_typ($("#typ").val());
			});
		$("#typ").change();
});
	
</script>

<?php if(!empty($info))
	echo '<p class="info_box">'.$info.'</p>';
?>
<form action="/master/employee/<?php echo $employee_id ?>#tabs-1" method="post">
	<table width="100%">
		<!-- IMIE -->
		<tr>
			<td width="200">Imię*</td>
			<td>
				<input type="text" name="imie" value="<?php if(!empty($form_data['imie'])) echo $form_data['imie'];?>"/>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['imie'])) echo $errors['imie'];?></span></td>
		</tr>
		<!-- EMAIL -->	
		<tr>
			<td>Email*</td>
			<td>
				<input type="text" name="email" value="<?php if(!empty($form_data['email'])) echo $form_data['email'];?>"/>
			</td>
		</tr>		
		<tr>
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['email'])) echo $errors['email'];?></span></td>				
		</tr>
		<!-- AKTYWNOSC -->
		<tr>
			<td width="200">Aktywny*</td>
			<td>
				<select name="active" id="active">
					<?php
						echo '<option value="1" '.((isset($form_data['active']) && $form_data['active']==1) ? 'selected="selected"':'').'>tak</option>';
						echo '<option value="0" '.((isset($form_data['active']) && $form_data['active']==0) ? 'selected="selected"':'').'>nie</option>';
					?>
				</select>	
			</td>					
		</tr>
		<!-- ZWOLNIONY -->
		<tr>
			<td width="200">Zwolniony*</td>
			<td>
				<select name="zwolniony" id="zwolniony">
					<?php
						echo '<option value="0" '.((isset($form_data['zwolniony']) && $form_data['zwolniony'] == 0) ? 'selected="selected"' :'').'>nie</option>';					
						echo '<option value="1" '.((isset($form_data['zwolniony']) && $form_data['zwolniony'] == 1) ? 'selected="selected"' :'').'>tak</option>';
					?>
				</select>	
			</td>					
		</tr>
		<tr>
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['zwolniony'])) echo $errors['zwolniony'];?></span></td>				
		</tr>						
		<!-- TYPE -->
		<tr>
			<td>Typ*</td>
			<td>
				<?php
					if(isset($employee_types) && count($employee_types)>0)
					{
						echo '<select name="type" id="typ">';
							foreach($employee_types as $k=>$v)
							{
								if($v != 'master')
								echo '<option value="'.$k.'" '.((!empty($form_data['type']) && ($k == $form_data['type'])) ? 'selected="selected"':'').'>'.$v.'</option>';
							}
						echo '</select>';
					}
					else
						echo 'brak danych'
				?>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['type'])) echo $errors['type'];?></span></td>
		</tr>
				
		<!-- HASLO -->
		<tr>
			<td>Nowe hasło</td>
			<td><input type="password" name="haslo" value="<?php if(!empty($form_data['haslo'])) echo $form_data['haslo'];?>"/></td>
			
		</tr>		
		<tr>
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['haslo'])) echo $errors['haslo'];?></span></td>
		</tr>
		<!-- POTWIERDZ HASŁO -->
		<tr>
			<td>Potwierdź nowe hasło</td>
			<td><input type="password" name="haslo2" value="<?php if(!empty($form_data['haslo2'])) echo $form_data['haslo2'];?>"/></td>
		</tr>
		<tr>
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['haslo2'])) echo $errors['haslo2'];?></span></td>
		</tr>	

		<!-- UTRZYMANIE -->
		<tr class="pracownik">
			<td width="200">Utrzymanie*</td>
			<td>
				<select name="utrzymanie">
					<?php
						for($i=10;$i<=40;$i+=5)
							echo '<option value="'.$i.'" '.((!empty($form_data['utrzymanie']) && $form_data['utrzymanie']==$i) ? 'selected="selected"':'').' >'.$i.'%</option>'; 
					?>
				</select>
			</td>			
		</tr>
		<tr class="pracownik">
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['utrzymanie'])) echo $errors['utrzymanie'];?></span></td>
		</tr>
		<!-- RABAT -->
		<tr class="pracownik">
			<td width="200">Rabat*</td>
			<td>
				<input type="text" name="rabat" value="<?php if(isset($form_data['rabat'])) echo $form_data['rabat'];?>"/>
			</td>	
		</tr>
		<tr class="pracownik"> 
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['rabat'])) echo $errors['rabat'];?></span></td>
		</tr>
		<!-- KLIENCI -->
		<tr id="customers_row" class="pracownik" <?php /*echo (!empty($form_data['active']) && $form_data['active']==1) ? 'style="display:block;"':'style="display:none;"' */ ?> >
			<td width="200" valign="top">Klienci</td>
			<td>
				<?php
	
					function is_in_array($val,$arr)
					{
		
						for($i=0;$i<count($arr);$i++)
						{
							if($arr[$i] == $val)
							{
								return TRUE;
							}
						}						
						return FALSE;						
					}				
											
					if(!empty($customers) && count($customers) > 0)
					{
						echo '<select id="customers" name="customers[]" multiple="multiple">';
							foreach($customers as $k=>$v)
								echo '<option value="'.$v['id'].'"  '.((!empty($form_data['customers']) && is_in_array($v['id'], $form_data['customers'])) ? 'selected="selected"':'').'">'.$v['name'].'</option>';
						echo '</select>';
					}
					else
						echo 'brak danych';
					?>
			</td>
		</tr>
	
		<?php
		if(!empty($przepisz_klientow) && ($przepisz_klientow==1))
			{
		?>
			<!-- PRACOWNICY -->
			<tr id="managers_row" class="pracownik" <?php echo (!empty($form_data['active']) && $form_data['active']==1) ? 'style="display:none;"':'style="display:block;"' ?> >
				<td width="200" valign="top">Przepisz klientów*</td>
				<td>
					<?php
						if(isset($employees) && count($employees)>0)
						{
							echo '<select name="new_manager" id="new_manager">';
								foreach($employees as $k=>$v)
								{
									if($v['id'] != $superadmin['id'])
										echo '<option value="'.$v['id'].'" '.((!empty($form_data['new_manager']) && ($v['id']==$form_data['new_manager'])) ? 'selected="selected"':'').'>'.$v['name'].'</option>';
								}
							echo '</select>';
						}
						else
							echo 'brak danych'						
					?>	
				</td>					
			</tr>
		<tr class="pracownik">
			<td></td>
			<td><span class="error_info"><?php if(!empty($errors['new_manager'])) echo $errors['new_manager'];?></span></td>
		</tr>			
		<?php	
			}
		?>
		<tr>
			<td colspan="2" align="left">
                <div class="formHr">&nbsp;</div>
				<input type="submit" value="Zatwierdź" class="formButton"/>
                <input type="button" name="anuluj" value="Anuluj" class="formButton" onClick="document.location.href='<?php echo base_url();?>master/employees';"/>				
				<span class="field_required">* - pole wymagane</span>			
			</td>
		</tr>	
	</table>
</form>