var maxRow_Refer = 0;

function DataReplace(Data,Old,New){
	while(Data.match(Old)){
		Data = Data.replace(Old,New);
	}
	return Data;
}

function loadRow(rowNum, l_rowName, Name, Email) {
	Name = DataReplace(Name,'(!1!)','"');
	Name = DataReplace(Name,'(!2!)',"'");
	
	Email = DataReplace(Email,'(!1!)','"');
	Email = DataReplace(Email,'(!2!)',"'");

	addRow(rowNum, l_rowName, 0);
	var rowName = document.getElementById(l_rowName+"Name"+rowNum);
	var rowEmail = document.getElementById(l_rowName+"Email"+rowNum);
	rowName.value = Name;
	rowEmail.value = Email;
	return rowNum;
}

function addRow(rowNum, rowName, clicked) {
	if(document.getElementById('Dir')){
		Dir = document.getElementById('Dir').value;
	}

	if (rowNum != 1) {
		var nextRowNum = rowNum+1;
		// Row HTML
		var rowHTML = "<table width='100%' border='0' cellpadding='2' cellspacing='1'>";
		maxRowName = 'maxRow_Refer';
		rowHTML += "<tr>";
		rowHTML += "  <td width='15%' nowrap='nowrap'>Friends Name:</td>";
		rowHTML += "  <td width='30%' nowrap='nowrap'><input name='"+rowName+"[Name][]' type='text' id='"+rowName+"Name"+rowNum+"' size='20' maxlength='200'></td>";
		rowHTML += "  <td width='15%' nowrap='nowrap'>Friends Email:</td>";
		rowHTML += "  <td width='30%' nowrap='nowrap'><input name='"+rowName+"[Email][]' type='text' id='"+rowName+"Email"+rowNum+"' size='30' maxlength='200'></td>";
		rowHTML += " <td width='10%' class='rowBtns' nowrap='nowrap'>";
		rowHTML += "	<a id='"+rowName+"remRow"+rowNum+"' href='javascript:void(0);' onClick=\"removeRow("+rowNum+", '"+rowName+"', 1)\"><img src='images/button-row-minus.gif' alt='Click to remove' title='Click to remove' border='0' /></a>";
		rowHTML += "	<a id='"+rowName+"addRow"+rowNum+"' href='javascript:void(0);' onClick=\""+maxRowName+" = addRow("+nextRowNum+", '"+rowName+"', 1)\"><img src='images/button-row-add.gif' alt='Click to add' title='Click to add' border='0' /></a></span>";
		rowHTML += " </td>";
		rowHTML += "</tr>";
		rowHTML += "</table>";
		
	
		var newRow = document.getElementById(rowName+rowNum);
		var rowsContainer = document.getElementById(rowName+"rowsContainer");
		if (newRow == null) {
			var newRow = document.createElement("DIV");
			newRow.setAttribute("id", rowName+rowNum);
			rowsContainer.appendChild(newRow);
		}
		newRow.innerHTML = rowHTML;
		
		// Set focus to new Name text box
		if(clicked){
			var newName = document.getElementById(rowName+"Name"+rowNum);
			newName.focus();
		}
	
		// Show add link only on last row
		showAddRemLink(rowName);
	}
	return (nextRowNum-1);
}

/**
	Set visiblily of given row number to hidden
*/
function removeRow(rowNum, rowName, clicked) {
	// Get current row and data
	var currRow = document.getElementById(rowName+rowNum);
	var currName = document.getElementById(rowName+"Name"+rowNum);
	var currDesc = document.getElementById(rowName+"Email"+rowNum);
	
	// Get previous and next rows
	var nextNum = rowNum + 1;
	
	var nextName = document.getElementById(rowName+"Name"+nextNum);
	var nextDesc = document.getElementById(rowName+"Email"+nextNum);
	
	if (nextName == null) {
		currRow.innerHTML = '';
	} else {
		currName.value = nextName.value;
		currDesc.value = nextDesc.value;
		
		removeRow(nextNum, rowName, 0);
	}
	
	// Remove remove link
	showAddRemLink(rowName);
}

/**
	Only displays add link on last row
*/
function showAddRemLink(rowName) {
	// For each row set add link to hidden
	// Get last visible row
	var i = 1;
	var addRow = document.getElementById(rowName+"addRow"+i);
	var remRow = document.getElementById(rowName+"remRow"+i);
	
	while (addRow != null) {
		addRow.style.visibility = 'hidden';
		remRow.style.visibility = 'visible';
		i++;
		addRow = document.getElementById(rowName+"addRow"+i);
		remRow = document.getElementById(rowName+"remRow"+i);
	}
	i--;
	addRow = document.getElementById(rowName+"addRow"+i);
	addRow.style.visibility = 'visible';
	
	var remRow = document.getElementById(rowName+"remRow"+i);
	
	if (i == 1) {
		remRow.style.visibility = 'hidden';
	} else {
		remRow.style.visibility = 'visible';
	}
}


