<?php
/*

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce
  
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
 // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); 
 
 require(DIR_WS_CLASSES . 'split_page_results_event.php');
 $server_date = getServerDate(true);
 $command=(isset($HTTP_GET_VARS['command'])?tep_db_prepare_input($HTTP_GET_VARS['command']):'');
 $page=(isset($HTTP_GET_VARS['page'])?(int)$HTTP_GET_VARS['page']:'1');
 $cID=(isset($HTTP_GET_VARS['cID'])?$HTTP_GET_VARS['cID']:0);
 $check_ID=($HTTP_GET_VARS['chk_ID'])?$HTTP_GET_VARS['chk_ID']:0;
 $pID=(isset($HTTP_GET_VARS['pID'])?(int)$HTTP_GET_VARS['pID']:0);
 $level=(isset($HTTP_GET_VARS['level'])?tep_db_prepare_input($HTTP_GET_VARS['level']):0);
 $open_ids=isset($HTTP_GET_VARS["open_ids"])?tep_db_prepare_input($HTTP_GET_VARS["open_ids"]) . ",":'';
 $heading=array();
 $contents=array();
 if($command!=''){
  	$languages = tep_get_languages();
  	$categories=array();
  	if($HTTP_GET_VARS['cID']>0){
		$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, c.catagory_status from  $table_product_categories  c,  $table_product_categories_description  cd where c.categories_id = cd.categories_id and cd.language_id = '" . (int)$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($HTTP_GET_VARS['pID']>0){
		$product_query = tep_db_query("select pd.$product_name_str as product_name, pd.$product_description_str as product_description, pd.$product_shorttext_str as product_shorttext, pd.$product_url_str as product_url, 
										pd.$product_image_text_str as product_image_text, pd.$product_image_text_two_str as product_image_text_two, pd.$product_image_text_three_str as product_image_text_three, p.$product_id_str, p.$product_image_str as product_image, p.$product_image_two_str as product_image_two, 
										p.$product_image_three_str as product_image_three, p.$product_date_added_str as product_date_added, p.$product_last_modified_str as product_last_modified, date_format(p.$product_date_available_str, '%Y-%m-%d') 
										as product_date_available, p.$product_status_str as product_status, p.$product_sticky_str as product_sticky from $table_product  p, $table_product_description pd where p.$product_id_str = '" 
										. (int)$HTTP_GET_VARS['pID'] . "' and p.$product_id_str = pd.$product_id_str 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);
		}			
	}	
	if($command=='edit_category' && !$HTTP_GET_VARS['cID'] && $check_ID>0){		
		$products_query = tep_db_query("select count(*) as total from $table_product p, $table_product_to_categories p2c where p.$product_id_str = p2c.$product_id_str and p2c.categories_id = '" . $check_ID . "'");			 		
		$products=tep_db_fetch_array($products_query);							
		if($products['total']<=0){
			echo 'new_news^^^^';
			$command='edit_product';

		}			
	} 
	switch($command){
		case 'search_details':			
			echo 'command||search_details{}';
			echo output_sub_result($cID,1,'A',0,'',1);
			echo '<div id="errr" align="right"><a href="javascript:expand_category(1,1,\'reset\');">'.tep_image_button('button_reset.gif', IMAGE_RESET).'</a></div>';
			//echo output_products_result($cID,$page,$show_order);			
		break;
		case 'reset';
			echo 'command||search_details{}';
			output_sub_result(0,1,'A',0,'',1);
		break;
		case 'fetch_sub_list':
		case 'fetch_sub_list_open':
			echo 'command||fetch_sub_list{}category||' . tep_output_string($cID) . '{}page||' . tep_output_string($page) . '{}result||';
			echo output_sub_result($cID,$page,$show_order,$level,$open_ids);
			break;
		case 'fetch_all_list':
			$cID=substr($open_ids,0,strpos($open_ids,","));
			echo 'command||' . tep_output_string($command) . '{}category||' . tep_output_string($cID) . '{}page||' . tep_output_string($page) . '{}result||';
			echo output_sub_result($cID,$page,$show_order,$level,$open_ids);
			break;
		case 'fetch_sub_products':
			echo 'command||fetch_sub_products{}category||' . tep_output_string($cID) . '{}page||' . tep_output_string($page) . '{}result||';
			echo output_products_result($cID,$page,$show_order);
			break;
		case 'get_details':
			echo_get_details();
		break;
		case 'get_category_details':
 			$product_categories=tep_db_query("select 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.catagory_status,c.categories_image from $table_product_categories  c, $table_product_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);
		break;
		case 'edit_category':		
			$contents='<table width="100%" border="0" cellspacing="5" cellpadding="5" class="openContent"><tr><td class="main" colspan="'.(count($languages)+1).'"><b>' . (($cInfo->categories_id>0)?TEXT_EDIT_INTRO:TEXT_NEW_CATEGORY_INTRO) . '</b></td>'.(($check_ID==0 && !$HTTP_GET_VARS['cID'])?'<td class="main" align="right"><input type="image" border="0"  src="images/template/img_savel.gif">&nbsp;<a style="cursor:pointer" onClick="do_close_new_category();"><img src="images/template/img_closel.gif" alt="close"></a>&nbsp;</td>':'').'</tr><tr>';
			$languages = tep_get_languages();			
			if($cInfo->categories_id<=0) $contents.="<td class='main'>".TEXT_MAIN_CATEGORIES.'<br>'.tep_draw_checkbox_field('main_category','Y')."</td>";
			for ($i = 0, $n = sizeof($languages); $i < $n; $i++)
				$contents.= '<td class="main" align="left" nowrap="true">'.(($cInfo->categories_id>0)?TEXT_EDIT_CATEGORIES_NAME:TEXT_CATEGORIES_NAME).tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;<br>' . tep_draw_input_field('categories_name[' . $languages[$i]['id'] . ']',$cInfo->categories_name,'maxlength="32"').'</td>';						
				$contents.='<td class="main" align="left">' .(($cInfo->categories_id>0)?TEXT_EDIT_CATEGORIES_IMAGE:TEXT_CATEGORIES_IMAGE).'('.((strlen($cInfo->categories_image)>20)?substr($cInfo->categories_image,0,20).'...':$cInfo->categories_image) .')&nbsp;<br>'.tep_draw_file_field('categories_image').'</td></tr></table>';			
			/*if($check_ID==0 && !$HTTP_GET_VARS['cID']){
				echo 'cat^^^^' . $contents;	
			}else*/
				echo $contents;			
		break;	
		case 'file_upload':
			if($HTTP_POST_VARS['action_type']=="new_category" || $HTTP_POST_VARS['action_type']=="edit_category"){
				$cms_news = new upload('categories_image');
				$cms_news->set_destination(DIR_FS_CATALOG_IMAGES);
				$sql_data_array=array();					
				if ($cms_news->parse() && $cms_news->save()) $cms_filename=$cms_news->filename;								
				if($filename && $HTTP_POST_VARS['action_type']=='new_category'){
					$sql_data_array=array('categories_image'=>$cms_filename,'date_added'=>$server_date,'parent_id'=>($HTTP_POST_VARS['main_category']=='Y')?0:tep_db_prepare_input($HTTP_POST_VARS['cPath']));
					tep_db_perform($table_product_categories, $sql_data_array);					
					$insert_id=tep_db_insert_id();
					for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
							$language_id = $languages[$i]['id'];						
							$insert_sql_data = array(
								'categories_id' => $insert_id,
								'language_id' => $languages[$i]['id']);											
						tep_db_perform($table_product_categories_description, $insert_sql_data);
					}
					echo tep_output_string($insert_id);  
				} else if($filename && $HTTP_POST_VARS['action_type']=='edit_category') 			
					tep_db_query("update " . $table_product_categories . " set categories_image = '" . $cms_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"){
 				 	$cms_news = new upload('product_image');
					$cms_news->set_destination(DIR_FS_CATALOG_IMAGES);
					$cms_news->parse();
					$cms_news->save();
					$cms_news_2 = new upload('product_image_two');
					$cms_news_2->set_destination(DIR_FS_CATALOG_IMAGES);
					$cms_news_2->parse();
					$cms_news_2->save();
					$cms_news_3 = new upload('product_image_three');
					$cms_news_3->set_destination(DIR_FS_CATALOG_IMAGES);
					$cms_news_3->parse();
					$cms_news_3->save();					
					$sql_data_array=array("$product_date_added_str"=>$server_date,"$product_image_str"=>$cms_news->filename,"$product_image_two_str"=>$cms_news_2->filename,"$product_image_three_str"=>$cms_news_3->filename);		
					if($HTTP_POST_VARS['action_type']=='new'){
						tep_db_perform($table_product, $sql_data_array);						
						$product_id=tep_db_insert_id();
						echo $product_id;
						tep_db_query("insert into " . $table_product_to_categories . " ($product_id_str, categories_id) values ('" . $product_id . "', '" . tep_db_input(tep_db_prepare_input($HTTP_POST_VARS['cPath'])) . "')");
						for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
							$language_id = $languages[$i]['id'];
							$insert_sql_data = array(
								"$product_id_str" => $product_id,
								"language_id" => $language_id
							);
							tep_db_perform($table_product_description, $insert_sql_data);
						}					  
					} else if($HTTP_POST_VARS['action_type']=='edit')
						tep_db_perform($table_product, $sql_data_array, 'update', "$product_id_str ='" . tep_db_input($HTTP_POST_VARS['product_id'])."'");
			}
		break;
		case 'update_category':
			$categories_id = $cID;
			$current_category_id=(int)$HTTP_POST_VARS['cPath'];
			$sort_query=tep_db_query("select max(sort_order) as sort_order from ".$table_product_categories);
			if(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 tep_output_string($HTTP_POST_VARS['action_type']);
			$sql_data_array = array('sort_order' => $sort_order, 'catagory_status' => 1);													
			if($HTTP_GET_VARS['cID']>0) $sql_data_array = array('catagory_status' => 1);			
			if (!$HTTP_GET_VARS['cID']) { 
				$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_product_categories, $sql_data_array);
				$insert_id = tep_db_insert_id();												
			} elseif ($HTTP_GET_VARS['cID']>0) {
				$update_sql_data = array('last_modified' => $server_date);
				$sql_data_array = array_merge($sql_data_array, $update_sql_data);
				tep_db_perform($table_product_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 = $HTTP_POST_VARS['categories_name'];
				$language_id = $languages[$i]['id'];
				$sql_data_array = array('categories_name' => $categories_name_array[$language_id]);
				if (!$HTTP_GET_VARS['cID']) {
					$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_product_categories_description, $sql_data_array);
				} elseif ($HTTP_GET_VARS['cID']>0) {
					tep_db_perform($table_product_categories_description, $sql_data_array, 'update', 'categories_id = \'' . $categories_id . '\' and language_id = \'' . $languages[$i]['id'] . '\'');
					$insert_id=tep_db_prepare_input($HTTP_GET_VARS['cID']);
				}
			}			
			if($HTTP_POST_VARS['action_type']=='new_category'){ 
			echo tep_output_string($insert_id).'{}'.tep_output_string($categories_name_array[$languages_id]).'{}'.tep_output_string($current_category_id);
			}
			else if($HTTP_POST_VARS['action_type']=='edit_category'){
				echo tep_output_string($categories_id).'{}'.$categories_name_array[$languages_id].'{}';								
 				echo get_category_details($categories);
			} 
		break;	
		case '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>';
			if($product_id_str=='newsdesk_id') {
				$category_childs = newsdesk_childs_in_category_count($cInfo->categories_id);
				$category_products =newsdesk_products_in_category_count($cInfo->categories_id);						
			}else if($product_id_str=='faqdesk_id') {
				$category_childs = faqdesk_childs_in_category_count($cInfo->categories_id);
				$category_products =faqdesk_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_NEWSDESK, $category_products).'</td></tr>';
			echo $contents;								
		break;		
		case 'delete_category_confirm':
				if ($HTTP_GET_VARS['cID']) {
				$categories_id = tep_db_prepare_input($HTTP_GET_VARS['cID']);				
				if($product_id_str=='newsdesk_id') $categories = newsdesk_get_category_tree($categories_id, '', '0', '', true);
				else if($product_id_str=='faqdesk_id') $categories = faqdesk_get_category_tree($categories_id, '', '0', '', true);				
				$products = array();
				$products_delete = array();				
				for ($i = 0, $n = sizeof($categories); $i < $n; $i++) {
					$product_ids_query = tep_db_query("select $product_id_str from " . $table_product_to_categories . " where categories_id = '" . $categories[$i]['id'] . "'");
					while ($product_ids = tep_db_fetch_array($product_ids_query)) {
						$products[$product_ids["$product_id_str"]]['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_product_to_categories . " where $product_id_str = '" . $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++) {
					if($product_id_str=='newsdesk_id') newsdesk_remove_category($categories[$i]['id']);
					else if($product_id_str=='faqdesk_id') faqdesk_remove_category($categories[$i]['id']);
				}
				
				reset($products_delete);
				while (list($key) = each($products_delete)) {
					if($product_id_str=='newsdesk_id') newsdesk_remove_product($key);
					else if($product_id_str=='faqdesk_id') faqdesk_remove_product($key);
				}				
				}  // main if closing bracket
				if($categories_id!=$cInfo->parent_id) $parent_id=$cInfo->parent_id;
				echo 'delete_category_success'.tep_output_string($categories_id);
		break;
		case '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', (($product_id_str=='newsdesk_id')?newsdesk_get_category_tree('0', '', $cInfo->categories_id):faqdesk_get_category_tree('0', '', $cInfo->categories_id)), $current_category_id).'</td></tr>';			
			echo $contents;
		break;
		case 'move_category_confirm':				
			if ($cID  && ($cID != $HTTP_POST_VARS['move_to_category_id'])){				
				$new_parent_id = tep_db_prepare_input($HTTP_POST_VARS['move_to_category_id']);
				tep_db_query("update " . $table_product_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'.tep_output_string($new_parent_id);
			}  // top if closing bracket				
		break;
		case 'edit_product':
						
			products_new_edit();
		break;
		case 'update':
			// Another double case situation -- must be an all in one mentality!
			if($HTTP_POST_VARS['action_type']=='new') echo tep_output_string($HTTP_POST_VARS['action_type']).'_product'; 			
			$pID = tep_db_prepare_input($HTTP_GET_VARS['pID']);			
			$product_date_available = ($HTTP_POST_VARS['product_date_available'])?tep_db_prepare_input($HTTP_POST_VARS['product_date_available']):$server_date;					
			$sql_data_array = array(					
					$product_date_available_str => tep_convert_date_raw($product_date_available),
					$product_status_str => tep_db_prepare_input($HTTP_POST_VARS['product_status']),
					$product_sticky_str => tep_db_prepare_input($HTTP_POST_VARS['product_sticky']),
				);						
			if (!$HTTP_GET_VARS['pID']) {
				$insert_sql_data = array($product_date_added_str => $server_date);
				$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
				tep_db_perform($table_product, $sql_data_array);
				$pID = tep_db_insert_id();
				tep_db_query("insert into " . $table_product_to_categories . " ($product_id_str, categories_id) values ('" . $pID . "', '" . tep_db_input($cID) . "')");				
			} elseif ($HTTP_GET_VARS['pID']>0) {
				$update_sql_data = array($product_last_modified_str => $server_date);
				$sql_data_array = array_merge($sql_data_array, $update_sql_data);
				tep_db_perform($table_product, $sql_data_array, 'update', "$product_id_str = '" . tep_db_input($pID) . "'");				
			}			
			$languages = tep_get_languages();
			for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
				$language_id = $languages[$i]['id'];
				$description = '';
				$shorttext = '';
				if($product_id_str == 'faqdesk_id'){
					$description = tep_db_prepare_input($HTTP_POST_VARS['faqdesk_answer_long'][$language_id][0]);
					$shorttext = tep_db_prepare_input($HTTP_POST_VARS['faqdesk_answer_short'][$language_id][0]);
				}else if($product_id_str == 'newsdesk_id'){
					$description = tep_db_prepare_input($HTTP_POST_VARS['newsdesk_article_description'][$language_id][0]);
					$shorttext = tep_db_prepare_input($HTTP_POST_VARS['newsdesk_article_shorttext'][$language_id][0]);
				}
				$sql_data_array = array(
						$product_name_str => tep_db_prepare_input($HTTP_POST_VARS['product_name'][$language_id]),
						$product_description_str => $description,
						$product_shorttext_str => $shorttext,
						$product_url_str => tep_db_prepare_input($HTTP_POST_VARS['product_url'][$language_id]),
						$product_image_text_str => tep_db_prepare_input($HTTP_POST_VARS['product_image_text'][$language_id]),
						$product_image_text_two_str => tep_db_prepare_input($HTTP_POST_VARS['product_image_text_two'][$language_id]),
						$product_image_text_three_str => tep_db_prepare_input($HTTP_POST_VARS['product_image_text_three'][$language_id]),
					);
				
		//		print_r($sql_data_array);	
					
				if (!$HTTP_GET_VARS['pID']) {
					$insert_sql_data = array(
						$product_id_str => $pID,
						"language_id" => $language_id
					);
					$sql_data_array = array_merge($sql_data_array, $insert_sql_data);					
					tep_db_perform($table_product_description, $sql_data_array);
				} elseif ($HTTP_GET_VARS['pID']>0){
					$check_sql = "select count(*) as count from " . $table_product_description . " where $product_id_str = '" . tep_db_input($pID) . "' and language_id = '" . (int)$language_id . "'";
					$check_query = tep_db_query($check_sql);
					$check_result = tep_db_fetch_array($check_query);
					if($check_result['count']>0){
						tep_db_perform($table_product_description, $sql_data_array, 'update', "$product_id_str = '" . tep_db_input($pID) . "' and language_id = '" . (int)$language_id . "'");
					}else{
						$insert_sql_data = array(
							$product_id_str => $pID,
							"language_id" => $language_id
						);
						$sql_data_array = array_merge($sql_data_array, $insert_sql_data);	
						tep_db_perform($table_product_description, $sql_data_array);
					}
				}
			  }
			  echo_get_details();	
		break;
		case 'delete_product':
			$contents='<table width="100%" class="openContent" border="0" cellspacing="3" cellpadding="2"><tr><td width="10" align="right">'.tep_image('images/template/img_trash.gif').'</td><td class="main" colspan="4"><b>' . TEXT_DELETE_PRODUCT_INTRO . '</b></td></tr><tr>';									
			$contents.='<tr><td></td><td class="main">' . $pInfo->$product_name_str . '</td></tr>';
			$product_categories_string ="";								
			if($product_id_str=='newsdesk_id') $product_categories = newsdesk_generate_category_path($pInfo->$product_id_str, 'product');
			else if($product_id_str=='faqdesk_id') $product_categories = faqdesk_generate_category_path($pInfo->$product_id_str, 'product');
			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 .='<tr><td></td><td class="main" align="left"">'. tep_draw_checkbox_field('product_categories[]', $product_categories[$i][sizeof($product_categories[$i])-1]['id'], true) . '&nbsp;' . $category_path . '</td></tr>';
			}
			$contents.=$product_categories_string.'<tr><td colspan="2">'.tep_draw_separator("pixel_trans.gif",50,10).'</td></tr></table>';
			echo $contents;
		break;
		case 'delete_confirm':			
			if ($pID && (is_array($HTTP_POST_VARS['product_categories']))) {			
				$product_categories = tep_db_prepare_input($HTTP_POST_VARS['product_categories']);			
				for ($i = 0, $n = sizeof($product_categories); $i < $n; $i++) {
					tep_db_query("delete from " . $table_product_to_categories . " where $product_id_str = '" . tep_db_input($pID) . "' and categories_id = '" . tep_db_input($product_categories[$i]) . "'");
				}
				$product_categories_query = tep_db_query("select count(*) as total from " . $table_product_to_categories . " where $product_id_str = '" . tep_db_input($pID) . "'");
				$product_categories = tep_db_fetch_array($product_categories_query);			
				if ($product_categories['total'] == '0') {
					if($product_id_str=='newsdesk_id') newsdesk_remove_product($pID);
					else if($product_id_str=='faqdesk_id') faqdesk_remove_product($pID);
				}						
				if ($HTTP_POST_VARS['delete_image'] == 'yes') unlink(DIR_FS_CATALOG_IMAGES . tep_db_prepare_input($HTTP_POST_VARS['products_previous_image']));						
				echo 'delete_success'.tep_output_string($pID);
			}  // top if closing bracket			
		break;
		case 'copy_product':
			if($product_id_str=='newsdesk_id'){
				$current_categories=newsdesk_output_generated_category_path($pInfo->$product_id_str, 'product');
				$current_categories_tree=newsdesk_get_category_tree();
			}else if($product_id_str=='faqdesk_id'){
				$current_categories=faqdesk_output_generated_category_path($pInfo->$product_id_str, 'product');
				$current_categories_tree=faqdesk_get_category_tree();				
			}    
			$contents='<table width="100%" class="openContent" border="0" cellspacing="3" cellpadding="2"><tr><td width="10" align="right">'.tep_image('images/template/img_copy.gif').'</td><td class="main" colspan="4"><b>' . TEXT_INFO_COPY_TO_INTRO . '</b></td></tr><tr>';									
			$contents.='<tr><td></td><td class="main">' . TEXT_INFO_CURRENT_CATEGORIES . '&nbsp;'.$current_categories.'</td></tr>';
			$contents.='<tr><td></td><td class="main">' . TEXT_CATEGORIES . '&nbsp;'.tep_draw_pull_down_menu('categories_id', $current_categories_tree , $cID).'</td></tr>';
			$contents.='<tr><td></td><td class="main">' . TEXT_HOW_TO_COPY . '&nbsp;'.tep_draw_radio_field('copy_as', 'link', true) . ' ' . TEXT_COPY_AS_LINK .'&nbsp;'.tep_draw_radio_field('copy_as', 'duplicate') . ' ' . TEXT_COPY_AS_DUPLICATE.'</td></tr>';
			$contents.= '<tr><td colspan="2">'.tep_draw_separator("pixel_trans.gif",50,10).'</td></tr></table>';
			echo  $contents;											
		break;
		case 'copy_confirm':		
			$result="copy_details";
			if ($pID && (tep_not_null($HTTP_POST_VARS['categories_id']))) {				
				$categories_id = tep_db_prepare_input($HTTP_POST_VARS['categories_id']);							
					if ($HTTP_POST_VARS['copy_as'] == 'link') {
						if ($categories_id != $cID) {
								$check_query = tep_db_query("select count(*) as total from " . $table_product_to_categories . " where $product_id_str = '" . tep_db_input($pID) . "' and categories_id = '" . tep_db_input($categories_id) . "'");
								$check = tep_db_fetch_array($check_query);
								if ($check['total'] < '1') 
									tep_db_query("insert into " . $table_product_to_categories . " ($product_id_str, categories_id) values ('" . tep_db_input($pID) . "', '" . tep_db_input($categories_id) . "')");
							$result.='copy_success';														
						} else $result.=ERROR_CANNOT_LINK_TO_SAME_CATEGORY;					
					} elseif ($HTTP_POST_VARS['copy_as'] == 'duplicate') {			
						$product_query = tep_db_query("select $product_image_str, $product_image_two_str, $product_image_three_str, $product_date_added_str, $product_date_available_str, $product_status_str, $product_sticky_str 
													   from " . $table_product . " where $product_id_str = '" . tep_db_input($pID) . "'");
						$product = tep_db_fetch_array($product_query);						
						tep_db_query("insert into " . $table_product . " ($product_image_str, $product_image_two_str, $product_image_three_str, $product_date_added_str, $product_date_available_str, 
									 $product_status_str, $product_sticky_str) values ('" . $product[$product_image_str] . "','" . $product[$product_image_two_str] . "',
									 '" . $product[$product_image_three_str] . "', '" . $product[$product_date_added_str]  . "', '" . $product[$product_date_available_str] . "', 
									 '" . $product[$product_status_str] . "', '" . $product[$product_sticky_str] . "')");
						$dup_product_id = tep_db_insert_id();
						$description_query = tep_db_query("select language_id, $product_name_str, $product_description_str, $product_url_str, $product_image_text_str, $product_image_text_two_str, 
														   $product_image_text_three_str, $product_viewed_str, $product_shorttext_str from " . $table_product_description . " where $product_id_str = '" . 
														   tep_db_input($pID) . "'");
						
						while ($description = tep_db_fetch_array($description_query)) {
										tep_db_query("insert into " . $table_product_description . " ($product_id_str, language_id, $product_name_str, 
													$product_description_str, $product_url_str, $product_image_text_str, $product_image_text_two_str, $product_image_text_three_str, 
													$product_viewed_str, $product_shorttext_str) values ('" . $dup_product_id . "', '" . $description['language_id'] . "', '" 
													. addslashes($description[$product_name_str]) . "', '" . addslashes($description[$product_description_str]) . "', 
													'" . tep_db_input($description[$product_url_str]) . "', '" . tep_db_input($description[$product_image_text_str]) . "', '" . tep_db_input($description[$product_image_text_two_str]) . "', 
													'" . tep_db_input($description[$product_image_text_three_str]) . "', '" . tep_db_input($description[$product_viewed_str]) . "', 
													'" . tep_db_input($description[$product_shorttext_str]) . "')");
						}				
						tep_db_query("insert into " . $table_product_to_categories . " ($product_id_str, categories_id) values ('" . $dup_product_id . "', '" . tep_db_input($categories_id) . "')");
						$pID = $dup_product_id;
						$result.='copy_success';
					}			
			}  // top closing if bracket
			echo $result;			
		break;
		case 'move_product':
			if($product_id_str=='newsdesk_id'){
				$categories_output=newsdesk_output_generated_category_path($pInfo->$product_id_str, 'product');
				$categories_output_tree=newsdesk_get_category_tree();
			}else if($product_id_str=='faqdesk_id') {
				$categories_output=faqdesk_output_generated_category_path($pInfo->$product_id_str, 'product');					
				$categories_output_tree=faqdesk_get_category_tree();
			}
			$contents='<table width="100%" class="openContent" border="0" cellspacing="3" cellpadding="2"><tr><td width="10" align="right">'.tep_image('images/template/img_move.gif').'</td><td class="main" colspan="4"><b>' . sprintf(TEXT_MOVE_NEWSDESK_INTRO, $pInfo->$product_name_str) . '</b></td></tr><tr>';									
			$contents.='<tr><td></td><td class="main">' . TEXT_INFO_CURRENT_CATEGORIES . '&nbsp;'.$categories_output.'</td></tr>';				
			$contents.='<tr><td></td><td class="main">' . sprintf(TEXT_MOVE, $pInfo->$product_name_str) . '&nbsp;'.tep_draw_pull_down_menu('move_to_category_id', $categories_output_tree , $cID).'</td></tr>';
			$contents.= '<tr><td colspan="2">'.tep_draw_separator("pixel_trans.gif",50,10).'</td></tr></table>';
			echo $contents;									
		break;
		case 'move_product_confirm':			
			$new_parent_id = tep_db_prepare_input($HTTP_POST_VARS['move_to_category_id']);
			$duplicate_check_query = tep_db_query("select count(*) as total from " . $table_product_to_categories . " where $product_id_str = '" . tep_db_input($pID) . "' and categories_id = '" . tep_db_input($new_parent_id) . "'");
			$duplicate_check = tep_db_fetch_array($duplicate_check_query);
			if ($duplicate_check['total'] < 1) tep_db_query("update " . $table_product_to_categories . " set categories_id = '" . tep_db_input($new_parent_id) . "' where $product_id_str = '" . tep_db_input($pID) . "' and categories_id = '" . tep_db_input($cID) . "'");
			echo 'move_product_success'.tep_ouput_string($new_parent_id);
		break;							
		case 'setflag':
		 if (($HTTP_GET_VARS['flag'] == '0') || ($HTTP_GET_VARS['flag'] == '1')) {
			if ($HTTP_GET_VARS['pID']) {
				if($product_id_str=='newsdesk_id') newsdesk_set_product_status(tep_db_prepare_input($HTTP_GET_VARS['pID']), tep_db_prepare_input($HTTP_GET_VARS['flag']));
				else if($product_id_str=='faqdesk_id') faqdesk_set_product_status(tep_db_prepare_input($HTTP_GET_VARS['pID']), tep_db_prepare_input($HTTP_GET_VARS['flag']));
			}			
			if ($HTTP_GET_VARS['cID']) {
				if($product_id_str=='newsdesk_id') newsdesk_set_categories_status(tep_db_prepare_input($HTTP_GET_VARS['cID']), tep_db_prepare_input($HTTP_GET_VARS['flag']));	
				else if($product_id_str=='faqdesk_id') faqdesk_set_categories_status(tep_db_prepare_input($HTTP_GET_VARS['cID']), tep_db_prepare_input($HTTP_GET_VARS['flag']));
			}			
		}			
		break;
		case 'setflag_sticky':			
			if ( ($HTTP_GET_VARS['flag'] == '0') || ($HTTP_GET_VARS['flag'] == '1') ) {
				if ($HTTP_GET_VARS['pID']) {
					if($product_id_str=='newsdesk_id') newsdesk_set_product_sticky(tep_db_prepare_input($HTTP_GET_VARS['pID']), tep_db_prepare_input($HTTP_GET_VARS['flag']));
					else if($product_id_str=='faqdesk_id') faqdesk_set_product_sticky(tep_db_prepare_input($HTTP_GET_VARS['pID']), tep_db_prepare_input($HTTP_GET_VARS['flag']));
				}							
			}
		break;
		case 'pos_up':
		case 'pos_down':		 		
			  $result='sort_order_error';	
			  $position = isset($HTTP_GET_VARS['pos'])?tep_db_prepare_input($HTTP_GET_VARS['pos']):0;
			  if (trim($command) == 'pos_up')
					$sort_query=tep_db_query("select sort_order, categories_id from " . $table_product_categories . " where  sort_order<" . tep_db_input($position) . " order by sort_order desc limit 1");
			  else if($command=='pos_down')
					$sort_query=tep_db_query("select sort_order, categories_id from " . $table_product_categories. " where sort_order>" . tep_db_input($position) . " order by sort_order limit 1");							
				if (tep_db_num_rows($sort_query)>0){						
						$sort_result = tep_db_fetch_array($sort_query);
						$prev_order=$sort_result['sort_order'];
						$result = tep_db_query("update " . $table_product_categories . " set sort_order='" . tep_db_input($position) ."' where categories_id='" . (int)$sort_result['categories_id'] . "'");
						if($result<0) $result='error';												
						$result = tep_db_query("update " . $table_product_categories . " set sort_order='" . (int)$sort_result['sort_order'] . "' where categories_id='" . tep_db_input($cID) ."'");
						if($result<0) $result='error';						 
						$result="sort_order_success".$prev_order.'{}';
						$cquery=tep_db_query("select parent_id from ".$table_product_categories." where categories_id='".tep_db_input($cID)."'");
						if(tep_db_num_rows($cquery)>0){
							$cresult=tep_db_fetch_array($cquery);
							$result.=$cresult['parent_id'];
						}
			    }	
			   echo $result; 	 	
		 break;
	}	
	exit;  
  }  
  function echo_get_details(){
   	 global $pID,$languages_id,$cID,$table_product,$table_product_description,$table_product_categories,$table_product_categories_description,$product_id_str,$product_name_str,$product_description_str,$product_date_added_str,$product_last_modified_str,$product_date_available_str,$product_image_str,$product_status_str,$product_sticky_str;
   	 	$str_vals="";
		$product_query = tep_db_query("select n.$product_id_str,nd.$product_name_str as product_name,nd.$product_description_str as product_description,date_format(n.$product_date_added_str,'%Y-%m-%d') as product_date_added,date_format(n.$product_last_modified_str,'%Y-%m-%d') as product_last_modified, date_format(n.$product_date_available_str,'%Y-%m-%d') as product_date_available,n.$product_image_str as product_image, n.$product_status_str as product_status, n.$product_sticky_str as product_sticky from " . $table_product . " n,".$table_product_description." nd where n.$product_id_str=nd.$product_id_str and n.$product_id_str = '" . tep_db_input($pID) . "' and nd.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,c.sort_order,c.catagory_status from ".$table_product_categories." c,".$table_product_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.'{}';
		  	}
		  }
		  while(list($key,$vals)=each($product)){
			if($key=='product_date_added' || $key=='product_last_modified' || $key=='product_date_available')
				$vals=format_date($vals);			
			$str_vals.=$vals.'{}';
		 }		 
	   }else $str_vals="No Record Found";
	   echo 'get_details'.$str_vals;
  }
  function products_new_edit(){
   global $HTTP_GET_VARS,$languages_id,$pInfo,$table_product,$cID,$product_id_str,$check_ID,$product_status_str,$product_sticky_str,$product_image_str,$product_image_two_str,$product_image_three_str;

	$product=array();	
	if(!$cID) $cID=$check_ID;	
	$languages = tep_get_languages();	
	switch ($pInfo->product_status) {
		case '0': $in_status = false; $out_status = true; break;
		case '1':
		default: $in_status = true; $out_status = false;
	}	
	switch ($pInfo->product_sticky) {
		case '0': $sticky_on = false; $sticky_off = true; break;
		case '1': $sticky_on = true; $sticky_off = false; break;
		default: $sticky_on = false; $sticky_off = true;
	}	
 ?>	
 <table border="0" width="100%" cellspacing="3" cellpadding="3">
 	<tr>
 	 <td>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
		 <tr>
			<td class="pageHeading"><?php echo (($product_id_str=='newsdesk_id')?sprintf(TEXT_NEW_NEWSDESK, newsdesk_output_generated_category_path($cID)):sprintf(TEXT_NEW_FAQDESK, faqdesk_output_generated_category_path($cID))); ?></td>
			<td class="pageHeading" align="right"></td>
		</tr>
	</table>
	<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<tr class="headerBar">
			<td class="headerBarContent"><?php echo TEXT_NEWSDESK_HEADLINE; ?></td>
		</tr>
	</table>
	<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<?php for ($i = 0, $n = sizeof($languages); $i < $n; $i++) { ?>
			<tr>
				<td class="main">
		<?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
				</td>
				<td class="main">
		<?php if($product_id_str=='newsdesk_id') $pro_name=(($newsdesk_article_name[$languages[$i]['id']]) ?stripslashes($newsdesk_article_name[$languages[$i]['id']]) : newsdesk_get_newsdesk_article_name($pInfo->$product_id_str, $languages[$i]['id']));
			  else if($product_id_str=='faqdesk_id') $pro_name=(($faqdesk_article_name[$languages[$i]['id']]) ?stripslashes($faqdesk_article_name[$languages[$i]['id']]) : faqdesk_get_faqdesk_question($pInfo->$product_id_str, $languages[$i]['id']));
			  echo tep_draw_input_field('product_name[' . $languages[$i]['id'] . ']', $pro_name , 'maxlength="64" size="50"'); ?>
				</td>
			</tr>
		<?php
		} //  ........... <<<< .............. end of products name language loop
		?>
	</table>
	<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<tr class="headerBar">
			<td class="headerBarContent"><?php echo TEXT_NEWSDESK_SUMMARY; ?></td>
		</tr>
	</table>
	<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<?php 
		for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
		?>		
			<tr>
				<td class="main" valign="top">
		<?php
		echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']);
		?>
				</td>
				<td class="main">		
		<?php 
			if($product_id_str=='newsdesk_id') require(DIR_WS_INCLUDES . 'modules/newsdesk/html_editor/summary_bb.php');
			else if($product_id_str=='faqdesk_id') require(DIR_WS_INCLUDES . 'modules/faqdesk/html_editor/summary_bb.php');					
		?>		
				</td>
			</tr>
		<?php
		} // ........... <<<< .............. end of products description language loop
		?>
	</table>
	<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<tr class="headerBar">
			<td class="headerBarContent"><?php echo TEXT_NEWSDESK_CONTENT; ?></td>
		</tr>
	</table>
	<table border="0" width="100%" cellspacing="3" cellpadding="3">
	<?php for ($i = 0, $n = sizeof($languages); $i < $n; $i++) { ?>

		<tr>
			<td class="main" valign="top">
	<?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
			</td>
			<td class="main">
	<?php 	
	if($product_id_str=='newsdesk_id') require(DIR_WS_INCLUDES . 'modules/newsdesk/html_editor/content_bb.php');
	else if($product_id_str=='faqdesk_id') require(DIR_WS_INCLUDES . 'modules/faqdesk/html_editor/content_bb.php');
	?>
			</td>
		</tr>
	<?php } // ........... <<<< .............. end of products description language loop 
	?>
	</table>
	 </td>
	 <td class="main" width="50%" valign="top">
			<table border="0" width="100%" cellspacing="3" cellpadding="3">
				<tr class="headerBar">
					<td class="headerBarContent"><?php echo (($product_id_str=='newsdesk_id')?TEXT_NEWSDESK_STATUS:TEXT_FAQDESK_STATUS); ?></td>
				</tr>
			</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr>
				<td class="main">
					<?php echo tep_draw_radio_field('product_status', '1', $in_status) . '&nbsp;' . TEXT_NEWSDESK_AVAILABLE; ?>
					<?php echo tep_draw_radio_field('product_status', '0', $out_status) . '&nbsp;' . TEXT_NEWSDESK_NOT_AVAILABLE; ?>
				</td>
			</tr>			
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr class="headerBar">
				<td class="headerBarContent"><?php echo TEXT_NEWSDESK_STICKY; ?></td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr>
				<td class="main">
					<?php echo tep_draw_radio_field('product_sticky', '1', $sticky_on) . '&nbsp;' . TEXT_NEWSDESK_STICKY_ON; ?>
					<?php echo tep_draw_radio_field('product_sticky', '0', $sticky_off) . '&nbsp;' . TEXT_NEWSDESK_STICKY_OFF; ?>
				</td>
			</tr>			
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr class="headerBar">
				<td class="headerBarContent"><?php echo TEXT_DATE_FORMAT; ?>&nbsp;&nbsp;<small>(YYYY-MM-DD)</small></td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr>
				<td class="main" width="25%"><?php echo TEXT_NEWSDESK_START_DATE; ?></td>
				<td class="main">	
					<?php 
					$_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
					<?php echo tep_draw_input_field("product_date_available",(($pInfo->product_date_available)?format_date($pInfo->product_date_available):format_date($current_date)),"size=10",false,'text',false);?>
					<a href="javascript:show_calendar('new_product.product_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"/>  
					</a>				
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr class="headerBar">
				<td class="headerBarContent"><?php echo TEXT_NEWSDESK_URL . '&nbsp;&nbsp;<small>' . TEXT_NEWSDESK_URL_WITHOUT_HTTP . '</small>'; ?></td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			</tr>
		<?php for ($i = 0, $n = sizeof($languages); $i < $n; $i++) { ?>
			<tr>
				<td class="main">
		<?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
				</td>
				<td class="main">
		<?php 
		if($product_id_str=='newsdesk_id') $pro_url=(($newsdesk_article_url[$languages[$i]['id']]) ? stripslashes($newsdesk_article_url[$languages[$i]['id']]) : newsdesk_get_newsdesk_article_url($pInfo->$product_id_str, $languages[$i]['id']));
		else if($product_id_str=='faqdesk_id') $pro_url=(($faqdesk_article_url[$languages[$i]['id']]) ? stripslashes($faqdesk_article_url[$languages[$i]['id']]) : faqdesk_get_faqdesk_extra_url($pInfo->$product_id_str, $languages[$i]['id']));
		echo tep_draw_input_field('product_url[' . $languages[$i]['id'] . ']', $pro_url , 'maxlength="255" size="45"'); ?>
				</td>
			</tr>
		<?php } // ........... <<<< .............. end of loop for article url?>
			<tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr class="headerBar">
				<td class="headerBarContent"><?php echo (($product_id_str=='newsdesk_id')?TEXT_NEWSDESK_IMAGE:TEXT_FAQDESK_IMAGE); ?></td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?>
				</td>
			</tr>
			<tr class="main">
				<td class="" colspan="2"><?php echo TEXT_NEWSDESK_IMAGE_ONE; ?></td>
			</tr>
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?>
				</td>
			</tr>
			<tr>
				<td class="main">
		<?php
		if($product_id_str=='newsdesk_id') echo newsdesk_draw_file_field('product_image', 'size="40"'); 
		else if($product_id_str=='faqdesk_id') echo faqdesk_draw_file_field('product_image', 'size="40"');
		 ?>
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<?php for ($i = 0, $n = sizeof($languages); $i < $n; $i++) { ?>
			<tr class="">
				<td class="main" colspan="2"><?php echo TEXT_NEWSDESK_IMAGE_SUBTITLE; ?></td>
			</tr>
			<tr>
				<td class="main">
		<?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
				</td>
				<td class="main">
		<?php
		if($product_id_str=='newsdesk_id') $pro_image_text=(($newsdesk_image_text[$languages[$i]['id']]) ? stripslashes($newsdesk_image_text[$languages[$i]['id']]) : newsdesk_get_newsdesk_image_text($pInfo->$product_id_str, $languages[$i]['id']));
		else if($product_id_str=='faqdesk_id') $pro_image_text=(($faqdesk_image_text[$languages[$i]['id']]) ? stripslashes($faqdesk_image_text[$languages[$i]['id']]) : faqdesk_get_faqdesk_image_text($pInfo->$product_id_str, $languages[$i]['id'])); 
		echo tep_draw_input_field('product_image_text[' . $languages[$i]['id'] . ']', $pro_image_text, 'size="50"'); ?>
				</td>
			</tr>
		<?php
		} //  ........... <<<< .............. end of newsdesk_image_text loop
		?>
		</table>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td class="main">
		<?php
		echo tep_draw_hidden_field('products_previous_image', $pInfo->$product_image_str);
		if($pInfo->$product_image_str)
			echo tep_image(DIR_WS_CATALOG_IMAGES . $pInfo->$product_image_str);
		?>
				</td>
			</tr>
			<tr>
				<td class="main">
		<?php
		if($pInfo->$product_image_str)
			 echo $pInfo->$product_image_str;
		?>
				</td>
			</tr>
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?>
				</td>
			</tr>
			<tr>
				<td class="headerBar">
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '1'); ?>
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?>
				</td>
			</tr>
			<tr class="main">
				<td class="main" colspan="2"><?php echo TEXT_NEWSDESK_IMAGE_TWO; ?></td>
			</tr>
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?>
				</td>
			</tr>
			<tr>
				<td class="main">
		<?php
		if($product_id_str=='newsdesk_id') echo newsdesk_draw_file_field('product_image_two', 'size="40"'); 
		else if($product_id_str=='faqdesk_id') echo faqdesk_draw_file_field('product_image_two', 'size="40"');?>
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<?php for ($i = 0, $n = sizeof($languages); $i < $n; $i++) { ?>
			<tr>
				<td class="main" colspan="2"><?php echo TEXT_NEWSDESK_IMAGE_SUBTITLE_TWO; ?></td>
			</tr>
			<tr>
				<td class="main">
		<?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
				</td>
				<td class="main">
		<?php 
		if($product_id_str=='newsdesk_id') $pro_image_text_two=(($newsdesk_image_text_two[$languages[$i]['id']]) ?stripslashes($newsdesk_image_text_two[$languages[$i]['id']]) : newsdesk_get_newsdesk_image_text_two($pInfo->$product_id_str, $languages[$i]['id']));
		else if($product_id_str=='faqdesk_id') $pro_image_text_two=(($faqdesk_image_text_two[$languages[$i]['id']]) ?stripslashes($faqdesk_image_text_two[$languages[$i]['id']]) : faqdesk_get_faqdesk_image_text_two($pInfo->$product_id_str, $languages[$i]['id'])); 
		echo tep_draw_input_field('product_image_text_two[' . $languages[$i]['id'] . ']', $pro_image_text_two , 'size="50"'); ?>
				</td>
			</tr>
		<?php
		} //  ........... <<<< .............. end of newsdesk_image_text_two loop
		?>
		</table>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td class="main">
		<?php
		echo tep_draw_hidden_field('products_previous_image_two', $pInfo->$product_image_two_str);
		if($pInfo->$product_image_two_str)	
			echo tep_image(DIR_WS_CATALOG_IMAGES . $pInfo->$product_image_two_str);
		?>
				</td>
			</tr>
			<tr>
				<td class="main">
		<?php
		if($pInfo->$product_image_two_str)
			echo $pInfo->$product_image_two_str;
		?>
				</td>
			</tr>
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?>
				</td>
			</tr>
			<tr>
				<td class="headerBar">
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '1'); ?>
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?>
				</td>
			</tr>
			<tr class="main">
				<td class="" colspan="2"><?php echo TEXT_NEWSDESK_IMAGE_THREE; ?></td>
			</tr>
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?>
				</td>
			</tr>
			<tr>
				<td class="main">
		<?php
		if($product_id_str=='newsdesk_id') echo newsdesk_draw_file_field('product_image_three', 'size="40"');
		else if($product_id_str=='faqdesk_id') echo faqdesk_draw_file_field('product_image_three', 'size="40"');
		 ?>
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellspacing="3" cellpadding="3">
		<?php for ($i = 0, $n = sizeof($languages); $i < $n; $i++) { ?>
			<tr>
				<td class="main" colspan="2"><?php echo TEXT_NEWSDESK_IMAGE_SUBTITLE_THREE; ?></td>
			</tr>
			<tr>
				<td class="main">
		<?php echo tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?>
				</td>
				<td class="main">
		<?php
		 if($product_id_str=='newsdesk_id') $pro_image_text_three=(($newsdesk_image_text_three[$languages[$i]['id']]) ?stripslashes($newsdesk_image_text_three[$languages[$i]['id']]) : newsdesk_get_newsdesk_image_text_three($pInfo->$product_id_str, $languages[$i]['id']));
		 else if($product_id_str=='faqdesk_id') $pro_image_text_three=(($faqdesk_image_text_three[$languages[$i]['id']]) ?stripslashes($faqdesk_image_text_three[$languages[$i]['id']]) : faqdesk_get_faqdesk_image_text_three($pInfo->$product_id_str, $languages[$i]['id']));
		 echo tep_draw_input_field('product_image_text_three[' . $languages[$i]['id'] . ']',$pro_image_text_three , 'size="50"'); ?>
				</td>
			</tr>
		<?php
		} //  ........... <<<< .............. end of newsdesk_image_text_three loop
		?>
		</table>
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td class="main">
		<?php
		echo tep_draw_hidden_field('products_previous_image_three', $pInfo->$product_image_three_str);
		if($pInfo->$product_image_three_str)
		   echo tep_image(DIR_WS_CATALOG_IMAGES . $pInfo->$product_image_three_str);
		?>
				</td>
			</tr>
			<tr>
				<td class="main">
		<?php 
		if($pInfo->$product_image_three_str)
			echo $pInfo->$product_image_three_str;
		?>
				</td>
			</tr>
			<tr>
				<td>
		<?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr><td colspan="2" align="right" id="img_save"><input type='image' src='images/template/img_savel.gif' alt='Save' title='Save' border='0'></td></tr>
  </table>
 </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/small/'.$product['categories_image'],$product['categories_name'],'100','100'):'');
					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'>
							<tr>
								<td align='left' width='50%' valign='top'><?php echo TEXT_DATE_ADDED;?></td>
								<td align='left' width='50%' valign='top'><?php echo format_date($product['date_added']);?></td>
							</tr>
							<tr>
								<td align='left' width='50%'><?php echo TEXT_LAST_MODIFIED;?></td>
								<td align='left' width='50%'><?php echo format_date($product['last_modified']);?></td>
							</tr>													
						</table>
				</tr>
			</table>
		</td>
	</tr>
  </table>
<?php }
function output_category_details($cID){
	global $languages_id,$table_product,$table_product_description,$table_product_categories,$table_product_categories_description;
	$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.catagory_status,c.categories_image from ".$table_product_categories." c,".$table_product_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);			
 ?>
		<table width='100%' border='0' cellspacing='0' cellpadding='0' class="openContent_top" id="category_details_<?php echo $product['categories_id'];?>">
				<tr heigth='40' class='openContent_top'>
				  <td width='10'></td>
				  <td width='20' valign='middle' align='center'><img style="cursor:pointer;cursor:hand;" onclick='javascript:expand_category(this,11,<?php echo tep_output_string($cID);?>);' src='images/template/<?php echo (($product['catagory_status']==0)?"icon_inactive.gif":"icon_active.gif");?>' <?php echo (($product['catagory_status']==0)?'alt=Inactive title=Inactive':'alt=Active title=Active');?>></td>
				  <td width='40' valign='top' align='center'><?php if($product['parent_id']==0) { ?><a id='sort_order_up' href='javascript:expand_category(<?php echo $product['categories_id'];?>,12,<?php echo $product['sort_order'];?>);'><img border='0' src='images/template/img_arrow_up.gif'></a>&nbsp;<a id='sort_order_down' href='javascript:expand_category(<?php echo $product['categories_id'];?>,13,<?php echo $product['sort_order'];?>);'><img border='0' src='images/template/img_arrow_down.gif'></a><?php } else echo "&nbsp;";?></td>
				  <td align='left' valign='middle' id="categories_name" width='350' class='main'><?php echo $product['categories_name'];?></td>
				  <td width='470' 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);expand_category(<?php echo $product['categories_id'];?>,3);'><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);expand_category(<?php echo $product['categories_id'];?>,4);'><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);expand_category(<?php echo $product['categories_id'];?>,5);'><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='240' align='right' id='img_save_<?php echo $product['categories_id'];?>' style='display:none'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' border='0'>&nbsp;&nbsp;<a href='javascript:expand_category(<?php echo $product['categories_id'];?>,1)'><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">
				  	<input type="hidden" name="cPath" id="cPath" value="<?php echo $product['categories_id'];?>">
				  		<span id="span_details_<?php echo $product['categories_id'];?>">
							<?php echo get_category_details($product);?>							
						</span>
					</td>
				   </tr>				   
			</table>
<?php } 
	}
function tep_get_product_cat_ajax_list($parent_id,$level=0,$value_assign_type='default',&$results,&$open_ids){
		global $languages_id,$table_product,$table_product_description,$table_product_categories,$table_product_categories_description;
		//$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";
		 $categories_sql = "select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified, c.catagory_status from " . $table_product_categories . " c, " . $table_product_categories_description . " cd where c.parent_id = '" . $parent_id . "' and c.categories_id = cd.categories_id and cd.language_id = '" . $languages_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:' . (tep_output_string($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 output_sub_result($parent_id,$page,$show_order,$level,$open_ids,$start_up=0) {
		global $languages_id,$selected_name,$login_id,$login_groups_type,$HTTP_GET_VARS,$page,$command,$table_product,$table_product_description,$table_product_categories,$table_product_categories_description,$product_id_str,$product_name_str,$product_status_str;					
		$open_ids="," . $open_ids;
		echo output_category_details($parent_id);						
		echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr style="height:10"><td></td></tr></table>';
		// first fetch the products in this category
		if ($parent_id>0 && $command!='search_details') output_products_result($parent_id,$page,$show_order);				
		$search=" c.parent_id = '" . $parent_id . "'";
		if($command=='search_details' && $HTTP_GET_VARS['search']!='') $search=" cd.categories_name like '%" . tep_db_input(tep_db_prepare_input($HTTP_GET_VARS['search'])) . "%'";		
	 	$categories_sql = "select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified, c.catagory_status from " . $table_product_categories . " c, " . $table_product_categories_description . " cd where  " . $search . " and c.categories_id = cd.categories_id and cd.language_id = '" . $languages_id . "' order by c.sort_order, cd.categories_name";	 		 		 		
				
		$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'].'">';		
		// iterate with nested category
		$icnt=0; $toggle_flag=0;
		if(tep_db_num_rows($categories_query)>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 && $command!='search_details') output_sub_result($category_result['categories_id'],$page,$show_order,$level+1,$open_ids);			
			tep_category_title_bottom();			
		}
		} else { echo "<tr><td align=center class=main>No records Found</td></tr>"; }
		echo '</table>';
	}
	function output_products_result($categories_id,$page,$show_order){
		global $languages_id,$command,$HTTP_GET_VARS,$table_product,$table_product_description,$table_product_categories,$table_product_categories_description,$table_product_to_categories,$product_id_str,$product_name_str,$product_status_str;			
			$prd_where="";			
			$search=" and p2c.categories_id = '" . tep_db_input($categories_id) . "'";
			if($command=='search_details' && $HTTP_GET_VARS['search']!='') $search=" and pd.$product_name_str like '%" . tep_db_input(tep_db_prepare_input($HTTP_GET_VARS['search'])) . "%'";
			//$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 pd.products_name " . $prd_order;			
			$products_sql = "select p.$product_id_str, pd.$product_name_str as product_name 
							 ,p.$product_status_str as product_status  from " . $table_product . " p, " 
							. $table_product_description . " pd, " . $table_product_to_categories . " p2c where p.$product_id_str = pd.$product_id_str and pd.language_id = '" 
							. $languages_id . "' and p.$product_id_str = p2c.$product_id_str ".$search." order by 
							pd.$product_name_str";										
			//$query_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $products_sql, $query_split_numrows);		
			$products_query=tep_db_query($products_sql);			
			/*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_'.tep_output_string($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" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="javascript:if(document.getElementById(\'head_'.$products["$product_id_str"].'_'.tep_output_string($categories_id).'\').style.display==\'none\') expand_category('.$products["$product_id_str"].',1,'.tep_output_string($categories_id).');"><td class="main"><div id="data_'.$products["$product_id_str"].'_'.tep_output_string($categories_id).'">';
					echo  ($products['product_status']==1)?tep_image(DIR_WS_IMAGES  . 'template/icon_active.gif', '','','','id="img_'.$products["$product_id_str"].'"') . tep_draw_separator('pixel_trans.gif', '5', '10'):tep_image(DIR_WS_IMAGES  . 'template/icon_inactive.gif', '','','','id="img_'.$products["$product_id_str"].'"') . tep_draw_separator('pixel_trans.gif', '5', '10');
					echo  $products['product_name'] . '</div><div class="openContent" id="head_'.$products["$product_id_str"].'_'.tep_output_string($categories_id).'" style="display:none"></div></td></tr>';
					$i++;
				} 	
			}
			echo '</table>';
			//if ($command!='fetch_sub_products')	echo '</div>';
	}
?>
