<?php 

			function getGroupScore($title){			
			
				switch ($title) {
					case 'Codzienne':
						$score = '5';
					break;
					case 'Dwa trzy razy w tygodniu':
						$score = '4';
					break;
					case 'Raz w tygodniu':
						$score = '3';
					break;
					case 'Raz w miesiącu':
						$score = '2';
					break;
					case 'Raz w roku':
						$score = '1';
					break;
				}		
				return $score;	
			}
			

function generateSql($table,$data, $method, $where = false, $id = false) {
	
	global $enabledLang;
	
	if($method != 'insert' && $method != 'update') {
		exit("Invalid method passed to processPost()!");
	}

	$tableResult = mysql_query("SHOW FULL COLUMNS FROM `$table`");
	while($trow = mysql_fetch_assoc($tableResult)) {
		$validColumns[] = $trow['Field']; 	
	}
	
	
	
	$excludedKeys = array("password"); 
	foreach($data as $k=>$v) {
	if(!is_array($v)){
		if(!in_array($k,$excludedKeys) && in_array($k,$validColumns)) { 			
			$queryChunks[] = "`$k` = '".$v."'"; 				
			}	
		}	
	}	
	
	
	// lang
	if($enabledLang){		
		$result=mysql_query("SELECT lang FROM lang WHERE `show` = 1 AND lang !='pl' ORDER BY position ASC");
		while($row = mysql_fetch_assoc($result)) {
			$arrayLang[]=$row['lang'];
		}
			
		foreach($data as $k=>$v) {	
			$a=explode("_",$k);			
			if(in_array($a[1],$arrayLang)) { 
				$validColumnsLang[]=$k;		
				$isLang=true;	
			}		
		}		
			
		foreach($data as $k=>$v) {
		if(!is_array($v)){
			if(in_array($k,$validColumnsLang)) {
				$a=explode("_",$k);						
				$queryChunksLang[$a[1]][] = "`$a[0]` = '".$v."'"; 				
			}
		}	
		}		
	}
	
	if($enabledLang) {
		foreach($queryChunksLang as $k=>$v){
	 		$queryStringLang[$k] = implode(", ",$queryChunksLang[$k]);
		}		
	}	
	// lang
	
	
	

	$queryString = implode(", ",$queryChunks); 
	if($method == 'insert') {
	
		$query = "SELECT * FROM $table LIMIT 1";
		//lang
		if($enabledLang && $isLang) {
			mysql_query("INSERT `$table` SET $queryString") or die(mysql_error());
			$id=mysql_insert_id();
			$table=$table."_lang"; 
			foreach($queryStringLang as $k=>$v){
				mysql_query("INSERT `$table` SET $queryStringLang[$k] , `content` = '$id', `lang` = '$k' ") or die(mysql_error());					
			}		
		//lang
		} else {
			$query = "INSERT `$table` SET $queryString";
		}
		
	}
	else {
		if($where) {
			$query = "UPDATE `$table` SET $queryString $where";
			
			// lang
			if($enabledLang){
				$table=$table."_lang";	
				foreach($queryStringLang as $k=>$v){
					$check=mysql_num_rows(mysql_query("SELECT id FROM `$table` WHERE `content` = '$id' AND `lang` = '$k'"));
					if($check > 0){												
						mysql_query("UPDATE `$table` SET $queryStringLang[$k]   WHERE `content` = '$id' AND `lang` = '$k'") or die(mysql_error());
					} else {
						mysql_query("INSERT `$table` SET $queryStringLang[$k] , `content` = '$id', `lang` = '$k' ") or die(mysql_error());
					}	
				}	
			}
			// lang
		}
		else {
			exit("Must specify 'where' field when doing update query using processPost!");
		}
	}
	mysql_query($query) or die(mysql_error());
	return true;
}


	function validateForm($data,$post, $show_errors = false){
		foreach($data as $k => $v){			
			if($v['required'] && empty($post[$v[column]])){
				$errors[]=$v['name'];
			} 
		}
		
		if(!is_array($errors)){
			return  true;
		} elseif($show_errors){
			$string = "Wypełnij wszystkie wymagane pola: ";
			foreach($errors as $e){
				$string.= "<br/> - <strong> ".$e."</strong>";
			}
			return $string;
		}
		
	}


function generateFormInput($input,$post = false, $lang = false, $inputType = false){   
	
	//lang	
	if($lang){
		$lang="_".$lang;
	}	
	if($lang && $inputType=='insert'){
		$value =  $post[$input['column'].$lang];
	} else {
		$value = $post[$input['column']];
	}			
	//lang
	
	switch ($input['type']): 			
		case 'text':
			if(isset($input['limit'])){
				$limit='maxlength="'.$input['limit'].'"';	
				$class='inputlimiter';
			}	
			$string = '<input type="text" name="'.$input['column'].$lang.'"  class="'.$class.'" '.$limit.' value="'.$value.'"  style="width:630px;" />'; 					
		break;
		case 'select':
			if(isset($input['limit'])){
				$limit='maxlength="'.$input['limit'].'"';	
				$class='inputlimiter';
			}	
			$string = '<select name="'.$input['column'].'">'; 	
				foreach($input['data'] as $k=>$v){
					if($k == $value){
						$selected = 'selected="selected"';
					} else {
						$selected = '';
					}
					$string .= '<option value ="'.$k.'" '.$selected.'>'.$v.'</option>'; 
				}
			$string .= '</select>'; 					
		break;		
		
		case 'date':	
			if($post[$input[column]]){
				$value=$post[$input['column']];				
			} else {
				$value=date('Y-m-d');	
			}					
			$string = '<input type="text" name="'.$input['column'].$lang.'" class="datepicker" value = "'.$value.'"  />'; 		
		break;		
		case 'textarea':	
			if(isset($input['limit'])){
				$limit='maxlength="'.$input['limit'].'"';	
				$class='inputlimiter';
			}
			if(isset($input['editor'])){
				$classEditor=' ckeditor';
			}	
			$string = '<textarea name="'.$input['column'].$lang.'" class="'.$class.$classEditor.'" id="textarea'.$lang.'" '.$limit.' style="width:634px; height:100px;" >'.$value.'</textarea>'; 		
		break;						
				
		
	endswitch;	
	
	return $string;
}

function generateForm($table, $post = false,$mode = false){		
	global $moduleTable;
	foreach($table as $k => $v){
		
		echo '
		<tr>
			<td width="100" valign="top"><strong>'.$v['name'].':</strong></td>
			<td>'.generateFormInput($v,$post).'</td>
		</tr>';	
		
		//lang
		if($v['lang']){		
			$result=mysql_query("SELECT lang FROM lang WHERE `show` = 1 AND lang !='pl' ORDER BY position ASC");
			while($row = mysql_fetch_assoc($result)) {
				$table=$moduleTable."_lang";	
				if($mode!=='insert'){		
					$roww=mysql_fetch_assoc(mysql_query("SELECT * FROM $table WHERE lang = '$row[lang]' AND content = $post[id] LIMIT 0,1"));
					$inputType='update';
				} else {
					$roww=$post;
					$inputType='insert';
				}	
				echo '				
				<tr>
					<td width="100" valign="top"><strong>'.$v['name'].'</strong> ('.$row['lang'].'):</td>
					<td>'.generateFormInput($v,$roww,$row['lang'],$inputType).'</td>
				</tr>';		
			}		
		} 	
		//lang					
	}			
}

function generateCMSitem($type,$module,$id, $extra){   
	
	global $basehttp;
	global $basepath;
			
	switch ($type): 			
		case 'edit':				
			$string = '<a href="'.$basehttp.'/admin/modules/'.$module.'/'.$module.'.edit.php?id='.$id.$extra.'" class="tooltip" title="edytuj"><img src="'.$basehtttp.'/admin/gfx/icons/page_edit.png" border="0" /></a>'; 		
		break;
		case 'showItem':				
			$string = '<a href="'.$basehttp.'/admin/modules/'.$module.'/'.$module.'.show.php?id='.$id.$extra.'" class="tooltip" title="pokaż szczegóły"><img src="'.$basehtttp.'/admin/gfx/icons/vcard.png" border="0" /></a>'; 		
		break;		
			
		case 'parent':	
			if($extra){
				$title= $extra;
			} else {
				$title ='podstrony';
			}			
			$string = '<a href="'.$basehttp.'/admin/modules/'.$module.'/'.$module.'.list.php?parent_id='.$id.'" class="tooltip" title="'.$title.'"><img src="'.$basehtttp.'/admin/gfx/icons/folder.png" border="0" /></a>'; 		
		break;					
		case 'image':	
			if($extra){
				$title= $extra;
			} else {
				$title ='zmień zdjęcie';
			}			
			$string = '<a href="'.$basehttp.'/admin/uploadPhotoForm.php?mode='.$module.'&id='.$id.'" class="tooltip" title="'.$title.'"><img src="'.$basehtttp.'/admin/gfx/icons/picture.png" border="0" /></a>';
		break;	
		case 'gallery':				
			$string = '<a href="'.$basehttp.'/admin/modules/'.$module.'_gallery/'.$module.'_gallery.list.php?id='.$id.'" class="tooltip" title="galeria"><img src="'.$basehtttp.'/admin/gfx/icons/picture_add.png" border="0" /></a>'; 	
		break;	
		case 'files':				
			$string = '<a href="'.$basehttp.'/admin/modules/files/files.list.php?id='.$id.'&mode='.$module.'" class="tooltip" title="pliki"><img src="'.$basehtttp.'/admin/gfx/icons/report_disk.png" border="0" /></a>'; 	
		break;
		case 'pdf':				
			$string = '<a href="'.$basehttp.'/getPDF.php?id='.$id.'&mode='.$module.'" class="tooltip" title="Raport"><img src="'.$basehtttp.'/admin/gfx/icons/pdf.png" border="0" /></a>'; 	
		break;		
		
		case 'delete':
			$string ="<a  href=\"javascript:confirmClick('Czy napewno chcesz usunąć ten element?','javascript:deleteItem(\'".$module."\',\'".$id."\');')\" class=\"tooltip\" title=\"usuń\"><img src=\"".$basehtttp."/admin/gfx/icons/cross.png\" border=\"0\" /></a>";
		break;		
		case 'imageShow':
			if($extra) { 
				$src = $basehttp."/media/photos/$module/$id/min/$extra";
				$size = getimagesize($basepath."/media/photos/$module/$id/min/$extra");
				if($size[0] < 100){
					$class='';
					$class2='style="vertical-align:0px; padding-left:5px;"';
				} else {
					$class='class="img_list"';
					$class2='style="vertical-align:20px; padding-left:5px;"';
				}			
				$extraString = '<a href="javascript:deleteImage(\''.$module.'\',\''.$id.'\');" class="tooltip" title="usuń zdjęcie" id="image'.$id.'"><img '.$class2.' src="'.$basehtttp.'/admin/gfx/icons/picture_delete.png" border="0" /></a>';
			} else { 
				$class='class="img_list"';
				$src = "$basehttp/admin/gfx/no_photo.gif";
			}
			$string ='<img src="'.$src.'" '.$class.' id="image2'.$id.'" />'.$extraString;		
		break;
		case 'imageShowItem':
			$src = $basehttp."/media/photos/$module/$id/min/$extra";
			$size = getimagesize($basepath."/media/photos/$module/$id/min/$extra");
			if($size[0] < 100){
				$class='';
			} else {
				$class='class="img_list"';
			}	
			$string ='<img src="'.$src.'" '.$class.' id="image2'.$id.'" />';		
		break;
		case 'show':
			$a=explode("|",$extra);				
			if($a[0] == 0) { 				
				$tooltip='pokaż';
			} else {
				$tooltip='ukryj';
			}	
			$string ="<a  href=\"javascript:javascript:showItem('".$module."','".$a[1]."','".$id."');\" class=\"tooltip\" title=\"".$tooltip."\"><img  id=\"show_".$a[1]."_".$id."\" src=\"".$basehtttp."/admin/gfx/icons/show_".$a[0]."_.png\" border=\"0\" /></a>";	
		break;			
			
	endswitch;		
				
				
		
		
		        
	
	echo $string;

}



function showNote($text,$class) {
	return "<div class=\"note $class\"><p>$text</p></div>";
}


function showSort($type,$cat=false){
		echo '
        <div id="right_top2" style="margin-top:7px;"></div>        
        <div id="right_bg">        
      	<h3>
        <span style="float:left;">
        <img src="'.$basehttp.'/admin/gfx/icons/arrow.png" style="vertical-align:middle; padding-right:3px;" />
        </span>
        <span style="padding-top:0px; display:block; float:left;">Ustaw Kolejność</span>
        </h3>
            <div id="table"> 
			<a href="'.$basehttp.'/admin/sort.php?type='.$type.'&cat='.$cat.'">Kliknij by przejść do narzędzia ustawiania kolejności</a>
          </div>
        </div>        
        <div id="right_bottom"></div> ';    

}


//input sanitization
function mysql_real_escape_array ( $array ) {
   foreach ( $array as $key => $value ) {
       if ( is_array ( $value )) {
           $array [ $key ] = mysql_real_escape_array ( $value );
       }
       else {
           $array [ $key ] = mysql_real_escape_string ( $value );
       }
   }
   return $array ;
}

function showSortGallery($type,$cat = false){
		echo '
        <div id="right_top2" style="margin-top:7px;"></div>        
        <div id="right_bg">        
      	<h3>
        <span style="float:left;">
        <img src="'.$basehttp.'/admin/gfx/icons/arrow.png" style="vertical-align:middle; padding-right:3px;" />
        </span>
        <span style="padding-top:0px; display:block; float:left;">Ustaw Kolejność</span>
        </h3>
            <div id="table"> 
			<a href="'.$basehttp.'/admin/sort.php?type='.$type.'&id='.$cat.'">Kliknij by przejść do narzędzia ustawiania kolejności</a>
          </div>
        </div>        
        <div id="right_bottom"></div> ';    

}





 function DeleteDir($dir)
    {
     
        $fd = opendir($dir);
        if(!$fd) return false;
        while (($file = readdir($fd))!== false)
        {
            if($file =="." || $file== "..") continue;
            if(is_dir($dir."/".$file))
            {
                  DeleteDir($dir."/".$file);
            }
            else
            {
                unlink("$dir/$file");
            }
        }
        closedir($fd);
        rmdir($dir);
    }
	
	

function resize($file=null,$dest=null,$max_width=null,$max_height=null,$quality=80){
    if($file==null || $dest==null){
        echo '<p align=center>Nie wskazano pliku źródlowego lub docelowego.</p>';
        return;
    }
    list($width,$height,$ext) = @GetImageSize($file);
    switch($ext){
        case 1: $src=imageCreateFromGif($file); break;
        case 2: $src=imageCreateFromJpeg($file); break;
        case 3: $src=imageCreateFromPng($file); break;
        default: echo "<p>Nieznany plik ($file).</p>"; return;
    }
    $x_ratio = $max_width / $width;
    $y_ratio = $max_height / $height;
    if( ($width <= $max_width) && ($height <= $max_height) ){
        $tn_width = $width;
        $tn_height = $height;
    }
    elseif (($x_ratio * $height) < $max_height){
        $tn_height = ceil($x_ratio * $height);
        $tn_width = $max_width;
    }
    else{
        $tn_width = ceil($y_ratio * $width);
        $tn_height = $max_height;
    }
    ini_set('memory_limit', '64M');
    $dst = ImageCreateTrueColor($tn_width, $tn_height);
    ImageCopyResampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
    ImageJpeg($dst,$dest,$quality);
    ImageDestroy($src);
    ImageDestroy($dst);
} 


      function cropImage($nw, $nh, $source, $stype, $dest) {
          $size = getimagesize($source);
          $w = $size[0];
          $h = $size[1];
          switch($stype) {
              case 'gif':
              $simg = imagecreatefromgif($source);
              break;
              case 'jpg':
              $simg = imagecreatefromjpeg($source);
              break;
              case 'png':
              $simg = imagecreatefrompng($source);
              break;
          }
          $dimg = imagecreatetruecolor($nw, $nh);
          $wm = $w/$nw;
          $hm = $h/$nh;
          $h_height = $nh/2;
          $w_height = $nw/2;
          if($w> $h) {
              $adjusted_width = $w / $hm;
              $half_width = $adjusted_width / 2;
              $int_width = $half_width - $w_height;
              imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
          } elseif(($w <$h) || ($w == $h)) {
              $adjusted_height = $h / $wm;
            $half_height = $adjusted_height / 2;
              $int_height = $half_height - $h_height;
         imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
          } else {
              imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
            }
          imagejpeg($dimg,$dest,100);
		  
		  }
		  
		  
  
function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
    //getting the image dimensions 
    list($width_orig, $height_orig) = getimagesize($imgSrc);  
    $myImage = imagecreatefromjpeg($imgSrc);
    $ratio_orig = $width_orig/$height_orig;
   
    if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
       $new_height = $thumbnail_width/$ratio_orig;
       $new_width = $thumbnail_width;
    } else {
       $new_width = $thumbnail_height*$ratio_orig;
       $new_height = $thumbnail_height;
    }
   
    $x_mid = $new_width/2;  //horizontal middle
    $y_mid = $new_height/2; //vertical middle
   
    $process = imagecreatetruecolor(round($new_width), round($new_height));
   
    imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
    $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
    imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);

    imagedestroy($process);
    imagedestroy($myImage);
    return $thumb;
}


function dateV($format,$timestamp=null){
	$to_convert = array(
		'l'=>array('dat'=>'N','str'=>array('Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota','Niedziela')),
		'F'=>array('dat'=>'n','str'=>array('styczeń','luty','marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień')),
		'f'=>array('dat'=>'n','str'=>array('stycznia','lutego','marca','kwietnia','maja','czerwca','lipca','sierpnia','września','października','listopada','grudnia'))
	);
	if ($pieces = split('[:/.\ \-]', $format)){	
		if ($timestamp === null) { $timestamp = time(); }
		foreach ($pieces as $datepart){
			if (array_key_exists($datepart,$to_convert)){
				$replace[] = $to_convert[$datepart]['str'][(date($to_convert[$datepart]['dat'],$timestamp)-1)];
			}else{
				$replace[] = date($datepart,$timestamp);
			}
		}
		$result = str_replace($pieces,$replace,$format);
		return $result;
	}
}




function cleanString($str)
{
    $chars = array(
          chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
          chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
          chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
          chr(195).chr(134) => 'AE', chr(195).chr(166) => 'ae',
          chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
          chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
          chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
          chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
          chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
          chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
          chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
          chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
          chr(195).chr(152) => 'O', chr(195).chr(184) => 'o',
          chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
          chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
          chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
          chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
          chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
          chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
          chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
          chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
          chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
          chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
          chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
          chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
          chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
          chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
          chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
          chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
          chr(195).chr(191) => 'y',
          chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
          chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
          chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
          chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
          chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
          chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
          chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
          chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
          chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
          chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
          chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
          chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
          chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
          chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
          chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
          chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
          chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
          chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
          chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
          chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
          chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
          chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
          chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
          chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
          chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
          chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
          chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
          chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
          chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
          chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
          chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
          chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
          chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
          chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
          chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
          chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
          chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
          chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
          chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
          chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
          chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
          chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
          chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
          chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
          chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
          chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
          chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
          chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
          chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
          chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
          chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
          chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
          chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
          chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
          chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
          chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
          chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
          chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
          chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
          chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
          chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
          chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
          chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
          chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
          // Euro
          chr(226).chr(130).chr(172) => 'E',
          // Funt
          chr(194).chr(163) => '');
		  
		  
  
         $str= strtr($str, $chars);
		 
  $str = strip_tags(strtolower($str));

  $trans = array(
    '_'             => '-',
    '\s+'           => '-',
    '[^a-z0-9-]'    => '',
    '-+'            => '-'
  );

  foreach ($trans as $search => $replace)
  {
    $str = preg_replace("#".$search."#i", $replace, $str);
  }

  return trim($str, '-');
}




function tak_nie($parametr)
{

if ($parametr=="Y") {$wartosc="TAK";} else { $wartosc="NIE";} 

return $wartosc;

}

function sprawdz_dane($parametr)
{

if (empty($parametr)) {$wartosc="<i>brak danych</i>";} else { $wartosc=$parametr;} 

return $wartosc;

}


function view_static($parametr1)
{
	//echo "SELECT * FROM `pages_static_lang` WHERE `id`='$parametr1' AND lang='$lang' ";
	$wynik = mysql_query("SELECT * FROM `static` WHERE `id`='$parametr1' ");
	$row = mysql_fetch_array($wynik); 	
	$wartosc = $row[text];

return $wartosc;

}

function view_static_lang($parametr1,$lang)
{
	//echo "SELECT * FROM `pages_static_lang` WHERE `id`='$parametr1' AND lang='$lang' ";
	$wynik = mysql_query("SELECT * FROM `pages_static_lang` WHERE `pages_static_id`='$parametr1' AND lang='$lang' ");
	$row = mysql_fetch_array($wynik); 	
	$wartosc = $row[text];

return $wartosc;

}

function view_static2($parametr1)
{
	//echo "SELECT * FROM `pages_static_lang` WHERE `id`='$parametr1' AND lang='$lang' ";
	$wynik = mysql_query("SELECT * FROM `pages_static` WHERE `id`='$parametr1' ");
	$row = mysql_fetch_array($wynik); 	
	$wartosc = $row[text];

return $wartosc;

}

function view_headers($parametr1)
{
	
	$wynik = mysql_query("SELECT * FROM `headers` WHERE `id`='$parametr1'");
	$row = mysql_fetch_array($wynik); 	
	$wartosc = $row[nazwa];

return $wartosc;

}


function truncate($tekst,$ile) {
                 $znaki = strlen($tekst);
                 if ($znaki>=$ile) {
                 $tnij = substr($tekst,0,$ile);
                 $skrocone = $tnij."...";
                 }
                 else {
                 $skrocone = $tekst;
                 }
                 return $skrocone;
}


function rozszerzenie_pliku($filename)
    {
        preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);

        switch(strtolower($fileSuffix[1]))
        {



            case "jpg" :
            case "jpeg" :
            case "jpe" :    
                return "jpg";
				
            case "bmp" :
                return "bmp";
				
            case "png" :
                return "png";
							
            case "gif" :
                return "gif";
				
								
            case "css" :
                return "css";

            case "xml" :
                return "xml";

            case "doc" :
            case "docx" :
                return "doc";
				

			case "rtf" :
			case "txt" :
                return "txt";
				
            case "xls" :
            case "xlt" :
            case "xlm" :
            case "xld" :
            case "xla" :
            case "xlc" :
            case "xlw" :
            case "xll" :
                return "excel";

            case "ppt" :
            case "pps" :
                return "powerpoint";
            
           
            case "pdf" :
                return "pdf";

            case "html" :
			case "htm" :
                return "html";

            case "php" :
                return "php";
				

            case "js" :
                return "js";
									
          				
		 	case "avi" :
                return "avi";
								
            case "mpeg" :
            case "mpg" :
			case "wmv" :
			case "mov" :
                return "mpg";
				
            case "mp3" :
			case "wav" :
                return "audio";



            case "zip" :
                return "zip";

            case "tar" :
			case "rar" :
                return "rar";

            case "swf" :
                return "flash";

            default :
            if(function_exists("mime_content_type"))
            {
                $fileSuffix = mime_content_type($filename);
            }

            return "";
        }
    }
	


function mask( &$picture, $mask ) {
    // Get sizes and set up new picture
    $xSize = imagesx( $picture );
    $ySize = imagesy( $picture );
    $newPicture = imagecreatetruecolor( $xSize, $ySize );
    imagesavealpha( $newPicture, true );
    imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) );
   
    // Resize mask if necessary
    if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) {
        $tempPic = imagecreatetruecolor( $xSize, $ySize );
        imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) );
        imagedestroy( $mask );
        $mask = $tempPic;
    }

    // Perform pixel-based alpha map application
    for( $x = 0; $x < $xSize; $x++ ) {
        for( $y = 0; $y < $ySize; $y++ ) {
            $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );
            $alpha = 127 - floor( $alpha[ 'red' ] / 2 );
            $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
            imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );
        }
    }
   
    // Copy back to original picture
    imagedestroy( $picture );
    $picture = $newPicture;
}

function jpg2png($originalFile, $outputFile, $quality) {
    $image = imagecreatefromjpeg($originalFile);
    imagepng($image, $outputFile, $quality);
    imagedestroy($image);
}
	
	
function createmask($dir,$mask,$save,$name)
{
	
	$a=explode('.',$name);
	$save_file=$a[0].'.png';
	$source =  jpg2png($dir.$name,$dir.$save_file,0);
	$mask = imagecreatefrompng($mask);
	$source = imagecreatefrompng($dir.$save_file);
	mask( $source, $mask );
	$save=imagepng($source,$save.$save_file );
	return $save_file;

}


function adminBreadcrumbs($path,$item,$title,$type, $item = false){

if($type=='add'){
	$header = 'Dodaj';
}
if($type=='edit'){
	$header = 'Edytuj';
}

if($type=='list'){
	$header = 'Lista';
}

if($item){
	$headerItem = '<a href="#">'.$item.'</a>  ';
}

echo '
<div id="right_top">
    <div id="right_home"></div>
    <div id="right_right">            
    <a href="'.$path.'">'.ucfirst($title).'</a>    
	'.$headerItem.'                      
    <span><a href="#">'.$header.'</a></span>            
    </div>
</div>';
}

function _p($array){
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}

function getTree($parent = 0, $showDisabled = false){
	if($parent){
		$parentSql=" AND `parent_id` = '$parent'";
	} else {
		$parentSql='';
	}
	
	if($showDisabled){
		$enabled ='';
	} else {
		$enabled = "AND `show`= 1 ";
	}
	
	
	$q=mysql_query('SELECT title,parent_id, id,ip,link, depth FROM pages WHERE 1=1 '.$enabled.' '.$parentSql.' ORDER by position');  
	$tab=array();  
	while($r=mysql_fetch_assoc($q)){  
		$tab[$r['parent_id']][$r['id']]=$r;  
		
	
	}  
	return $tab;
}

function generateUrl($type,$name,$id,$ip = false){
	global $basehttp;

  switch($type) {
	  case 'page':	 
	  	$prefix=str_replace(".",",",$ip);
	  	$link=$basehttp."/".cleanString($name)."_".$prefix.".html";
	  break;
	  case 'news':
	  	$link=$basehttp."/aktualnosci/".cleanString($name)."/".$id."/";
	  break;
	  case 'oferty':
	  	$link=$basehttp."/oferty/".cleanString($name)."/".$id."/";
	  break;	  
	  case 'szkolenia':
	  	$link=$basehttp."/szkolenia/".cleanString($name)."/".$id."/";
	  break;	  
	
  }
	return $link;

}


function getTranslate($table,$column,$id){
	global $basehttp;
	global $lang;
	
	if($lang=='pl'){
		$row=mysql_fetch_array(mysql_query("SELECT $column FROM  $table  WHERE `id`='$id'"));	
	} else {
		$table=$table."_lang";
		$row=mysql_fetch_array(mysql_query("SELECT $column FROM  $table  WHERE `content`='$id' AND `lang`='$lang'"));
	}
	
	return $row[0];

}
function displayNav($parentID, &$tab) { 
	$i=0; 
	if (!is_array($tab[$parentID])) return; #czy kategoria ma podkategorie?  
	echo '<ul>';  
	$i++;
	$j=0;
	foreach ($tab[$parentID] as $element) { #iteracja podkategorii  
		$j++;
		if($element['link']){
			$link=$element['link'];
		} else {
			$link=generateUrl('page',$element['title'],$element['id'],$element['ip']);
		}
		if($j==1) {$class=' class="none"';} else {unset($class);}
		$countSub = mysql_num_rows(mysql_query("SELECT id FROM `pages` WHERE `parent_id`='$element[id]' ORDER by `position` ASC "));	
		if($countSub > 0) {$classSub=' class="parent"';} else {$classSub='';}
		echo '<li '.$class.'><a href="'.$link.'" '.$classSub.'>'.$element['title'].'</a>';  
		displayNav($element['id'], $tab); #wyswietlenie podkategorii  
		echo '</li>';  
	}  
	echo '</ul>';  
  
}	

if(function_exists('lcfirst') === false) {
    function lcfirst($str) {
        $str[0] = strtolower($str[0]);
        return $str;
    }
}
function FileSizeConvert($bytes)
{
    $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

    foreach($arBytes as $arItem)
    {
        if($bytes >= $arItem["VALUE"])
        {
            $result = $bytes / $arItem["VALUE"];
            $result = str_replace(".", "." , strval(round($result, 2)))." ".$arItem["UNIT"];
            break;
        }
    }
    return $result;
}



function getStatic($id){
	global $lang;
	global $enabledLang;
	
	$row=mysql_fetch_array(mysql_query("SELECT text, id FROM static WHERE id = $id"));
	if($enabledLang){
		$row['text']=getTranslate('static','text',$row['id']);
	}
	
	return $row['text'];
}

function getTable($table,$order = 'position'){			
	$array = array();	
	$result = mysql_query("SELECT * FROM `$table` ORDER by `$order`");
	while($row = mysql_fetch_array($result)) {
		$array[$row[id]] = $row[title];
	}
	return $array;
}

function getRow($table,$id){			
	$result = mysql_query("SELECT * FROM `$table` WHERE id = '$id' LIMIT 1");
	$array = mysql_fetch_assoc($result);
	return $array;
}

/* ---------- */

function filterRequestVar(&$var, $filterArray){
    
    if(in_array(FILTER_TYPE_POSITIVE, $filterArray) && (intval($var) < 0)){
        return false;
    }
    
    if(in_array(FILTER_TYPE_NO_EMPTY, $filterArray) && !$var ){
        return false;
    }
    
    if(in_array(FILTER_TYPE_STRING, $filterArray) && !is_string($var)){
        return false;
    }
    
    if(in_array(FILTER_TYPE_PREVENT_SQLI, $filterArray)){
        $var = mysql_real_escape_string(trim($var));
    }
    
    if(in_array(FILTER_TYPE_INTEGER, $filterArray) && !is_numeric($var)){
        $var = intval($var);
        return false;
    }
    
    return true;
}

?>