var INPUT_NAME_PREFIX = 'txt_';
var RADIO_NAME = 'totallyrad';
//var TABLE_NAME = 'tblSample';
var ROW_BASE = 1;
var hasLoaded = true;
//window.onload=fillInRows;
function fillInRows(tname)
{
	hasLoaded = true;
	addPayment(tname);
}

function myRowObject(one, two, three, four)
{
	this.one = one;
	this.two = two;
	this.three = three;
	this.four = four;
}

function insertRowToTable()
{
	if (hasLoaded) {
		var tbl = document.getElementById();
		var rowToInsertAt = tbl.tBodies[0].rows.length;		
		addPayment(rowToInsertAt);
		reorderRows(tbl, rowToInsertAt);
	}
}

function addPlaces(TABLE_NAME, country, country_text, state, state_text, cities, cities_text, num)
{
	if (hasLoaded) 
	{
		tbl_name = TABLE_NAME;
		var tbl = document.getElementById(TABLE_NAME);
		tbl.border=1;
		tbl.width="100%";
		tbl.className="graphTable";
		var nextRow = tbl.tBodies[0].rows.length;
		var iteration = nextRow + ROW_BASE;
		if (num == null) 
		{ 
			num = nextRow;
		} 
		else 
		{
			iteration = num + ROW_BASE;
		}

		var row = tbl.tBodies[0].insertRow(num);		
		row.className = 'classy' + (iteration % 2);
	
		var cell0 = row.insertCell(0);
		var textNode = document.createTextNode(iteration);
		cell0.className="blacktxt";
		cell0.width="6%";
		cell0.appendChild(textNode);
		
		var cell1 = row.insertCell(1);
		var textNode1 = document.createTextNode(country_text);
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'hidden');
		txtInp1.setAttribute('name', 'OperatingCountry[]');
		txtInp1.setAttribute('id', 'OperatingCountry' + iteration);
		txtInp1.setAttribute('size', '19');
		txtInp1.setAttribute('value', country);
		txtInp1.className="textfield1";	
		cell1.width="20%";
		cell1.appendChild(textNode1);
		cell1.appendChild(txtInp1);
				
		var cell2 = row.insertCell(2);
		var textNode2 = document.createTextNode(state_text);
		var txtInp2 = document.createElement('input');		
		txtInp2.setAttribute('type', 'hidden');
		txtInp2.setAttribute('name', 'OperatingState[]');
		txtInp2.setAttribute('id', 'OperatingState' + iteration);
		txtInp2.setAttribute('size', '19');
		txtInp2.setAttribute('value', state);
		txtInp2.className="textfield1";		
		cell2.width="20%";
		cell2.appendChild(textNode2);
		cell2.appendChild(txtInp2);
		
		var cell3 = row.insertCell(3);
		var textNode3 = document.createTextNode(cities_text);
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'hidden');
		txtInp3.setAttribute('name', 'OperatingCity[]');
		txtInp3.setAttribute('id', 'OperatingCity' + iteration);
		txtInp3.setAttribute('size', '23');
		txtInp3.setAttribute('value', cities);
		txtInp3.className="textfield1";		
		cell3.width="54%";		
		cell3.appendChild(textNode3);
		cell3.appendChild(txtInp3);

		row.myRow = new myRowObject(textNode, txtInp1, txtInp2, txtInp3);
	}
}

function deleteChecked(TABLE_NAME)
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
	
		var tbl = document.getElementById(TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) 
		{
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.three.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.three.checked) 
			{
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
			}
		}
		if (checkedObjArray.length > 0) 
		{
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

function deleteCurrentRow(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
	}
}

function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) 
	{
		if (tbl.tBodies[0].rows[startingIndex]) 
		{
			var count = startingIndex + ROW_BASE;

			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) 
			{
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text

				tbl.tBodies[0].rows[i].myRow.two.name = "Country[]"; 
				tbl.tBodies[0].rows[i].myRow.two.id = "Country" + count +'_id';// input text

				tbl.tBodies[0].rows[i].myRow.three.name = "State[]"; 
				tbl.tBodies[0].rows[i].myRow.three.id = "State" + count +'_id';// input text

				tbl.tBodies[0].rows[i].myRow.four.name = "Cities[]"; 
				tbl.tBodies[0].rows[i].myRow.four.id = "Cities" + count +'_id';// input text

				var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
			
				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);

				count++;
			}
		}
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) 
	{
		for (var i=0; i<rowObjArray.length; i++) 
		{
			var rIndex = rowObjArray[i].sectionRowIndex;
			var tbl = rowObjArray[i].parentNode.parentNode;
			rowObjArray[i].parentNode.deleteRow(rIndex);
			reorderRows(tbl, rIndex);
		}
	}
}

function openInNewWindow(frm)
{
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	frm.target = 'TableAddRow2NewWindow';
	
	frm.submit();
}