String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function update_checkbox(cb, cb_hidden) {
	cb_hidden.value = cb.checked ? 1 : 0;
}

function redirect($url) {
	window.location.href = $url;
}

function open_window($url, $window_name, $width, $height) {
	window.open($url, $window_name, 'width='+$width+',height='+$height+',resizable=yes');
	return false;
}

function addLoadEvent(func, wnd) {
	if (!wnd) wnd = window
	var oldonload = wnd.onload;
	if (typeof wnd.onload != 'function') {
		wnd.onload = func;
	} else {
		wnd.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

// ItemCategories class
function ItemCategories($table_id, $field_id, $primary_category, $phrases) {
	this.CategoryTable = document.getElementById($table_id);
	this.CategorySelector = document.getElementById($field_id + '_select');
	this.MoreCategoriesField = document.getElementById($field_id);
	this.PrimaryCategory = $primary_category;
	this.Phrases = $phrases;

	// get additional categories from item
	if (this.MoreCategoriesField.value.length) {
		this.MoreCategories = this.MoreCategoriesField.value;
		this.MoreCategories = this.MoreCategories.substring(1, this.MoreCategories.length - 1).split('|');
	}
	else {
		this.MoreCategories = new Array ();
	}
}

ItemCategories.prototype.AddCategory = function($separator, $delete_button, $max_categories) {
	var $category_id = this.CategorySelector.options[this.CategorySelector.selectedIndex].value;
	var $category_name = this.CategorySelector.options[this.CategorySelector.selectedIndex].innerHTML.trim();

	if ((this.SearchCategory($category_id) !== false) || ($category_id == this.PrimaryCategory) || ($category_id == 0)) {
		// don't add same category twice & don't allow to add item's primary category		
		alert(this.Phrases[1]);
		return ;
	}
	
	if ((this.MoreCategories.length + 2) > $max_categories)
	{
		// don't add more category - $max_categories limit
		alert(this.Phrases[0]);
		return ;
	}

	// strip trailing HTML spaces & separator
	var $separator_pos = $category_name.indexOf($separator);
	if ($separator_pos != -1) {
		$category_name = $category_name.substring($separator_pos + $separator.length);
	}


	var $row = this.CategoryTable.insertRow(-1);
	$row.id = 'category_' + $category_id;

	var $cell = $row.insertCell(-1);
	$cell.innerHTML = $category_name;

	$cell = $row.insertCell(-1);
	$cell.innerHTML = $delete_button.replace(/#CATEGORY_ID#/g, $category_id);

	this.MoreCategories.push($category_id);
	this.updateMoreCategoriesField();
}

ItemCategories.prototype.SearchCategory = function($category_id) {
	var $i = 0;
	while ($i < this.CategoryTable.rows.length) {
		if (this.CategoryTable.rows[$i].id == 'category_' + $category_id) {
			return $i;
		}
		$i++;
	}

	return false;
}

ItemCategories.prototype.DeleteCategory = function($category_id) {
	var $row_index = this.SearchCategory($category_id);

	if ($row_index !== false) {
		this.CategoryTable.deleteRow($row_index);
		var $i = 0;
		while ($i < this.MoreCategories.length) {
			if (this.MoreCategories[$i] == $category_id) {
				this.MoreCategories.splice($i, 1);
				break;
			}
			$i++;
		}
		this.updateMoreCategoriesField();
	}
}

ItemCategories.prototype.updateMoreCategoriesField = function() {
	this.MoreCategoriesField.value = this.MoreCategories.length ? '|' + this.MoreCategories.join('|') + '|' : '';
}