try
{
var currentIndex;

function checkMandatoryFields( element )
{
	var error = 0;
	var message = "";
	
	if( element.name.indexOf( "staffGraphics" ) != -1 )
	{
		if( document.getElementById( 'firstName' ).value.length == 0 )
		{
			message += "Please provide a first name for the staff member. ";
			error = 1;
		}
	}
	
	if( message.length > 0 )
		alert( message );
	
	return( error );
}

function storeMandatoryFields( element, rowToAppendTo )
{
	if( element.name.indexOf( "staffGraphics" ) != -1 )
	{
		var firstName = document.createElement( 'input' );
		firstName.type = 'hidden';
		firstName.value = document.getElementById( 'firstName' ).value;
		firstName.name = element.name + '_firstName';
		
		rowToAppendTo.appendChild( firstName );
		
		document.getElementById( 'firstName' ).value = "";
	}
}

function basename(path, suffix) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ash Searle (http://hexmen.com/blog/)
    // +   improved by: Lincoln Ramsay
    // +   improved by: djmix
    // *     example 1: basename('/www/site/home.htm', '.htm');
    // *     returns 1: 'home'
 
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

function changeWelcomeText( absolutePath, webRoot, selectedLanguage )
{
	new Ajax.Request( webRoot + '/includes/ChangeWelcomeText.php', {
		method: 'get',
		parameters: 'selectedLanguage=' + selectedLanguage + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			document.getElementById( 'title' ).innerHTML = transport.responseText;
		}
	});
}

function orderByDate( absolutePath, webRoot )
{
	new Ajax.Request( webRoot + '/includes/ChangeArticleOrder.php', {
		method: 'get',
		parameters: 'order=1' + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			createNewsList( transport.responseText );
		}
	});
}

function manualOrder( absolutePath, webRoot )
{
	new Ajax.Request( webRoot + '/includes/ChangeArticleOrder.php', {
		method: 'get',
		parameters: 'order=0' + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			createNewsList( transport.responseText );
		}
	});
}

function createNewsList( contents )
{
	var ul = document.getElementById( 'boxes' );
	var listItems = ul.getElementsByTagName( 'li' );
	var len = listItems.length;
			
	for( var i = 0; i < len; i++ )
		ul.removeChild( listItems[0] );
			
	var array = contents.split( '%' );
	var len = array.length;
			
	for( var i = 0; i < len; i++ )
	{
		var temp = array[i].split( '^' );
	
		var li = document.createElement( 'li' );
				
		var div = document.createElement( 'div' );
		div.setAttribute( 'id', 'div' + temp[0] );
				
		var span = document.createElement( 'span' );
		span.className = "white";
		
		var href = document.createElement( 'a' );
		href.setAttribute( 'href', 'EditNews.php?id=' + temp[0] );
		href.appendChild( document.createTextNode( temp[1] ) );
				
		span.appendChild( href );
		div.appendChild( span );
				
		var input = document.createElement( 'input' );
		input.setAttribute( 'type', 'checkbox' );
		input.setAttribute( 'name', 'delete[]' );
		input.setAttribute( 'id', 'delete[]' );
		input.className = "tick";
		input.onclick = function() { deleteFromOrderedList(); };
				
		div.appendChild( input );
				
		var input = document.createElement( 'input' );
		input.setAttribute( 'type', 'hidden' );
		input.setAttribute( 'value', temp[2] );
				
		div.appendChild( input );
				
		li.appendChild( div );
				
		ul.appendChild( li );
	}
	
	setupDrag();
}

function updateDisplayOrder()
{
	var list = document.getElementById( 'boxes' );
	var items = list.getElementsByTagName( "li" );
	var len = items.length;
		
	var order = new Array();
		
	for( i = 0; i < len; i++ )
	{
		var html = items[i].innerHTML.toLowerCase();
		
		idxJavascript = html.indexOf( 'javascript:edit' );
		
		if( idxJavascript != -1 )
		{
			idxStartQuote = html.indexOf( "'", idxJavascript );
			idxStartQuote++;
			idxEndQuote = html.indexOf( "'", idxStartQuote );
			order.push( html.substring( idxStartQuote, idxEndQuote ) );
		}
		else
		{
			var idxId = html.indexOf( '.php?id=' );
			var idxEqual = html.indexOf( '=', idxId );
			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 ){
		}
	});
}

function openShadowbox( type, filename )
{
	Shadowbox.init();
	
	Shadowbox.open({
    	options:    {
           onFinish:  function(o){ navigateLargeImages( currentIndex ); }
        },
        type:       type,
        content:    filename,
		height:	1300,
		width: 1200
    });
}

function navigateLargeImages( indexPositionOfImage )
{
	var innerHTML = document.getElementById( 'shadowbox_content' ).innerHTML;
	var idxImage = innerHTML.indexOf( 'id="image"' );
	
	if( idxImage == -1 )
		idxImage = innerHTML.indexOf( 'id=image' );
		
	var idxSrc = innerHTML.indexOf( 'src=', idxImage );
	var idxStartQuote = innerHTML.indexOf( '"', idxSrc );
	idxStartQuote++;
	var idxEndQuote = innerHTML.indexOf( '"', idxStartQuote );
	
	innerHTML = innerHTML.substring( 0, idxStartQuote ) + shadowboxImages[indexPositionOfImage] + 
		innerHTML.substring( idxEndQuote, innerHTML.length );
	
	var previousImageIndex = 0;
	
	if( indexPositionOfImage == 0 )
		previousImageIndex = shadowboxImages.length - 1;
	else
	{
		previousImageIndex = indexPositionOfImage;
		previousImageIndex--;
	}
	
	var nextImageIndex = 0;
	
	if( indexPositionOfImage == ( shadowboxImages.length - 1 ) )
		nextImageIndex = 0;
	else
	{
		nextImageIndex = indexPositionOfImage;
		nextImageIndex++;
	}

	var idxPrevious = innerHTML.indexOf( 'previous' );
	var idxOnClick = innerHTML.indexOf( 'onclick', idxPrevious );
	idxStartQuote = innerHTML.indexOf( '"', idxOnClick );
	idxStartQuote++;
	idxEndQuote = innerHTML.indexOf( '"', idxStartQuote );
	
	innerHTML = innerHTML.substring( 0, idxStartQuote ) + "javascript:navigateLargeImages( '" + previousImageIndex + "');" + 
		innerHTML.substring( idxEndQuote, innerHTML.length );
	
	var idxNext = innerHTML.indexOf( 'next' );
	var idxOnClick = innerHTML.indexOf( 'onclick', idxNext );
	idxStartQuote = innerHTML.indexOf( '"', idxOnClick );
	idxStartQuote++;
	idxEndQuote = innerHTML.indexOf( '"', idxStartQuote );
	
	innerHTML = innerHTML.substring( 0, idxStartQuote ) + "javascript:navigateLargeImages( '" + nextImageIndex + "');" + 
		innerHTML.substring( idxEndQuote, innerHTML.length );
	
	document.getElementById( 'shadowbox_content' ).innerHTML = innerHTML;
}

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( 'boxes' );
	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].substring( 1, array[6].length - 1 );
	
	var success = deleteFromDatabase( absolutePath, webRoot, checkBoxName, tableNameToColumnName,
		thingBeingRemoved, noDataRows );
	
	if( success == 1 || typeof( success ) == "undefined" )
	{
		document.getElementById( 'div' + id ).parentNode.parentNode.removeChild
			( document.getElementById( 'div' + id ).parentNode );
	
		if( getNumRows() == 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 = "";
			}
		}
	}
}

/*
 * 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, headingRow )
{	
	// If the row should be deleted.
	if( confirm( "Are you sure you want to delete this " + thingBeingRemoved + "?" ))
	{
		// Using Ajax to delete the row from the database.
		new Ajax.Request( webRoot + '/includes/DeleteFromDatabase.php', {
			method: 'get',
			parameters: 'tableNameToColumnName=' + tableNameToColumnName + '&' + 'absolutePath=' + absolutePath,
  			onSuccess: function( transport )
			{
				if( typeof headingRow !== "undefined" )
				{
					var row = document.getElementById( headingRow );
					var start = row.rowIndex;
				
					var table = row.parentNode.parentNode;
	
					var rows = table.rows;
					var len = rows.length;
					var end = -1;
	
					for( i = start; i < len; i++ )
					{
						if( rows[i].innerHTML.indexOf( '<td>&nbsp;</td>' ) != -1 && end == -1 )
							end = i;
					}
				
					if( end == -1 )
						end = len;
				
					for( i = start; i < end; i++ )
					{
						// Delete the table row from the HTML document.
						document.getElementById( 'contentRows' ).deleteRow( start );
					
						// Decrement the value of the variable that indicates how many rows are left in the table.
						numRows--;
					}
				
					if( numRows != 0 )
					{
						if( start != 0 )
						{
							start--;
						
							if( rows[start].innerHTML.indexOf( '<td>&nbsp;</td>' ) != -1 )
							{
								document.getElementById( 'contentRows' ).deleteRow( start );
								numRows--;
							}
						}
						else if( rows[start].innerHTML.indexOf( '<td>&nbsp;</td>' ) != -1 )
						{
							document.getElementById( 'contentRows' ).deleteRow( start );
							numRows--;
						}
						else if( start + 1 != rows.length )
						{
							start++;
						
							if( rows[start].innerHTML.indexOf( '<td>&nbsp;</td>' ) != -1 )
							{
								document.getElementById( 'contentRows' ).deleteRow( start );
								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 = "";
					}
				}
				
				return( 1 );
			}
		});
	}
	else
	{
		var checkBoxGrp = document.getElementsByName( checkBoxName );
		var len = checkBoxGrp.length;
		
		for( i = 0; i < len; i++ )




		{
			if( checkBoxGrp[i].checked )
				checkBoxGrp[i].checked = false;
		}
		
		return( 0 );
	}
}

function showNewsDetail( num )
{
	document.getElementById( 'largeImage' ).src = "images/news/" + document.getElementById( 'large' + num ).value;
	document.getElementById( 'detailTitle' ).innerHTML = document.getElementById( 'title' + num ).innerHTML;
	document.getElementById( 'allCopy' ).innerHTML = document.getElementById( 'copy' + num ).value;
	
	var copyHeight = 360 - document.getElementById( 'large' + num + 'Height' ).value;
	setupPageCopy( 'maincopy', copyHeight );
}

function getNumRows()
{
	var contents = document.body.innerHTML.toLowerCase();
	
	var idxStartTable = contents.lastIndexOf( '<table' );
	var idxEndTable = contents.indexOf( '</table>', idxStartTable );
	contents = contents.substring( idxStartTable, idxEndTable );
	
	var currentIdx = 0;
	var numRows = 0;
	
	var idxDelete = contents.indexOf( 'deletefrom', currentIdx );
	
	while( idxDelete != -1 )
	{
		numRows++;
		currentIdx = idxDelete;
		currentIdx++;
		idxDelete = contents.indexOf( 'deletefrom', currentIdx );
	}
	
	currentIdx = 0;
	
	var idxBlank = contents.indexOf( '<td>&nbsp;</td>', currentIdx );
	
	while( idxBlank != -1 )
	{
		numRows++;
		currentIdx = idxBlank;
		currentIdx++;
		idxBlank = contents.indexOf( '<td>&nbsp;</td>', currentIdx );
	}
	
	return( numRows );
}

/*
 * 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 picture, download or link given the parent div to which each div is appended.
 */
 
function deleteDiv( divIdToRemove, headingRow )
{
	var ul = document.getElementById( divIdToRemove ).parentNode.parentNode;
	var li = document.getElementById( divIdToRemove ).parentNode;
	
	ul.removeChild( li );
	
	var listItems = ul.getElementsByTagName( 'li' );
	
	if( listItems.length == 0 )
	{
		var row = document.getElementById( headingRow );
		var table = row.parentNode;
		
		var rowIndex = row.rowIndex;
		table.deleteRow( rowIndex );
		table.deleteRow( rowIndex );
	}
}

function editLanguage( language )
{
	var rows = document.getElementsByTagName( 'tr' );
	
	for( var i = 0; i < rows.length; i++ )
	{
		if( rows[i].id.indexOf( 'copy_' ) != -1 )
		{
			if( rows[i].id == "copy_" + language + "_header" )
				rows[i].style.display = "";
			else
				rows[i].style.display = "none";
		}
		
		if( rows[i].id.indexOf( 'title_' ) != -1 )
		{
			if( rows[i].id == "title_" + language + "_header" )
				rows[i].style.display = "";
			else
				rows[i].style.display = "none";
		}
	}
}

function changeSiteLanguage( absolutePath, webRoot, languageSelected )
{
	new Ajax.Request( webRoot + '/includes/ChangeLanguageOfHomepageCopy.php', {
		method: 'get',
		parameters: 'language=' + languageSelected + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '~' );
			
			document.getElementById( 'title' ).innerHTML = array[0];
			document.getElementById( 'allCopy' ).innerHTML = "<p class='first'>" + array[1] + "</p>";
			document.getElementById( 'newsLink' ).innerHTML = array[2];
			
			document.getElementById( 'more' ).innerHTML = '<a href="javascript:updateCopy(' + "'more'" + ')">' +
				array[3] + "</a>";
			
			document.getElementById( 'back' ).innerHTML = '<a href="javascript:updateCopy(' + "'back'" + ')">' +
				array[4] + "</a>";
				
			document.getElementById( 'not-actual-size-text' ).innerHTML = array[5];
			
			var buttonText = array[6].split( '|' );
			
			document.getElementById( 'homeButtonTop' ).innerHTML = buttonText[0];
			document.getElementById( 'homeButtonBottom' ).innerHTML = buttonText[0];
			document.getElementById( 'searchButtonTop' ).innerHTML = buttonText[1];
			document.getElementById( 'searchButtonBottom' ).innerHTML = buttonText[1];
			document.getElementById( 'productsButtonTop' ).innerHTML = buttonText[2];
			document.getElementById( 'productsButtonBottom' ).innerHTML = buttonText[2];
			document.getElementById( 'aboutButtonTop' ).innerHTML = buttonText[3];
			document.getElementById( 'aboutButtonBottom' ).innerHTML = buttonText[3];
			document.getElementById( 'contactButtonTop' ).innerHTML = buttonText[4];
			document.getElementById( 'contactButtonBottom' ).innerHTML = buttonText[4];
			document.getElementById( 'pnsImg' ).src = "../images/PnS_" + languageSelected + ".png";
			document.getElementById( 'goButton' ).src = "../images/buttons/go_" + languageSelected + ".png";
			
			// Update global variable.
			selectedLanguage = languageSelected;
			setupPageCopy( 'main', 260 );
		}
	});
}

function sendEmail( absolutePath, webRoot )
{
	var name = "";
	var company = "";
	var telephone = "";
	var email = "";
	var interest = "";
	
	var futureGrp = document.getElementsByName( 'future' );
	var future = "";
	
	for( var i = 0; i < futureGrp.length; i++ )
	{
		if( futureGrp[i].checked )
			future = futureGrp[i].value;
	}
	
	var inputs = document.getElementsByTagName( 'input' );
	
	for( i = 0; i < inputs.length; i++ )
	{
		if( inputs[i].id == "name" && inputs[i].value != "" )
			name = inputs[i].value;
		
		else if( inputs[i].id == "company" && inputs[i].value != "" )
			company = inputs[i].value;
		
		else if( inputs[i].id == "telephone" && inputs[i].value != "" )
			telephone = inputs[i].value;
		
		else if( inputs[i].id == "email" && inputs[i].value != "" )
			email = inputs[i].value;
		
		else if( inputs[i].id == "interest" && inputs[i].value != "" )
			interest = inputs[i].value;
	}
	
	var proceed = 1;
	
	if( name.length == 0 )
	{
		proceed = 0;
		document.getElementById( 'errorName' ).style.display = "";
	}
	else
		document.getElementById( 'errorName' ).style.display = "none";
	
	if( company.length == 0 )
	{
		proceed = 0;
		document.getElementById( 'errorCompany' ).style.display = "";
	}
	else
		document.getElementById( 'errorCompany' ).style.display = "none";
	
	if( telephone.length == 0 )
	{
		proceed = 0;
		document.getElementById( 'errorTelephone' ).style.display = "";
	}
	else
		document.getElementById( 'errorTelephone' ).style.display = "none";
	
	if( email.length == 0 )
	{
		proceed = 0;
		document.getElementById( 'errorEmail' ).style.display = "";
	}
	else
		document.getElementById( 'errorEmail' ).style.display = "none";
	
	if( interest.value == 0 )
	{
		proceed = 0;
		document.getElementById( 'errorInterest' ).style.display = "";
	}
	else
		document.getElementById( 'errorInterest' ).style.display = "none";
	
	if( proceed == 0 )
	{
		document.getElementById( 'errorMessage' ).style.display = "";
		return;
	}
	
	document.getElementById( 'errorMessage' ).style.display = "none";	
	
	new Ajax.Request( webRoot + '/includes/SendEmail.php', {
		method: 'get',
		parameters: 'name=' + name + '&' + 'company=' + company + '&' + 'telephone=' + telephone + '&' + 'email=' + email +
			'&' + 'interest=' + interest + '&' + 'future=' + future,
  		onSuccess: function( transport )
		{
			document.getElementById( 'confirmation' ).style.display = "";
			
			for( var i = 0; i < futureGrp.length; i++ )
			{
				if( futureGrp[i].value = "no" )
					futureGrp[i].checked;
				
				document.getElementById( 'name' ).value = "";
				document.getElementById( 'company' ).value = "";
				document.getElementById( 'telephone' ).value = "";
				document.getElementById( 'email' ).value = "";
				document.getElementById( 'interest' ).value = "";
			}
		}
	});
}

function navigateNews( absolutePath, webRoot, currentIndex, numRowsToRetrieve, navigationDirection )
{
	if( navigationDirection == "more" )
		currentIndex = parseInt( currentIndex ) + parseInt( numRowsToRetrieve );
	else
		currentIndex = parseInt( currentIndex ) - parseInt( numRowsToRetrieve );
	
	new Ajax.Request( webRoot + '/includes/GetNewsArticles.php', {
		method: 'get',
		parameters: 'currentIndex=' + currentIndex + '&' + 'absolutePath=' + absolutePath + '&' + 'navigationDirection=' + 
			navigationDirection + '&' + 'numRowsToRetrieve=' + numRowsToRetrieve,
  		onSuccess: function( transport )
		{
			var response = transport.responseText;
			
			var containerDiv = document.getElementById( 'seasonsmain' );
			containerDiv.innerHTML = "";
				
			var temp = response.split( '#' );
			var displayMore = temp[0];
			var displayBack = temp[1];
			
			if( displayMore == 1 )
			{
				document.getElementById( 'nextNewsLink' ).href = "javascript:navigateNews( '" + absolutePath + "','" + webRoot + 
					"','" + currentIndex + "','" + numRowsToRetrieve + "','more' )";
				
				document.getElementById( 'nextNewsLink' ).style.display = "";
				document.getElementById( 'nextNewsLink' ).parentNode.style.display = "";
			}
			else
			{
				document.getElementById( 'nextNewsLink' ).style.display = "none";
				document.getElementById( 'nextNewsLink' ).parentNode.style.display = "none";
			}
			
			if( displayBack == 1 )
			{
				document.getElementById( 'previousNewsLink' ).href = "javascript:navigateNews( '" + absolutePath + "','" + 
					webRoot + "','" + currentIndex + "','" + numRowsToRetrieve + "','back' )";
				
				document.getElementById( 'previousNewsLink' ).style.display = "";
				document.getElementById( 'previousNewsLink' ).parentNode.style.display = "";
			}
			else
			{
				document.getElementById( 'previousNewsLink' ).style.display = "none";
				document.getElementById( 'previousNewsLink' ).parentNode.style.display = "none";
			}
			
			var array = temp[2].split( '|' );
			var len = array.length;
			
			for( var i = 0; i < len; i++ )
			{
				var newsNum = i + 1;
				
				var newsDetails = array[i].split( '~' );
				
				var newsDiv = document.createElement( 'div' );
				newsDiv.setAttribute( 'id', 'news-article' );
				
				var detailLink = document.createElement( 'a' );
				detailLink.setAttribute( 'id', 'link' + newsNum );
				detailLink.setAttribute( 'href', "javascript:showNewsDetail( '" + newsNum + "' )" );
				
				var newsThumbnail = document.createElement( 'img' );
				newsThumbnail.setAttribute( 'id', 'thumbnail' + newsNum );
				newsThumbnail.setAttribute( 'src', 'images/news/' + newsDetails[0] );
				
				var newsHeader = document.createElement( 'h2' );
				newsHeader.setAttribute( 'id', 'date' + newsNum );
				newsHeader.appendChild( document.createTextNode( newsDetails[1] ) );
				
				var newsTitle = document.createElement( 'div' );
				newsTitle.setAttribute( 'id', 'title' + newsNum );
				newsTitle.appendChild( document.createTextNode( newsDetails[2] ) );
				
				document.getElementById( 'large' + newsNum ).value = newsDetails[3];
				document.getElementById( 'large' + newsNum + 'Height' ).value = newsDetails[4];
				document.getElementById( 'copy' + newsNum ).value = newsDetails[5];
				
				detailLink.appendChild( newsThumbnail );
				detailLink.appendChild( newsHeader );
				detailLink.appendChild( newsTitle );
				
				newsDiv.appendChild( detailLink );
				containerDiv.appendChild( newsDiv );
				
				if( i == 0 )
				{
					document.getElementById( 'largeImage' ).src = "images/news/" + newsDetails[3];
					document.getElementById( 'detailTitle' ).innerHTML = newsDetails[2];
					document.getElementById( 'allCopy' ).innerHTML = newsDetails[5];
					
					var copyHeight = 340 - newsDetails[4];
					setupPageCopy( 'maincopy', copyHeight );
				}
			}
		}
	});
}

function openAboutShadowbox( picToDisplayIndex )
{
	currentIndex = picToDisplayIndex;
	openShadowbox( "html", document.getElementById( 'displayLargeImage' ).innerHTML );
}

function openHistoryShadowbox( picToDisplayIndex )
{
	currentIndex = picToDisplayIndex;
	openShadowbox( "html", document.getElementById( 'displayLargeImage' ).innerHTML );
}

var imageCounter = 1;
var smallImages = new Array();
var largeImages = new Array();

function setupScroller( imageString )
{
	// Stores image details - thumbnail width, thumbnail filename and large file filename
	var images = imageString.split( ',' );
	var len = images.length;
	
	if( len == 1 )
	{
		// Only one image so don't display the scroller.
		document.getElementById( 'seasonsgallerycontrol' ).style.display = "none";
		document.getElementById( 'not-actual-size-text' ).style.display = "none";
		return;
	}
	
	// The content array stores image details for the scroller. The first index stores the
	// path to the images.
	var ContentAry=['http://www.culpitt.com/images/'];
	var contentArrayIndex = 1;
	
	// Preload the images so they display quicker.
	var preloadImageObject = new Image();
	var imageUrl = new Array();
		
	for( var i = 0; i < len; i++ )
	{
		// Split on pipe to get the thumbnail width, small image filename and large image filename.
		var temp = images[i].split( '|' );
		var smallImageWidth = temp[0];
		smallImages.push( temp[1] );
		var spacer = temp[2];
		var image = temp[3];
		var largeHeight = temp[4];
		largeImages.push( image + '~' + largeHeight );	
			
		// First parameter to denote image display, second parameter is small image filename, third parameter
		// is the function to invoke on mouse rollover on scrolling image. The argument to this function is the
		// name of the large file to display. The fourth parameter is the width of the thumbnail image and the
		// fifth argument is the height of the thumbnail image.
		
		// Thumbnail images are 50 pixels high by proportional width.
		ContentAry[contentArrayIndex]=['IMG',temp[1],'showPic^|^' + temp[3] + '~' + largeHeight,smallImageWidth,50,];
		contentArrayIndex++;
		
		ContentAry[contentArrayIndex]=['IMG',spacer,'showPic^|^'+spacer,30,50,];
		contentArrayIndex++;
		
		// Ensure that both large and small images are pre-loaded.
		imageUrl.push( "http://www.culpitt.com/images/" + temp[0] );
		imageUrl.push( "http://www.culpitt.com/images/" + temp[1] );
    }
	
	// Preload images.
    len = imageUrl.length;
		
	for( var i = 0; i < len; i++ )
		preloadImageObject.src = imageUrl[i];
		
	// Initialise horizontal image scroller.
	zxcCSBanner('scroller','H','',50,ContentAry);
	
	// Start scroller.
	zxcBannerStart('scroller',2);
}

function showPic( pic, largeImageHeight ) 
{
		
	// The pic optional argument is undefined when this function is invoked to change the large image after a time delay.
	// The pic optional argument contain the name of a large file to display when the user rolls the mouse over a thumbnail
	// image in the scroller.
	
	if(typeof(largeImageHeight)=="undefined" || typeof(pic)=="undefined")
	{
		var temp = largeImages[imageCounter].split( '~' );
		var imageName = temp[0];
		largeImageHeight = temp[1];
	
		if( imageName.indexOf( '.gif' ) != -1 )
			imageCounter++;
			
			
		// Need to load the image before setting the div height to bottom align it.
		objImage = new Image();
		
		objImage.onload = function() {
			// The time interval has expired on the current image. Get the next image to display from the array of large images.
			document.getElementById( 'large' ).src = objImage.src;
			
			//set the height of the empty div above the image to centre it, (should be the height of seasonsgallery / homegallery)
			
			if( document.getElementById('large').parentNode.id == 'seasonsgallery' ){
				document.getElementById( 'centre-seasons-gallery' ).height = (300 - height) / 2 + "px";
			}
			else if( document.getElementById('large').parentNode.id == 'homegallery' ){
				document.getElementById( 'centre-home-gallery' ).style.height = (300 - largeImageHeight) + "px";
			}
		}
		
		// Remember to set source after onload becasue the image may finsih downloading before the onload event is set.
		objImage.src = "images/" + imageName;
		
		imageCounter++;
		
		// If the end of the array has been reached reset the index to start the image display from the beginning again.
		if( imageCounter == largeImages.length )
			imageCounter = 0;
	}
	else
	{
		if( pic.indexOf( '.gif' ) == -1 )
		{
			
			// Display the large image for the thumbnail image selected by the user.
			//set the height of the empty div above the image to centre it, (should be the height of seasonsgallery / homegallery;
			// Need to load the image before setting the div height to bottom align it.
			
			objImage = new Image();
			
			objImage.onload = function() {
				
				document.getElementById( 'large' ).src = objImage.src;
				
				if( document.getElementById('large').parentNode.id == 'seasonsgallery' ){
					document.getElementById( 'centre-seasons-gallery' ).height = (300 - largeImageHeight) / 2 + "px";
				}
				else if( document.getElementById('large').parentNode.id == 'homegallery' ){
					document.getElementById( 'centre-home-gallery' ).style.height = (300 - largeImageHeight) + "px";
				}
			}
			
			objImage.src = "images/" + pic;
		}
	}
	
	return false;
}

var copySections;
var count;

function setupPageCopy( divId, divHeight )
{
	try
	{
	copySections = new Array();
	count = 0;

	document.getElementById( divId ).innerHTML = "";
	
	// Get all the product copy for the page.
	var text = document.getElementById( 'allCopy' ).innerHTML;
	text = text.replace(/<P><\/P>/g,'');
	text = text.replace(/<P/g,'<p');
	text = text.replace(/<B>/g,'<b>');
	text = text.replace(/<\/B>/g,'</b>');
	text = text.replace(/<\/P/g,'</p');
	text = text.replace(/<BR/g,'<br');
	text = text.replace(/<p class="first">/g,'');
	text = text.replace(/<p>/g,'<br/><br/>');
	text = text.replace(/<\/p>/g,'');
	
	// Split the text on space to create an array of words.
	var array = text.split( ' ' );

	var copyToAdd = "";
	var paraTag = '<p class="first">';
	var idxLastBr = 0;
	var idxPreviousBr = 0;
	var brText = '<br><br>';
	var brIncrement = 8;
	var idxPeriods = new Array();
	var idxBrokenLines = new Array();
	var idxSearches = new Array();
	var textToAppend = "";
	var found = 0;
	
	for( var i = 0; i < array.length; i++ )
	{
		if( copyToAdd != "" )
		{
			var idxBr = array[i].indexOf( ' ' );
			
			if( idxBr != -1 )
			{
				if( array[i].indexOf( ' ' ) != -1 )
				{
					brText = ' ';
					brIncrement = 1;
				}
				
				idxLastBr = i;
			}
			
			// Safari fix.
			copyToAdd = copyToAdd.replace(/<br\/><br\/>(\s)<br\/><br\/>/,'<br/><br/>');
			
			document.getElementById( divId ).innerHTML = paraTag + copyToAdd + " " + array[i];
			
			// If the copy length exceeds the limit another sections has been found.
			if( document.getElementById( divId ).offsetHeight > divHeight )
			{
				var idxEndBr = array[i].indexOf( "<br" );
				
				if( idxEndBr != -1 )
				{
					document.getElementById( divId ).innerHTML = paraTag + copyToAdd + " " + array[i].substring( 0, idxEndBr ) + "</p>";
					
					/*if( window.location.href.indexOf( 'index.php' ) != -1 || window.location.href.indexOf( '.php' ) == -1 )
					{*/
					
					if( document.getElementById( divId ).offsetHeight < divHeight )
					{
						copyToAdd += " " + array[i].substring( 0, idxEndBr );
						copySections.push( paraTag + copyToAdd + "</p>" );
						idxEndBr = array[i].lastIndexOf( '>' );
						idxEndBr++;
						copyToAdd = array[i].substring( idxEndBr, array[i].length );
					}
					else
					{
						copySections.push( paraTag + copyToAdd + "</p>" );
						copyToAdd = array[i];
					}
				}
				else
				{
					found = 1;
					copySections.push( paraTag + copyToAdd + '</p>' );
					copyToAdd = array[i];
				}
				
				idxPreviousBr = i;
			}
			else
				copyToAdd += " " + array[i];
		}
		else
		{
			if( array[i].length > 0 )
				copyToAdd = array[i];
		}
	}
	
	if( copySections.length >= 1 )
	{
		var idxSpace = copyToAdd.indexOf( ' ' );
		
		if( idxSpace == -1 )
		{
			var idxEndPara = copySections[0].indexOf( '</p>' );
			
			if( idxEndPara == -1 )
				idxEndPara = copySections[0].indexOf( '</P>' );
			
			copySections[0] = copySections[0].substring( 0, idxEndPara ) + " " + copyToAdd + "</p>";
		}
		else
		{
			var idxSpace = copySections[0].lastIndexOf( ' ' );
			var idxPeriod = copySections[0].lastIndexOf( '.' );
			
			if( idxSpace > idxPeriod )
			{
				idxSpace++;
				
				var idxPara = copySections[0].lastIndexOf( '</p>' );
				
				if( idxPara == -1 )
					idxPara = copySections[0].lastIndexOf( '</P>' );
					
				//copyToAdd = copySections[0].substring( idxSpace, idxPara ) + " " + copyToAdd;
				//copySections[0] = copySections[0].substring( 0, idxSpace ) + "</p>";
				copySections.push( '<p class="first">' + copyToAdd + '</p>' );
			}
			else
			{
				idxPeriod++;
				
				if( copySections[0].substring( idxPeriod, idxPeriod+3 ) != "com" )
				{
					var idxPara = copySections[0].lastIndexOf( '</p>' );
				
					if( idxPara == -1 )
						idxPara = copySections[0].lastIndexOf( '</P>' );
					
					copyToAdd = copySections[0].substring( idxPeriod, idxPara ) + " " + copyToAdd;
					copySections[0] = copySections[0].substring( 0, idxPeriod ) + "</p>";
				}
				else
				{
					var idxHref = copySections[0].lastIndexOf( '<a' );
				
					if( idxHref == -1 )
						idxHref = copySections[0].lastIndexOf( '<A' );
					
					copySections[0] = copySections[0].substring( 0, idxHref );
					
					idxHref = copyToAdd.indexOf( '<a' );
					
					if( idxHref == -1 )
						idxHref = copyToAdd.indexOf( '<A' );
					
					if( idxHref == -1 )
						copyToAdd = "<A " + copyToAdd;
				}

				// Safari fix. Remove broken lines at beginning on spanish homepage text.
				copyToAdd = copyToAdd.replace(/^\s+|\s+$/g,"");
				copyToAdd = copyToAdd.replace(/^<br>/,'');
				copyToAdd = copyToAdd.replace(/^\s+|\s+$/g,"");
				copyToAdd = copyToAdd.replace(/^<br>/,'');
				
				if( copySections.length < 3 )
					copySections.push( '<p class="first">' + copyToAdd + '</p>' );
			}
		}
	}
	else
		copySections.push( '<p class="first">' + copyToAdd + '</p>' );
	
	// Display the first section.
	document.getElementById( divId ).innerHTML = copySections[0];

	if( copySections.length > 1 )
		document.getElementById( 'more' ).style.display = "";
	else
		document.getElementById( 'more' ).style.display = "none";
		
	document.getElementById( 'back' ).style.display = "none";
	
	}
	catch( err )
	{
		alert( err );
	}
}

function updateCopy( direction )
{
	var obj = document.getElementById( divId );
		
	if( direction == "more" )
	{
		// Get the next section and update the innerHTML.
		count++;
		
		obj.innerHTML = copySections[count];
		
		if( count + 1 == copySections.length )
		{
			// All the copy has been displayed hide the more button and make sure that the height of
			// the div is fixed in case the copy is too short.
			document.getElementById( 'more' ).style.display = "none";
		}
			
		// Ensure that the back button is displayed.
		document.getElementById( 'back' ).style.display = "";
	}
	else if( direction == "back" )
	{
		// Get the previous set of copy displayed.
		count--;
		
		// Update the HTML to set the copy.
		obj.innerHTML = copySections[count];
		
		// If the user has navigated back to the first section of copy then hide the back button.
		if( count == 0 )
			document.getElementById( 'back' ).style.display = "none";
			
		// Ensure that the more button is displayed.
		document.getElementById( 'more' ).style.display = "";
	}
}

function preloadMenuImages()
{
	var dir = "images/layout/menu/";
	var menuImages = new Array( "search_" + selectedLanguage + ".jpg", "searchOver_" + selectedLanguage + ".jpg",
	"home_" + selectedLanguage + ".jpg", "homeOver_" + selectedLanguage + ".jpg", "ourproducts_" + selectedLanguage + ".jpg", 
	"ourproductsOver_" + selectedLanguage + ".jpg", "aboutus_" + selectedLanguage + ".jpg", "aboutusOver_" + selectedLanguage +
	".jpg", "contact_" + selectedLanguage + ".jpg", "contactOver_" + selectedLanguage + ".jpg" );

	var menuImagesLen = menuImages.length;

	for( var i = 0; i < menuImagesLen; i++ )
	{
		var preloadImageObject = new Image();
		preloadImageObject.src = dir + menuImages[i];
	}
}

function updateTermsCopy( direction )
{
	if( direction == "more" )
		copyCounter++;
	else
		copyCounter--;
	
	if( copyCounter == 0 )
		document.getElementById( 'back' ).style.display = "none";
	else
		document.getElementById( 'back' ).style.display = "";
	
	if( copyCounter == copyTotal )
		document.getElementById( 'more' ).style.display = "none";
	else
		document.getElementById( 'more' ).style.display = "";
		
	document.getElementById( 'main-large' ).innerHTML = document.getElementById( 'copy' + copyCounter ).innerHTML;
}

function showSearchBox() {
	document.getElementById('searchBox').style.visibility = 'visible';	
}

function hideSearchBox() {
	document.getElementById('searchBox').style.visibility = 'hidden';	
}


}
catch( err )
{
	alert( err );
}