<?php
//This code is copyright (c) ViciSoft SL, all rights reserved.
//The contents of this file are protected under law as the intellectual property 
//of ViciSoft SL. Any use, reproduction, disclosure or copying of any kind 
//without the express and written permission of ViciSoft SL is forbidden.
//Author: Vince Reid, vincereid@gmail.com
global $isrootsection,$hascheckedperproductdiscounts,$alldiscounts,$noapplydiscounts,$defimagejs,$tslist,$showcategories,$shortdescriptionlimit,$nostripshortdescription,$shortdesc,$productpagelayout,$quickbuylayout,$cs,$atcmu,$atcmuqb,$rs,$hascustomlayout,$allimages,$numallimages,$numalllgimages,$quickbuylayoutarray,$hasformvalidator,$isbackorder,$optionshavestock,$isinstock,$hasmultipurchase,$optionshtml,$optjs,$prodoptions,$thedetailslink,$startlink,$endlink,$thetax;
if(!@isset($GLOBALS['xxNoFrSh']))$GLOBALS['xxNoFrSh']='This product does not qualify for Free Shipping';
for($cpnindex=0; $cpnindex < $adminProdsPerPage; $cpnindex++) $aDiscSection[$cpnindex][0]='';
$prodoptions=[];
$extraimages=0;
$hasmultipurchase=FALSE;
$hascustomlayout=FALSE;
$savequickbuylayout=@$quickbuylayout;
$saveusecsslayout=@$usecsslayout;
if(@$productpagelayout!=''){ $usecsslayout=$nomarkup=TRUE; $GLOBALS['sstrong']=$GLOBALS['estrong']=''; }
if(@$productpagelayout=='' || ! $usecsslayout) $productpagelayout='productid,sku,productimage,productname,discounts,reviewstars'.(@$GLOBALS['showinstock']?',instock':'').',description'.(@$GLOBALS['noproductoptions']?'':',options').',listprice,price,currency'.(@$GLOBALS['nobuyorcheckout']?'':',addtocart'); else $hascustomlayout=TRUE;
if(@$quickbuylayout=='') $quickbuylayout='productimage,productid,productname,description,options,price,detaillink'.(@$GLOBALS['nobuyorcheckout']?'':',addtocart');
if(stripos($productpagelayout,'quickbuy')===FALSE) $quickbuylayout='';
if(stripos($productpagelayout.$quickbuylayout,'detaillink')!==FALSE) $GLOBALS['forcedetailslink']=TRUE;
if(stripos($productpagelayout.$quickbuylayout,'instock')!==FALSE) $GLOBALS['showinstock']=TRUE;
if(stripos($productpagelayout.$quickbuylayout,'quantity')!==FALSE||stripos($productpagelayout.$quickbuylayout,'addtocartquant')!==FALSE) $GLOBALS['showquantonproduct']=TRUE;
$GLOBALS['noproductoptions']=stripos($productpagelayout.$quickbuylayout,'options')===FALSE;
$customlayoutarray=explode(',',$productpagelayout);
$quickbuylayoutarray=explode(',',$quickbuylayout);
$needsformtag=stripos($productpagelayout.$quickbuylayout,'addtocart')!==FALSE||stripos($productpagelayout.$quickbuylayout,'options')!==FALSE;
$localcount=0;
$analyticsoutput=$savejs='';
if(@$_SESSION['clientID']=='' || @$GLOBALS['enablewishlists']==FALSE || @$GLOBALS['wishlistonproducts']=='') $GLOBALS['wishlistonproducts']=FALSE;
productdisplayscript(!@$GLOBALS['noproductoptions']||strpos($quickbuylayout,'options')!==FALSE,FALSE);
if(@$productcolumns=="") $productcolumns=1;
if(! (@isset($showcategories) && @$showcategories==FALSE)){
	if(! (@$GLOBALS['nobuyorcheckout'] || @$GLOBALS['nocheckoutbutton'])) print '<div class="catnavandcheckout catnavproducts">';
	print '<div class="catnavigation catnavproducts">' . $tslist . "</div>\n";
	if(! (@$GLOBALS['nobuyorcheckout'] || @$GLOBALS['nocheckoutbutton'])) print '<div class="catnavcheckout">' . imageorbutton(@$GLOBALS['imgcheckoutbutton'],@$GLOBALS['xxCOTxt'],'checkoutbutton',$GLOBALS['pathtohere'] . 'cart'.$GLOBALS['extension'], FALSE) . "</div></div>\n";
}
if(@$isproductspage) dofilterresults($productcolumns);
if(! @$usecsslayout) print '<table class="' . $cs . 'products" width="98%" border="0" cellspacing="3" cellpadding="3">';
$saveprodlist='';
if(($GLOBALS['adminPayLater'] & 259)!=0){
	if(getpayprovdetx(27,$data1,$data2,$data3,$data4,$data5,$data6,$ppflag1,$ppflag2,$ppflag3,$ppbits,$demomode,$ppmethod)){
		print '<script src="https://www.paypal.com/sdk/js?client-id=' . $data1 . '&currency=' . $GLOBALS['countryCurrency'] . '&components=messages" data-partner-attribution-id="EcommerceTemplates_Ecom_PPCP"></script>'."\n";
		if(($GLOBALS['adminPayLater'] & 2)==2){
			print '<div data-pp-message data-pp-placement="product" data-pp-amount="" data-pp-style-layout="flex" data-pp-style-ratio="20x1" class="paylaterquote prodpaylaterbanner"></div>';
		}
	}
}
if(@$globaldiscounttext!=''){
	if(! @$usecsslayout) print '<tr><td align="left" class="allproddiscounts" colspan="' . $productcolumns . '">';
	print '<div class="discountsapply allproddiscounts">' . $GLOBALS['xxDsProd'] . '</div><div class="proddiscounts allproddiscounts">' . $globaldiscounttext . '</div>';
	if(! @$usecsslayout) print '</td></tr>';
}
	if($iNumOfPages > 1 && @$pagebarattop==1){
		if(@$usecsslayout) print '<div class="pagenumbers">' . "\r\n"; else print '<tr><td colspan="' . $productcolumns . '" align="center" class="pagenumbers"><p class="pagenumbers">';
		print writepagebar($CurPage,$iNumOfPages,$GLOBALS['xxPrev'],$GLOBALS['xxNext'],$pblink,$nofirstpg);
		if(@$usecsslayout) print "</div>\r\n"; else print '</p></td></tr>';
	}
	if(@$usecsslayout) print '<div class="' . $cs . 'products">';
	$totrows=ect_num_rows($allprods);
	if(ect_num_rows($allprods)==0){
		print (! @$usecsslayout ? '<tr><td colspan="' . $productcolumns . '" align="center">' : '');
		notfoundmarkup($GLOBALS['xxNoPrds'],FALSE);
		print (! @$usecsslayout ? '</td></tr>' : '');
	}else while($rs=ect_fetch_assoc($allprods)){
		$saveprodlist.=$rs['pId'].' ';
		$thedetailslink=getdetailsurl($rs['pId'],$rs['pStaticPage'],$rs[getlangid('pName',1)],$rs['pStaticURL'],'',@$GLOBALS['pathtohere']);
		$allimages=$alllgimages=[];
		$plargeimage='';
		$numallimages=$numalllgimages=0;
		$needdetaillink=trim_(str_replace('<br />','',$rs[getlangid('pLongDescription',4)]))!='';
		$result2=ect_query("SELECT imageSrc FROM productimages WHERE imageType=0 AND imageProduct='" . escape_string($rs['pId']) . "' ORDER BY imageNumber") or ect_error();
		while($rs2=ect_fetch_assoc($result2)) $allimages[$numallimages++]=$rs2;
		ect_free_result($result2);
		if(@$magictoolboxproducts!=''&&$numallimages>0){
			$result2=ect_query("SELECT imageSrc FROM productimages WHERE imageType=1 AND imageProduct='" . escape_string($rs['pId']) . "' ORDER BY imageNumber") or ect_error();
			if(ect_num_rows($result2)>0){
				while($rs2=ect_fetch_assoc($result2)) $alllgimages[$numalllgimages++]=$rs2;
				$needdetaillink=TRUE;
				$plargeimage=$alllgimages[0]['imageSrc'];
			}elseif(@$GLOBALS['thumbnailsonproducts']){
				$alllgimages=$allimages;
				$numalllgimages=$numallimages;
				$plargeimage=$alllgimages[0]['imageSrc'];
			}
			ect_free_result($result2);
		}
		if((!@$GLOBALS['forcedetailslink'] && ! $needdetaillink) || @$detailslink!=''){
			$result2=ect_query("SELECT imageSrc FROM productimages WHERE imageType=1 AND imageProduct='" . escape_string($rs['pId']) . "' ORDER BY imageNumber LIMIT 0,1") or ect_error();
			if($rs2=ect_fetch_assoc($result2)){ $needdetaillink=TRUE; $plargeimage=$rs2['imageSrc']; }
			ect_free_result($result2);
		}
		$startlink=$endlink='';
		if((@$GLOBALS['forcedetailslink'] || $needdetaillink) && ! @$GLOBALS['forcenodetailslink']){
			if(@$detailslink!=''){
				$startlink=str_replace('%pid%', $rs['pId'], str_replace('%largeimage%', $plargeimage, $detailslink));
				$endlink=@$detailsendlink;
			}else{
				$startlink='<a class="ectlink" href="'. $thedetailslink .'">';
				$endlink='</a>';
			}
		}
		if(! $isrootsection){
			$thetopts=$rs["pSection"];
			$gotdiscsection=FALSE;
			for($cpnindex=0; $cpnindex < $adminProdsPerPage; $cpnindex++){
				if($aDiscSection[$cpnindex][0]==$thetopts){
					$gotdiscsection=TRUE;
					break;
				}elseif($aDiscSection[$cpnindex][0]=="")
					break;
			}
			$aDiscSection[$cpnindex][0]=$thetopts;
			if(! $gotdiscsection){
				$topcpnids=$thetopts;
				for($index=0; $index<= 10; $index++){
					if($thetopts==0)
						break;
					else{
						$sSQL="SELECT topSection FROM sections WHERE sectionID=" . $thetopts;
						$result2=ect_query($sSQL) or ect_error();
						if(ect_num_rows($result2) > 0){
							$rs2=ect_fetch_assoc($result2);
							$thetopts=$rs2["topSection"];
							$topcpnids.="," . $thetopts;
						}else
							break;
					}
				}
				$aDiscSection[$cpnindex][1]=$topcpnids;
			}else
				$topcpnids=$aDiscSection[$cpnindex][1];
		}
		$alldiscounts=$noapplydiscounts='';
		$hascheckedperproductdiscounts=FALSE;
		getperproductdiscounts();
		if(($localcount % $productcolumns)==0 && ! @$usecsslayout) print '<tr>';
		if(! @$usecsslayout) print '<td width="' . (int)(100 / $productcolumns) . '%" align="center" valign="top" class="' . $cs . 'product">';
		print '<div class="' . $cs . 'product ' . ($rs['pSchemaType']==1?'prodarticle ':'') . trim_($rs['pCustomCSS'] . ' ' . (is_numeric(substr($rs['pId'],0,1))?'ect_':'') . $rs['pId']) . '">';
		if(@$perproducttaxrate==TRUE && ! is_null($rs['pTax'])) $thetax=$rs['pTax']; else $thetax=$countryTaxRate;
		updatepricescript();
		$shortdesc=trim_($rs[getlangid('pDescription',2)]);
		if(@$shortdescriptionlimit!=''){ if(@$nostripshortdescription!=TRUE)$shortdesc=strip_tags($shortdesc); $shortdesc=substr($shortdesc, 0, $shortdescriptionlimit) . (strlen($shortdesc)>$shortdescriptionlimit && $shortdescriptionlimit!=0 ? '...' : ''); }
		if($needsformtag) print '<form method="post" id="ectform' . $Count . '" action="' . $GLOBALS['pathtohere'] . 'cart'.$GLOBALS['extension'].'" onsubmit="return formvalidator' . $Count . '(this)">';
		if(! @$usecsslayout) print '<table width="100%" border="0" cellspacing="4" cellpadding="4">';
		$hasformvalidator=$isbackorder=FALSE;
		$optionshavestock=$isinstock=TRUE;
		$hasmultipurchase=0;
		$atcmu=$atcmuqb=$optionshtml=$optjs='';
		// Options Markup
		if(count($prodoptions)>0){
			if(!@$GLOBALS['noproductoptions']||strpos($quickbuylayout,'options')!==FALSE){
				if($prodoptions[0]['optType']==4) $thestyle=''; else $thestyle=' width="100%"';
				$optionshtml=displayproductoptions($optdiff,$thetax,FALSE,$hasmultipurchase,$optjs);
				if($optionshtml!='') $optionshtml='<div class="'.(strpos($quickbuylayout,'options')!==FALSE?'':$cs).'prodoptions"' . (@$usecsslayout?'':' style="float:left;width:98%;"') . '>' . $optionshtml . '</div>';
				if(!@$GLOBALS['noproductoptions']) $rs['pPrice']+=$optdiff;
			}
			if(@$GLOBALS['noproductoptions']) $hasmultipurchase=2;
		}
		if($needsformtag) displayformvalidator();
		if($optjs!=''){
			$savejs.=$optjs;
			if($GLOBALS['mustincludepopcalendar'] && !$GLOBALS['hasincludedpopcalendar']){ $optionshtml.='<script>var ectpopcalisproducts=1;' . @$GLOBALS['ectpopcalendarjs'] . ($GLOBALS['storelang']!='en' && $GLOBALS['storelang']!=''?"var ectpopcallang='".$GLOBALS['storelang']."'":'') . "\r\n" . '</script><script src="vsadmin/popcalendar.js"></script>'; $GLOBALS['hasincludedpopcalendar']=TRUE; }
		}
		// Add to Cart Markup
		if(!@$GLOBALS['nobuyorcheckout']||stripos($productpagelayout.$quickbuylayout,'addtocart')!==FALSE){
			if($rs['pId']==$GLOBALS['giftcertificateid'] || $rs['pId']==$GLOBALS['donationid']) $hasmultipurchase=2;
			if(! @$usecsslayout) $atcmu.='<tr><td align="center">';
			if($GLOBALS['useStockManagement'])
				if($rs['pStockByOpts']!=0) $isinstock=$optionshavestock; else $isinstock=$rs['pInStock']>$rs['pMinQuant'];
			else
				$isinstock=($rs['pSell']!=0);
			if($rs['pPrice']==0 && @$nosellzeroprice==TRUE)
				$atcmu.='&nbsp;';
			else{
				if(@$usecsslayout) $atcmu.='<div class="XXXECTCSPLACEHOLDERXXXaddtocart prodaddtocart">';
				if(! $isinstock && !($GLOBALS['useStockManagement'] && $hasmultipurchase==2) && $rs['pBackOrder']==0 && @$GLOBALS['notifybackinstock']!=TRUE){
					$atcmu.='<button class="ectbutton outofstock prodoutofstock" type="button" disabled>' . $GLOBALS['xxOutStok'] . '</button>';
					$atcmuqb=$atcmu;
				}else{
					$atcmuqb=$atcmu;
					$isbackorder=! $isinstock && $rs['pBackOrder']!=0;
					writehiddenvar('id', $rs['pId']);
					writehiddenvar('mode', 'add');
					if($GLOBALS['wishlistonproducts']) writehiddenvar('listid', '');
					if(! $hascustomlayout && @$showquantonproduct && $hasmultipurchase==0 && ($isinstock || $isbackorder)){
						$atcmuqb.=(@$usecsslayout ? '' : '<table><tr><td align="center">') . quantitymarkup(FALSE,$Count,FALSE,'XXXECTCSPLACEHOLDERXXX',FALSE) . (@$usecsslayout ? '' : '</td><td align="center">');
					}
					if($isbackorder){
						if(@$GLOBALS['usehardaddtocart']) $atcmuqb.=imageorsubmit(@$imgbackorderbutton,$GLOBALS['xxBakOrd'],'buybutton backorder'); else $atcmuqb.=imageorbuttontag(@$imgbackorderbutton,$GLOBALS['xxBakOrd'],'buybutton backorder','subformid('.$Count.",'','')",TRUE);
					}elseif(! $isinstock && @$GLOBALS['notifybackinstock'])
						$atcmuqb.=imageorbuttontag(@$GLOBALS['imgnotifyinstock'],$GLOBALS['xxNotBaS'],'notifystock prodnotifystock',count($prodoptions)>0 && $GLOBALS['useStockManagement'] ? 'subformid('.$Count.",'','')" : "return notifyinstock(false,'".str_replace("'","\\'",$rs['pId'])."','".str_replace("'","\\'",$rs['pId'])."',".($rs['pStockByOpts']!=0&&!@$optionshavestock?'-1':'0').")", TRUE);
					else{
						if(@$custombuybutton!='')
							$atcmuqb.=$custombuybutton;
						else{
							if(@$GLOBALS['usehardaddtocart']) $atcmuqb.=imageorsubmit(@$imgbuybutton,$GLOBALS['xxAddToC'],'buybutton'); else $atcmuqb.=imageorbuttontag(@$imgbuybutton,$GLOBALS['xxAddToC'],'buybutton ectaddcart'.$Count,'subformid('.$Count.",'','')",TRUE);
						}
					}
					if($GLOBALS['wishlistonproducts']) $atcmuqb.='<div class="wishlistcontainer productwishlist">' . imageorbuttontag(@$imgaddtolist,$GLOBALS['xxAddLis'],'prodwishlist','gtid='.$Count.';return displaysavelist(this,event,window)',TRUE) . '</div>';
					if(@$showquantonproduct && $hasmultipurchase==0 && ($isinstock || $isbackorder)) $atcmuqb.=(! @$usecsslayout ? '</td></tr></table>' : '');
					if($hasmultipurchase==2)
						$atcmu.=imageorbuttontag(@$imgconfigoptions,$GLOBALS['xxConfig'],'configbutton'.($isbackorder?' configbackorder':($isinstock?'':' confignostock')),$thedetailslink, FALSE);
					else
						$atcmu=$atcmuqb;
				}
				if(@$usecsslayout){ $atcmu.='</div>'; $atcmuqb.='</div>'; }
			}
			if(! @$usecsslayout){ $atcmu.='</td></tr>'; $atcmuqb.='</td></tr>'; }
			$atcmuqb=strreplace('XXXECTCSPLACEHOLDERXXXaddtocart prodaddtocart','XXXECTCSPLACEHOLDERXXXaddtocart qbaddtocart',$atcmu);
		}
		displaylayoutarray($customlayoutarray,FALSE);
		if(!$hasformvalidator){
			$optjs=$defimagejs='';
			$prodoptions=[];
			$optionshavestock=TRUE;
			$hasmultipurchase=0;
			if($needsformtag) displayformvalidator();
			$savejs.=$optjs;
			if($GLOBALS['mustincludepopcalendar'] && !$GLOBALS['hasincludedpopcalendar']){ print '<script>var ectpopcalisproducts=1;' . @$GLOBALS['ectpopcalendarjs'] . ($GLOBALS['storelang']!='en' && $GLOBALS['storelang']!=''?"var ectpopcallang='".$GLOBALS['storelang']."'":'') . "\r\n" . '</script><script src="vsadmin/popcalendar.js"></script>'; $GLOBALS['hasincludedpopcalendar']=TRUE; }
		}
		if(! @$usecsslayout) print '</table>';
		if($needsformtag) print '</form>';
		print '</div>';
		if(! @$usecsslayout) print '</td>';
		addtogoogleanalyticsplist($rs,$analyticsoutput,$Count+1);
		$Count++;
		$localcount++;
		if((($localcount % $productcolumns)==0) && ! @$usecsslayout) print '</tr>';
	}
	if($savejs!='') print '<script>/* <![CDATA[ */'.$savejs.'/* ]]> */</script>';
	if(@$GLOBALS['ectsiteid']!='') $_SESSION['saveprodlist']=$saveprodlist;
	if((($localcount % $productcolumns)!=0) && ! @$usecsslayout){
		while($localcount % $productcolumns!=0){
			print '<td class="'.$cs.'noproduct" width="' . (int)(100 / $productcolumns) . '%" align="center">&nbsp;</td>';
			$localcount++;
		}
		print '</tr>';
	}
	if(@$usecsslayout) print '</div>';
	if($iNumOfPages>1 && @$nobottompagebar!=TRUE){
		if(@$usecsslayout) print '<div class="pagenumbers">' . "\r\n"; else print '<tr><td colspan="' . $productcolumns . '" align="center" class="pagenumbers"><p class="pagenumbers">';
		print writepagebar($CurPage,$iNumOfPages,$GLOBALS['xxPrev'],$GLOBALS['xxNext'],$pblink,$nofirstpg);
		if(@$usecsslayout) print "</div>\r\n"; else print '</p></td></tr>';
	}
	if(!@$usecsslayout) print '</table>';
	if($defimagejs!='') print '<script>' . $defimagejs . '</script>';
	$quickbuylayout=$savequickbuylayout;
	$usecsslayout=$saveusecsslayout;
	
	if(@$GLOBALS['googletagid']!=''&&$analyticsoutput!=''){
		print '<script>gtag("event","view_item_list",{items:[' . $analyticsoutput . ']});</script>'."\n";
	}
?>