<?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(@$storesessionvalue=='') $storesessionvalue='virtualstore'.time();
if(@$_SESSION['loggedon']!=$storesessionvalue||@$GLOBALS['disallowlogin']||!@$GLOBALS['incfunctionsdefined']) exit;
$cartisincluded=TRUE;
include './inc/inccart.php';
$success=TRUE;
if(@$dateformatstr=='') $dateformatstr = 'm/d/Y';
$admindatestr='Y-m-d';
if(@$admindateformat=='') $admindateformat=0;
if($admindateformat==1)
	$admindatestr='m/d/Y';
elseif($admindateformat==2)
	$admindatestr='d/m/Y';
$alreadygotadmin = getadminsettings();
$sSQL = '';
$nonhomecountries=$dorefresh=FALSE;
if(@$maxloginlevels=='') $maxloginlevels=5;
$resultcounter=0;
$allcountries=$allstates=[];
$numallcountries=$numallstates=0;
if(@$adminclientloginperpage=='') $adminclientloginperpage=200;
if(is_numeric(getrequest("perpage"))) $adminclientloginperpage=(int)getrequest("perpage");
if(getrequest("perpage")=="x") $adminclientloginperpage=1000000;
function dodeletecl($clid){
	if(is_numeric($clid)){
		$sSQL = "DELETE FROM customerlogin WHERE clID='" . $clid . "'";
		ect_query($sSQL) or ect_error();
		$sSQL = "DELETE FROM address WHERE addCustID='" . $clid . "'";
		ect_query($sSQL) or ect_error();
		$sSQL = "UPDATE orders SET ordClientID=0 WHERE ordClientID='" . $clid . "'";
		ect_query($sSQL) or ect_error();
	}
}
function dispcountries($ind){
	global $allcountries,$numallcountries,$yySelect;
	$dspc='<select size="1" id="newcntryname'.$ind.'" name="newcntryname'.$ind.'"><option value="">'.$yySelect.'</option>';
	$dspc.='<option value="xxxdeletexxx">'.'DELETE ADDRESS - Country No Longer Supported'.'</option>';
	$dspc.='<option value="" disabled="disabled">==============================</option>';
	for($index=0;$index<$numallcountries;$index++){
		$dspc.='<option value="' . htmlspecials($allcountries[$index]['countryID']) . '">'.$allcountries[$index]['countryName']."</option>\n";
	}
	return($dspc.'</select>');
}
function dispstates($ind){
	global $allstates,$numallstates,$yySelect;
	$dspc='<select size="1" id="newstatename'.$ind.'" name="newstatename'.$ind.'"><option value="">'.$yySelect.'</option>';
	$dspc.='<option value="xxxdeletexxx">'.'DELETE ADDRESS - State No Longer Supported'.'</option>';
	$dspc.='<option value="" disabled="disabled">==============================</option>';
	for($index=0;$index<$numallstates;$index++){
		$dspc.='<option value="' . htmlspecials($allstates[$index]['stateID']) . '">'.$allstates[$index]['stateName']."</option>\n";
	}
	return($dspc.'</select>');
}
if(getpost('updatestate')=='1'){
	$oldstateid=getpost("oldstatename");
	if(is_numeric($oldstateid)){
		$sSQL="SELECT addState,addZip,addCountry FROM address WHERE addID=" . $oldstateid;
		$result=ect_query($sSQL) or ect_error();
		if($rs=ect_fetch_assoc($result)){
			$statename=$rs['addState'];
			$statecountry=$rs['addCountry'];
		}
		$sSQL="";
		if(getpost("newstatename")=="xxxdeletexxx"){
			if($statecountry!=""){
				ect_query("DELETE FROM address WHERE addID=" . $oldstateid) or ect_error();
				$sSQL="DELETE FROM address WHERE addState='" . escape_string($statename) . "' AND addCountry='" . escape_string($statecountry) . "'";
				ect_query($sSQL) or ect_error();
			}
		}else{
			$newstateid=getpost("newstatename");
			if(is_numeric($newstateid)){
				$newstatename="";
				$sSQL="SELECT stateID,stateName,stateAbbrev FROM states WHERE stateID=" . $newstateid;
				$result=ect_query($sSQL) or ect_error();
				if($rs=ect_fetch_assoc($result))
					$newstatename=$rs[@$GLOBALS['usestateabbrev']?"stateAbbrev":"stateName"];
				if($newstatename!=""){
					$sSQL="UPDATE address SET addState='" . escape_string($newstatename) . "' WHERE addState='" . escape_string($statename) . "' AND addCountry='" . escape_string($statecountry) . "'";
					ect_query($sSQL) or ect_error();
				}
			}
		}
	}
}elseif(getpost('updatecountry')=='1'){
	$oldcountryid=getpost("oldcntryname");
	$sSQL="SELECT addState,addZip,addCountry FROM address WHERE addID=" . $oldcountryid;
	$result=ect_query($sSQL) or ect_error();
	if($rs=ect_fetch_assoc($result))
		$countryname=$rs['addCountry'];
	$sSQL="";
	if(getpost("newcntryname")=="xxxdeletexxx"){
		ect_query("DELETE FROM address WHERE addID=" . $oldcountryid);
		$sSQL="DELETE FROM address WHERE addCountry='" . escape_string($countryname) . "'";
		ect_query($sSQL) or ect_error();
	}else{
		$newcountryid=getpost("newcntryname");
		if(is_numeric($newcountryid)){
			$newcountryname="";
			$sSQL="SELECT countryName FROM countries WHERE countryID=" . $newcountryid;
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result))
				$newcountryname=$rs['countryName'];
			if($newcountryname!=""){
				$sSQL="UPDATE address SET addCountry='" . escape_string($newcountryname) . "' WHERE addCountry='" . escape_string($countryname) . "'";
				ect_query($sSQL) or ect_error();
			}
		}
	}
}elseif(getpost('posted')=='1'){
	if(getpost("act")=="quickupdate"){
		$clact=getpost("clact");
		foreach(@$_POST as $objItem => $objValue){
			if(substr($objItem, 0, 4)=='pra_'){
				$origid=substr($objItem, 4);
				$theid=getpost('pid'.$origid);
				$theval=trim_(unstripslashes($objValue));
				$sSQL='';
				if($clact=="dsc"){
					if(is_numeric($theval)) $sSQL="UPDATE customerlogin SET clPercentDiscount='" . escape_string($theval) . "'";
				}elseif($clact=='ste' || $clact=='cte' || $clact=='she' || $clact=='wsp' || $clact=='ped' || $clact=='hae'){
					$fieldnum=1;
					if($clact=='cte') $fieldnum=2;
					if($clact=='she') $fieldnum=4;
					if($clact=='wsp') $fieldnum=8;
					if($clact=='ped') $fieldnum=16;
					if($clact=='hae') $fieldnum=32;
					$result=ect_query("SELECT clActions FROM customerlogin WHERE clID='".escape_string($theid)."'") or ect_error();
					if($rs=ect_fetch_assoc($result))$theval=$rs['clActions']; else $theval=0;
					ect_free_result($result);
					if(@$_POST['prb_' . $origid]=="1")
						$theval |= $fieldnum;
					else{
						$theval &= ~$fieldnum;
					}
					$sSQL="UPDATE customerlogin SET clActions=" . $theval;
				}elseif($clact=='lol'){
					$sSQL="UPDATE customerlogin SET clLoginLevel='" . escape_string($theval) . "'";
				}elseif($clact=='del'){
					if($theval=="del") dodeletecl($theid);
					$sSQL='';
				}
				if($sSQL!=''){
					$sSQL.=" WHERE clID='".escape_string($theid)."'";
					ect_query($sSQL) or ect_error();
				}
			}
		}
		$dorefresh=TRUE;
	}elseif(getpost('act')=='delete'){
		dodeletecl(getpost('id'));
		$dorefresh=TRUE;
	}elseif(getpost('act')=='deleteaddress'){
		$sSQL = "DELETE FROM address WHERE addID='" . getpost('theid') . "'";
		ect_query($sSQL) or ect_error();
	}elseif(getpost('act')=='doeditaddress' || getpost('act')=='donewaddress'){
		$addID=str_replace("'",'',getpost('theid'));
		if(!is_numeric($addID))$addID=0;
		$ordName=strip_tags(getpost('name'));
		$ordLastName=strip_tags(getpost('lastname'));
		$ordAddress=strip_tags(getpost('address'));
		$ordAddress2=strip_tags(getpost('address2'));
		$ordState=strip_tags(getpost('state2'));
		if(getpost('state')!='')
			$ordState=strip_tags(getpost('state'));
		$ordState=strip_tags(getstatefromid($ordState));
		$ordCity=strip_tags(getpost('city'));
		$ordZip=strip_tags(getpost('zip'));
		$ordPhone=strip_tags(getpost('phone'));
		$ordCountry=strip_tags(getcountryfromid(getpost('country')));
		$ordExtra1=strip_tags(getpost('ordextra1'));
		$ordExtra2=strip_tags(getpost('ordextra2'));
		if(getpost('act')=='doeditaddress')
			$sSQL = "UPDATE address SET addName='" . escape_string($ordName) . "',addLastName='" . escape_string($ordLastName) . "',addAddress='" . escape_string($ordAddress) . "',addAddress2='" . escape_string($ordAddress2) . "',addCity='" . escape_string($ordCity) . "',addState='" . escape_string($ordState) . "',addZip='" . escape_string($ordZip) . "',addCountry='" . escape_string($ordCountry) . "',addPhone='" . escape_string($ordPhone) . "',addExtra1='" . escape_string($ordExtra1) . "',addExtra2='" . escape_string($ordExtra2) . "' WHERE addID=" . $addID;
		else
			$sSQL = "INSERT INTO address (addCustID,addIsDefault,addName,addLastName,addAddress,addAddress2,addCity,addState,addZip,addCountry,addPhone,addExtra1,addExtra2) VALUES (" . getpost('id') . ",0,'" . escape_string($ordName) . "','" . escape_string($ordLastName) . "','" . escape_string($ordAddress) . "','" . escape_string($ordAddress2) . "','" . escape_string($ordCity) . "','" . escape_string($ordState) . "','" . escape_string($ordZip) . "','" . escape_string($ordCountry) . "','" . escape_string($ordPhone) . "','" . escape_string($ordExtra1) . "','" . escape_string($ordExtra2) . "')";
		ect_query($sSQL) or ect_error();
	}elseif(getpost('act')=='domodify'){
		$sSQL = "SELECT clEmail FROM customerlogin WHERE clID<>'" . getpost('id') . "' AND clEmail='" . escape_string(getpost('clEmail')) . "'";
		$result=ect_query($sSQL) or ect_error();
		if($rs=ect_fetch_assoc($result)){
			$success=FALSE;
			$errmsg=$yyEmReg . '<br />' . htmlspecials(getpost('clEmail'));
		}
		ect_free_result($result);
		if(getpost('clUserName')==''){
			$success=FALSE;
			$errmsg='Username is NULL';
		}
		if($success){
			$sSQL = "UPDATE customerlogin SET clUserName='" . escape_string(getpost('clUserName')) . "'";
			if(getpost('clPW')!='') $sSQL.=",clPW='" . escape_string(dohashpw(getpost('clPW'))) . "'";
			$sSQL.=",clLoginLevel=" . getpost('clLoginLevel');
			$sSQL.=",loyaltyPoints=" . getpostint('loyaltyPoints');
			$cpd = getpost('clPercentDiscount');
			$sSQL.=",clPercentDiscount=" . (is_numeric($cpd) ? $cpd : 0);
			if(trim_(@$extraclientfield1)!='') $sSQL.=",clientCustom1='" . escape_string(getpost('clientCustom1')) . "'";
			if(trim_(@$extraclientfield2)!='') $sSQL.=",clientCustom2='" . escape_string(getpost('clientCustom2')) . "'";
			$sSQL.=",clientAdminNotes='" . escape_string(getpost('clientAdminNotes')) . "'";
			$sSQL.=",clEmail='" . escape_string(getpost('clEmail')) . "'";
			$sSQL.=",avalaraentityuse='" . escape_string(getpost('avalaraentityuse')) . "'";
			$clActions=0;
			if(is_array(@$_POST['clActions'])){
				foreach(@$_POST['clActions'] as $objValue){
					if(is_array($objValue)) $objValue = $objValue[0];
					$clActions+=$objValue;
				}
			}
			$sSQL.=",clActions=" . $clActions;
			$sSQL.=" WHERE clID='" . getpost('id') . "'";
			ect_query($sSQL) or ect_error();
			if(getpost('clAllowEmail')=='ON'){
				$sSQL="SELECT email FROM mailinglist WHERE email='" . escape_string(strtolower(getpost('clEmail'))) . "'";
				$result=ect_query($sSQL) or ect_error();
				if(ect_num_rows($result)==0){
					@ect_query("INSERT INTO mailinglist (email,isconfirmed,mlConfirmDate,mlIPAddress) VALUES ('" . escape_string(strtolower(getpost('clEmail'))) . "',1,'".date('Y-m-d', time())."','".escape_string(getipaddress())."')");
				}
			}else
				ect_query("DELETE FROM mailinglist WHERE email='" . escape_string(getpost('clEmail')) . "'");
			$dorefresh=TRUE;
		}
	}elseif(getpost('act')=='doaddnew'){
		$sSQL = "SELECT clEmail FROM customerlogin WHERE clEmail='" . escape_string(getpost('clEmail')) . "'";
		$result=ect_query($sSQL) or ect_error();
		if($rs=ect_fetch_assoc($result)){
			$success=FALSE;
			$errmsg=$yyEmReg . '<br />' . htmlspecials(getpost('clEmail'));
		}
		ect_free_result($result);
		if(getpost('clUserName')==''){
			$success=FALSE;
			$errmsg='Username is NULL';
		}
		if($success){
			$sSQL = "INSERT INTO customerlogin (clUserName,clPW,clLoginLevel,loyaltyPoints,clPercentDiscount,clientCustom1,clientCustom2,clientAdminNotes,clEmail,clDateCreated,clActions,avalaraentityuse) VALUES (";
			$sSQL.="'" . escape_string(getpost('clUserName')) . "'";
			$sSQL.=",'" . escape_string(dohashpw(getpost('clPW'))) . "'";
			$sSQL.="," . getpost('clLoginLevel');
			$sSQL.="," . (is_numeric(getpost('loyaltyPoints'))?getpost('loyaltyPoints'):0);
			$cpd = getpost('clPercentDiscount');
			$sSQL.="," . (is_numeric($cpd) ? $cpd : 0);
			$sSQL.=",'" . escape_string(getpost('clientCustom1')) . "'";
			$sSQL.=",'" . escape_string(getpost('clientCustom2')) . "'";
			$sSQL.=",'" . escape_string(getpost('clientAdminNotes')) . "'";
			$sSQL.=",'" . escape_string(getpost('clEmail')) . "'";
			$sSQL.=",'" . date('Y-m-d', time() + ($GLOBALS['dateadjust']*60*60)) . "'";
			$clActions=0;
			if(is_array(@$_POST['clActions'])){
				foreach(@$_POST['clActions'] as $objValue){
					if(is_array($objValue)) $objValue = $objValue[0];
					$clActions+=$objValue;
				}
			}
			$sSQL.=',' . $clActions . ",'" . escape_string(getpost('avalaraentityuse')) . "')";
			ect_query($sSQL) or ect_error();
			if(getpost('clAllowEmail')=='ON')
				ect_query("INSERT INTO mailinglist (email,isconfirmed,mlConfirmDate,mlIPAddress) VALUES ('" . escape_string(strtolower(getpost('clEmail'))) . "',1,'".date('Y-m-d', time())."','".escape_string(getipaddress())."')");
			else
				ect_query("DELETE FROM mailinglist WHERE email='" . escape_string(getpost('clEmail')) . "'");
			$dorefresh=TRUE;
		}
	}elseif(getpost('act')=='addorphans'){
		$sSQL = "SELECT clEmail FROM customerlogin WHERE clID='" . getpost('id') . "'";
		$result=ect_query($sSQL) or ect_error();
		if($rs=ect_fetch_assoc($result)){
			$theemail = $rs['clEmail'];
		}
		ect_free_result($result);
		if(@$loyaltypoints!=''){
			$loyaltypointtotal=0;
			$sSQL = "SELECT SUM(loyaltyPoints) AS pointsSum FROM orders WHERE ordClientID=0 AND ordEmail='" . escape_string($theemail) . "'";
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result))
				if($rs['pointsSum']!=NULL) $loyaltypointtotal = $rs['pointsSum'];
			ect_free_result($result);
			$sSQL = "UPDATE customerlogin SET loyaltyPoints=loyaltyPoints+" . $loyaltypointtotal . " WHERE clID='" . escape_string(getpost('id')) . "'";
			ect_query($sSQL) or ect_error();
		}
		$sSQL = "UPDATE orders SET ordClientID='".escape_string(getpost('id'))."' WHERE ordEmail='" . escape_string($theemail) . "'";
		ect_query($sSQL) or ect_error();
	}elseif(getpost('act')=='addorphan'){
		if(@$loyaltypoints!=''){
			$loyaltypointtotal=0;
			$sSQL = "SELECT loyaltyPoints FROM orders WHERE ordClientID=0 AND ordID='" . escape_string(getpost('theid')) . "'";
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result))
				$loyaltypointtotal = $rs['loyaltyPoints'];
			ect_free_result($result);
			$sSQL = "UPDATE customerlogin SET loyaltyPoints=loyaltyPoints+" . $loyaltypointtotal . " WHERE clID='" . escape_string(getpost('id')) . "'";
			ect_query($sSQL) or ect_error();
		}
		$sSQL = "UPDATE orders SET ordClientID='".escape_string(getpost('id'))."' WHERE ordID='" . escape_string(getpost('theid')) . "'";
		ect_query($sSQL) or ect_error();
	}
}
if($dorefresh){
	print '<meta http-equiv="refresh" content="1; url=adminclientlog.php';
	print '?stext=' . urlencode(getpost('stext')) . '&accdate=' . urlencode(getpost('accdate')) . '&slevel=' . urlencode(getpost('slevel')) . '&stype=' . urlencode(getpost('stype')) . '&daterange=' . urlencode(getpost('daterange')) . '&pg=' . urlencode(getpost('pg'));
	print '">';
}
?>
<script>
<!--
function formvalidator(theForm){
if (theForm.clUserName.value==""){
alert("<?php print jscheck($yyPlsEntr . ' "' . $yyLiName)?>\".");
theForm.clUserName.focus();
return (false);
}
return (true);
}
function vieworder(theid){
	document.location="adminorders.php?id="+theid;
}
function editaddress(theid){
	document.forms.mainform.act.value="editaddress";
	document.forms.mainform.theid.value=theid;
	document.forms.mainform.submit();
}
function newaddress(){
	document.forms.mainform.act.value="newaddress";
	document.forms.mainform.submit();
}
function editaccount(){
	document.forms.mainform.act.value="modify";
	document.forms.mainform.submit();
}
function addorphan(theid){
	if(confirm("<?php print jscheck($yySureCa)?>")){
		document.forms.mainform.act.value="addorphan";
		document.forms.mainform.theid.value=theid;
		document.forms.mainform.submit();
	}
}
function addorphans(){
	if(confirm("<?php print jscheck($yySureCa)?>")){
		document.forms.mainform.act.value="addorphans";
		document.forms.mainform.submit();
	}
}
function deleteaddress(theid){
	if(confirm("<?php print jscheck($GLOBALS['xxDelAdd'])?>")){
		document.forms.mainform.act.value="deleteaddress";
		document.forms.mainform.theid.value=theid;
		document.forms.mainform.submit();
	}
}
//-->
</script>
<?php	if(getpost('posted')=='1' && (getpost('act')=='modify' || getpost('act')=='addnew')){
			if(getpost('act')=='modify' && is_numeric(getpost('id'))){
				$sSQL = "SELECT clUserName,clPW,clLoginLevel,clActions,clPercentDiscount,clEmail,clDateCreated,loyaltyPoints,avalaraentityuse,clientCustom1,clientCustom2,clientAdminNotes FROM customerlogin WHERE clID='" . getpost('id') . "'";
				$result=ect_query($sSQL) or ect_error();
				$rs=ect_fetch_assoc($result);
				$clUserName=$rs['clUserName'];
				$clPW='';
				$clLoginLevel=$rs['clLoginLevel'];
				$clActions=$rs['clActions'];
				$clPercentDiscount=$rs['clPercentDiscount'];
				$clEmail=$rs['clEmail'];
				$clDateCreated=$rs['clDateCreated'];
				$clLoyaltyPoints=$rs['loyaltyPoints'];
				$avalaraentityuse=$rs['avalaraentityuse'];
				$clientCustom1=$rs['clientCustom1'];
				$clientCustom2=$rs['clientCustom2'];
				$clientAdminNotes=$rs['clientAdminNotes'];
				ect_free_result($result);
				$sSQL = "SELECT email FROM mailinglist WHERE email='" . escape_string($clEmail) . "'";
				$result=ect_query($sSQL) or ect_error();
				if(ect_num_rows($result)>0) $clAllowEmail=1; else $clAllowEmail=0;
				ect_free_result($result);
			}else{
				$clUserName='';
				$clPW='';
				$clLoginLevel=0;
				$clActions=0;
				$clPercentDiscount=0;
				$clEmail='';
				$clDateCreated=date('Y-m-d');
				$clAllowEmail=0;
				$clLoyaltyPoints=0;
				$clientCustom1='';
				$clientCustom2='';
				$clientAdminNotes='';
				$avalaraentityuse='';
			}
?>
	<form name="mainform" method="post" action="adminclientlog.php" onsubmit="return formvalidator(this)">
<?php		writehiddenvar('posted', '1');
			if(getpost('act')=='modify')
				writehiddenvar('act', 'domodify');
			else
				writehiddenvar('act', 'doaddnew');
			writehiddenvar('stext', getpost('stext'));
			writehiddenvar('accdate', getpost('accdate'));
			writehiddenvar('daterange', getpost('daterange'));
			writehiddenvar('slevel', getpost('slevel'));
			writehiddenvar('stype', getpost('stype'));
			writehiddenvar('pg', getpost('pg'));
			writehiddenvar('id', getpost('id')); ?>
            <table width="100%" border="0" cellspacing="2" cellpadding="2">
			  <tr> 
                <td width="100%" colspan="4" align="center"><strong><?php print $yyLiAdm?></strong><br /><br /><?php print '<strong>' . $yyDateCr. ':</strong> ' . date($admindatestr, strtotime($clDateCreated)); ?><br /><br /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print $yyLiName?>:</strong></td>
				<td align="left"><input type="text" name="clUserName" size="20" value="<?php print htmlspecials($clUserName)?>" /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print $yyEmail?>:</strong></td>
				<td align="left"><input type="text" name="clEmail" size="30" value="<?php print htmlspecials($clEmail)?>" /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print $yyReset.' '.$yyPass?>:</strong></td>
				<td align="left"><input type="text" name="clPW" size="20" value="" /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print $yyLiLev?>:</strong></td>
				<td align="left"><select name="clLoginLevel" size="1">
				<?php	for($rowcounter=0; $rowcounter<=$maxloginlevels; $rowcounter++){
							print '<option value="' . $rowcounter . '"';
							if($rowcounter==(int)$clLoginLevel) print ' selected';
							print '>&nbsp; ' . $rowcounter . " </option>\r\n";
						} ?>
				</select></td>
			  </tr>
			  <tr>
				<td align="right" valign="top"><strong><?php print $yyActns?>:</strong></td>
				<td align="left" valign="top"><select name="clActions[]" size="6" multiple="multiple">
				<option value="1"<?php if(($clActions & 1)==1) print ' selected' ?>><?php print $yyExStat?></option>
				<option value="2"<?php if(($clActions & 2)==2) print ' selected' ?>><?php print $yyExCoun?></option>
				<option value="4"<?php if(($clActions & 4)==4) print ' selected' ?>><?php print $yyExShip?></option>
				<option value="32"<?php if(($clActions & 32)==32) print ' selected' ?>><?php print $yyExHand?></option>
				<option value="8"<?php if(($clActions & 8)==8) print ' selected' ?>><?php print $yyWholPr?></option>
				<option value="16"<?php if(($clActions & 16)==16) print ' selected' ?>><?php print $yyPerDis?></option>
				<option value="64"<?php if(($clActions & 64)==64) print ' selected' ?>><?php print 'Share Carts'?></option>
				</select></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print $yyPerDis?>:</strong></td>
				<td align="left"><input type="text" name="clPercentDiscount" size="10" value="<?php print $clPercentDiscount?>" /></td>
			  </tr>
			  <tr>
				<td align="right"><strong><?php print $yyAllEml?>:</strong></td>
				<td align="left"><input type="checkbox" name="clAllowEmail" value="ON"<?php if($clAllowEmail!=0) print ' checked'?> /></td>
			  </tr>
<?php		if(@$loyaltypoints!=''){ ?>
			  <tr>
				<td align="right" height="22"><strong><?php print $GLOBALS['xxLoyPoi']?>:</strong></td>
				<td align="left"><input type="text" name="loyaltyPoints" size="10" value="<?php print $clLoyaltyPoints?>" /></td>
			  </tr>
<?php		}
			if(trim_(@$extraclientfield1)!=''){ ?>
			  <tr>
				<td align="right" height="22"><strong><?php print $extraclientfield1?>:</strong></td>
				<td align="left"><input type="text" name="clientCustom1" size="30" value="<?php print $clientCustom1?>" /></td>
			  </tr>
<?php		}
			if(trim_(@$extraclientfield2)!=''){ ?>
			  <tr>
				<td align="right" height="22"><strong><?php print $extraclientfield2?>:</strong></td>
				<td align="left"><input type="text" name="clientCustom2" size="30" value="<?php print $clientCustom2?>" /></td>
			  </tr>
<?php		}
			if(@$GLOBALS['avalarastate']!=0){ ?>
				<tr>
					<td align="right" height="22"><strong><?php print "Entity Use Code"?>: </strong></td>		
					<td><select name="avalaraentityuse">
							<option value="">Not Set (Only set for tax exemption)</option>
							<option value="A"<?php print $avalaraentityuse=='A'?' selected':''?>>FEDERAL GOV</option>
							<option value="B"<?php print $avalaraentityuse=='B'?' selected':''?>>STATE GOV</option>
							<option value="C"<?php print $avalaraentityuse=='C'?' selected':''?>>TRIBAL GOVERNMENT</option>
							<option value="D"<?php print $avalaraentityuse=='D'?' selected':''?>>FOREIGN DIPLOMAT</option>
							<option value="E"<?php print $avalaraentityuse=='E'?' selected':''?>>CHARITABLE/EXEMPT ORG</option>
							<option value="F"<?php print $avalaraentityuse=='F'?' selected':''?>>RELIGIOUS ORG</option>
							<option value="G"<?php print $avalaraentityuse=='G'?' selected':''?>>RESALE</option>
							<option value="H"<?php print $avalaraentityuse=='H'?' selected':''?>>AGRICULTURE</option>
							<option value="I"<?php print $avalaraentityuse=='I'?' selected':''?>>INDUSTRIAL PROD/MANUFACTURERS</option>
							<option value="J"<?php print $avalaraentityuse=='J'?' selected':''?>>DIRECT PAY</option>
							<option value="K"<?php print $avalaraentityuse=='K'?' selected':''?>>DIRECT MAIL</option>
							<option value="L"<?php print $avalaraentityuse=='L'?' selected':''?>>OTHER/CUSTOM</option>
							<option value="M"<?php print $avalaraentityuse=='M'?' selected':''?>>EDUCATIONAL ORG</option>
							<option value="N"<?php print $avalaraentityuse=='N'?' selected':''?>>LOCAL GOVERNMENT</option>
							<option value="P"<?php print $avalaraentityuse=='P'?' selected':''?>>COMMERCIAL AQUACULTURE</option>
							<option value="Q"<?php print $avalaraentityuse=='Q'?' selected':''?>>COMMERCIAL FISHERY</option>
							<option value="R"<?php print $avalaraentityuse=='R'?' selected':''?>>NON-RESIDENT</option>
							<option value="TAXABLE"<?php print $avalaraentityuse=='TAXABLE'?' selected':''?>>NON-EXEMPT TAXABLE CUSTOMER</option>
						</select>
					</td>
				</tr>
<?php		} ?>
			  <tr>
				<td align="right" height="22"><strong>Client Admin Notes:</strong></td>
				<td align="left"><textarea name="clientAdminNotes" cols="60" rows="10"><?php print htmlspecials($clientAdminNotes)?></textarea></td>
			  </tr>
			  <tr>
                <td width="100%" colspan="4" align="center"><br /><input type="submit" value="<?php print $yySubmit?>" />&nbsp;<input type="reset" value="<?php print $yyReset?>" /><br />&nbsp;</td>
			  </tr>
			  <tr> 
                <td width="100%" colspan="4" align="center"><br />
                          <a href="admin.php"><strong><?php print $yyAdmHom?></strong></a><br />
                          &nbsp;</td>
			  </tr>
            </table>
	</form>
<?php	}elseif(getget('act')=='mapcountries'){
			$sSQL="SELECT addID,addCountry,countryID FROM address LEFT JOIN countries ON address.addCountry=countries.countryName WHERE countryID IS NULL LIMIT 0,100";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0){ ?>
				<form id="updcntryid" method="post" action="adminclientlog.php?act=mapcountries">
				<input type="hidden" name="updatecountry" value="1">
				<input type="hidden" id="oldcntryname" name="oldcntryname" value="" />
				<input type="hidden" id="newcntryname" name="newcntryname" value="" />
				</form>
				<table border="1" cellspacing="3" cellpadding="3">
				  <tr><td colspan="3">There are countries in the client login table that do not now exist. These need to be mapped to actual countries.</td></tr>
<?php			$sSQL="SELECT countryName,countryID FROM countries WHERE countryEnabled=1 ORDER BY countryOrder DESC,countryName";
				$result2=ect_query($sSQL) or ect_error();
				while($rs2=ect_fetch_assoc($result2))
					$allcountries[$numallcountries++]=$rs2;
				$index=0;
				while($rs=ect_fetch_assoc($result)){
					print "<tr><td>" . $rs['addCountry'] . "</td><td>" . dispcountries($index) . "</td><td>"; ?>
<input type="button" value="<?php print $yySubmit?>" onclick="document.getElementById('oldcntryname').value='<?php print jsspecials($rs['addID'])?>';document.getElementById('newcntryname').value=document.getElementById('newcntryname<?php print $index?>')[document.getElementById('newcntryname<?php print $index?>').selectedIndex].value;if(document.getElementById('newcntryname<?php print $index?>').selectedIndex==0)alert('Please select a country...');else document.getElementById('updcntryid').submit()" />
<?php				print "</td></tr>";
					$index++;
				} ?>
				</table>
<?php		}else
				print '<div style="text-align:center;margin:30px">All countries are now correctly mapped.</div>';
			ect_free_result($result);
			print '<div style="text-align:center;margin:30px"><a href="adminclientlog.php">' . $yyClkBac . '</a></div>';
		}elseif(getget('act')=='mapstates'){
			$countrylist='';
			$sSQL="SELECT countryName,countryName2,countryName3 FROM countries WHERE countryID IN (1,2)";
			$result=ect_query($sSQL) or ect_error();
			while($rs=ect_fetch_assoc($result)){
				$countrylist.=($countrylist!=''?',':'')."'".escape_string($rs['countryName'])."',"."'".escape_string($rs['countryName2'])."',"."'".escape_string($rs['countryName3'])."'";
			}
			ect_free_result($result);
			
			$sSQL="SELECT addID,addState,addCity,addZip,stateID FROM address LEFT JOIN states ON address.addState=states.".(@$GLOBALS['usestateabbrev']?"stateAbbrev":"stateName")." WHERE addCountry IN (".$countrylist.") AND stateID IS NULL LIMIT 0,100";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0){ ?>
				<form id="updstateid" method="post" action="adminclientlog.php?act=mapstates">
				<input type="hidden" name="updatestate" value="1">
				<input type="hidden" id="oldstatename" name="oldstatename" value="">
				<input type="hidden" id="newstatename" name="newstatename" value="">
				</form>
				<table border="1" cellspacing="3" cellpadding="3">
				  <tr><td colspan="3">There are states in the client login table that do not now exist. These need to be mapped to actual states.</td></tr>
<?php			$sSQL="SELECT ".(@$GLOBALS['usestateabbrev']?"stateAbbrev":"stateName")." AS stateValue,stateID,stateName FROM states WHERE stateCountryID IN (1,2) AND stateEnabled=1 ORDER BY stateName";
				$result2=ect_query($sSQL) or ect_error();
				while($rs2=ect_fetch_assoc($result2))
					$allstates[$numallstates++]=$rs2;
				$index=0;
				while($rs=ect_fetch_assoc($result)){
					print "<tr><td>" . (trim_($rs['addCity'])=='' ? '(blank)' : $rs['addCity']) . ', ' .  (trim_($rs['addState'])=='' ? '(blank)' : $rs['addState']) . ', ' .  (trim_($rs['addZip'])=='' ? '(blank)' : $rs['addZip']) . "</td><td>" . dispstates($index) . "</td><td>"; ?>
<input type="button" value="<?php print $yySubmit?>" onclick="document.getElementById('oldstatename').value='<?php print jsspecials($rs['addID'])?>';document.getElementById('newstatename').value=document.getElementById('newstatename<?php print $index?>')[document.getElementById('newstatename<?php print $index?>').selectedIndex].value;if(document.getElementById('newstatename<?php print $index?>').selectedIndex==0)alert('Please select a state...');else document.getElementById('updstateid').submit()" />
<?php				print "</td></tr>";
					$index++;
					if($index>200) break;
				} ?>
				</table>
<?php		}else
				print '<div style="text-align:center;margin:30px">All states are now correctly mapped.</div>';
			ect_free_result($result);
			print '<div style="text-align:center;margin:30px"><a href="adminclientlog.php">' . $yyClkBac . '</a></div>';
		}elseif(getpost('act')=='editaddress' || getpost('act')=='newaddress'){
			$addID=str_replace("'",'',getpost('theid'));
			if(!is_numeric($addID))$addID=0;
			$addIsDefault='';
			$addName='';
			$addLastName='';
			$addAddress='';
			$addAddress2='';
			$addState='';
			$addCity='';
			$addZip='';
			$addPhone='';
			$addCountry='';
			$addExtra1='';
			$addExtra2='';
			$sSQL = 'SELECT stateID FROM states INNER JOIN countries ON states.stateCountryID=countries.countryID WHERE countryEnabled<>0 AND stateEnabled<>0 AND (loadStates=2 OR countryID=' . $origCountryID . ') ORDER BY stateCountryID,stateName';
			$result=ect_query($sSQL) or ect_error();
			$hasstates = (ect_num_rows($result)>0);
			ect_free_result($result);
			$sSQL = "SELECT countryName,countryOrder,".getlangid("countryName",8).",countryID,loadStates FROM countries WHERE countryEnabled=1 ORDER BY countryOrder DESC," . getlangid("countryName",8);
			$result=ect_query($sSQL) or ect_error();
			while($rs=ect_fetch_assoc($result)){
				$allcountries[$numallcountries++]=$rs;
			}
			ect_free_result($result);
			for($index=0;$index<$numallcountries;$index++){
				if($allcountries[$index]['loadStates']==0){ $nonhomecountries=TRUE; break; }
			}
			if(! $nonhomecountries){
				for($index=0;$index<$numallcountries;$index++){
					if($allcountries[$index]['loadStates']>0){
						$sSQL = 'SELECT stateID FROM states WHERE stateEnabled<>0 AND stateCountryID=' . $allcountries[$index]['countryID'];
						$result=ect_query($sSQL) or ect_error();
						if(ect_num_rows($result)==0) $nonhomecountries=TRUE;
						ect_free_result($result);
						if($nonhomecountries) break;
					}
				}
			}
			if(getpost('act')=='editaddress'){
				$sSQL = "SELECT addID,addIsDefault,addName,addLastName,addAddress,addAddress2,addState,addCity,addZip,addPhone,addCountry,addExtra1,addExtra2 FROM address WHERE addID=" . $addID;
				$result=ect_query($sSQL) or ect_error();
				if($rs=ect_fetch_assoc($result)){
					$addIsDefault=$rs['addIsDefault'];
					$addName=strip_tags($rs['addName']);
					$addLastName=strip_tags($rs['addLastName']);
					$addAddress=strip_tags($rs['addAddress']);
					$addAddress2=strip_tags($rs['addAddress2']);
					$addState=strip_tags($rs['addState']);
					$ordState=$addState;
					$addCity=strip_tags($rs['addCity']);
					$addZip=strip_tags($rs['addZip']);
					$addPhone=strip_tags($rs['addPhone']);
					$addCountry=strip_tags($rs['addCountry']);
					$addExtra1=strip_tags($rs['addExtra1']);
					$addExtra2=strip_tags($rs['addExtra2']);
				}
				ect_free_result($result);
			} ?>
	<form method="post" name="mainform" action="" onsubmit="return checkform(this)">
	<input type="hidden" name="act" value="<?php if(getpost('act')=='editaddress') print 'doeditaddress'; else print 'donewaddress' ?>" />
	<input type="hidden" name="theid" value="<?php print $addID?>" />
	<input type="hidden" name="id" value="<?php print getpost('id')?>" />
	<input type="hidden" name="posted" value="1" />
	  <table class="cobtbl" width="100%" border="0" cellspacing="1" cellpadding="3">
		<tr><td align="center" class="cobhl" colspan="2" height="32"><strong><?php print $GLOBALS['xxEdAdd']?></strong></td></tr>
		<?php	if(trim_(@$GLOBALS['extraorderfield1'])!=''){ ?>
		<tr><td align="right" class="cobhl"><strong><?php print (@$GLOBALS['extraorderfield1required'] ? $redstar : '') . $GLOBALS['extraorderfield1'] ?>:</strong></td><td class="cobll"><?php if(@$GLOBALS['extraorderfield1html']!='') print $GLOBALS['extraorderfield1html']; else print '<input type="text" name="ordextra1" id="ordextra1" size="20" value="' . htmlspecials($addExtra1) . '" />'?></td></tr>
		<?php	} ?>
		<tr><td align="right" width="40%" class="cobhl"><strong><?php print $redstar . $GLOBALS['xxName']?>:</strong></td><td class="cobll"><?php
		if(@$GLOBALS['usefirstlastname']){
			$thestyle='';
			if($addName=='' && $addLastName==''){ $addName=$GLOBALS['xxFirNam']; $addLastName=$GLOBALS['xxLasNam']; $thestyle='style="color:#BBBBBB" '; }
			print '<input type="text" name="name" size="11" value="'.htmlspecials($addName).'" alt="'.$GLOBALS['xxFirNam'].'" onfocus="if(this.value==\''.$GLOBALS['xxFirNam'].'\'){this.value=\'\';this.style.color=\'\';}" '.$thestyle.'/> <input type="text" name="lastname" size="11" value="'.htmlspecials($addLastName).'" alt="'.$GLOBALS['xxLasNam'].'" onfocus="if(this.value==\''.$GLOBALS['xxLasNam'].'\'){this.value=\'\';this.style.color=\'\';}" '.$thestyle.'/>';
		}else
			print '<input type="text" name="name" id="name" size="20" value="'.htmlspecials($addName).'" />';
		?></td></tr>
		<tr><td align="right" class="cobhl"><strong><?php print $redstar . $GLOBALS['xxAddress']?>:</strong></td><td class="cobll"><input type="text" name="address" id="address" size="25" value="<?php print htmlspecials($addAddress)?>" /></td></tr>
		<?php	if(@$useaddressline2==TRUE){ ?>
		<tr><td align="right" class="cobhl"><strong><?php print $GLOBALS['xxAddress2']?>:</strong></td><td class="cobll"><input type="text" name="address2" id="address2" size="25" value="<?php print htmlspecials($addAddress2)?>" /></td></tr>
		<?php	} ?>
		<tr><td align="right" class="cobhl"><strong><?php print $redstar . $GLOBALS['xxCity']?>:</strong></td><td class="cobll"><input type="text" name="city" id="city" size="20" value="<?php print htmlspecials($addCity)?>" /></td></tr>
		<?php	if($hasstates || $nonhomecountries){ ?>
		<tr><td align="right" class="cobhl"><strong><?php print replace($redstar,'<span','<span id="statestar"')?><span id="statetxt"><?php print $GLOBALS['xxState']?></span>:</strong></td><td class="cobll"><select name="state" id="state" size="1" onchange="dosavestate('')"><?php $havestate = show_states($addState) ?></select><input type="text" name="state2" id="state2" size="20" value="<?php if(! $havestate) print htmlspecials($addState)?>" /></td></tr>
		<?php	} ?>
		<tr><td align="right" class="cobhl"><strong><?php print $redstar . $GLOBALS['xxCountry']?>:</strong></td><td class="cobll"><select name="country" id="country" size="1" onchange="checkoutspan('')" ><?php show_countries($addCountry,FALSE) ?></select></td></tr>
		<tr><td align="right" class="cobhl"><strong><?php print replace($redstar,'<span','<span id="zipstar"') . '<span id="ziptxt">' . $GLOBALS['xxZip'] . '</span>'?>:</strong></td><td class="cobll"><input type="text" name="zip" id="zip" size="10" value="<?php print htmlspecials($addZip)?>" /></td></tr>
		<tr><td align="right" class="cobhl"><strong><?php print $redstar . $GLOBALS['xxPhone']?>:</strong></td><td class="cobll"><input type="text" name="phone" id="phone" size="20" value="<?php print htmlspecials($addPhone)?>" /></td></tr>
		<?php	if(trim_(@$GLOBALS['extraorderfield2'])!=''){ ?>
		<tr><td align="right" class="cobhl"><strong><?php print (@$GLOBALS['extraorderfield2required'] ? $redstar : '') . $GLOBALS['extraorderfield2'] ?>:</strong></td><td class="cobll"><?php if(@$GLOBALS['extraorderfield2html']!='') print $GLOBALS['extraorderfield2html']; else print '<input type="text" name="ordextra2" id="ordextra2" size="20" value="' . htmlspecials($addExtra2) . '" />'?></td></tr>
		<?php	} ?>
		<tr><td align="center" colspan="2" class="cobll"><input type="submit" value="<?php print $GLOBALS['xxSubmt']?>" /> <input type="button" value="Cancel" onclick="history.go(-1)" /></td></tr>
	  </table>
	</form>
<script>
/* <![CDATA[ */
var checkedfullname=false;
function zipoptional(cntobj){
var cntid=cntobj[cntobj.selectedIndex].value;
if(cntid==85 || cntid==91 || cntid==154 || cntid==200)return true; else return false;
}
function stateoptional(cntobj){
var cntid=cntobj[cntobj.selectedIndex].value;
if(false<?php
$result=ect_query('SELECT countryID FROM countries WHERE countryEnabled<>0 AND loadStates<0') or ect_error();
while($rs=ect_fetch_assoc($result)) print '||cntid==' . $rs['countryID'];
ect_free_result($result);
?>)return true; else return false;
}
function checkform(frm)
{
<?php if(trim_(@$GLOBALS['extraorderfield1'])!='' && @$GLOBALS['extraorderfield1required']){ ?>
if(frm.ordextra1.value==""){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['extraorderfield1'])?>\".");
	frm.ordextra1.focus();
	return (false);
}
<?php } ?>
if(frm.name.value==""||frm.name.value=="<?php print $GLOBALS['xxFirNam']?>"){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . @$GLOBALS['usefirstlastname'] ? $GLOBALS['xxFirNam'] : $GLOBALS['xxName'])?>\".");
	frm.name.focus();
	return (false);
}
<?php	if(@$GLOBALS['usefirstlastname']){ ?>
if(frm.lastname.value==""||frm.lastname.value=="<?php print $GLOBALS['xxLasNam']?>"){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['xxLasNam'])?>\".");
	frm.lastname.focus();
	return (false);
}
<?php	} ?>
if(frm.address.value==""){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['xxAddress'])?>\".");
	frm.address.focus();
	return (false);
}
if(frm.city.value==""){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['xxCity'])?>\".");
	frm.city.focus();
	return (false);
}
if(stateoptional(document.getElementById('country'))){
	}else if(stateselectordisabled[0]==false){
<?php
	if($hasstates){ ?>
	if(frm.state.selectedIndex==0){
		alert("<?php print jscheck($GLOBALS['xxPlsSlct']) . ' '?>" + document.getElementById('statetxt').innerHTML);
		frm.state.focus();
		return(false);
	}
<?php	} ?>
	}else{
<?php	if($nonhomecountries){ ?>
	if(frm.state2.value==""){
		alert("<?php print jscheck($GLOBALS['xxPlsEntr'])?> \"" + document.getElementById('statetxt').innerHTML + "\".");
		frm.state2.focus();
		return(false);
	}
<?php	} ?>}
if(frm.zip.value=="" && ! zipoptional(document.getElementById('country'))){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['xxZip'])?>\".");
	frm.zip.focus();
	return(false);
}
if(frm.phone.value==""){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['xxPhone'])?>\".");
	frm.phone.focus();
	return (false);
}
<?php if(trim_(@$GLOBALS['extraorderfield2'])!='' && @$GLOBALS['extraorderfield2required']){ ?>
if(frm.ordextra2.value==""){
	alert("<?php print jscheck($GLOBALS['xxPlsEntr'] . ' "' . $GLOBALS['extraorderfield2'])?>\".");
	frm.ordextra2.focus();
	return (false);
}
<?php } ?>
return (true);
}
<?php if(@$termsandconditions==TRUE){ ?>
function showtermsandconds(){
newwin=window.open("termsandconditions.php","Terms","menubar=no, scrollbars=yes, width=420, height=380, directories=no,location=no,resizable=yes,status=no,toolbar=no");
}
<?php } ?>
var savestate=0;
var ssavestate=0;
function dosavestate(shp){
	thestate = eval('document.forms.mainform.'+shp+'state');
	eval(shp+'savestate = thestate.selectedIndex');
}
function checkoutspan(shp){
	document.getElementById(shp+'zipstar').style.display=(zipoptional(document.getElementById(shp+'country'))?'none':'');
	document.getElementById(shp+'statestar').style.display=(stateoptional(document.getElementById(shp+'country'))?'none':'');<?php
	if($hasstates){
		print "thestate=document.getElementById(shp+'state');\r\n";
		print "dynamiccountries(document.getElementById(shp+'country'),shp);\r\n";
	}
	print "if(stateselectordisabled[shp=='s'?1:0]==false&&!stateoptional(document.getElementById(shp+'country'))){\r\n";
	print "if(document.getElementById(shp+'state2'))document.getElementById(shp+'state2').style.display='none';\r\n";
	if($hasstates){
		print "thestate.disabled=false;\r\n";
		print "eval('thestate.selectedIndex='+shp+'savestate');\r\n";
		print "document.getElementById(shp+'state').style.display='';\r\n";
	} ?>
}else{<?php
	print "if(document.getElementById(shp+'state2'))document.getElementById(shp+'state2').style.display='';\r\n";
	if($hasstates){ ?>
		document.getElementById(shp+'state').style.display='none';
		if(thestate.disabled==false){
		thestate.disabled=true;
		eval(shp+'savestate = thestate.selectedIndex');
		thestate.selectedIndex=0;}
<?php
	} ?>
}}
<?php
	createdynamicstates('SELECT stateID,stateAbbrev,stateName,stateName2,stateName3,stateCountryID,countryName FROM states INNER JOIN countries ON states.stateCountryID=countries.countryID WHERE countryEnabled<>0 AND stateEnabled<>0 AND (loadStates=2 OR countryID=' . $origCountryID . ') ORDER BY stateCountryID,' . getlangid('stateName',1048576));
	print "checkoutspan('');setinitialstate('');\r\n";
?>/* ]]> */
</script>
<?php	}elseif((getpost('act')=='viewacct' || getpost('act')=='deleteaddress' || getpost('act')=='addorphans' || getpost('act')=='addorphan') AND is_numeric(getpost('id'))){
			$clID = getpost('id');
			$sSQL = "SELECT clUserName,clPW,clLoginLevel,clActions,clPercentDiscount,clEmail,clDateCreated,loyaltyPoints,clientCustom1,clientCustom2,clientAdminNotes FROM customerlogin WHERE clID='" . $clID . "'";
			$result=ect_query($sSQL) or ect_error();
			$rs=ect_fetch_assoc($result);
			$clUserName=$rs['clUserName'];
			$clPW=$rs['clPW'];
			$clLoginLevel=$rs['clLoginLevel'];
			$clActions=$rs['clActions'];
			$clPercentDiscount=$rs['clPercentDiscount'];
			$clEmail=$rs['clEmail'];
			$clDateCreated=$rs['clDateCreated'];
			$clLoyaltyPoints=$rs['loyaltyPoints'];
			$clientCustom1=$rs['clientCustom1'];
			$clientCustom2=$rs['clientCustom2'];
			$clientAdminNotes=$rs['clientAdminNotes'];
			ect_free_result($result);
			$sSQL = "SELECT email FROM mailinglist WHERE email='" . escape_string($clEmail) . "'";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0) $clAllowEmail=1; else $clAllowEmail=0;
			ect_free_result($result);
			$ordersnotinacct=0;
			$sSQL = "SELECT COUNT(*) AS thecnt FROM orders WHERE ordClientID=0 AND ordEmail='" . escape_string($clEmail) . "'";
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result)){
				if(! is_null($rs['thecnt'])) $ordersnotinacct=$rs['thecnt'];
			}
			ect_free_result($result); ?>
		  <form method="post" name="mainform" action="">
<?php		writehiddenvar('posted', '1');
			writehiddenvar('act', 'none');
			writehiddenvar('theid', '');
			writehiddenvar('stext', getpost('stext'));
			writehiddenvar('accdate', getpost('accdate'));
			writehiddenvar('daterange', getpost('daterange'));
			writehiddenvar('slevel', getpost('slevel'));
			writehiddenvar('stype', getpost('stype'));
			writehiddenvar('pg', getpost('pg'));
			writehiddenvar('id', $clID); ?>
			<table class="cobtbl" width="100%" border="0" cellspacing="1" cellpadding="3">
              <tr> 
                <td class="cobhl" align="center" height="34"><strong><?php print $GLOBALS['xxAccDet']?></strong></td>
			  </tr>
			  <tr> 
                <td class="cobll" height="34" align="center">
				  <table class="cobtbl" width="100%" border="0" cellspacing="1" cellpadding="3">
<?php		$sSQL = "SELECT email,isconfirmed FROM mailinglist WHERE email='" . escape_string($clEmail) . "'";
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result)){ $allowemail=1; $isconfirmed=$rs['isconfirmed']; }else{ $allowemail=0; $isconfirmed=FALSE; }
			ect_free_result($result);
?>
					<tr><td class="cobhl" align="right" width="25%" height="22"><strong><?php print $GLOBALS['xxName']?>:</strong></td>
					<td class="cobll" align="left" width="25%"><?php print htmlspecials($clUserName)?></td>
					<td class="cobhl" align="right" width="25%"><strong><?php print $yyActns?>:</strong></td>
					<td class="cobll" align="left" width="25%"><?php
						if(($clActions & 1)==1) print 'STE ';
						if(($clActions & 2)==2) print 'CTE ';
						if(($clActions & 4)==4) print 'SHE ';
						if(($clActions & 32)==32) print 'HAE ';
						if(($clActions & 8)==8) print 'WSP ';
						if(($clActions & 16)==16) print 'PED '; ?>&nbsp;</td>
					</tr>
					<tr><td class="cobhl" align="right" height="22"><strong><?php print $GLOBALS['xxEmail']?>:</strong></td>
					<td class="cobll" align="left"><?php print htmlspecials($clEmail)?></td>
					<td class="cobhl" align="right"><strong><?php print $GLOBALS['xxAlPrEm']?>:</strong></td>
					<td class="cobll" align="left"><?php if(@$noconfirmationemail!=TRUE && $allowemail!=0 && $isconfirmed==0) print $GLOBALS['xxWaiCon']; else print '<input type="checkbox" name="allowemail" value="ON"' . ($allowemail!=0 ? ' checked="checked"' : '') . ' disabled="disabled" />'?></td>
					</tr>
					<tr><td class="cobhl" align="right" height="22"><strong><?php print $yyPerDis?>:</strong></td>
					<td class="cobll" align="left"><?php if(($clActions & 16)==16) print $clPercentDiscount; else print '-';?></td>
					<td class="cobhl" align="right"><strong><?php print $yyLiLev?>:</strong></td>
					<td class="cobll" align="left"><?php print $clLoginLevel?></td>
					</tr>
<?php		if(@$loyaltypoints!=''){ ?>
					<tr><td class="cobhl" align="right" height="22"><strong><?php print $GLOBALS['xxLoyPoi']?>:</strong></td>
					<td class="cobll" colspan="3" align="left"><?php print $clLoyaltyPoints?></td>
					</tr>
<?php		}
			if(trim_(@$extraclientfield1)!=''){ ?>
					<tr><td class="cobhl" align="right" height="22"><strong><?php print $GLOBALS['extraclientfield1']?>:</strong></td>
					<td class="cobll" colspan="3" align="left"><?php print htmlspecials($clientCustom1)?></td>
					</tr>
<?php		}
			if(trim_(@$extraclientfield2)!=''){ ?>
					<tr><td class="cobhl" align="right" height="22"><strong><?php print $GLOBALS['extraclientfield2']?>:</strong></td>
					<td class="cobll" colspan="3" align="left"><?php print htmlspecials($clientCustom2)?></td>
					</tr>
<?php		} ?>
					<tr><td class="cobhl" align="right" height="22"><strong>Client Admin Notes:</strong></td>
					<td class="cobll" colspan="3" align="left"><?php print ($clientAdminNotes!=''?$clientAdminNotes:'-')?></td>
					</tr>
					<tr><td class="cobll" align="left" colspan="4"><br /><ul><li><?php print $GLOBALS['xxChaAcc']?> <a class="ectlink" href="javascript:editaccount()"><strong><?php print $GLOBALS['xxClkHere']?></strong></a>.</li>
<?php		if($ordersnotinacct!=0) print '<li>' . $ordersnotinacct . " orders with this email are not registered to the account. To add them all please" . ' <a class="ectlink" href="javascript:addorphans()"><strong>'.$GLOBALS['xxClkHere'].'</strong></a>.</li>' ?>
					</ul></td>
					</tr>
				  </table>
				</td>
			  </tr>
              <tr> 
                <td class="cobhl" align="center" height="34"><strong><?php print $GLOBALS['xxAddMan']?></strong></td>
			  </tr>
			  <tr> 
                <td class="cobll" height="34" align="center">
				  <table class="cobtbl" width="100%" border="0" cellspacing="1" cellpadding="3">
<?php		$sSQL = "SELECT addID,addIsDefault,addName,addLastName,addAddress,addAddress2,addState,addCity,addZip,addPhone,addCountry,addExtra1,addExtra2 FROM address WHERE addCustID=" . $clID . " ORDER BY addIsDefault";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0){
				while($rs=ect_fetch_assoc($result)){
					print '<tr><td width="50%" class="cobll" align="left">' . 
						(strip_tags(trim_($rs['addExtra1']))!='' && @$GLOBALS['extraorderfield1']!='' ? $rs['addExtra1'] . '<br>' : '') .
						strip_tags(trim_($rs['addName'].' '.$rs['addLastName'])) . '<br>' .
						strip_tags($rs['addAddress']) . (trim_($rs['addAddress2'])!='' ? '<br>' .
						strip_tags($rs['addAddress2']) : '') . '<br>' .
						strip_tags($rs['addCity']) . ', ' .
						strip_tags($rs['addState']) . ($rs['addZip']!='' ? '<br>' . strip_tags($rs['addZip']) : '') . '<br>' .
						strip_tags($rs['addCountry']) .
						(strip_tags(trim_($rs['addExtra2']))!='' && @$GLOBALS['extraorderfield2']!='' ? '<br>' . $rs['addExtra2'] : '') .
						'</td>';
					print '<td class="cobhl" align="left"><ul><li><a class="ectlink" href="javascript:editaddress(' . $rs['addID'] . ')">' . $GLOBALS['xxEdAdd'] . '</a><br><br></li><li><a class="ectlink" href="javascript:deleteaddress(' . $rs['addID'] . ')">' . $GLOBALS['xxDeAdd'] . '</a></li></ul></td></tr>';
				}
			}else{
				print '<tr><td class="cobll" align="center" colspan="2" height="34">' . $GLOBALS['xxNoAdd'] . '</td></tr>';
			}
			ect_free_result($result);
?>
					<tr><td class="cobhl" colspan="2" align="left"><br /><ul><li><?php print $GLOBALS['xxPCAdd']?> <a class="ectlink" href="javascript:newaddress()"><strong><?php print $GLOBALS['xxClkHere']?></strong></a>.</li></ul></td></tr>
				  </table>
				</td>
			  </tr>
			  <tr> 
                <td class="cobhl" align="center" height="34"><strong><?php print $GLOBALS['xxOrdMan']?></strong></td>
			  </tr>
			  <tr> 
                <td class="cobll" height="34" align="center">
				  <table class="cobtbl" width="100%" border="0" cellspacing="1" cellpadding="3">
<?php		$hastracknum=FALSE;
			$sSQL = "SELECT ordID FROM orders WHERE ordClientID=" . $clID . " AND ordTrackNum<>''";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0) $hastracknum=TRUE;
			ect_free_result($result);
			$hasorphan=FALSE;
			$sSQL = "SELECT ordID FROM orders WHERE ordClientID=0 AND ordEmail='" . escape_string($clEmail) . "'";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0) $hasorphan=TRUE;
			ect_free_result($result); ?>
					<tr><td class="cobhl"><?php print $GLOBALS['xxOrdId']?></td>
					<td class="cobhl"><?php print $GLOBALS['xxDate']?></td>
					<td class="cobhl"><?php print $GLOBALS['xxStatus']?></td>
<?php		if($hastracknum) print '<td class="cobhl">' . $GLOBALS['xxTraNum'] . '</td>'; ?>
					<td class="cobhl"><?php print $GLOBALS['xxGndTot']?></td>
<?php		if($hasorphan) print '<td class="cobhl">' . 'Account' . '</td>'; ?>
					<td class="cobhl"><?php print $GLOBALS['xxCODets']?></td></tr>			
<?php		$grandtotal=0;
			$sSQL = "SELECT ordID,ordDate,ordTrackNum,ordTotal,ordStateTax,ordCountryTax,ordShipping,ordHSTTax,ordHandling,ordDiscount," . getlangid('statPublic',64) . ",ordClientID FROM orders LEFT OUTER JOIN orderstatus ON orders.ordStatus=orderstatus.statID WHERE ordClientID=" . $clID . " OR ordEmail='" . escape_string($clEmail) . "' ORDER BY ordDate";
			$result=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result)>0){
				while($rs=ect_fetch_assoc($result)){
					$subtotal = ($rs['ordTotal']+$rs['ordStateTax']+$rs['ordCountryTax']+$rs['ordShipping']+$rs['ordHSTTax']+$rs['ordHandling'])-$rs['ordDiscount'];
					$grandtotal+=$subtotal;
					print '<tr><td class="cobll">' . $rs['ordID'] . '</td>';
					print '<td class="cobll">' . $rs['ordDate'] . '</td>';
					print '<td class="cobll">' . $rs[getlangid("statPublic",64)] . '</td>';
					if($hastracknum) print '<td class="cobll">' . ($rs['ordTrackNum']!=''?$rs['ordTrackNum']:'&nbsp;') . '</td>';
					print '<td class="cobll" align="right">' . FormatEuroCurrency($subtotal) . '&nbsp;</td>';
					if($hasorphan){
						print '<td class="cobll">';
						if($rs['ordClientID']==0) print '<a href="javascript:addorphan('.$rs['ordID'].')">'.'Link to Account'.'</a>'; else print '&nbsp;';
						print '</td>';
					}
					print '<td class="cobll"><a class="ectlink" href="javascript:vieworder(' . $rs['ordID'] . ')">' . $GLOBALS['xxClkHere'] . '</a></td></tr>';
				}
				if($subtotal!=$grandtotal) print '<tr><td class="cobll" colspan="'.($hastracknum ? '4' : '3') . '">&nbsp;</td><td class="cobll" align="right">' . FormatEuroCurrency($grandtotal) . '&nbsp;</td><td class="cobll"'.($hasorphan?' colspan="2"':'').'>&nbsp;</td></tr>';
			}else{
				print '<tr><td class="cobll" colspan="5" height="34" align="center">' . $GLOBALS['xxNoOrd'] . '</td></tr>';
			}
			ect_free_result($result);
?>
				  </table>
				</td>
			  </tr>
			</table>
		  </form>
<?php	}elseif(getget('loginas')!='' && is_numeric(getget('loginas'))){
			$sSQL = "SELECT clID,clUserName,clPW,clActions,clLoginLevel,clPercentDiscount FROM customerlogin WHERE clID=".getget('loginas');
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result)){
				$_SESSION['clientID']=$rs['clID'];
				$_SESSION['clientUser']=$rs['clUserName'];
				$_SESSION['clientPW']=$rs['clPW'];
				$_SESSION['clientActions']=$rs['clActions'];
				$_SESSION['clientLoginLevel']=$rs['clLoginLevel'];
				$_SESSION['clientPercentDiscount']=(100.0-(float)$rs['clPercentDiscount'])/100.0;
				$redirecturl = $storeurl;
				if(@$_SERVER['HTTPS']=='on') $redirecturl=str_replace('http:','https:',$redirecturl);
				header('Location: '.$redirecturl.'cart'.$GLOBALS['extension']);
			}else
				print 'Login not found';
		}elseif(getpost('posted')=='1' && $success){ ?>
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
        <tr>
          <td width="100%">
			<table width="100%" border="0" cellspacing="0" cellpadding="2">
			  <tr> 
                <td width="100%" colspan="2" align="center"><br /><strong><?php print $yyUpdSuc?></strong><br /><br /><?php print $yyNowFrd?><br /><br />
<?php		if(getpost('act')=='doeditaddress' || getpost('act')=='donewaddress'){ ?>
					<form action="adminclientlog.php" method="post" id="postform">
					<input type="hidden" name="act" value="viewacct" />
					<input type="hidden" name="id" value="<?php print getpost('id')?>" />
					&nbsp;<br />&nbsp;<br />
					<?php print $yyNoAuto?><br />&nbsp;<br />
					<input type="submit" value="<?php print $yyClkHer?>" /><br />&nbsp;<br />&nbsp;
					</form>
<?php			print '<script>document.getElementById("postform").submit();</script>' . "\r\n";
			}else{ ?>
					<?php print $yyNoAuto?> <a href="adminclientlog.php"><strong><?php print $yyClkHer?></strong></a>.<br />&nbsp;</br />
<?php		} ?>                </td>
			  </tr>
			</table></td>
        </tr>
	  </table>
<?php	}elseif(getpost('posted')=="1"){ ?>
	  <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
        <tr>
          <td width="100%">
			<table width="100%" border="0" cellspacing="0" cellpadding="2">
			  <tr> 
                <td width="100%" colspan="2" align="center"><br /><span style="color:#FF0000;font-weight:bold"><?php print $yyOpFai?></span><br /><br /><?php print $errmsg?><br /><br />
				<a href="javascript:history.go(-1)"><strong><?php print $yyClkBac?></strong></a><br />&nbsp;<br />&nbsp;</td>
			  </tr>
			</table></td>
        </tr>
	  </table>
<?php	}else{
			$sortorder=getrequest('sort');
			$clact=@$_COOKIE['clact']; ?>
<script src="popcalendar.js"></script>
<script>
<!--
try{languagetext('<?php print @$adminlang?>');}catch(err){}
function mrec(id){
	document.mainform.id.value = id;
	document.mainform.act.value = "viewacct";
	document.mainform.submit();
}
function newrec(id){
	document.mainform.id.value = id;
	document.mainform.act.value = "addnew";
	document.mainform.submit();
}
function lrec(id){
	window.open('adminclientlog.php?loginas='+id,'clientlogin','menubar=no, scrollbars=yes, width=950, height=700, directories=no,location=no,resizable=yes,status=yes,toolbar=no')
}
function drec(id){
if(confirm("<?php print jscheck($yyConDel)?>\n")){
	document.mainform.id.value = id;
	document.mainform.act.value = "delete";
	document.mainform.submit();
}
}
function startsearch(){
	document.mainform.action="adminclientlog.php";
	document.mainform.act.value = "search";
	document.mainform.posted.value = "";
	document.mainform.submit();
}
function quickupdate(){
	if(document.mainform.clact.value=="del"){
		if(!confirm("<?php print jscheck($yyConDel)?>\n"))
			return;
	}
	document.mainform.action="adminclientlog.php";
	document.mainform.act.value="quickupdate";
	document.mainform.posted.value="1";
	document.mainform.submit();
}
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function changeclact(obj){
	setCookie('clact',obj[obj.selectedIndex].value,600);
	startsearch();
}
function chi(x,pid){
	x.name='pra_'+patch_pid(pid);
}
function setsxz(){
	maxitems=document.getElementById("resultcounter").value;
	amnt=document.getElementById("txtset").selectedIndex;
	for(index=0;index<maxitems;index++){
		if(document.getElementById("sxz"+index)){
			document.getElementById("sxz"+index).selectedIndex=amnt;
			document.getElementById("sxz"+index).onchange();
		}
	}
}
function setto(){
	maxitems=document.getElementById("resultcounter").value;
	amnt=document.getElementById("txtadd").value;
	for(index=0;index<maxitems;index++){
		if(document.getElementById("chkbx"+index)){
			document.getElementById("chkbx"+index).value=amnt;
			document.getElementById("chkbx"+index).onchange();
		}
	}
}
function checkboxes(docheck){
	maxitems=document.getElementById("resultcounter").value;
	for(index=0;index<maxitems;index++){
		var thiselem=document.getElementById("chkbx"+index);
		if(thiselem.checked!=docheck){
			thiselem.checked=docheck;
			thiselem.onchange();
		}
	}
}
// -->
</script>
<h2><?php print $yyAdmCli?></h2>
<?php
	$sSQL="SELECT COUNT(*) AS numcountries FROM address LEFT JOIN countries ON address.addCountry=countries.countryName WHERE countryID IS NULL";
	$result=ect_query($sSQL) or ect_error();
	if($rs=ect_fetch_assoc($result)){
		if($rs['numcountries']>0){ ?>
	<div style="text-align:center;margin:5px"><button type="button" class="mainbutton" onclick="document.location='adminclientlog.php?act=mapcountries'">There are <?php print $rs['numcountries']?> countries in the client login table that do not exist. Click here to correct this.</button></div>
<?php	}
	}
	ect_free_result($result);
	
	$countrylist='';
	$sSQL="SELECT countryName,countryName2,countryName3 FROM countries WHERE countryID IN (1,2)";
	$result=ect_query($sSQL) or ect_error();
	while($rs=ect_fetch_assoc($result)){
		$countrylist.=($countrylist!=''?',':'')."'".escape_string($rs['countryName'])."',"."'".escape_string($rs['countryName2'])."',"."'".escape_string($rs['countryName3'])."'";
	}
	ect_free_result($result);
	
	$sSQL="SELECT COUNT(*) AS numstates FROM address LEFT JOIN states ON address.addState=states.".(@$GLOBALS['usestateabbrev']?"stateAbbrev":"stateName")." WHERE addCountry IN (".$countrylist.") AND stateID IS NULL";
	$result=ect_query($sSQL) or ect_error();
	if($rs=ect_fetch_assoc($result)){
		if($rs['numstates']>0){ ?>
	<div style="text-align:center;margin:5px"><button type="button" class="mainbutton" onclick="document.location='adminclientlog.php?act=mapstates'">There are <?php print $rs['numstates']?> states in the client login table that do not exist. Click here to correct this.</button></div>
<?php	}
	}
	ect_free_result($result);
	
	?>
	<form name="mainform" method="post" action="adminclientlog.php">
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
        <tr>
		  <td width="100%">
			<input type="hidden" name="posted" value="1" />
			<input type="hidden" name="act" value="xxxxx" />
			<input type="hidden" name="id" value="xxxxx" />
			<input type="hidden" name="pg" value="<?php print (getpost('act')=='search' ? '1' : getget('pg')) ?>" />
<?php	$themask = 'yyyy-mm-dd';
		if($admindateformat==1)
			$themask='mm/dd/yyyy';
		elseif($admindateformat==2)
			$themask='dd/mm/yyyy';
		$thelevel = @$_REQUEST['slevel'];
		if(@$thelevel!='') $thelevel = (int)$thelevel;
?>			<table class="cobtbl" width="100%" border="0" cellspacing="1" cellpadding="3">
			  <tr> 
                <td class="cobhl" width="25%" align="right"><?php print $yySrchFr?>:</td>
				<td class="cobll" width="25%"><input type="text" name="stext" size="20" value="<?php print htmlspecials(@$_REQUEST['stext'])?>" /></td>
				<td class="cobhl" width="20%" align="right"><?php print $yyDate?>:</td>
				<td class="cobll"><div style="position:relative;display:inline">
					<select name="daterange" size="1" style="vertical-align:middle">
					<option value=""><?php print $yySinc?></option>
					<option value="1"<?php if(@$_REQUEST['daterange']=="1") print ' selected'?>><?php print $yyTill?></option>
					<option value="2"<?php if(@$_REQUEST['daterange']=="2") print ' selected'?>><?php print $yyOn?></option>
					</select>
					<input type="text" size="14" name="accdate" value="<?php print htmlspecials(@$_REQUEST['accdate'])?>" style="vertical-align:middle" /> <input type="button" onclick="popUpCalendar(this, document.forms.mainform.accdate, '<?php print $themask?>', -205)" value="DP" />
				</div></td>
			  </tr>
			  <tr>
			    <td class="cobhl" align="right"><?php print $yySrchTp?>:</td>
				<td class="cobll"><select name="stype" size="1">
					<option value=""><?php print $yySrchAl?></option>
					<option value="any" <?php if(@$_REQUEST['stype']=='any') print 'selected'?>><?php print $yySrchAn?></option>
					<option value="exact" <?php if(@$_REQUEST['stype']=='exact') print 'selected'?>><?php print $yySrchEx?></option>
					</select>
				</td>
				<td class="cobhl" align="right"><?php print $yyLiLev?>:</td>
				<td class="cobll">
				  <select name="slevel" size="1">
				  <option value=""><?php print $yyAllLev?></option>
<?php						for($rowcounter=0; $rowcounter <= $maxloginlevels; $rowcounter++){
								print "<option value='" . $rowcounter . "'";
								if($thelevel !== '' && $thelevel !== NULL){
									if($thelevel==$rowcounter) print ' selected';
								}
								print '>&nbsp; ' . $rowcounter . ' </option>';
							} ?>
				  </select>
				</td>
              </tr>
			  <tr>
				    <td class="cobhl" align="center">
<?php			if(getpost('act')=='search' || getget('pg')!=''){
					if($clact=='ste' || $clact=='cte' || $clact=='she' || $clact=='wsp' || $clact=='ped' || $clact=='hae' || $clact=='del'){ ?>
					<div style="margin-top:2px"><input type="button" value="<?php print $yyCheckA?>" onclick="checkboxes(true);" /> <input type="button" value="<?php print $yyUCheck?>" onclick="checkboxes(false);" /></div>
<?php				}elseif($clact=='dsc'){ ?>
					<div style="margin-top:2px"><input type="text" name="txtadd" id="txtadd" size="5" value="" style="vertical-align:middle" /> <input type="button" value="Set" onclick="setto()" /></div>
<?php				}elseif($clact=='lol'){ ?>
					<div style="margin-top:2px"><select size="1" id="txtset" style="vertical-align:middle">
						<option value="0">0</option>
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						</select> <input type="button" value="Set" onclick="setsxz()" /></div>
<?php				}
				} ?>
					</td>
				    <td class="cobll" colspan="3"><table width="100%" cellspacing="0" cellpadding="0" border="0">
					    <tr>
						  <td class="cobll" align="center">
							<select name="perpage" size="1" style="vertical-align:middle" title="Per Page">
							<option value="">200</option>
							<option value="300"<?php print (getrequest("perpage")=="300"?" selected":'')?>>300</option>
							<option value="500"<?php print (getrequest("perpage")=="500"?" selected":'')?>>500</option>
							<option value="1000"<?php print (getrequest("perpage")=="1000"?" selected":'')?>>1000</option>
							<option value="2000"<?php print (getrequest("perpage")=="2000"?" selected":'')?>>2000</option>
							<option value="x"<?php print (getrequest("perpage")=="x"?" selected":'')?>>ALL</option>
							</select>
							<select name="sort" size="1" onchange="changesortorder(this)" style="vertical-align:middle">
							<option value="">Sort - <?php print $yyLiName?> ASC</option>
							<option value="nad"<?php if($sortorder=='nad') print ' selected'?>>Sort - <?php print $yyLiName?> DESC</option>
							<option value="ema"<?php if($sortorder=='ema') print ' selected'?>>Sort - Email ASC</option>
							<option value="emd"<?php if($sortorder=='emd') print ' selected'?>>Sort - Email DESC</option>
							<option value="loa"<?php if($sortorder=='loa') print ' selected'?>>Sort - Last Order ASC</option>
							<option value="lod"<?php if($sortorder=='lod') print ' selected'?>>Sort - Last Order DESC</option>
							<option value="tsa"<?php if($sortorder=='tsa') print ' selected'?>>Sort - Total Sales ASC</option>
							<option value="tsd"<?php if($sortorder=='tsd') print ' selected'?>>Sort - Total Sales DESC</option>
							<option value="aca"<?php if($sortorder=='aca') print ' selected'?>>Sort - Actions ASC</option>
							<option value="acd"<?php if($sortorder=='acd') print ' selected'?>>Sort - Actions DESC</option>
							</select>
						  
						  <input type="button" value="<?php print $yyListRe?>" onclick="startsearch();" />
							<input type="button" value="<?php print $yyCLNew?>" onclick="newrec();" />
						  </td>
						  <td class="cobll" height="26" width="20%" align="right">&nbsp;</td>
						</tr>
					  </table></td>
				  </tr>
			</table>
		<table width="100%" class="stackable admin-table-a sta-white">
<?php	$jscript=$qetype=$qesize='';
		if(getpost('act')=='search' || getget('pg')!=''){
			function displayprodrow($xrs){
				global $bgcolor,$yyModify,$yyDelete,$yyLogin,$resultcounter,$clact,$jscript,$qetype,$qesize,$sortorder;
				$jscript.='pa['.$resultcounter.']=[';
			?><tr class="<?php print $bgcolor?>" id="tr<?php print $resultcounter?>"><td class="minicell"><?php
				$qetype='text';
				$qesize='18';
				if($clact=='lol'){
					$qetype='special';
					$jscript.="''";
					print '<select id="sxz'.$resultcounter.'" onchange="chi(this,'.$resultcounter.')">';
					for($index=0;$index<=5;$index++)
						print '<option value="'.$index.'"'.($index==$xrs['clLoginLevel']?' selected':'').'>'.$index.'</option>';
					print '</select>';
				}elseif($clact=='dsc'){
					$jscript.="'".jsspecials($xrs['clPercentDiscount'])."'";
					$qesize='5';
				}elseif($clact=='uid'){
					$qetype='special';
					print $xrs['clID'];
				}elseif($clact=='ste' || $clact=='cte' || $clact=='she' || $clact=='wsp' || $clact=='ped' || $clact=='hae'){
					$fieldnum=1;
					if($clact=='cte') $fieldnum=2;
					if($clact=='she') $fieldnum=4;
					if($clact=='wsp') $fieldnum=8;
					if($clact=='ped') $fieldnum=16;
					if($clact=='hae') $fieldnum=32;
					$jscript.=(($xrs['clActions'] & $fieldnum)!=0?1:0);
					$qetype='checkbox';
				}elseif($clact=='del'){
					$jscript.="'del'";
					$qetype='delbox';
				}else
					$qetype='';
				?></td><td><?php print htmlspecials($xrs['clUserName'])?></td><td><?php print htmlspecials($xrs['clEmail'])?></td>
				<td style="text-align:<?php print ($sortorder=='tsa'||$sortorder=='tsd'?'right':'center')?>"><?php
							if($sortorder=='tsa'||$sortorder=='tsd')
								print FormatEuroCurrency($xrs['totsales']);
							elseif($sortorder=='loa'||$sortorder=='lod')
								print strtotime(trim_($xrs['thedate']))!=FALSE ? date('Y-m-d',strtotime($xrs['thedate'])) : '-';
							else
								print '-';?></td>
				<td><?php	if(($xrs['clActions'] & 1)==1) print 'STE ';
							if(($xrs['clActions'] & 2)==2) print 'CTE ';
							if(($xrs['clActions'] & 4)==4) print 'SHE ';
							if(($xrs['clActions'] & 32)==32) print 'HAE ';
							if(($xrs['clActions'] & 8)==8) print 'WSP ';
							if(($xrs['clActions'] & 16)==16) print 'PED ';
							if(($xrs['clActions'] & 64)==64) print 'SHC ';
				?>&nbsp;</td>
				<td class="minicell" style="white-space:nowrap"><input type="button" value="L" title="<?php print $yyLogin?>" onclick="lrec('<?php print $xrs['clID']?>',event)">
					<input type="button" value="M" title="<?php print $yyModify?>" onclick="mrec('<?php print $xrs['clID']?>',event)">
					<input type="button" value="X" title="<?php print $yyDelete?>" onclick="drec('<?php print $xrs['clID']?>')"></td></tr>
<?php			$jscript.=",'".jsspecials($xrs['clID'])."'];\r\n";
				$resultcounter++;
			}
			function displayheaderrow(){
				global $yyLiName,$yyEmail,$yyPass,$yyLiLev,$yyActns,$yyModify,$yyDelete,$yyLogin,$clact,$yyDscAmt,$yyExStat,$yyExCoun,$yyExShip,$yyExHand,$yyWholPr,$yyPerDis,$sortorder;
?>
			  <tr>
				<th class="small minicell">
					<select name="clact" id="clact" size="1" onchange="changeclact(this)" style="width:150px">
				<option value="none">Quick Entry...</option>
				<option value="lol"<?php if($clact=='lol') print ' selected'?>><?php print $yyLiLev?></option>
				<option value="dsc"<?php if($clact=='dsc') print ' selected'?>><?php print $yyDscAmt?></option>
				<option value="" disabled="disabled">---------------------</option>
				<option value="ste"<?php if($clact=='ste') print ' selected'?>><?php print $yyExStat?></option>
				<option value="cte"<?php if($clact=='cte') print ' selected'?>><?php print $yyExCoun?></option>
				<option value="she"<?php if($clact=='she') print ' selected'?>><?php print $yyExShip?></option>
				<option value="hae"<?php if($clact=='hae') print ' selected'?>><?php print $yyExHand?></option>
				<option value="wsp"<?php if($clact=='wsp') print ' selected'?>><?php print $yyWholPr?></option>
				<option value="ped"<?php if($clact=='ped') print ' selected'?>><?php print $yyPerDis?></option>
				<option value="uid"<?php if($clact=='uid') print ' selected'?>>Unique ID</option>
				<option value="" disabled="disabled">---------------------</option>
				<option value="del"<?php if($clact=='del') print ' selected'?>><?php print $yyDelete?></option>
					</select>
				</th>
				<th class="maincell"><?php print $yyLiName?></th>
				<th class="maincell"><?php print $yyEmail?></th>
				<th class="minicell"><?php
					if($sortorder=='loa'||$sortorder=='lod')
						print 'Date';
					elseif($sortorder=='tsa'||$sortorder=='tsd')
						print 'Sales';
					else
						print '&nbsp;';?></th>
				<th class="minicell"><?php print $yyActns?></th>
				<th class="small" style="width:5%;text-align:center"><?php print $yyModify?></th>
			  </tr>
<?php		}
			$whereand = ' WHERE ';
			$sSQL="SELECT clID,clUserName,clActions,clLoginLevel,clPercentDiscount,clEmail,clPW";
			if($sortorder=='tsa'||$sortorder=='tsd') $sSQL.=",SUM(ordTotal) AS totsales ";
			if($sortorder=='loa'||$sortorder=='lod') $sSQL.=",MAX(ordDate) AS thedate ";
			$sSQL.=" FROM customerlogin ";
			if($sortorder=='tsa'||$sortorder=='tsd'||$sortorder=='loa'||$sortorder=='lod') $sSQL.=" LEFT JOIN orders ON customerlogin.clID=orders.ordClientID";
			if($thelevel !== '' && $thelevel !== NULL){
				$sSQL.=$whereand . " clLoginLevel=" . $thelevel;
				$whereand=' AND ';
			}
			$accdate=getrequest('accdate');
			if($accdate!=''){
				$accdate = parsedate($accdate);
				if(@$_REQUEST['daterange']=='1') // Till
					$sSQL.=$whereand . "clDateCreated <= '" . date("Y-m-d", $accdate) . "' ";
				elseif(@$_REQUEST['daterange']=='2') // On
					$sSQL.=$whereand . "clDateCreated BETWEEN '"  . date("Y-m-d", $accdate) . "' AND '" . date("Y-m-d", $accdate+(60*60*24)) . "' ";
				else // Since
					$sSQL.=$whereand . "clDateCreated >= '" . date("Y-m-d", $accdate) . "' ";
				$whereand=' AND ';
			}
			if(getrequest('stext')!=''){
				$stext=getrequest('stext');
				$stype=getrequest('stype');
				$Xstext = escape_string($stext);
				$aText = explode(' ',$Xstext);
				$aFields[0]='clUserName';
				$aFields[1]='clPw';
				$aFields[2]='clEmail';
				if($stype=='exact'){
					$sSQL.=$whereand . "(clUserName LIKE '%" . $Xstext . "%' OR clPw LIKE '%" . $Xstext . "%' OR clEmail LIKE '%" . $Xstext . "%') ";
					$whereand=' AND ';
				}else{
					$sJoin='AND ';
					if($stype=='any') $sJoin='OR ';
					$sSQL.=$whereand . '(';
					$whereand=' AND ';
					for($index=0;$index<=2;$index++){
						$sSQL.='(';
						$rowcounter=0;
						$arrelms=count($aText);
						foreach($aText as $theopt){
							if(is_array($theopt))$theopt=$theopt[0];
							$sSQL.=$aFields[$index] . " LIKE '%" . $theopt . "%' ";
							if(++$rowcounter < $arrelms) $sSQL.=$sJoin;
						}
						$sSQL.=') ';
						if($index < 2) $sSQL.='OR ';
					}
					$sSQL.=') ';
				}
			}
			if($sortorder=='ema')
				$sSQL.=' ORDER BY clEmail';
			elseif($sortorder=='emd')
				$sSQL.=' ORDER BY clEmail DESC';
			elseif($sortorder=='aca')
				$sSQL.=' ORDER BY clActions';
			elseif($sortorder=='acd')
				$sSQL.=' ORDER BY clActions DESC';
			elseif($sortorder=='tsa')
				$sSQL.=' ORDER BY SUM(ordTotal)';
			elseif($sortorder=='tsd')
				$sSQL.=' ORDER BY SUM(ordTotal) DESC';
			elseif($sortorder=='loa')
				$sSQL.=' ORDER BY MAX(ordDate)';
			elseif($sortorder=='lod')
				$sSQL.=' ORDER BY MAX(ordDate) DESC';
			elseif($sortorder=='nad')
				$sSQL.=' ORDER BY clUserName DESC';
			else
				$sSQL.=' ORDER BY clUserName';
			if(! @is_numeric(getget('pg')))
				$CurPage = 1;
			else
				$CurPage = (int)getget('pg');
			// $tmpSQL = "SELECT COUNT(DISTINCT products.pId) AS bar" . $sSQL;
			$tmpSQL=str_replace('clID,clUserName,clActions,clLoginLevel,clPercentDiscount,clEmail,clPW', 'COUNT(*) AS bar', $sSQL);
			$tmpSQL=str_replace(',SUM(ordTotal) AS totsales','',$tmpSQL);
			$tmpSQL=str_replace(',MAX(ordDate) AS thedate','',$tmpSQL);
			if($sortorder=='tsa'||$sortorder=='tsd'||$sortorder=='loa'||$sortorder=='lod') $sSQL=str_replace('ORDER BY',' GROUP BY clID,clUserName,clActions,clLoginLevel,clPercentDiscount,clEmail,clPW ORDER BY',$sSQL);
			$allprods=ect_query($tmpSQL) or ect_error();
			$rs=ect_fetch_assoc($allprods);
			$iNumOfPages = ceil($rs['bar']/$adminclientloginperpage);
			ect_free_result($allprods);
			$sSQL.=' LIMIT ' . ($adminclientloginperpage*($CurPage-1)) . ', ' . $adminclientloginperpage;
			$result=ect_query($sSQL) or ect_error();
			$haveerrprods=FALSE;
			if(ect_num_rows($result) > 0){
				$pblink = '<a href="adminclientlog.php?rid=' . @$_REQUEST['rid'] . '&stext=' . urlencode(@$_REQUEST['stext']) . '&stype=' . @$_REQUEST['stype'] . '&slevel=' . @$_REQUEST['slevel'] . '&accdate=' . @$_REQUEST['accdate'] . '&perpage=' . urlencode(getrequest('perpage')) . '&sort=' . urlencode(getrequest('sort')) . '&daterange=' . urlencode(@$_REQUEST['daterange']) . '&pg=';
				if($iNumOfPages > 1) print '<tr><td colspan="6" align="center">' . writepagebar($CurPage,$iNumOfPages,$yyPrev,$yyNext,$pblink,FALSE) . '</td></tr>';
				displayheaderrow();
				while($rs=ect_fetch_assoc($result)){
					if(@$bgcolor=='altdark') $bgcolor='altlight'; else $bgcolor='altdark';
					displayprodrow($rs);
				}
				if($haveerrprods) print '<tr><td width="100%" colspan="6"><br />' . $redasterix . $yySeePr . '</td></tr>';
				if($iNumOfPages > 1) print '<tr><td colspan="6" align="center">' . writepagebar($CurPage,$iNumOfPages,$yyPrev,$yyNext,$pblink,FALSE) . '</td></tr>';
			}else{
				print '<tr><td width="100%" colspan="6" align="center"><br />' . $yyItNone . '<br />&nbsp;</td></tr>';
			}
			ect_free_result($result);
		}else{
			$numitems=0;
			$sSQL='SELECT COUNT(*) as totcount FROM customerlogin';
			$result=ect_query($sSQL) or ect_error();
			if($rs=ect_fetch_assoc($result))
			$numitems=$rs['totcount'];
			print '<tr><td colspan="7"><div class="itemsdefine">You have ' . $numitems . ' customer accounts defined.</div></td></tr>';
		}
		if($resultcounter>0){ ?>
			  <tr>
				<td align="center" style="white-space:nowrap"><?php if($clact!='' && $clact!="none") print '<input type="hidden" name="resultcounter" id="resultcounter" value="'.$resultcounter.'" /><input type="button" value="' . $yyUpdate . '" onclick="quickupdate()" /> <input type="reset" value="' . $yyReset . '" />'; else print '&nbsp;'?></td>
                <td width="100%" colspan="6" align="center"><br /><ul><li><?php print $yyCLTyp?></li></ul></td>
			  </tr>
<?php	} ?>
            </table>
			<div class="adminhome"><a href="admin.php"><?php print $yyAdmHom?></a></div>
		  </td>
        </tr>
      </table>
	</form>
<script>
/* <![CDATA[ */
var pa=[];
<?php print $jscript; ?>
function patch_pid(pid){
	document.getElementById('pid'+pid).name='pid'+pid;
	document.getElementById('pid'+pid).value=pa[pid][1];
	return pid;
}
for(var pidind in pa){
	var ttr=document.getElementById('tr'+pidind);
	var stockcell=<?php print $useStockManagement?1:0 ?>;
	ttr.cells[1].innerHTML+='<input type="hidden" id="pid'+pidind+'" value="" />';
<?php		if($qetype=='text'){ ?>
	ttr.cells[0].innerHTML=pa[pidind][0]===false?'-':'<input type="text" id="chkbx'+pidind+'" size="<?php print $qesize?>" onchange="this.name=\'pra_'+patch_pid(pidind)+'\'" value="'+pa[pidind][0].replace('"','&quot;')+'" tabindex="'+(pidind+1)+'" />';
<?php		}elseif($qetype=="delbox"){ ?>
	ttr.cells[0].innerHTML='<input type="checkbox" id="chkbx'+pidind+'" onchange="this.name=\'pra_'+patch_pid(pidind)+'\'" value="del" tabindex="'+(pidind+1)+'" />';
<?php		}elseif($qetype=="checkbox"){ ?>
	ttr.cells[0].innerHTML='<input type="hidden" id="pra_'+pa[pidind][1]+'" value="1" /><input type="checkbox" id="chkbx'+pidind+'" onchange="this.name=\'prb_'+patch_pid(pidind)+'\';document.getElementById(\'pra_'+pa[pidind][1]+'\').name=\'pra_'+patch_pid(pidind)+'\'" value="1" '+(pa[pidind][0]==1?'checked="checked" ':'')+'tabindex="'+(pidind+1)+'" />';
<?php		}elseif($qetype=="image"){ ?>
	ttr.cells[0].innerHTML=(pa[pidind][0]==''?'-':'<img class="lazyload" id="lazyimg'+pidind+'" src="adminimages/imageload.png" data-src="'+pa[pidind][0]+'" style="max-width:80px;cursor:pointer" alt="" onclick="mr(\''+pa[pidind][1]+'\')" />');
<?php		} ?>
}
/* ]]> */
</script>
<?php
}
?>