	function Sorter (isPost, SORT_FIELD, SORT_ORDER, page) {
		var myform = document.createElement('FORM');
		var sort_order = (SORT_ORDER != null && SORT_ORDER != '') ? SORT_ORDER : 'DESC'
		myform.name='sorter';
		myform.id='sorter';
		if (isPost) myform.method='post';
		myform.style.display ="none";
		document.body.appendChild(myform);
		var pin = addFieldToForm(myform,'SORT_FIELD')
		pin.value = SORT_FIELD;
		pin = addFieldToForm(myform,'SORT_ORDER')
		pin.value = sort_order;
		pin = addFieldToForm(myform,'page')
		pin.value = page != null ? page : 1 ;
		this.f  = myform;
		this.addField = addField;
		this.addFieldWithValue = addFieldWithValue;
		this.sortByIndex = sortByIndex;
		this.goToPage = goToPage;
		this.clearSorting = clearSorting;
		this.getSortIndex = getSortIndex;
		this.getSortOrder = getSortOrder;
		this.writeSortOrder = writeSortOrder;
		this.setFieldByIndex = setFieldByIndex;
		this.getFieldByIndex = getFieldByIndex;
		this.setFieldByName = setFieldByName;
		this.getFieldByName = getFieldByName;
		return this;
	}
	
	function addFieldToForm(f, name) {
		var pin = document.createElement('INPUT');
		pin.type = "hidden";
		pin.name = name;
		pin.id = pin.name;
		pin.setAttribute("name",pin.name);
		f.appendChild(pin);
		return pin;
	}
	
	function addField(name){
		return addFieldToForm(this.f,name);
	}

	function addFieldWithValue(name, value){
		var pin = addFieldToForm(this.f,name);
		pin.value = value;
		return pin;
	}


	function setFieldByIndex(iIndex, value) {
		if (this.f[iIndex] != null) {
			this.f[iIndex].value = value
			return true;
		} else {
			return false;
		}
	}

	function getFieldByIndex(iIndex) {
		var result = null;
		if (this.f[iIndex] != null) {
			result = this.f[iIndex].value;
		}
		return result;
	}

	function setFieldByName(name, value) {
		var field = document.getElementById(name);

		if (field != null) {
			field.value = value;
			return true;
		} else {
			return false;
		}
	}

	function getFieldByName(name) {
		var field = document.getElementById(name);
		var result = null;
		if (field != null) {
			result = field.value;
		}
		return result;
	}

	
	function goToPage(page) {
		this.f[2].value = page
		this.f.submit();
	}
	
	function sortByIndex( iIndex ) {
		if (iIndex != this.f[0].value) {
			this.f[1].value = 'DESC'
		} else  {
			this.f[1].value = (this.getSortOrder() == 'DESC') ? 'ASC' : 'DESC';
		}
		this.f[0].value = iIndex
		this.f[2].value = 1
		this.f.submit();
	}
	
	function getSortIndex() {
		return this.f[0].value;
	}

	function getSortOrder() {
		return this.f[1].value;
	}

	function writeSortOrder(iIndex){
		if (iIndex == this.getSortIndex()) {
			var str = this.getSortOrder() == 'DESC' ? '&nbsp;<img src="/admin/images/sort-d.gif" width="9" height="9" alt="DESC"/>' : '&nbsp;<img src="/admin/images/sort-u.gif" alt="ASC"/>';
			document.write(str)
		}
	}

	function clearSorting() {
		this.f[1].value = ''
		this.f[1].value = 'DESC'
		this.f[2].value = 1
		this.f.submit();
	}

