HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/php/sitefunctions.php
<?php

//////// temp /////////
function randomImage(){
	for($i=1;$i<=6;$i++){
		$img[$i] = '/images/temp/product-thumb'.$i.'.png';
	}
	$rand = round(rand(1,6));
	return $img[$rand];
}

function checkCustomerLogin(){
	//access the global sql object
	global $mysqli;
	
	$login = 0;
	if(isset($_SESSION['customerstr'])){	
		//get the admins
		$temp = explode('_',$_SESSION['customerstr']);
		$customerId = $temp[0];
		$query = 'SELECT id, personalId, email, hash, status FROM shop_customer_login WHERE id="'.$customerId.'" LIMIT 1';
		if(!$result = $mysqli->query($query))
		{
			//error handling
		}
		else{
			$record = $result->fetch_assoc();
			$str = $record['id'].'_'.md5($record['hash']);
			if($str == ( $_SESSION['customerstr'] )){
				if($record['status'] == 1){				
					$login = 1; // 1 stands for correct login
				}
				else{
					$login = 2; // 2 stands for inactive account
				}
			}
		}
	}	
	
	return $login;
}

function getNumberFromLetter($letter){
	switch(strtolower($letter)){
		case 'a' : $number = 1; break; case 'b' : $number = 2; break; case 'c' : $number = 3; break; case 'd' : $number = 4; break; case 'e' : $number = 5; break; case 'f' : $number = 6; break; 
		case 'g' : $number = 7; break; case 'h' : $number = 8; break; case 'i' : $number = 9; break; case 'j' : $number = 10; break; case 'k' : $number = 11; break; case 'l' : $number = 12; break; 
		case 'm' : $number = 13; break; case 'n' : $number = 14; break; case 'o' : $number = 15; break; case 'p' : $number = 16; break; case 'q' : $number = 17; break; case 'r' : $number = 18; break; 
		case 's' : $number = 19; break; case 't' : $number = 20; break; case 'u' : $number = 21; break; case 'v' : $number = 22; break; case 'w' : $number = 23; break; case 'x' : $number = 24; break; 
		case 'y' : $number = 25; break; case 'z' : $number = 26; 
		default : $number = 0;
	}
	return $number;
}

function showAllProducts($numCol = 6, $var = ''){
	include './lang/'.$_SESSION['lang'].'.php';
	global $mysqli;


    include_once $_SERVER['DOCUMENT_ROOT'] . '/php/discount/DiscountService.php';
    $discountService = new DiscountService();

    // check url
    $returnUrl = '/';
    if(defined('URL_PAGE')) $returnUrl = '/' . URL_PAGE;
    if(defined('URL_SUB')) $returnUrl .= '/' . URL_SUB;
    if(defined('URL_SUB2')) $returnUrl .= '/' . URL_SUB2;
    $_SESSION['product_return_url'] = $returnUrl;

	$output = '';
	// for brand id check the database \\
	if(defined('URL_SUB') && URL_SUB == 'van-het-merk'){
		
		$brandfiltername = URL_SUB2;
		//search for id
		$brandId = 0;
		$titleQuery = 'SELECT b.id, b.title
				  FROM merken AS b, content_status AS s
				  WHERE s.active = 1
				  AND s.itemId = b.id
				  AND s.linkname = "merken"
				  ORDER BY b.itemOrder DESC';
		if($titelResult = $mysqli->query($titleQuery)){
			while($titleRecord = $titelResult->fetch_assoc()){
				$title = fromDatabase($titleRecord['title']);
				$linkTitle = linkname($title);
				if($linkTitle == URL_SUB2){
					$brandId = $titleRecord['id'];
				}
			}
		}	
	}
	
	// for category id check the database \\
	if(defined('URL_SUB') && URL_SUB == 'uit-de-categorie'){
		$catfiltername = URL_SUB2;
		//search for id
		$catId = 0;
		$titleQuery = 'SELECT b.id, b.category, b.categoryEn
				  FROM product_categories AS b, content_status AS s
				  WHERE s.active = 1
				  AND s.itemId = b.id
				  AND s.linkname = "product_categories"';
		if($titelResult = $mysqli->query($titleQuery)){
			while($titleRecord = $titelResult->fetch_assoc()){
				$title = fromDatabase($titleRecord['category']);
				$titleEn = fromDatabase($titleRecord['categoryEn']);
				$linkTitle = linkname($title);
				$linkTitleEn = linkname($titleEn);
				if($linkTitle == URL_SUB2 || $linkTitleEn == URL_SUB2){
					$catId = $titleRecord['id'];
				}
			}
		}	
	}
	////////////// QUERY \\\\\\\\\\\\\\
				
	$query =  'SELECT DISTINCT p.id AS productId, p.titleNL, p.published, p.catId, p.brandId, p.price, p.discountPrice, p.isNew, p.isExtraLeuk, p.color, p.thumb, p.hover, r.route_nl, r.route_en,p.productOrder
                    FROM product_instances AS i, product_products AS p, content_status AS s, routes AS r
                    WHERE s.itemId = i.productId
                    AND s.active = 1
                    AND s.linkname = "product_products"
                    AND p.id = i.productId
                    AND p.id = r.routeable_id
                    AND r.routeable_type = "product"
                    AND p.published = 1';
                    if($var == 'extraleuk') $query  .= ' AND p.isExtraLeuk = 1';
                    if(isset($brandId) && !empty($brandId)) $query .= ' AND p.brandId = '.$brandId;
                    if(isset($catId) && !empty($catId)) $query .= ' AND p.catId = '.$catId;
                    if( (defined('URL_SUB') && URL_SUB == 'nieuw') ||  ! defined('URL_PAGE') ) $query .= ' AND p.isNew = 1 ';

						$query .= ' ORDER BY p.productOrder DESC';
    if($phresult = $mysqli->query($query)){
		
		////////////// SETTINGS \\\\\\\\\\\\\\
		
		$numitems = $phresult->num_rows;
		$page = 1;	

		$from = (($page-1)*$numitems)+1;
        $to = $from + $numitems;

		$_SESSION['colorCatArr'] = array();

		////////////// ACTUAL ITEMS \\\\\\\\\\\\\\
		
		$output .= '<div class="product-grid">';
			
			# load all instances from the database
			$i = $from;
			$result = $mysqli->query($query);
			while($record = $result->fetch_assoc()){
				$published = $record['published'];
				if($published == 1){
					#get id's
					$productId = $record['productId'];
					$brandId = $record['brandId'];
					
					$hexcolor = $record['color'];
					$title = $record['titleNL'];
					$linkTitle = linkname($title);

                    $thumbShortCode = $record['thumb'];
                    $hoverShortCode = $record['hover'];

					$route = fromDatabase($record['route_nl']);

					#price
					$price = $record['price'] * 100;

					#check discount
					$discountPrice = $record['discountPrice'];
					if(!empty($discountPrice)){
						// het product is hard afgeprijsd
                        $oldPrice = $price;
                        $price = $discountPrice * 100;
					}
					else{

						/*// als het product niet hard afgeprijsd is.
						if($per = checkProductDiscount($productId)){
							$tempAmount = ($price * $per) / 100;
							if($checkAmount = checkPrice($tempAmount)){
								//$discount = $checkAmount;
								//$discountPrice = $price - $discount;
								//$displayDiscountPrice = displayPrice($discountPrice);
							}
						}*/

                        $oldPrice = 0;
                        $discountFactor = $discountService->getDiscountFactorOnProduct($productId);
                        if($discountFactor > 0 && $discountFactor < 1)
                        {
                            $oldPrice = $price;
                            $price = round($price * $discountFactor);
                        }
					}
                    $displayOldPrice = displayPrice($oldPrice / 100, false);
                    $displayPrice = displayPrice($price / 100, false);

					#brand
                    $brand = $brandlink = '';
					$brandQuery = 'SELECT b.id, b.title
									  FROM merken AS b
									  WHERE b.id = '.$brandId.' LIMIT 1';
					if($brandResult = $mysqli->query($brandQuery))
                    {
                        $brandRecord = $brandResult->fetch_assoc();
                        $brand = fromDatabase($brandRecord['title']);
                        $brandlink = linkname($brand);
                    }
					#color category
					$hexR = substr($hexcolor,0,2);
					$hexG = substr($hexcolor,2,2);
					$hexB = substr($hexcolor,4,2);
		
					$decR = hexdec($hexR);
					$decG = hexdec($hexG);
					$decB = hexdec($hexB);
					
					$hsl = rgbhsv($decR,$decG,$decB);
					$colorName = returnColorName($hsl);
					$colorCat = returnColorCat($colorName);

                    $new = $record['isNew'];


                    #get thumbnail
					$thumb = '';
					$mediaTitle = '';
					#new
					$thumbQuery =  'SELECT title, filename_medium, filename_small
										FROM media_files
										WHERE shortcode = "'.$thumbShortCode.'" LIMIT 1';
					if($thumbResult = $mysqli->query($thumbQuery)){
						$thumbRecord = $thumbResult->fetch_assoc();
						$thumb = '/images/uploads/'.$thumbRecord['filename_medium'];
						if( ! empty($thumbRecord['filename_small']) &&
							is_file(DOCUMENT_ROOT . '/images/uploads/'.$thumbRecord['filename_small']))
						{
							list($width) = getimagesize(DOCUMENT_ROOT . '/images/uploads/'.$thumbRecord['filename_small']);
							if($width > 200)
								$thumb = '/images/uploads/'.$thumbRecord['filename_small'];
						}
						$mediaTitle = $thumbRecord['title'];
					}

                    # hover ?
                    if($hoverShortCode != $thumbShortCode && ! empty($hoverShortCode))
                    {
                        $hoverQuery =  'SELECT filename_medium, filename_small
										FROM media_files
										WHERE shortcode = "'.$hoverShortCode.'" LIMIT 1';
                        if($hoverResult = $mysqli->query($hoverQuery)){
                            $hoverRecord = $hoverResult->fetch_assoc();
							$hover = '/images/uploads/'.$hoverRecord['filename_medium'];

                            if(!empty($hoverRecord['filename_small'])
								&& is_file(DOCUMENT_ROOT . '/images/uploads/'.$hoverRecord['filename_small']) )
							{
								list($width) = getimagesize(DOCUMENT_ROOT . '/images/uploads/'.$thumbRecord['filename_small']);
								if($width > 200)
									$hover = '/images/uploads/'.$hoverRecord['filename_small'];
							}
                        }
                    }
                    else
                    {
                        $hover = '';
                    }

					#fallback no thumb
					if($thumbResult->num_rows == 0){ $thumb = '/images/structure/noimage.jpg'; }
				
					#output thumb
					$thumbElementStyle = '';
					//if(($i) % $numCol == 0) $thumbElementStyle = ' style="margin-right: 0"';


                    $output .= '<a href="/' . $route . '" id="item'.$i.'" class="product-cell"'.$thumbElementStyle.' data-color="'.$colorCat.'">';

                        // Product
                        $output .= '<div class="product-thumb-image">';

                            // Loading icon
                            $output .= '<img src="/images/structure/loading.gif" alt="deense kroon laadt '.$mediaTitle.'" class="loading" />';

                            // New label
                            if($new == 1) $output .= '<div class="product-thumb-new">NEW</div>';

                            // Hover image
                            if(!empty($hover)) $output .= '<img src="/images/structure/spacer.gif" data-src="'.$hover.'" alt="Deense Kroon - porseleinen sieraden en accesoires - '.$mediaTitle.'" class="collection_hover preload" id="product'.$productId.'"/>';

                            // Normal image
                            $output .= '<img src="/images/structure/spacer.gif" data-src="'.$thumb.'" alt="Deense Kroon - porseleinen sieraden en accesoires - '.$mediaTitle.'" class="non-hover preload"/>';

                        $output .= '</div>';

                        // Title / price
                        $output .= '<div class="product-thumb-title">';

                            $output .= '<span>'.$title.'</span>';

                            // Price
                            if(empty($oldPrice))
                            {
                                $output .= '<span class="product-thumb-price">'.$displayPrice.'</span>';
                            }
                            else{
                                $output .= '<span class="product-thumb-price product-price-line-through">' . $displayOldPrice . '</span>
                                            <span class="product-thumb-price-discount"> ' . $displayPrice . '</span>';
                            }

                        $output .= '</div>';

					$output .= '</a>';


					$i++;
				} //end published
			} //end while
		//end collection holder
		$output .= '</div>';
	}

	return $output;
}


function validateAddProductsFromGet($type){
	global $mysqli;
	
	// add from post (from page_products.php) \\
	$numProducts = 1;
	
	defined('URL_PAGE') ? $from = URL_PAGE : $from = 'home';
	
	if($type == 'instance'){
		$instanceId = URL_SUB2;
	}
	else if($type == 'product'){
		$productId = URL_SUB2;
		$instanceQuery = 'SELECT i.id AS instanceId
						FROM product_instances AS i
						WHERE productId = '.$productId.'
						ORDER BY i.instanceOrder DESC LIMIT 1'; 
		if($instanceResult = $mysqli->query($instanceQuery)){ 
			if($instanceResult->num_rows == 0){
				//error niet gevondemn
			}
			else{
				$record = $instanceResult->fetch_assoc();
				$instanceId = $record['instanceId'];
			}
		}
	}
	
	if(empty($numProducts) || empty($instanceId)){
		$errors[] = 'empty values';
	}
	
	// num products must be above 0 \\
	if($numProducts <= 0){
		$errors[] = 'nothing to add';
	}
	
	// num products must be numeric \\
	if(!is_numeric($numProducts)){
		$errors[] = 'not numeric';
	}
	
	// does the instanceId exists? \\
	$instanceQuery = 'SELECT DISTINCT i.id AS instanceId, i.productId, p.titleNL
						FROM product_instances as i, product_products as p
						WHERE i.id = "'.$instanceId.'"
						AND p.id = i.productId LIMIT 1'; 
	if($instanceResult = $mysqli->query($instanceQuery)){ 
		if($instanceResult->num_rows == 0){
			$errors[] = 'weird instance Id';
		}
		else{
			$record = $instanceResult->fetch_assoc();
			$title = linkname($record['titleNL']);
			$productId = $record['productId'];
		}	
	}
	else{
		$errors[] = 'query error';
	}
	
	if(isset($errors)){
		//do nothing
		//header('location: ./');
		foreach($errors as $key => $error){
			echo $mysqli->error;
		}
	}
	else{
		if(isset($_SESSION['cart']['products'][$instanceId])){
			$_SESSION['cart']['products'][$instanceId] = $_SESSION['cart']['products'][$instanceId] + $numProducts;
		}
		else{
			$_SESSION['cart']['products'][$instanceId] = $numProducts;
		}			
		
		updateTotalPrice();
		
		if($from == 'toont-het-artikel'){
			$location = '/'.$from.'/'.$title.'-'.$productId.'/';
		}
		else{
			$location = '/'.$from.'/';
		}
		header('location: '.$location);
	}
}


function updateTotalPrice(){
	global $mysqli;


    include_once $_SERVER['DOCUMENT_ROOT'] . '/php/discount/DiscountService.php';
    $discountService = new DiscountService();

	//reset cart session
	$_SESSION['cart']['validate']['cart'] = false;
	
	//reset discount sessionm
	$_SESSION['cart']['discountcode']['code'] = '';	
	$_SESSION['cart']['discountcode']['amount'] = 0;
	$_SESSION['cart']['discountcode']['message'] = '';
	$_SESSION['cart']['discountcode']['freeshipnl'] = false;
	
	if(!empty($_SESSION['cart']['products'])){
		
		$i = 0;
		
		$subtotalprice = 0;
		$totalDiscount = 0;
		if(!isset($discount)) $discount = 0;
		
		foreach($_SESSION['cart']['products'] as $instanceId => $numProducts){
			//get information for each instance in the session
			
			$instanceQuery =  'SELECT DISTINCT i.id AS instanceId, i.color, i.shortcode, i.productId, m.filename_medium
								FROM product_instances AS i, media_files AS m
								WHERE m.shortcode = i.shortcode
								AND i.id = "'.$instanceId.'"
								LIMIT 1';
			if($instanceResult = $mysqli->query($instanceQuery)){
				$instanceRecord = $instanceResult->fetch_assoc();
				$thumb = '/images/uploads/'.$instanceRecord['filename_medium'];
				$color = $instanceRecord['color'];
				$productId = $instanceRecord['productId'];
				
				$productQuery =  'SELECT titleNL,titleEN, price, discountPrice, published FROM product_products WHERE id = "'.$productId.'" LIMIT 1';
				if($productResult = $mysqli->query($productQuery)){
					$productRecord = $productResult->fetch_assoc();
						
					if($productRecord['published'] == 1){
					
						$title = $productRecord['titleNL'];
						$titleLink = linkname($title);
					
						// price \\

						/*
						// does this product has a discount rule ? \\
						$discountper = checkProductDiscount($productId);				
						//discount * numproducts
						if($discountper){
							$tempAmount = ($productRecord['price'] * $discountper) / 100;
							$tempAmount = $tempAmount * $numProducts;
							if($checkAmount = checkPrice($tempAmount)){
								$discount = $checkAmount;
								$totalDiscount += $discount;
							}
						}*/

                        $price = ($productRecord['price'] * $numProducts);
                        //DISCOUNT
						$discountPrice = $productRecord['discountPrice'];
						if( ! empty($discountPrice)){
							// het product is hard afgeprijsd
                            $price = $discountPrice;
						}
						else{
							// als het product niet hard afgeprijsd is.
							/*if($per = checkProductDiscount($productId)){ // stond eerst als commentaar
								$tempAmount = ($price * $per) / 100;
								if($checkAmount = checkPrice($tempAmount)){
									$discount = $checkAmount;
								}
							}*/

                            $discountFactor = $discountService->getDiscountFactorOnProduct($productId);
                            if($discountFactor > 0 && $discountFactor < 1)
                            {
                                $price = round(( $price * 100) * $discountFactor) / 100;
                            }

						}
						
						$subtotalprice = $subtotalprice + $price;

					}
					$i++;
				}
			}
		}//end for each
		
		// subtotal \\
		$totalprice = $subtotalprice;
		
		// discount \\
		if($totalDiscount > 0){
			if(($subtotalprice - $totalDiscount) > 0){
				$totalprice = $subtotalprice - $totalDiscount;
			}
		}	
		
		$_SESSION['cart']['totalprice'] = $totalprice;
			
		// display prices \\
		$displaySubtotalPrice = displayPrice($subtotalprice);
		$displayTotalPrice = displayPrice($totalprice);
		$displayDiscountPrice = displayPrice($totalDiscount);
	}
	else{
		$_SESSION['cart']['totalprice'] = 0;
	}
}


function showPopular($numCol = 6){
	global $mysqli;
	
	include './lang/'.$_SESSION['lang'].'.php';


    include_once $_SERVER['DOCUMENT_ROOT'] . '/php/discount/DiscountService.php';
    $discountService = new DiscountService();
	
	/* load & display popular collection */
	$output = '';
	$output .= '<div class="collection-title">';
		$output .= '<span class="icon-heart-bullet"></span>';
		$output .= '<h2>popular</h2>';
	$output .= '</div>';

	switch($numCol){
		case 6:
			$colClass = "hextuple-column";
		break;
		case 5:
			$colClass = "quintuple-column";
		break;
		case 4:
			$colClass = "quadruple-column";
		break;
	}
	$output .= '<div class="'.$colClass.'">';
		/* start first row */
		$output .= '<div class="product-thumb-row">';
			/* load products from database */
			$query = 'SELECT p.id, p.catId, p.titleNL, p.isNew, p.published, p.price, p.brandId, p.discountPrice, p.thumb, p.hover, pm.modified
				  FROM product_products as p, content_status as s, popular_modified AS pm
				  WHERE s.active = 1
				  AND s.itemId = p.id
				  AND s.linkname = "product_products"
				  AND pm.productId = p.id
				  AND p.published = 1
				  ORDER BY pm.total DESC LIMIT '.$numCol;
				  
			if($result = $mysqli->query($query)){
			$i = 1;
			while($record = $result->fetch_assoc()){

                //create productid
                $id = $record['id'];
                $brandId = $record['brandId'];
                $title = $record['titleNL'];
                $linkTitle = linkname($title);


                //price
                $price = $record['price'] * 100;

                //DISCOUNT
                $discountPrice = $record['discountPrice'];
                $oldPrice = 0;

                if(!empty($discountPrice)){
                    // het product is hard afgeprijsd
                    $oldPrice = $price * 100;
                    $price = $discountPrice * 100;
                }
                else{
                    // als het product niet hard afgeprijsd is.
                    /*if($per = checkProductDiscount($id)){
                        $tempAmount = ($price * $per) / 100;
                        if($checkAmount = checkPrice($tempAmount)){
                            $discount = $checkAmount;
                            $discountPrice = $price - $discount;
                            $displayDiscountPrice = displayPrice($discountPrice);
                        }
                    }*/

                    $discountFactor = $discountService->getDiscountFactorOnProduct($id);
                    if($discountFactor > 0 && $discountFactor < 1)
                    {
                        $oldPrice = $price;
                        $price = round($price * $discountFactor);
                    }
                }
                $displayPrice = displayPrice($price / 100, false);
                $displayOldPrice = displayPrice($oldPrice / 100, false);



                //BRAND
                $brandQuery = 'SELECT b.id, b.title
                                  FROM merken AS b
                                  WHERE b.id = '.$brandId.' LIMIT 1';
                $brandResult = $mysqli->query($brandQuery);
                $brandRecord = $brandResult->fetch_assoc();
                $brand = fromDatabase($brandRecord['title']);
                $brandlink = linkname($brand);



                //new ?
                $new = $record['isNew'];

                //thumbnail
                $thumbShortCode = $record['thumb'];
                $hoverShortCode = $record['hover'];

                $thumb = '';
                $thumbQuery =  'SELECT title, filename_medium, filename_small
                                    FROM media_files
                                    WHERE shortcode = "'.$thumbShortCode.'" LIMIT 1';
                if($thumbResult = $mysqli->query($thumbQuery)){
                    $thumbRecord = $thumbResult->fetch_assoc();
                    #thumb
                    if(!empty($thumbRecord['filename_small']) && is_file(DOCUMENT_ROOT . '/images/uploads/'.$thumbRecord['filename_small']) ){ $thumb = '/images/uploads/'.$thumbRecord['filename_small']; }
                    else{ $thumb = '/images/uploads/'.$thumbRecord['filename_medium']; }
                }

                # hover ?
                if($hoverShortCode != $thumbShortCode && ! empty($hoverShortCode))
                {
                    $hoverQuery =  'SELECT filename_medium, filename_small
                                    FROM media_files
                                    WHERE shortcode = "'.$hoverShortCode.'" LIMIT 1';
                    if($hoverResult = $mysqli->query($hoverQuery)){
                        $hoverRecord = $hoverResult->fetch_assoc();
                        #hover
                        if(!empty($hoverRecord['filename_small']) && is_file(DOCUMENT_ROOT . '/images/uploads/'.$hoverRecord['filename_small']) ){ $hover = '/images/uploads/'.$hoverRecord['filename_small']; }
                        else{ $hover = '/images/uploads/'.$hoverRecord['filename_medium']; }
                    }
                }
                else
                {
                    $hover = '';
                }

                if(empty($thumb)){
                    // if no image
                    $thumb = '/images/structure/noimage.jpg';
                }

                /* output */
                $output .= '<div class="product-cell ';
                    if(!($i % 6) ) $output .= ' no-margin';
                $output .='">';

                    //output image
                    $output .= '<div class="product-thumb-image"';
                        //on hover?
                        //if(!empty($hover)) $output .= ' onmouseover="hoveroverProduct(\'product'.$id.'\');" onmouseout="hoveroutProduct(\'product'.$id.'\');"';
                    $output .= '>';
                        //new ?
                        if($new == 1) $output .= '<div class="product-thumb-new"><h3>'.$siteLabels['mm-new'].'</h3></div>';
                        $output .= '<a href="/toont-het-artikel/'.$linkTitle.'-'.$id.'/">';
                            if(!empty($hover)) $output .= '<img src="'.$hover.'" alt="Deense Kroon sieraad genaamd '.$title.'" class="collection_hover" id="product'.$id.'"/>';

                            $output .= '<img src="/images/structure/spacer.gif" data-src="'.$thumb.'" alt="Deense Kroon sieraad genaamd '.$title.'" class="preload" />';
                        $output .= '</a>';
                    $output .= '</div>';
                    //output title
                    $output .= '<div class="product-thumb-title">
                                        <a href="/toont-het-artikel/'.$linkTitle.'-'.$id.'/">'.$title.'</a>
                                        <span class="collection-brand">
                                            <a href="/toont-haar-collectie/van-het-merk/'.$brandlink.'/">'.$brand.'</a>
                                        </span>
                                    </div>';
                    //output bottom
                    $output .= '<a href="/toont-het-artikel/'.$linkTitle.'-'.$id.'/"><span class="icon-arrow"></span></a>';

                    if(empty($oldPrice))
                    {
                        $output .= '<span class="product-thumb-price">'.$displayPrice.'</span>';
                    }
                    else
                    {
                        $output .= '<span class="product-thumb-price product-price-line-through">'.$displayOldPrice.'</span>
                        <span class="product-thumb-price-discount">'.$displayPrice.'</span>';
                    }

                $output .= '</div>';
                $i++;
			}
		}
			$output .= '<div class="clear"></div>';
		/* end row */
		$output .= '</div>';
	$output .= '</div>';
	$output .= '<div class="clear"></div>';
	
	return $output;
}


function rgbhsv($r, $g, $b)  // rgb values:Number 0-255
{                                 // hsv results:Number 0-1
   $hsl = array();

   $var_r = ($r / 255);
   $var_g = ($g / 255);
   $var_b = ($b / 255);

   $varMin = min($var_r, $var_g, $var_b);
   $varMax = max($var_r, $var_g, $var_b);
   $delMax = $varMax - $varMin;

   $v = $varMax;

   if ($delMax == 0)
   {
      $h = 0;
      $s = 0;
   }
   else
   {
      $s = $delMax / $varMax;

      $del_r = ( ( ( $varMax - $var_r ) / 6 ) + ( $delMax / 2 ) ) / $delMax;
      $del_g = ( ( ( $varMax - $var_g ) / 6 ) + ( $delMax / 2 ) ) / $delMax;
      $del_b = ( ( ( $varMax - $var_b ) / 6 ) + ( $delMax / 2 ) ) / $delMax;

      if      ($var_r == $varMax) $h = $del_b - $del_g;
      else if ($var_g == $varMax) $h = ( 1 / 3 ) + $del_r - $del_b;
      else if ($var_b == $varMax) $h = ( 2 / 3 ) + $del_g - $del_r;

      if ($h<0) $h++;
      if ($h>1) $h--;
   }

   $hsl['h'] = $h;
   $hsl['s'] = $s;
   $hsl['v'] = $v;

   return $hsl;
}

function returnColorName($hsl){
	
	#hue
	$main[0]['min'] = 0;
	$main[0]['max'] = 15;
	$main[0]['name'] = 'red';
	$main[1]['min'] = 16;
	$main[1]['max'] = 35;
	$main[1]['name'] = 'orange';
	$main[2]['min'] = 36;
	$main[2]['max'] = 50;
	$main[2]['name'] = 'gold';
	$main[3]['min'] = 51;
	$main[3]['max'] = 80;
	$main[3]['name'] = 'yellow';
	$main[4]['min'] = 81;
	$main[4]['max'] = 91;
	$main[4]['name'] = 'lime';
	$main[5]['min'] = 92;
	$main[5]['max'] = 135;
	$main[5]['name'] = 'green';
	$main[6]['min'] = 136;
	$main[6]['max'] = 142;
	//$main[6]['name'] = 'artsiegreen';
	$main[6]['name'] = 'green';
	$main[7]['min'] = 143;
	$main[7]['max'] = 165;
	$main[7]['name'] = 'turquoise';
	$main[8]['min'] = 166;
	$main[8]['max'] = 210;
	$main[8]['name'] = 'sky blue';
	$main[9]['min'] = 211;
	$main[9]['max'] = 250;
	$main[9]['name'] = 'sea blue';
	$main[10]['min'] = 251;
	$main[10]['max'] = 260;
	$main[10]['name'] = 'violet';
	$main[11]['min'] = 261;
	$main[11]['max'] = 275;
	$main[11]['name'] = 'purple';
	$main[12]['min'] = 276;
	$main[12]['max'] = 340;
	$main[12]['name'] = 'pink';
	$main[13]['min'] = 341;
	$main[13]['max'] = 360;
	$main[13]['name'] = 'red';
	
	$color='';
	foreach($main as $key => $value){
		if(floor($hsl['h']*360) >= $value['min'] && floor($hsl['h']*360) <= $value['max']){
			$color = $value['name'];
		}	
	}
	
	#brown
	if(($color == 'red' || $color == 'orange') && $hsl['s'] >= 0.05 && $hsl['v'] < 0.55) $color = 'brown';
	
	#grey 
	if($hsl['s'] < 0.05 && $hsl['v'] < 0.95 && $hsl['v'] > 0.2) $color = 'grey';
	
	#white
	if($hsl['s'] < 0.05 && $hsl['v'] > 0.95) $color = 'white';
	
	#black
	if( ($hsl['s'] < 0.05 && $hsl['v'] < 0.2) || ($hsl['v'] < 0.1 ) ) $color = 'black';
	
	#saturation
	if($hsl['s'] > 0.25 && $hsl['s'] <= 0.5){
		$color = 'pastel '.$color;
	}
	
	
		
	return $color; 
}

function returnColorCat($colorName){
	$colorCat = '';
	switch($colorName){
		case 'red':
			$colorCat = 0;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'FF8080';
				$_SESSION['colorCatArr'][$colorCat]['over'] = 'FF4D4D';
			}
		break;
		case 'orange':
		case 'pastel orange':
			$colorCat = 1;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'FFAA80';
				$_SESSION['colorCatArr'][$colorCat]['over'] = 'FF884D';
			}
		break;
		case 'gold':
		case 'yellow':
		case 'pastel gold':
		case 'pastel yellow':
			$colorCat = 2;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'FFF980';
				$_SESSION['colorCatArr'][$colorCat]['over'] = 'FFF64D';
			}
		break;
		case 'lime':
		case 'green':
		case 'artsiegreen':
		case 'pastel lime':
		case 'pastel green':
		case 'pastel artsiegreen':		
			$colorCat = 3;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = '80FF80';
				$_SESSION['colorCatArr'][$colorCat]['over'] = '4DFF4D';
			}
		break;
		case 'turquoise':
		case 'sky blue':
		case 'pastel sky blue':
		case 'pastel turquoise':
			$colorCat = 4;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = '80E7FF';
				$_SESSION['colorCatArr'][$colorCat]['over'] = '4DDDFF';
			}
		break;
		case 'sea blue':
		case 'pastel sea blue':
			$colorCat = 5;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = '8080FF';
				$_SESSION['colorCatArr'][$colorCat]['over'] = '4D4DFF';
			}
		break;
		case 'violet':
		case 'purple':
		case 'pastel violet':
			$colorCat = 6;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'AC4DFF';
				$_SESSION['colorCatArr'][$colorCat]['over'] = 'AC4DFF';
			}
		break;
		case 'pink':
		case 'pastel purple':
		case 'pastel red':
			$colorCat = 7;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'FF80F9';
				$_SESSION['colorCatArr'][$colorCat]['over'] = 'FF4DF7';
			}
		break;
		case 'grey':
			$colorCat = 8;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'BFBFBF';
				$_SESSION['colorCatArr'][$colorCat]['over'] = '919191';
			}
		break;
		case 'brown':
		case 'pastel brown':
			$colorCat = 9;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = '604318';
				$_SESSION['colorCatArr'][$colorCat]['over'] = '4a3413';
			}
		break;
		case 'black':
			$colorCat = 10;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = '333333';
				$_SESSION['colorCatArr'][$colorCat]['over'] = '141414';
			}
		break;
		case 'white':
			$colorCat = 11;
			if(!isset($_SESSION['colorCatArr'][$colorCat])){
				$_SESSION['colorCatArr'][$colorCat] = array();
				$_SESSION['colorCatArr'][$colorCat]['off'] = 'FFFEF8';
				$_SESSION['colorCatArr'][$colorCat]['over'] = 'F4F3EF';
			}
		break;
		
		default:
			$colorCat = 'none';
	}
	return $colorCat;
}



function submitNewsletter($email){
	global $mysqli;
	
	#1 email valid ?	
	$email = strtolower($email);
	if(!isEmail($email)) {
		//feedback invalid email
		$errors[] = '<span class="icon-fm-err"></span> Je hebt geen geldig e-mail adres ingevoerd.';
	}
	
	$dbEmail = inDatabase($email);
	#2 email exists in 'customers' ?
	$result = $mysqli->query('SELECT l.email 
							  FROM shop_customer_login AS l, shop_customer_personal AS p
							  WHERE l.personalId = p.id
							  AND l.email = "'.$dbEmail.'"
							  AND p.newsletter = 1');
	if($result->num_rows > 0){
		$errors[] = '<span class="icon-fm-err"></span>Bedankt, maar dit email-adress staat al ingeschreven voor onze nieuwsbrief.';
	}
	
	#3 email exists in 'newsletter' ?
	$result = $mysqli->query('SELECT email 
							  FROM newsletter
							  WHERE email = "'.$dbEmail.'"');
	if($result->num_rows > 0){
		$errors[] = '<span class="icon-fm-err"></span> Dit email-adress is al ingeschreven voor onze nieuwsbrief.';
	}
	
	if(isset($errors)){
		#error handling	
		$_SESSION['feed-msg-wait'] = $errors[0];
	}
	else{
		#VALID!
		// Include Mailchimp API class
		require_once('./php/MCAPI.class.php');
	 
		// Your API Key: http://admin.mailchimp.com/account/api/
		$api = new MCAPI('ccb62294f1d37a7e16dbdbba4b4b25ca-us5');
	 
		// Your List Unique ID: http://admin.mailchimp.com/lists/ (Click "settings")
		$list_id = "fad30feefb";
	 
		// Variables in your form that match up to variables on your subscriber
		// list. You might have only a single 'name' field, no fields at all, or more
		// fields that you want to sync up.
		
		$merge_vars = array(
			
		);
	 
		// SUBSCRIBE TO LIST
		if ( $api->listSubscribe($list_id, $_POST['email'], $merge_vars) === true ){
			$mailchimp_result = 'Success! Check your email to confirm sign up.';
		} else {
			$mailchimp_result = 'Error: ' . $api->errorMessage;
		}
		
		$ip = ip();
		$ip = inDatabase($ip);
		$mysqli->query('INSERT INTO newsletter(email, ip, timest) VALUES ("'.$dbEmail.'","'.$ip.'",'.time().')');
		
		$_SESSION['feed-msg-wait'] = '<span class="fm-suc"><span class="fm-suc-v">&radic;</span>Bedankt! Je hebt een e-mail gekregen waarin je de inschrijving kunt bevestigen!</span>';
	}	
	header('location: ./');
}

function shortLinkToProduct($productId)
{
    global $mysqli;
    $query = 'SELECT p.titleNL
              FROM product_products AS p
              WHERE p.id = "'.$productId.'"
              LIMIT 1';
    if($result = $mysqli->query($query))
    {
        if($result->num_rows > 0)
        {
            $record = $result->fetch_assoc();

            $title = fromDatabase($record['titleNL']);
            $linkTitle = linkname($title);
            $link = $linkTitle . '-' . $productId;

            header('location: /toont-het-artikel/' . $link . '/');
        }
        else
        {
            header('location: /');
        }
    }
    else
    {
        header('location: /');
    }
}
?>