<?php
// Set flag that this is a parent file
	defined('_FEXEC') or die();
  	$server_date = getServerDate(true);  	
  	$pID=isset($HTTP_GET_VARS['pID'])?$HTTP_GET_VARS['pID']:$HTTP_POST_VARS['product_id'];
  	$products_id=$pID;
  	$check_ID=($HTTP_GET_VARS['chk_ID'])?$HTTP_GET_VARS['chk_ID']:0;
  	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
 
  	if($action!=""){  	
 	//End Categories Description 1.5
 	$languages = tep_get_languages();
 	 if($HTTP_GET_VARS['cID']>0){
	 $cID=$HTTP_GET_VARS['cID'];
		$categories_query = tep_db_query("select c.parent_id,c.categories_id, cd.categories_name, c.categories_image, c.sort_order, c.date_added, c.last_modified from  ".TABLE_CATEGORIES."  c,  ".TABLE_CATEGORIES_DESCRIPTION."  cd where c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "' and c.categories_id='".(int)$cID."' order by c.sort_order, cd.categories_name");
		if(tep_db_num_rows($categories_query)>0){
			$categories=tep_db_fetch_array($categories_query);
			$cInfo=new objectInfo($categories);
		}
	 }
	 if($pID>0 && $action!=""){
	  	$product_query = tep_db_query("select pd.products_id,pd.products_name, pd.products_description, p.products_quantity,p.products_price,date_format(p.products_date_added,'%Y-%m-%d') as products_date_added, date_format(p.products_last_modified,'%Y-%m-%d') as products_last_modified,p.products_status,p.products_image_1,p.products_sort_order,p.is_subscription from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$pID . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'");
		if(tep_db_num_rows($product_query)>0){
			$product = tep_db_fetch_array($product_query);
			$pInfo = new objectInfo($product);
		}
		$cPath=isset($HTTP_POST_VARS['cPath'])?$HTTP_POST_VARS['cPath']:'0';
		$current_category_id = $cPath;
	 }
	 /*if($action=='edit_category' && !$HTTP_GET_VARS['cID'] && $check_ID>0){
		$products_query = tep_db_query("select count(*) as total from ".TABLE_PRODUCTS." p, ".TABLE_PRODUCTS_TO_CATEGORIES." p2c where p.products_id = p2c.products_id and p2c.categories_id = '" . $check_ID . "'");
		$products=tep_db_fetch_array($products_query);
		if($products['total']<=0){
			echo 'new^';
			$action='edit_product';
		}
	  }*/
	 if($action=='file_upload'){
		 $sql_data_array=array();
		 $upload_fs_dir = DIR_FS_CATALOG_IMAGES;
		 $upload_error=false;
		 $categories_filename="";
		 if($HTTP_POST_VARS['action_type']=="new_category" || $HTTP_POST_VARS['action_type']=="edit_category"){
			 $categories = new upload('categories_image');			 
			 if(!is_writable($upload_fs_dir) || !is_writable($upload_fs_dir)){
			 	echo 'upload_error^';
			 	if(!is_writable($upload_fs_dir)) echo sprintf(WARNING_IMAGE_DIRECTORY_WRITEABLE,$upload_fs_dir);
			 }else	{
				 	$categories->set_destination($upload_fs_dir);
				 	if ($categories->parse() && $categories->save()) $categories_filename=$categories->filename;
				 	else {
				 		$upload_error=true;
					   	echo 'upload_error^'.ERR_CATEGORIES_IMAGE_LENGTH;	
				 	}
				 	/*if(strlen($categories_filename)>64) {
					    $upload_error=true;
					    echo 'upload_error^'.ERR_CATEGORIES_IMAGE_LENGTH;	
					}else*/ 
					if($categories_filename!="" && $HTTP_POST_FILES['categories_image']['size']<=0) {
						$upload_error=true;
					    echo 'upload_error^'.ERR_CATEGORIES_IMAGE_DOESNT_EXISTS;
					}
				 	if($HTTP_POST_VARS['action_type']=='new_category' && !$upload_error){
						$sql_data_array=array('categories_image'=>$categories_filename,'date_added'=>$server_date,'parent_id'=>($HTTP_POST_VARS['main_category']=='Y')?0:$HTTP_POST_VARS['cPath']);
						tep_db_perform(TABLE_CATEGORIES, $sql_data_array);
						$insert_id=tep_db_insert_id();
						echo $insert_id;
						for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
							$insert_sql_data = array(
								'categories_id' => $insert_id,
								'language_id' => $languages[$i]['id']);	
							tep_db_perform(TABLE_CATEGORIES_DESCRIPTION, $insert_sql_data);		
						}
					}else if($HTTP_POST_VARS['action_type']=='edit_category' && !$upload_error) tep_db_query("update " . TABLE_CATEGORIES . " set categories_image = '" . $categories_filename . "' where categories_id = '" . tep_db_input($HTTP_POST_VARS['cPath']) . "'");
			 }			 			  			 			
		 }else if($HTTP_POST_VARS['action_type']=="new" || $HTTP_POST_VARS['action_type']=="edit"){ 
			 if($HTTP_POST_VARS['action_type']=="new"){
					for ($icnt=1;$icnt<=5;$icnt++){						
						$products_image[$icnt] = new upload('products_image_' . $icnt);
						$products_image[$icnt]->set_destination(DIR_FS_CATALOG_IMAGES);
						if(!is_writable($upload_fs_dir) || !is_writable($upload_fs_dir)){
			 				echo 'upload_error^';
			 				if(!is_writable($upload_fs_dir)) echo sprintf(WARNING_IMAGE_DIRECTORY_WRITEABLE,$upload_fs_dir);
						}else if ($products_image[$icnt]->parse() && $products_image[$icnt]->save(true))
						/*if(strlen($products_image[$icnt]->filename)>64) {
			  				$upload_error=true;
			  				echo 'upload_error^'.ERR_PRODUCT_IMAGE_LENGTH; 	
						}else*/
						if($products_image[$icnt]->filename!="" && $HTTP_POST_FILES['products_image_'.$icnt]['size']<=0){
							$upload_error=true;
			    			echo 'upload_error^'.ERR_CATEGORIES_IMAGE_DOESNT_EXISTS;
						}						
						$products_image_name[$icnt] = $products_image[$icnt]->filename;						
				    }
					
					
				    $sql_data_array=array('products_image_1'=>$products_image_name[1],'products_image_2'=>$products_image_name[2],'products_image_3'=>$products_image_name[3],'products_image_4'=>$products_image_name[4],'products_image_5'=>$products_image_name[5]);				    
					
				    if ($HTTP_POST_VARS['action_type'] == "new" && !$upload_error) {
				    	tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
				    	$products_id=tep_db_insert_id();
						echo $products_id;
						for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {				    						
		              		$language_id=$languages[$i]['id'];	
		              		$insert_sql_data = array('products_id' => $products_id,
		                                       'language_id' => $language_id);		              
		              		tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $insert_sql_data);            
						}
				    }
			}else if($HTTP_POST_VARS['action_type']=='edit' && !$upload_error){
				$products_id=(int)$HTTP_POST_VARS['product_id'];
				for ($icnt=1;$icnt<=5;$icnt++){
						$products_image[$icnt] = new upload('products_image_' . $icnt);					
						$products_image[$icnt]->set_destination(DIR_FS_CATALOG_IMAGES);					
						if ($products_image[$icnt]->parse() && $products_image[$icnt]->save(true)) 
						   tep_db_query("update ".TABLE_PRODUCTS . " set products_image_".$icnt."='".$products_image[$icnt]->filename."' where products_id='".$products_id."'");					   					   					   												
				}				
			}   			 				 
			 $download_link= new upload('download_link');
			$download_link->set_destination(DIR_FS_DOWNLOAD);
			if (!is_dir(DIR_FS_DOWNLOAD)){
				@mkdir(DIR_FS_DOWNLOAD,0777);
	 		 }
			if(!is_writable(DIR_FS_DOWNLOAD)){
				echo 'upload_error^';
				echo sprintf(WARNING_IMAGE_DIRECTORY_WRITEABLE,DIR_FS_DOWNLOAD);
			}else if ($download_link->parse() && $download_link->save())
			{
			   tep_db_query("update ".TABLE_PRODUCTS . " set download_link='".$download_link->filename."' where products_id='".$products_id."'");					   					   					   												
			}
	   }
	  }else if($action=='update_category'){
			$categories_id = $cID;
			$current_category_id=(int)$HTTP_POST_VARS['cPath'];
			$sort_order = $HTTP_POST_VARS['categories_sortorder'];
			$sort_query=tep_db_query("select max(sort_order) as sort_order from ".TABLE_CATEGORIES);
			if((!$categories_id || !$sort_order) && tep_db_num_rows($sort_query)>0){
				$sort=tep_db_fetch_array($sort_query);
				$sort_order = $sort['sort_order']+1;
			}
			if($HTTP_POST_VARS['action_type']=='new_category' && $HTTP_POST_VARS['main_category']=='Y') {
				echo 'new_main_category^';
				$current_category_id=0;
			}else echo $HTTP_POST_VARS['action_type'].'^';
			$sql_data_array = array('sort_order' => $sort_order);
			if (!$categories_id) {
				$insert_sql_data = array(
					'parent_id' => $current_category_id,
					'date_added' => $server_date
				);
				$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
				tep_db_perform(TABLE_CATEGORIES, $sql_data_array);
				$insert_id = tep_db_insert_id();												
			} elseif ($categories_id>0) {
				$update_sql_data = array('last_modified' => $server_date);
				$sql_data_array = array_merge($sql_data_array, $update_sql_data);
				tep_db_perform(TABLE_CATEGORIES, $sql_data_array, 'update', 'categories_id = \'' . $categories_id . '\'');				
			}  // top if closing bracket			
				for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
					$categories_name_array = str_replace("|||","&",$HTTP_POST_VARS['categories_name']);
					$categories_title_array = $HTTP_POST_VARS['categories_heading_title'];
					$categories_description_array = $HTTP_POST_VARS['categories_description'];							
					$language_id = $languages[$i]['id'];
					$sql_data_array = array('categories_name' => tep_db_prepare_input($categories_name_array[$language_id]),'categories_heading_title'=>tep_db_prepare_input($categories_title_array[$language_id]),'categories_description'=>tep_db_prepare_input($categories_description_array[$language_id]));				
					if (!$categories_id) {
						$insert_sql_data = array(
							'categories_id' => $insert_id,
							'language_id' => $languages[$i]['id']
						);
						$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
						tep_db_perform(TABLE_CATEGORIES_DESCRIPTION, $sql_data_array);
					} elseif ($categories_id>0) {
						tep_db_perform(TABLE_CATEGORIES_DESCRIPTION, $sql_data_array, 'update', 'categories_id = \'' . $categories_id . '\' and language_id = \'' . $languages[$i]['id'] . '\'');
						$insert_id=$categories_id;
					}
				}
			if($HTTP_POST_VARS['action_type']=='new_category') echo $insert_id.'{}'.$categories_name_array[$languages_id].'{}'.$current_category_id;
			else if($HTTP_POST_VARS['action_type']=='edit_category'){
				echo $categories_id.'{}'.$categories_name_array[$languages_id].'{}';								
 				echo get_category_details($categories);
			} 
	  	tep_reset_seo_cache('products');
	  }else if($action=='get_category_details'){
 			$product_categories=tep_db_query("select c.parent_id,date_format(c.date_added,'%Y-%m-%d') as date_added,date_format(c.last_modified,'%Y-%m-%d') as last_modified,cd.categories_id,cd.categories_name,c.sort_order,c.categories_image from ".TABLE_CATEGORIES."  c, ".TABLE_CATEGORIES_DESCRIPTION." cd where c.categories_id=cd.categories_id and cd.language_id='".(int)$languages_id."' and c.categories_id='".(int)$cID."'");
			if(tep_db_num_rows($product_categories)>0) $product=tep_db_fetch_array($product_categories);
 			echo get_category_details($product);
	  }else if($action=='edit_category'){
		   echo '<table width="100%" border="0" cellspacing="1" cellpadding="1" class="openContent">';?>
		   <tr id="img_save_close_category" style="display:none;"><td  colspan="2" class="openContent_top" align="right"><input type="image" border="0"  src="images/template/img_savel.gif">&nbsp;<a style="cursor:pointer;cursor:hand;" onClick="if(document.getElementById('new_category')) document.getElementById('new_category').style.display='none';"><img src="images/template/img_closel.gif" alt="close"></a>&nbsp;</td></tr>			
 	  	   <tr><td colspan="2" align="right"></td></tr>
 	  	   <?php if($cInfo->categories_id<=0) { ?><tr>
 	  	   			<td colspan="2">
 	  	   				<table border="0" cellspacing="0" cellpadding="0" width="100%">
 	  	   					<tr>
 	  	   						<td width="100%" class="main">&nbsp;&nbsp;<?php echo tep_draw_checkbox_field('main_category','Y').'&nbsp;'.TEXT_MAIN_CATEGORIES;?></td>	
			   				</tr>
 	  	   				</table>
 	  	   			</td>
 	  	   		</tr>
 	  	   <?php  } ?>
		  	<tr><td colspan="2">
		  		<table border="0" cellspacing="3" cellpadding="3" width="100%">
		  			<tr>
		  			<td>
		  				<table border="0" cellspacing="3" cellpadding="3" width="80%">
						   <?php $n=sizeof($languages);
				   			 for ($i=0; $i<$n; $i++) {
							?>
					          <tr>
					            <td class="main"><?php if ($i == 0) echo TEXT_EDIT_CATEGORIES_NAME ; ?></td>
					            <td class="main"><?php  echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;' . tep_draw_input_field('categories_name[' . $languages[$i]['id'] . ']', (($categories_name[$languages[$i]['id']]) ? stripslashes($categories_name[$languages[$i]['id']]) : tep_get_category_name($cInfo->categories_id, $languages[$i]['id'])),'maxlength="32"'); ?></td>
					          </tr>
					        <?php } ?>
					      </table>  			        
			        <td>
           		 	<table border="0" cellspacing="3" cellpadding="3" width="80%"> 	
			           <?php  for ($i=0; $i<$n; $i++) { ?>
			          	  <tr>
			            	<td class="main"><?php if ($i == 0) echo TEXT_EDIT_CATEGORIES_HEADING_TITLE; ?></td>
			            	<td class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;' . tep_draw_input_field('categories_heading_title[' . $languages[$i]['id'] . ']', (($categories_name[$languages[$i]['id']]) ? stripslashes($categories_name[$languages[$i]['id']]) : tep_get_category_heading_title($cInfo->categories_id, $languages[$i]['id'])),'maxlength="64"'); ?></td>
			          	  </tr>
			           <?php } ?>
           		   </table>
	       	   	  </td>	  
	       	   	  </tr>      		  	          
	        	</table>	        	
	       	   </td>
	        </tr>	    	      	           	            	 
          	  	<tr>
            		<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          	 </tr>
		  	<td valign="top" width="100%">
					  <table align="left" cellpadding="0" cellspacing="0" border="0" width="80%">
						  <tr>
							<td class="main" width="20%" align="left" valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '11', '10').TEXT_EDIT_CATEGORIES_IMAGE; ?></td>
							<td class="main" width="30%" align="left" valign="top"><?php echo tep_draw_separator('pixel_trans.gif','10','1') . tep_draw_file_field('categories_image') . '<br>' . tep_draw_separator('pixel_trans.gif','10','1') . '&nbsp;' . $cInfo->categories_image; ?></td>
							<td class="main" width="10%" align="center" valign="top"><?php echo tep_draw_separator('pixel_trans.gif','',''); ?></td>
							<td class="main" width="40%" align="left" valign="top"><?Php echo tep_draw_hidden_field('categories_sortorder',$cInfo->sort_order);?></td>
						  </tr>
					  </table>
			  </td>	
	     <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>          	
          	 <tr>
          	 <td colspan="2">
          	 	<div style='width:100%;height:100%;overflow:auto;'>
           		<table border="0" cellspacing="3" cellpadding="3" width="100%">
           		  <tr>           		              		 	            		 	   
		          	<?php  for ($i=0; $i<$n; $i++) { ?> 	
		          	  <td>          	  
		          	  <table border="0" cellspacing="3" cellpadding="3" width="100%">
		          	   <tr>
		                <td class="main" valign="top"><?php if ($i == 0) echo TEXT_EDIT_CATEGORIES_DESCRIPTION; ?></td>
		            	 <td>
		            		<table border="0" cellspacing="0" cellpadding="0">
				              <tr>
				                <td class="main" valign="top"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>&nbsp;</td>
				                <td class="main"><?php echo tep_draw_textarea_field('categories_description[' . $languages[$i]['id'] . ']', 'soft', '70', '15', (($categories_description[$languages[$i]['id']]) ? stripslashes($categories_description[$languages[$i]['id']]) : tep_get_category_description($cInfo->categories_id, $languages[$i]['id']))); ?></td>
				              </tr>
		            		</table>
		            	  </td>
		          	    </tr>
		          	   </table>
		          	  </td>
					<?php } ?>				
			  </tr>  
			</table>
			</div>
		   </td>
		 </tr>          	
		<tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
        </tr>	
        </table>  
	<?php 
	 } else if($action=='pos_up' || $action=='pos_down'){			
			$pos = isset($HTTP_GET_VARS['pos'])?$HTTP_GET_VARS['pos']:0;
			$res='sort_order_error^';
			if ($pos>0){
		         if ($action=='pos_up')
				 	$products_sort_query=tep_db_query("select p.products_sort_order, p.products_id from products p, products_to_categories pc where pc.categories_id='" . $HTTP_GET_VARS['cPath'] . "' and pc.products_id=p.products_id and p.products_sort_order<" . $pos . " order by products_sort_order desc limit 1");
		      	 else if($action=='pos_down')
					$products_sort_query=tep_db_query("select p.products_sort_order, p.products_id from products p, products_to_categories pc where pc.categories_id='" . $HTTP_GET_VARS['cPath'] . "' and pc.products_id=p.products_id and p.products_sort_order>" . $pos . " order by products_sort_order limit 1");
				 if(tep_db_num_rows($products_sort_query)>0){
			 		$products_sort_result=tep_db_fetch_array($products_sort_query);
					$prev_order=$products_sort_result['products_sort_order'];
					tep_db_query("UPDATE " . TABLE_PRODUCTS . " set products_sort_order='" . $pos ."' where products_id='" . (int)$products_sort_result['products_id'] . "'");
					tep_db_query("UPDATE " . TABLE_PRODUCTS . " set products_sort_order='" . $prev_order . "' where products_id='" . (int)$HTTP_GET_VARS['pID'] ."'");
			        $res="sort_order_success^".$prev_order;
				 }
			}
			echo $res;
	  }else if($action=='category_pos_up' || $action=='category_pos_down'){
			$pos = isset($HTTP_GET_VARS['pos'])?$HTTP_GET_VARS['pos']:0;
			$res='category_sort_order_error^';
			if ($pos>0){
		         if ($action=='category_pos_up')
				 	$category_sort_query=tep_db_query("select sort_order, categories_id from categories where parent_id in(SELECT parent_id FROM categories WHERE categories_id ='" .$HTTP_GET_VARS['cPath']. "') and sort_order<" . $pos . " order by sort_order desc limit 1");
		      	 else if($action=='category_pos_down')
					$category_sort_query=tep_db_query("select sort_order, categories_id from categories where parent_id in(SELECT parent_id FROM categories WHERE categories_id ='" .$HTTP_GET_VARS['cPath']. "') and sort_order>" . $pos . " order by sort_order limit 1");
				 if(tep_db_num_rows($category_sort_query)>0){
			 		$categories_sort_result=tep_db_fetch_array($category_sort_query);
					$prev_order=$categories_sort_result['sort_order'];
					tep_db_query("UPDATE " . TABLE_CATEGORIES . " set sort_order='" . $pos ."' where categories_id='" . (int)$categories_sort_result['categories_id'] . "'");
					tep_db_query("UPDATE " . TABLE_CATEGORIES . " set sort_order='" . $prev_order . "' where categories_id='" . (int)$HTTP_GET_VARS['cPath'] ."'");
			        $res="category_sort_order_success^".$prev_order;
				 }
			}
			echo $res;
	  }else if($action=='get_details')			
			echo_get_details();
	  else if($action=='setflag' && $HTTP_GET_VARS['flag']!="" && $HTTP_GET_VARS['pID']>0) 	  				
			tep_set_product_status($HTTP_GET_VARS['pID'], $HTTP_GET_VARS['flag']);
	  else if($action=='delete_category'){
			$contents='<table width="100%" class="openContent" border="0" cellspacing="5" cellpadding="5"><tr><td width="10" align="right">'.tep_image('images/template/img_trash.gif').'</td><td class="main" colspan="4"><b>' . TEXT_DELETE_CATEGORY_INTRO . '</b></td></tr><tr>';
			$contents.='<tr><td></td><td class="main">' . $cInfo->categories_name . '</td></tr>';
			$category_childs = tep_childs_in_category_count($cInfo->categories_id);
			$category_products =tep_products_in_category_count($cInfo->categories_id);
			if ($category_childs > 0) $contents.='<tr><td></td><td class="main">' . sprintf(TEXT_DELETE_WARNING_CHILDS, $category_childs).'</td></tr>';
			if ($category_products > 0) $contents.='<tr><td></td><td class="main">'. sprintf(TEXT_DELETE_WARNING_PRODUCTS, $category_products).'</td></tr>';
			$contents.='<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif', '1', '10').'</td></tr></table>';
			echo $contents;
	  		tep_reset_seo_cache('products');
	  }else if($action=='delete_category_confirm'){	  	
				if ($HTTP_GET_VARS['cID']) {
				$categories_id = tep_db_prepare_input($HTTP_GET_VARS['cID']);				
				$categories = tep_get_category_tree($categories_id, '', '0', '', true);							
				$products = array();
				$products_delete = array();				
				for ($i = 0, $n = sizeof($categories); $i < $n; $i++) {
					$product_query = tep_db_query("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . $categories[$i]['id'] . "'");
					while ($product_ids = tep_db_fetch_array($product_query))
						$products[$product_ids["products_id"]]['categories'][] = $categories[$i]['id'];					
				}				
				reset($products);
				while (list($key, $value) = each($products)) {
					$category_ids = '';
					for ($i = 0, $n = sizeof($value['categories']); $i < $n; $i++) $category_ids .= '\'' . $value['categories'][$i] . '\', ';					
					$category_ids = substr($category_ids, 0, -2);				
					$check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $key . "' and categories_id not in (" . $category_ids . ")");
					$check = tep_db_fetch_array($check_query);
					if ($check['total'] < '1') $products_delete[$key] = $key;					
				}				
				// Removing categories can be a lengthy process
				tep_set_time_limit(0);
				for ($i = 0, $n = sizeof($categories); $i < $n; $i++) 
					tep_remove_category($categories[$i]['id']);
				reset($products_delete);
				while (list($key) = each($products_delete)) 
					tep_remove_product($key);												
				}  // main if closing bracket
				if($categories_id!=$cInfo->parent_id) $parent_id=$cInfo->parent_id;
				echo 'delete_category_success^'.$categories_id;
				tep_reset_seo_cache('products');
	 }else if($action=='move_category'){
			$contents='<table width="100%"  border="0" cellspacing="5" cellpadding="5" class="openContent"><tr><td width="10" class="main">'.tep_image('images/template/img_move.gif').'</td><td class="main" colspan="4"><b>' . sprintf(TEXT_MOVE_CATEGORIES_INTRO, $cInfo->categories_name) . '</b></td></tr><tr>';
			$contents.='<tr><td></td><td class="main" align="left">' . sprintf(TEXT_MOVE, $cInfo->categories_name) . '&nbsp;' . tep_draw_pull_down_menu('move_to_category_id', tep_get_category_tree('0', '',$cID), $current_category_id).'</td></tr>';
			$contents.='<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif', '1', '10').'</td></tr></table>';						
			echo $contents;
	 }else if($action=='move_category_confirm'){
			if ($cID  && ($cID != $HTTP_POST_VARS['move_to_category_id'])){
				$cID=tep_db_prepare_input($HTTP_GET_VARS['cID']);
				$new_parent_id = tep_db_prepare_input($HTTP_POST_VARS['move_to_category_id']);
				
          		$path = explode('_', tep_get_generated_category_path_ids($new_parent_id));
				
			    if (in_array($cID, $path)) {
				 echo 'move_category_failure^'.$cID;
			    } else {
				tep_db_query("update " . TABLE_CATEGORIES . " set parent_id = '" . tep_db_input($new_parent_id) . "', last_modified = '" . $server_date . "' where categories_id = '" . tep_db_input($cID) . "'");
				echo 'move_category_success^'.$new_parent_id;
			   }	
			}  // top if closing bracket	
	 }else if($action=='delete_product'){
		 	$product_categories_string.="<table border='0' cellspacing='2' cellpadding='1' width='100%'><tr><td width='17' align='center'>".tep_image('images/template/img_trash.gif').'</td>';
		 	$product_categories_string.='<td width="500" align="left" class="inner_title">'.TEXT_INFO_HEADING_DELETE_PRODUCT . '</td></tr>';
		 	$product_categories_string.='<tr><td></td><td width="500" align="left" class="main">'.TEXT_DELETE_PRODUCT_INTRO. '</td></tr>';
		 	$product_categories_string.='<tr><td></td><td  width="500" align="left" class="main"><b>'.$pInfo->products_name. '</b></td></tr>';				      
		 	$product_categories_string.='<tr><td></td><td width="500" align="left" class="main"><b>';
	        $product_categories = tep_generate_category_path($pInfo->products_id, 'product');
	        if($pInfo->product_type=='S') $product_categories_string.='</b>'.DELETE_ERR_SOFTWARE_PACK_EXITS;
	        else {
		        for ($i = 0, $n = sizeof($product_categories); $i < $n; $i++) {
		          $category_path = '';
		          for ($j=0, $k = sizeof($product_categories[$i]); $j < $k; $j++)
		            $category_path .= $product_categories[$i][$j]['text'] . '&nbsp;&gt;&nbsp;';		          
		          $category_path = substr($category_path, 0, -16);
		          $product_categories_string .= tep_draw_checkbox_field('product_categories[]', $product_categories[$i][sizeof($product_categories[$i])-1]['id'], true) . '&nbsp;' . $category_path . '<br>';
		        }
		        $product_categories_string = substr($product_categories_string, 0, -4);
	        }
	        $product_categories_string.='</b></td></tr>';
			$product_categories_string.='<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif',50,10).'</td></tr></table>';
	        echo $product_categories_string;     
		tep_reset_seo_cache('products');          
	 	}else if($action=='delete_confirm'){			
			 $HTTP_POST_VARS['products_id']=$HTTP_GET_VARS['pID'];
		 	 if (isset($HTTP_POST_VARS['products_id']) && isset($HTTP_POST_VARS['product_categories']) && is_array($HTTP_POST_VARS['product_categories'])) {
				  tep_set_time_limit(100);
		          $product_id = tep_db_prepare_input($HTTP_POST_VARS['products_id']);
		          $product_categories = $HTTP_POST_VARS['product_categories'];		
		          for ($i=0, $n=sizeof($product_categories); $i<$n; $i++) {
		            tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$pID . "' and categories_id = '" . (int)$product_categories[$i] . "'");
		          }		
		          $product_categories_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$pID . "'");
		          $product_categories = tep_db_fetch_array($product_categories_query);		          	
		          if ($product_categories['total'] == '0') tep_remove_product($pID);		            		         
		          echo 'delete_success^' . $HTTP_GET_VARS['cID'];
			}
		tep_reset_seo_cache('products');
		}else if($action=='move_product'){			
			$move_to .="<table border='0' cellspacing='2' cellpadding='2' width='100%'><tr><td width='10' align='center'>".tep_image('images/template/img_move.gif').'</td>';
			$move_to .='<td width="500" align="left" class="inner_title">'.TEXT_INFO_HEADING_MOVE_PRODUCT . '</td></tr>';  
			$move_to .='<tr><td></td><td width="500" align="left" class="main">'. TEXT_INFO_CURRENT_CATEGORIES.'&nbsp;&nbsp;'.'<b>'.tep_output_generated_category_path($pInfo->products_id, 'product'). '</b></td></tr>';
			$move_to .='<tr><td></td><td width="500" align="left" class="main">'. sprintf(TEXT_MOVE_PRODUCTS_INTRO, $pInfo->products_name). '</td></tr>'; 
			$move_to .= '<tr><td></td><td width="500" align="left" class="main">'. TEXT_MOVE_TO.tep_draw_separator('pixel_trans.gif',50,10) . tep_draw_pull_down_menu('move_to_category_id', tep_get_category_tree('0','','0'), $current_category_id). '</td></tr>';
			$move_to .= '<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif',50,10).'</td></tr></table>';;
			echo $move_to;
		}else if($action=='move_confirm'){
			$products_id = tep_db_prepare_input($HTTP_GET_VARS['pID']);
        	$new_parent_id = tep_db_prepare_input($HTTP_POST_VARS['move_to_category_id']);
			$current_category_id = $cPath;			
        	$duplicate_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$products_id . "' and categories_id = '" . (int)$new_parent_id . "'");
        	$duplicate_check = tep_db_fetch_array($duplicate_check_query);
        	if ($duplicate_check['total'] < 1) tep_db_query("update " . TABLE_PRODUCTS_TO_CATEGORIES . " set categories_id = '" . (int)$new_parent_id . "' where products_id = '" . (int)$products_id . "' and categories_id = '" . (int)$current_category_id . "'");
        	echo 'move_success^'.$new_parent_id;        	
		}else if($action=='attrib'){
			$sql = "select products_id,products_name from products_description where language_id='" . $languages_id . "' order by products_name";
			$result = tep_db_query($sql);
  			$make_copy_from_products_array[] = array("id"=>-1,"text"=>" -- " . TEXT_ALL_PRODUCTS . " -- ");
			while($row = tep_db_fetch_array($result)) if($row['products_name']) $make_copy_from_products_array[] = array("id"=>$row['products_id'],"text"=>$row['products_name']);				 			 			
			$attributes_copy .="<table border='0' cellspacing='2' cellpadding='2' width='100%'><tr><td width='17' align='center'>".tep_image('images/template/img_attrib.gif').'</td>'; 
			$attributes_copy .= '<td width="500" align="left" class="inner_title">'.TEXT_INFO_HEADING_ATTRIBUTES . '</td></tr>';
			$attributes_copy .= '<tr><td></td><td width="500" align="left" class="main">'. TEXT_COPY_ATTRIBUTES_FROM .'&nbsp;&nbsp;' . $pInfo->products_name. '</td></tr>';
			$attributes_copy .= '<tr><td></td><td width="500" align="left" class="main">'. TEXT_COPY_ATTRIBUTES_TO .'&nbsp;&nbsp;' . tep_draw_pull_down_menu('copy_to_products_id', $make_copy_from_products_array). '</td></tr>';
			$attributes_copy .= '<tr><td></td><td width="500" align="left" class="main">'. TEXT_COPY_ATTRIBUTES_INTRO .'&nbsp;&nbsp;' . tep_draw_checkbox_field('copy_attributes_delete_first',$copy_attributes_delete_first,true,'' ,'size="2" onclick="javascript:show_attributes(this)"') . '</td></tr>';
			$attributes_copy .= '<tr><td></td><td width="500" align="left" class="main" style="display:none" id="dublicate">'. TEXT_ATTRIBUTES_DUPLICATE . tep_draw_separator('pixel_trans.gif',40,10) . tep_draw_checkbox_field('copy_attributes_duplicates_skipped',$copy_attributes_duplicates_skipped, 'size="2"') .'be skipped'. tep_draw_separator('pixel_trans.gif',35,10) . tep_draw_checkbox_field('copy_attributes_duplicates_overwrite',$copy_attributes_duplicates_overwrite, 'size="2"') . 'be overwritten' . '</td></tr>';
			$attributes_copy .= '<tr><td></td><td width="500" align="left" class="main" style="display:none" id="download">'. TEXT_ATTRIBUTES_DOWNLOADS .'&nbsp;&nbsp;&nbsp;&nbsp;' . tep_draw_checkbox_field('copy_attributes_include_downloads',$copy_attributes_include_downloads, 'size="2"') . '</td></tr>';
			$attributes_copy .= '<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif',50,10).'</td></tr>';
			echo $attributes_copy;
		}else if($action=='attrib_confirm'){			
			$copy_to_products_id = tep_db_prepare_input($HTTP_POST_VARS['copy_to_products_id']);			
			tep_copy_products_attributes($pID,$copy_to_products_id);
			echo 'attrib_success';
		}else if($action=='copy_product'){	 		
	 		$copy_to .='<table  border="0" cellpadding="2" cellspacing="2" width="100%"><tr><td width="25" align="center">'.tep_image('images/template/img_copy.gif').'</td>';
			$copy_to .= '<td width="500" align="left" class="inner_title">'.TEXT_INFO_HEADING_COPY_PRODUCT . '</td><td></td></tr>'; 
			$copy_to .= '<tr><td></td><td width="500" align="left" class="main">'.TEXT_INFO_CURRENT_CATEGORIES .'&nbsp;&nbsp;'.'<b>'.tep_output_generated_category_path($pInfo->products_id, 'product'). '</b></td><td></td></tr>';
			$copy_to .= '<tr><td></td><td colspan="2" align="left" class="main">'.TEXT_INFO_COPY_TO_INTRO. '</td><td></td></tr>'; 
			$copy_to .= '<tr><td></td><td width="500" align="left" class="main">'.TEXT_COPY_TO .tep_draw_separator('pixel_trans.gif',50,10) . tep_draw_pull_down_menu('categories_id', tep_get_category_tree('0','','0'), $current_category_id).  '</td><td></td></tr>';	
			$copy_to .= '<tr><td></td><td width="500" align="left" class="main">'.TEXT_HOW_TO_COPY . tep_draw_separator('pixel_trans.gif',52,10) . tep_draw_radio_field('copy_as', 'link', true,'','onclick="javascript:show_duplicate(\'link\');"') . ' ' . TEXT_COPY_AS_LINK . '<br>'.tep_draw_separator('pixel_trans.gif','90','10') .'&nbsp;&nbsp;'. tep_draw_radio_field('copy_as', 'duplicate','','','onclick="javascript:show_duplicate(\'dup\');"') . ' ' . TEXT_COPY_AS_DUPLICATE . '</td><td class="main" style="display:none" id="dub_prd" ><br>'.TEXT_COPY_ATTRIBUTES.'&nbsp;'.tep_draw_radio_field('copy_attributes', 'copy_attributes_yes', true) . ' ' . TEXT_COPY_ATTRIBUTES_YES.'&nbsp;' .tep_draw_radio_field('copy_attributes', 'copy_attributes_no') . ' ' . TEXT_COPY_ATTRIBUTES_NO.'</td></tr>'; 
			$copy_to .='<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif',50,10).'</td></tr></table>';
			echo $copy_to;
		}else if($action=='copy_confirm'){
			$products_id =$HTTP_GET_VARS['pID'];
			$error="";						
			$categories_id = tep_db_prepare_input($HTTP_POST_VARS['categories_id']);
			if ($HTTP_POST_VARS['copy_as'] == 'link') {
            if ($categories_id != $current_category_id) {
              $check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$products_id . "' and categories_id = '" . (int)$categories_id . "'");
              $check = tep_db_fetch_array($check_query);
              if ($check['total'] < '1') {
                tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . (int)$products_id . "', '" . (int)$categories_id . "')");
              }
            } else {
              $error=ERROR_CANNOT_LINK_TO_SAME_CATEGORY;
            }
          } elseif ($HTTP_POST_VARS['copy_as'] == 'duplicate') {
								  // This is for Diveabout
            $product_query = tep_db_query("select products_quantity, author_name, products_model,products_sku, products_price, products_date_available, products_weight, products_sort_order, products_status, products_tax_class_id,is_attributes, products_price_break,manufacturers_id,products_image_1,products_image_2,products_image_3" .
										  ",products_image_4,products_image_5,products_title_1,products_title_2,products_title_3,products_title_4,products_title_5,restrict_to_groups,restrict_to_customers,product_type,is_subscription,product_mode,download_last_date,downloads_per_customer,download_link from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
            $product = tep_db_fetch_array($product_query);
			

           //tep_db_query("insert into " . TABLE_PRODUCTS . " (products_quantity, products_model,products_image, products_price, products_date_added, products_date_available, products_weight, products_status, products_tax_class_id, manufacturers_id) values ('" . tep_db_input($product['products_quantity']) . "', '" . tep_db_input($product['products_model']) . "', '" . tep_db_input($product['products_image_1']) . "', '" . tep_db_input($product['products_price']) . "',  now(), '" . tep_db_input($product['products_date_available']) . "', '" . tep_db_input($product['products_weight']) . "', '0', '" . (int)$product['products_tax_class_id'] . "', '" . (int)$product['manufacturers_id'] . "')");
			 tep_db_query("insert into " . TABLE_PRODUCTS . " (products_quantity, author_name,products_model,products_sku,products_price, products_date_added, is_attributes,products_price_break,products_date_available, products_weight,products_sort_order, products_status, products_tax_class_id, manufacturers_id" .
			 				",products_image_1,products_image_2,products_image_3,products_image_4,products_image_5,products_title_1,products_title_2,products_title_3,products_title_4,products_title_5,restrict_to_groups,restrict_to_customers,product_type,is_subscription,product_mode,download_last_date,downloads_per_customer,download_link) values ('" . tep_db_input($product['products_quantity']) . "', '" .tep_db_input($product['author_name']) . "', '". tep_db_input($product['products_model']) . "', '" .tep_db_input($product['products_sku'])."', '". tep_db_input($product['products_price']) . "','" . $server_date . "','" .tep_db_input($product['is_attributes'])."','" . tep_db_input($product['products_price_break'])."', " .
							(empty($product['products_date_available']) ? "null" : "'" . tep_db_input($product['products_date_available']) . "'") . ", '" . tep_db_input($product['products_weight']) . "','".tep_db_input($product['products_sort_order'])."', '" . tep_db_input($product['products_status']) . "', '" . (int)$product['products_tax_class_id'] . "', '" . (int)$product['manufacturers_id'] . "','".
							tep_db_input($product["products_image_1"]) . "','" . tep_db_input($product["products_image_2"]) . "','" . tep_db_input($product["products_image_3"])  . "','" . tep_db_input($product["products_image_4"]) . "','" . tep_db_input($product["products_image_5"]) . "','" .
							tep_db_input($product["products_title_1"]) . "','" . tep_db_input($product["products_title_2"]) . "','" . tep_db_input($product["products_title_3"])  . "','" . tep_db_input($product["products_title_4"]) . "','" . tep_db_input($product["products_title_5"]) . "','" . tep_db_input($product["restrict_to_groups"]) ."','" . tep_db_input($product["restrict_to_customers"])."','" . tep_db_input($product["product_type"]) ."','" . tep_db_input($product["is_subscription"]). 
							"','" . tep_db_input($product["product_mode"]) . "','" . tep_db_input($product["download_last_date"]) . "','" . tep_db_input($product["downloads_per_customer"]) . "','" . tep_db_input($product["download_link"]) . "')");
		    $dup_products_id = tep_db_insert_id();
		    if($product['product_type']=='S'){
		    	$product_support_query=tep_db_query("select subscription_id,no_users from ".TABLE_PRODUCTS_TO_SUPPORT_PACKS." where product_id='".(int)$products_id."'");
		    	if(tep_db_num_rows($product_support_query)>0){
		    		while($product_support=tep_db_fetch_array($product_support_query))
						tep_db_query("insert into " . TABLE_PRODUCTS_TO_SUPPORT_PACKS . " (product_id, subscription_id, no_users) values ('" . (int)$dup_products_id . "', '" . (int)$product_support['subscription_id'] . "', '" . $product_support['no_users']."')");		    		
		    	}
		    }
		    if($product['is_subscription']=='Y'){
				$product_is_subscription_query=tep_db_query("select subscription_id from ".TABLE_PRODUCTS_TO_SUBSCRIPTIONS." where products_id='".(int)$products_id."'");
				if(tep_db_num_rows($product_is_subscription_query)>0){
					while($product_is_subscription=tep_db_fetch_array($product_is_subscription_query))
						tep_db_query("insert into " . TABLE_PRODUCTS_TO_SUBSCRIPTIONS . " (products_id, subscription_id) values ('" . (int)$dup_products_id . "', '" . $product_is_subscription['subscription_id']."')");					
				}						    		
		    }
		    
            $description_query = tep_db_query("select language_id, products_name, products_description, products_url from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$products_id . "'");
            while ($description = tep_db_fetch_array($description_query)) 
              tep_db_query("insert into " . TABLE_PRODUCTS_DESCRIPTION . " (products_id, language_id, products_name, products_description, products_url, products_viewed) values ('" . (int)$dup_products_id . "', '" . (int)$description['language_id'] . "', '" . tep_db_input($description['products_name']) . "', '" . tep_db_input($description['products_description']) . "', '" . tep_db_input($description['products_url']) . "', '0')");
			if($product['products_price_break']=='Y'){
				$price_break_query=tep_db_query("select * from " . TABLE_PRODUCTS_PRICE_BREAK . " where products_id='" . (int)$products_id . "'");
				while($price_break_result=tep_db_fetch_array($price_break_query)){
					tep_db_query("insert into " . TABLE_PRODUCTS_PRICE_BREAK . " (products_id,quantity,discount_per_item) values('" . (int)$dup_products_id . "','" . $price_break_result['quantity'] . "','" .$price_break_result['discount_per_item'] . "')");
				}
			}
            tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . (int)$dup_products_id . "', '" . (int)$categories_id . "')");
// BOF: WebMakers.com Added: Attributes Copy on non-linked
            $products_id_from=tep_db_input($products_id);
            $products_id_to= $dup_products_id;
            $products_id = $dup_products_id;
			if ( $HTTP_POST_VARS['copy_attributes']=='copy_attributes_yes' && $HTTP_POST_VARS['copy_as'] == 'duplicate' ) {
// WebMakers.com Added: Copy attributes to duplicate product
            $copy_attributes_delete_first='1';
            $copy_attributes_duplicates_skipped='1';
            $copy_attributes_duplicates_overwrite='0';

            if (DOWNLOAD_ENABLED == 'true') {
              $copy_attributes_include_downloads='1';
              $copy_attributes_include_filename='1';
            } else {
              $copy_attributes_include_downloads='0';
              $copy_attributes_include_filename='0';
            }
            tep_copy_products_attributes($products_id_from,$products_id_to);
			// 	EOF: WebMakers.com Added: Attributes Copy on non-linked
			}
          }
          	if($error!="") echo 'copy_error^'.$error;
		  	else echo "copy_success^".$categories_id;
		  }else if($action=='edit_product') product_new_edit();
		  else if($action=='update_product'){
				  $products_id=$pID;
		     	  $sort_id=$pInfo->products_sort_order;
		     	  if(!$products_id || !$sort_id){
			     	  $sort_id=1;
			     	  $max_sql = "select max(products_sort_order) as max_total from products ";
				      $max_query = tep_db_query($max_sql);
				      if(tep_db_num_rows($max_query)>0){
						$max_total = tep_db_fetch_array($max_query);				
						$sort_id=($max_total['max_total']+1);							
				      }		 					      	   
		     	  }
		 	 	  //PR Algozone: copy image only if modified
		          $products_date_available = tep_convert_date_raw(tep_db_prepare_input($HTTP_POST_VARS['products_date_available']));
				  $products_date_available = (getServerDate() < $products_date_available) ? $products_date_available : getServerDate(); 				  
				  $products_date_available = (date('Y-m-d') < $products_date_available) ? $products_date_available : date('Y-m-d');
				  $pro_sku="";
				  $is_attributes=(($HTTP_POST_VARS['is_attributes']!="")?'Y':'N');				  				
				  $is_subscription=isset($HTTP_POST_VARS['is_subscription'])?$HTTP_POST_VARS['is_subscription']:'N';
				  $is_price_break=isset($HTTP_POST_VARS['products_price_break'])?$HTTP_POST_VARS['products_price_break']:'N';
				  $product_type=isset($HTTP_POST_VARS['product_type'])?$HTTP_POST_VARS['product_type']:'G';
				  $authour_name="";
				  if($is_attributes=='N') $pro_sku=tep_db_prepare_input($HTTP_POST_VARS['products_sku']);
				  if($product_type=='B') $authour_name=tep_db_prepare_input($HTTP_POST_VARS['author_name']);				  
				  if($HTTP_POST_VARS['product_mode']=="V")
				  {
		            $download_date = tep_convert_date_raw(tep_db_prepare_input($HTTP_POST_VARS['download_date']));
				    $download_date = (getServerDate() < $download_date) ? $download_date : getServerDate(); 				  
					$download_date = (date('Y-m-d') < $download_date) ? $download_date : date('Y-m-d');
					$download_count=$HTTP_POST_VARS['download_count'];
					$product_weight=0;
				  }
				  else
				  {
					  $download_date=$download_count="";
					  $product_weight=(tep_db_prepare_input($HTTP_POST_VARS['products_weight'])!='')?tep_db_prepare_input($HTTP_POST_VARS['products_weight']):0	;
				  }
				  //get restrict groups values
					  $restrict_to_groups_string="";
					  if($HTTP_POST_VARS['restrict_to_groups']){
					  	 $selected_groupids=$HTTP_POST_VARS['restrict_to_groups'];
					  	 if(count($selected_groupids)>0){					  	   
					  	   $cnt_g=0;
					  	   foreach ($selected_groupids as $groups_id){
					  	 	 $restrict_to_groups_string.=(($cnt_g==0)?',':'').$groups_id.",";
					  	 	 $cnt_g++;
					  	   }				  	   
					  	 }
					  }
				  //get restrict groups values
				  
				  //get restrict customers values
					  $restrict_to_customers_string="";
					  if($HTTP_POST_VARS['restrict_to_customers']){
					  	 $selected_customersids=$HTTP_POST_VARS['restrict_to_customers'];
					  	 if(count($selected_customersids)>0){
					  	   $cnt_c=0;
					  	   foreach ($selected_customersids as $customers_id){
					  	 	 $restrict_to_customers_string.=(($cnt_c==0)?',':'').$customers_id.",";
					  	 	 $cnt_c++;
					  	   }
					  	 }
					  }
				  //get restrict customers values
				  $sql_data_array = array('products_quantity' => (tep_db_prepare_input($HTTP_POST_VARS['products_quantity'])!='')?tep_db_prepare_input($HTTP_POST_VARS['products_quantity']):0,
		                                  'products_model' => tep_db_prepare_input($HTTP_POST_VARS['products_model']),
										  'author_name' => $authour_name,
										  'products_sku' =>$pro_sku,
		                                  'products_price' => tep_db_prepare_input($HTTP_POST_VARS['products_price']),
		                                  'products_date_available' =>$products_date_available,
		                                  'products_weight' => $product_weight,
		                                  'products_status'=>(int)tep_db_prepare_input($HTTP_POST_VARS['products_status']),										  
		                                  'products_tax_class_id' => tep_db_prepare_input($HTTP_POST_VARS['products_tax_class_id']),
		                                  'manufacturers_id' => (tep_db_prepare_input($HTTP_POST_VARS['manufacturers_id'])!='')?tep_db_prepare_input($HTTP_POST_VARS['manufacturers_id']):0,
										  'is_attributes'=> $is_attributes,
										  'is_subscription'=> $is_subscription,
										  'products_sort_order'=>$sort_id,
										  'restrict_to_groups'=>$restrict_to_groups_string,
										  'restrict_to_customers'=>$restrict_to_customers_string,
										  'product_type'=>$product_type,
										  'download_last_date'=>$download_date,
										  'downloads_per_customer'=>$download_count,
										  'product_mode'=>$HTTP_POST_VARS['product_mode'],
										  'products_price_break'=>$is_price_break
										  );								
				   for ($icnt=1;$icnt<=5;$icnt++)
					$sql_data_array['products_title_' . $icnt] = tep_db_prepare_input($HTTP_POST_VARS["products_title_" . $icnt]);				  
		            $update_sql_data = array('products_last_modified' => $server_date);
		            $sql_data_array = array_merge($sql_data_array, $update_sql_data);
		            if(!$HTTP_GET_VARS['pID']){
			            $insert_sql_data = array('products_date_added' => $server_date);
			            $sql_data_array = array_merge($sql_data_array, $insert_sql_data);
			            tep_db_perform(TABLE_PRODUCTS, $sql_data_array);
			            $products_id = tep_db_insert_id();
		            } else if($HTTP_GET_VARS['pID']>0) tep_db_perform(TABLE_PRODUCTS, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");		
					#PR Algozone: delete categories saved in the tables
					tep_db_query("delete from " . TABLE_PRODUCTS_TO_SUBSCRIPTIONS . " where products_id='" . $products_id . "' ");
					tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '". $products_id . "'");
					tep_db_query("delete from ". TABLE_PRODUCTS_STOCK . " where products_id='".$products_id."'");
					tep_db_query("delete from " . TABLE_PRODUCTS_PRICE_BREAK . " where products_id = '". (int)$products_id . "'");
					tep_db_query("delete from " . TABLE_PRODUCTS_TO_SUPPORT_PACKS . " where product_id = '". (int)$products_id . "'");
					//get product to support pack details
					  if($HTTP_POST_VARS['product_to_subscripiton']!="" && $product_type=='S'){
					  	   $product_to_subscription_split=split("/",$HTTP_POST_VARS['product_to_subscripiton']);
					  	   $n=count($product_to_subscription_split);
					  	   for($i=0;$i<$n;$i++){
								$split_subscription=$product_to_subscription_split[$i];
								if($split_subscription!=""){
									$split_subscription_split=split(",",$split_subscription);
									$n_cnt=count($split_subscription_split)-1;
									for($j=0;$j<$n_cnt;$j++)
									  if($split_subscription_split[$j] && $split_subscription_split[$n_cnt]>0) tep_db_query("insert into ".TABLE_PRODUCTS_TO_SUPPORT_PACKS." (product_id,subscription_id,no_users) values('".$products_id."','".$split_subscription_split[$j]."','".$split_subscription_split[$n_cnt]."')");
								}
					  	   }
					  }
					//get product to support pack details
					if(($is_price_break=='Y') && $HTTP_POST_VARS['price_break_details']) { 
						$price_break_details=(isset($HTTP_POST_VARS['price_break_details'])?$HTTP_POST_VARS['price_break_details']:'');
						if($price_break_details!="") {
							$price_break_array=split('/#/',$price_break_details);
							for($jcnt=0;$jcnt< (count($price_break_array)-1);$jcnt++){
								$break_array=split('{#{}#}',$price_break_array[$jcnt]);
								tep_db_query("insert into " . TABLE_PRODUCTS_PRICE_BREAK . " (products_id,quantity,discount_per_item) values('" . (int)$products_id. "','" . $break_array[0] . "','" . $break_array[1] . "')");
							}
						}					
				    }
				  if($is_attributes=='Y'){
					$attributes_ids=(isset($HTTP_POST_VARS['attributes_array'])?$HTTP_POST_VARS['attributes_array']:'');
					if($attributes_ids!=''){ 
						$attributes_array=split('\^',$attributes_ids);
						for($bcnt=0;$bcnt<count($attributes_array);$bcnt++){	
							$attribute_split=split("\#",$attributes_array[$bcnt]);
							tep_db_query("insert into " .TABLE_PRODUCTS_STOCK . " (products_id,attributes_id,products_quantity,sku,attribute_status) values('" .$products_id."','".$attribute_split[1]."','".$attribute_split[2]."','".$attribute_split[0]."','" . $attribute_split[3] . "')");
						  }
					}
				 }
		 		# create loop here to insert rows for multiple categories
		 		$differ_category="";
		 		$differ_category_found=true;
				$selected_catids = $HTTP_POST_VARS['categories_ids'];		
				if ($selected_catids && count($selected_catids)>0)
				{
					tep_db_query("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '". $products_id . "'");
					foreach ($selected_catids as $current_category_id)
					 {
						if($HTTP_GET_VARS['cID']==$current_category_id) $differ_category_found=false;
						if($HTTP_GET_VARS['cID']!=$current_category_id) $differ_category=$current_category_id;
						if($current_category_id) tep_db_query("insert into " . TABLE_PRODUCTS_TO_CATEGORIES . " (products_id, categories_id) values ('" . $products_id . "', '" . $current_category_id . "')");		
					 }
				}
		
		          $languages = tep_get_languages();
		          for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
		            $language_id = $languages[$i]['id'];
		            $sql_data_array = array('products_name' => tep_db_prepare_input($HTTP_POST_VARS['products_name'][$language_id]),
		                                    'products_description' => stripslashes(tep_db_prepare_input($HTTP_POST_VARS['products_description'][$language_id])),
		                                    'products_url' => tep_db_prepare_input($HTTP_POST_VARS['products_url'][$language_id]));
					if(!$HTTP_GET_VARS['pID']){						
		              $insert_sql_data = array('products_id' => $products_id,
		                                       'language_id' => $language_id);
		              $sql_data_array = array_merge($sql_data_array, $insert_sql_data);
		              tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array);            
					}else if ($HTTP_GET_VARS['pID']>0) tep_db_perform(TABLE_PRODUCTS_DESCRIPTION, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "' and language_id = '" . (int)$language_id . "'");            
		          }
				  if($is_subscription=='Y') {
				  	$subscription_categories_array_selected=$HTTP_POST_VARS['subscription_categories_ids'];
				  	if ($subscription_categories_array_selected)
					  foreach ($subscription_categories_array_selected as $current_sub_id)
						 tep_db_query("insert into " . TABLE_PRODUCTS_TO_SUBSCRIPTIONS . " (products_id, subscription_id) values ('" . (int)$products_id . "', '" . (int)$current_sub_id . "')");													
				  }
			//==========================================================
			// Update Product Attributes
				  $rows = 0;
				  $options_query = tep_db_query("select products_options_id, products_options_name from " . TABLE_PRODUCTS_OPTIONS . " where language_id = '" . $languages_id . "' order by products_options_sort_order");
				  while ($options = tep_db_fetch_array($options_query)) {
					$values_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p where pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" . $options['products_options_id'] . "' and pov.language_id = '" . $languages_id . "'");
					while ($values = tep_db_fetch_array($values_query)) {
					  $rows ++;
				// original    $attributes_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $products_id . "' and options_id = '" . $options['products_options_id'] . "' and options_values_id = '" . $values['products_options_values_id'] . "'");
					  $attributes_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix, products_options_sort_order, product_attributes_one_time, products_attributes_weight, products_attributes_weight_prefix, products_attributes_units, products_attributes_units_price from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $products_id . "' and options_id = '" . $options['products_options_id'] . "' and options_values_id = '" . $values['products_options_values_id'] . "'");
					  if (tep_db_num_rows($attributes_query) > 0) {
						$attributes = tep_db_fetch_array($attributes_query);
						
						if ($HTTP_POST_VARS['option'][$rows]) {
						  if ( ($HTTP_POST_VARS['prefix'][$rows] <> $attributes['price_prefix']) || ($HTTP_POST_VARS['price'][$rows] <> $attributes['options_values_price']) || ($HTTP_POST_VARS['products_options_sort_order'][$rows] <> $attributes['products_options_sort_order']) || ($HTTP_POST_VARS['product_attributes_one_time'][$rows] <> $attributes['product_attributes_one_time']) || ($HTTP_POST_VARS['products_attributes_weight'][$rows] <> $attributes['products_attributes_weight']) ||  ($HTTP_POST_VARS['products_attributes_weight_prefix'][$rows] <> $attributes['products_attributes_weight_prefix']) || ($HTTP_POST_VARS['products_attributes_units'][$rows] <> $attributes['products_attributes_units']) || ($HTTP_POST_VARS['products_attributes_units_price'][$rows] <> $attributes['products_attributes_units_price']) ) {
							tep_db_query("update " . TABLE_PRODUCTS_ATTRIBUTES . " set options_values_price = '" . $HTTP_POST_VARS['price'][$rows] . "', price_prefix = '" . $HTTP_POST_VARS['prefix'][$rows] . "', products_options_sort_order = '" . $HTTP_POST_VARS['products_options_sort_order'][$rows] . "', product_attributes_one_time = '" . $HTTP_POST_VARS['product_attributes_one_time'][$rows] . "', products_attributes_weight = '" . $HTTP_POST_VARS['products_attributes_weight'][$rows] . "',  products_attributes_weight_prefix = '" . $HTTP_POST_VARS['products_attributes_weight_prefix'][$rows] . "', products_attributes_units = '" . $HTTP_POST_VARS['products_attributes_units'][$rows] . "', products_attributes_units_price = '" . $HTTP_POST_VARS['products_attributes_units_price'][$rows] . "' where products_attributes_id = '" . $attributes['products_attributes_id'] . "'");
						  }
						} else {
						  tep_db_query("delete from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_attributes_id = '" . $attributes['products_attributes_id'] . "'");
						}
					  } elseif ($HTTP_POST_VARS['option'][$rows]) {
						tep_db_query("insert into " . TABLE_PRODUCTS_ATTRIBUTES . "  values ('', '" . $products_id . "', '" . $options['products_options_id'] . "', '" . $values['products_options_values_id'] . "', '" . $HTTP_POST_VARS['price'][$rows] . "', '" . $HTTP_POST_VARS['prefix'][$rows] . "', '" . $HTTP_POST_VARS['products_options_sort_order'][$rows] . "', '" . $HTTP_POST_VARS['product_attributes_one_time'][$rows] . "', '" . $HTTP_POST_VARS['products_attributes_weight'][$rows] . "', '" . $HTTP_POST_VARS['products_attributes_weight_prefix'][$rows] . "', '" . $HTTP_POST_VARS['products_attributes_units'][$rows] . "', '" . $HTTP_POST_VARS['products_attributes_units_price'][$rows] . "' )");
					  }
					}
				  }
		  //=====================================================
	          if($differ_category_found) echo 'diff_category^'.$differ_category.'#';
	          else if($HTTP_POST_VARS['action_type']=='new') echo $HTTP_POST_VARS['action_type'].'_product^';
	          echo_get_details();
		 }
    exit;  
 }
  function product_new_edit(){
  	global $languages_id,$pID,$cID,$check_ID,$languages,$actions,$HTTP_GET_VARS,$currencies;  		
	$product_query = tep_db_query("select pd.products_id,pd.products_name, pd.products_description, pd.products_url, p.products_quantity, p.is_attributes,p.products_price_break,p.author_name,p.products_model,p.products_sku,
		  								 p.products_image_1,p.products_image_2,p.products_image_3,p.products_image_4,p.products_image_5,p.products_title_1,p.products_title_2,
										 p.products_title_3,p.products_title_4,p.products_title_5,p.products_price, p.products_weight, p.products_sort_order, date_format(p.products_date_added,'%Y-%m-%d') as products_date_added, date_format(p.products_last_modified,'%Y-%m-%d') as products_last_modified, date_format(p.products_date_available, '%Y-%m-%d') as products_date_available, p.products_status, p.products_tax_class_id, p.manufacturers_id,p.is_subscription,p.restrict_to_groups,p.restrict_to_customers,p.product_type,p.product_mode,p.download_link,p.downloads_per_customer,p.download_last_date from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$pID . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'");
			$product = tep_db_fetch_array($product_query);
			if(tep_db_num_rows($product_query)>0) $pInfo = new objectInfo($product);
			$manufacturers_array = array(array('id' => '', 'text' => TEXT_NONE));
    		$manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS . " order by manufacturers_name");
			while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
			  $manufacturers_array[] = array('id' => $manufacturers['manufacturers_id'],
											 'text' => $manufacturers['manufacturers_name']);
			}			
			//get customers groups list
			  $customer_groups_list=array();
			  $customer_group_query=tep_db_query('select customers_groups_id,customers_groups_name from '.TABLE_CUSTOMERS_GROUPS);
			  if(tep_db_num_rows($customer_group_query)>0)
			  	while($customer_group=tep_db_fetch_array($customer_group_query))
			  		if($customer_group['customers_groups_name']!="") $customer_groups_list[]=array('id'=>$customer_group['customers_groups_id'],'text'=>$customer_group['customers_groups_name']);
			 $get_selected_groups=array();
			 if($product['restrict_to_groups']!="") {
				$get_selected_groups_string=substr($product['restrict_to_groups'],1,-1);				
				if($get_selected_groups_string!="") $get_selected_groups_string_split=split(",",$get_selected_groups_string);
				if(is_array($get_selected_groups_string_split) && count($get_selected_groups_string_split)>0){
					$n=count($get_selected_groups_string_split);
					for($i=0;$i<$n;$i++) $get_selected_groups[]=array('id'=>$get_selected_groups_string_split[$i]); 
				}			 		
			 }
			//get customers group list
			//get customers list
			  $customers_list=array();
			  $customer_query=tep_db_query("select customers_id,concat(customers_lastname,' ',customers_firstname) as customer_name from ".TABLE_CUSTOMERS);
			  if(tep_db_num_rows($customer_query)>0)
			  	while($customers=tep_db_fetch_array($customer_query))
			  		if($customers['customer_name']!="") $customers_list[]=array('id'=>$customers['customers_id'],'text'=>$customers['customer_name']);
			 $get_selected_customers=array();
			 if($product['restrict_to_customers']!="") {
				$get_selected_customers_string=substr($product['restrict_to_customers'],1,-1);
				if($get_selected_customers_string!="") $get_selected_customers_string_split=split(",",$get_selected_customers_string);				
				if(is_array($get_selected_customers_string_split) && count($get_selected_customers_string_split)>0){
					$n=count($get_selected_customers_string_split);
					for($i=0;$i<$n;$i++) $get_selected_customers[]=array('id'=>$get_selected_customers_string_split[$i]); 
				}			 		
			 }		
			
			 if($product['products_price_break']=='Y') {
			 	$break_query=tep_db_query("select * from " . TABLE_PRODUCTS_PRICE_BREAK . " where products_id='" .(int)$pID . "' order by quantity");
			 	while($break_result=tep_db_fetch_array($break_query))
					$break_array[]=array('id'=>$break_result['quantity'] . '{#{}#}' . $break_result['discount_per_item'], 'text'=>$break_result['quantity'] . ' for ' .tep_add_tax(($break_result['quantity']*($product['products_price']- $break_result['discount_per_item'])),tep_get_tax_rate($product['products_tax_class_id'])));
			 }
			//get customers list
			# get selected categories
			$categories_array = tep_get_category_tree(); // added by R Calder
			$sel_cat=$cID;			
			if(!$sel_cat && $check_ID) $sel_cat=$check_ID;
			if(!$sel_cat) $sel_cat=$categories_array[1]['id'];
			$categories_array_selected[] = array('id' =>$sel_cat);


			if($action=='edit_product' && $HTTP_GET_VARS['pID']){ 
				$categories_query_selected = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $HTTP_GET_VARS['pID'] . "'");
				while ($categories = tep_db_fetch_array($categories_query_selected)) {
				  $categories_array_selected[] = array('id' => $categories['categories_id']);
				}
			}			
			//subscription
			$productsubs=tep_db_query("select * from " . TABLE_PRODUCTS_TO_SUBSCRIPTIONS ." where products_id='" .(int)$pID ."'");
  			while($productsubsres=tep_db_fetch_array($productsubs))  $subscription_categories_array_selected[]=array('id' => $productsubsres['subscription_id']);
			$language_id = 1;
			$tax_class_array = array(array('id' => '0', 'text' => TEXT_NONE));
			$tax_class_query = tep_db_query("select tax_class_id, tax_class_title from " . TABLE_TAX_CLASS . " order by tax_class_title");
			while ($tax_class = tep_db_fetch_array($tax_class_query)) {
			  $tax_class_array[] = array('id' => $tax_class['tax_class_id'],
										 'text' => $tax_class['tax_class_title']);
			}		
			$languages = tep_get_languages();
			if (!isset($pInfo->products_status)) $pInfo->products_status = '1';
		    switch ($pInfo->products_status) {
		      case '0': $in_status = false; $out_status = true; break;
		      case '1':
		      default: $in_status = true; $out_status = false;
		    }
			$current_date=getServerDate();
			$products_type=array(
				array('id'=>'G','text'=>TEXT_GENERAL),
				array('id'=>'S','text'=>TEXT_SOFTWARE_PACK),
				array('id'=>'B','text'=>TEXT_BOOK)
			);
			$selected_soft_subscription=array();
			$get_subscription_array=array();
			$software_pack_subscription_array=array();
			$span_pack_subscription_string="";
			$select_software_subscription_query=tep_db_query("SELECT su.subscription_id,sud.subscription_name from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud where su.subscription_id=sud.subscription_id and language_id='" . $languages_id . "' " . SUBSCRIPTION_CONDITION_STATUS . " and su.subscription_type='P' order by sud.subscription_name");
			if(tep_db_num_rows($select_software_subscription_query)>0)
				while($select_software_subscription=tep_db_fetch_array($select_software_subscription_query)){
					$software_pack_subscription_array[]=array('id'=>$select_software_subscription['subscription_id'],'text'=>$select_software_subscription['subscription_name']);
					$span_pack_subscription_string.="<span style='display:none' id='id_".$select_software_subscription['subscription_id']."'>".$select_software_subscription['subscription_name']."</span>";
					$get_subscription_array[$select_software_subscription['subscription_id']]=$select_software_subscription['subscription_name'];
				}
			$selected_soft_query=tep_db_query("select subscription_id,no_users from ".TABLE_PRODUCTS_TO_SUPPORT_PACKS." where product_id='".(int)$pID."' order by no_users");
			$selected_soft_group_query=tep_db_query("select no_users from ".TABLE_PRODUCTS_TO_SUPPORT_PACKS." where product_id='".(int)$pID."' group by no_users");
			if(tep_db_num_rows($selected_soft_query)>0)
				while($selected_soft=tep_db_fetch_array($selected_soft_query))					
					$raw_array[]=array('id'=>$selected_soft['no_users'],'text'=>$selected_soft['subscription_id']);
								
			if(tep_db_num_rows($selected_soft_group_query)>0){
				while($selected_soft_group=tep_db_fetch_array($selected_soft_group_query)){
					$selected_text="";
					$selected_value="";
					if($selected_soft_group['no_users']>0){
						$n=count($raw_array);
						for($i=0;$i<$n;$i++){
						   if($selected_soft_group['no_users']==$raw_array[$i]['id']){
						   	  $selected_text.=$get_subscription_array[$raw_array[$i]['text']].',';
						   	  $selected_value.=$raw_array[$i]['text'].',';
						   }	
						}
					}
					$selected_value.=$selected_soft_group['no_users'];
					$selected_text.=$selected_soft_group['no_users'].' users';
					$selected_soft_subscription[]=array('id'=>$selected_value,'text'=>$selected_text);																			
				}
			}			
			?>
	  	  <table border="0" cellspacing="4" cellpadding="2" width="100%" class="openContent_top">		   
 			<tr id="img_save_close_product" <?php echo (($HTTP_GET_VARS['pID']>0 && $HTTP_GET_VARS['cID']>0 && (isset($HTTP_GET_VARS['source']) && $HTTP_GET_VARS['source']=='products_expected'))?'':'style="display:none"') ;?>><td width='100%' align='right'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;&nbsp;<img src='images/template/img_closel.gif' alt='close' title='close' border='0' style="cursor:pointer;cursor:hand;" onclick="<?php echo (($HTTP_GET_VARS['pID']>0 && $HTTP_GET_VARS['cID']>0 && (isset($HTTP_GET_VARS['source']) && $HTTP_GET_VARS['source']=='products_expected'))?'javascript:location.href=\'products_expected.php\';':'javascript:if(document.getElementById(\'new_category\')) document.getElementById(\'new_category\').style.display=\'none\';') ;?>">&nbsp;&nbsp;</td></tr>
			<tr>
             <td colspan="4">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
                   <td class="main" width="18%" align="right"><?php echo TEXT_PRODUCTS_STATUS; ?></td>
           		   <td class="main" align="left" width="20%" nowrap="nowrap"><?php echo '&nbsp;' . tep_draw_radio_field('products_status', '1', $in_status) . '&nbsp;' . TEXT_PRODUCT_AVAILABLE . '&nbsp;' . tep_draw_radio_field('products_status', '0', $out_status) . '&nbsp;' . TEXT_PRODUCT_NOT_AVAILABLE; ?></td>
           		   <?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
            	   <td class="main" width="230" align="right"><?php echo TEXT_PRODUCTS_DATE_AVAILABLE; ?><br><small><?php echo "(" . $date_format . ")";?></small></td>
            	   <td class="main" align="left"><?php echo  '&nbsp;' . tep_draw_input_field("products_date_available",(tep_not_null($pInfo->products_date_available)?format_date($pInfo->products_date_available):format_date($current_date)),"size=10",false,'text',false);?>
			                <a href="javascript:show_calendar('new_product.products_date_available',null,null,'<?php echo $date_format;?>');"
							onmouseover="window.status='Date Picker';return true;"
							onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/ align="absmiddle">  
							</a>
				   </td> 				   
                 </tr>
               </table>
            </td>            
          </tr>              
          <tr>
          	 <td colspan="4">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
                   <td colspan="2" width="48%" valign="top">
                      <table border="0" cellspacing="0" cellpadding="2" width="100%">
                        <tr>
                          <td class="main" width="195" nowrap="nowrap" align="right" valign="top"><?php echo TEXT_PRODUCTS_MANUFACTURER; ?></td>
           		   		  <td class="main" align="left" valign="top"><?php echo '&nbsp;' . tep_draw_pull_down_menu('manufacturers_id', $manufacturers_array, $pInfo->manufacturers_id); ?></td>
                        </tr>
                    </table>
                   </td>                               	  
				   <?php  				
						$categories_selected=array();
						if($cID) $category_selected[0]=array('id'=>$cID);
						$categories_query_selected = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $HTTP_GET_VARS['pID'] . "'");
						while ($categories = tep_db_fetch_array($categories_query_selected)) 
						 	$category_selected[]=array('id'=>$categories['categories_id']);
						 	
						$cat_query = tep_db_query("select cd.categories_name,cd.categories_id from " . TABLE_CATEGORIES . " c," . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id=cd.categories_id and cd.language_id='" . $language_id  ."'" );
						$categories_array=array();
						while($cat_array=tep_db_fetch_array($cat_query))
							$categories_array[]=array('id'=>$cat_array['categories_id'],'text'=>$cat_array['categories_name']);
				   ?>				   
				   <td align="right" valign="top" nowrap="nowrap" class="main" width="9%"><?php echo TEXT_RESTRICT_GROUPS;?></td>
            		<td class="main"><?php echo tep_draw_mselect_checkbox('restrict_to_groups[]',$customer_groups_list,$get_selected_groups,' onClick="do_change_customers(\'1\');"');?></td>
                 </tr>
               </table>
            </td>                               
          </tr>          
          <tr>
            <td colspan="4">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
                 	<td class="main" width="200" align="right" valign="top"><?php echo TEXT_CATEGORIES; ?></td>
                 	<td class="main" align="left" width="20%"><?php echo tep_draw_mselect_checkbox('categories_ids[]',$categories_array,$category_selected);?></td>
            		<td align="right" valign="top" class="main" width="20%"><?php echo TEXT_RESTRICT_CUSTOMERS;?></td>
            		<td class="main" width=""><?php echo tep_draw_mselect_checkbox('restrict_to_customers[]',$customers_list,$get_selected_customers,' onClick="do_change_customers(\'2\');"');?></td>
                 </tr>
                </table>
            </td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td colspan="4">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
                 	<td align="right" valign="top" class="main" width="200"><?php echo TEXT_PRODUCT_TYPE;?></td>
            		<td class="main" width="110"><?php echo tep_draw_pull_down_menu('product_type',$products_type,$pInfo->product_type,'onchange="javascript:do_change_product(this);"');?></td>
            		<td class="main">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span <?php echo (($pInfo->product_type=='B')?'':'style="visibility:hidden;"');?> id="span_book"><?php echo  '&nbsp;&nbsp;'. TEXT_AUTHOR_NAME.'&nbsp;'. tep_draw_input_field('author_name', $pInfo->author_name,'maxlength="255"'); ?></span><span <?php echo (($pInfo->product_type=='G' || $pInfo->product_type=='')?'':'style="visibility:hidden;"');?> id="span_general"><?php echo  TEXT_PRODUCTS_MODEL.'&nbsp;'. tep_draw_input_field('products_model', $pInfo->products_model,'maxlength="12"'); ?></span></td>
                 </tr>
                </table>
            </td>
          </tr> 
          <tr>
            <td colspan="2">
				<table border="0" cellspacing="2" cellpadding="2" width="100%">
					<tr>
					  <td class="main" width="200">&nbsp;</td>
					  <td class="main" align="left" valign="top">
					  <span <?php echo (($pInfo->product_type=='S')?'':'style="display:none;"');?> id="span_software_pack">
						<table border="0" cellspacing="0" cellpadding="0" width="77%">
						  <tr>
						  	<td width="80" class="main" valign="top"><?php echo tep_draw_pull_down_menu('product_to_subscription_list',$selected_soft_subscription,$product_to_subscription_list,' size="5" style="width:300" onclick="javascript:do_subscription(\'edit\')"').'&nbsp;';?></td>
							<td width="10" align="left" class="main" valign="top"><?php echo tep_image(DIR_WS_IMAGES.'template/img_copy.gif',IMAGE_ADD_SUBSCRIPTION,'','',' onmouseout="javascript:this.src=this.src.replace(\'img_copy_hover.gif\',\'img_copy.gif\');" onmousemove="javascript:this.src=this.src.replace(\'img_copy.gif\',\'img_copy_hover.gif\');" onclick="javascript:do_subscription(\'add\');"').'<br>&nbsp;'.tep_image(DIR_WS_IMAGES.'template/img_trash.gif',IMAGE_DELETE_SUBSCRIPTION,'','',' onmouseout="javascript:this.src=this.src.replace(\'img_trash_hover.gif\',\'img_trash.gif\');" onmousemove="javascript:this.src=this.src.replace(\'img_trash.gif\',\'img_trash_hover.gif\');" onclick="javascript:do_subscription(\'delete\');"');?></td>
							<td width="10">&nbsp;</td>
							<td width="700" valign="top">
							   <span id="subscription_selection" style="display:none;">
								<table border="0" cellspacing="0" cellpadding="0" width="100%" style="background-color: #F9FAFF; border-color: #7b9ebd; border-style: solid; border-width: 1px;">
									<tr>
									 	<td colspan="2">
									 	  <table border="0" cellspacing="2" cellpadding="2" width="100%">
									 	  	<tr>
									 	  	  <td class="main" align="left" valign="top" width="180"><?php echo tep_draw_mselect_checkbox('software_subscription[]',$software_pack_subscription_array,$selected_soft_subscription).$span_pack_subscription_string;?></td>
									 	  	  <td width="50" valign="top" align="right"><?php echo tep_image(DIR_WS_IMAGES.'template/img_savel.gif',IMAGE_SAVE,'','','style="cursor:pointer;cursor:hand;" onclick="javascript:do_subscription(\'save\');"');?></td>
									 	  	  <td width="30" valign="top" align="right"><?php echo tep_image(DIR_WS_IMAGES.'template/img_closel.gif',IMAGE_CLOSE,'','','style="cursor:pointer;cursor:hand;" onclick="javascript:do_subscription(\'close\');"');?></td>
									 	  	</tr>
									 	  	<tr>
									 	  	<td colspan="3">
									 	  		<table border="0" cellspacing="2" cellpadding="3" width="100%">
									 	  		  <tr>
									 	  		     <td  nowrap="nowrap" valign="bottom" align="left" class="main"><?php echo 'Number of Users '.tep_draw_input_field('num_users','',' size="6" maxlength="7" onKeyPress="return keyRestrict(event)" ').'&nbsp;';?></td>
									 	  		  </tr>
									 	  		</table>
									 	  	</td>									 	  	
									 	  </table>
									 	</td>						   	
						 		   </tr>
								</table>
							 </span>	
							</td>
						 </tr>						 
						</table>						  
					 </span>
					  </td>					  
					</tr>
				</table>
			</td>
          </tr>
		  <tr>
		  	<Td colspan="2" align="center">
				<table width=80% cellspacing="5">
					<tr><td class="main" width="17%" nowrap align="right"><?php echo TEXT_PRODUCT_MODE;?></td>
					<td ><?php
					$mode_array[]=array("id"=>"P","text"=>"Physical");
					$mode_array[]=array("id"=>"V","text"=>"Downloadable");
					 echo tep_draw_pull_down_menu("product_mode",$mode_array,$pInfo->product_mode,' onchange="javascript:change_product_mode(this)"');?></td></tr>
					</tr>
					<tr><tD colspan=2 id="product_download_details" <?php if($pInfo->product_mode!='V') echo 'style=display:none';?>>
						<table cellpadding="5">
							<Tr>
								<td class="main"><?php echo TEXT_DOWNLOAD_DATE?></td>
            	 				  <td class="main" align="left"><?php echo  '&nbsp;' . tep_draw_input_field("download_date",(tep_not_null($pInfo->download_last_date)?format_date($pInfo->download_last_date):format_date($current_date)),"size=10",false,'text',false);?>
			              		  <a href="javascript:show_calendar('new_product.download_date',null,null,'<?php echo $date_format;?>');"
									onmouseover="window.status='Date Picker';return true;"
									onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/ align="absmiddle"> </a><small><?php echo "(" . $date_format . ")";?></small>
								</td>
								<td class="main"><?php echo TEXT_DOWNLOADS_PER_CUSTOMERS?></td>
								<td><?php echo tep_draw_input_field('download_count',$pInfo->downloads_per_customer,' size=6');?></td>
							</Tr>
							<tr>
								<td class="main"><?php echo TEXT_DOWNLOAD_LINK?></td>
								<td colspan="3" class="main"><?php echo tep_draw_file_field('download_link') . '&nbsp;' . $pInfo->download_link;?></td>
							</tr>
						</table>
					</tD></tr>
				</table>
			</Td>
		  </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
		   <?php tep_content_title_top("General"); ?>
<?php
    for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
?>
          <tr>
          	<td colspan="4" valign="middle">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
             	   <td class="main" width="15%" align="right" valign="top"><?php if ($i == 0) echo TEXT_PRODUCTS_NAME; ?></td>
           		   <td width="5%" align="left" valign="top"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name'],'','','valign="absmiddle"');?></td>
           		   <td class="main" align="left" width="20%" valign="top"><?php echo  tep_draw_input_field('products_name[' . $languages[$i]['id'] . ']', (isset($products_name[$languages[$i]['id']]) ? stripslashes($products_name[$languages[$i]['id']]) : tep_get_products_name($pInfo->products_id, $languages[$i]['id'])),'maxlength="64" align="center"'); ?></td>            	   			 
			   		<td valign="top">
						<table border="0" cellpadding="2" cellspacing="0" width="100%">
							  <tr>
								<td class="main" width="18%" align="right" valign="center" nowrap="nowrap"><?php if ($i == 0) echo TEXT_PRODUCTS_URL . '<br><small>' . TEXT_PRODUCTS_URL_WITHOUT_HTTP . '</small>'; ?></td>
								<td width="5%" align="left" valign="top"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']);?></td>
								<td class="main" align="left"  valign="top"  >&nbsp;<?php echo '&nbsp;' . tep_draw_input_field('products_url[' . $languages[$i]['id'] . ']', (isset($products_url[$languages[$i]['id']]) ? stripslashes($products_url[$languages[$i]['id']]) : tep_get_products_url($pInfo->products_id, $languages[$i]['id'])),'maxlength="255"'); ?></td>										  	
							  </tr>
						</table>
					</td>			       
			         	 
             	 </tr>
               </table>
            </td>            			
          </tr>
<?php
    }
   /* for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
?>
          <tr>
            <td colspan="4" valign="middle">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
                   <td class="main" width="15%" align="right" valign="top" nowrap="nowrap"><?php if ($i == 0) echo TEXT_PRODUCTS_DESCRIPTION; ?></td>
                   <td width="5%" align="left" valign="top"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>&nbsp;</td>           		              		              		   
            	   <td width="35%" colspan="2" class="main"><?php echo tep_draw_textarea_field('products_description[' . $languages[$i]['id'] . ']', 'soft', '80', '24', (isset($products_description[$languages[$i]['id']]) ? stripslashes($products_description[$languages[$i]['id']]) : tep_get_products_description($pInfo->products_id, $languages[$i]['id'])),'id="products_description[' . $languages[$i]['id'] . ']"'); ?></td>        
				   <td></td>
                 </tr>
               </table>
            </td>
          </tr>
<?php
    }*/
?>
		  <?php tep_content_title_bottom(); ?>
		  <?php tep_content_title_top('Description',false,false,true);
    for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
?>
          <tr>
            <td colspan="4" valign="middle">
               <table border="0" cellspacing="0" cellpadding="2" width="100%">
                 <tr>
                   <td class="main" width="15%" align="right" valign="top" nowrap="nowrap"><?php if ($i == 0) echo TEXT_PRODUCTS_DESCRIPTION; ?></td>
                   <td width="5%" align="left" valign="top"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>&nbsp;</td>           		              		              		   
            	   <td width="35%" colspan="2" class="main"><?php echo tep_draw_textarea_field('products_description[' . $languages[$i]['id'] . ']', 'soft', '80', '24', (isset($products_description[$languages[$i]['id']]) ? stripslashes($products_description[$languages[$i]['id']]) : ' ' .tep_get_products_description($pInfo->products_id, $languages[$i]['id']))); ?></td>        
				   <td></td>
                 </tr>
               </table>
            </td>
          </tr>
<?php
    }
		  ?>
		  <?php tep_content_title_bottom(); ?>
          <?php tep_content_title_top("Cost"); ?>		  
				 <tr>
				 	<td colspan="4" valign="top">
               			<table border="0" cellspacing="0" cellpadding="2" width="100%">
                 		  <tr>
							<td class="main" width="10%" align="right" valign="top"><?php echo TEXT_PRODUCTS_TAX_CLASS; ?></td>           		   				
			           		<td class="main" align="left" width="20%" valign="top"><?php echo  '&nbsp;' . tep_draw_pull_down_menu('products_tax_class_id', $tax_class_array, $pInfo->products_tax_class_id, 'onchange="updateGross();" onblur="update_price_break();"'); ?></td>
			           		<td class="main" width="100%" align="right" valign="top">
			           			<table border="0" cellspacing="0" cellpadding="2" width="100%">
		                 		  <tr>
		                 		 	    <td class="main" width="20%" align="right" valign="top"><?php echo TEXT_PRODUCTS_PRICE_NET; ?></td>           		   				
					           		   	<td class="main" align="left" width="40%" valign="top"><?php
					           		   	$price=$pInfo->products_price;
					           		   	if(strpos($price,".") && substr($price,strpos($price,"."))<=0) $price=(int)$price;
					           		   	 echo  '&nbsp;' . tep_draw_input_field('products_price', (tep_not_null($price) ? $price:1), 'maxlength="12" onKeyUp="updateGross();" onblur="update_price_break();"'); ?></td>           		   
					            	    <td class="main" width="10%" align="right" valign="top" nowrap="nowrap"><?php echo TEXT_PRODUCTS_PRICE_GROSS; ?></td>
					            	    <td class="main" align="left" width="40%" valign="top"><?php echo  '&nbsp;' . tep_draw_input_field('products_price_gross', $pInfo->products_price, ' OnKeyUp="updateNet();" onblur="update_price_break();"'); ?></td>
		                 		  </tr>
               					</table>		
			           		</td>                 		  
                 		  </tr>
                 		</table>
                 	</td>  
				  </tr>
				  <tr>
         			<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
         		 </tr>
		  	<?php tep_content_title_bottom();
		          tep_content_title_top("Stock"); 
			?>
          <tr>
          	<td colspan="4" valign="top">
               	<table border="0" cellspacing="0" cellpadding="2" width="100%">
                  <tr>
             		   <td class="main" align="right" valign="center"><?php  echo TEXT_PRODUCTS_QUANTITY; ?></td>	           		  
	           		   <td class="main" align="left" valign="center"><?php echo  '&nbsp;' . tep_draw_input_field('products_quantity', (tep_not_null($pInfo->products_quantity ) ? $pInfo->products_quantity :1),'onKeyPress="return keyRestrict(event)" maxlength=4'); ?></td>           		   
	            	   <td class="main"  align="right" valign="center"><?php echo TEXT_PRODUCTS_SKU;?></td>
	            	   <td class="main" align="left"><?php echo tep_draw_input_field('products_sku',$pInfo->products_sku,' size=20  maxlength='.((SKU_LENGTH=='false')?'20':SKU_COUNT)); ?></td>					   		                          		  
			       	   <td nowrap=true  valign="center"><?php echo ((SKU_LENGTH=='true')?sprintf(TEXT_SKU_COUNT,SKU_COUNT):'');?></td>
			       	   <td id="show_weight_field" <?php if($pInfo->product_mode=='V') echo 'style=display:none';?> >
							<table border="0" cellpadding="2" cellspacing="0" width="100%">
							  <tr>
									<td class="main"  align="right" valign="center"><?php echo TEXT_PRODUCTS_WEIGHT; ?></td>							
									<td class="main" align="left" valign="center" nowrap="nowrap"><?php  echo '&nbsp;' . tep_draw_input_field('products_weight', (SHOP_WEIGHT_UNIT=="OZ"?floor($pInfo->products_weight) ."":$pInfo->products_weight),'maxlength="6"') . "&nbsp;" . tep_get_unit_name(); ?></td>
							  </tr>
							</table>  		
					</td>
                  </tr>                  
              	</table>
            </td>      	             
          </tr>       
           <tr>
         		<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>                        
		  <?php 
		    tep_content_title_bottom();	
			// price breaks start
			tep_content_title_top("Price Breaks"); 
			echo tep_draw_hidden_field('price_break_details');
			?>
			<tr><td colspan="4" class="main"><?php echo tep_draw_checkbox_field('products_price_break','Y',($pInfo->products_price_break)=='Y'?'Checked':'','','Onclick="javascript:do_price_break(this);"') . '&nbsp;' . TEXT_PRICE_BREAK;?>
			<tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '10', '10'); ?></td></tr> 
			<tr id="price_break" style="display:none">
				<td colspan="4"><table cellpadding="2" cellspacing="2" border="0" width="100%">
					<tr>
						<td colspan="2">
							<table cellpadding="2" cellspacing="2" border="0" width="100%">
								<tr>
									<td class="main" width="50" align="right"><?php echo TEXT_PRODUCT_QUANTITY;?></td>
									<td class="main" width="75"><?php echo tep_draw_input_field('quantity','',' size="7" maxlength="6" onkeyup="javascript:do_price_change();" onKeyPress="return keyRestrict(event)"');?></td>
									<td class="main" width="150" align="right"><?php echo TEXT_DISCOUNT_PRICE;?></td>
									<td class="main" width="100"><?php echo tep_draw_input_field('discount_price','',' size="10" maxlength="15" onkeyup="javascript:do_price_change();"');?></td>
									<td class="main" id="cal_price"></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td valign="top" width="33%"><?php echo tep_draw_separator('pixel_trans.gif', '35', '10') .tep_draw_pull_down_menu('price_break_list',$break_array,'',' size="7" style="width:350px;" onClick="do_price_select(\'select\');"');?></td>
						<td>
							<table cellpadding="3" cellspacing="0" border="0">
								<tr><td><img src="images/template/img_new.gif" alt="<?php echo IMAGE_ADD;?>" title="<?php echo IMAGE_ADD;?>"  style="cursor:pointer;cursor:hand;" onclick="javascript:do_price_select('add');" onmouseover="do_breaks_hover_change('img_new',1,this);" id="img_add" onmouseout="do_breaks_hover_change('img_new',2,this)" /><?php //echo tep_image_button('button_add.gif',IMAGE_ADD,'onClick="javascript:do_price_select(\'add\');"');?></td></tr>
								<tr><td><img src="images/template/img_edit.gif" alt="<?php echo IMAGE_UPDATE;?>" title="<?php echo IMAGE_UPDATE;?>" style="cursor:pointer;cursor:hand;" onclick="javascript:do_price_select('update');" onmouseover="do_breaks_hover_change('img_edit',1,this);" id="img_update" onmouseout="do_breaks_hover_change('img_edit',2,this)" /><?php //echo tep_image_button('button_update.gif',IMAGE_UPDATE,'onClick="javascript:do_price_select(\'update\');"');?></td></tr>
								<tr><td><img src="images/template/img_trash.gif" alt="<?php echo IMAGE_DELETE;?>" title="<?php echo IMAGE_DELETE;?>" style="cursor:pointer;cursor:hand;" onclick="javascript:do_price_select('delete');" onmouseover="do_breaks_hover_change('img_trash',1,this);" id="img_trash" onmouseout="do_breaks_hover_change('img_trash',2,this)" /><?php //echo tep_image_button('button_delete.gif',IMAGE_DELETE,'onClick="javascript:do_price_select(\'delete\');"');?></td></tr>
							</table>
						</td>		
					</tr>
				</table></td>			
			</tr>							
				
		 <?php 	tep_content_title_bottom();
			//price breaks end
				
			tep_content_title_top("Image");			
		  	for ($icnt=1;$icnt<=5;$icnt++){ 
		  			$var_name_1="products_image_" . $icnt;
					$var_name_2="products_title_" . $icnt;
					if (isset($HTTP_POST_VARS[$var_name_1])) $image_name=$HTTP_POST_VARS[$var_name_1];
					else $image_name=$pInfo->$var_name_1;
					
		  ?>
					  <tr>
						<td colspan="4" valign="top">
						<table border="0" cellpadding="2" cellspacing="0" width="100%">
							  <tr>
								<td class="main" width="20%" align="right" valign="center"><?php echo sprintf(TEXT_PRODUCTS_IMAGE,$icnt); ?></td>
								<td class="main" align="left" width="20%" valign="center"><?php echo  '&nbsp;' . tep_draw_file_field('products_image_' . $icnt) . '<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $image_name . tep_draw_hidden_field('products_previous_image_' .$icnt, $image_name); ?></td>
								<td class="main" width="15%" align="right" valign="center" nowrap="nowrap">&nbsp;&nbsp;<?php echo sprintf(TEXT_PRODUCTS_TITLE,$icnt); ?></td>
								<td class="main" align="left" width="20%"><?php echo '&nbsp;' . tep_draw_input_field('products_title_' . $icnt,$pInfo->$var_name_2,'maxlength="100"'); ?></td>
							  	<td width="35%"></td>
							  </tr>
						</table>
						</td>
					</tr>
		  <?php  }		 
	tep_content_title_bottom();
 	tep_content_title_top(TEXT_IS_ATTRIBUTES);
	// BOF: WebMakers.com Added: Draw Attribute Tables
	//get option value names
	  $option_name_value_array=array();
	  $option_name_value_query=tep_db_query("select pa.options_values_id,pov.products_options_values_name from ".TABLE_PRODUCTS_OPTIONS_VALUES." pov,".TABLE_PRODUCTS_ATTRIBUTES." pa where pa.options_values_id=pov.products_options_values_id");
	  if(tep_db_num_rows($option_name_value_query)>0){
	  	while($option_name_value=tep_db_fetch_array($option_name_value_query))
			$option_name_value_array[$option_name_value['options_values_id']]=$option_name_value['products_options_values_name'];
	  }
	//get option value names
	//get saved attribute list
/*	  $attribute_query=tep_db_query("select attributes_id,products_quantity,sku,attribute_status from ".TABLE_PRODUCTS_STOCK." where products_id='".(int)$pID."'");
	  $attrib_length_exits="";
	  if(tep_db_num_rows($attribute_query)>0){
	  	while($attribute=tep_db_fetch_array($attribute_query)){
			if($attribute['attributes_id']!=""){
				$attribute_split=split("-",$attribute['attributes_id']);
				$n=count($attribute_split);
				$attribute_list_text="";
	  			$attribute_list_value="";
				for($as=0;$as<$n;$as++){
				   if($attribute_split[$as]!="") {
				   	  $option_split=split("{",$attribute_split[$as]);
				   	  $option_id=$option_split[0];				   	  				   	 				   	 
				   	  $option_value_id=str_replace("}","",$option_split[1]);				   	  
				   	  $attribute_list_text.=$option_name_value_array[$option_value_id].',';
				   	  $attribute_list_value.=$option_id.'##'.$option_value_id."{{}}";
				   }
				}
				$attribute_list_text.=$attribute['products_quantity'].' SKU:'.$attribute['sku'].' status:'.$attribute['attribute_status'];
				$attribute_list_value.=$attribute['products_quantity'].'##'.$attribute['sku'].'##'.$attribute['attribute_status'];
				$attribute_list_array[]=array('id'=>$attribute_list_value,'text'=>$attribute_list_text);
				if($attrib_length_exits=="" && strlen($attribute_list_text)>200) $attrib_length_exits="overflow:auto;";								
			}
	  	}
	 }*/
	//get saved attribute list
	$options_options_query = tep_db_query("select po.products_options_id, po.products_options_name from " . TABLE_PRODUCTS_OPTIONS . " po,".TABLE_PRODUCTS_ATTRIBUTES." pa where pa.options_id=po.products_options_id and pa.products_id='".(int)$pID."' and po.language_id = '" . $languages_id . "' group by pa.options_id order by po.products_options_sort_order");
	?>
 
<?php
/////////////////////////////////////////////////////////////////////
// BOF: WebMakers.com Added: Draw Attribute Tables
?>
 <tr>
	<td colspan="2" class="main"><?php echo tep_draw_checkbox_field("is_attributes",'Y',($pInfo->is_attributes=='Y'?true:false),'',"onClick='javascript:do_skew_action(this.checked);'") .'&nbsp;'. TEXT_IS_ATTRIBUTES;?></td>
</tr>
 	
<tr>
  <td>
 <div id="attrib_list" style="<?php echo ($pInfo->is_attributes!='Y'?'display:none':'');?>">
   <table border="0" cellpadding="1" cellspacing="1">
<?php
    $rows = 0;
	$pos=1;
    $options_query = tep_db_query("select products_options_id, products_options_name from " . TABLE_PRODUCTS_OPTIONS . " where language_id = '" . $languages_id . "' order by products_options_sort_order");
	while ($options = tep_db_fetch_array($options_query)) { 
	$values_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p where pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" . $options['products_options_id'] . "' and pov.language_id = '" . $languages_id . "'");
    $header = false;
      while ($values = tep_db_fetch_array($values_query)) {
        $rows ++;
        if (!$header) {
          $header = true;
?> 
          <tr valign="top">
          	<td>
          		<table border="0" cellpadding="1" cellspacing="1" >
	               <tr>
	            		<td colspan="15"><?php if ($options['products_options_name']=='Size')  echo tep_black_line(); ?></td>
	         	 	</tr>
	               <tr class="dataTableHeadingRow">
		                <td class="dataTableHeadingContent" width="250" align="left"><?php echo $options['products_options_name']; ?></td>
		                <td class="dataTableHeadingContent" width="50" align="center"><?php echo 'Prefix'; ?></td>
		                <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'Price'; ?></td>
		                <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'Sort Order'; ?></td>
		                <td class="dataTableHeadingContent" width="20">&nbsp;</td>
		                <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'One Time Charge'; ?></td>
		                <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'Weight Prefix'; ?></td>
		                <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'Weight'; ?></td>
				          <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'Units'; ?></td>
		                <td class="dataTableHeadingContent" width="70" align="center"><?php echo 'Units Price'; ?></td>
	              </tr>
<?php
        }
        $attributes = array();
        if (sizeof($HTTP_POST_VARS) > 0) {
          if ($HTTP_POST_VARS['option'][$rows]) {
            $attributes = array(
                                'products_attributes_id' => $HTTP_POST_VARS['option'][$rows],
                                'options_values_price' => $HTTP_POST_VARS['price'][$rows],
                                'price_prefix' => $HTTP_POST_VARS['prefix'][$rows],
                                'products_options_sort_order' => $HTTP_POST_VARS['products_options_sort_order'][$rows],
                                'product_attributes_one_time' => $HTTP_POST_VARS['product_attributes_one_time'][$rows],
                                'products_attributes_weight' => $HTTP_POST_VARS['products_attributes_weight'][$rows],
										  'products_attributes_weight_prefix' => $HTTP_POST_VARS['products_attributes_weight_prefix'][$rows],
                                'products_attributes_units' => $HTTP_POST_VARS['products_attributes_units'][$rows],
                                'products_attributes_units_price' => $HTTP_POST_VARS['products_attributes_units_price'][$rows],
                                );
          		  }
		  
        } else {
          $attributes_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix, products_options_sort_order, product_attributes_one_time, products_attributes_weight,  products_attributes_weight_prefix, products_attributes_units, products_attributes_units_price from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . $pInfo->products_id . "' and options_id = '" . $options['products_options_id'] . "' and options_values_id = '" . $values['products_options_values_id'] . "'");
          if (tep_db_num_rows($attributes_query) > 0) {
            $attributes = tep_db_fetch_array($attributes_query);
          }
        }
		if(sizeof($attributes)>0)
			$option_values_array[]=$attributes;
		
?>                
              <tr class="attributes-odd">
                <td class="smallText"><?php echo tep_draw_checkbox_field('option'. '['.$rows . ']', $attributes['products_attributes_id'], $attributes['products_attributes_id'],'','onClick=javascript:change(this,1,' . $pos . '); ') . '&nbsp;' . $values['products_options_values_name']; ?>&nbsp;</td>
                <td class="smallText" width="50" align="center"><?php echo tep_draw_input_field('prefix' .'['. $rows . ']', $attributes['price_prefix'], 'size="2"'); ?></td>
                <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('price' .'['.$rows . ']', $attributes['options_values_price'], 'size="7"'); ?></td>
                <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('products_options_sort_order' .'[ '.$rows . ']', $attributes['products_options_sort_order'], 'size="7"'); ?></td>
                <td class="smallText" width="20">&nbsp;</td>
                <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('product_attributes_one_time' .'[' .$rows . ']', $attributes['product_attributes_one_time'], 'size="2"'); ?></td>
                <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('products_attributes_weight_prefix' . '['.$rows . ']', $attributes['products_attributes_weight_prefix'], 'size="2"'); ?></td>
                <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('products_attributes_weight' .'['. $rows . ']', $attributes['products_attributes_weight'], 'size="7"'); ?></td>
              <!--  <td class="smallText" width="70" align="center"><?php //echo tep_draw_input_field('products_attributes_quantity[' . $rows . ']', $attributes['products_attributes_quantity'], 'size="7"'); ?><?PHP //$qcheck = $qcheck + $attributes['products_attributes_quantity']; ?></td>-->
			    <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('products_attributes_units' .'['. $rows . ']', $attributes['products_attributes_units'], 'size="7"'); ?></td>
                <td class="smallText" width="70" align="center"><?php echo tep_draw_input_field('products_attributes_units_price' .'['. $rows . ']', $attributes['products_attributes_units_price'], 'size="7"'); ?></td>
              </tr>
<?php
      }
      if ($header) {
	  
	?>
	 </table>
</td>
<?php
      }
	  $pos++;
    }
?>
  <tr>
         <td colspan="15"><?php  echo tep_black_line(); ?></td>
   </tr>
 <tr>
	<td ><?php echo tep_draw_separator('pixel_trans.gif', '24', '15');?></td>
  </tr>
	<?php 

			 if(sizeof($HTTP_POST_VARS)){
				if($HTTP_POST_VARS['attributes_array']){
					$attributes_ids=$HTTP_POST_VARS['attributes_array'];
					$attributes_array=split('\^',$attributes_ids);
					for($bcnt=0;$bcnt<sizeof($attributes_array)-1;$bcnt++){	
						$values=split("\#",$attributes_array[$bcnt]);
						$attrb_values=split('-',$values[1]);
						$values_text="";
						for($i=0;$i<count($attrb_values);$i++){
							$option_values=split('{',$attrb_values[$i]);
							$option_name_query=tep_db_query("select products_options_values_name from ".TABLE_PRODUCTS_OPTIONS_VALUES ." where products_options_values_id='".substr($option_values[1],0,-1)."'");
							$option_name_result=tep_db_fetch_array($option_name_query);
							$values_text.=$option_name_result['products_options_values_name'].",";
						}
						$attribute_array[]=array('id' =>$values[1],
												 'option_name'=>substr($values_text,0,-1),
												 'sku'=>$values[0],
												 'qty'=>$values[2],
												 'status'=>$values[3]);
					}
				}
			} else{
					$attributes_query=tep_db_query("select attributes_id,products_quantity,sku,attribute_status from ".TABLE_PRODUCTS_STOCK . " where products_id='".$HTTP_GET_VARS['pID']."'");
					while($attributes_result = tep_db_fetch_array($attributes_query)){
						$attrb_values=split('-',$attributes_result['attributes_id']);
						//$values_text="";
						for($i=0;$i<count($attrb_values);$i++){
							$option_values=split('{',$attrb_values[$i]);
							$attrb_list[$option_values[0]]=substr($option_values[1],0,-1);
						}
						$values_text="";

						mysql_data_seek($options_query,0);
						while($options=tep_db_fetch_array($options_query)){
							$option_values=$attrb_list[$options['products_options_id']];
							$option_name_query=tep_db_query("select products_options_values_name from ".TABLE_PRODUCTS_OPTIONS_VALUES ." where products_options_values_id='".$option_values."'");


							$option_name_result=tep_db_fetch_array($option_name_query);
							if($option_name_result['products_options_values_name']!="")
							$values_text.=$option_name_result['products_options_values_name'].",";
						}
						$attribute_array[]=array('id' => $attributes_result['attributes_id'],
												'option_name'=>substr($values_text,0,-1),
												 'sku'=>$attributes_result['sku'],
												 'qty'=>$attributes_result['products_quantity'],
												 'status'=>$attributes_result['attribute_status']);
					}
			}	
			?>
	<?php 
	$options_query = tep_db_query("select products_options_id, products_options_name from " . TABLE_PRODUCTS_OPTIONS . " where language_id = '" . $languages_id . "' order by products_options_sort_order");
	?>
	<tr>
	<td colspan="15" valign="bottom" ><?php echo '<a onMouseOver="javascript:change_class(this,2);" onMouseOut="javascript:change_class(this,3);"  href="javascript:show_add_attribute()";>'.tep_image(DIR_WS_IMAGES.'template/img_new.gif','New','22','22').'</a>'; ?></td>
	</tr>
	<tr valign="top">
	<td colspan="15">
	<table border="0" cellpadding="1" cellspacing="1" id="dynamic_table" width="100%">
	<tbody>
		
		<tr class="dataTableHeadingRow">
			<td  class="dataTableHeadingContent" width="15%"><?php echo 'SKU'; ?></td> 
			<td class="dataTableHeadingContent" ><?php echo 'Attributes';?></td>
			<td  class="dataTableHeadingContent" width="10%"><?php echo 'Stock'; ?></td> 
			<td  class="dataTableHeadingContent" width="10%"><?php echo 'Status'; ?></td>  
			<td width="5%" >&nbsp;</td>
		 </tr>	

		 <?php 
		 for($tr=0;$tr<sizeof($attribute_array);$tr++){
		   $split_array = $attribute_array[$tr]; 
		 	echo '<tr  class="attributes-odd" id=\'' . $split_array["id"] . '\'>';
		 	echo '<td class="smallText">' . $split_array["sku"] . '</td>';
		 	echo '<td class="smallText">' . $split_array["option_name"] . '</td>';
		 	echo '<td class="smallText">' . $split_array["qty"] . '</td>';
		 	echo '<td class="smallText">' . $split_array["status"] . '</td>';
			echo '<td valign="top" align="center"><a href="javascript:do_add_attribute(\'select\',\'' . $split_array["id"] . '\');">' . tep_image(DIR_WS_IMAGES.'template/img_copy.gif',IMAGE_ADD_ATTRIBUTE) . '</a>&nbsp;<a href="javascript:do_add_attribute(\'delete\',\'' . $split_array["id"] . '\')">' . tep_image(DIR_WS_IMAGES.'template/img_trash.gif',IMAGE_DELETE_ATTRIBUTE) .'</a></span></td>';
			echo '</tr>';
		}
		 ?>
	</tbody>	
	</table>
	</td>
	</tr>
	<tr>
		  	<td colspan="15" align="center"><br>
		  	<div id="option_list" style="display:none;">
			<table cellpadding="0" cellspacing="1" border="0" width="70%" bgcolor="#000000">
			<tr><td>
			<table cellpadding="5" cellspacing="0" border="0" width="100%" bgcolor="#ebebff">
			 <tr >	
				<td class="main" width="35%" colspan="2"><?php //echo tep_draw_pull_down_menu('attribute_list',$attribute_array,'',' size=10 style="width:100%" onkeyup="javascript:do_action(\'select\');" onkeydown="javascript:do_action(\'select\');" onClick="javascript:do_action(\'select\');"');
					echo '<b>Attributes</b>';
					echo tep_draw_hidden_field('current_action');
					echo tep_draw_hidden_field('current_edit_id');
				?></td>
			 	<td width='100%' align='right'><img src='images/template/img_savel.gif' alt='save' title='save' border='0' style="cursor:pointer;cursor:hand;" onclick="do_add_attribute('add');">&nbsp;&nbsp;<img src='images/template/img_closel.gif' alt='Reset' title='close' border='0' style="cursor:pointer;cursor:hand;" onclick="javascript:clear_attrib_details();"></td>


			</tr>
			<tr >
				<td colspan="3">
				<table cellpadding="2" cellspacing="2" border="0" >
				  <?php   
				  if(tep_db_num_rows($options_query)>0){
					 mysql_data_seek($options_query,0);
					  while($options=tep_db_fetch_array($options_query)){?>
								<tr id="<?php echo 'options'.$options['products_options_id'];?>">
									<td class="main" id="<?php echo 'text'.$options['products_options_id'];?>"><?php echo $options['products_options_name'];?></td>
									<td class="main"><?php echo tep_draw_pull_down_menu('option_values'.$options['products_options_id'],array(),'','  id="option_values'.$options['products_options_id'].'"');?></td>
									<td>&nbsp;</td>
								</tr>
			<?php	  } 
				}?>
			</table>
		</td></tr>
		<tr>
				<td class="main" nowrap><?php echo TEXT_QUANTITY.'&nbsp;&nbsp;: '.tep_draw_input_field('attrib_quantity','',' id="attrib_quantity" size=10');?></td>
				<td class="main" align="left" nowrap><?php echo TEXT_PRODUCTS_SKU.'&nbsp;'.tep_draw_input_field('attrb_sku','',' id="attrb_sku" size=13 maxlength=12');?></td>

				<td><?php 
					if(defined(SKU_LENGTH)){
						echo '<input type="hidden" name="default_sku_value" id="default_sku_value" value="' . SKU_LENGTH . '">';
					} else {
						echo '<input type="hidden" name="default_sku_value" id="default_sku_value" value="">';
					}
					echo tep_draw_separator('pixel_trans.gif',"5");
				?>
				</td>
		</tr>	
		  <tr>
           <td class="main"colspan="4"  nowrap><?php echo TEXT_ATTRIBUTE_STATUS; ?>&nbsp;&nbsp;<?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . tep_draw_radio_field('attribute_status', '1',' checked ') . '&nbsp;' . TEXT_ATTRIBUTE_AVAILABLE . '&nbsp;' . tep_draw_radio_field('attribute_status', '0') . '&nbsp;' . TEXT_ATTRIBUTE_NOT_AVAILABLE; ?></td>

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



		</div>
		</td>
	</tr>
	</table>
		</div>
	  </td>
	</tr>
<?php
// EOF: WebMakers.com Added: Draw Attribute Tables
/////////////////////////////////////////////////////////////////////
	tep_content_title_bottom();
	// EOF: WebMakers.com Added: Draw Attribute Tables
	tep_content_title_top('Linked Subscription'); 
	?>
	<tr>
	<td class="main"><?PHP echo tep_draw_checkbox_field('is_subscription','Y',($pInfo->is_subscription=='Y'?true:false),'','onclick=show_sub();') .'Linked Subscription '; ?>
		<div id="show_is_sub" >
			<table border=0 cellpadding="4" cellspacing="0" width="50%">
				<tr>
					<td class="main" width="150" valign="top"></td>
					<td class="main" width="200">
						<?php 
							$events_link=true; 
							echo tep_draw_subscription_select_menu('subscription_categories_ids[]',$subscription_array, $subscription_categories_array_selected[0]['id'], 'size=10 id=subscription_categories_ids style="width:350"',false,0,false);
						?>
					</td>
				</tr>
		    </table>
		  </div>
	  </td>
	</tr>	
<?php	tep_content_title_bottom();
?>	 	  
 <tr style="height:20">	  
    <td class="main" align="right"><?php 
		 //preview 
		 echo '<input type="hidden" name="shop_weight_unit" id="shop_weight_unit" value="' . SHOP_WEIGHT_UNIT . '">';		 		 		
		 echo tep_draw_hidden_field('products_date_added', (tep_not_null($pInfo->products_date_added) ? $pInfo->products_date_added : date('Y-m-d'))); 
		 echo tep_draw_hidden_field('attribute_quantity',$qcheck);		
		 ?></td>
      </tr>            
    </table>			
 <?php
  }  
  
  function echo_get_details(){
   	 global $currencies,$languages_id,$pID,$products_id,$cID, $HTTP_POST_VARS;
	 	 
   	 	$str_vals="";
   	 	$c=1;
   	 	 $find_sort_order_query=tep_db_query("select products_id from products where products_sort_order>0");
   	 	 if(tep_db_num_rows($find_sort_order_query)==0){			 
			 $select_query=tep_db_query("select products_id from products");
			 while($up=tep_db_fetch_array($select_query)){
				tep_db_query("update products set products_sort_order='".$c."' where products_id='".$up['products_id']."'");
				$c++;
			 }
   	 	 }
   	 	if(!$pID) $pID=$products_id;
		$product_query = tep_db_query("select pd.products_id,pd.products_name, pd.products_description, p.products_quantity,p.products_tax_class_id,p.products_price,date_format(p.products_date_added,'%Y-%m-%d') as products_date_added, date_format(p.products_last_modified,'%Y-%m-%d') as products_last_modified,p.products_status,p.products_image_1,p.products_sort_order,date_format(p.products_date_available,'%Y-%m-%d') as products_date_available from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$pID . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'");
		if(tep_db_num_rows($product_query)>0){
		  $product = tep_db_fetch_array($product_query);
		  $product_categories=tep_db_query("select cd.categories_id,cd.categories_name from ".TABLE_CATEGORIES." c,".TABLE_CATEGORIES_DESCRIPTION." cd where c.categories_id=cd.categories_id and cd.language_id='".(int)$languages_id."' and c.categories_id='".(int)$cID."'");
		  if(tep_db_num_rows($product_categories)>0){		  			 
		  	$categories=tep_db_fetch_array($product_categories);
		  	while(list(,$vals)=each($categories))		  					
				$str_vals.=$vals.'{}';
		  }
		  if(!$cID) {
			$find_category_query=tep_db_query("select categories_id from products_to_categories where products_id='".(int)$products_id."'");
			if(tep_db_num_rows($find_category_query)>0){
				$find_category=tep_db_fetch_array($find_category_query);
				$cID=$find_category['categories_id'];
			}											  	 
		  }
		  while(list($key,$vals)=each($product)){		
			if($key=='products_description')								
		 		$vals = str_replace("\n","", $vals);
			if($key=='products_price')
				$vals = $currencies->format(tep_add_tax($vals,tep_get_tax_rate($tax_id)));
			if($key=='products_date_added' || $key=='products_last_modified' || $key=='products_date_available')
				$vals=format_date($vals);
			if($key=='products_tax_class_id'){
				$tax_id = $vals;
				continue;
			} 	
			//echo $key;	
			$str_vals.=$vals.'{}';
		 }
	  }else $str_vals="No Record Found";	  
	  echo (($HTTP_POST_VARS['action_type'] == 'new') ? '' : 'get_details^') . $str_vals.'{}'.$cID;
  }
	
function load_non_category_details($non_categories_products_id){
   global $currencies;
	$products_query=tep_db_query("select p.products_id,p.products_quantity,pd.products_name,pd.products_description,date_format(p.products_date_added,'%d-%m-%Y') as products_date_added,p.products_last_modified,p.products_price,p.products_tax_class_id from ".TABLE_PRODUCTS." p, ".TABLE_PRODUCTS_DESCRIPTION." pd where p.products_id=pd.products_id and p.products_id='$non_categories_products_id'");
	$products_array=tep_db_fetch_array($products_query);
	$result='<table width="100%" cellpadding="0" class="openContent" cellspacing="0" border="0">'.
	'<tr class="openContent_top">
	<td width="430" class="main">'.tep_image(DIR_WS_IMAGES.'template/icon_active.gif').'&nbsp;'.$products_array['products_name'].'</td>'.
	'<td width="30" align="center">'. 
		'<table style="background:#DEE4F5;cursor:pointer;" width="100%" border="0" cellpadding="0" cellspacing="0">'.
		'<tr>'.
		'<td class="cell_bg_popm_left" width="10%">&nbsp;</td>'.
		'<td width="10%"><a href="javascript: delete_non_product('.$products_array['products_id'].');" onMouseOver="javascript:do_hover_change(this,2);" onMouseOut="javascript:do_hover_change(this,1);"><img alt="Delete" title="Delete" border="0" src="images/template/img_trash.gif"></a></td>'.
		'<td class="cell_bg_popm_right" width="10">&nbsp;</td>'.
	'</tr>'.
	'</table>' .' </td>'.
	'<td align="right">'.
	'<a href="javascript: close_non_product_category('.$products_array['products_id'].');">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'.
	'&nbsp;</td>'.
	'</tr>'.
	'<tr>
	<td colspan="3">
		<table width="100%" cellpadding="0" cellspacing="0" border="0">
		<tr>
			<td style="padding-left:20px;" class="smalltext" width="400">Date Added : '.$products_array['products_date_added'].'<br>'.
					  'Price &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : '.$currencies->format(tep_add_tax($products_array['products_price'],tep_get_tax_rate($products_array['products_tax_class_id'])))	.'<br>'.
					  'Quantity &nbsp;&nbsp;&nbsp;&nbsp; : '.$products_array['products_quantity'].
			'</td>
		<td>'. '<div style="overflow: auto; width: 100%; height: 80px;"> '.$products_array['products_description'].'</td>
		</tr>
		</table>
	</td>
	</tr>
	<tr><td colspan="3"><br></td></tr><table>';
	echo $result;
  }
	
  	function tep_get_product_cat_ajax_list($parent_id,$level=0,$value_assign_type='default',&$results,&$open_ids){
		global $languages_id;
		$categories_sql = "select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' and c.parent_id = '" . (int)$parent_id . "' order by c.sort_order, cd.categories_name";
		//echo $categories_sql;
		$categories_query = tep_db_query($categories_sql);
		if (tep_db_num_rows($categories_query)<=0) return;
		while ($category_result = tep_db_fetch_array($categories_query)) {
			$open_ids.="," . $category_result["categories_id"];
			switch($value_assign_type){
				case 'open_ids':
					$id=substr($open_ids,1);
					break;
				default:
					$id=$category_result["categories_id"];
			}
			$name=$category_result["categories_name"];
			$style="";
			if ($level>0) $style='padding-left:' . ($level*10) . "px";
			
			$results[]=array('id'=>$id,'text'=>$name,'style'=>$style);
			tep_get_product_cat_ajax_list($category_result["categories_id"],$level+1,$value_assign_type,$results,$open_ids);
			$open_ids=substr($open_ids,0,strrpos($open_ids,","));
		}
	}
	function do_sort($show_order,$level){
		global $languages_id,$selected_name,$login_id,$login_groups_type,$HTTP_GET_VARS,$page,$command;
		
		$sort_by='ASC';
		if($show_order=='DN') 	$sort_by='DESC';
		$categories_sort_sql = "select c.categories_id,c.sort_order, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' ".$search_criteria." order by c.parent_id,cd.categories_name $sort_by ";		
		//echo $categories_sort_sql;
		$categories_sort_query = tep_db_query($categories_sort_sql);
		$sort_orders=array();
		
		while($categories_sort=tep_db_fetch_array($categories_sort_query)){
			if (!$sort_orders[$categories_sort["parent_id"]]) $sort_orders[$categories_sort["parent_id"]]=1;
				tep_db_query("UPDATE categories set sort_order= ". $sort_orders[$categories_sort["parent_id"]] ." where categories_id= ". $categories_sort['categories_id']);
				//echo "UPDATE categories set sort_order= ". $sort_orders[$categories_sort["parent_id"]] ."where categoried_id= ". $categories_sort['categories_id'];
				$sort_orders[$categories_sort["parent_id"]]++;
		}
		
		$products_sql="SELECT pd.products_name,pd.products_id,p.products_status,p.products_sort_order from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS ." p, ". TABLE_PRODUCTS_TO_CATEGORIES . " pcd where p.products_id=pd.products_id and pd.products_id=pcd.products_id and pd.language_id = '" . (int)$languages_id . "' " .$prd_where . " ORDER BY pd.products_name $sort_by " ;
		$products_query=tep_db_query($products_sql);
		if(tep_db_num_rows($products_query)>0){
			$i=1;
			while($result=tep_db_fetch_array($products_query)){
				tep_db_query("update ".TABLE_PRODUCTS." set products_sort_order='".$i."' where products_id='". (int)$result['products_id']."'");	
				$i++;													 																																
			}
		}
	}
	
	function output_sub_result($parent_id,$page,$show_order,$level,$open_ids,$start_up=0,$search_item='') {
		global 	$languages_id,$selected_name,$login_id,$login_groups_type,$HTTP_GET_VARS,$page,$command;
		$display=true;
		if($open_ids!="") {
		  $find_child_category=tep_db_query("select categories_id from categories where parent_id=0 and categories_id='".$parent_id."'");
		  $display=true;
		  if(tep_db_num_rows($find_child_category)==0) $display=false;
		}
		if($display) echo output_category_details($parent_id);
		echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr style="height:10"><td></td></tr></table>';
		$search_criteria="";
			if($search_item!='')
				$search_criteria=" and categories_name like '%" . tep_db_input($search_item) . "%' ";
		$open_ids="," . $open_ids;
		if ($parent_id>0 ){
			// first fetch the products in this category
			output_products_result($parent_id,$page,$show_order);
		}
		$categories_sql = "select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$languages_id . "' and c.parent_id = '" . (int)$parent_id . "' ".$search_criteria." order by c.sort_order, cd.categories_name";
		//echo $categories_sql;
		$categories_query = tep_db_query($categories_sql);		
		if (tep_db_num_rows($categories_query)<=0) return;
		else $category_ids = tep_db_fetch_array(tep_db_query($categories_sql));
		echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" id="parent_'.$category_ids['categories_id'].'">';
		// fetch the sub category
		// iterate with nested category
		$icnt=0; $toggle_flag=0;
		while ($category_result = tep_db_fetch_array($categories_query)) {
			$show=false; 
			if (($open_ids!='' && strpos($open_ids,"," . $category_result["categories_id"] . ",")!==false) || $start_up) {
				
				if ($start_up){
					echo '<script language="javascript">prev_category='. $category_result["categories_id"] . ';open_ids[0]=' . $category_result["categories_id"] . ';</script>';
				}
				$start_up=false;
				$show=true;
			}			
			tep_category_title_top($category_result['categories_name'],$category_result['categories_id'],$show,$level);			
				if ($show)   output_sub_result($category_result['categories_id'],$page,$show_order,$level+1,$open_ids);			
			tep_category_title_bottom();
		}
		echo '</table>';
	}
	
	function tep_list_not_categories_product(){
	global $languages_id;  
	$products_query=tep_db_query('select pd.products_id,pd.products_name,p.products_status from '.TABLE_PRODUCTS.' p, '.TABLE_PRODUCTS_DESCRIPTION." pd, ".TABLE_PRODUCTS_TO_CATEGORIES." ptc where p.products_id=pd.products_id and ptc.products_id=pd.products_id and ptc.categories_id='0' and pd.language_id='$languages_id' order by products_name");
    $class="dataTableRowOdd";
		
		if (tep_db_num_rows($products_query)>0){
		echo '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
		echo '<tr class="dataTableHeadingRow"><td class="dataTableHeadingContent">Non Existent Category Product</td></tr>';
			while ($products = tep_db_fetch_array($products_query)){
			$products_id=$products['products_id'];
				$class=($class=="dataTableRowOdd")?"dataTableRowEven":"dataTableRowOdd";					
				echo '<tr height="18">
				<td class="main">
				<div  class="'.$class.'" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript: focus_non_category_product('.$products_id.');" id="non_category_product_'.$products_id.'">';
				echo  ($products['products_status']==1)?tep_image(DIR_WS_IMAGES  . 'template/icon_active.gif', '') . tep_draw_separator('pixel_trans.gif', '5', '10'):tep_image(DIR_WS_IMAGES  . 'template/icon_inactive.gif', '') . tep_draw_separator('pixel_trans.gif', '5', '10');
				echo  $products['products_name'] . '</div>
				<div id="ajax_non_category_product_'.$products_id.'" style="display:none"></div>
				</td></tr>';					
			} 
		echo '</table>';		
		}
	}

	function output_products_result($categories_id,$page,$show_order){
		global $languages_id,$command;
			$prd_where="";
		
			if (substr($show_order,1,1)=="V") $prd_where=" AND p.products_status=1 ";	
		
			$products_sql="SELECT pd.products_name,pd.products_id,p.products_status,products_sort_order from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS ." p, ". TABLE_PRODUCTS_TO_CATEGORIES . " pcd where p.products_id=pd.products_id and pcd.categories_id='" . $categories_id . "' and pd.products_id=pcd.products_id and pd.language_id = '" . (int)$languages_id . "' " .$prd_where . " ORDER BY p.products_sort_order ASC " ;
			$query_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $products_sql, $query_split_numrows);
			$products_query=tep_db_query($products_sql);
			
			//check sort order
			if ($command!='fetch_sub_products'){
				echo '<table cellpadding="0" cellspacing="0" border="0" width="100%">';				
				if($query_split_numrows>MAX_DISPLAY_SEARCH_RESULTS){
					echo '<tr>
							<td align="right">
								<table border="0" cellspacing="0" cellpadding="2">
									<tr> 
										<td class="smallText" align="left">'.													
											$query_split->display_script_links($query_split_numrows, MAX_DISPLAY_SEARCH_RESULTS, REPORT_MAX_LINKS_PAGE, $page,'cID=' . $categories_id,'do_page_fetch(\'fetch_sub_products\'',$categories_id).
										'</td>
									</tr>
								</table>
							</td>
						</tr>';
				} 
				echo '</table><div id="panel_' . $categories_id . '_contentp">';
			}
			echo '<table cellpadding="0" cellspacing="0" border="0" width="100%" id="id_product_list_'.$categories_id.'">';			
			if (tep_db_num_rows($products_query)>0){
				$i=0;												
				while ($products = tep_db_fetch_array($products_query)){										
					$class="dataTableRowEven";
					if($i%2==0) $class="dataTableRowOdd";					
					echo '<tr class="'.$class.'" height="18"><td class="main"><div onclick="javascript:if(document.getElementById(\'head_'.$products['products_id'].'_'.$categories_id.'\').innerHTML==\'\') do_actions(\'get_details\','.$products['products_id'].','.$categories_id.');" style="cursor:pointer;cursor:hand;" id="data_'.$products['products_id'].'_'.$categories_id.'">';
					echo  ($products['products_status']==1)?tep_image(DIR_WS_IMAGES  . 'template/icon_active.gif', '') . tep_draw_separator('pixel_trans.gif', '5', '10'):tep_image(DIR_WS_IMAGES  . 'template/icon_inactive.gif', '') . tep_draw_separator('pixel_trans.gif', '5', '10');
					echo  $products['products_name'] . '</div><div id="head_'.$products['products_id'].'_'.$categories_id.'" style="display:none"></div></td></tr>';					
					$i++;									
				}
			}
			echo '</table>';
	}
	
	function output_category_details($cID){
	global $languages_id;
	$product_categories=tep_db_query("select c.parent_id,date_format(c.date_added,'%Y-%m-%d') as date_added,date_format(c.last_modified,'%Y-%m-%d') as last_modified,cd.categories_id,cd.categories_name,c.sort_order,c.categories_image from ".TABLE_CATEGORIES ." c,".TABLE_CATEGORIES_DESCRIPTION." cd where c.categories_id=cd.categories_id and cd.language_id='".(int)$languages_id."' and c.categories_id='".(int)$cID."'");
	if(tep_db_num_rows($product_categories)>0){
		$category_check_sql = "select count(*) as count from " . TABLE_CATEGORIES . " where parent_id=" . $cID;
		$category_check_query = tep_db_query($category_check_sql);
		$category_check_result = tep_db_fetch_array($category_check_query);
		$check_sql = "select count(*) as count from " . TABLE_PRODUCTS . " p," . TABLE_PRODUCTS_TO_CATEGORIES . " pc," . TABLE_CATEGORIES . " c where p.products_id=pc.products_id and c.categories_id=pc.categories_id and c.categories_id=" . $cID;
		$check_query = tep_db_query($check_sql);
		$check_result = tep_db_fetch_array($check_query);
		echo tep_draw_hidden_field('has_products_' . $cID,$check_result['count']+$category_check_result['count']);
		$product=tep_db_fetch_array($product_categories);
    ?>
		<table width='100%' border='0' cellspacing='3' cellpadding='2' class="openContent_top" id="category_details_<?php echo $product['categories_id'];?>">
				<tr heigth='40' class='openContent_top'>
				  <td width='10' valign="center"><a id="category_up_<?php echo $product['categories_id'];?>" href="javascript:do_actions('category_pos_up',<?php echo $product['categories_id'];?>,<?php echo $product['sort_order'];?>);"><img border="0" src="images/template/img_arrow_up.gif" align="top"></a></td>
				  <td width='10' valign="center"><a id="category_down_<?php echo $product['categories_id'];?>" href="javascript:do_actions('category_pos_down',<?php echo $product['categories_id'];?>,<?php echo $product['sort_order'];?>);"><img border="0" src="images/template/img_arrow_down.gif"></a></td>
				  <td align='left' valign='middle' width='528' class='main'>&nbsp;&nbsp;<?php echo $product['categories_name'];?></td>
				  <td width='100' align='left' class='main' valign='top'>
				  	<table border='0' cellspacing='0' cellpadding='0' style='height:30px;display:inline;position:relative;'>
				  	 <tr class='img_move' id='img_ids'>
						 <td class='cell_bg_popm_left' width='10'>&nbsp;</td>						 
						 <td width='10%' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' onclick="javascript:change_class(this,1);do_actions('edit_category',<?php echo $product['categories_id'];?>);"><img alt='Edit Category' title='Edit Category' src='images/template/img_edit.gif'></td>
						 <td width='10%' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' onclick="javascript:change_class(this,1);do_actions('delete_category',<?php echo $product['categories_id'];?>);"><img alt='Delete Category' title='Delete Category' src='images/template/img_trash.gif'></td>						 
						 <td width='10%' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' onclick="javascript:change_class(this,1);do_actions('move_category',<?php echo $product['categories_id'];?>);"><img alt='Move Category' title='Move Category' src='images/template/img_move.gif'></td>
						 <td class='cell_bg_popm_right' width='10'>&nbsp;</td>
				  	 </tr>
				  	</table>
				  </td>
				  <td width='540' align='right' id='img_save_<?php echo $product['categories_id'];?>' style='visibility:hidden;'><input id="save_button_<?php echo $product['categories_id'];?>" type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;&nbsp;<a href="javascript:do_actions('get_details',<?php echo $product['categories_id'];?>);"><img src='images/template/img_closel.gif' alt='close' title='close' border='0'></a>&nbsp;&nbsp;</td>
				  </tr>
				  <tr style='height:10'><td id='loading' style='display:none' colspan='5'><img src='images/24-1.gif' alt='Loading...' title='Loading...'></td></tr>
				  <tr>				  
				  	<td colspan="8" class="main">				  						  
				  		<span id="span_details_<?php echo $product['categories_id'];?>"><?php echo get_category_details($product);?></span>
				  		<span id="cpath_span_<?php echo $product['categories_id'];?>"><input type="hidden" name="cPath" id="cPath" value="<?php echo $product['categories_id'];?>"></span>
					</td>
				   </tr>				   
			</table>
<?php }
	}
	function get_category_details($product){
?>
	<table width='100%' border='0' cellspacing='0' cellpadding='0' class='openContent'>
	  <tr>
		<td colspan='2' valign='top'>
			<table width='50%' border='0' cellspacing='5' cellpadding='3'>
				<tr>
					<td width="30"><?php echo (($product['categories_image']!="")?tep_image(HTTP_SERVER.DIR_WS_CATALOG.'images/'.$product['categories_image'],$product['categories_name'],'100','100'):'');?></td>
					<td width='500' valign='top'>
						<table width='80%' border='0' cellspacing='5' cellpadding='0' class='main'>
							<?php if(format_date($product['date_added'])){ ?>
								<tr>
									<td align='left' width='50%' valign='top' nowrap="true"><?php echo TEXT_DATE_ADDED;?></td>
									<td align='left' width='50%' valign='top' nowrap="true"><?php echo format_date($product['date_added']);?></td>
								</tr>
							<?php }							
							if(format_date($product['last_modified'])){ ?>
								<tr>
									<td align='left' width='50%' nowrap="true"><?php echo TEXT_LAST_MODIFIED;?></td>
									<td align='left' width='50%' nowrap="true"><?php echo format_date($product['last_modified']);?></td>
								</tr>									
							<?php } ?>				
						</table>
					</td>
				</tr>
			</table>
		</td>
 	 </tr>
   </table>
<?php }

 function tep_create_new_product_category(){ ?>
	 <table width="100%" cellpadding="0" cellspacing="0" border="0">
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr><?php echo tep_draw_form('new_category', FILENAME_CATEGORIES, 'cPath=' . $cPath . '&cID=' . $HTTP_GET_VARS['cID'] . '&action=new_category_preview', 'post', ' onSubmit="javascript:return validate_image();" enctype="multipart/form-data"'); ?>
        <td><table border="0" cellspacing="0" cellpadding="2">
<?php
	$languages = tep_get_languages();
    for ($i=0; $i<sizeof($languages); $i++) {
?>
          <tr>
            <td class="main"><?php if ($i == 0) echo TEXT_EDIT_CATEGORIES_NAME; ?></td>
            <td class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;' . tep_draw_input_field('categories_name[' . $languages[$i]['id'] . ']', (($categories_name[$languages[$i]['id']]) ? stripslashes($categories_name[$languages[$i]['id']]) : tep_get_category_name($cInfo->categories_id, $languages[$i]['id']))); ?></td>
          </tr>
<?php
    }
?>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
<?php 
    for ($i=0; $i<sizeof($languages); $i++) {
?>
          <tr>
            <td class="main"><?php if ($i == 0) echo TEXT_EDIT_CATEGORIES_HEADING_TITLE; ?></td>
            <td class="main"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;' . tep_draw_input_field('categories_heading_title[' . $languages[$i]['id'] . ']', (($categories_name[$languages[$i]['id']]) ? stripslashes($categories_name[$languages[$i]['id']]) : tep_get_category_heading_title($cInfo->categories_id, $languages[$i]['id']))); ?></td>
          </tr>
<?php
    }
?>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
<?php
    for ($i=0; $i<sizeof($languages); $i++) {
?>
          <tr>
            <td class="main" valign="top"><?php if ($i == 0) echo TEXT_EDIT_CATEGORIES_DESCRIPTION; ?></td>
            <td><table border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td class="main" valign="top"><?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>&nbsp;</td>
                <td class="main"><?php echo tep_draw_textarea_field('categories_description[' . $languages[$i]['id'] . ']', 'soft', '70', '15', (($categories_description[$languages[$i]['id']]) ? stripslashes($categories_description[$languages[$i]['id']]) : tep_get_category_description($cInfo->categories_id, $languages[$i]['id']))); ?></td>
              </tr>
            </table></td>
          </tr>
<?php
    }
?>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
          <tr>
            <td class="main"><?php echo TEXT_EDIT_CATEGORIES_IMAGE; ?></td>
            <td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . tep_draw_file_field('categories_image') . '<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . $cInfo->categories_image . tep_draw_hidden_field('categories_previous_image', $cInfo->categories_image); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
          <tr>
            <td class="main"><?php echo TEXT_EDIT_SORT_ORDER; ?></td>
            <td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . tep_draw_input_field('sort_order', $cInfo->sort_order, 'size="2"'); ?></td>
          </tr>
          <tr>
            <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
          </tr>
        </table></td>
      </tr>
	  <?php tep_content_title_top("Subscription"); ?>
	<tr>
		<td>
		<table border=0 cellpadding="4" cellspacing="0" width="50%">
			<tr>
				<td class="main" width="150" valign="top"><?php echo TEXT_SUBSCRIPTION_CATEGORIES.'asd'; ?></td>
				<td class="main" width="200"><?php 
				$events_link=true;
				echo tep_draw_subscription_select_menu('subscription_categories_ids[]',$subscription_array, $subscription_categories_array_selected, 'size=10 id=subscription_categories_ids style="width:350"');
				?></td>
			</tr>
		</table>
		</td>
	 </tr>	  
				<?php tep_content_title_bottom(); ?>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td class="main" align="right"><?php echo tep_draw_hidden_field('categories_date_added', (($cInfo->date_added) ? $cInfo->date_added : date('Y-m-d'))) . tep_draw_hidden_field('parent_id', $cInfo->parent_id); ?></form></td>
      </tr>
	  </table>
<?php } ?>