function getOrdinalNumber(rowNum)
{
	if (rowNum > 99)
	{
		newRowNum = rowNum.toString().substring(rowNum.toString().length - 1);
	}
	else
	{
		newRowNum = rowNum.toString();
	}

	switch (newRowNum)
	{
		case "1":
			return("First");
			break;
		case "2":
			return("Second");
			break;
		case "3":
			return("Third");
			break;
		case "4":
			return("Fourth");
			break;
		case "5":
			return("Fifth");
			break;
		case "6":
			return("Sixth");
			break;
		case "7":
			return("Seventh");
			break;
		case "8":
			return("Eighth");
			break;
		case "9":
			return("Ninth");
			break;
		case "10":
			return("Tenth");
			break;
		case "21": case "31": case "41": case "51": case "61": case "71": case "81": case "91":
			return(rowNum + "st");
			break;
		case "22": case "32": case "42": case "52": case "62": case "72": case "82": case "92":
			return(rowNum + "nd");
			break;
		case "23": case "33": case "43": case "53": case "63": case "73": case "83": case "93":
			return(rowNum + "rd");
			break;
		default:
			return(rowNum + "th");
			break;
	}
}

function addMore(rowNum, placeHolderId, newDivId, hiddenDivId)
{
	// find the place holder div
	var placeHolder = document.getElementById(placeHolderId);

	// create a new div to contain the additional fields
	var additionalDiv = document.createElement("div");

	// set the attributes of the new div
	//additionalDiv.setAttribute("class", "grouping");
	additionalDiv.className = "newGrouping";
	additionalDiv.setAttribute("id", newDivId + rowNum);

	// copy the html from the hidden additional template div to the new div
	//additionalDiv.innerHTML = document.getElementById(hiddenDivId).innerHTML.replace(/replaceRowNum/g, rowNum).replace(/##NAME##/, getOrdinalNumber(rowNum));
	additionalDiv.innerHTML = document.getElementById(hiddenDivId).innerHTML.replace(/replaceRowNum/g, rowNum); // no need to replace ##NAME## anymore, reOrder function will do that

	// add the new div to the place holder
	placeHolder.appendChild(additionalDiv);

	// write the ordinal names to the divs
	reOrder(placeHolder, newDivId);
}

function removeMore(rowNum, placeHolderId, newDivId)
{
	// find the place holder div
	var placeHolder = document.getElementById(placeHolderId);

	// find the additional div to be removed
	var additionalDiv = document.getElementById(newDivId + rowNum);

	// remove the additional div from the place holder
	placeHolder.removeChild(additionalDiv);

	// write the ordinal names to the divs
	reOrder(placeHolder, newDivId);
}

function reOrder(placeHolder, newDivId)
{
	// ordinalCounter will be the ordinal number that is written
	var ordinalCounter = 1;

	// loop through the divs inside the placeholder
	for (var childCounter = 0; childCounter < placeHolder.childNodes.length; childCounter++)
	{
		// get the id of the div
		var divId = placeHolder.childNodes[childCounter].id;
		if (divId)
		{
			// check that the div is an additional div
			if (divId.toString().match(newDivId) == newDivId)
			{
				// increase the ordinal counter
				ordinalCounter++;

				// if the element is found, replace the current ordinal with the new one
				var objElement = document.getElementById(divId + "Ordinal");
				if (objElement)
				{
					objElement.replaceChild(document.createTextNode(getOrdinalNumber(ordinalCounter)), objElement.childNodes[0]);
				}
			}
		}
	}
}
