<!--#include file="../../owFunctions/I_GeneralFunctions.asp"-->
<!--#include file="../header.asp"-->
<%
	'************************************************************************************
	'*																					*
	'*	MODIFICATION TO THIS SCRIPT OR ANY FILES INCLUDED WITH 							*
	'*		ONEWORLDSTORE IS DONE AT YOUR OWN RISK!										*
	'*	Product  : OneWorldStore														*
	'*	Version  : 9.0000																*
	'*	Modified : June 2004															*
	'*	Copyright:	Copyright (C) 2004 OneWorldStore.Com								*
	'*				Registration Number: 1024507										*
	'*				http://strategis.ic.gc.ca/											*
	'*																					*
	'************************************************************************************

	Dim sAction, sCheckbox, sTemp
	Dim sSearchText, sSearch, sSQLCount, bResetTtlNumItems, bResetDBLoc
	Dim iBeginPagination, sURL, iTtlNumItems, iDBLoc, sQuerystring
	Dim sTarget, iPaginationCount, sSort
	
	sAction			= Trim(Request("sAction"))
	sSort			= Trim(Request("sSort"))
	sSearchText		= Trim(Request("sSearchText"))
	sSearch			= Trim(Request("sSearch"))
	sGroupUnq		= Trim(Request("sGroupUnq"))
	bResetDBLoc		= False
	sURL			= "owListCustomers.asp"
	sQuerystring	= "sGroupUnq=" & sGroupUnq & "&sAction=" & sAction & "&sSearchText=" & sSearchText & "&sSearch=" & sSearch & "&sSort=" & sSort
	sTarget			= "owMain"
	sMsg			= Trim(Request("sMsg"))
	
	If ( sMsg = "1" ) Then
		sSuccess = LANG_DT( "CUSTOMER_ADDOK", "** Customer record added. **" )
	End If
	
	If ( sGroupUnq = "" ) Or ( NOT IsNumeric(sGroupUnq) ) Then
		sGroupUnq = C_GROUP_RETAIL
	Else
		sGroupUnq = CInt(sGroupUnq)
	End If

	If ( sAction = "Delete" ) Then
		For Each sCheckbox In Request.Form
			If ( InStr(sCheckbox, "sDelete") > 0 ) Then
				sTemp = Replace(sCheckbox, "sDelete", "")
				sSuccess = DeleteCustomer(sTemp)
			End If
		Next
		bResetDBLoc = True
	End If
	
	sTempSearch = " AND GM.GroupUnq = " & sGroupUnq
	
	If ( sSearchText <> "" ) Then
		If ( sSearch = "FirstChar" ) Then
			sTempSearch = sTempSearch & " AND AD.MapName = 'OW_CUSTLNAME' AND AD.VarCharData LIKE '" & sSearchText & "%'"
		Else
			Select Case sSearch
				Case "LName"
					sTempSearch = sTempSearch & " AND AD.MapName = 'OW_CUSTLNAME' AND AD.VarCharData LIKE '%" & sSearchText & "%'"
				Case "FName"
					sTempSearch = sTempSearch & " AND AD.MapName = 'OW_CUSTFNAME' AND AD.VarCharData LIKE '%" & sSearchText & "%'"
				Case "Company"
					sTempSearch = sTempSearch & " AND AD.MapName = 'OW_CUSTCOMPANY' AND AD.VarCharData LIKE '%" & sSearchText & "%'"
				Case "Phone"
					sTempSearch = sTempSearch & " AND AD.MapName = 'OW_CUSTPHONE' AND AD.VarCharData LIKE '%" & sSearchText & "%'"
				Case "Email"
					sTempSearch = sTempSearch & " AND AD.MapName = 'OW_CUSTEMAIL' AND AD.VarCharData LIKE '%" & sSearchText & "%'"
				Case "Active"
					sTempSearch = sTempSearch & " AND ( A.RemoveDate IS NULL OR A.AddDate >= A.RemoveDate )"
			End Select
		End If
	
		Select Case sSort
			Case "LName"
				sSortText = "(SELECT AD2.VarCharData FROM owAccountData AD2 WHERE AD2.AccountUnq = A.AccountUnq AND AD2.MapName = 'OW_CUSTLNAME')"
			Case "FName"
				sSortText = "(SELECT AD2.VarCharData FROM owAccountData AD2 WHERE AD2.AccountUnq = A.AccountUnq AND AD2.MapName = 'OW_CUSTFNAME')"
			Case "Company"
				sSortText = "(SELECT AD2.VarCharData FROM owAccountData AD2 WHERE AD2.AccountUnq = A.AccountUnq AND AD2.MapName = 'OW_CUSTCOMPANY')"
			Case "Phone"
				sSortText = "(SELECT AD2.VarCharData FROM owAccountData AD2 WHERE AD2.AccountUnq = A.AccountUnq AND AD2.MapName = 'OW_CUSTPHONE')"
			Case "Email"
				sSortText = "(SELECT AD2.VarCharData FROM owAccountData AD2 WHERE AD2.AccountUnq = A.AccountUnq AND AD2.MapName = 'OW_CUSTEMAIL')"
			Case "Active"
				sSortText = "(SELECT (AddDate>RemoveDate OR RemoveDate IS NULL) FROM owAccounts A2 WHERE A2.AccountUnq = A.AccountUnq"
			Case Else
				sSortText = "(SELECT AD2.VarCharData FROM owAccountData AD2 WHERE AD2.AccountUnq = A.AccountUnq AND AD2.MapName = 'OW_CUSTLNAME')"
		End Select

		sSQLText	= "SELECT A.AccountUnq, A.AddDate, A.RemoveDate, A.ExpireDate, " & sSortText & " FROM owAccounts A, owAGroupMap GM, owAccountData AD WHERE AD.AccountUnq = A.AccountUnq AND GM.AccountUnq = A.AccountUnq" & sTempSearch & " ORDER BY 5"
		sSQLCount	= "SELECT Count(*) FROM owAccounts A, owAGroupMap GM, owAccountData AD WHERE AD.AccountUnq = A.AccountUnq AND GM.AccountUnq = A.AccountUnq" & sTempSearch
	Else
		sSQLText	= "SELECT A.AccountUnq, A.AddDate, A.RemoveDate, A.ExpireDate FROM owAccounts A, owAGroupMap GM WHERE GM.AccountUnq = A.AccountUnq" & sTempSearch
		sSQLCount	= "SELECT Count(*) FROM owAccounts A, owAGroupMap GM WHERE GM.AccountUnq = A.AccountUnq" & sTempSearch
	End If

	
	sQueryString = "sSearchText=" & Server.URLEncode(sSearchText) & "&sSearch=" & Server.URLEncode(Trim(sSearch)) & "&sSort="

	iBeginPagination = SetupPagination( sSQLCount, bResetTtlNumItems, bResetDBLoc )
	Call PaginationQuery( sSQLText, rsStoreData, iBeginPagination )
	
	%>
	<script language = "javascript">
		parent.owBodyFooter.location="owBodyFooter.asp?<%=sQuerystring%><%=sSort%>&iOWNumPerPage=<%=iOWNumPerPage%>&iTtlNumItems=<%=iTtlNumItems%>&iDBLoc=<%=iDBLoc%>"
	</script>
	<%
	
	If ( sSuccess <> "" ) Or ( rsStoreData.EOF ) Then
		If ( sSuccess <> "" ) Then
			%>
			<script language = "javascript">
				parent.owBodyTopNav.location="owBodyTopNav.asp?sGroupUnq=<%=sGroupUnq%>&sMessage=<%=Server.URLEncode(sSuccess)%>"
			</script>
			<%
		End If
		If ( rsStoreData.EOF ) Then
			%>
			<script language = "javascript">
				parent.owBodyTopNav.location="owBodyTopNav.asp?sGroupUnq=<%=sGroupUnq%>&sMessage=<%=Server.URLEncode(LANG_DT( "CUSTOMER_NONEFOUND", "** No customers found **" ))%>"
			</script>
			<%
		End If
	Else
		' just refresh the top bar to remove any "Save" buttons or old messages
		%>
		<script language = "javascript">
			parent.owBodyTopNav.location="owBodyTopNav.asp?sGroupUnq=<%=sGroupUnq%>&sMessage="
		</script>
		<%
	End If
	
	%>
	
	<script language = "javascript">
		
		function ColumnOn(sStyle)
		{
			sStyle.background	= "#F5C058";
		}
		function ColumnOff(sStyle)
		{
			sStyle.background	= "#C3DAF9";
		}
		
		function ColumnDataOn(sStyle)
		{
			sStyle.background	= "#EAF1F9";
		}
		function ColumnDataOff(sStyle)
		{
			sStyle.background	= "#FFFFFF";
		}
		
	</script>
	<body topmargin="0" leftmargin="0" bgcolor=C3DAF9>
	<div id="tablediv" style="position: absolute; top: 0px; height: 100%; width:100%">
		<table width=100% border="0" cellpadding="2" cellspacing="0" style="font-size: 11px" bgcolor=FFFFFF>
		<form action='owListCustomers.asp' method='post' name='CustomerList'>
			<input type='hidden' name='sAction' value=''>
			<input type='hidden' name='sSort' value=''>
			<tr>
				<td class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_SELECT", "Select" )%></td>
				<td onclick="location.href='owListCustomers.asp?<%=sQueryString%>LName'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_LNAME", "Last Name" )%></td>
				<td onclick="location.href='owListCustomers.asp?<%=sQueryString%>FName'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_FNAME", "First Name" )%></td>
				<td onclick="location.href='owListCustomers.asp?<%=sQueryString%>Company'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_COMPANY", "Company" )%></td>
				<td onclick="location.href='owListCustomers.asp?<%=sQueryString%>Phone'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_PHONE", "Phone" )%></td>
				<td onclick="location.href='owListCustomers.asp?<%=sQueryString%>Email'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_EMAIL", "Email" )%></td>
				<td onclick="location.href='owListCustomers.asp?<%=sQueryString%>Active'" onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_ACTIVEQUEST", "Active?" )%></td>
				<%If ( sGroupUnq = C_GROUP_AFFILIATE ) Then%>
				<td onMouseOver="ColumnOn(this.style);" onMouseOut="ColumnOff(this.style);" class="ColumnHead" align="center"><%=LANG_DT( "CUSTOMER_AFFILIATEORDERS", "Affiliate Orders" )%></td>
				<%End If%>
			</tr>
			<%
			If ( NOT rsStoreData.EOF ) Then
				iPaginationCount = 0
				While ( ( NOT rsStoreData.EOF ) And ( iPaginationCount < iOWNumPerPage ) )
					sAccountUnq 	= rsStoreData("AccountUnq")
					sQueryString2	= sQueryString & sSort & "&sAccountUnq=" & sAccountUnq & "&sGroupUnq=" & sGroupUnq
					sEmail			= ACCNT_ReturnADV("OW_CUSTEMAIL", "V", sAccountUnq, C_OVERRIDE_ADV)
					dAddDate		= rsStoreData("AddDate")
					dRemoveDate		= rsStoreData("RemoveDate")
					dExpireDate		= rsStoreData("ExpireDate")
					%>
					<tr onMouseOver="ColumnDataOn(this.style);" onMouseOut="ColumnDataOff(this.style);">
						<td width=5% class='ColumnData' align="center"><input size="1" type="checkbox" style="font-size: 6px;" name="sDelete<%=sAccountUnq%>" value="46"></td>
						<td width=20% onclick="location.href='owModifyCustomer.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=ACCNT_ReturnADV("OW_CUSTLNAME", "V", sAccountUnq, C_OVERRIDE_ADV)%>&nbsp;</td>
						<td width=15% onclick="location.href='owModifyCustomer.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=ACCNT_ReturnADV("OW_CUSTFNAME", "V", sAccountUnq, C_OVERRIDE_ADV)%>&nbsp;</td>
						<td width=20% onclick="location.href='owModifyCustomer.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=ACCNT_ReturnADV("OW_CUSTCOMPANY", "V", sAccountUnq, C_OVERRIDE_ADV)%>&nbsp;</td>
						<td width=15% onclick="location.href='owModifyCustomer.asp?<%=sQueryString2%>'" class='ColumnData' align="left"><%=ACCNT_ReturnADV("OW_CUSTPHONE", "V", sAccountUnq, C_OVERRIDE_ADV)%>&nbsp;</td>
						<td width=20% class='ColumnData' align="left"><a href="mailto:<%=sEmail%>" style='font-weight: normal; font-size: 12px;'><%=sEmail%></a>&nbsp;</td>
						<td width=5% onclick="location.href='owModifyCustomer.asp?<%=sQueryString2%>'" class='ColumnData' align="center">
							<%
							If ( IsNull(dRemoveDate) ) Then
								Response.Write LANG_DT( "YES", "Yes" )
							Else
								If ( DateDiff("s", dAddDate, dRemoveDate) > 0 ) Then
									Response.Write LANG_DT( "NO", "No" )
								Else
									Response.Write LANG_DT( "YES", "Yes" )
								End If
							End If
							%>
						</td>
						<%If ( sGroupUnq = C_GROUP_AFFILIATE ) Then%>
						<td width=5% onclick="location.href='AffiliateOrders/Default.asp?<%=sQueryString2%>'" class='ColumnData' align="center"><%=LANG_DT( "CUSTOMER_VIEWAFFILIATE", "View" )%>&gt;</td>
						<%End If%>
					</tr>
					<%
					rsStoreData.movenext
					iPaginationCount = iPaginationCount + 1
				Wend
			End If
			Call DB_closeRecordset(rsStoreData)
			%>

		</form>
	</table>
	<!--#include file="../footer.asp"-->  

	<%
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function DeleteCustomer(sAccountUnq)

		sSQLText = "SELECT IdOrder FROM orders WHERE AccountUnq = " & sAccountUnq
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		While ( NOT rsStoreData.EOF )
			sIDOrder = rsStoreData("IdOrder")

			Call DB_updateDatabase("DELETE FROM OrdersProdOptions WHERE idOrder = " & sIDOrder)
			Call DB_updateDatabase("DELETE FROM OrdersProdRef WHERE idOrder = " & sIDOrder)
			call DB_updateDatabase("DELETE FROM OrdersCreditCards WHERE idOrder=" & sIDOrder)
			call DB_updateDatabase("DELETE FROM orders WHERE idOrder = " & sIDOrder)
			
			rsStoreData.movenext 
		Wend
		Call DB_closeRecordset(rsStoreData)
		
		Call DB_updateDatabase("DELETE FROM CustAltBill WHERE AccountUnq = " & sAccountUnq)
		Call DB_updateDatabase("DELETE FROM CustAltShip WHERE AccountUnq = " & sAccountUnq)
		Call DB_updateDatabase("DELETE FROM owAGroupMap WHERE AccountUnq = " & sAccountUnq)
		Call DB_updateDatabase("DELETE FROM owAccountData WHERE AccountUnq = " & sAccountUnq)
		
		Session.Contents.Remove("GROUP_" & C_GROUP_ADMIN & "_" & sAccountUnq)
		Session.Contents.Remove("GROUP_" & C_GROUP_RETAIL & "_" & sAccountUnq)
		Session.Contents.Remove("GROUP_" & C_GROUP_WHOLESALE & "_" & sAccountUnq)
		Session.Contents.Remove("GROUP_" & C_GROUP_DISTRIBUTOR & "_" & sAccountUnq)
		
		Call DB_updateDatabase("UPDATE owAccounts SET RemoveDate = #" & CONVERSION_FormatDate(Now(), "S") & "# WHERE AccountUnq = " & sAccountUnq)
		
		DeleteCustomer = LANG_DT( "CUSTOMER_DELOK", "** Customer(s) successfully deleted **" )
		
	End Function
	'************************************************************************************
%>