/**
* Constructor
* @param name string
*/
Data = function (name) {
	this.name = name.toString();
	this.form = document.getElementById(this.name + '_form');
	this.sortColumn = document.getElementById(this.name + '_sortingColumn');
	this.sortOrder = document.getElementById(this.name + '_sortingOrder');
	this.alphas = new AlphaDataFilter(this);
	this.paging = new DataPaging(this);
	this.selection = new DataSelection(this);
}

/**
* Sorts grid by column
* @param column string
* @param order string 'a' or 'd'
*/
Data.prototype.sort = function (column, order) {
	if (this.form && this.sortColumn && this.sortOrder) {
		this.sortColumn.value = column.toString();
		this.sortOrder.value = order.toString();
		this.form.submit();
	}
	if (window.event) {
		window.event.cancelBubble = true;
	}
	return false;
}

AlphaDataFilter = function (grid) {
	this.grid = grid;
	this.filterChar = document.getElementById(this.grid.name + '_filterChar');
}

AlphaDataFilter.prototype.filter = function (alpha) {
	if (this.grid.form && this.filterChar) {
		this.filterChar.value = alpha.toString();
		if (this.grid.paging.canGo) {
			this.grid.paging.go(0);
		} else {
			this.grid.form.submit();
		}
	}
	if (window.event) {
		window.event.cancelBubble = true;
	}
}

DataPaging = function (grid) {
	this.grid = grid;
	this.currentPage = document.getElementById(this.grid.name + '_currentPage');
	this.canGo = this.currentPage !== undefined;
	this.countPerPage = document.getElementById(this.grid.name + '_countPerPage');
	this.canCount = this.countPerPage !== undefined;
}

DataPaging.prototype.go = function (page) {
	if (this.grid.form && this.currentPage) {
		this.currentPage.value = page.toString();
		this.grid.form.submit();
	}
	if (window.event) {
		window.event.cancelBubble = true;
	}
}

DataPaging.prototype.count = function (count) {
	if (this.grid.form && this.countPerPage) {
		this.countPerPage.value = count.toString();
		this.grid.form.submit();
	}
	if (window.event) {
		window.event.cancelBubble = true;
	}
}



Data.backupRowStyle = '';

Data.setRowStyle = function (rowId, rowStyle) {
	row = document.getElementById(rowId);
	if (! row) {
		return false;
	}
	if (! rowStyle || ! isset(row.style)) {
		return false;
	}
	Data.backupRowStyle = row.className;
	row.className += ' ' + rowStyle;

	var rRow = new RegExp('_row$', 'i'), rDsc = new RegExp('_description$', 'i'), otherId;
	if (rowId.match(rRow)) {
		otherId = rowId.replace(rRow, '_description');
	} else if (rowId.match(rDsc)) {
		otherId = rowId.replace(rDsc, '_row');
	}

	row = document.getElementById(otherId);
	if (! row) {
		return false;
	}
	if (! rowStyle || ! isset(row.style)) {
		return false;
	}
	backupDescStyle = row.className;
	row.className += ' ' + rowStyle;

	return true;
}

Data.backRowStyle = function (rowId) {
	row = document.getElementById(rowId);
	if (! row) {
		return false;
	}
	row.className = Data.backupRowStyle;

	var rRow = new RegExp('_row$', 'i'), rDsc = new RegExp('_description$', 'i'), otherId;
	if (rowId.match(rRow)) {
		otherId = rowId.replace(rRow, '_description');
	} else if (rowId.match(rDsc)) {
		otherId = rowId.replace(rDsc, '_row');
	}

	row = document.getElementById(otherId);
	if (! row) {
		return false;
	}
	row.className = backupDescStyle;

	return true;
}

Data.toggleAllChilds = function (name, count) {
	var show = false, img = document.getElementById(name + '_childsImage');
	if (img) {
		rx = new RegExp('plus', 'i');
		show = img.src.match(rx) != null;
		if (show) {
			rx = new RegExp('plus', 'i');
			img.src = img.src.replace(rx, 'minus');
		} else {
			rx = new RegExp('minus', 'i');
			img.src = img.src.replace(rx, 'plus');
		}
	}
	var row = null;
	for (var i = 0; i < count; i++) {
		row = document.getElementById(name + '_' + i + '_child');
		img = document.getElementById(name + '_' + i + '_childImage');
		if (row && row.style && img) {
			if (show) {
				try {
					row.style.display = 'table-row';
				} catch (e) {
					row.style.display = 'block';
				}
				rx = new RegExp('plus', 'i');
				img.src = img.src.replace(rx, 'minus');
			} else {
				row.style.display = 'none';
				rx = new RegExp('minus', 'i');
				img.src = img.src.replace(rx, 'plus');
			}
		}
	}
}

Data.prototype.toggleAllDescriptions = function (rows) {
	var img = document.getElementById(this.name + '.descriptionsImage');
	var show = false;

	if (img) {
		show = img.src.match('plus') != null;
		if (show) {
			img.src = img.src.replace('plus', 'minus');
		} else {
			img.src = img.src.replace('minus', 'plus');
		}
	}

	for (var i = 0; i < rows; i++) {
		var row = document.getElementById(this.name + '.' + i + '.description');
		if (! row) {
			continue;
		}
		var img = document.getElementById(row.id + 'Image');
		if (! img) {
			continue;
		}
		if (show) {
			try {
				row.style.display = 'table-row';
			} catch (e) {
				row.style.display = 'block';
			}
			img.src = img.src.replace('plus', 'minus');
		} else {
			row.style.display = 'none';
			img.src = img.src.replace('minus', 'plus');
		}
	}
}

Data.prototype.toggleDescription = function (id) {
	var img = document.getElementById(id + '.descriptionImage');
	if (! img) {
		return;
	}
	var row = document.getElementById(id + '.description');
	if (! row || ! row.style) {
		return;
	}
	if (row.style.display == 'none' || row.currentStyle.display == 'none') {
		try {
			row.style.display = 'table-row';
		} catch (e) {
			row.style.display = 'block';
		}
		img.src = img.src.replace('plus', 'minus');
	} else {
		row.style.display = 'none';
		img.src = img.src.replace('minus', 'plus');
	}
}

Data.toggleChild = function (name) {
	var row = document.getElementById(name + '_child');
	var img = document.getElementById(name + '_childImage');
	if (row && row.style && img) {
		if (row.style.display == 'none') {
			try {
				row.style.display = 'table-row';
			} catch (e) {
				row.style.display = 'block';
			}
			rx = new RegExp('plus', 'i');
			img.src = img.src.replace(rx, 'minus');
		} else {
			row.style.display = 'none';
			rx = new RegExp('minus', 'i');
			img.src = img.src.replace(rx, 'plus');
		}
	}
}

Data.toggleAllGroups = function (name, count) {
	var show = false, img = document.getElementById(name + '_groupsImage');
	if (img) {
		rx = new RegExp('plus', 'i');
		show = img.src.match(rx) != null;
		if (show) {
			rx = new RegExp('plus', 'i');
			img.src = img.src.replace(rx, 'minus');
		} else {
			rx = new RegExp('minus', 'i');
			img.src = img.src.replace(rx, 'plus');
		}
	}
	var row = null;
	for (var i = 0; i < count; i++) {
		row = document.getElementById(name + '_' + i + '_group');
		img = document.getElementById(name + '_' + i + '_groupImage');
		if (row && row.style && img) {
			if (show) {
				try {
					row.style.display = 'table-row';
				} catch (e) {
					row.style.display = 'block';
				}
				rx = new RegExp('plus', 'i');
				img.src = img.src.replace(rx, 'minus');
			} else {
				row.style.display = 'none';
				rx = new RegExp('minus', 'i');
				img.src = img.src.replace(rx, 'plus');
			}
		}
	}
}

Data.toggleGroup = function (name) {
	var row = document.getElementById(name + '_group');
	var img = document.getElementById(name + '_groupImage');
	if (row && row.style && img) {
		if (row.style.display == 'none') {
			try {
				row.style.display = 'table-row';
			} catch (e) {
				row.style.display = 'block';
			}
			rx = new RegExp('plus', 'i');
			img.src = img.src.replace(rx, 'minus');
		} else {
			row.style.display = 'none';
			rx = new RegExp('minus', 'i');
			img.src = img.src.replace(rx, 'plus');
		}
	}
}
