<?php
	Require("Includes/i_Includes.php");
	Require("Templates/" . $sTemplates . "/Nav/PHP_JK_GALLERIES_OPEN.php");
	
	$bAnyChildWGallery	= False;
	$bAdmin				= False;
	$bCanCreate			= False;
	$sBreadCrumb		= "";
	$iParentUnq			= "";

	Echo "<center>";
	DispCategories();
	Echo "<BR>";
	Require("Templates/" . $sTemplates . "/Nav/PHP_JK_GALLERIES_CLOSE.php");
	
	
	//************************************************************************************
	//*																					*
	//*																					*
	//*																					*
	//************************************************************************************
	Function DispCategories()
	{
		Global $bAnyChildWGallery;
		Global $iLoginAccountUnq;
		Global $PUBLIC_GALLERIES;
		Global $bAdmin;
		Global $bCanCreate;
		Global $aVariables;
		Global $aValues;
		Global $sBreadCrumb;
		Global $iParentUnq;
		Global $iTableWidth;
		Global $iColorScheme;
		Global $iLevel;
		Global $iNumCatsToDisp;
		
		$iParentUnq = Trim(Request("iParentUnq"));
		If ( $iParentUnq == "" )
			$iParentUnq = "0";

		G_STRUCTURE_CatBreadcrumbs($sBreadCrumb, $iParentUnq, -1, -1);
		
		$iNumColumns 	= DOMAIN_Conf("IMAGEGALLERY_CAT_COLUMNS");
		$bAdmin			= ACCNT_ReturnRights("PHPJK_IG_ADMIN_ALL");
		$bCanCreate		= ACCNT_ReturnRights("PHPJK_IG_CREATE_GALLERY");
		$iNumCatsToDisp	= strtoupper(Trim(DOMAIN_Conf("IMAGEGALLERY_NUM_CAT_LIST_ON_CATEGORY_DISPLAY")));
		$iNumGalsToDisp	= strtoupper(Trim(DOMAIN_Conf("IMAGEGALLERY_NUM_GAL_LIST_ON_CATEGORY_DISPLAY")));
		$sLines			= strtoupper(Trim(DOMAIN_Conf("IMAGEGALLERY_DISP_LINES_ON_CATEGORY_DISPLAY")));
		$sCatImageLoc	= strtoupper(Trim(DOMAIN_Conf("IMAGEGALLERY_DISP_IMAGE_ON_CATEGORY_DISPLAY")));
		$iWidth			= DOMAIN_Conf("IMAGEGALLERY_CAT_IMAGE_WIDTH");
		$iHeight		= DOMAIN_Conf("IMAGEGALLERY_CAT_IMAGE_HEIGHT");
		
		If ( $iNumCatsToDisp == "" ) {
			$iNumCatsToDisp = 4;
		}Else{
			$iNumCatsToDisp = $iNumCatsToDisp;
		}
		If ( $iNumGalsToDisp == "" ) {
			$iNumGalsToDisp = 4;
		}Else{
			$iNumGalsToDisp = $iNumGalsToDisp;
		}
		
		$bAnyChildWGallery = False;
		AnyChildWGallery(0);
		If ( ( $bAnyChildWGallery ) && ( $iParentUnq == "0" ) ) {
			$aCategories[0][0]	= "Galleries not in a category";
			$aCategories[1][0]	= 0;
			$aCategories[2][0]	= "N";
			$aCategories[3][0]	= 0;
			$iNumCategories		= 1;
		}Else{
			$iNumCategories		= 0;
		}

		$sQuery			= "SELECT * FROM IGCategories (NOLOCK) WHERE (Parent = " . $iParentUnq . ") ORDER BY Position";
		$rsRecordSet	= DB_Query($sQuery);
		While ( $rsRow = DB_Fetch($rsRecordSet) )
		{
			$bAnyChildWGallery = False;
			AnyChildWGallery($rsRow["CategoryUnq"]);
			If ( $bAnyChildWGallery ) {
				$aCategories[0][$iNumCategories] = $rsRow["Name"];
				$aCategories[1][$iNumCategories] = $rsRow["CategoryUnq"];
				$aCategories[2][$iNumCategories] = strtoupper(Trim($rsRow["HasImage"]));
				$aCategories[3][$iNumCategories] = $rsRow["NumChildren"];
				$iNumCategories++;
			}
		}

		// if iNumCategories = 0, but there is a iParentUnq, then check to see if this category has any galleries in it. if it does, redirect to g_display
		If ( $iNumCategories <= 0 ) {
			If ( $bAdmin ) {
				$sQuery = "SELECT GalleryUnq, AccountUnq, Name FROM Galleries (NOLOCK) WHERE CategoryUnq = " . $iParentUnq . " ORDER BY Position";
			}Else{
				// they are not an admin so only display public galleries, or galleries they own
				$sQuery = "SELECT G.GalleryUnq, G.AccountUnq, G.Name FROM Galleries G (NOLOCK) LEFT OUTER JOIN PrivateAccounts P (NOLOCK) ON G.GalleryUnq = P.GalleryUnq WHERE (G.Visibility = " . $PUBLIC_GALLERIES . " OR (G.AccountUnq = " . $iLoginAccountUnq . ")) AND G.CategoryUnq = " . $iParentUnq . " AND P.AccountUnq = -1";
			}
			DB_Query("SET ROWCOUNT 1");
			$rsRecordSet = DB_Query($sQuery);
			DB_Query("SET ROWCOUNT 0");
			If ( $rsRow = DB_Fetch($rsRecordSet) ){
				header( 'location:/PHPJK/G_Display.php?iCategoryUnq=' . $iParentUnq );
				ob_flush();
				exit;
			}
		}

		?>
		<table cellpadding=5 width=<?=$iTableWidth?> cellspacing = 0 border = 0 class='TablePage'><tr><td><?php G_LINK_Category_Gallery_Map(); ?></td><td align=right><?php G_LINK_New_Image_Search()?></td></tr></table>
		<?php G_STRUCTURE_HeaderBar_ReallySpecific("CategoriesHead.gif", "", "", "", "Galleries"); ?>
		<table cellpadding = 5 cellspacing=0 border=0 width=<?=$iTableWidth?> class='TablePage_Boxed'>
		<?php If ( $sBreadCrumb != "" ) {?>
			<tr><td colspan=<?=$iNumColumns?> bgcolor = <?=$GLOBALS["PageBGColor"]?>><a href = 'index.php?<?php DOMAIN_Link("G");?>' class='MediumNavPage'>Galleries</a> <img src = '<?=G_STRUCTURE_DI("Arrow.gif", $GLOBALS["COLORBASED"])?>'> <?=$sBreadCrumb?></td></tr>
			<tr><td colspan=<?=$iNumColumns?> bgcolor = <?=$GLOBALS["PageBGColor"]?>><table cellpadding=0 cellspacing=0 border=0 width=100%><tr><td bgcolor = <?=$GLOBALS["BorderColor1"]?>><img src='Images/Blank.gif' width=10 height=1></td></tr></table></td></tr>
		<?php }
		For ( $x = 0; $x < $iNumCategories; $x++)
		{
			Echo "<tr>\n";
			For ( $y = 1; $y <= $iNumColumns; $y++)
			{
				If ( $y > 1 )
					$x++;
				If ( $x < $iNumCategories ) {
					$sFilePath = DOMAIN_Conf("IMAGEGALLERY_CAT_IMAGE_LOC") . "/" . "CatImage_" . $aCategories[1][$x] . ".jpg";
					$sFilePath = str_replace("\\", "/", $sFilePath);
					$sFilePath = str_replace("//", "/", $sFilePath);
					
					$iCount = 0;
					If ( $bAdmin ) {
						$sQuery = "SELECT GalleryUnq, AccountUnq, Name FROM Galleries (NOLOCK) WHERE CategoryUnq = " . $aCategories[1][$x] . " ORDER BY Position";
					}Else{
						// they are not an admin so only display public galleries, or galleries they own
						$sQuery = "SELECT G.GalleryUnq, G.AccountUnq, G.Name FROM Galleries G (NOLOCK) LEFT OUTER JOIN PrivateAccounts P (NOLOCK) ON G.GalleryUnq = P.GalleryUnq WHERE (G.Visibility = " . $PUBLIC_GALLERIES . " OR (G.AccountUnq = " . $iLoginAccountUnq . ")) AND G.CategoryUnq = " . $aCategories[1][$x] . " AND P.AccountUnq = -1 ORDER BY G.Position";
					}
					$rsRecordSet = DB_Query($sQuery);
					If ( DB_NumRows($rsRecordSet) > 0 )
					{
						?>
						<td align=center valign=top>
							<table cellpadding = 0 cellspacing=0 border=0 width=100% class='TablePage'>
								<tr>
									<td colspan=2>
										<?php If ( $sCatImageLoc == "TOP" ) {?>
										<?php If ( $aCategories[2][$x] == "Y" ) {?>
											<table cellpadding=0 cellspacing=0 border=0 width=<?=$iWidth?> class='Table1_Boxed'><tr><td><a href='G_Display.php?iCategoryUnq=<?=$aCategories[1][$x]?>'><img src='<?=$sFilePath?>' alt = '<?=$aCategories[0][$x]?>' width=<?=$iWidth?> height=<?=$iHeight?> border=0></a></td></tr></table>
										<?php }?>
										<?php }?>
									</td>
								</tr>
								<tr>
									<td valign=top colspan=2>
										<?php 
										If ( $aCategories[3][$x] > 0 ) {
											G_STRUCTURE_Category_Lines($aCategories[0][$x], $aCategories[0][$x], "index.php?iParentUnq=" . $aCategories[1][$x], "Galleries", $sLines);
										}Else{
											G_STRUCTURE_Category_Lines($aCategories[0][$x], $aCategories[0][$x], "G_Display.php?iCategoryUnq=" . $aCategories[1][$x], "Galleries", $sLines);
										}
										?>
									</td>
								</tr>
								<tr><td colspan=2><img src='Images/Blank.gif' width=3 height=3></td></tr>
								<tr>
									<td valign=top width=100%>
										<?php
										$iX = DB_NumRows($rsRecordSet); 
										if ( $iX > 0 )
										{
											while ( ( $rsRow = DB_Fetch($rsRecordSet) ) && ( $iCount < $iNumGalsToDisp ) )
											{
												$iGalleryUnq = $rsRow["GalleryUnq"];
												$iAccountUnq = $rsRow["AccountUnq"];
												$sQuery = "SELECT Count(*) FROM ImagesInGallery (NOLOCK) WHERE GalleryUnq = " . $iGalleryUnq;
												$rsRecordSet2 = DB_Query($sQuery);
												if ( $rsRow2 = DB_Fetch($rsRecordSet2) ) {
													$iNumImages = $rsRow2[0];
													$sTemp = " <i>(" . $rsRow2[0] . " images)</i>";
												}Else{
													$iNumImages = 0;
													$sTemp = " <i>(no images)</i>";
												}
												If ( $iNumImages > 0 ) {
													If ( $bAdmin ) {
														Echo "<img src='Images/Blank.gif' width=25 height=2 border=0>&#149;<a href='ThumbnailView.php?iGalleryUnq=" . $iGalleryUnq . "' class='SmallNavPage'>" . $rsRow["Name"] . "</a>";
														$iCount++;
													}Else{
														If ( G_ADMINISTRATION_AccessLocked($iGalleryUnq, $iAccountUnq) ) {
															Echo "<img src='Images/Blank.gif' width=25 height=2 border=0>&#149;<a href='ThumbnailView.php?iGalleryUnq=" . $iGalleryUnq . "' class='SmallNavPage'>" . $rsRow["Name"] . "</a>";
															$iCount++;
														}
													}
													Echo $sTemp . "<br>";
												}
											}
											If ( $iCount > $iNumGalsToDisp-1 )
											{
												If ( ( $iX == 1 ) && ( $iNumGalsToDisp > 0 ) )
												{
													$iGalleryUnq = $rsRow["GalleryUnq"];
													$iAccountUnq = $rsRow["AccountUnq"];
													$sQuery = "SELECT Count(*) FROM ImagesInGallery (NOLOCK) WHERE GalleryUnq = " . $iGalleryUnq;
													$rsRecordSet2 = DB_Query($sQuery);
													if ( $rsRow2 = DB_Fetch($rsRecordSet2) ) {
														$iNumImages = $rsRow2[0];
														$sTemp = " <i>(" . $rsRow2[0] . " image)</i>";
													}Else{
														$iNumImages = 0;
													}
													If ( $iNumImages > 0 ) {
														If ( $bAdmin ) {
															Echo "<img src='Images/Blank.gif' width=25 height=2 border=0>&#149;<a href='ThumbnailView.php?iGalleryUnq=" . $iGalleryUnq . "' class='SmallNavPage'>" . $rsRow["Name"] . "</a>";
															$iCount++;
														}Else{
															If ( G_ADMINISTRATION_AccessLocked($iGalleryUnq, $iAccountUnq) ) {
																Echo "<img src='Images/Blank.gif' width=25 height=2 border=0>&#149;<a href='ThumbnailView.php?iGalleryUnq=" . $iGalleryUnq . "' class='SmallNavPage'>" . $rsRow["Name"] . "</a>";
																$iCount++;
															}
														}
														Echo $sTemp . "<br>";
													}
												}ElseIf ( ( $iX > 1 ) && ( $iNumGalsToDisp > 0 ) ){
													Echo "<img src='Images/Blank.gif' width=25 height=2 border=0>&#149;<a href='G_Display.php?iCategoryUnq=" . $aCategories[1][$x] . "' class='SmallNavPage'><b>Full Gallery List...</b></a><br>";
												}
											}
										}Else{
											If ( $aCategories[3][$x] <= 0 )
												Echo "<img src='Images/Blank.gif' width=25 height=2 border=0>&#149;No galleries in this category.<br>";
										}
										?>
									</td>
									<td valign=top align=right>
										<?php If ( $sCatImageLoc == "RIGHT" ) {?>
										<?php If ( $aCategories[2][$x] == "Y" ) {?>
											<table cellpadding=0 cellspacing=0 border=0 width=<?=$iWidth?> class='Table1_Boxed'><tr><td bgcolor='<?=$GLOBALS["BorderColor1"]?>'><a href='G_Display.php?iCategoryUnq=<?=$aCategories[1][$x]?>'><img src='<?=$sFilePath?>' alt = '<?=$aCategories[0][$x]?>' width=<?=$iWidth?> height=<?=$iHeight?> border=0></a></td></tr></table>
										<?php }?>
										<?php }?>
									</td>
								</tr>
								<tr>
									<td colspan=2>
										<?php 
										If ( $iParentUnq != $aCategories[1][$x] ) {
											$iLevel = 0;
											DispSubCats($aCategories[1][$x]);
										}

										If ( ( $iCount > 0 ) || ( $aCategories[3][$x] >  0 ) ) {	// only display this if there are galleries and/or subcategories
											If ( strtoupper(Trim(DOMAIN_Conf("IMAGEGALLERY_ALLOW_ALL_DISPLAY"))) == "YES" ) {
												?>
												<br><a href='ThumbnailView.php?iCategoryUnq=<?=$aCategories[1][$x]?>' class='SmallNavPage'>List <u>ALL</u> images in this category and its subcategories.</a>
												<?php
											}
										}
										?>
									</td>
								</tr>
							</table>
						</td>
						<?php 
					}Else{
						If ( $aCategories[3][$x] > 0 ) {
							?>
							<td align=center valign=top>
								<table cellpadding = 0 cellspacing=0 border=0 width=100% class='TablePage'>
									<?php If ( $sCatImageLoc == "TOP" ) {?>
									<?php If ( $aCategories[2][$x] == "Y" ) {?>
									<tr>
										<td colspan=2>
											<table cellpadding=0 cellspacing=0 border=0 width=<?=$iWidth?> class='Table1_Boxed'><tr><td><a href='G_Display.php?iCategoryUnq=<?=$aCategories[1][$x]?>'><img src='<?=$sFilePath?>' alt = '<?=$aCategories[0][$x]?>' width=<?=$iWidth?> height=<?=$iHeight?> border=0></a></td></tr></table>
										</td>
									</tr>
									<?php }?>
									<?php }?>
									<tr>
										<td valign=top colspan=2>
											<?php G_STRUCTURE_Category_Lines($aCategories[0][$x], $aCategories[0][$x], "index.php?iParentUnq=" . $aCategories[1][$x], "Galleries", $sLines);?>
										</td>
									</tr>
									<?php If ( $sCatImageLoc == "RIGHT" ) {?>
									<?php If ( $aCategories[2][$x] == "Y" ) {?>
									<tr>
										<td valign=top align=right colspan=2>
											<table cellpadding=0 cellspacing=0 border=0 width=<?=$iWidth?> class='Table1_Boxed'><tr><td><a href='G_Display.php?iCategoryUnq=<?=$aCategories[1][$x]?>'><img src='<?=$sFilePath?>' alt = '<?=$aCategories[0][$x]?>' width=<?=$iWidth?> height=<?=$iHeight?> border=0></a></td></tr></table>
										</td>
									</tr>
									<?php }?>
									<?php }?>
									<tr>
										<td colspan=2>
											<?php 
											If ( $iParentUnq != $aCategories[1][$x] ) {
												$iLevel = 0;
												DispSubCats($aCategories[1][$x]);
											}
											
											If ( ( $iCount > 0 ) || ( $aCategories[3][$x] >  0 ) ) {	// only display this if there are galleries and/or subcategories
												If ( strtoupper(Trim(DOMAIN_Conf("IMAGEGALLERY_ALLOW_ALL_DISPLAY"))) == "YES" ) {
													?>
													<br><a href='ThumbnailView.php?iCategoryUnq=<?=$aCategories[1][$x]?>' class='SmallNavPage'>List <u>ALL</u> images in this category and its subcategories.</a>
													<?php
												}
											}
											?>
										</td>
									</tr>
								</table>
							</td>
							<?php 
						}Else{
							If ( $y <= 1 )
								$x++;
							$y--;
						}
					}
				}Else{
					Echo "<td bgcolor = " . $GLOBALS["PageBGColor"] . ">&nbsp;</td>";
				}
			}
			Echo "</tr>\n";
		}
		Echo "</table>\n";
		Echo "</td></tr></table>\n";
	}
	//************************************************************************************
	
	
	
	//************************************************************************************
	//*																					*
	//*																					*
	//*																					*
	//************************************************************************************
	Function DispSubCats($iCategoryUnq)
	{
		Global $bAnyChildWGallery;
		Global $iParentUnq;
		Global $iLevel;
		Global $iNumCatsToDisp;
		
		$sQuery			= "SELECT Name, CategoryUnq FROM IGCategories (NOLOCK) WHERE Parent = " . $iCategoryUnq . " AND Parent != 0 ORDER BY Position";
		$rsRecordSet2	= DB_Query($sQuery);
		if ( DB_NumRows($rsRecordSet2) > 0 )
		{
			$iCount2 = 0;
			If ( $iNumCatsToDisp > 0 )
			{
				If ( $iParentUnq == $iCategoryUnq ) {
					Echo "<span class='SmallPageText'><b>Subcategories:&nbsp;</b></span>";
				}ElseIf ( $iLevel == 0 ) {
					Echo "<b><a href='index.php?iParentUnq=" . $iCategoryUnq . "' class='SmallNavPage'>Subcategories:</a>&nbsp;</b></font>";
				}
			}
			If ( $iNumCatsToDisp > 0 )
			{
				While ( ( $rsRow2 = DB_Fetch($rsRecordSet2) ) && ( $iCount2 < $iNumCatsToDisp ) ) 
				{
					$bAnyChildWGallery = False;
					AnyChildWGallery($rsRow2["CategoryUnq"]);
					$iNumImages = GetNumImages($rsRow2["CategoryUnq"]);
					If ( ( $bAnyChildWGallery ) || ( $iNumImages > 0 ) ) {
						$iCount2++;
						If ( $iNumImages > 0 ) {
							$sTemp = " (" . $iNumImages . ")";
						}Else{
							$sTemp = "";
						}
						$iNumSubs = PopulateSubCatCount($rsRow2["CategoryUnq"]);
						If ( $iParentUnq == $iCategoryUnq ) {
							Echo "<BR><img src='Images/Blank.gif' width=" . (25*($iLevel+1)) . " height=2><span class='SmallPageText'>" . $rsRow2["Name"] . "</span> (" . $iNumSubs . ")";
						}Else{
							If ( $iNumSubs == 0 ) {
								Echo "<BR><img src='Images/Blank.gif' width=" . (25*($iLevel+1)) . " height=2><a href='G_Display.php?iCategoryUnq=" . $rsRow2["CategoryUnq"] . "' class='SmallNavPage'>" . $rsRow2["Name"] . "</a>" . $sTemp;
							}Else{
								Echo "<BR><img src='Images/Blank.gif' width=" . (25*($iLevel+1)) . " height=2><a href='index.php?iParentUnq=" . $rsRow2["CategoryUnq"] . "' class='SmallNavPage'>" . $rsRow2["Name"] . "</a>" . $sTemp;
							}
						}
						$iLevel++;
						DispSubCats($rsRow2["CategoryUnq"]);
						$iLevel--;
					}
				}
				Echo "<BR>";
	
				While ( $rsRow2 = DB_Fetch($rsRecordSet2) )
				{
					$bAnyChildWGallery = False;
					AnyChildWGallery($rsRow2["CategoryUnq"]);
					If ( $bAnyChildWGallery )
						$iCount2++;
				}
			}
			If ( $iCount2 > $iNumCatsToDisp ) {
				If ( $iParentUnq != $iCategoryUnq ) {
					Echo "<img src='Images/Blank.gif' width=25 height=2><a href='index.php?iParentUnq=" . $iCategoryUnq . "' class='SmallNavPage'>..." . ($iCount2 - $iNumCatsToDisp) . " more</a>";
				}
			}
		}
	}	
	//************************************************************************************
	
	
	//************************************************************************************
	//*																					*
	//*																					*
	//*																					*
	//************************************************************************************
	Function PopulateSubCatCount($iCategoryUnq)
	{		
		$iCount			= 0;
		If ( $GLOBALS["sUseDB"] == "MSSQL" ) {
			$sQuery	= "SELECT Count(*), C.CategoryUnq FROM Galleries G (NOLOCK), IGCategories C (NOLOCK) WHERE C.Parent = " . $iCategoryUnq . " AND C.CategoryUnq = G.CategoryUnq AND G.GalleryUnq IN (SELECT DISTINCT G.GalleryUnq FROM Galleries G (NOLOCK), ImagesInGallery IG (NOLOCK) WHERE G.GalleryUnq = IG.GalleryUnq AND G.CategoryUnq = C.CategoryUnq) GROUP BY C.CategoryUnq";
		}ElseIf ( $GLOBALS["sUseDB"] == "MYSQL" ) {
			$sTemp			= "";
			$sQuery			= "SELECT DISTINCT G.GalleryUnq FROM Galleries G (NOLOCK), ImagesInGallery IG, IGCategories C (NOLOCK) WHERE G.GalleryUnq = IG.GalleryUnq AND G.CategoryUnq = C.CategoryUnq";
			$rsRecordSet	= DB_Query($sQuery);
			While ( $rsRow = DB_Fetch($rsRecordSet) )
				$sTemp .= $rsRow["GalleryUnq"] . ",";
			$sTemp .= "0";
			$sQuery	= "SELECT Count(*), C.CategoryUnq FROM Galleries G (NOLOCK), IGCategories C (NOLOCK) WHERE C.Parent = " . $iCategoryUnq . " AND C.CategoryUnq = G.CategoryUnq AND G.GalleryUnq IN (" . $sTemp . ") GROUP BY C.CategoryUnq";
		}
		$rsRecordSet	= DB_Query($sQuery);
		while ( $rsRow = DB_Fetch($rsRecordSet) )
			$iCount++;
			
		Return $iCount;
	}
	//************************************************************************************
	
	
	//************************************************************************************
	//*																					*
	//*																					*
	//*																					*
	//************************************************************************************
	Function GetNumImages($iCategoryUnq)
	{
		$sQuery			= "SELECT count(*) FROM Galleries G (NOLOCK), ImagesInGallery IG (NOLOCK) WHERE G.GalleryUnq = IG.GalleryUnq AND G.CategoryUnq = " . $iCategoryUnq;
		$rsRecordSet	= DB_Query($sQuery);
		if ( $rsRow = DB_Fetch($rsRecordSet) )
			Return $rsRow[0];
			
		Return 0;		
	}
	//************************************************************************************
	
	
	//************************************************************************************
	//*																					*
	//*	This is a recursive function to search for any category or it's children who	*
	//*		have a gallery -- stops at the first gallery it finds and returns true.		*
	//*																					*
	//************************************************************************************
	Function AnyChildWGallery($iCategoryUnq)
	{
		Global $bAdmin;
		Global $bCanCreate;
		Global $PUBLIC_GALLERIES;
		Global $iLoginAccountUnq;
		Global $bAnyChildWGallery;
		
		$sQuery			= "SELECT CategoryUnq FROM IGCategories (NOLOCK) WHERE Parent = " . $iCategoryUnq;
		$rsRecordSet3	= DB_Query($sQuery);
		if ( DB_NumRows($rsRecordSet3) > 0 )
		{
			while ( ( $rsRow3 = DB_Fetch($rsRecordSet3) ) && ( $bAnyChildWGallery == False ) ) 
			{
				If ( $bAdmin ) {
					// they can see ALL galleries for this domain
					$sQuery = "SELECT G.GalleryUnq FROM Galleries G (NOLOCK), ImagesInGallery IG (NOLOCK) WHERE G.CategoryUnq = " . $rsRow3["CategoryUnq"] . " AND IG.GalleryUnq = G.GalleryUnq";
				}ElseIf ( $bCanCreate ) {
					// they can see their and public galleries
					$sQuery = "SELECT G.GalleryUnq FROM Galleries G (NOLOCK), ImagesInGallery IG (NOLOCK) WHERE (G.AccountUnq = " . $iLoginAccountUnq . " OR G.Visibility = '" . $PUBLIC_GALLERIES . "') AND G.CategoryUnq = " . $rsRow3["CategoryUnq"] . " AND IG.GalleryUnq = G.GalleryUnq";
				}Else{
					// they can only see public galleries
					$sQuery = "SELECT G.GalleryUnq FROM Galleries G (NOLOCK), ImagesInGallery IG (NOLOCK) WHERE G.Visibility = '" . $PUBLIC_GALLERIES . "' AND G.CategoryUnq = " . $rsRow3["CategoryUnq"] . " AND IG.GalleryUnq = G.GalleryUnq";
				}
				DB_Query("SET ROWCOUNT 1");
				$rsRecordSet4 = DB_Query($sQuery);
				DB_Query("SET ROWCOUNT 0");
				If ( $rsRow4 = DB_Fetch($rsRecordSet4) )
					$bAnyChildWGallery = True;

				If ( ! $bAnyChildWGallery )
					AnyChildWGallery($rsRow3["CategoryUnq"]);
			}
		}Else{
			// no more child categories, so just check the current (leaf) one
			$sQuery = "SELECT G.GalleryUnq FROM Galleries G (NOLOCK), ImagesInGallery IG (NOLOCK) WHERE G.CategoryUnq = " . $iCategoryUnq . " AND IG.GalleryUnq = G.GalleryUnq";
			DB_Query("SET ROWCOUNT 1");
			$rsRecordSet4 = DB_Query($sQuery);
			DB_Query("SET ROWCOUNT 0");
			If ( $rsRow4 = DB_Fetch($rsRecordSet4) )
				$bAnyChildWGallery = True;
		}
	}
	//************************************************************************************
?>