

function outputGuestlistSpaces() {
	filledNames = new Array;
	filledSurnames = new Array;
	totalToDisplay = document.getElementById("num_bookings").value;
	//alert("Initial Total To Display: " + totalToDisplay);

	// check to see if any names have already been added

	if(document.getElementById("guestListNames").innerHTML != "") {
		
		x = 1;
		
		var debug = "";
		
		totalSpacesShown=1;
		totalSpacesFilled=1;
		
		var theForm = document.guestlist;
		// loop through all the guestlist form elements
		for(i=0; i<theForm.elements.length; i++){
			if(theForm.elements[i].type == "text"){
				
				if(theForm.elements[i].value != "" && theForm.elements[i].name == "guestlistSurname[]") {
					fieldID = theForm.elements[i].id.substring(17,21);
					if(document.getElementById("guestListName_" + fieldID).value != "") {
						filledSurnames.push(document.getElementById("guestListSurname_" + fieldID).value);
						filledNames.push(document.getElementById("guestListName_" + fieldID).value);
						totalSpacesFilled++;
					}
				}
				
				totalSpacesShown++;
			}
		}
		
//		alert("Total Surnames: " + filledSurnames.length);
		
		totalEmptyNames = totalToDisplay - filledSurnames.length;
		
//		alert("Total Blank Spaces to be added: " + totalEmptyNames);

	} else {
		totalEmptyNames = totalToDisplay;	
	}

	
	// if they have, add all added names to an array
	
	// calculate how many spaces difference there are between the total entered and the total remaining
	// if less that remaining, then add X number of boxes, if more, then remove last X boxes (regardless
	// of whether names have been entered)
	
	var guestlistNames = "";
	
	x = 1;
	initialOutput = "";
	// output the filled values
	
	

	if(filledSurnames.length > totalToDisplay) {
		//alert("second total to display: " + totalToDisplay);
		initialOutput = totalToDisplay;
		//alert("total to display: " + initialOutput);
	} else if(filledSurnames.length <= totalToDisplay) {
		initialOutput = filledSurnames.length;	
	}

	namecount = 1;
	
	guestlistNames = '<br /><h2>Contine Below:</h2><div class="event_hr"></div><strong>You must enter both the first name and surname for each person on the guest list</strong><br /><table><tr><td></td><td>First Name</td><td>Surname</td></tr>';
	
	if(initialOutput != "") {
		while(x <= initialOutput) {
			guestlistNames += '<tr><td>' + namecount +'. </td><td>\n<input id="guestListName_' + x + '" name="guestlistName[]" value="' + filledNames[x-1] + '" />\n</td><td><input id="guestListSurname_' + x + '" name="guestlistSurname[]" value="' + filledSurnames[x-1] + '" /></td></tr>\n';
			x=x+1;
			namecount++;
		}
		x=1;
	}
	
	while(x <= totalEmptyNames) {
		guestlistNames += '<tr><td>' + namecount +'. </td><td>\n<input id="guestListName_' + namecount + '" name="guestlistName[]" value="" />\n</td><td><input id="guestListSurname_' + namecount + '" name="guestlistSurname[]" value="" /></td></tr>\n';
		x=x+1;
		namecount++;
	}

	guestlistNames += '</table><input type="submit" name="submit" value="Book Guest List" />';
	
	document.getElementById("guestListNames").innerHTML = guestlistNames;
	
}