<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_ecom.gif" width="20" height="21" align="middle"> '.$MsgText[9][3];
	$BrowserTitle = $MsgText[9][3];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[9][3];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[901] == 'y') {
		
		
			$query = "select * from `".$Settings['DBPrefix']."ec-Settings`";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				$ecSettings[$row['Name']] = $row['Value'];
			}
			
			
			if ($ecSettings['PriceDecimals'] == '') {
				$ecSettings['PriceDecimals'] = 2;
			}
			
			if ($ecSettings['DecimalSymbol'] == '') {
				$ecSettings['DecimalSymbol'] = '.';
			}
			
			if ($ecSettings['ThousandsSep'] == '') {
				$ecSettings['ThousandsSep'] = '.';
			}
			
			
			if ($ecSettings['CurrencyLocation'] == '' || $ecSettings['CurrencyLocation'] == 'b') {
				if ($ecSettings['Currency'] == '') {
					$CurrencyBefore = '$';
				}
				else {
					$CurrencyBefore = $ecSettings['Currency'];
				}
			}
			else {
				if ($ecSettings['Currency'] == '') {
					$CurrencyAfter = '$';
				}
				else {
					$CurrencyAfter = $ecSettings['Currency'];
				}
			}
		
		
			
			$query = "select * from `".$Settings['DBPrefix']."ec-Categories` ORDER BY Name ASC, RecordNumber ASC";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) { 
				$CategoryIDsArray[$row['Parent']][] = $row['RecordNumber'];
				$CategoryNamesArray[$row['Parent']][] = $row['Name'];
				$CategoryExists[$row['RecordNumber']] = 'y';
			}
			
			$query = "select * from `".$Settings['DBPrefix']."ec-Products` where Active=1 ORDER BY Name ASC, RecordNumber ASC";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				if ($CategoryExists[$row['Category']] == 'y' || $row['Category'] == 0) {
					$ProductIDsArray[$row['Category']][] = $row['RecordNumber'];
					$ProductNamesArray[$row['Category']][] = $row['Name'];
					
					if ($row['Category2'] > 0) {
						$ProductIDsArray[$row['Category2']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category2']][] = $row['Name'];
					}
					if ($row['Category3'] > 0) {
						$ProductIDsArray[$row['Category3']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category3']][] = $row['Name'];
					}
					if ($row['Category4'] > 0) {
						$ProductIDsArray[$row['Category4']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category4']][] = $row['Name'];
					}
					if ($row['Category5'] > 0) {
						$ProductIDsArray[$row['Category5']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category5']][] = $row['Name'];
					}
				}
				else {
					$OrphanProducts++;
					$OrphanProductIDsArray[] = $row['RecordNumber'];
					$OrphanProductNamesArray[] = $row['Name'];
				}
				
				
				$query2 = "select * from `".$Settings['DBPrefix']."ec-Items` where Product=".$row['RecordNumber']." limit 2";
				$result2 = mysql_query($query2) or die (mysql_error());
				if (mysql_num_rows($result2) > 1) { 
					$ProductsWithItemsArray[] = $row['RecordNumber'];
					$ProductsWithItemsNamesArray[] = $row['Name'].' ('.$row['ProductID'].')';
				}
				
			}
			
			
			for ($y = 0; $y < count($ProductsWithItemsArray); $y++) {
				$CloneList .= '<option value="'.$ProductsWithItemsArray[$y].'"> '.$ProductsWithItemsNamesArray[$y].'</option>';
			}
			
			
			$query = "select Category from `".$Settings['DBPrefix']."ec-Products` ORDER BY RecordNumber DESC LIMIT 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			$LastProductParent = $row['Category'];
			
			
			
			$PageContents .= '<br><a href="'.$MainURL.'/admin/store-categories.html"><b>'.$MsgText[9][2].'</b></a><br><br><a href="#new">'.$MsgText[9][21].'</a><br><br><br>'.$MsgText[0][248];
			
			for ($j = 0; $j < count($ProductIDsArray[0]); $j++) {
				$CategoriesPrint .= '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <a href="'.$MainURL.'/admin/store-products_edit/'.$ProductIDsArray[0][$j].'.html">'.$ProductNamesArray[0][$j].'</a>';
			}
			
			if ($OrphanProducts > 0) {
				$CategoriesPrint .= '<br>Orphan Products';
				for ($j = 0; $j < count($OrphanProductIDsArray); $j++) {
					$CategoriesPrint .= '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <a href="'.$MainURL.'/admin/store-products_edit/'.$OrphanProductIDsArray[$j].'.html">'.$OrphanProductNamesArray[$j].'</a>';
				}
			}


			$CatParent5 = 0;
			$LastParentAbove5 = '0';
			$CatLevel5['0'] = 0;
									
			function SpiderCategories5($CatParent5) {
							
				Global $CategoryIDsArray, $CategoryNamesArray, $CategoryDropdown, $CategoryDropdown2, $LastParent5, $LastParentAbove5, $CatSpacersArray5, $CatLevel5, $CatsCount5, $CurrentPageCategory, $MainURL, $CategoriesPrint, $ProductIDsArray, $ProductNamesArray, $LastProductParent;
							
				for ($i = 0; $i < count($CategoryIDsArray[$CatParent5]); $i++) {
								
					$LastParentAboveB5 = $LastParentAbove5;
					$LastParentB5 = $LastParent5;
								
					$CatLevel5[$CategoryIDsArray[$CatParent5][$i]] = $CatLevel5[$CatParent5];
					if ($CatParent5 != $LastParent5 && $CatParent5 == $LastParentAbove5) {
							
						$CatLevel5[$CategoryIDsArray[$CatParent5][$i]] = $CatLevel5[$CatParent5] + 1;
						$CatLevel5[$CatParent5]++;
															
						$CatsCount5[$CatLevel5[$CatParent5]] = 0;
							
					}
								
					$LastParent5 = $CatParent5;
					$LastParentAbove5 = $CategoryIDsArray[$CatParent5][$i];
								
					$CatsCount5[$CatLevel5[$CatParent5]]++;
								
										
					$CatSpacer5 = '';
					for ($x = 0; $x < $CatLevel5[$CatParent5]; $x++) {
									
						if ($CatsCount5[$CatLevel5[$CatParent5]] == count($CategoryIDsArray[$CatParent5]) && $x == $CatLevel5[$CatParent5]-1) {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'y';
						}
						else if ($CatsCount5[$CatLevel5[$CatParent5]] == count($CategoryIDsArray[$CatParent5])) {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						else {
							if ($x < $CatLevel5[$CatParent5]) {
								$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
								$ResetAfter5 = 'y2';
							}
							else {
								$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							}
						}
						
							
						$CatSpacer5 .= $CatSpacersArray5[$x+1];
						
						if ($ResetAfter5 == 'y') {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'n';
						}
						else if ($ResetAfter5 == 'y2') {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'n';
						}
													
					}
					
					
					
					$CategoryID = $CategoryIDsArray[$CatParent5][$i];
				
					if ($CategoryID == $LastProductParent) {
						$CategoryDropdown .= '<option value="'.$CategoryID.'"'.$ElementStyle.' selected>&nbsp;'.$CatSpacer5.' '.$CategoryNamesArray[$CatParent5][$i].$AsteriskAdded.'&nbsp;</option>';
					}
					else {
						$CategoryDropdown .= '<option value="'.$CategoryID.'"'.$ElementStyle.'>&nbsp;'.$CatSpacer5.' '.$CategoryNamesArray[$CatParent5][$i].$AsteriskAdded.'&nbsp;</option>';
					}
					
					$CategoryDropdown2 .= '<option value="'.$CategoryID.'"'.$ElementStyle.'>&nbsp;'.$CatSpacer5.' '.$CategoryNamesArray[$CatParent5][$i].$AsteriskAdded.'&nbsp;</option>';
						
					$CategoriesPrint .= '<br>'.$CatSpacer5.' '.$CategoryNamesArray[$CatParent5][$i];
						
						
						
					for ($j = 0; $j < count($ProductIDsArray[$CategoryID]); $j++) {
						$CategoriesPrint .= '<br>'.$CatSpacer5.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <a href="'.$MainURL.'/admin/store-products_edit/'.$ProductIDsArray[$CategoryID][$j].'.html">'.$ProductNamesArray[$CategoryID][$j].'</a>';
					}	
					
								
												
					
					if (count($CategoryIDsArray[$CategoryIDsArray[$CatParent5][$i]]) > 0) {
						SpiderCategories5($CategoryIDsArray[$CatParent5][$i]);
					}
							
				}
							
							
			}
			
			SpiderCategories5('0');
			
			
			if (function_exists("gd_info")) {
				$GDInfo = @gd_info();
			}
			if ($GDInfo['GIF Create Support'] == true) {
				$MsgText[0][314] = $MsgText[0][537];
			}
			else {
				$JPEGPNGOnly = '<br>('.$MsgText[0][313].')';
			}
			
			
			
			if ($CloneList != '') {
			
				$CloneSection = '<br><table width="400" cellpadding="1" cellspacing="0" border="0"><tr><td align="center" bgcolor="'.$Colors['Text'].'"><table width="100%" cellpadding="10" cellspacing="0" border="0"><tr><td bgcolor="'.$Colors['BG'].'"><font face="'.$FontFace.'" size="2">

<div align="center"><b>'.$MsgText[9][22].':</b><br><br>

<select name="CloneFrom" style="'.$FormStyle.'"><option value=""> '.$MsgText[9][23].'</option>'.$CloneList.'</select>

</div></font></td></tr></table></td></tr></table>';
			
			
			}
			
			


			$PageContents .= $CategoriesPrint. '<br><br><br><br><a name="new"></a><b>'.$MsgText[9][21].':</b><br><br><form name="PostForm" action="'.$MainURL.'/admin/store-products_new" method="post" enctype="multipart/form-data"><b>'.$MsgText[9][24].':</b> ('.$MsgText[0][418].')<br><input type="text" name="ProductID" size="40" style="'.$FormStyle.'"><br><font size="1">('.$MsgText[9][25].')</font><br><br><b>'.$MsgText[9][26].':</b><br><input type="text" name="ProductName" size="55" style="'.$FormStyle.' width:400;"><br><br><b>'.$MsgText[9][27].':</b> ('.$MsgText[0][418].')<br><textarea name="ShortDescription" cols="40" rows="2" wrap="virtual" style="'.$FormStyle.' width:400;"></textarea><br><br><b>'.$MsgText[9][28].':</b> ('.$MsgText[0][418].')<br><textarea name="ProductDescription" cols="40" rows="6" wrap="virtual" style="'.$FormStyle.' width:400;"></textarea><br><br><b>'.$MsgText[9][29].':</b> ('.$MsgText[0][418].')<br><input type="hidden" name="MAX_FILE_SIZE" value="'.number_format(($ecSettings['MaxImageFileSize']*1024),0,'','').'"><input type="file" size="30" name="FileData" style="'.$FormStyle.'"><br><input type="file" size="30" name="FileData2" style="'.$FormStyle.'"><br><input type="file" size="30" name="FileData3" style="'.$FormStyle.'"><br><input type="file" size="30" name="FileData4" style="'.$FormStyle.'"><br><input type="file" size="30" name="FileData5" style="'.$FormStyle.'"><br><font face="Arial" size="1">('.$MsgText[0][311].': '.number_format($ecSettings['MaxImageFileSize'],0,'','').' KBytes)</font><br><br><b>'.$MsgText[9][30].':</b><br><select name="Parent" style="'.$FormStyle.'"><option value="0">'.$MsgText[0][248].'</option>'.$CategoryDropdown.'</select><br><select name="Category2" style="'.$FormStyle.'"><option value="0"></option><option value="0">'.$MsgText[0][248].'</option>'.$CategoryDropdown2.'</select><br><select name="Category3" style="'.$FormStyle.'"><option value="0"></option><option value="0">'.$MsgText[0][248].'</option>'.$CategoryDropdown2.'</select><br><select name="Category4" style="'.$FormStyle.'"><option value="0"></option><option value="0">'.$MsgText[0][248].'</option>'.$CategoryDropdown2.'</select><br><select name="Category5" style="'.$FormStyle.'"><option value="0"></option><option value="0">'.$MsgText[0][248].'</option>'.$CategoryDropdown2.'</select><br><br><br>

<table width="400" cellpadding="1" cellspacing="0" border="0"><tr><td align="center" bgcolor="'.$Colors['Text'].'"><table width="100%" cellpadding="10" cellspacing="0" border="0"><tr><td bgcolor="'.$Colors['BG'].'"><font face="'.$FontFace.'" size="2"><div align="center"><b>'.$MsgText[9][31].'</b></div><br>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="50%"><font face="'.$FontFace.'" size="2"><b>'.$MsgText[9][32].':</b><br><b>'.$CurrencyBefore.'</b>&nbsp;<input type="text" name="ProductPrice" size="5" style="'.$FormStyle.'">&nbsp;<b>'.$CurrencyAfter.'</b>&nbsp;&nbsp;&nbsp;</td>
	  <td width="50%"><font face="'.$FontFace.'" size="2"><b>'.$MsgText[9][33].':</b><br><b>'.$CurrencyBefore.'</b>&nbsp;<input type="text" name="ComparePrice" size="5" style="'.$FormStyle.'">&nbsp;<b>'.$CurrencyAfter.'</b> ('.$MsgText[0][418].')</td>
    </tr>
</table>
<br><table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="35%"><font face="'.$FontFace.'" size="2"><b>'.$MsgText[9][34].':</b><br><input type="text" name="ShippingWeight" size="2" style="'.$FormStyle.'">lbs&nbsp;<input type="text" name="ShippingWeightOz" size="1" style="'.$FormStyle.'">oz</td>
	  <td width="65%"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="5" alt="" border="0"><br><font face="'.$FontFace.'" size="2">('.$MsgText[9][35].')</td>
    </tr>
</table>
<br><table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="50%"><font face="'.$FontFace.'" size="2"><b>'.$MsgText[9][38].':</b><br><b>'.$CurrencyBefore.'</b>&nbsp;<input type="text" name="AdditionalShipping" size="5" style="'.$FormStyle.'">&nbsp;<b>'.$CurrencyAfter.'</b> ('.$MsgText[0][418].')</font></td>
	  <td width="50%"><font face="'.$FontFace.'" size="2"><b>'.$MsgText[9][36].':</b><br><input type="text" name="ProductInventory" size="4" style="'.$FormStyle.'"> ('.$MsgText[0][418].')</font></td>
	</tr>
</table>

</td></tr></table></td></tr></table>

'.$CloneSection.'

<br><br><br><input type="submit" value="'.$MsgText[9][37].'" style="'.$FormStyle.'"></form>';
		
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

	// ========================================================================================




?>
