<?php

class CategoriesManagerView extends Slot {

	var $SectionID = 22;
	var $ModuleID = 3;
	var $categories = array();//array('0' => '*** Root Category');
	var $colors = array(
		'red' => '#DC143C',
		'yellow' => '#FF8C00',
		'green' => '#006400',
		'black' => '#000000',
		'blue' => '#4169E1',
	);
	var $terminated = false;

	function __construct($pquery) {
		parent::__construct($pquery);
	}

	function init() {
		global $TOOLS_PATH;
		$undo = 0;
		$categories = $this->Action->Server->getGuide('category');
		$this->prod_colors = $this->Action->Server->getGuide('color');
		switch(@$this->LocalTarget[0]) {
			case 'CRList': {
				$this->customerreviews = $this->Db->SelectGuide(array('customerreview g'),array('g.id','g.name'),array('1' => '1'));
				foreach($this->customerreviews as $key => $value) {
					$this->TempFillEls['items'][] = array(
						'number' => $key,
						'name' => $value,
						'name2' => htmlspecialchars(strip_tags($value), ENT_QUOTES),
						'id' => @$this->LocalTarget[1],
						'form_name' => 'category',
					);
				}
				$this->Scheme = 'crlist';
			break;
			}
			case 'ViewAll': {
				if($this->server_type=='dev') {
					$request = $this->LocalTarget;
					$request['pageRecordsQty'] = 20;
					$request['curPageNumber'] = isset($this->LocalTarget[2])? $this->LocalTarget[2]+1 : 1;
					$this->Action->processEvent($this->SectionID,'select_categorycomments',$request,$this);
					if(!empty($this->PreparedTemplates['_error'])) $this->Scheme = 'message';
					else {
						$this->getPageControl($this->Home.'/Registar/CategoriesManager/ViewAll/'.$this->LocalTarget[1]);
						if(empty($this->PreparedTemplates['curPage'])) $this->Scheme = 'empty';
					}
					$this->Scheme = 'viewall';
				}
				else $this->Scheme = 'default';
			break;
			}
			case 'Update': case 'Duplicate': case 'Delete': {
				if($this->server_type=='dev') {
					if(!empty($this->LocalTarget[3]) && validate(intval($this->LocalTarget[3])) && $this->LocalTarget[2]=='Undo')
						$undo = 1;
					else $undo = 0;
					if(!empty($this->LocalTarget[1]) && in_array($this->LocalTarget[1],array_keys($categories))) {
						$array = array('id' => $this->LocalTarget[1]);
						if($undo == 1) $array['undo'] = $this->LocalTarget[3];
						$this->Action->processEvent($this->SectionID,'getlock',array('category' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
						if(!empty($this->PreparedTemplates['access_flag']) && $this->PreparedTemplates['access_flag'] != $this->Action->Security->currentuser->id) {
							header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
							$this->terminated = true;
							return;
						}
						else {
							if(empty($this->PreparedTemplates['access_flag'])) {
								$this->Action->processEvent($this->SectionID,'lock',array('category' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
							}
							$this->Action->processEvent($this->SectionID,'tree',array('full' => 0,'admin' => 1,'parent' => 0),$this);
							$this->setCategories($this->PreparedTemplates,0);
							$this->customerreviews = $this->Db->SelectGuide(array('customerreview g'),array('g.id','g.name'),array('1' => '1'));
							$this->Action->processEvent($this->SectionID,'open',$array,$this);
							if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
							else $this->Scheme = 'action';
						}
					}
					else header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
				}
				else header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
			break;
			}
			case 'Add': {
				$this->Action->processEvent($this->SectionID,'tree',array('full' => 0,'admin' => 1,'parent' => 0),$this);
				$this->setCategories($this->PreparedTemplates,0);
				$this->Action->processEvent($this->SectionID,'open',array('id' => 0),$this);
				$this->Scheme = 'action';
			break;
			}
			case 'Unlock': {
				if($this->server_type=='dev') {
					$this->Action->processEvent($this->SectionID,'getlock',array('category' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
					if(!empty($this->PreparedTemplates)){
						$this->Action->processEvent($this->SectionID,'unlock',array('category' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
					}	
					header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
				}
				else $this->Scheme = 'default';
			break;
			}
			case 'Sort': {
				$this->Action->processEvent($this->SectionID,'tree',array('full' => 0,'admin' => 1,'parent' => 0),$this);
				$this->Scheme = 'sort';
			break;
			}
			default: {
				if(!empty($this->LocalTarget[0])){
					header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
					$this->terminated = true;
					return;
				}
				$this->Action->processEvent($this->SectionID,'tree',array('full' => 1,'admin' => 1,'parent' => 0),$this);
				$this->Scheme = 'default';
			break;
			}
		}
		if(!empty($this->Request)) {
			//printArray($this->Request);
			switch($this->getRequestField('step')) {
				case 'add': case 'edit': {
						$this->Request['form']['id_user']=$this->Action->Security->currentuser->id;
						if($undo ==1) $this->Request['form']['_undo_'] = $this->LocalTarget[3];
						$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
						if(isset($this->PreparedTemplates['ownerid']) && $this->PreparedTemplates['ownerid'] != $this->Request['form']['ownerid'] && empty($this->Request['form']['duplicate_id']))
							$this->Request['form']['autochange'] = 'true';
						if($this->PreparedTemplates['sys_name']!=$this->Request['form']['sys_name'] && empty($this->Request['form']['duplicate_id']))
							$this->Request['form']['autochange'] = 'true';
						if($this->PreparedTemplates['path2']!=$this->Request['form']['path2'] && empty($this->Request['form']['duplicate_id']))
							$this->Request['form']['autochange'] = 'true';
						$this->validateForm()
						and $this->Action->processEvent($this->SectionID,'save',$this->getRequestField('form'),$this)
						and header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
				break;
				}
				case 'sort': {
						$this->sortingPages()
						and $this->Action->processEvent($this->SectionID,'sort',$this->getRequestField('form'),$this)
						and header('Location: '.$this->Home.'/Registar/CategoriesManager/Sort'.$this->SID);
				break;
				}
				case 'sortproducts': {
							$this->Action->processEvent($this->SectionID,'sortproducts',$this->getRequestField('form'),$this)
							and header('Location: '.$this->Home.'/Registar/CategoriesManager/'.$this->SID);
				break;
				}
				case 'delete': {
						include ($TOOLS_PATH.'\checker.php');
						$this->check = check_to_delete($this->getRequestFormField('id'),'Category');
						if(isset($this->check['pages']) && empty($this->check['pages']) &&
						isset($this->check['menues']) && empty($this->check['menues']) &&
						isset($this->check['layouts']) && empty($this->check['layouts']) &&
						isset($this->check['templates']) && empty($this->check['templates'])) {
							$this->validateForm()
							and $this->Action->processEvent($this->SectionID,'delete',$this->getRequestField('form'),$this)
							and $this->clearUndo($this->getRequestFormField('id'))
							and header('Location: '.$this->Home.'/Registar/CategoriesManager'.$this->SID);
						}
								else $this->Scheme = 'nodelete';
				break;
				}
			}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function clearUndo($id) {
		global $ROOT_PATH;
		if(file_exists($ROOT_PATH.'/undo/category/'.$id)) {
			chdir($ROOT_PATH.'/undo/category/'.$id);
			$dir = dir('.');
			$dir->rewind();
			while($file = $dir->read()) if(is_file($file)) unlink($ROOT_PATH.'/undo/category/'.$id.'/'.$file);
			rmdir($ROOT_PATH.'/undo/category/'.$id);
			$dir->close();
		}
		return true;
	}

	function validateForm() {
		$categories = $this->Action->Server->getGuide('category');
		//printArray($categories);
		if($this->LocalTarget[0] != 'Delete') {
			if(!validate($this->Request['form']['name'])) $this->errors['name'] = 'Category Name is empty';
			//if(!validate($this->Request['form']['product_class'])) $this->errors['product_class'] = 'Product Class is empty';
			if(!validate($this->Request['form']['description'])) $this->Request['form']['description'] = ' ';
			if(!validate($this->getRequestFormField('sys_name'))) 'Category\'s URL is empty';
				else if(validate($this->getRequestFormField('sys_name')) && !is_URLText($this->getRequestFormField('sys_name'),1,255)) $this->errors['sys_name'] = 'Category\'s URL is not valid';
					else if(validate($this->getRequestFormField('sys_name')) && in_array($this->getRequestFormField('sys_name'),array('.','..'))) $this->errors['sys_name'] = 'Category\'s URL is not valid';
						else $this->Request['form']['sys_name'] = stripslashes($this->getRequestFormField('sys_name'));
			if (isset($this->Request['form']['links']) )
				foreach($this->Request['form']['links'] as $key => $value) {
					if(empty($value['link']) && empty($value['url'])) unset($this->Request['form']['links'][$key]);
					else if(empty($value['link'])) $this->Request['form']['links'][$key]['link'] = $this->Request['form']['links'][$key]['url'];
					else if(empty($value['url'])) $this->Request['form']['links'][$key]['url'] = 'http://www.atncorp.com/';
					else if(empty($value['description'])) $this->Request['form']['links'][$key]['description'] = 'ATN';
					else if(empty($value['linktype'])) $this->Request['form']['links'][$key]['linktype'] = 1;
				}
			$this->customerreviews = $this->Db->SelectGuide(array('customerreview g'),array('g.id','g.name'),array('1' => '1'));
			empty($this->Request['form']['customerreviewitems']) and $this->Request['form']['customerreviewitems'] = array();
			$cr_ids = array();
			$i = 1;
			foreach($this->Request['form']['customerreviewitems'] as $key => $value) {
				if(!in_array($value['object'],array_keys($this->customerreviews)) || in_array($value['object'],$cr_ids)) unset($this->Request['form']['customerreviewitems'][$key]);
				else {
					$this->Request['form']['customerreviewitems'][$key]['sort_order'] = $i*10;
					$cr_ids[] = $value['object'];
					$i++;
				}
			}
			empty($this->Request['form']['reviews']) and $this->Request['form']['reviews'] = array();
			foreach($this->Request['form']['reviews'] as $key => $value) {
				if(!empty($value['image_title']) && !is_AdvCleanText($value['image_title'],1,512)) $this->errors['review'][$key]['image_title'] = 'Image Title is not valid';
				if(!empty($value['image_alt']) && !is_AdvCleanText($value['image_alt'],1,512)) $this->errors['review'][$key]['image_alt'] = 'Image Alt is not valid';
				if(!empty($value['description']) && !is_FullText($value['description'],1,512)) $this->errors['review'][$key]['description'] = 'Description is not valid';
				$url = checkURL($value['url']);
				if(empty($url)) $this->errors['review'][$key]['url'] = 'Review URL is not valid';
				else $this->Request['form']['reviews'][$key]['url'] = $url;
				$url = checkURL($value['image_url']);
				if(empty($url)) $this->errors['review'][$key]['image_url'] = 'Image URL is not valid';
				else $this->Request['form']['reviews'][$key]['image_url'] = $url;
				$this->Request['form']['reviews'][$key]['sort_order'] = intval($value['sort_order']);
			}
			if($this->LocalTarget[0] == 'Update')
				if(!is_FullText($this->getRequestFormField('comment'),5,4096)) $this->errors['comment'] = 'Comment is empty or is not valid';
			if($this->getRequestFormField('id')!=1 && $this->getRequestFormField('ownerid',1)==$this->getRequestFormField('id')) $this->errors['parent'] = 'The category cannot be own parent';
			else if(!empty($categories[$this->getRequestFormField('id')]) && $this->validateMenuArray($categories,$this->getRequestFormField('ownerid',1)))
				$this->errors['parent'] = 'The category cannot have the parent the category enclosed in it';
		}
		if($this->getRequestFormField('id')==1 && $this->LocalTarget[0]=='Delete') $this->errors['model'] = 'The given category cannot be deleted';
		if(count($this->errors)>0) return FALSE;
		return TRUE;
	}

	function fillElements() {
		switch($this->Scheme) {
			case 'action': $this->fillAction(); break;
			case 'viewall': $this->fillViewAll(); break;
			case 'sort': $this->fillSort(); break;
			case 'nodelete': $this->fillNoDelete(); break;
			default: $this->fillDefault(); break;
		}
	}

	function fillNoDelete() {
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
		$this->TempFillEls['id'] = $this->PreparedTemplates['id'];
		$this->TempFillEls['sec_name'] = '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']';
		empty($this->check['pages']) and $this->check['pages'] = array();
		empty($this->check['menues']) and $this->check['menues'] = array();
		empty($this->check['templates']) and $this->check['templates'] = array();
		empty($this->check['layouts']) and $this->check['layouts'] = array();
		foreach($this->check['pages'] as $key => $value) {
			$this->TempFillEls['Pages'][0]['pitems'][] = array(
				'item' => makeLink($this->Home.'/Registar/'.(empty($value['update_path']) ? (empty($value['js']) ? 'ContentManager' : 'MetaManager/Page').'/Update/'.$value['id'] : $value['update_path']), $value['name']),
				'id' => $value['id'],
				'path' => empty($value['path'])? '&nbsp;' : makeLink($this->Home.$value['path'], 'View', array(), '', 'class=adminlink'),
			);
		}
		foreach($this->check['menues'] as $key => $value) {
			$this->TempFillEls['Menues'][0]['mitems'][] = array(
				'item' => makeLink($this->Home.'/Registar/MenuManager/'.$value['type'].'Menu/Update/'.$value['id'], $value['name']),
			);
		}
		foreach($this->check['templates'] as $key => $value) {
			$this->TempFillEls['Templates'][0]['titems'][] = array(
				'item' => makeLink($this->Home.'/Registar/TemplateManager/Update/'.$value['id'],$value['name']),
			);
		}
		foreach($this->check['layouts'] as $key => $value) {
			$this->TempFillEls['Layouts'][0]['litems'][] = array(
				'item' => makeLink($this->Home.'/Registar/LayoutManager/Update/'.$value['id'],$value['name']),
			);
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID,'open');
	}

	function fillSort() {
		if($this->terminated) return;
		$this->TempFillEls['addBut'] = $this->server_type == 'dev' ? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type == 'pro' ? 'hidden' : 'visible';
		$this->setSortArray($this->PreparedTemplates,0.01);
	}

	function fillViewAll() {
		global $REGISTRY, $ROOT_PATH;
		foreach($this->PreparedTemplates['curPage'] as $ind => $val) {
			$tok = explode(".",$val['date']);
			$this->TempFillEls['comments'][] = array(
				'number' => ($this->PreparedTemplates['pageRecordsQty']*($this->PreparedTemplates['curPageNumber']-1))+($ind+1),
				'date' => $tok[0],
				'author' => $val['member'],
				'changed' => $val['value'],
				'version' => $val['version'],
				'button' =>  file_exists($ROOT_PATH.'/undo/category/'.$this->PreparedTemplates[1].'/'.$val['version'].'.inc')? array(array('id' => $this->PreparedTemplates[1],'version' => $val['version'])) : array(),
			);
		}
	}

	function fillDefault() {
		if($this->terminated) return;
		$this->TempFillEls['addBut'] = $this->server_type == 'dev' ? 'visible' : 'hidden';
		$this->TempFillEls['pubBut'] = $this->server_type == 'pro' ? 'hidden' : 'visible';
		$this->setTargetArray($this->PreparedTemplates,0.01);
	}

	function fillAction() {
		global $ROOT_PATH;
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'open');
		//printArray($this->PreparedTemplates);
		$this->cat_type = array(0 => 'Products',1 => 'Accessories and Kits',2 => 'Sale',3 => 'Accessories Grid', 4 => 'Accessories and Kits Archive', 5 => 'Download Information');
		$this->TempFillEls['id'] = $this->LocalTarget[0]!='Add' && $this->LocalTarget[0]!='Duplicate'? $this->PreparedTemplates['id'] : '';
		switch($this->LocalTarget[0]) {
			case 'Duplicate': {
				$this->TempFillEls['subaction'] = 'Duplicate';
				$this->TempFillEls['subprocess'] = 'duplicating';
				$this->TempFillEls['edit_id'] = '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']';
				$this->TempFillEls['duplicate_id'] = $this->PreparedTemplates['id'];
				$this->TempFillEls['step'] = 'add';
				$this->TempFillEls['is_active'] = inputHidden('form[is_active]',0).inputCheckbox('form[is_active]',(!empty($this->Request)? $this->getRequestFormField('is_active') : $this->PreparedTemplates['is_active']),'class=check');
				$this->TempFillEls['is_chart'] = inputHidden('form[is_chart]',0).inputCheckbox('form[is_chart]',(!empty($this->Request)? $this->getRequestFormField('is_chart') : $this->PreparedTemplates['is_chart']),'class=check');
				$this->TempFillEls['name'] = inputText('form[name]',(!empty($this->Request)? $this->Request['form']['name'] : $this->PreparedTemplates['name']),55,'style="width:250px;"');
				$this->TempFillEls['sys_name'] = inputText('form[sys_name]',(!empty($this->Request)? $this->getRequestFormField('sys_name') : $this->PreparedTemplates['sys_name']),55,'style="width:250px;"');
				$this->TempFillEls['root_check'] = inputCheckbox('root_check',(!empty($this->Request)? $this->getRequestField('root_check') : ($this->PreparedTemplates['path2']? 1 : 0)),' onClick="javascript:correctRootPath();"');
				$this->TempFillEls['path2'] = inputText('form[path2]',(!empty($this->Request)? $this->getRequestFormField('path2') : $this->PreparedTemplates['path2']),75,'style="width:460px;display:'.(validate($this->getRequestField('root_check')) || !empty($this->PreparedTemplates['path2'])? 'inline' : 'none').'"');
				$this->TempFillEls['desc'] = inputTextArea('form[description]',(!empty($this->Request)? $this->Request['form']['description'] : @$this->PreparedTemplates['description']),100,8,'style="width:740px;"');
				$this->TempFillEls['parent'] = inputSelect('form[ownerid]',$this->categories,(!empty($this->Request)? $this->getRequestFormField('ownerid') : $this->PreparedTemplates['ownerid']),'style="width:250px;"');
				$this->TempFillEls['cat_type'] = inputSelect('form[cat_type]',$this->cat_type,(!empty($this->Request)? $this->getRequestFormField('cat_type') : $this->PreparedTemplates['cat_type']),'style="width:250px;"');
				$this->TempFillEls['color'] = inputSelect('form[color][id]',$this->prod_colors,(!empty($this->Request)? $this->getRequestFormField('color',1) : $this->PreparedTemplates['color']['id']),'style="width:250px;"');
				$this->TempFillEls['old_url'] = empty($this->PreparedTemplates['old_url']) ? @$this->PreparedTemplates['sys_name'] : $this->PreparedTemplates['old_url'];
				$this->TempFillEls['link_button'] = array(array());
				$this->TempFillEls['cr_buttons'] = array(array());
				$this->TempFillEls['review_buttons'] = array(array());
				$number_link = !empty($this->Request['form']['links']) && count($this->Request['form']['links']) > 3? count($this->Request['form']['links']) : (count($this->PreparedTemplates['links']) > 3? count($this->PreparedTemplates['links']) : 3);
				for($i = 1; $i <= $number_link; $i++) {
					$this->TempFillEls['links'][$i] = array(
						'name' => inputTextArea('form[links]['.$i.'][description]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['description'] : @$this->PreparedTemplates['links'][$i-1]['description']),55,2,'style="width:350px;"'),
						'js' => inputCheckbox('form[links]['.$i.'][js]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['js'] : @$this->PreparedTemplates['links'][$i-1]['js']),'class=check',1),
						'type' => inputSelect('form[links]['.$i.'][linktype]',array('1' => 'Featured Links','2' => 'Featured Retailers'),(!empty($this->Request)? @$this->Request['form']['links'][$i]['linktype'] : @$this->PreparedTemplates['links'][$i-1]['linktype'])),
						'link' => inputTextArea('form[links]['.$i.'][link]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['link'] : @$this->PreparedTemplates['links'][$i-1]['link']),55,2,'style="width:300px;"'),
						'url' => inputTextArea('form[links]['.$i.'][url]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['url'] : @$this->PreparedTemplates['links'][$i-1]['url']),55,2,'style="width:350px;"'),
						'id' => inputHidden('form[links]['.$i.'][moduletype]',1),
						'key' => $i,
						'delete_link' => array(array('key' => $i)),
					);
				}
				$number_review = !empty($this->Request['form']['reviews']) && count($this->Request['form']['reviews']) > 0? count($this->Request['form']['reviews']) : (!empty($this->PreparedTemplates['reviews']) && count($this->PreparedTemplates['reviews']) > 0? count($this->PreparedTemplates['reviews']) : 0);
				for($i = 1; $i <= $number_review; $i++) {
					$this->TempFillEls['reviews'][$i] = array(
						'num' => $i,
						'url' => inputTextArea('form[reviews]['.$i.'][url]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['url'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['url'] : '')),32,2,'style="width:320px;"'),
						'image_url' => inputTextArea('form[reviews]['.$i.'][image_url]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['image_url'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_url'] : '')),32,2,'style="width:320px;"'),
						'image_alt' => inputTextArea('form[reviews]['.$i.'][image_alt]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['image_alt'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_alt'] : '')),32,2,'style="width:320px;"'),
						'image_title' => inputTextArea('form[reviews]['.$i.'][image_title]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['image_title'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_title'] : '')),32,2,'style="width:320px;"'),
						'description' => inputTextArea('form[reviews]['.$i.'][description]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['description'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['description'] : '')),32,2,'style="width:320px;"'),
						'sort_order' => inputText('form[reviews]['.$i.'][sort_order]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['sort_order'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['sort_order'] : $i*10)),2,'style="width:30px;"'),
						'delete_review' => array(array('num' => $i)),
						'moduletype' => inputHidden('form[reviews]['.$i.'][moduletype]',2),
					);
					if(!empty($this->errors['review'][$i]))
						foreach($this->errors['review'][$i] as $key => $value) $this->TempFillEls['reviews'][$i][$key.'_error'] = $this->printError($value);
				}
				$number_cr = !empty($this->Request['form']['customerreviewitems']) && count($this->Request['form']['customerreviewitems']) > 0? count($this->Request['form']['customerreviewitems']) : (!empty($this->PreparedTemplates['customerreviewitems']) && count($this->PreparedTemplates['customerreviewitems']) > 0? count($this->PreparedTemplates['customerreviewitems']) : 0);
				for($i = 1; $i <= $number_cr; $i++) {
					$this->TempFillEls['cr_items'][$i] = array(
						'num' => $i,
						'sort_order' => inputText('form[customerreviewitems]['.$i.'][sort_order]',(!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['sort_order'] : (isset($this->PreparedTemplates['customerreviewitems'][$i-1])? $this->PreparedTemplates['customerreviewitems'][$i-1]['sort_order'] : $i*10)),2,'style="width:30px;"'),
						'delete_cr' => array(array('num' => $i)),
						'object' => inputHidden('form[customerreviewitems]['.$i.'][object]', (!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['object'] : $this->PreparedTemplates['customerreviewitems'][$i-1]['object'])),
						'name' => inputText('form[customerreviewitems]['.$i.'][name]', htmlspecialchars(strip_tags($this->customerreviews[(!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['object'] : $this->PreparedTemplates['customerreviewitems'][$i-1]['object'])]), ENT_QUOTES),2,'disabled="true" style="width:480px;"'),
						'type' => inputHidden('form[customerreviewitems]['.$i.'][moduletype]', 2),
						'change_cr' => array(array('num' => $i)),
					);
					if(!empty($this->errors['cr'][$i]))
						foreach($this->errors['cr'][$i] as $key => $value) $this->TempFillEls['cr_items'][$i][$key.'_error'] = $this->printError($value);
				}
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'save');
			break;
			}
			case 'Update': {
				if(!empty($this->LocalTarget[3]) && validate(intval($this->LocalTarget[3])) && $this->LocalTarget[2]=='Undo')
					$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[3].')';
				$this->TempFillEls['subaction'] = 'Update';
				$this->TempFillEls['subprocess'] = 'editing';
				$this->TempFillEls['edit_id'] = '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']';
				$this->TempFillEls['step'] = 'edit';
				$this->TempFillEls['is_active'] = inputHidden('form[is_active]',0).inputCheckbox('form[is_active]',(!empty($this->Request)? $this->getRequestFormField('is_active') : $this->PreparedTemplates['is_active']),'class=check');
				$this->TempFillEls['is_chart'] = inputHidden('form[is_chart]',0).inputCheckbox('form[is_chart]',(!empty($this->Request)? $this->getRequestFormField('is_chart') : $this->PreparedTemplates['is_chart']),'class=check');
				$this->TempFillEls['name'] = inputText('form[name]',(!empty($this->Request)? $this->Request['form']['name'] : $this->PreparedTemplates['name']),55,'style="width:250px;"');
				$this->TempFillEls['sys_name'] = inputText('form[sys_name]',(!empty($this->Request)? $this->getRequestFormField('sys_name') : $this->PreparedTemplates['sys_name']),55,'style="width:250px;"');
				$this->TempFillEls['root_check'] = inputCheckbox('root_check',(!empty($this->Request)? $this->getRequestField('root_check') : ($this->PreparedTemplates['path2']? 1 : 0)),' onClick="javascript:correctRootPath();"');
				$this->TempFillEls['path2'] = inputText('form[path2]',(!empty($this->Request)? $this->getRequestFormField('path2') : $this->PreparedTemplates['path2']),75,'style="width:460px;display:'.(validate($this->getRequestField('root_check')) || !empty($this->PreparedTemplates['path2'])? 'inline' : 'none').'"');
				$this->TempFillEls['desc'] = inputTextArea('form[description]',(!empty($this->Request)? $this->Request['form']['description'] : @$this->PreparedTemplates['description']),100,8,'style="width:740px;"');
				$this->TempFillEls['parent'] = inputSelect('form[ownerid]',$this->categories,(!empty($this->Request)? $this->getRequestFormField('ownerid') : $this->PreparedTemplates['ownerid']),'style="width:250px;"');
				$this->TempFillEls['cat_type'] = inputSelect('form[cat_type]',$this->cat_type,(!empty($this->Request)? $this->getRequestFormField('cat_type') : $this->PreparedTemplates['cat_type']),'style="width:250px;"');
				$this->TempFillEls['color'] = inputSelect('form[color][id]',$this->prod_colors,(!empty($this->Request)? $this->getRequestFormField('color',1) : $this->PreparedTemplates['color']['id']),'style="width:250px;"');
				$this->TempFillEls['old_url'] = empty($this->PreparedTemplates['old_url']) ? @$this->PreparedTemplates['sys_name'] : $this->PreparedTemplates['old_url'];
				$this->TempFillEls['link_button'] = array(array());
				$this->TempFillEls['cr_buttons'] = array(array());
				$this->TempFillEls['review_buttons'] = array(array());
				$number_link = !empty($this->Request['form']['links']) && count($this->Request['form']['links']) > 3? count($this->Request['form']['links']) : (count($this->PreparedTemplates['links']) > 3? count($this->PreparedTemplates['links']) : 3);
				for($i = 1; $i <= $number_link; $i++) {
					$this->TempFillEls['links'][$i] = array(
						'name' => inputTextArea('form[links]['.$i.'][description]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['description'] : @$this->PreparedTemplates['links'][$i-1]['description']),55,2,'style="width:350px;"'),
						'js' => inputCheckbox('form[links]['.$i.'][js]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['js'] : @$this->PreparedTemplates['links'][$i-1]['js']),'class=check',1),
						'type' => inputSelect('form[links]['.$i.'][linktype]',array('1' => 'Featured Links','2' => 'Featured Retailers'),(!empty($this->Request)? @$this->Request['form']['links'][$i]['linktype'] : @$this->PreparedTemplates['links'][$i-1]['linktype'])),
						'link' => inputTextArea('form[links]['.$i.'][link]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['link'] : @$this->PreparedTemplates['links'][$i-1]['link']),55,2,'style="width:300px;"'),
						'url' => inputTextArea('form[links]['.$i.'][url]',(!empty($this->Request)? @$this->Request['form']['links'][$i]['url'] : @$this->PreparedTemplates['links'][$i-1]['url']),55,2,'style="width:350px;"'),
						'id' => inputHidden('form[links]['.$i.'][moduletype]',1),
						'key' => $i,
						'delete_link' => array(array('key' => $i)),
					);
				}
				$number_review = !empty($this->Request['form']['reviews']) && count($this->Request['form']['reviews']) > 0? count($this->Request['form']['reviews']) : (!empty($this->PreparedTemplates['reviews']) && count($this->PreparedTemplates['reviews']) > 0? count($this->PreparedTemplates['reviews']) : 0);
				for($i = 1; $i <= $number_review; $i++) {
					$this->TempFillEls['reviews'][$i] = array(
						'num' => $i,
						'url' => inputTextArea('form[reviews]['.$i.'][url]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['url'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['url'] : '')),32,2,'style="width:320px;"'),
						'image_url' => inputTextArea('form[reviews]['.$i.'][image_url]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['image_url'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_url'] : '')),32,2,'style="width:320px;"'),
						'image_alt' => inputTextArea('form[reviews]['.$i.'][image_alt]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['image_alt'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_alt'] : '')),32,2,'style="width:320px;"'),
						'image_title' => inputTextArea('form[reviews]['.$i.'][image_title]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['image_title'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_title'] : '')),32,2,'style="width:320px;"'),
						'description' => inputTextArea('form[reviews]['.$i.'][description]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['description'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['description'] : '')),32,2,'style="width:320px;"'),
						'sort_order' => inputText('form[reviews]['.$i.'][sort_order]',(!empty($this->Request)? @$this->Request['form']['reviews'][$i]['sort_order'] : (isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['sort_order'] : $i*10)),2,'style="width:30px;"'),
						'delete_review' => array(array('num' => $i)),
						'moduletype' => inputHidden('form[reviews]['.$i.'][moduletype]',2),
					);
					if(!empty($this->errors['review'][$i]))
						foreach($this->errors['review'][$i] as $key => $value) $this->TempFillEls['reviews'][$i][$key.'_error'] = $this->printError($value);
				}
				$number_cr = !empty($this->Request['form']['customerreviewitems']) && count($this->Request['form']['customerreviewitems']) > 0? count($this->Request['form']['customerreviewitems']) : (!empty($this->PreparedTemplates['customerreviewitems']) && count($this->PreparedTemplates['customerreviewitems']) > 0? count($this->PreparedTemplates['customerreviewitems']) : 0);
				for($i = 1; $i <= $number_cr; $i++) {
					$this->TempFillEls['cr_items'][$i] = array(
						'num' => $i,
						'sort_order' => inputText('form[customerreviewitems]['.$i.'][sort_order]',(!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['sort_order'] : (isset($this->PreparedTemplates['customerreviewitems'][$i-1])? $this->PreparedTemplates['customerreviewitems'][$i-1]['sort_order'] : $i*10)),2,'style="width:30px;"'),
						'delete_cr' => array(array('num' => $i)),
						'object' => inputHidden('form[customerreviewitems]['.$i.'][object]', (!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['object'] : $this->PreparedTemplates['customerreviewitems'][$i-1]['object'])),
						'name' => inputText('form[customerreviewitems]['.$i.'][name]', htmlspecialchars(strip_tags($this->customerreviews[(!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['object'] : $this->PreparedTemplates['customerreviewitems'][$i-1]['object'])]), ENT_QUOTES),2,'disabled="true" style="width:480px;"'),
						'type' => inputHidden('form[customerreviewitems]['.$i.'][moduletype]', 2),
						'change_cr' => array(array('num' => $i)),
					);
					if(!empty($this->errors['cr'][$i]))
						foreach($this->errors['cr'][$i] as $key => $value) $this->TempFillEls['cr_items'][$i][$key.'_error'] = $this->printError($value);
				}
				$this->TempFillEls['textareaValidJS']='if(parseInt(document.getElementById("comment").value.length+"-")<5){
							//document.getElementById("comment").focus();
							alert("Comment is empty or is not valid");
							return false;
							}';
				$this->TempFillEls['ViewAllButton']='<input type="button" name="View_All" class=adminsubmit value="View All" onclick="open(\'/Registar/CategoriesManager/ViewAll/'.$this->PreparedTemplates['id'].'\',\'displayWindow\',\'width=950,height=600,scrollbars=yes\');">';
				$this->TempFillEls['textareaComment']='<textarea name=form[comment] id=comment cols=80 rows=5>'.(!empty($this->Request['form']['comment'])? $this->Request['form']['comment'] : '').'</textarea>';
				$selected = $this->Db->SelectAny("select cc.*,m.username as member from categorycomment cc, member m where m.id=cc.member and cc.ownerid = ".$this->PreparedTemplates['id']." order by cc.version DESC LIMIT 10 OFFSET 0");
				if(!empty($selected)) {
					foreach($selected as $ind => $val) {
						$tok = explode(".",$val['date']);
						$this->TempFillEls['comment'][] = array(
							'number' => ($ind+1),
							'date' => $tok[0],
							'author' => $val['member'],
							'changed' => $val['value'],
							'version' => $val['version'],
							'button' => $ind!=0 && file_exists($ROOT_PATH.'/undo/category/'.$this->PreparedTemplates['id'].'/'.$val['version'].'.inc')? array(array('id' => $this->PreparedTemplates['id'],'version' => $val['version'])) : array(),
						);
					}
				}
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'save');
			break;
			}
			case 'Delete': {
				if(!empty($this->LocalTarget[3]) && validate(intval($this->LocalTarget[3])) && $this->LocalTarget[2]=='Undo')
					$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[3].')';
				$this->TempFillEls['subaction'] = 'Delete';
				$this->TempFillEls['subprocess'] = 'deleting';
				$this->TempFillEls['edit_id'] = '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']';
				$this->TempFillEls['step'] = 'delete';
				$this->TempFillEls['is_active'] = $this->PreparedTemplates['is_active']? 'Yes' : 'No';
				$this->TempFillEls['is_chart'] = $this->PreparedTemplates['is_chart']? 'Yes' : 'No';
				$this->TempFillEls['name'] = $this->PreparedTemplates['name'];
				$this->TempFillEls['sys_name'] = $this->PreparedTemplates['sys_name'];
				$this->TempFillEls['root_check'] = $this->PreparedTemplates['path2']? ' Yes -> ' : ' No';
				$this->TempFillEls['path2'] = $this->PreparedTemplates['path2'];
				$this->TempFillEls['desc'] = $this->PreparedTemplates['description'];
				$this->TempFillEls['cat_type'] = @$this->cat_type[$this->PreparedTemplates['cat_type']];
				$this->TempFillEls['color'] = @$this->prod_colors[$this->PreparedTemplates['color']['id']];
				$this->TempFillEls['parent'] = $this->categories[$this->PreparedTemplates['ownerid']];
				$link_type = array('1' => 'Featured Links','2' => 'Featured Retailers');
				$number_link = !empty($this->PreparedTemplates['links']) && count($this->PreparedTemplates['links']) > 3? count($this->PreparedTemplates['links']) : 3;
				for($i = 1; $i <= $number_link; $i++) {
					$this->TempFillEls['links'][$i] = array(
						'name' => @$this->PreparedTemplates['links'][$i]['description']? @$this->PreparedTemplates['links'][$i-1]['description'] : '&nbsp;',
						'js' => @$this->PreparedTemplates['links'][$i]['js']? 'Yes' : 'No',
						'type' => @$this->PreparedTemplates['links'][$i]['linktype']? $link_type[@$this->PreparedTemplates['links'][$i-1]['linktype']] : '-- empty --',
						'link' => @$this->PreparedTemplates['links'][$i]['link']? @$this->PreparedTemplates['links'][$i-1]['link'] : '&nbsp;',
						'url' => @$this->PreparedTemplates['links'][$i]['url']? @$this->PreparedTemplates['links'][$i-1]['url'] : '&nbsp;',
						'id' => inputHidden('form[links]['.$i.'][moduletype]',1),
						'key' => $i,
					);
				}
				$number_review = !empty($this->PreparedTemplates['reviews']) && count($this->PreparedTemplates['reviews']) > 0? count($this->PreparedTemplates['reviews']) : 0;
				for($i = 1; $i <= $number_review; $i++) {
					$this->TempFillEls['reviews'][$i] = array(
						'num' => $i,
						'url' => isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['url'] : ' --- Empty --- ',
						'image_url' => isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_url'] : ' --- Empty --- ',
						'image_alt' => isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_alt'] : ' --- Empty --- ',
						'image_title' => isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['image_title'] : ' --- Empty --- ',
						'description' => isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['description'] : ' --- Empty --- ',
						'sort_order' => isset($this->PreparedTemplates['reviews'][$i-1])? $this->PreparedTemplates['reviews'][$i-1]['sort_order'] : $i*10,
					);
				}
				$number_cr = !empty($this->PreparedTemplates['customerreviewitems']) && count($this->PreparedTemplates['customerreviewitems']) > 0? count($this->PreparedTemplates['customerreviewitems']) : 0;
				for($i = 1; $i <= $number_cr; $i++) {
					$this->TempFillEls['cr_items'][$i] = array(
						'num' => $i,
						'sort_order' => isset($this->PreparedTemplates['customerreviewitems'][$i-1])? $this->PreparedTemplates['customerreviewitems'][$i-1]['sort_order'] : $i*10,
						'name' => strip_tags($this->customerreviews[$this->PreparedTemplates['customerreviewitems'][$i-1]['object']]),
					);
				}
				$selected = $this->Db->SelectAny("select lc.*,m.username as member from categorycomment lc, member m where m.id=lc.member and lc.ownerid = ".$this->PreparedTemplates['id']." order by lc.version DESC LIMIT 10 OFFSET 0");
				if(!empty($selected)) {
					$this->TempFillEls['ViewAllButton']='<input type="button" name="View_All" class=adminsubmit value="View All" onclick="open(\'/Registar/CategoriesManager/ViewAll/'.$this->PreparedTemplates['id'].'\',\'displayWindow\',\'width=950,height=600,scrollbars=yes\');">';
					foreach($selected as $ind => $val){
						$tok = explode(".",$val['date']);
						$this->TempFillEls['comment'][] = array(
							'number' => ($ind+1),
							'date' => $tok[0],
							'author' => $val['member'],
							'changed' => $val['value'],
							'version' => $val['version'],
							'button' => $ind!=0 && file_exists($ROOT_PATH.'/undo/category/'.$this->PreparedTemplates['id'].'/'.$val['version'].'.inc')? array(array('id' => $this->PreparedTemplates['id'],'version' => $val['version'])) : array(),
						);
					}
				}
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'delete');
			break;
			}
			case 'Add': {
				$this->TempFillEls['subaction'] = 'Add';
				$this->TempFillEls['subprocess'] = 'adding';
				$this->TempFillEls['step'] = 'add';
				$this->TempFillEls['is_active'] = inputHidden('form[is_active]',0).inputCheckbox('form[is_active]',(!empty($this->Request)? $this->getRequestFormField('is_active') : 1),' class=check');
				$this->TempFillEls['is_chart'] = inputHidden('form[is_chart]',0).inputCheckbox('form[is_chart]',(!empty($this->Request)? $this->getRequestFormField('is_chart') : 1),' class=check');
				$this->TempFillEls['name'] = inputText('form[name]',$this->getRequestFormField('name'),55,'style="width:250px;"');
				$this->TempFillEls['sys_name'] = inputText('form[sys_name]',$this->getRequestFormField('sys_name'),55,'style="width:250px;"');
				$this->TempFillEls['root_check'] = inputCheckbox('root_check',(!empty($this->Request)? $this->getRequestField('root_check') : ($this->getRequestFormField('path2')? 1 : 0)),' onClick="javascript:correctRootPath();"');
				$this->TempFillEls['path2'] = inputText('form[path2]',$this->getRequestFormField('path2'),75,'style="width:460px;display:'.(validate($this->getRequestField('root_check')) || $this->getRequestFormField('path2')? 'inline' : 'none').'"');
				$this->TempFillEls['desc'] = inputTextArea('form[description]',@$this->Request['form']['description'],100,8,'style="width:740px;"');
				$this->TempFillEls['parent'] = inputSelect('form[ownerid]',$this->categories,$this->getRequestFormField('ownerid'),'style="width:250px;"');
				$this->TempFillEls['cat_type'] = inputSelect('form[cat_type]',$this->cat_type,$this->getRequestFormField('cat_type'),'style="width:250px;"');
				$this->TempFillEls['color'] = inputSelect('form[color][id]',$this->prod_colors,$this->getRequestFormField('color',1),'style="width:250px;"');
				$this->TempFillEls['review_buttons'] = array(array());
				$number_link = !empty($this->Request['form']['links']) && count($this->Request['form']['links']) > 3? count($this->Request['form']['links']) : 3;
				for($i = 1; $i <= $number_link; $i++) {
					$this->TempFillEls['links'][$i] = array(
						'name' => inputTextArea('form[links]['.$i.'][description]',@$this->Request['form']['links'][$i]['description'],55,2,'style="width:350px;"'),
						'js' => inputCheckbox('form[links]['.$i.'][js]',@$this->Request['form']['links'][$i]['js'],' class=check',1),
						'type' => inputSelect('form[links]['.$i.'][linktype]',array('1' => 'Featured Links','2' => 'Featured Retailers'),@$this->Request['form']['links'][$i]['linktype']),
						'link' => inputTextArea('form[links]['.$i.'][link]',@$this->Request['form']['links'][$i]['link'],55,2,'style="width:300px;"'),
						'url' => inputTextArea('form[links]['.$i.'][url]',@$this->Request['form']['links'][$i]['url'],55,2,'style="width:350px;"'),
						'id' => inputHidden('form[links]['.$i.'][moduletype]',1),
						'key' => $i,
						'delete_link' => array(array('key' => $i)),
					);
				}
				$number_review = !empty($this->Request['form']['reviews']) && count($this->Request['form']['reviews']) > 0? count($this->Request['form']['reviews']) : 0;
				for($i = 1; $i <= $number_review; $i++) {
					$this->TempFillEls['reviews'][$i] = array(
						'num' => $i,
						'url' => inputTextArea('form[reviews]['.$i.'][url]',@$this->Request['form']['reviews'][$i]['url'],32,2,'style="width:320px;"'),
						'image_url' => inputTextArea('form[reviews]['.$i.'][image_url]',@$this->Request['form']['reviews'][$i]['image_url'],32,2,'style="width:320px;"'),
						'image_alt' => inputTextArea('form[reviews]['.$i.'][image_alt]',@$this->Request['form']['reviews'][$i]['image_alt'],32,2,'style="width:320px;"'),
						'image_title' => inputTextArea('form[reviews]['.$i.'][image_title]',@$this->Request['form']['reviews'][$i]['image_title'],32,2,'style="width:320px;"'),
						'description' => inputTextArea('form[reviews]['.$i.'][description]',@$this->Request['form']['reviews'][$i]['description'],32,2,'style="width:320px;"'),
						'sort_order' => inputText('form[reviews]['.$i.'][sort_order]',(!empty($this->Request)? (!empty($this->Request['form']['reviews'][$i]['sort_order'])? $this->Request['form']['reviews'][$i]['sort_order'] : $i*10) : $i*10),2,'style="width:30px;"'),
						'delete_review' => array(array('num' => $i)),
						'moduletype' => inputHidden('form[reviews]['.$i.'][moduletype]',2),
					);
					if(!empty($this->errors['review'][$i]))
						foreach($this->errors['review'][$i] as $key => $value) $this->TempFillEls['reviews'][$i][$key.'_error'] = $this->printError($value);
				}
				$number_cr = !empty($this->Request['form']['customerreviewitems']) && count($this->Request['form']['customerreviewitems']) > 0? count($this->Request['form']['customerreviewitems']) : 0;
				for($i = 1; $i <= $number_cr; $i++) {
					$this->TempFillEls['cr_items'][$i] = array(
						'num' => $i,
						'sort_order' => inputText('form[customerreviewitems]['.$i.'][sort_order]',(!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['sort_order'] : $i*10),2,'style="width:30px;"'),
						'delete_cr' => array(array('num' => $i)),
						'object' => inputHidden('form[customerreviewitems]['.$i.'][object]', (!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['object'] : 0)),
						'name' => inputText('form[customerreviewitems]['.$i.'][name]', htmlspecialchars(strip_tags($this->customerreviews[(!empty($this->Request)? @$this->Request['form']['customerreviewitems'][$i]['object'] : '')]), ENT_QUOTES),2,'disabled="true" style="width:480px;"'),
						'type' => inputHidden('form[customerreviewitems]['.$i.'][moduletype]', 2),
						'change_cr' => array(array('num' => $i)),
					);
					if(!empty($this->errors['cr'][$i]))
						foreach($this->errors['cr'][$i] as $key => $value) $this->TempFillEls['cr_items'][$i][$key.'_error'] = $this->printError($value);
				}
				$this->TempFillEls['link_button'] = array(array());
				$this->TempFillEls['cr_buttons'] = array(array());
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'save');
			break;
			}
		}
		$this->TempFillEls['new_param_id'] = $number_link;
		$this->TempFillEls['new_param_id_cr'] = $number_cr;
		$this->TempFillEls['new_param_id_review'] = $number_review;
		!empty($this->PreparedTemplates['_error']) and $this->errors['model'] = $this->PreparedTemplates['_error'];
		if(count($this->errors) > 0) $this->errors['header'] = 'There were some errors.<br>Please see below for details';
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $this->printError($v).($k=='header'? '<p>' : '');
	}

	function setCategories($array = array(),$level = 1,$i = 1,$subitems = array()) {
		global $i;
		foreach($array as $key => $value) {
			$this->categories[$value['id']] = str_repeat("-",$level*2).' '.$value['name'];
			$i++;
			if(!empty($value['subcat'])) $this->setCategories($value['subcat'],$level+1);
		}
	}

	function setTargetArray($array = array(),$level = 1,$levels = array()) {
		$i = 0;
		$btnEdit_vis = $this->server_type == 'dev' ? 'visible' : 'hidden';
		foreach($array as $value) {
			$levels[$level] = $i==sizeof($array)-1? 1 : 0;
			$locked = empty($value['access_flag']) || $value['access_flag'] == $this->Action->Security->currentuser->id ? false : true;
			$presets_count = explode('/', $value['published_count']);
			$c = count($value['publish']);
			$this->TempFillEls['pages'][$value['id']] = array(
				'id' => $value['id'],
				'name' => $value['name'],
				'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
				'published_count' => $value['published_count'],
				'published_count_cursor' => $c ? 'pointer' : 'auto',
				'published_count_color' => $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))],
				'chbAdds_disabled' => $locked ? 'disabled' : '',
				'date_changed' => $value['date_changed'],
				'btnUnlock_vis' => $value['access_flag'] == $this->Action->Security->currentuser->id ? 'visible' : 'hidden',
				'btnEdit_vis' => $value['id']==1? 'hidden' : $btnEdit_vis, //$btnEdit_vis,
				'btnView_vis' => $value['is_active']? 'visible' : 'hidden',
				'chbAdds_vis' => $this->TempFillEls['pubBut'],
				'btnUnlock_disp' => $locked ? 'none' : '',
				'btnEdit_disp' => $locked ? 'none' : '',
				'btnView_disp' => $locked ? 'none' : '',
				'locked_message_disp' => $locked ? '' : 'none',
				'user_name' => $value['username'],
				'date_locked' => $value['date_locked'],
				'path' => $this->path_sticker($value['id']),
				'width' => intval($level*25),
				'width2' => 319 - intval($level*25),
				'start_div' => '<div id=page'.$value['id'].(!empty($value['subcat'])? ' style="display: none;"' : '').'>',
				'minus' => !empty($value['subcat'])? '_minus' : '',
			);
			  //---products
			  $result = array();
				$result = $this->Db->SelectArray(array('product p, categorylineitem cli'),array('p.name','p.id','cli.sort_order','cli.id as li_id'),array('cli.ownerid' => $value['id'],'p.id' => 'cli.item'),'sort_order asc');
				$this->TempFillEls['pages'][$value['id']]['products'] = $result;

			if(!empty($value['subcat'])) {
				$this->TempFillEls['pages'][$value['id']]['header'] = array(array(
					'id' => $value['id'],
					'name' => $value['name'],
					'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
					'published_count' => $value['published_count'],
					'published_count_cursor' => $c ? 'pointer' : 'auto',
					'published_count_color' => $this->colors[($presets_count[0] >= $presets_count[1] ? 'green' : (empty($presets_count[0]) ? 'red' : 'blue'))],
					'chbAdds_disabled' => $locked ? 'disabled' : '',
					'date_changed' => $value['date_changed'],
					'btnUnlock_vis' => $value['access_flag'] == $this->Action->Security->currentuser->id ? 'visible' : 'hidden',
					'btnEdit_vis' => $btnEdit_vis,
					'chbAdds_vis' => $this->TempFillEls['pubBut'],
					'btnUnlock_disp' => $locked ? 'none' : '',
					'btnEdit_disp' => $locked ? 'none' : '',
					'locked_message_disp' => $locked ? '' : 'none',
					'user_name' => $value['username'],
					'date_locked' => $value['date_locked'],
					'path' => $this->path_sticker($value['id']),
					'width' => intval($level*25),
					'width2' => 319 - intval($level*25),
				));
				$this->TempFillEls['pages'][$value['id']]['onclick'] = array(array('id' => $value['id']));
				$this->setTargetArray($value['subcat'],$level+1,$levels);
			}	else {
						if($level==1) $this->TempFillEls['pages'][$value['id']]['end_div'] = '</div>';
							else $this->TempFillEls['pages'][$value['id']]['end_div'] = !empty($levels[$level])? (!empty($levels[$level-1])? (!empty($levels[$level-2])? str_repeat('</div>',4) : str_repeat('</div>',3)) : str_repeat('</div>',2)) : '</div>';
			  }
			$i++;
		}
		
	}

	function setSortArray($array = array(),$level = 1,$levels = array()) {
		$i = 0;
		foreach($array as $value) {
			$levels[$level] = $i==sizeof($array)-1? 1 : 0;
			$this->TempFillEls['pages'][$value['id']] = array(
				'id' => $value['id'],
				'ownerid' => $value['ownerid'],
				'name' => $value['name'],
				'sort_order' => $value['sort_order'],
				'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
				'path' => $this->path_sticker($value['id']),
				'width' => intval($level*25),
				'width2' => 440 - intval($level*25),
				'start_div' => '<div id=page'.$value['id'].(!empty($value['subcat'])? ' style="display: none;"' : '').'>',
				'minus' => !empty($value['subcat'])? '_minus' : '',
			);
			if(!empty($value['subcat'])) {
				$this->TempFillEls['pages'][$value['id']]['header'] = array(array(
					'id' => $value['id'],
					'ownerid' => $value['ownerid'],
					'name' => $value['name'],
					'sort_order' => $value['sort_order'],
					'name_color' => $this->colors[($value['unmodified'] > 30 ? 'black' : ($value['unmodified'] > 14 && $value['unmodified'] <= 30 ? 'green' : ($value['unmodified'] > 7 && $value['unmodified'] <= 14 ? 'yellow' : 'red')))],
					'path' => $this->path_sticker($value['id']),
					'width' => intval($level*25),
					'width2' => 440 - intval($level*25),
				));
				$this->TempFillEls['pages'][$value['id']]['onclick'] = array(array('id' => $value['id']));
				$this->setSortArray($value['subcat'],$level+1,$levels);
			}
			else {
				if($level==1) $this->TempFillEls['pages'][$value['id']]['end_div'] = '</div>';
				else $this->TempFillEls['pages'][$value['id']]['end_div'] = !empty($levels[$level])? (!empty($levels[$level-1])? (!empty($levels[$level-2])? str_repeat('</div>',4) : str_repeat('</div>',3)) : str_repeat('</div>',2)) : '</div>';
			}
			$i++;
		}
	}

	function sortingPages() {
		foreach($this->getRequestField('form') as $k => $v) {
			foreach($v as $key => $value)
				if(isset($value['sort_order']))
					$this->Request['form'][$k][$key]['sort_order'] = intval($value['sort_order']);
			uasort($this->Request['form'][$k],'by_order');
			$i = 1;
			foreach($this->Request['form'][$k] as $key => $value) {
				$sort[$key]['sort_order'] = $i*10;
				$i++;
			}
		}
		$this->Request['form'] = $sort;
		return true;
	}

	function validateMenuArray($array = array(),$parent) {
		if(empty($array)) $array = array();
		$categories = $this->Action->Server->getGuide('category');
		foreach($array as $value) {
			if($value['id']==$parent) return 1;
			if(!empty($categories[$value['id']])) $this->validateMenuArray($categories[$value['id']],$parent);
		}
		return 0;
	}
	
	function path_sticker($id, $p = '') {
		global $Paths;
		$selected = $this->Db->SelectAny('SELECT path2 FROM category where id='.$id);
		if(empty($selected[0]['path2'])) {
			if(empty($Paths)) {
				$selected = $this->Db->SelectAny('SELECT id, sys_name, ownerid, path2 FROM category');
				foreach($selected as $value)
					$Paths[$value['id']] = array(
						'path' => $value['sys_name'],
						'parent' => $value['ownerid'],
						'path2' => $value['path2'],
					);
			}
			while(1){
				if(empty($Paths[$id])) return null;
				if(empty($Paths[$id]['parent'])  || $Paths[$id]['parent'] == 1 ) return '/'.$Paths[$id]['path'].$p;
				if(!empty($Paths[$id]['path2'])) return '/'.$Paths[$id]['path2'].$p;
				$p = '/'.$Paths[$id]['path'].$p;
				$id = $Paths[$id]['parent'];
			}
		}
		else return '/'.$selected[0]['path2'];
	}
}
?>