<?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
if(!@$GLOBALS['incfunctionsdefined']){
	print 'No incfunctions.php file.';
	exit;
}
$GLOBALS['alreadygotadmin']=getadminsettings();
if(@$GLOBALS['menupoplimit']=='') $GLOBALS['menupoplimit']=9;
if(@$GLOBALS['menuid']=='') $GLOBALS['menuid']='';
if(@$GLOBALS['vertical2scripts']=='') $GLOBALS['vertical2scripts']='';
if(@$GLOBALS['popmenuscripts']=='') $GLOBALS['popmenuscripts']='';
if(@$GLOBALS['commonmenuscripts']=='') $GLOBALS['commonmenuscripts']='';
if(@$GLOBALS['menustyle']=='') $GLOBALS['menustyle']='baremenu';
$GLOBALS['saveormenuroot']=@$GLOBALS['ormenuroot'];
$GLOBALS['savecatalogroot']=@$GLOBALS['catalogroot'];
if(@$GLOBALS['ormenuroot']=='') $GLOBALS['ormenuroot']=0;
$arrayem='';
$ectself=explode('?',@$_SERVER['PHP_SELF']);
$ecturi=explode('?',@$_SERVER['REQUEST_URI']);
global $hasrewrite;
$hasrewrite=$ectself[0]!=$ecturi[0];
if(strtolower($GLOBALS['adminencoding'])=='iso-8859-1') $GLOBALS['raquo']='»'; else $GLOBALS['raquo']='&raquo;';
if(@isset($GLOBALS['menuraquo'])) $GLOBALS['raquo']=$GLOBALS['menuraquo'];
$GLOBALS['dirnameself']=str_replace('"','',strip_tags(str_replace('\\','/',dirname(@$_SERVER['PHP_SELF']))));
if(getget('cat')!='') $GLOBALS['theid']=trim(getget('cat')); else $GLOBALS['theid']='';
if(@$GLOBALS['iswordpress'] && get_query_var( 'ectcat' ) != '') $GLOBALS['theid']=urldecode(get_query_var('ectcat'));
if(@$GLOBALS['seocategoryurls']){$GLOBALS['usecategoryname']=TRUE;$GLOBALS['theid']=str_replace(@$GLOBALS['detlinkspacechar'],' ',$GLOBALS['theid']);}
$GLOBALS['ismenufilter']=FALSE;
if(is_numeric(@$explicitid))
	$GLOBALS['theid']=@$explicitid;
elseif(@$GLOBALS['usecategoryname'] && $GLOBALS['theid']!=''){
	$sSQL='SELECT sectionID FROM sections WHERE '.(@$GLOBALS['seocategoryurls']?getlangid('sectionurl',2048)."='".escape_string($GLOBALS['theid'])."' OR (":'').getlangid('sectionName',256)."='".escape_string($GLOBALS['theid'])."'".(@$GLOBALS['seocategoryurls']?' AND '.getlangid('sectionurl',2048)."='')":'');
	$result=ect_query($sSQL) or ect_error();
	if($rs=ect_fetch_assoc($result))$GLOBALS['theid']=$rs['sectionID'];
	ect_free_result($result);
}
if(! is_numeric($GLOBALS['theid'])) $GLOBALS['theid']='';
if(@$GLOBALS['menustyle']=='menufilter'){
	$GLOBALS['ormenuroot']=$GLOBALS['catalogroot']=$GLOBALS['theid'];
	$GLOBALS['menustyle']='verticalmenu2';
	$GLOBALS['ismenufilter']=TRUE;
}
if ( ! function_exists( 'join2paths' ) ) {
function join2paths($stourl,$securl){
	global $hasrewrite;
	if(strpos($securl,'://')!==FALSE)
		return($securl);
	if($securl[0]!='/'&&$hasrewrite)
		return(($stourl!=''?$stourl:str_replace('//','/',$GLOBALS['dirnameself'].'/')).$securl);
	if($securl[0]=='/'&&$stourl!=''){
		$urlparts=parse_url($stourl);
		$pos=strrpos($stourl,$urlparts['path']);
		if($pos!==FALSE) $stourl=substr_replace($stourl,'',$pos,strlen($urlparts['path']));
	}
	return($stourl.$securl);
}
function mwritemenulevel($id,$itlevel,$incatalogroot){
	global $mAlldata,$numrows,$menupoplimit,$menuprestr,$storeurl,$menucategoriesatroot,$incstoreurl,$menustyle,$arrayem,$menuid,$catalogroot,$seocategoryurls,$seocaturlpattern,$seoprodurlpattern;
	$hassub=FALSE;
	if($itlevel<=$menupoplimit){
		if(! (@$menucategoriesatroot===2 && $id==$GLOBALS['ormenuroot'])){
			for($mIndex=0;$mIndex < $numrows;$mIndex++){
				if($mAlldata[$mIndex]['topSection']==$id){
					if(($menustyle=='horizontalmenu1' || $menustyle=='verticalmenu3') && ! $hassub)
						print '<ul id="ecttop'.$menuid.'_'.$id.'" style="list-style:none;margin:0px;border:0px;'.($id!=$GLOBALS['ormenuroot']?'display:none;position:absolute;':'').'" class="ect'.($id!=$GLOBALS['ormenuroot']?"sub":'').$menustyle.' ectmenu'.($menuid+1).($id!=$GLOBALS['ormenuroot']?' ectsub'.($menuid+1):'').'">' . "\n";
					$hassub=TRUE;
					$mTID=$mAlldata[$mIndex]['topSection'];
					if($mTID==0) $mTID='';
					$sectionurl=trim($mAlldata[$mIndex][getlangid('sectionurl',2048)]);
					if($menustyle=='horizontalmenu1' || $menustyle=='verticalmenu3'){
						if(! (@$menucategoriesatroot===TRUE && $mAlldata[$mIndex]['sectionID']==$catalogroot)){
							$arrayem.=$mAlldata[$mIndex]['sectionID'].':'.$mAlldata[$mIndex]['topSection'].',';
							print '<li id="ect'.$menuid.'_'.$mAlldata[$mIndex]['sectionID'].'" class="ectmenu'.($menuid+1).($id!=$GLOBALS['ormenuroot']?' ectsub'.($menuid+1):'').'"'.($menustyle!='verticalmenu3'?' style="'.($id!=$GLOBALS['ormenuroot']?'margin-bottom:-1px':'display:inline;margin-right:-1px').'"':'').'>';
							if($sectionurl!=''){
								if($incatalogroot && strpos($sectionurl,'://')===FALSE) $caturl=getcatid($sectionurl,@$seocategoryurls?$sectionurl:'',$mAlldata[$mIndex]['rootSection']==1?$seoprodurlpattern:$seocaturlpattern); else $caturl=$sectionurl;
								print '<a href="'.join2paths($incstoreurl,$caturl).'">'.str_replace('<','&lt;',$mAlldata[$mIndex][getlangid('sectionName',256)]).'</a>';
							}else{
								if($mAlldata[$mIndex]['rootSection']==0)
									print '<a href="'.join2paths($incstoreurl,(!@$seocategoryurls?'categories'.$GLOBALS['extension'].'?cat=':'').getcatid($mAlldata[$mIndex]['sectionID'],$mAlldata[$mIndex][getlangid('sectionName',256)],$seocaturlpattern)).'">'.str_replace('<','&lt;',$mAlldata[$mIndex][getlangid('sectionName',256)]).'</a>';
								else
									print '<a href="'.join2paths($incstoreurl,(!@$seocategoryurls?'products'.$GLOBALS['extension'].'?cat=':'').getcatid($mAlldata[$mIndex]['sectionID'],$mAlldata[$mIndex][getlangid('sectionName',256)],$seoprodurlpattern)).'">'.str_replace('<','&lt;',$mAlldata[$mIndex][getlangid('sectionName',256)]).'</a>';
							}
							print " </li>\n";
						}
					}
				}
			}
			if(($menustyle=='horizontalmenu1' || $menustyle=='verticalmenu3') && $hassub) print '</ul>';
		}
		for($mIndex=0;$mIndex < $numrows;$mIndex++)
			if($mAlldata[$mIndex]['topSection']==$id && $mAlldata[$mIndex]['rootSection']==0 && @$menucategoriesatroot!==1) mwritemenulevel($mAlldata[$mIndex]['sectionID'],$itlevel+1,$incatalogroot || $mAlldata[$mIndex]['sectionID']==$catalogroot);
	}
}
function mstrdpth($dep){
	return(' ectraq'.($dep-1));
}
function cssmenulevel($id,$itlevel,$incatalogroot){
	global $menupoplimit,$menucategoriesatroot,$mAlldata,$arrayem,$numrows,$menustyle,$menuid,$catalogroot,$incstoreurl,$seocategoryurls,$seocaturlpattern,$seoprodurlpattern,$raquo;
	$starttag='<ul>';
	$endtag='';
	if($itlevel<=$menupoplimit){
		$startindex=0;
		$highindex=$numrows-1;
		$hasmatch=FALSE;
		while($startindex<=$highindex){
			$midindex=floor(($startindex + $highindex) / 2);
			if($mAlldata[$midindex]['topSection']==$id){
				$hasmatch=TRUE;
				break;
			}
			if($id<$mAlldata[$midindex]['topSection'])
				$highindex=$midindex-1;
			else
				$startindex=$midindex+1;
		}
		if($hasmatch){
			while($midindex>0&&$mAlldata[$midindex-1]['topSection']==$id) $midindex--;
			for($mIndex=$midindex;$mIndex < $numrows;$mIndex++){
				if($mAlldata[$mIndex]['topSection']==$id && ! (@$menucategoriesatroot===TRUE && $mAlldata[$mIndex]['sectionID']==$catalogroot)){
					$arrayem.=$mAlldata[$mIndex]['sectionID'].':'.$mAlldata[$mIndex]['topSection'].',';
					$sectionurl=trim($mAlldata[$mIndex][getlangid('sectionurl',2048)]);
					if($sectionurl!=''){
						if($itlevel==$menupoplimit||$mAlldata[$mIndex]['rootSection']!=0){
							if($incatalogroot && strpos($sectionurl,'://')===FALSE) $caturl=getcatid($sectionurl,@$seocategoryurls?$sectionurl:'',$mAlldata[$mIndex]['rootSection']==1?$seoprodurlpattern:$seocaturlpattern); else $caturl=$sectionurl;
							$mlink=join2paths($incstoreurl,$caturl);
						}else
							$mlink='#';
					}elseif($mAlldata[$mIndex]['rootSection']==0){
						if($itlevel==$menupoplimit)$mlink=join2paths($incstoreurl,(!@$seocategoryurls?'categories'.$GLOBALS['extension'].'?cat=':'').getcatid($mAlldata[$mIndex]['sectionID'],$mAlldata[$mIndex][getlangid('sectionName',256)],$seocaturlpattern)); else $mlink='#';
					}else
						$mlink=join2paths($incstoreurl,(!@$seocategoryurls?'products'.$GLOBALS['extension'].'?cat=':'').getcatid($mAlldata[$mIndex]['sectionID'],$mAlldata[$mIndex][getlangid('sectionName',256)],$seoprodurlpattern));
					if(@$menustyle=='baremenu'){
						print $starttag . '<li><a href="' . $mlink . '">' . str_replace('<','&lt;',$mAlldata[$mIndex][getlangid('sectionName',256)]) . "</a></li>\n";
						$starttag='';
						$endtag='</ul>';
					}else
						print '<li'.($id!=$GLOBALS['ormenuroot']?' class="ectsub'.mstrdpth($itlevel).'"':'').' id="ect'.$menuid.'_'.$mAlldata[$mIndex]['sectionID'].'"><a href="' . $mlink . '">' . str_replace('<','&lt;',$mAlldata[$mIndex][getlangid('sectionName',256)]) . "</a></li>\n";
					cssmenulevel($mAlldata[$mIndex]['sectionID'],$itlevel+1,$incatalogroot || $mAlldata[$mIndex]['sectionID']==$catalogroot);
					if($mIndex+1<$numrows && $mAlldata[$mIndex+1]['topSection']>$id) break;
				}
			}
			print $endtag;
		}
	}
}
function writesubmenus(){
	global $menucategoriesatroot,$catalogroot;
	$menucategoriesatroot=2;
	mwritemenulevel($GLOBALS['ormenuroot'],2,$catalogroot==$GLOBALS['ormenuroot']);
}
function displayectmenu($menstyle){
	global $arrayem,$menupoplimit,$numrows,$mAlldata,$menuid,$menustyle,$menucategoriesatroot,$catalogroot,$incstoreurl,$storeurl;
	if(@$_SESSION['clientLoginLevel']!='') $minloglevel=$_SESSION['clientLoginLevel']; else $minloglevel=0;
	$menustyle=$menstyle;
	if($menustyle!='baremenu'){
		if(@$menuid==='') $menuid=0; else $menuid++;
	}
	if((@$_SERVER['HTTPS']=='on' || @$_SERVER['SERVER_PORT']=='443') && strpos($storeurl,'https:')===FALSE) $incstoreurl=$storeurl; else $incstoreurl='';
	//print '$incstoreurl: ' . $incstoreurl . "<br>";
	$sSQL='SELECT sectionID,'.getlangid('sectionName',256).',topSection,rootSection,'.getlangid('sectionurl',2048).' FROM sections WHERE sectionID<>0 AND sectionDisabled<=' . $minloglevel . ($menupoplimit<=1?' AND topSection=0':'') . ' ORDER BY topSection,' . (@$GLOBALS['sortcategoriesalphabetically'] ? getlangid('sectionName',256) : 'sectionOrder') . (@$menustyle=='verticalmenu2'?',topSection':'');
	$result=ect_query($sSQL) or ect_error();
	$numrows=0;
	if(ect_num_rows($result) > 0){
		$theroot=$catalogroot;
		while($rs=ect_fetch_assoc($result)){
			$mAlldata[$numrows++]=$rs;
			if($rs['sectionID']==$catalogroot) $theroot=$rs['topSection'];
		}
		if(@$menucategoriesatroot===TRUE && ($menustyle=='verticalmenu2' || $menustyle=='horizontalmenu1' || $menustyle=='verticalmenu3')){
			$catrootpos=-1;
			$maxposition=$numrows;
			for($mIndex=0;$mIndex < $numrows;$mIndex++){
				if($mAlldata[$mIndex]['sectionID']==$catalogroot) $catrootpos=$mIndex;
				if($mAlldata[$mIndex]['topSection']==$catalogroot) $maxposition=$mIndex;
			}
			if($catrootpos>=0){
				for($sortindex=0;$sortindex < $numrows-$catrootpos;$sortindex++){
					$sortedone=FALSE;
					$newmaxposition=0;
					for($mIndex=$catrootpos+1;$mIndex < $maxposition;$mIndex++){
						if($mAlldata[$mIndex]['topSection']!=$catalogroot && $mAlldata[$mIndex+1]['topSection']==$catalogroot){
							$temparr=$mAlldata[$mIndex];
							$mAlldata[$mIndex]=$mAlldata[$mIndex+1];
							$mAlldata[$mIndex+1]=$temparr;
							$sortedone=TRUE;
							$newmaxposition=$mIndex;
						}
					}
					if(!$sortedone) break;
					$maxposition=$newmaxposition;
				}
			}
			for($mIndex=0;$mIndex < $numrows;$mIndex++){
				if($mAlldata[$mIndex]['topSection']==$catalogroot) $mAlldata[$mIndex]['topSection']=$theroot;
			}
		}
		if($menustyle=='baremenu'){
			cssmenulevel($GLOBALS['ormenuroot'],1,$catalogroot==$GLOBALS['ormenuroot']);
		}elseif($menustyle=='verticalmenu2'){
			print '<ul class="' . ($GLOBALS['ismenufilter']?'menufilter ':'') . 'ect'.$menustyle.' ectmenu'.($menuid+1).'" style="list-style:none">';
			cssmenulevel($GLOBALS['ormenuroot'],1,$catalogroot==$GLOBALS['ormenuroot']);
			print '</ul>';
		}elseif($menustyle=='horizontalmenu1' || $menustyle=='verticalmenu3')
			mwritemenulevel($GLOBALS['ormenuroot'],1,$catalogroot==$GLOBALS['ormenuroot']);
		else
			mwritemenulevel($GLOBALS['ormenuroot'],1,$catalogroot==$GLOBALS['ormenuroot']);
	}
	ect_free_result($result);
	if($menustyle=='horizontalmenu1' || $menustyle=='verticalmenu2' || $menustyle=='verticalmenu3'){ ?>
<script>
/* <![CDATA[ */
<?php
		if($menuid==0) print 'var curmen=[];var lastmen=[];var em=[];var emr=[];var emt=[];' . "\n";
		writemenuscripts();
		print 'em['.$menuid.']={'.rtrim($arrayem,',').'};emr['.$menuid.']=[];emt['.$menuid.']=[];curmen['.$menuid."]=0;\n";
		print 'addsubsclass('.$menuid.",0,'".$menustyle."')\n";
		if($GLOBALS['theid']!='') print 'opensubmenu(' . $menuid . ',' . $GLOBALS['theid'] . ")\n";
		print '/* ]]> */</script>';
	}
}
function writemenuscripts(){
	global $menustyle,$vertical2scripts,$popmenuscripts,$commonmenuscripts;
	if($menustyle!='verticalmenu2' && $popmenuscripts==''){
		$popmenuscripts=TRUE;
?>
function closepopdelay(menid){
	var re=new RegExp('ect\\d+_');
	var theid=menid.replace(re,'');
	var mennum=menid.replace('ect','').replace(/_\d+/,'');
	for(var ei in emt[mennum]){
		if(ei!=0&&emt[mennum][ei]==true&&!insubmenu(ei,mennum)){
			document.getElementById('ecttop'+mennum+"_"+ei).style.display='none';
			emt[mennum][ei]=false; // closed
		}
	}
}
function closepop(men){
	var mennum=men.id.replace('ect','').replace(/_\d+/,'');
	lastmen[mennum]=curmen[mennum];
	curmen[mennum]=0;
	setTimeout("closepopdelay('"+men.id+"')",1000);
}
function getPos(el){
	for (var lx=0,ly=0; el!=null; lx+=el.offsetLeft,ly+=el.offsetTop, el=el.offsetParent){
	};
	return{x:lx,y:ly};
}
function openpop(men,ispopout){
	var re=new RegExp('ect\\d+_');
	var theid=men.id.replace(re,'');
	var mennum=men.id.replace('ect','').replace(/_\d+/,'');
	curmen[mennum]=theid;
	if(lastmen[mennum]!=0)
		closepopdelay('ect'+mennum+'_'+lastmen[mennum]);
	if(mentop=document.getElementById('ecttop'+mennum+'_'+theid)){
		var px=getPos(men);
		if(em[mennum][theid]==0&&!ispopout){
			mentop.style.left=px.x+'px';
			mentop.style.top=(px.y+men.offsetHeight-1)+'px';
			mentop.style.display='';
		}else{
			mentop.style.left=(px.x+men.offsetWidth-1)+'px';
			mentop.style.top=px.y+'px';
			mentop.style.display='';
		}
		emt[mennum][theid]=true; // open
	}
}
<?php
	}
	if($menustyle=='verticalmenu2' && $vertical2scripts==''){
		$vertical2scripts=TRUE;
?>
function closecascade(men){
	var re=new RegExp('ect\\d+_');
	var theid=men.id.replace(re,'');
	var mennum=men.id.replace('ect','').replace(/_\d+/,'');
	curmen[mennum]=0;
	for(var ei in emt[mennum]){
		if(ei!=0&&emt[mennum][ei]==true&&!insubmenu(ei,mennum)){
			for(var ei2 in em[mennum]){
				if(em[mennum][ei2]==ei){
					document.getElementById('ect'+mennum+"_"+ei2).style.display='none';
				}
			}
		}
	}
	emt[mennum][theid]=false; // closed
	return(false);
}
function opencascade(men){
	var re=new RegExp('ect\\d+_');
	var theid=men.id.replace(re,'');
	var mennum=men.id.replace('ect','').replace(/_\d+/,'');
	if(emt[mennum][theid]==true) return(closecascade(men));
	curmen[mennum]=theid;
	for(var ei in em[mennum]){
		if(em[mennum][ei]==theid){
			document.getElementById('ect'+mennum+'_'+ei).style.display='block';
			emt[mennum][theid]=true; // open
		}
	}
	return(false);
}
function ectChCk(men){
return(hassubs_xxx(men)?opencascade(men):true)
}
<?php
	}
	if($commonmenuscripts==''){
		$commonmenuscripts=TRUE; ?>
function hassubs_xxx(men){
	var re=new RegExp('ect\\d+_');
	var theid=men.id.replace(re,'');
	var mennum=men.id.replace('ect','').replace(/_\d+/,'');
	return(hassubs(mennum,theid));
}
function hassubs(mennum,theid){
	if(emr[mennum][theid])return true;
	return false;
}
function insubmenu(mei,mid){
	if(curmen[mid]==0)return(false);
	curm=curmen[mid];
	maxloops=0;
	while(curm!=0){
		if(mei==curm)return(true);
		curm=em[mid][curm];
		if(maxloops++>10) break;
	}
	return(false);
}
function addsubsclass(mennum,menid,menutype){
	for(var ei in em[mennum]){
		emr[mennum][em[mennum][ei]]=ei;
	}
	for(var ei in em[mennum]){
		men=document.getElementById('ect'+mennum+'_'+ei);
		if(menutype=='verticalmenu2')
			men.onclick=function(){return(ectChCk(this))};
		else{
			men.onmouseover=function(){openpop(this,menutype=='verticalmenu3'?true:false)};
			men.onmouseout=function(){closepop(this)};
		}
		if(hassubs(mennum,ei))men.classList.add('ectmenuhassub'+(mennum+1));
	}
}
function opensubmenu(mennum,menid){
	if(document.getElementById('ect'+mennum+'_'+menid)){
		ectaddclass(document.getElementById('ect'+mennum+'_'+menid),'ectselected');
		curmen[mennum]=menid;
	}
	while(em[mennum][menid]){
		if(document.getElementById('ect'+mennum+'_'+em[mennum][menid]).onclick) document.getElementById('ect'+mennum+'_'+em[mennum][menid]).onclick();
		menid=em[mennum][menid];
	}
}
<?php
	}
} // writemenuscripts
}
displayectmenu($GLOBALS['menustyle']);
if($GLOBALS['ismenufilter']&&$arrayem!=''){
	print '<script>ectById("menufilter").style.display="block";</script>';
}
$GLOBALS['ormenuroot']=$GLOBALS['saveormenuroot'];
$GLOBALS['catalogroot']=$GLOBALS['savecatalogroot'];
?>