var keywordsAmount = 10;
var submitForm = true;
var lastId = 1;

function clickOption(obj) {
	if(obj.checked)
	{
		if(!document.getElementById('ctl00_CPHP_dss99_alert_9901').checked && !document.getElementById('ctl00_CPHP_dss99_alert_9902').checked)
			document.getElementById('ctl00_CPHP_dss99_alert_9901').checked = true;
	}
}

// ie event cought, enter pressed, row confirmed
function microsoftKeyPress2(e, rowId) {

	if(window.event) // IE
	{
	keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
	keynum = e.which
	}
	if(keynum == 13)
	{
		addRecord(rowId);
		return false;
	}
}

function addKeyword(word) {
	var hid = document.getElementById("ctl00_CPHP_dt99_notify_9901_keywords");
	hid.value += "," + word;
}

function removeKeyWord(word) {
	var hid = document.getElementById("ctl00_CPHP_dt99_notify_9901_keywords");
	str = new String(hid.value)
	hid.value = str.replace("," + word, "");
}

function findRowIndex(rowId)
{
	if (rowId == 0)
	{
		return -1;
	}
	var i;
	
	//alert(mainTable.rows.length);
	for (i=0; i<mainTable.rows.length; i++)
	{
		if (mainTable.rows[i].id == "row_" + rowId)
		{
			return i;
		}
	}
	//i = 10;
	return i;
}

function confirmRecord(rowId) {
	if(rowCount <= keywordsAmount) {
	var rowIndex = findRowIndex(rowId);
	var row = mainTable.rows[rowIndex];
	//alert(rowId);
	var keyword = document.getElementById("code_" + rowId).value;
	row.cells[0].innerHTML = "<input type=\"hidden\" value=\"" + keyword + "\" id=\"key_" + rowId + "\">" + keyword;
	row.cells[1].innerHTML = "<a id=\"btnDelRec\" href=javascript:delRecord("+rowId+")>Delete</a>";
	addKeyword(keyword);
	}
	//alert(document.getElementById("dt99_notify_9901_keywords").value);

}

function delRecord(rowId) {
	var rowIndex = findRowIndex(rowId);
	var row = mainTable.rows[rowIndex];
	var keyword = document.getElementById("key_" + rowId).value;
	//alert(keyword);
	removeKeyWord(keyword);
	mainTable.deleteRow(rowIndex);
	rowCount--;
	submitForm = rowCount <= keywordsAmount;

	//alert(rowCount);
	//alert(document.getElementById("dt99_notify_9901_keywords").value);
}

function addRecord(rowId)
{
	submitForm = rowCount <= keywordsAmount;
	confirmRecord(rowId);
	//alert(rowCount);
	if(rowCount <= keywordsAmount ) {
		rowCount++;
		var rowIndex = findRowIndex(rowId);
		var row = mainTable.rows[rowIndex];
		var lastRow = mainTable.rows[mainTable.rows.length - 1];
		var newRowId = rowId;

		// add new row using DOM, insertCell is not working, it adds a new row outside TBODY
		var demoTable = mainTable.getElementsByTagName("TBODY")[0];

		newRowDOM = document.createElement("tr");
		newRowDOM.id = "row_" + newRowId;

		newCell = document.createElement("td");
		newRowDOM.appendChild(newCell);

		newCell = document.createElement("td");
		newRowDOM.appendChild(newCell);

		demoTable.appendChild(newRowDOM);

		newRowId++;
		// assign it here, and use innerHTML
		var newRow = newRowDOM;
		//var newRow = mainTable.insertRow(rowIndex - 2);
		newRow.id = "row_" + newRowId;

		//alert(newRow.cells.length);
		
		newRow.cells[0].innerHTML = "<input style=\"width:360px\" type=\"text\" id=\"code_" + newRowId + "\" onKeyPress=\"microsoftKeyPress2(event, " + newRowId + ")\">";
		var newAddRow = mainTable.rows[rowIndex - 1];
		// hide add link to prevent multiple new rows added at the same time
		newRow.cells[1].innerHTML="<a id=\"btnAddNewRec\" href=javascript:addRecord("+newRowId+")>Add</a>";
		document.getElementById("code_" + newRowId).focus();
		lastId = newRowId;
		// assign it here, and use innerHTML
		var newRow = newRowDOM;
		//var newRow = mainTable.insertRow(rowIndex - 2);
		newRow.id = "row_" + newRowId;
	}
	else {
		alert("You can specify only " + keywordsAmount + " keywords");
	}
}


