function my_round(val, dec) {
	var parm = Math.pow(10, Math.round(dec));
	var ret_val = Math.round(val*parm)/parm;
	return ret_val;
}

function grid(cols, rows, name, style, cellWidths, cellAligns, colCaptions, colDefVals, multCells, resMultCell, trnx_no, userOnBlurFnc) {
	//properties declaration
	this.cols = cols;
	this.rows = rows;
	this.name = name;
	this.style = style;
	this.cellWidths = cellWidths;
	this.cellAligns = cellAligns;
	this.colCaptions = colCaptions;
	this.colDefVals = colDefVals;
	this.currCol = 0;
	this.currRow = 0;
	this.multCells = multCells;
	this.multCellsBinary = 0;
	for (i = 0; i < this.multCells.length; i++) {
		this.multCellsBinary = this.multCellsBinary + Math.pow(2, this.cols - this.multCells[i] - 1);
	}
	this.resMultCell = resMultCell;
	this.trnx_no = trnx_no;
	
	//Initialize NumerFormat class as property of this object
	//this.formator = new NumberFormat();
	
	//methods declaration
	this.draw = drawGrid;
	this.insertRow = insertRow;
	this.deleteRow = deleteRow;
	this.updateCoordinates = updateCoordinates;
	this.multiplyCells = multiplyCells;
	this.sumCells = sumCells;
	this.footer = footer;
	this.compareSumWithTrnxAmnt = compareSumWithTrnxAmnt;
	
/*	if (userOnBlurFnc != undefined) {
		this.userOnBlurFnc = userOnBlurFnc;
	} else {
		alert('userOnBlurFnc is not defined');
	}*/
}

function drawGrid() {
	var onBlurStr = '';
	var onChangeStr = '';
	var html = "<TABLE cellpadding='0' cellspacing=2 border='0'>\n";
	html += "<TR>\n";
	html += "<TD valign='top'>\n";
	html += "<DIV id='" + this.name + "' style='" + this.style + "'>\n";
	html += "	<TABLE class='grdTable' cellpadding='0' cellspacing='0'>\n";
	html += "<TR class='colCaptions'>";
	for (i = 0; i < this.colCaptions.length; i++) {
		html += "	<TD class='colCaptions' style='text-align: " + this.cellAligns[i] +"'>";
		html += this.colCaptions[i];
		html += "	</TD>";
	}
	html += "</TR>";
	for (i = 0; i < this.rows; i++) {
		if (i%2 == 0) {
			html += "		<TR class='grdRowLight'>\n";
		} else {
			html += "		<TR class='grdRowDark'>\n";
		}
		for (j = 0; j < this.cols; j++) {
			if ((this.multCellsBinary & (Math.pow(2, this.cols - j - 1))) != 0) {
				onBlurStr = " onblur='" + this.name + ".multiplyCells(); " + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
			} else {
				onBlurStr = " onblur='" + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
			}
			if (j == this.resMultCell) {
				onChangeStr = " onchange='" + this.name + ".sumCells();'";
			} else {
				onChangeStr = '';
			}
			html += "			<TD style='width: " + this.cellWidths[j] + "px;'>\n";
			html += "				<INPUT type='text' name='" + this.name + '_' + j + '_' + i + "' id='" + this.name + '_' + j + '_' + i + "' style='width: 96%; text-align: " + this.cellAligns[j] + "' value='" + this.colDefVals[j] + "' onfocus='" + this.name + ".updateCoordinates(" + j + ", " + i + ");'" + onBlurStr + " " + onChangeStr + " />\n";
			html += "			</TD>\n";
		}
		html += "		</TR>\n";
	}
	html += this.footer();
	html += "	</TABLE>\n";
	html += "</DIV>\n";
	html += "</TD>\n";
	html += "<TD valign='top'>\n";
	html += "	<BUTTON type='button' name='btnInsertGridRow' id='btnInsertGridRow' onclick='" + this.name + ".insertRow();' class='gridToolButton' title='Добавяне на нов ред'>+</BUTTON><BR>\n";
	html += "	<BUTTON type='button' name='btnDelGridRow' id='btnDelGridRow' onclick='" + this.name + ".deleteRow();' class='gridToolButton' title='Изтриване на ред'>-</BUTTON>\n";
	html += "</TD>\n";
	html += "</TR>\n";
	html += "</TABLE>\n";
	document.write(html);
/*	var cell_name = this.name + '_0_0';
	var cell = document.getElementById(cell_name);
	cell.focus();*/
}

function insertRow() {
	var onBlurStr = '';
	var html = "	<TABLE class='grdTable' cellpadding='0' cellspacing='0'>\n";
	html += "<TR class='colCaptions'>";
	for (i = 0; i < this.colCaptions.length; i++) {
		html += "	<TD class='colCaptions' style='text-align: " + this.cellAligns[i] +"'>";
		html += this.colCaptions[i];
		html += "	</TD>";
	}
	html += "</TR>";
	var row = 0;
	//Preserve rows to the selected one (including it)
	for (i = 0; i <= this.currRow; i++) {
		if (row % 2 == 0) {
			html += "		<TR class='grdRowLight'>\n";
		} else {
			html += "		<TR class='grdRowDark'>\n";
		}
		for (j = 0; j < this.cols; j++) {
			if ((this.multCellsBinary & (Math.pow(2, this.cols - j - 1))) != 0) {
				onBlurStr = " onblur='" + this.name + ".multiplyCells(); " + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
			} else {
				onBlurStr = " onblur='" + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
			}
			if (j == this.resMultCell) {
				onChangeStr = " onchange='" + this.name + ".sumCells();'";
			} else {
				onChangeStr = '';
			}
			html += "			<TD style='width: " + this.cellWidths[j] + "px;'>\n";
			var cellVal = document.getElementById(this.name + '_' + j + '_' + i).value;
			html += "				<INPUT type='text' name='" + this.name + '_' + j + '_' + row + "' id='" + this.name + '_' + j + '_' + row + "' style='width: 98%; text-align: " + this.cellAligns[j] + "' onfocus='" + this.name + ".updateCoordinates(" + j + ", " + row + ");' value='" + cellVal + "'" + onBlurStr + " />\n";
			html += "			</TD>\n";
		}
		html += "		</TR>\n";
		row ++;
	}
	//Add the new row
	if (row % 2 == 0) {
		html += "		<TR class='grdRowLight'>\n";
	} else {
		html += "		<TR class='grdRowDark'>\n";
	}
	for (j = 0; j < this.cols; j++) {
		if ((this.multCellsBinary & (Math.pow(2, this.cols - j - 1))) != 0) {
			onBlurStr = " onblur='" + this.name + ".multiplyCells(); " + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
		} else {
			onBlurStr = " onblur='" + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
		}
		if (j == this.resMultCell) {
			onChangeStr = " onchange='" + this.name + ".sumCells();'";
		} else {
			onChangeStr = '';
		}
		html += "			<TD style='width: " + this.cellWidths[j] + "px;'>\n";
		html += "				<INPUT type='text' name='" + this.name + '_' + j + '_' + row + "' id='" + this.name + '_' + j + '_' + row + "' style='width: 98%; text-align: " + this.cellAligns[j] + "' value='" + this.colDefVals[j] + "' onfocus='" + this.name + ".updateCoordinates(" + j + ", " + row + ");'" + onBlurStr + " />\n";
		html += "			</TD>\n";
	}
	html += "		</TR>\n";
	row ++;
	//Put the remaining rows
	for (i = this.currRow + 1; i < this.rows; i++) {
		if (row % 2 == 0) {
			html += "		<TR class='grdRowLight'>\n";
		} else {
			html += "		<TR class='grdRowDark'>\n";
		}
		for (j = 0; j < this.cols; j++) {
			if ((this.multCellsBinary & (Math.pow(2, this.cols - j - 1))) != 0) {
				onBlurStr = " onblur='" + this.name + ".multiplyCells(); " + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
			} else {
				onBlurStr = " onblur='" + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
			}
			if (j == this.resMultCell) {
				onChangeStr = " onchange='" + this.name + ".sumCells();'";
			} else {
				onChangeStr = '';
			}
			html += "			<TD style='width: " + this.cellWidths[j] + "px;'>\n";
			var cellVal = document.getElementById(this.name + '_' + j + '_' + i).value;
			html += "				<INPUT type='text' name='" + this.name + '_' + j + '_' + row + "' id='" + this.name + '_' + j + '_' + row + "' style='width: 98%; text-align: " + this.cellAligns[j] + "' onfocus='" + this.name + ".updateCoordinates(" + j + ", " + row + ");' value='" + cellVal + "'" + onBlurStr + " />\n";
			html += "			</TD>\n";
		}
		html += "		</TR>\n";
		row ++;
	}
	html += this.footer();
	html += "</TABLE>\n";
	this.rows = row;
	var cell = document.getElementById(this.name + '_' + this.currCol + '_' + this.currRow);
	var container = document.getElementById(this.name);
	container.innerHTML = html;
	cell.focus();

}

function deleteRow() {
	var onBlurStr = '';
	if (this.rows == 1) {
		alert("Таблицата трябва да съдържа поне 1 ред!");
		return false;
	}
	var res = confirm('Моля потърдете изтриването на ред ' + (this.currRow + 1));
	if (res) {
		var html = "	<TABLE class='grdTable' cellpadding='0' cellspacing='0'>\n";
		html += "<TR class='colCaptions'>";
		for (i = 0; i < this.colCaptions.length; i++) {
			html += "	<TD class='colCaptions' style='text-align: " + this.cellAligns[i] +"'>";
			html += this.colCaptions[i];
			html += "	</TD>";
		}
		html += "</TR>";
		var row = 0;
		for (i = 0; i < this.currRow; i++) {
			if (row % 2 == 0) {
				html += "		<TR class='grdRowLight'>\n";
			} else {
				html += "		<TR class='grdRowDark'>\n";
			}
			for (j = 0; j < this.cols; j++) {
				if ((this.multCellsBinary & (Math.pow(2, this.cols - j - 1))) != 0) {
					onBlurStr = " onblur='" + this.name + ".multiplyCells(); " + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
				} else {
					onBlurStr = " onblur='" + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
				}
				if (j == this.resMultCell) {
					onChangeStr = " onchange='" + this.name + ".sumCells();'";
				} else {
					onChangeStr = '';
				}
				html += "			<TD style='width: " + this.cellWidths[j] + "px;'>\n";
				var cellVal = document.getElementById(this.name + '_' + j + '_' + i).value;
				html += "				<INPUT type='text' name='" + this.name + '_' + j + '_' + row + "' id='" + this.name + '_' + j + '_' + row + "' style='width: 98%; text-align: " + this.cellAligns[j] + "' onfocus='" + this.name + ".updateCoordinates(" + j + ", " + row + ");' value='" + cellVal + "'" + onBlurStr + " />\n";
				html += "			</TD>\n";
			}
			html += "		</TR>\n";
			row ++;
		}
		for (i = this.currRow + 1; i < this.rows; i++) {
			if (row % 2 == 0) {
				html += "		<TR class='grdRowLight'>\n";
			} else {
				html += "		<TR class='grdRowDark'>\n";
			}
			for (j = 0; j < this.cols; j++) {
				if ((this.multCellsBinary & (Math.pow(2, this.cols - j - 1))) != 0) {
					onBlurStr = " onblur='" + this.name + ".multiplyCells(); " + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
				} else {
					onBlurStr = " onblur='" + this.name + ".sumCells(); " + this.name + ".compareSumWithTrnxAmnt(); userOnBlurFnc();' ";
				}
				if (j == this.resMultCell) {
					onChangeStr = " onchange='" + this.name + ".sumCells();'";
				} else {
					onChangeStr = '';
				}
				html += "			<TD style='width: " + this.cellWidths[j] + "px;'>\n";
				var cellVal = document.getElementById(this.name + '_' + j + '_' + i).value;
				html += "				<INPUT type='text' name='" + this.name + '_' + j + '_' + row + "' id='" + this.name + '_' + j + '_' + row + "' style='width: 98%; text-align: " + this.cellAligns[j] + "' onfocus='" + this.name + ".updateCoordinates(" + j + ", " + row + ");' value='" + cellVal + "'" + onBlurStr + " />\n";
				html += "			</TD>\n";
			}
			html += "		</TR>\n";
			row ++;
		}
		html += this.footer();
		html += "</TABLE>\n";
		this.rows = row;
		if (this.currRow >= this.rows) {
			this.currRow = this.rows - 1;
		}
		var cell = document.getElementById(this.name + '_' + this.currCol + '_' + this.currRow);
		var container = document.getElementById(this.name);
		container.innerHTML = html;
		this.sumCells();
		this.compareSumWithTrnxAmnt();
		cell.focus();
		//alert('Matrix is ' + this.cols + 'x' + this.rows + "\nPosition is " + this.currCol + 'x' + this.currRow);
	}
}

function updateCoordinates(col, row) {
	this.currCol = col;
	this.currRow = row;
}

function multiplyCells(srcCells, resCell) {
	var res = 1;
	var fld = '';
	for (i = 0; i < this.multCells.length; i++) {
		name = this.name + '_' + this.multCells[i] + '_' + this.currRow;
		fld = document.getElementById(name);
		fld.value = my_round(fld.value, 2);
		res = my_round(res * fld.value, 2);
	}
	fld = document.getElementById(this.name + '_' + this.resMultCell + '_' + this.currRow)
	res = my_round(res, 2);
	fld.value = parseFloat(res).toFixed(2);
}

function sumCells() {
	var res = 0;
	var fld = '';
	for (i = 0; i < this.rows; i++) {
		fld = document.getElementById(this.name + '_' + this.resMultCell + '_' + i);
		fld.value = my_round(fld.value, 2).toFixed(2);
		//alert(fld.value);
		res = my_round(res + parseFloat(fld.value), 2);
	}
	fld = document.getElementById(this.name + "_total");
	fld.value = my_round(res, 2);
	//alert(res);
}

function footer() {
	var html = '';
	html += "<TR class='colCaptions'>\n";
	html += "	<TD colspan='" + (parseInt(this.cols) - 1) + "' align='right' class='colCaptions'>\n";
		html += "	Всичко:";
	html += "	</TD>\n";
	html += "	<TD class='colCaptions'>";
	html += "		<INPUT type='text' id='" + this.name + "_total' style='width: 98%; text-align: right;' value='0'>\n";
	html += "	</TD>\n";
	html += "</TR>\n";
	return html;
}

function compareSumWithTrnxAmnt() {
	var elem_id = 'fld_trnx' + this.trnx_no + '_amnt';
	var elem = document.getElementById(elem_id);
	trnx_amnt = parseFloat(elem.innerHTML);
	var sum = document.getElementById(this.name + "_total");
	//alert(elem.style.backgroundColor);
	if (trnx_amnt != parseFloat(sum.value)) {
		elem.style.backgroundColor = 'red';
	} else {
		elem.style.backgroundColor = 'transparent';
	}
	//alert('Comparing');
}

//The Cell Object
//To be implemented!!!

function cell(width) {
}

