<inp2:adm_SetPopupSize width="1000" height="680"/>

<inp2:m_include t="incs/header"/>
<inp2:m_RenderElement name="combined_header" prefix="p" section="in-portal:browse" pagination="1" pagination_prefix="g" grid="Radio" title_preset="pricing" tab_preset="Default"/>

<!-- ToolBar -->
<table class="toolbar" height="30" cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
	<tr>
  	<td>
	  	<script type="text/javascript" src='js/catalog.js'></script>

  		<script type="text/javascript">
  				Request.progressText = '<inp2:m_phrase name="la_title_Loading" escape="1"/>';

				var defaultGroup = <inp2:conf_ConfigValue name="User_LoggedInGroup"/>;

				Catalog.prototype.AfterInit = function () {
					Grids['g'].SelectDefault(defaultGroup);
				}

				Catalog.prototype.go_to_group = function($group_id) {
					if (!isset($group_id)) {
						$group_id = 0; // gets current group
					}
					else {
						set_hidden_field('current_group_id', $group_id);
					}
					this.refreshTab('pr.tang', 'pr.tang_div');
				}

  				Catalog.prototype.refreshTab = function($prefix, $div_id, $force) {
					var $group_id = get_hidden_field('current_group_id');
					var $tab_group_id = document.getElementById($div_id).getAttribute('group_id');
					if ($group_id != $tab_group_id || $force) {
						// query tab content only in case if not queried or category don't match
						var $url = this.URLMask.replace('#ITEM_PREFIX#', $prefix).replace('#GROUP_ID#', $group_id);
						this.BusyRequest[$prefix] = false;
						Request.makeRequest($url, this.BusyRequest[$prefix], $div_id, this.successCallback, this.errorCallback, $div_id, this);
					}
				}

				// adds information about tab to tab_registry
				Catalog.prototype.registerTab = function($tab_id) {
					var $tab = document.getElementById($tab_id + '_div');
					var $index = this.TabRegistry.length;
					this.TabRegistry[$index] = new Array();
					this.TabRegistry[$index]['tab_id'] = $tab_id;
					this.TabRegistry[$index]['prefix'] = $tab.getAttribute('prefix');
					this.TabRegistry[$index]['dep_buttons'] = new Array();
					this.TabRegistry[$index]['index'] = $index;
				}

				Catalog.prototype.displaySearch = function ($prefix) {

				}

				Catalog.prototype.submit_event = function($prefix_special, $event, $t, $return_result) {
					var $prev_template = get_hidden_field('t');
					if (isset($event)) set_hidden_field('events[' + $prefix_special + ']', $event);
					if (isset($t)) set_hidden_field('t', $t);

					var $tab_id = this.queryTabRegistry('prefix', this.ActivePrefix, 'tab_id');
					if ($return_result)
						this.submit_kernel_form($tab_id);
					else
						this.submit_kernel_form();

					set_hidden_field('t', $prev_template);
				}

	  			var $BracketManager = new Catalog('<inp2:m_Link template="in-commerce/products/products_pricing_grid" item_prefix="#ITEM_PREFIX#" group_id="#GROUP_ID#" no_amp="1" pass="m,p,pr.tang"/>', 'permmanager_', 'ProductPriceBracketManager');

		</script>

  		<script type="text/javascript">
				a_toolbar = new ToolBar();

				a_toolbar.AddButton( new ToolBarButton('select', '<inp2:m_phrase label="la_ToolTip_Save" escape="1"/>', function() {
							submit_event('p','<inp2:p_SaveEvent/>');
						}
					) );
				a_toolbar.AddButton( new ToolBarButton('cancel', '<inp2:m_phrase label="la_ToolTip_Cancel" escape="1"/>', function() {
							submit_event('p','OnCancelEdit');
						}
				 ) );

				a_toolbar.AddButton( new ToolBarSeparator('sep1') );

				a_toolbar.AddButton( new ToolBarButton('prev', '<inp2:m_phrase label="la_ToolTip_Prev" escape="1"/>', function() {
							go_to_id('p', '<inp2:p_PrevId/>');
						}
				 ) );
				a_toolbar.AddButton( new ToolBarButton('next', '<inp2:m_phrase label="la_ToolTip_Next" escape="1"/>', function() {
							go_to_id('p', '<inp2:p_NextId/>');
						}
				 ) );

				a_toolbar.AddButton( new ToolBarSeparator('sep2') );

				function add_brackets()
	  			{
	  				//submit_event('pr.tang', 'OnMoreBrackets')
	  				set_hidden_field('group_id', get_hidden_field('current_group_id'));
	  				set_hidden_field('item_prefix', $BracketManager.ActivePrefix);
	  				$BracketManager.submit_event('pr.tang', 'OnMoreBrackets', 'in-commerce/products/products_pricing_grid', true);

	  			}
				a_toolbar.AddButton( new ToolBarButton('in-commerce:more_brackets', '<inp2:m_phrase label="la_ToolTip_MoreBrackets" escape="1"/>', add_brackets) );

				function infinity()
	  			{
					//submit_event('pr.tang', 'OnInfinity');
					set_hidden_field('group_id', get_hidden_field('current_group_id'));
					set_hidden_field('item_prefix', $BracketManager.ActivePrefix);
					$BracketManager.submit_event('pr.tang', 'OnInfinity', 'in-commerce/products/products_pricing_grid', true);
	  			}
				a_toolbar.AddButton( new ToolBarButton('in-commerce:infinity', '<inp2:m_phrase label="la_ToolTip_Infinity" escape="1"/>', infinity) );

				function arrange_brackets()
	  			{
					//submit_event('pr.tang', 'OnArrange')
					set_hidden_field('group_id', get_hidden_field('current_group_id'));
					set_hidden_field('item_prefix', $BracketManager.ActivePrefix);
					$BracketManager.submit_event('pr.tang', 'OnArrange', 'in-commerce/products/products_pricing_grid', true);
					//$BracketManager.refreshTab('pr.tang', 'pr.tang_div', true);
	  			}
				a_toolbar.AddButton( new ToolBarButton('in-commerce:arrange', '<inp2:m_phrase label="la_ToolTip_Arrange" escape="1"/>', arrange_brackets) );


				function edit(){ }

				a_toolbar.Render();

				<inp2:m_if check="p_IsSingle" >
					a_toolbar.HideButton('prev');
					a_toolbar.HideButton('next');
					a_toolbar.HideButton('sep1');
					//a_toolbar.HideButton('sep2');
				<inp2:m_else/>
					<inp2:m_if check="p_IsLast" >
						a_toolbar.DisableButton('next');
					</inp2:m_if>
					<inp2:m_if check="p_IsFirst" >
						a_toolbar.DisableButton('prev');
					</inp2:m_if>
				</inp2:m_if>
			</script>
		</td>

		<inp2:m_RenderElement name="search_main_toolbar" prefix="g" grid="Radio"/>
	</tr>
</tbody>
</table>

<script type="text/javascript">
	function set_start(id, next_id)
	{
		//var next_id = id - 1;
		var input_id = ('pr.tang[' + next_id) + '][MinQty]';
		var orig_id = ('pr.tang[' + id) + '][MinQty]';
		if(document.getElementById(input_id) != null)
		{
			var new_value = parseInt(document.getElementById('pr.tang[' + id + '][MaxQty]').value);
			if( isNaN(new_value) ) new_value = 0;
			document.getElementById('pr.tang[' + id + '][MaxQty]').value = new_value;
			document.getElementById(input_id).value = new_value;
		}
	}

	function enableKernelFormFields()
	{
		var kForm = document.forms.kernel_form;
		var elem = false;
		for (var i=0; i<kForm.elements.length; i++){
			elem = kForm.elements[i];
			if (elem.type == "text" && elem.disabled == true){
				elem.disabled = false;
			}
		}
	}
</script>

<inp2:pr.tang_SaveWarning name="grid_save_warning"/>

<inp2:m_RenderElement name="grid" PrefixSpecial="g" IdField="GroupId" per_page="-1" grid="Radio" header_block="grid_column_title_no_sorting" grid_height="220" grid_status="0"/>

<inp2:m_include t="in-commerce/products/products_pricing_grid" tab_init="1" item_prefix="pr.tang"/>

<inp2:m_include t="incs/footer"/>

<script type="text/javascript">
	Grids['g'].OnSelect = function ($id) {
		set_hidden_field('group_id', $id);
		$BracketManager.go_to_group($id);

	}

	Grids['g'].OnUnSelect = function ($id) {
		set_hidden_field('group_id', $id);
		set_hidden_field('item_prefix', $BracketManager.ActivePrefix);
		$BracketManager.submit_event('pr.tang', 'OnArrange', 'in-commerce/products/products_pricing_grid', false);

	}

	Grids['g'].SelectDefault = function (group_id) {
		for (var $i in this.Items) {
			if (this.Items[$i].ItemId == group_id){
				this.Items[$i].Select();
				break;
			}
		}
	}

	$(document).ready(
		function() {
			$BracketManager.Init(false);
		}
	);

	document.forms.kernel_form.onsubmit =  enableKernelFormFields;
</script>