<?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 $customeraccounturl,$forceloginonhttps,$minicssaction,$thesessionid;
if(@$GLOBALS['xxCkCoVC']=='') $GLOBALS['xxCkCoVC']='Please click checkout to view your cart contents.';
if(@$GLOBALS['xxCMCSC']=='') $GLOBALS['xxCMCSC']='View Cart';
if(@$GLOBALS['xxCMCEmp']=='') $GLOBALS['xxCMCEmp']='Your shopping cart is currently empty';
if(@$GLOBALS['xxCMCClo']=='') $GLOBALS['xxCMCClo']='Close';
$GLOBALS['alreadygotadmin']=getadminsettings();
if(getpost('sessionid')!='')
	$thesessionid = $thesessionid=str_replace(array("'",'"'),'',strip_tags(getpost('sessionid')));
else
	$thesessionid = getsessionid();
if(getget("action")=="deleteitem" && is_numeric(getget("cartid"))){
	$sSQL="SELECT cartID FROM cart WHERE cartID='" . escape_string(getget('cartid')) . "' AND cartCompleted=0 AND " . getsessionsql() . ' ORDER BY cartID';
	$result=ect_query($sSQL) or ect_error();
	while($rs=ect_fetch_assoc($result)){
		ect_query("DELETE FROM cartoptions WHERE coCartID='" . $rs['cartID'] . "'") or ect_error();
		ect_query("DELETE FROM cart WHERE cartID='" . $rs['cartID'] . "'") or ect_error();
		ect_query("DELETE FROM giftcertificate WHERE gcCartID='" . $rs['cartID'] . "'") or ect_error();
	}
	ect_free_result($result);
}
$mcgndtot=$totquant=$shipping=$mcdiscounts=0;
if(@$_SESSION['xscountrytax']!='') $xscountrytax=$_SESSION['xscountrytax']; else $xscountrytax=0;
$mcpdtxt='';
if(@$_POST['mode']=='checkout'){
	if(@$_POST['checktmplogin']=='x'){
		$_SESSION['clientID']=NULL; unset($_SESSION['clientID']);
	}elseif(@$_POST['checktmplogin']!=''){
		$sSQL="SELECT tmploginname FROM tmplogin WHERE tmploginid='" . escape_string(@$_POST['sessionid']) . "' AND tmploginchk='" . escape_string(@$_POST['checktmplogin']) . "'";
		$result=ect_query($sSQL) or ect_error();
		if($rs=ect_fetch_assoc($result))
			$_SESSION['clientID']=$rs['tmploginname'];
	}
}
$sSQL = "SELECT cartID,cartProdID,cartProdName,cartProdPrice,cartQuantity FROM cart WHERE cartCompleted=0 AND " . getsessionsql() . ' ORDER BY cartID';
$result=ect_query($sSQL) or ect_error();
while($rs=ect_fetch_assoc($result)){
	$sSQL = "SELECT imageSrc FROM productimages WHERE imageType=0 AND imageProduct='".escape_string($rs['cartProdID'])."' ORDER BY imageNumber";
	$result2=ect_query($sSQL) or ect_error();
	if($rs2=ect_fetch_assoc($result2)) $productimageurl=$rs2['imageSrc']; else $productimageurl='';
	ect_free_result($result2);

	$optPriceDiff=0;
	$sSQL = "SELECT SUM(coPriceDiff) AS sumDiff FROM cartoptions WHERE coCartID=".escape_string($rs['cartID']);
	$result2=ect_query($sSQL) or ect_error();
	$rs2=ect_fetch_assoc($result2);
	if(! is_null($rs2['sumDiff'])) $optPriceDiff=$rs2['sumDiff'];
	ect_free_result($result2);

	$productqty =$rs['cartQuantity'];
	$productprice = $rs['cartProdPrice']+$optPriceDiff;

	$subtot = (($rs['cartProdPrice']+$optPriceDiff)*(int)$rs['cartQuantity']);
	$totquant+=(int)$rs['cartQuantity'];
	$mcgndtot += $subtot; 
	$prod='';

	$mcpdtxt .= '<div class="minicartcnt_dp"><div class="ectdp_minicartitemImg">';
	if($productimageurl==''){
		$mcpdtxt.='<div style="min-width:40px;">&nbsp;</div>';
	}else
		$mcpdtxt.='<img src="'.$productimageurl.'" class="ectdp_minicartImg" alt="">';
	$mcpdtxt.='</div><div class="ectdp_minicartitemName">' . (@$GLOBALS['nopriceanywhere']?$productqty.' ':'') .$rs['cartProdName'] . (@$GLOBALS['nopriceanywhere']?'':'<br/><span class="ectMCtot">'.$productqty.' x '.FormatEuroCurrency($productprice).'</span>') . '</div><div class="ectdp_minicartitemDelete"><img class="cartdelete" src="images/delete.png" alt="'.$GLOBALS['xxDelete'].'" onclick="dodeleteitem('.$rs['cartID'].')" style="cursor:pointer"></div><div style="clear:both;"></div></div>';
}
ect_free_result($result);


	if(getget("action")==''){ ?>
<div class="ectdp_minicartmainwrapper_ct" id="ectdp_minicartmainwrapper_ct">
<?php
	} ?>
<div class="ectdp_minicartmainwrapper">
    
	<div class="minicartcnt_dp ectdp_minicartopen" onmouseover="domcopen()" onmouseout="startmcclosecount()">
	<img src="images/arrow-down.png" style="vertical-align:text-top;" width="16" height="16" alt="<?php print $GLOBALS['xxMCSC']?>" /> &nbsp;<a class="ectlink mincart" href="<?php print $GLOBALS['storeurl']?>cart<?php print $GLOBALS['extension']?>"><?php print $GLOBALS['xxCMCSC'] . (getpost('mode')=='movetocart'||getpost('mode')=='update'?'':' (<span class="ectMCquant">'.$totquant.'</span>)');?></a>
	</div>
    <div class="ectdp_minicartcontainer" id="ectdp_minicartcontainer" style="display:none;" onmouseover="domcopen()" onmouseout="startmcclosecount()">
<?php
		if($totquant==0){
			print '<div class="minicartcnt_dp ectdp_empty">' . $GLOBALS['xxCMCEmp'] . '</div>';
			print '<div class="minicartcnt_dp">' . imageorbuttontag(@$GLOBALS['imgminicartclose'],@$GLOBALS['xxCMCClo'],'dpminicartclose','domcclose()',TRUE) . '</div>';
		}else{
			if(@$_POST['mode']=='movetocart'){ ?>  
	<div class="minicartcnt_dp ectdp_pincart"><?php print $GLOBALS['xxCkCoVC']?></div>
<?php		}elseif(@$_POST['mode']=='update'){ ?> 
	<div class="minicartcnt_dp ectdp_pincart"><?php print $GLOBALS['xxMainWn']?></div>
<?php		}else{ ?>
	<div class="minicartcnt_dp ectdp_pincart"><?php print $totquant . ' ' . $GLOBALS['xxMCIIC'] ?></div>
	<div class="mcLNitems"><?php print $mcpdtxt?></div>
<?php			if($mcpdtxt!='' && @$_SESSION['discounts']!=''&&!@$GLOBALS['nopriceanywhere'])$mcdiscounts=(float)$_SESSION['discounts']; ?>
	<div class="ecHidDsc minicartcnt_dp"<?php if($mcdiscounts==0) print ' style="display:none"'?>><span class="minicartdsc"><?php print $GLOBALS['xxDscnts'] . ' <span class="mcMCdsct">' . FormatEuroCurrency($mcdiscounts)?></span></span></div>
<?php			if($mcpdtxt!='' && (string)@$_SESSION['xsshipping']!=''){
					$shipping = (float)$_SESSION['xsshipping'];
					if($shipping==0) $showshipping=' minicartdsc">'.$GLOBALS['xxFree']; else $showshipping='">'.FormatEuroCurrency($shipping); ?>
   	<div class="minicartcnt_dp"><?php print $GLOBALS['xxMCShpE'] . ' <span class="ectMCship' . $showshipping.'</span>'?></div>
<?php			}
				if($mcpdtxt=='') $xscountrytax=0;
				if(!@$GLOBALS['nopriceanywhere']){ ?>
	<div class="minicartcnt_dp ectdp_minicarttotal"><?php print $GLOBALS['xxTotal'] . ' <span class="ectMCtot">' . FormatEuroCurrency(($mcgndtot+$shipping+$xscountrytax)-$mcdiscounts)?></span></div>
<?php			}
			} ?>
        <div class="minicartcnt_dp">
			<?php print imageorbuttontag(@$GLOBALS['imgminicartclose'],@$GLOBALS['xxCMCClo'],'dpminicartclose','domcclose()',TRUE)?>
			&nbsp;&nbsp;&nbsp;&nbsp;
			<?php print imageorbuttontag(@$GLOBALS['imgminicartcheckout'],@$GLOBALS['xxMCCO'],'dpminicartcheckout',@$GLOBALS['storeurl'].'cart'.@$GLOBALS['extension'],FALSE)?>
		</div>
<?php	} ?>
    </div>
</div>
<?php
	if(getget("action")==''){ ?>
</div>
<script>
var mctmrid=0,ajaxobj,ajaxobjrf;
function domcopen(){
	clearTimeout(mctmrid);
	document.getElementById('ectdp_minicartcontainer').style.display='';
}
function domcclose(){
	document.getElementById('ectdp_minicartcontainer').style.display='none';
}
function startmcclosecount(){
	mctmrid=setTimeout("domcclose()",400);
}
function mcpagerefresh(){
	if(ajaxobj.readyState==4){
		document.location.reload();
	}
}
function refreshmcwindow(){
	if(ajaxobjrf.readyState==4){
		document.getElementById('ectdp_minicartmainwrapper_ct').innerHTML=ajaxobjrf.responseText;
	}
}
function dodeleteitem(cartid){
	ajaxobj=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("MSXML2.XMLHTTP");
	ajaxobj.onreadystatechange=mcpagerefresh;
	ajaxobj.open("GET", "vsadmin/miniajaxdropdowncart.php?action=deleteitem&cartid="+cartid,true);
	ajaxobj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	ajaxobj.send('');
}
function dorefreshmctimer(){
	setTimeout("dorefreshmc()",1000);
}
function dorefreshmc(){
	ajaxobjrf=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("MSXML2.XMLHTTP");
	ajaxobjrf.onreadystatechange=refreshmcwindow;
	ajaxobjrf.open("GET", "vsadmin/miniajaxdropdowncart.php?action=refresh",true);
	ajaxobjrf.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	ajaxobjrf.send('');
}
function addOnclick(elem, func) {
    var old=elem.onclick;
    if(typeof elem.onclick!='function'){
        elem.onclick=func;
    }else{
        elem.onclick=function(){
            if(old) old();
            func();
        };        
    }
}
function addbuttonclickevent(){
	var buybuttons=document.getElementsByClassName('buybutton');
	for(var i = 0; i < buybuttons.length; i++) {
		var buybutton=buybuttons[i];
		addOnclick(buybutton, dorefreshmctimer);
	}
}
if(window.addEventListener){
	window.addEventListener("load",addbuttonclickevent);
}else if(window.attachEvent)
    window.attachEvent("load", addbuttonclickevent);
</script>
<?php
	} ?>