<?php

//global $Debug;$Debug = true;

class AccessoriesManagerView extends Slot {

	var $SectionID = 20;
	var $ModuleID = 2;
//	var $warranty = array();
	var $parentitems = array();
	var $products = array();
	var $shipcosts = array();
	var $upload_formats = array(
		'image/jpeg' => 'jpg',
		'image/pjpeg' => 'jpg',
		'image/jpeg' => 'jpg',
	 	'image/png' => 'jpg',
	);
	var $upload_settings = array(
		'prew_width' => 115,
		'prew_height' => 82,
		'middle_width' => 115,
		'middle_height' => 82,
	);
	var $colors = array(
		'red' => '#DC143C',
		'yellow' => '#FF8C00',
		'green' => '#006400',
		'black' => '#000000',
		'blue' => '#4169E1',
	);
	var $terminated = false;

	function __construct($pquery) {
		$this->Slot($pquery);
	}

	function init() {   
		global $TOOLS_PATH;
		$undo = 0;
		$accessories1 = $this->Db->SelectGuide(array('parentitem i','product g'),array('g.id','g.name'),array('g.item' => 'i.id','i.itemtype' => 2));
		$accessories2 = $this->Db->SelectGuide(array('parentitem i','product g'),array('g.id','g.name'),array('g.item' => 'i.id','i.itemtype' => 3));
		$accessories = mergeArrays($accessories1,$accessories2);
		switch(@$this->LocalTarget[0]) {
			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_productcomments',$request,$this);
					if(!empty($this->PreparedTemplates['_error'])) $this->Scheme = 'message';
					else {
						$this->getPageControl($this->Home.'/Registar/ProductsManager/ViewAll/'.$this->LocalTarget[1]);
						if(empty($this->PreparedTemplates['curPage'])) $this->Scheme = 'empty';
					}
					$this->Scheme = 'viewall';
				}
				else $this->Scheme = 'default';
			break;
			}
			case 'DeleteFile': {
				if(!empty($this->LocalTarget[1]) && in_array($this->LocalTarget[1],array_keys($products))) {
					switch(@$this->LocalTarget[2]) {
						case 'ug': {
							$this->Action->processEvent($this->SectionID,'delete_file',array('id' => $this->LocalTarget[1],'type' => 7),$this);
							if(empty($this->PreparedTemplates['_error'])) die('200');
							else die('400');
						break;
						}
						case 'ss': {
							$this->Action->processEvent($this->SectionID,'delete_file',array('id' => $this->LocalTarget[1],'type' => 6),$this);
							if(empty($this->PreparedTemplates['_error'])) die('200');
							else die('400');
						break;
						}
						case 'hi': {
							$this->Action->processEvent($this->SectionID,'delete_file',array('id' => $this->LocalTarget[1],'type' => 5),$this);
							if(empty($this->PreparedTemplates['_error'])) die('200');
							else die('400');
						break;
						}
						default: die('400'); break;
					}
				}
			break;
			}
			case 'Add': {
				$this->getGuides();
				//$this->Action->processEvent($this->SectionID,'tree',array('full' => 0,'admin' => 1,'parent' => 0,'type' => 1),$this);
				$this->Action->processEvent($this->SectionID,'treeacc',array('full' => 0,'admin' => 1,'parent' => 0,'type' => 1),$this);
				//printArray($this->PreparedTemplates);
				$this->Action->processEvent($this->SectionID,'open',array('id' => 0,'ship' => 1),$this);
				$this->Scheme = 'action';
			break;
			}
			case 'Update': case 'Duplicate': case 'Delete': {
				$this->getGuides();
				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($accessories))) {
						$array = array('id' => $this->LocalTarget[1],'ship' => 1);
						if($undo == 1) $array['undo'] = $this->LocalTarget[3];
						$this->Action->processEvent($this->SectionID,'getlock',array('product' => $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/AccessoriesManager'.$this->SID);
							$this->terminated = true;
							return;
						}
						else {
							if(empty($this->PreparedTemplates['access_flag'])) {
								$this->Action->processEvent($this->SectionID,'lock',array('product' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
							}
//							$this->Action->processEvent($this->SectionID,'tree',array('full' => 0,'admin' => 1,'parent' => 0,'type' => 1),$this);
							$this->Action->processEvent($this->SectionID,'treeacc',array('full' => 0,'admin' => 1,'parent' => 0,'type' => 1),$this);
							$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/AccessoriesManager'.$this->SID);
				}
				else header('Location: '.$this->Home.'/Registar/AccessoriesManager'.$this->SID);
			break;
			}
			case 'Unlock': {
				if($this->server_type=='dev' && isset($this->LocalTarget[1])) {
					$this->Action->processEvent($this->SectionID,'getlock',array('product' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
					if(!empty($this->PreparedTemplates)){
						$this->Action->processEvent($this->SectionID,'unlock',array('product' => $this->LocalTarget[1],'member' => $this->Action->Security->currentuser->id),$this);
					}	
					header('Location: '.$this->Home.'/Registar/AccessoriesManager'.$this->SID);
				}
				else $this->Scheme = 'default';
			break;
			}
			case 'Sort': {
				$this->Action->processEvent($this->SectionID,'list',array('full' => 0),$this);
				$this->Scheme = 'sort';
			break;
			}
			default: {
				if(!empty($this->LocalTarget[0])){
					header('Location: '.$this->Home.'/Registar/AccessoriesManager'.$this->SID);
					$this->terminated = true;
					return;
				}
				$this->Action->processEvent($this->SectionID,'list',array('full' => 1),$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->validateForm()
					and $this->Action->processEvent($this->SectionID,'save',$this->getRequestField('form'),$this)
					and header('Location: '.$this->Home.'/Registar/AccessoriesManager'.$this->SID);
					//printArray($this->Request);
				break;
				}
				case 'delete': {
					$this->Action->processEvent($this->SectionID,'delete',$this->getRequestField('form'),$this)
					and $this->clearUndo($this->getRequestFormField('id'))
					and header('Location: '.$this->Home.'/Registar/AccessoriesManager'.$this->SID);
				break;
				}
				case 'sort': {
					$this->sortingPages()
					and $this->Action->processEvent($this->SectionID,'sort',$this->getRequestField('form'),$this)
					and header('Location: '.$this->Home.'/Registar/AccessoriesManager/Sort'.$this->SID);
				break;
				}
			}
		}
		$this->setSchemeTemplate($this->Scheme);

	}

	function makeImageLink($type,$id) {
		foreach($this->upload_formats as $value) {
			if(file_exists($this->ImagePath.'/products/'.$id.'/'.$type.'.'.$value))
				return makeImage($this->ImagePath.'/products/'.$id.'/'.$type.'.'.$value,$this->ImageWPath.'/products/'.$id.'/'.$type.'.'.$value,'','',array(),0,'',$type.'img');
		}
		return makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'',$type.'img');
	}

	function deleteFiles($array) {
		$path = $this->ImagePath.'/products/'.$array['id'];
		$output = shell_exec("rm -rf ".$path." 2>&1");
	}

	function saveFiles($array) {
		$path = $this->ImagePath.'/products/'.$array['id'];
		if($this->getRequestField('step')=='add' || !file_exists($path)) {
			umask(0002); mkdir($path,0775);
		}
		if(validate($this->getRequestFormField('duplicate_id'))) {
			$output = shell_exec("cp -r -a ".$this->ImagePath."/products/".$this->getRequestFormField('duplicate_id')."/* ".$path." 2>&1");
		}
		if(!empty($this->Request['prew_image_link'])) {
			foreach($this->upload_formats as $value)
				if(file_exists($path.'/prew.'.$value)) unlink($path.'/prew.'.$value);
		}
		else {
			if(!empty($this->Request['form']['prew'])) {
				$image = $this->Request['form']['prew'];
				if(!empty($image['_tmp_name'])) {
					foreach($this->upload_formats as $value)
						if(file_exists($path.'/prew.'.$value)) unlink($path.'/prew.'.$value);
					copy($image['_tmp_name'],$path.'/prew.'.$image['image_type']);
					chmod($path.'/prew.'.$image['image_type'],0664);
				}
			}
		}
//printArray($this->Request['form']);exit;		
		if(!empty($this->Request['middle_image_link'])) {
			foreach($this->upload_formats as $value)
				if(file_exists($path.'/middle.'.$value)) unlink($path.'/middle.'.$value);
		}
		else {
			if(!empty($this->Request['form']['middle'])) {
				$image = $this->Request['form']['middle'];
				if(!empty($image['_tmp_name'])) {
					foreach($this->upload_formats as $value)
						if(file_exists($path.'/middle.'.$value)) unlink($path.'/middle.'.$value);
					copy($image['_tmp_name'],$path.'/middle.'.$image['image_type']);
					chmod($path.'/middle.'.$image['image_type'],0664);
				}
			}
		}
	}

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

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

	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/product/'.$this->PreparedTemplates[1].'/'.$val['version'].'.inc')? array(array('id' => $this->PreparedTemplates[1],'version' => $val['version'])) : array(),
			);
		}
	}

	function setSortArray($array = array()) {
		foreach($array as $value) {
			$this->TempFillEls['pages'][$value['id']] = array(
				'id' => $value['id'],
				'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')))],
			);
		}
	}

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

	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);
	}

	function fillSelect() {
//printArray($this->PreparedTemplates);exit;
		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);
	}

	function getGuides() {
		$this->categories = $this->Db->SelectGuide(array('categorylineitem g'),array('g.id','g.ownerid as name'),array('g.item' => (isset($this->LocalTarget[1])? $this->LocalTarget[1] : 0)));
	//	$this->warranty = mergeArrays(array('0' => ' --- Select Warranty --- '),$this->Action->Server->getGuide('warranty'));
		$this->parentitems1 = mergeArrays(array('0' => ' --- Select Parent Item --- '),$this->Db->SelectGuide(array('parentitem g'),array('g.id','g.name'),array('itemtype' => 2)));
		$this->parentitems2 = mergeArrays(array('0' => ' --- Select Parent Item --- '),$this->Db->SelectGuide(array('parentitem g'),array('g.id','g.name'),array('itemtype' => 3)));
		$this->parentitems = mergeArrays($this->parentitems1,$this->parentitems2);
		$this->products = mergeArrays(array('0' => ' --- Select Product --- '),$this->Db->SelectGuide(array('parentitem i','product g'),array('g.id','g.name'),array('g.item' => 'i.id','i.itemtype' => 1)));
		$this->accessories = mergeArrays(array('0' => ' --- Select Accessory --- '),$this->Db->SelectGuide(array('parentitem i','product g'),array('g.id','g.name'),array('g.item' => 'i.id','i.itemtype' => 2)));
		$this->kits = $this->Db->SelectGuide(array('parentitem i','product g'),array('g.id','g.name'),array('g.item' => 'i.id','i.itemtype' => 3));
		$this->accessories = mergeArrays($this->accessories,$this->kits);
		$this->shipcosts = $this->Db->SelectGuide(array('shipcosttype g'),array('g.id','g.name'),array('1' => '1'));
		$this->export = mergeArrays(array('0' => ' --- Select Export Restriction --- '),$this->Db->SelectGuide(array('export g'),array('g.id','g.name'),array('1' => '1')));
	}

	function validateForm() {
		if(!validate($this->getRequestFormField('name'))) $this->errors['name'] = 'Product Name is empty';
		if( empty($this->Request['form']['full_desc']) ) $this->Request['form']['full_desc'] = ' ';
		//if(!validate($this->getRequestFormField('header'))) $this->errors['name'] = 'Product Header is empty';
		if(!validate($this->getRequestFormField('item',1))) $this->errors['item'] = 'Parent Item is empty';
		//if(!validate($this->getRequestFormField('warranty',1))) $this->errors['warranty'] = 'Product Warranty is empty';
		//if(!is_AlphaNum($this->getRequestFormField('pid'),1,32)) $this->errors['pid'] = 'Item # is empty or is not valid';
		empty($this->Request['form']['applicableitems']) and $this->Request['form']['applicableitems'] = array();
		foreach($this->Request['form']['applicableitems'] as $key => $value)
			if(empty($value['product']['id'])) unset($this->Request['form']['applicableitems'][$key]);
		foreach($this->Request['form']['shipcosts'] as $key => $value) {
			//if(empty($value['value'])) $this->Request['form']['shipcosts'][$key]['value'] = '0.00';
			if(empty($value['value_zone1'])) $this->Request['form']['shipcosts'][$key]['value_zone1'] = '0.00';
			if(empty($value['value_zone2'])) $this->Request['form']['shipcosts'][$key]['value_zone2'] = '0.00';
			if(empty($value['value_zone3'])) $this->Request['form']['shipcosts'][$key]['value_zone3'] = '0.00';
		}
		
		
		if(!validate($this->getRequestField('prew_image_link'))) {
			$image = $this->getRequestField('prew_image');
			if(!empty($image['name'])) {
				if(!$image['size']) $this->errors['prew_image'] = 'Error uploading - image is empty';
				else {
					$size = GetImageSize($image['tmp_name']);
					//if($size['0'] != $this->upload_settings['prew_width']) $this->errors['prew_image'] = 'Error uploading - width of image is not valid';
					//else if($size['1'] != $this->upload_settings['prew_height']) $this->errors['prew_image'] = 'Error uploading - height of image is not valid';
					//else 
					if($size['mime']!='image/png') $this->errors['prew_image'] = 'Error uploading - type of image is not valid';
					else {
						$this->Request['form']['prew']['image_type'] = $this->upload_formats[$size['mime']];
						$this->Request['form']['prew']['_tmp_name'] = $image['tmp_name'];
						$this->Request['form']['prew']['image_link'] = $this->getRequestField('prew_image_link');
					}
				}
			}
			else if($this->LocalTarget[0]!='Add'){
				foreach($this->PreparedTemplates['files'] as $value) {
					if($value['filetype']==1 && validate($value['image_link'])) $this->errors['prew_image'] = 'Error uploading - image is empty';
				}
			}
		}
		else {
			if($this->getRequestField('prew_image_link')==$this->getRequestFormField('id')) $this->errors['prew_image'] = 'Error uploading - Accessory can\'t use itself as a link to image';
			$this->Request['form']['prew']['image_type'] = ' ';
			$this->Request['form']['prew']['image_link'] = $this->getRequestField('prew_image_link');
		}
		if(!validate($this->getRequestField('middle_image_link'))) {
			$image = $this->getRequestField('middle_image');
			if(!empty($image['name'])) {
				if(!$image['size']) $this->errors['middle_image'] = 'Error uploading - image is empty';
				else {
					$size = GetImageSize($image['tmp_name']);
					//if($size['0'] != $this->upload_settings['middle_width']) $this->errors['middle_image'] = 'Error uploading - width of image is not valid';
					//else if($size['1'] != $this->upload_settings['middle_height']) $this->errors['middle_image'] = 'Error uploading - height of image is not valid';
					//else 
					if($size['mime']!='image/png') $this->errors['prew_image'] = 'Error uploading - type of image is not valid';
					else {
						$this->Request['form']['middle']['image_type'] = $this->upload_formats[$size['mime']];
						$this->Request['form']['middle']['_tmp_name'] = $image['tmp_name'];
						$this->Request['form']['middle']['image_link'] = $this->getRequestField('middle_image_link');
					}
				}
			}
			else if($this->LocalTarget[0]!='Add'){
				foreach($this->PreparedTemplates['files'] as $value) {
					if($value['filetype']==8 && validate($value['image_link'])) $this->errors['middle_image'] = 'Error uploading - image is empty';
				}
			}
		}
		else {
			if($this->getRequestField('middle_image_link')==$this->getRequestFormField('id')) $this->errors['middle_image'] = 'Error uploading - Accessory can\'t use itself as a link to image';
			$this->Request['form']['middle']['image_type'] = ' ';
			$this->Request['form']['middle']['image_link'] = $this->getRequestField('middle_image_link');
		}
		
		
		if($this->LocalTarget[0] == 'Update')
			if(!is_FullText($this->getRequestFormField('comment'),5,4096)) $this->errors['comment'] = 'Comment is empty or is not valid';
//printArray($this->errors);			exit;
		if(count($this->errors)>0) return FALSE;
		return TRUE;
	}

	function fillAction() {
		global $ROOT_PATH;
		switch($this->LocalTarget[0]) {
			case 'Add': {
				$this->TempFillEls = array(
					'subaction' => 'Add',
					'subprocess' => 'adding',
					'step' => 'add',
					'is_active' => inputHidden('form[is_active]',0).inputCheckbox('form[is_active]',(!empty($this->Request)? $this->getRequestFormField('is_active') : 1),' class=check'),
					'callforquote' => inputHidden('form[callforquote]',0).inputCheckbox('form[callforquote]',(!empty($this->Request)? $this->getRequestFormField('callforquote') : 1),' class=check'),
					'pid' => inputText('form[pid]',$this->getRequestFormField('pid'),55,'style="width:260px;"'),
					'name' => inputText('form[name]',$this->getRequestFormField('name'),55,'style="width:260px;"'),
					'header' => inputText('form[header]',$this->getRequestFormField('header'),55,'style="width:260px;"'),
					'item' => inputSelect('form[item][id]',$this->parentitems,$this->getRequestFormField('item',1),'style="width:260px;"'),
				//	'warranty' => inputSelect('form[warranty][id]',$this->warranty,$this->getRequestFormField('warranty',1),'style="width:260px;"'),
//					'export_restriction' => inputSelect('form[export_restriction][id]',$this->export,$this->getRequestFormField('export_restriction',1),'style="width:260px;"'),
					'full_desc' => inputTextArea('form[full_desc]',$this->getRequestFormField('full_desc'),110,10,'style="width:730px;"'),
					'prew_image' => validate($this->getRequestField('prew_image_link'))? $this->makeImageLink('prew',$this->getRequestField('prew_image_link')) : makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','prewimg'),
					'prew_upload' => inputFile('prew_image'),
					'prew_image_link' => inputSelect('prew_image_link',$this->accessories,$this->getRequestField('prew_image_link'),'style="width:260px;" onchange="selectImage(\'prew\')"'),
					'prew_display' => validate($this->getRequestField('prew_image_link'))? 'none' : 'inline',
					'middle_image' => validate($this->getRequestField('middle_image_link'))? $this->makeImageLink('middle',$this->getRequestField('middle_image_link')) : makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','middleimg'),
					'middle_upload' => inputFile('middle_image'),
					'middle_image_link' => inputSelect('middle_image_link',$this->accessories,$this->getRequestField('middle_image_link'),'style="width:260px;" onchange="selectImage(\'middle\')"'),
					'middle_display' => validate($this->getRequestField('middle_image_link'))? 'none' : 'inline',
					'applicable_buttons' => array(array()),
				);
				$number_product = !empty($this->Request['form']['applicableitems']) && count($this->Request['form']['applicableitems']) > 3? count($this->Request['form']['applicableitems']) : 3;
				for($i = 1; $i <= $number_product; $i++) {
					$this->TempFillEls['applicable_items'][$i] = array(
						'num' => $i,
						'item' => inputSelect('form[applicableitems]['.$i.'][product][id]',$this->products,@$this->Request['form']['applicableitems'][$i]['product']['id'],'style="width:520px;"'),
						'delete_product' => array(array('num' => $i)),
					);
				}
				$number_price = 1;
				$this->TempFillEls['prices'][$i] = array(
					'num' => 1,
					'name' => 'Price ',
					'value' => inputText('form[prices][1][value]',@$this->Request['form']['prices']['1']['value'],15,'style="width:150px;"'),
				);
				foreach($this->shipcosts as $key => $value) {
					$this->TempFillEls['costs'][$key] = array(
				//		'value' =>       inputText('form[shipcosts]['.$key.'][value]',@$this->Request['form']['shipcosts'][$key]['value'],15,'style="width:150px;"'),
						'value_zone1' => inputText('form[shipcosts]['.$key.'][value_zone1]',@$this->Request['form']['shipcosts'][$key]['value_zone1'],15,'style="width:150px;"'),
						'value_zone2' => inputText('form[shipcosts]['.$key.'][value_zone2]',@$this->Request['form']['shipcosts'][$key]['value_zone2'],15,'style="width:150px;"'),
						'value_zone3' => inputText('form[shipcosts]['.$key.'][value_zone3]',@$this->Request['form']['shipcosts'][$key]['value_zone3'],15,'style="width:150px;"'),
						'id' => inputHidden('form[shipcosts]['.$key.'][type][id]',$key),
						'name' => $value,
					);
				}
				$this->TempFillEls['product_options'] = '';
				foreach($this->products as $key => $value) $this->TempFillEls['product_options'].='<option value=\"'.$key.'\">'.addslashes(preg_replace("/\r?\n/", " ", $value)).'</option>';
				//$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'tree');
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'treeacc');
				$this->setCategories($this->PreparedTemplates);
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'save');
			break;
			}
			case 'Update': {
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
				$path = '/products/'.$this->PreparedTemplates['id'];
				$files = array('prew' => array(),'big' => array(),'image' => array(),'3d' => array(),'hi' => array(),'ss' => array(),'ug' => array(),'middle' => array());
//printArray($this->PreparedTemplates['files']);exit;
				foreach($this->PreparedTemplates['files'] as $key => $value) {
					switch($value['filetype']) {
						case 1: $files['prew'] = array('image_link' => $value['image_link'],'name' => 'prew.'.$value['imagetype']); break;
						case 8: $files['middle'] = array('image_link' => $value['image_link'],'name' => 'middle.'.$value['imagetype']); break;
					}
				}
				$this->TempFillEls = array(
					'subaction' => 'Update',
					'subprocess' => 'editing',
					'step' => 'edit',
					'is_active' => inputHidden('form[is_active]',0).inputCheckbox('form[is_active]',(!empty($this->Request)? $this->getRequestFormField('is_active') : $this->PreparedTemplates['is_active']),' class=check'),
					'callforquote' => inputHidden('form[callforquote]',0).inputCheckbox('form[callforquote]',(!empty($this->Request)? $this->getRequestFormField('callforquote') : $this->PreparedTemplates['callforquote']),' class=check'),
					'pid' => inputText('form[pid]',(!empty($this->Request)? $this->getRequestFormField('pid') : $this->PreparedTemplates['pid']),55,'style="width:260px;"'),
					'name' => inputText('form[name]',(!empty($this->Request)? $this->getRequestFormField('name') : $this->PreparedTemplates['name']),55,'style="width:260px;"'),
					'header' => inputText('form[header]',(!empty($this->Request)? $this->getRequestFormField('header') : $this->PreparedTemplates['header']),55,'style="width:260px;"'),
					'item' => inputSelect('form[item][id]',$this->parentitems,(!empty($this->Request)? $this->getRequestFormField('item',1) : $this->PreparedTemplates['item']['id']),'style="width:260px;"'),
			//		'warranty' => inputSelect('form[warranty][id]',$this->warranty,(!empty($this->Request)? $this->getRequestFormField('warranty',1) : $this->PreparedTemplates['warranty']['id']),'style="width:260px;"'),
					'full_desc' => inputTextArea('form[full_desc]',(!empty($this->Request)? $this->Request['form']['full_desc'] : $this->PreparedTemplates['full_desc']),110,10,'style="width:730px;"'),
//					'export_restriction' => inputSelect('form[export_restriction][id]',$this->export,(!empty($this->Request)? $this->getRequestFormField('export_restriction',1) : $this->PreparedTemplates['export_restriction']['id']),'style="width:260px;"'),
					'id' => $this->PreparedTemplates['id'],
					'edit_id' => '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']',
					'prew_image' => !empty($files['prew']['image_link'])? $this->makeImageLink('prew',$files['prew']['image_link']) : (empty($files['prew']['name'])? makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','prewimg') : makeImage($this->ImagePath.$path.'/'.$files['prew']['name'],$this->ImageWPath.$path.'/'.$files['prew']['name'],'','',array(),0,'','prewimg')),
					'prew_upload' => inputFile('prew_image'),
					'prew_image_link' => inputSelect('prew_image_link',$this->accessories,(!empty($this->Request)? $this->getRequestField('prew_image_link') : @$files['prew']['image_link']),'style="width:260px;" onchange="selectImage(\'prew\')"'),
					//'prew_display' => (!empty($this->Request)? validate($this->getRequestField('prew_image_link')) : !empty($files['prew']['image_link']))? 'none' : 'inline',
					'prew_display' => (!empty($this->Request)? validate($this->getRequestField('prew_image_link')) : 0)? 'none' : 'inline',
					'middle_image' => !empty($files['middle']['image_link'])? $this->makeImageLink('middle',$files['middle']['image_link']) : (empty($files['middle']['name'])? makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','middleimg') : makeImage($this->ImagePath.$path.'/'.$files['middle']['name'],$this->ImageWPath.$path.'/'.$files['middle']['name'],'','',array(),0,'','middleimg')),
					'middle_upload' => inputFile('middle_image'),
					'middle_image_link' => inputSelect('middle_image_link',$this->accessories,(!empty($this->Request)? $this->getRequestField('middle_image_link') : @$files['middle']['image_link']),'style="width:260px;" onchange="selectImage(\'middle\')"'),
					//'middle_display' => (!empty($this->Request)? validate($this->getRequestField('middle_image_link')) : !empty($files['middle']['image_link']))? 'none' : 'inline',
					'middle_display' => (!empty($this->Request)? validate($this->getRequestField('middle_image_link')) : 0)? 'none' : 'inline',
					'applicable_buttons' => array(array()),
				);
				$number_product = !empty($this->Request['form']['applicableitems']) && count($this->Request['form']['applicableitems']) > 3? count($this->Request['form']['applicableitems']) : (!empty($this->PreparedTemplates['applicableitems']) && count($this->PreparedTemplates['applicableitems']) > 3? count($this->PreparedTemplates['applicableitems']) : 3);
				for($i = 1; $i <= $number_product; $i++) {
					$this->TempFillEls['applicable_items'][$i] = array(
						'num' => $i,
						'item' => inputSelect('form[applicableitems]['.$i.'][product][id]',$this->products,(!empty($this->Request)? @$this->Request['form']['applicableitems'][$i]['product']['id'] : (isset($this->PreparedTemplates['applicableitems'][$i-1])? $this->PreparedTemplates['applicableitems'][$i-1]['product']['id'] : '')),'style="width:520px;"'),
						'delete_product' => array(array('num' => $i)),
					);
				}
				$number_price = 1;
				$this->TempFillEls['prices'][$i] = array(
					'num' => 1,
					'name' => 'Price ',
					'value' => inputText('form[prices][1][value]',(!empty($this->Request)? @$this->Request['form']['prices']['1']['value'] : $this->PreparedTemplates['prices'][0]['value']),15,'style="width:150px;"'),
					'delete_price' => array(array('num' => 1)),
				);
				foreach($this->shipcosts as $key => $value) {
					$this->TempFillEls['costs'][$key] = array(
						//'value' => inputText('form[shipcosts]['.$key.'][value]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key)),15,'style="width:150px;"'),
						'value_zone1' => inputText('form[shipcosts]['.$key.'][value_zone1]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value_zone1'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key, 1)),15,'style="width:150px;"'),
						'value_zone2' => inputText('form[shipcosts]['.$key.'][value_zone2]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value_zone2'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key, 2)),15,'style="width:150px;"'),
						'value_zone3' => inputText('form[shipcosts]['.$key.'][value_zone3]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value_zone3'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key, 3)),15,'style="width:150px;"'),
						'id' => inputHidden('form[shipcosts]['.$key.'][type][id]',$key),
						'name' => $value,
					);
				}
				if(!empty($this->LocalTarget[3]) && validate(intval($this->LocalTarget[3])) && $this->LocalTarget[2]=='Undo')
					$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[3].')';
				$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/ProductsManager/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'] : date("M d, Y H:i:s")  ).'</textarea>';
				$selected = $this->Db->SelectAny("select cc.*,m.username as member from productcomment 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/product/'.$this->PreparedTemplates['id'].'/'.$val['version'].'.inc')? array(array('id' => $this->PreparedTemplates['id'],'version' => $val['version'])) : array(),
						);
					}
				}
				$this->TempFillEls['product_options'] = '';
				foreach($this->products as $key => $value) $this->TempFillEls['product_options'].='<option value=\"'.$key.'\">'.addslashes(preg_replace("/\r?\n/", " ", $value)).'</option>';
				//$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'tree');
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'treeacc');
				$this->setCategories($this->PreparedTemplates);
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'save');
			break;
			}
			case 'Duplicate': {
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
				$path = '/products/'.$this->PreparedTemplates['id'];
				$files = array('prew' => array(),'big' => array(),'image' => array(),'3d' => array(),'hi' => array(),'ss' => array(),'ug' => array(),'middle' => array());
				foreach($this->PreparedTemplates['files'] as $key => $value) {
					switch($value['filetype']) {
						case 1: $files['prew'] = array('image_link' => $value['image_link'],'name' => 'prew.'.$value['imagetype']); break;
						case 8: $files['middle'] = array('image_link' => $value['image_link'],'name' => 'middle.'.$value['imagetype']); break;
					}
				}
				$this->TempFillEls = array(
					'subaction' => 'Duplicate',
					'subprocess' => 'duplicating',
					'step' => 'add',
					'is_active' => inputHidden('form[is_active]',0).inputCheckbox('form[is_active]',(!empty($this->Request)? $this->getRequestFormField('is_active') : $this->PreparedTemplates['is_active']),' class=check'),
					'callforquote' => inputHidden('form[callforquote]',0).inputCheckbox('form[callforquote]',(!empty($this->Request)? $this->getRequestFormField('callforquote') : $this->PreparedTemplates['callforquote']),' class=check'),
					'pid' => inputText('form[pid]',(!empty($this->Request)? $this->getRequestFormField('pid') : $this->PreparedTemplates['pid']),55,'style="width:260px;"'),
					'name' => inputText('form[name]',(!empty($this->Request)? $this->getRequestFormField('name') : $this->PreparedTemplates['name']),55,'style="width:260px;"'),
					'header' => inputText('form[header]',(!empty($this->Request)? $this->getRequestFormField('header') : $this->PreparedTemplates['header']),55,'style="width:260px;"'),
					'item' => inputSelect('form[item][id]',$this->parentitems,(!empty($this->Request)? $this->getRequestFormField('item',1) : $this->PreparedTemplates['item']['id']),'style="width:260px;"'),
			//		'warranty' => inputSelect('form[warranty][id]',$this->warranty,(!empty($this->Request)? $this->getRequestFormField('warranty',1) : $this->PreparedTemplates['warranty']['id']),'style="width:260px;"'),
//					'export_restriction' => inputSelect('form[export_restriction][id]',$this->export,(!empty($this->Request)? $this->getRequestFormField('export_restriction',1) : $this->PreparedTemplates['export_restriction']['id']),'style="width:260px;"'),
					'full_desc' => inputTextArea('form[full_desc]',(!empty($this->Request)? $this->Request['form']['full_desc'] : $this->PreparedTemplates['full_desc']),110,10,'style="width:730px;"'),
					'edit_id' => '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']',
					'duplicate_id' => $this->PreparedTemplates['id'],
					'prew_image' => !empty($files['prew']['image_link'])? $this->makeImageLink('prew',$files['prew']['image_link']) : (empty($files['prew']['name'])? makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','prewimg') : makeImage($this->ImagePath.$path.'/'.$files['prew']['name'],$this->ImageWPath.$path.'/'.$files['prew']['name'],'','',array(),0,'','prewimg')),
					'prew_upload' => inputFile('prew_image'),
					'prew_image_link' => inputSelect('prew_image_link',$this->accessories,(!empty($this->Request)? $this->getRequestField('prew_image_link') : @$files['prew']['image_link']),'style="width:260px;" onchange="selectImage(\'prew\')"'),
					'prew_display' => (!empty($this->Request)? validate($this->getRequestField('prew_image_link')) : !empty($files['prew']['image_link']))? 'none' : 'inline',
					'middle_image' => !empty($files['middle']['image_link'])? $this->makeImageLink('middle',$files['middle']['image_link']) : (empty($files['middle']['name'])? makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','middleimg') : makeImage($this->ImagePath.$path.'/'.$files['middle']['name'],$this->ImageWPath.$path.'/'.$files['middle']['name'],'','',array(),0,'','middleimg')),
					'middle_upload' => inputFile('middle_image'),
					'middle_image_link' => inputSelect('middle_image_link',$this->accessories,(!empty($this->Request)? $this->getRequestField('middle_image_link') : @$files['middle']['image_link']),'style="width:260px;" onchange="selectImage(\'middle\')"'),
					'middle_display' => (!empty($this->Request)? validate($this->getRequestField('middle_image_link')) : !empty($files['middle']['image_link']))? 'none' : 'inline',
					'applicable_buttons' => array(array()),
				);
				$number_product = !empty($this->Request['form']['applicableitems']) && count($this->Request['form']['applicableitems']) > 3? count($this->Request['form']['applicableitems']) : (!empty($this->PreparedTemplates['applicableitems']) && count($this->PreparedTemplates['applicableitems']) > 3? count($this->PreparedTemplates['applicableitems']) : 3);
				for($i = 1; $i <= $number_product; $i++) {
					$this->TempFillEls['applicable_items'][$i] = array(
						'num' => $i,
						'item' => inputSelect('form[applicableitems]['.$i.'][product][id]',$this->products,(!empty($this->Request)? @$this->Request['form']['applicableitems'][$i]['product']['id'] : (isset($this->PreparedTemplates['applicableitems'][$i-1])? $this->PreparedTemplates['applicableitems'][$i-1]['product']['id'] : '')),'style="width:520px;"'),
						'delete_product' => array(array('num' => $i)),
					);
				}
				$number_price = 1;
				$this->TempFillEls['prices'][$i] = array(
					'num' => 1,
					'name' => 'Price ',
					'value' => inputText('form[prices][1][value]',(!empty($this->Request)? @$this->Request['form']['prices']['1']['value'] : $this->PreparedTemplates['prices'][0]['value']),15,'style="width:150px;"'),
					'delete_price' => array(array('num' => 1)),
				);
				foreach($this->shipcosts as $key => $value) {
					$this->TempFillEls['costs'][$key] = array(
					  //'value'       => inputText('form[shipcosts]['.$key.'][value]',      (!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value']       : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key   )),15,'style="width:150px;"'),
						'value_zone1' => inputText('form[shipcosts]['.$key.'][value_zone1]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value_zone1'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key, 1)),15,'style="width:150px;"'),
						'value_zone2' => inputText('form[shipcosts]['.$key.'][value_zone2]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value_zone2'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key, 2)),15,'style="width:150px;"'),
						'value_zone3' => inputText('form[shipcosts]['.$key.'][value_zone3]',(!empty($this->Request)? @$this->Request['form']['shipcosts'][$key]['value_zone3'] : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key, 3)),15,'style="width:150px;"'),
						'id' => inputHidden('form[shipcosts]['.$key.'][type][id]',$key),
						'name' => $value,
					);
				}
				if(!empty($this->LocalTarget[3]) && validate(intval($this->LocalTarget[3])) && $this->LocalTarget[2]=='Undo')
					$this->TempFillEls['undo'] = '(Undo v.'.$this->LocalTarget[3].')';
				$this->TempFillEls['product_options'] = '';
				foreach($this->products as $key => $value) $this->TempFillEls['product_options'].='<option value=\"'.$key.'\">'.addslashes(preg_replace("/\r?\n/", " ", $value)).'</option>';
				//$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'tree');
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'treeacc');
				$this->setCategories($this->PreparedTemplates);
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'save');
			break;
			}
			case 'Delete': {
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'open');
				$path = '/products/'.$this->PreparedTemplates['id'];
				$files = array('prew' => array(),'big' => array(),'image' => array(),'3d' => array(),'hi' => array(),'ss' => array(),'ug' => array(),'middle' => array());
				foreach($this->PreparedTemplates['files'] as $key => $value) {
					switch($value['filetype']) {
						case 1: $files['prew'] = array('image_link' => $value['image_link'],'name' => 'prew.'.$value['imagetype']); break;
						case 8: $files['middle'] = array('image_link' => $value['image_link'],'name' => 'middle.'.$value['imagetype']); break;
					}
				}
				$this->TempFillEls = array(
					'subaction' => 'Delete',
					'subprocess' => 'deleting',
					'step' => 'delete',
					'id' => $this->PreparedTemplates['id'],
					'edit_id' => '"'.$this->PreparedTemplates['name'].'" ['.$this->PreparedTemplates['id'].']',
					'is_active' => $this->PreparedTemplates['is_active']? 'Yes' : 'No',
					'callforquote' => $this->PreparedTemplates['callforquote']? 'Yes' : 'No',
					'pid' => $this->PreparedTemplates['pid'],
					'name' => $this->PreparedTemplates['name'],
					'header' => $this->PreparedTemplates['header'],
					'item' => $this->parentitems[$this->PreparedTemplates['item']['id']],
				//	'warranty' => $this->warranty[$this->PreparedTemplates['warranty']['id']],
					'full_desc' => $this->PreparedTemplates['full_desc'],
//					'export_restriction' => !empty($this->PreparedTemplates['export_restriction']['id'])? $this->export[$this->PreparedTemplates['export_restriction']['id']] : ' --- Empty --- ',
					'prew_image' => !empty($files['prew']['image_link'])? $this->makeImageLink('prew',$files['prew']['image_link']) : (empty($files['prew']['name'])? makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','prewimg') : makeImage($this->ImagePath.$path.'/'.$files['prew']['name'],$this->ImageWPath.$path.'/'.$files['prew']['name'],'','',array(),0,'','prewimg')),
					'prew_image_link' => empty($files['prew']['image_link'])? ' --- Empty --- ' : $this->products[$files['prew']['image_link']],
					'prew_display' => 'inline',
					'middle_image' => !empty($files['middle']['image_link'])? $this->makeImageLink('middle',$files['middle']['image_link']) : (empty($files['middle']['name'])? makeImage($this->ImagePath.'/1.gif',$this->ImageWPath.'/1.gif','','',array(),0,'','middleimg') : makeImage($this->ImagePath.$path.'/'.$files['middle']['name'],$this->ImageWPath.$path.'/'.$files['middle']['name'],'','',array(),0,'','middleimg')),
					'middle_image_link' => empty($files['middle']['image_link'])? ' --- Empty --- ' : $this->products[$files['middle']['image_link']],
					'middle_display' => 'inline',
				);
				$number_product = !empty($this->PreparedTemplates['applicableitems']) && count($this->PreparedTemplates['applicableitems']) > 3? count($this->PreparedTemplates['applicableitems']) : 3;
				for($i = 1; $i <= $number_product; $i++) {
					$this->TempFillEls['applicable_items'][$i] = array(
						'num' => $i,
						'item' => isset($this->PreparedTemplates['applicableitems'][$i-1])? $this->products[$this->PreparedTemplates['applicableitems'][$i-1]['product']['id']] : ' --- Empty --- ',
					);
				}
				$number_price = !empty($this->PreparedTemplates['prices']) && count($this->PreparedTemplates['prices']) > 1? count($this->PreparedTemplates['prices']) : 1;
				for($i = 1; $i <= $number_price; $i++) {
					$this->TempFillEls['prices'][$i] = array(
						'num' => $i,
						'name' => 'Price ',
						'value' => empty($this->PreparedTemplates['prices'][($i-1)]['value'])? ' --- Empty --- ' : $this->PreparedTemplates['prices'][($i-1)]['value'],
					);
				}
				/*foreach($this->shipcosts as $key => $value) {
					$this->TempFillEls['costs'][$key] = array(
						'value' => empty($this->PreparedTemplates['shipcosts'][$key-1]['value'])? ' --- Empty --- ' : $this->getShipCost($this->PreparedTemplates['shipcosts'],$key),
						'name' => $value,
					);
				}*/
				//$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'tree');
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID,'treeacc');
				$this->setCategories($this->PreparedTemplates,1,true);
				$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'delete');
			break;
			}
		}
		$this->TempFillEls['new_param_id_product'] = $number_product;
		!empty($this->PreparedTemplates['_error']) and $this->errors['model'] = $this->PreparedTemplates['_error'];
		if(count($this->errors) > 0) $this->errors['header'] = $this->globalLangContent['error_view']['header_error'];
		foreach($this->errors as $k => $v) $this->TempFillEls[$k.'_error'] = $k=='header'? $this->printError($v).'<p>' : $this->printError($v);
	}

	function setTargetArray($array = array()) {
		$btnEdit_vis = $this->server_type == 'dev' ? 'visible' : 'hidden';
		foreach($array as $value) {
/*			$path = $this->ImagePath.'/products/'.$value['id'];
			$handle = opendir($path); 
		    while (false !== ($entry = readdir($handle))) {
		    	if (strpos($entry, "-s.jpg")) rename($path.'/'.$entry, $path.'/prew.jpg'); //echo "$entry\n"; 
		    	if (strpos($entry, "-b.jpg")) rename($path.'/'.$entry, $path.'/middle.jpg'); //echo "$entry\n";
    		}*/
			$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['header'],
				'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'],
			);         
		}
	}

	function getShipCost($array,$type,$zone) {
		foreach($array as $value) {
			//if($value['type']['id']==$type) return $value['value'];
			if($value['type']['id']==$type) return $value['value_zone'.$zone];
		}
		return '';
	}

	function setCategories($array = array(),$level = 1,$del = FALSE,$levels = array()) {
//printArray($array);		    
		$i = 0;
		foreach($array as $value) {
			$levels[$level] = $i==sizeof($array)-1? 1 : 0;
			$this->TempFillEls['categories'][$value['id']] = array(
				'id' => $value['id'],
				'ownerid' => $value['ownerid'],
				'name' => $value['name'],
				'input' => !empty($value['subcat'])? '&nbsp;' : ($del? (in_array($value['id'],$this->categories)? '<b>+</b>' : '&nbsp;') : inputCheckbox('form[categorylineitems]['.$value['id'].'][id]',(!empty($this->Request)? @$this->Request['form']['categorylineitems'][$value['id']]['id'] : (in_array($value['id'],$this->categories)? 1 : 0)),' class=check',$value['id'])),
				'width' => intval($level*25),
				'width2' => 790 - 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['categories'][$value['id']]['cat_header'] = array(array(
					'id' => $value['id'],
					'ownerid' => $value['ownerid'],
					'name' => $value['name'],
					'input' => !empty($value['subcat'])? '&nbsp;' : ($del? (in_array($value['id'],$this->categories)? '<b>+</b>' : '&nbsp;') : inputCheckbox('form[categorylineitems]['.$value['id'].'][id]',(!empty($this->Request)? @$this->Request['form']['categorylineitems'][$value['id']]['id'] : (in_array($value['id'],$this->categories)? 1 : 0)),' class=check',$value['id'])),
					'width' => intval($level*25),
					'width2' => 790 - intval($level*25),
				));
				$this->TempFillEls['categories'][$value['id']]['cat_onclick'] = array(array('id' => $value['id']));
				$this->setCategories($value['subcat'],$level+1,$del,$levels);
			}
			else {
				if($level==1) $this->TempFillEls['categories'][$value['id']]['end_div'] = '</div>';
				//else $this->TempFillEls['categories'][$value['id']]['end_div'] = !empty($levels[$level])? (!empty($levels[$level-1])? (!empty($levels[$level-2])? (!empty($levels[$level-3])? str_repeat('</div>',4) : str_repeat('</div>',3)) : str_repeat('</div>',3)) : str_repeat('</div>',2)) : '</div>';
				else $this->TempFillEls['categories'][$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++;
		}
	}
}
?>