
/*
 * Ensure that a title and a description must be supplied for each picture or download selected.
 * Return 0 if title and descriptions were provided, otherwise return 1.
 */
 
function checkMandatoryFields( element )
{
	var error = 0;
	var message = "";
	
	if( element.name.indexOf( "file_file" ) != -1 )
	{
		if( document.getElementById( 'fileDescriptor' ).innerHTML.indexOf( "Thumbnail" ) != -1 )
		{
			if( document.getElementById( 'pictureDescription' ).value.length == 0 )
			{
				message += "Please provide a short description of the picture. ";
				error = 1;
			}
			
			if( document.getElementById( 'altTag' ).value.length == 0 )
			{
				message += "Please provide alternative text for the picture. ";
				error = 1;
			}
		}
	
	}
	
	if( message.length > 0 )
		alert( message );
		
	return( error );
}

function storeMandatoryFields( element, rowToAppendTo )
{
	if( element.name.indexOf( "file" ) != -1 )
	{
		if( document.getElementById( 'fileDescriptor' ).innerHTML.indexOf( "Big Picture" ) != -1 )
		{
			var description = document.createElement( 'input' );
			description.type = 'hidden';
			description.value = document.getElementById( 'pictureDescription' ).value;
			description.name = element.name + '_description';
		
			rowToAppendTo.appendChild( description );
			
			var altTag = document.createElement( 'input' );
			altTag.type = 'hidden';
			altTag.value = document.getElementById( 'altTag' ).value;
			altTag.name = element.name + '_alttag';
		
			rowToAppendTo.appendChild( altTag );
		
			document.getElementById( 'pictureDescription' ).value = "";
			document.getElementById( 'altTag' ).value = "";
		}
	}
}

function setAsGalleryText( absolutePath, webRoot )
{
	var checkBoxGrp = document.getElementsByName( 'galleryText' );
	var value = "";
	
	for( var i = 0; i < checkBoxGrp.length; i++ )
	{
		if( checkBoxGrp[i].checked == true )
			value = checkBoxGrp[i].value;
	}
	
	new Ajax.Request( webRoot + '/includes/SetAsGalleryText.php', {
		method: 'get',
		parameters: 'value=' + value + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
		}
	});
}

function addToHomepage( absolutePath, webRoot, index, galleryId )
{
	var checkBoxGrp = document.getElementsByName( 'feed[]' );
	
	var checked = 0;
	
	if( checkBoxGrp[index].checked == true )
		checked = 1;
	
	new Ajax.Request( webRoot + '/includes/AddToHomepage.php', {
		method: 'get',
		parameters: 'galleryId=' + galleryId + '&' + 'checked=' + checked + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
		}
	});
}

function addToAboutpage( absolutePath, webRoot, index, galleryId )
{
	var checkBoxGrp = document.getElementsByName( 'feed[]' );
	
	var checked = 0;
	
	if( checkBoxGrp[index].checked == true )
		checked = 1;
	
	new Ajax.Request( webRoot + '/includes/AddToAboutpage.php', {
		method: 'get',
		parameters: 'galleryId=' + galleryId + '&' + 'checked=' + checked + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
		}
	});
}

function getNewsDetail( absolutePath, webRoot, newsId )
{
	var contents = "";
	
	new Ajax.Request( webRoot + '/includes/GetNewsDetail.php', {
		method: 'get',
		parameters: 'newsId=' + newsId + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
		
			var array = transport.responseText.split( '~' );
			
			var div = "<div class=\"newsdetail\"><h1>" + array[0] + "</h1><img src=\"" + array[2] + "\" alt=\"" +
				array[3] + "\" />" + array[1] + "</div>";
			
			/*var options = {
        		overlayOpacity: '0.00'
    		};

   	 		Shadowbox.init(options);*/
			
			Shadowbox.init();
	
			Shadowbox.open({
        		type:       'html',
        		content:    div,
				height:	400,
				width: 840
			});
		}
	});
}

function buildNewsMenu( absolutePath, webRoot, variableName, variableValue, currentPage, pageTotal, 
	numRowsToRetrieve, nextReference )
{
	var previousId = "";
	var nextId = "";
	
	if( nextReference.indexOf( 'next' ) != -1 )
	{
		currentPage++;
		variableValue = parseInt( variableValue ) + parseInt( numRowsToRetrieve );
		
		previousId = nextReference.replace(/next/,"prev");
		nextId = nextReference;
	}
	else
	{
		currentPage--;
		variableValue = parseInt( variableValue ) - parseInt( numRowsToRetrieve );
		
		previousId = nextReference;
		nextId = nextReference.replace(/prev/,"next");
	}
	
	new Ajax.Request( webRoot + '/includes/BuildNewsIndex.php', {
		method: 'get',
		parameters: variableName + '=' + variableValue + '&' + 'ajax=1' + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '~' );
			
			var newsItems = array[0];
			var newsTooltips = array[1];
			var newsItemToTooltips = array[2];
			
			var buttons = "";
			
			if( currentPage != 1 )
			{
				var previousPage = currentPage;
				previousPage--;
				
				buttons += "<a class=\"previous\" id=\"" + previousId + "\" href=\"javascript:buildNewsMenu('" + 
					absolutePath + "','" + webRoot + "','" + variableName + "','" + variableValue + "','" + 
					currentPage + "','" + pageTotal + "','" + numRowsToRetrieve + "','" + previousId + "')" + 
				    '" ' + "target=\"_self\"></a>";
			}
			
			if( currentPage != pageTotal )
			{		
				buttons += "<a class=\"next\" id=\"" + nextId + "\" href=\"javascript:buildNewsMenu('" + 
					absolutePath + "','" + webRoot + "','" + variableName + "','" + variableValue + "','" + 
					currentPage + "','" + pageTotal + "','" + numRowsToRetrieve + "','" + nextId + "')" + 
					'" ' + "target=\"_self\"></a>";
			}
			
			document.getElementById( 'arrows' ).innerHTML = buttons;
			
			var container = document.getElementById( 'list' );
			var uls = container.getElementsByTagName( 'ul' );
			var ul = uls[0];
			
			ul.innerHTML = newsItems;
			ul.innerHTML += newsTooltips;
			
			var array = newsItemToTooltips.split( ',' );
			var len = array.length;
			
			for( var i = 0; i < len; i++ )
			{
				var newsItemToTooltip = array[i].split( '|' );
				var my_tooltip = new Tooltip( newsItemToTooltip[0], newsItemToTooltip[1] );
			}
		}
	});
}

function getProjectDetail( absolutePath, webRoot, projectId )
{
	var contents = "";
	
	new Ajax.Request( webRoot + '/includes/GetProjectDetail.php', {
		method: 'get',
		parameters: 'projectId=' + projectId + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
			
			var array = transport.responseText.split( '~' );
			
			var div = "<div class=\"gallerydetail\"><img id=\"projectImage\" src=\"" + array[0] + "\" " +
				"alt=\"" + array[2] + "\" /><div id=\"top\">";
			
			div += "<a class=\"next\" id=\"nextProjectImage\" href=\"javascript:getProjectImage('" + 
				absolutePath + "','" + webRoot + "','" + projectId + "','nextImage','1','1','" + array[3] + 
				"','1'," + "'nextProjectImage')" + '" ' + "target=\"_self\"";
				
			if( array[3] <= 1 )
				div += " style=\"display:none\"";
			
			div += "></a>";
			
			div += "<a class=\"previous\" id=\"previousProjectImage\" href=\"javascript:getProjectImage('" + 
				absolutePath + "','" + webRoot + "','nextImage','1','2','" + array[2] + "','1'," +
				"'nextProjectImage')" + '" ' + "target=\"_self\"";
			
			if( array[3] <= 1 )
				div += " style=\"display:none\"";
				
			div += "></a>";
			
			div += "<h1>" + array[4] + "</h1><span id=\"desc\" class=\"desc\">" + array[1] + "</span></div>";
			
			div += "<div id=\"details\"><span>Location: " + array[5] + "</span><span>Client: " + array[6] + 
				"</span><span>Sector: " + array[7] + "</span><span>Image Type: " + array[8] + "</span>" +
				"<span class=\"last\">Date: " + array[9] + "</span></div>";
				
			div += "<div class=\"text\"><p>" + array[10] + "</p></div>";
			div += "<div class=\"text\"><p>" + array[11] + "</p></div></div>";
			
			/*var options = {
        		overlayOpacity: '0.00'
    		};

   	 		Shadowbox.init(options);*/
			Shadowbox.init();
	
			/*Shadowbox.open({
        		type:       'html',
        		content:    div,
				width: 830,
				height: 630
			});*/
			
			Shadowbox.open({
        		type:       'html',
        		content:    div,
				width: 880,
				height: 630
			});
		}
	});
}

function getProjectImage( absolutePath, webRoot, galleryId, variableName, variableValue, currentPage, pageTotal, 
	numRowsToRetrieve, nextReference )
{
	document.getElementById( 'projectImage' ).src = loadingImage.src;
	document.getElementById( 'projectImage' ).alt = "Loading requested image";
	
	var previousId = "";
	var nextId = "";
	
	if( nextReference.indexOf( 'next' ) != -1 )
	{
		currentPage++;
		variableValue = parseInt( variableValue ) + parseInt( numRowsToRetrieve );
		
		previousId = nextReference.replace(/next/,"prev");
		nextId = nextReference;
	}
	else
	{
		currentPage--;
		variableValue = parseInt( variableValue ) - parseInt( numRowsToRetrieve );
		
		previousId = nextReference;
		nextId = nextReference.replace(/prev/,"next");
	}
	
	new Ajax.Request( webRoot + '/includes/GetProjectImage.php', {
		method: 'get',
		parameters: 'galleryId=' + galleryId + '&' + variableName + '=' + variableValue + '&' + 
			'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '~' );
			
			document.getElementById( 'projectImage' ).src = array[0];
			document.getElementById( 'projectImage' ).height = array[1];
			document.getElementById( 'projectImage' ).width = array[2];
			document.getElementById( 'projectImage' ).alt = array[3];
			document.getElementById( 'desc' ).innerHTML = array[4];
			  
			if( currentPage != 1 )
			{
				var previousPage = currentPage;
				previousPage--;
				
				// Create a hyperlink to allow the previous image to be retrieved when the previous link
				// is clicked.
				var imageLink = "javascript:getProjectImage('" + absolutePath + "','" + webRoot + "','" + 
					galleryId + "','" + variableName + "','" + variableValue + "','" + currentPage + "','" + 
					pageTotal + "','" + numRowsToRetrieve + "','" + previousId + "')";
				
				document.getElementById( 'previousProjectImage' ).href = imageLink;
				document.getElementById( 'previousProjectImage' ).style.display = "";
			}
			else
				document.getElementById( 'previousProjectImage' ).href = "#";
			
			if( currentPage != pageTotal )
			{		
				var imageLink = "javascript:getProjectImage('" + absolutePath + "','" + webRoot + "','" + 
					galleryId + "','" + variableName + "','" + variableValue + "','" + currentPage + "','" + 
					pageTotal + "','" + numRowsToRetrieve + "','" + nextId + "')";
				
				// Only ever update a next link since when the detail of a selected project is retrieved
				// a next link is created if one is needed. If a next link is not created at this point
				// it is not needed for this project and this section of code is not executed.
				document.getElementById( 'nextProjectImage' ).href = imageLink;
				document.getElementById( 'nextProjectImage' ).style.display = "";
			}
			else
				document.getElementById( 'nextProjectImage' ).href = "#";
		}
	});
}

function buildProjectMenu( absolutePath, webRoot, variableName, variableValue, currentPage, pageTotal, 
	numRowsToRetrieve, nextReference )
{
	var previousId = "";
	var nextId = "";
	
	if( nextReference.indexOf( 'next' ) != -1 )
	{
		currentPage++;
		variableValue = parseInt( variableValue ) + parseInt( numRowsToRetrieve );
		
		previousId = nextReference.replace(/next/,"prev");
		nextId = nextReference;
	}
	else
	{
		currentPage--;
		variableValue = parseInt( variableValue ) - parseInt( numRowsToRetrieve );
		
		previousId = nextReference;
		nextId = nextReference.replace(/prev/,"next");
	}
	
	new Ajax.Request( webRoot + '/includes/BuildProjectsIndex.php', {
		method: 'get',
		parameters: variableName + '=' + variableValue + '&' + 'ajax=1' + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '~' );
			
			var projects = array[0];
			var projectTooltips = array[1];
			var projectToTooltip = array[2];
			
			var buttons = "";
			
			if( currentPage != 1 )
			{
				var previousPage = currentPage;
				previousPage--;
				
				buttons += "<a class=\"previous\" id=\"" + previousId + "\" href=\"javascript:buildProjectMenu('" + 
					absolutePath + "','" + webRoot + "','" + variableName + "','" + variableValue + "','" + 
					currentPage + "','" + pageTotal + "','" + numRowsToRetrieve + "','" + previousId + "')" + 
				    '" ' + "target=\"_self\"></a>";
			}
			
			if( currentPage != pageTotal )
			{		
				buttons += "<a class=\"next\" id=\"" + nextId + "\" href=\"javascript:buildProjectMenu('" + 
					absolutePath + "','" + webRoot + "','" + variableName + "','" + variableValue + "','" + 
					currentPage + "','" + pageTotal + "','" + numRowsToRetrieve + "','" + nextId + "')" + 
					'" ' + "target=\"_self\"></a>";
			}
			
			document.getElementById( 'arrows' ).innerHTML = buttons;
			
			var container = document.getElementById( 'list' );
			var uls = container.getElementsByTagName( 'ul' );
			var ul = uls[0];
			
			ul.innerHTML = projects;
			ul.innerHTML += projectTooltips;
			
			var array = projectToTooltip.split( ',' );
			var len = array.length;
			
			for( var i = 0; i < len; i++ )
			{
				var projectToTooltip = array[i].split( '|' );
				var my_tooltip = new Tooltip( projectToTooltip[0], projectToTooltip[1] );
			}
		}
	});
}

function updateDatabase( absolutePath, webRoot, checkBoxName, tableName, columnName, index, primaryKeyValue )
{
	var checkBoxGrp = document.getElementsByName( checkBoxName );
	
	var checked = 0;
	
	if( checkBoxGrp[index].checked == true )
		checked = 1;
	
	new Ajax.Request( webRoot + '/includes/UpdateDatabase.php', {
		method: 'get',
		parameters: 'tableName=' + tableName + '&' + 'columnName=' + columnName + '&' + 'status=' + checked + '&' +
			'primaryKeyValue=' + primaryKeyValue + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
		}
	});
}

function deleteFromOrderedList()
{
	var checkBoxGrp = document.getElementsByName( 'delete[]' );
	var len = checkBoxGrp.length;
	
	var idxOfCheck = 0;
	
	for( i = 0; i < len; i++ )
	{
		if( checkBoxGrp[i].checked )
			idxOfCheck = i;
	}
	
	var list = document.getElementById( 'sections' );
	var items = list.getElementsByTagName( "li" );
	
	var html = items[idxOfCheck].innerHTML;
	var idxValue = html.indexOf( 'value' );
	var idxStartQuote = html.indexOf( '"', idxValue );
	idxStartQuote++;
	var idxEndQuote = html.indexOf( '"', idxStartQuote );
	
	var temp = html.substring( idxStartQuote, idxEndQuote );
	
	var array = temp.split( ',' );
	
	var id = array[0];
	var absolutePath = array[1];
	var webRoot = array[2];
	var checkBoxName = array[3];
	var tableNameToColumnName = array[4];
	var thingBeingRemoved = array[5];
	var noDataRows = array[6];
	
	if( deleteFromDatabase( absolutePath, webRoot, checkBoxName, tableNameToColumnName,
		thingBeingRemoved, noDataRows ) == '1' )
	{
		document.getElementById( 'listitem' + id ).parentNode.removeChild( document.getElementById( 'listitem' + id ) );
	}
}

function orderByDate( absolutePath, webRoot, column )
{
	new Ajax.Request( webRoot + '/includes/ChangeArticleOrder.php', {
		method: 'get',
		parameters: 'order=1' + '&' + 'column=' + column + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
		}
	});
}

function manualOrder( absolutePath, webRoot, column )
{
	new Ajax.Request( webRoot + '/includes/ChangeArticleOrder.php', {
		method: 'get',
		parameters: 'order=0' + '&' + 'column=' + column + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
		}
	});
}

function sendEmail( webRoot )
{
	var name = "";
	var email = "";
	var subject = "";
	var message = "";
	
	var inputs = document.getElementsByTagName( 'input' );
	
	for( i = 0; i < inputs.length; i++ )
	{
		if( inputs[i].id == "name" && inputs[i].value != "" )
			name = inputs[i].value;
			
		if( inputs[i].id == "email" && inputs[i].value != "" )
			email = inputs[i].value;
		
		if( inputs[i].id == "subject" && inputs[i].value != "" )
			subject = inputs[i].value;
	}
	
	var textareas = document.getElementsByTagName( 'textarea' );
	
	for( i = 0; i < textareas.length; i++ )
	{
		if( textareas[i].id == "message" && textareas[i].value != "" )
			message = textareas[i].value;
	}
	
	if( name == "" || email == "" || subject == "" || message == "" )
	{
		alert( "Please complete all of the fields" );
		return;
	}

	new Ajax.Request( 'includes/SendEmail.php', {
		method: 'get',
		parameters: 'name=' + name + '&' + 'message=' + message + '&' + 'email=' + email + '&' + 
			'subject=' + subject,
  		onSuccess: function( transport )
		{
			message = '<p class="success_message">Thank you, your email has been sent</p></font>';
				
			document.getElementById( 'form' ).innerHTML = message;
		}
	});
}

function updateDisplayOrder()
{
	var list = document.getElementById( 'sections' );
	var items = list.getElementsByTagName( "li" );
	var len = items.length;
		
	var order = new Array();
	
	for( i = 0; i < len; i++ )
	{
		var html = items[i].innerHTML;
		var id = html.indexOf( 'id=' );
		var idxEqual = html.indexOf( '=', id );
		idxEqual++;
		var idxQuote = html.indexOf( '"', idxEqual );
			
		order.push( html.substring( idxEqual, idxQuote ) );
	}
	
	new Ajax.Request( webRoot + '/includes/UpdateDisplayOrder.php', {
		method: 'get',
		parameters: 'order=' + order.join( "," ) + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
		}
	});
}
	
/*
 * Use Ajax to remove a database entry after double checking with the user. checkBoxName is used to identify the
 * checkbox (along with rowReference) if the user cancels the delete and in which case the checkbox should be unchecked.
 * tableNameToColumn is used to identify the table and the table row, thingBeingRemoved is used to customise the confirm 
 * message and noDataRows is used to make certain rows visible or invisible when there are no rows left in tableName to delete.
 */
 	
function deleteFromDatabase( absolutePath, webRoot, checkBoxName, tableNameToColumnName,
	thingBeingRemoved, noDataRows, rowReference )
{
	var deleted = 1;
	
	// If the row should be deleted.
	if( confirm( "Are you sure you want to delete this " + thingBeingRemoved + "?" ))
	{
		var currentPage = window.location;
		
		// Using Ajax to delete the row from the database.
		new Ajax.Request( webRoot + '/includes/DeleteFromDatabase.php', {
			method: 'get',
			parameters: 'tableNameToColumnName=' + tableNameToColumnName + '&' + 'absolutePath=' + absolutePath + 
				'&' + 'currentPage=' + currentPage,
  			onSuccess: function( transport ){
				
				if( typeof rowReference !== "undefined" )
				{
					// Delete the table row from the HTML document.
					var i = rowReference.parentNode.parentNode.rowIndex;
					document.getElementById( 'contentArea' ).deleteRow(i);
				}
				
				// Decrement the value of the variable that indicates how many rows are left in the table.
				numRows--;
				
				if( numRows == 0 )
				{
					// If no rows are left retrieve the name of the row to update and an integer value to 
					// determine whether the row should be made visible or invisible.
					var rows = noDataRows.split( '|' );
					
					for( i = 0; i < rows.length; i++ )
					{
						var rowNameToStatus = rows[i].split( '~' );
						
						if( rowNameToStatus[1] == "0" )
							document.getElementById( rowNameToStatus[0] ).style.display = "none";
						else
							document.getElementById( rowNameToStatus[0] ).style.display = "";
					}
				}
			}
		});
	}
	else
		deleted = 0;
	
	var checkBoxGrp = document.getElementsByName( 'delete[]' );
	var len = checkBoxGrp.length;
		
	for( i = 0; i < len; i++ )
	{
		if( checkBoxGrp[i].checked )
			checkBoxGrp[i].checked = false;
	}
	
	return( deleted );
}

/*
 * This function is called when the user adds a new menu item using the HTML form. A div is created to store 
 * the item title, description and price. These are stored in hidden input fields to make sure that these values 
 * are accessible in $_POST when the form is submitted. The div is given an id and name to allow the menu item to 
 * be removed if the user decides. The textboxes are cleared and the number of menu items, used to produce 
 * unique id and name values, is incremented. This function has been created as a menu item may to added when 
 * the menu is created or edited.
 */
 
function newItem( cmsImages )
{
	var message = "";
	
	var title = document.getElementById( 'itemTitle' ).value;
	var description = document.getElementById( 'itemDescription' ).value;
	var price = document.getElementById( 'itemPrice' ).value;
	
	if( title.length == 0 )
		message += "Please provide a title for the menu item. ";
	
	if( description.length == 0 )
		message += "Please provide a description for the menu item. ";
	
	if( price.length == 0 )
		message += "Please provide the price for the menu item. ";
		
	if( message == "" )
	{
		var row = document.createElement( 'tr' );
		var rowIdName = 'new_items_' + newItemsCount;
		row.setAttribute( 'id', rowIdName );
		
		var column1 = document.createElement( 'td' );
		column1.setAttribute( 'width', '129' );
		column1.appendChild( document.createTextNode( title ) );
		
		var inputTitle = document.createElement( 'input' );
		inputTitle.setAttribute( 'type', 'hidden' );
		inputTitle.setAttribute( 'id', 'new_items_title' + newItemsCount );
		inputTitle.setAttribute( 'name', 'new_items_title' + newItemsCount );
		inputTitle.setAttribute( 'value', title );
		column1.appendChild( inputTitle );
		
		var inputDescription = document.createElement( 'input' );
		inputDescription.setAttribute( 'type', 'hidden' );
		inputDescription.setAttribute( 'id', 'new_items_description' + newItemsCount );
		inputDescription.setAttribute( 'name', 'new_items_description' + newItemsCount );
		inputDescription.setAttribute( 'value', description );
		column1.appendChild( inputDescription );
		
		var inputPrice = document.createElement( 'input' );
		inputPrice.setAttribute( 'type', 'hidden' );
		inputPrice.setAttribute( 'id', 'new_items_price' + newItemsCount );
		inputPrice.setAttribute( 'name', 'new_items_price' + newItemsCount );
		inputPrice.setAttribute( 'value', price );
		column1.appendChild( inputPrice );
		
		var column2 = document.createElement( 'td' );
		column2.setAttribute( 'width', '341' );
		
		var new_row_button = document.createElement( 'img' );
		new_row_button.setAttribute( 'src', cmsImages + '/ButtonDeleteOn.png' );
		new_row_button.setAttribute( 'width', '45' );
		new_row_button.setAttribute( 'height', '15' );
		new_row_button.setAttribute( 'border', '0' );
		var deleteButtonIdName = 'newItemDeleteImage' + newItemsCount;
		new_row_button.setAttribute( 'id', deleteButtonIdName );
		new_row_button.setAttribute( 'name', deleteButtonIdName );
		
		var button_link = document.createElement( 'a' );
		button_link.onmouseover = function() { swapImage( deleteButtonIdName, cmsImages + '/ButtonDeleteOff.png' ) };
		button_link.onmouseout = function() { swapImage( deleteButtonIdName, cmsImages + '/ButtonDeleteOn.png' ) };
		button_link.onclick = function() { deleteDiv( rowIdName ); };
		
		button_link.appendChild( new_row_button );
		
		column2.appendChild( button_link );
		row.appendChild( column1 );
		row.appendChild( column2 );
		document.getElementById( 'itemsToBeAdded' ).appendChild( row );
	
		document.getElementById( 'itemTitle' ).value = "";
		document.getElementById( 'itemDescription' ).value = "";
		document.getElementById( 'itemPrice' ).value = "";
	
		newItemsCount++;
	}
	else
		alert( message );
}

/*
 * When deleting a row from a table it is sufficient just to have the id or name of the table row since
 * the table it belongs to can be found using parentNode. The removeChild function can then be used to
 * remove the table row from the table element. However, when adding rows it is necessary to identify
 * where in the table the rows should be inserted. headingRow does just this. It also explains what
 * data follows and therefore should be displayed if data exists and not if it doesn't. Once a row is
 * removed a count of the number of rows is computed and if this is zero the appendTo/header row is no
 * longer displayed. It can easily be displayed again if new data is added. The third argument to this
 * function is used to determine how many rows and left and when the header row should no longer be displayed.
 */
 
function deleteTableRow( headingRow, rowToDelete, deleteImage )
{
	var table = document.getElementById( rowToDelete ).parentNode;
	
	// Remove the row chosen by the user by navigating to the parent of the table row, the table, and then
	// using the removeChild function.
	table.removeChild( document.getElementById( rowToDelete ) );
	
	// Calculate how many rows are left following the headingRow. Each row has a delete button which
	// is a unique id but whose id has a common part for all rows belonging to the append to row.
	
	// Initially find all images on the page.
	var images = table.getElementsByTagName( 'img' );
	var len = images.length;
	
	var count = 0;
	
	for( i = 0; i < len; i++ )
	{
		// We are only interested in delete images that belong to rows related to headingRow
		if( images[i].id.indexOf( deleteImage ) != -1 )
			count++;
	}
	
	// If there aren't any rows hide the headingRow row so that we are not describing data that doesn't
	// exist.
	if( count == 0 )
	{
		var row = document.getElementById( headingRow );
		
		var rowIndex = row.rowIndex;
		rowIndex++;
		
		table.deleteRow( rowIndex );
		
		row.style.display = "none";
	}
}

/*
 * Delete a menu item given the parent div to which each div is appended.
 */
 
function deleteDiv( divIdToRemove )
{
	document.getElementById( divIdToRemove ).parentNode.parentNode.removeChild( document.getElementById( divIdToRemove ).parentNode );
}

/*
 * Change the current image to that passed for the HTML id/name also passed. Used for implementing rollovers.
 */
 
function swapImage( htmlIdName, imageSrc )
{
	document.getElementById( htmlIdName ).src = imageSrc;
}