/* ******************  Case Studies  ************** */
// The preview value. Boolean. If 1 then we're on the case studies intro page.
var preview;

// (If we're previewing only) The GET parentID value.
var previewParentID = 0;

// (If we're previewing only) The GET categoryID value.
var previewCategoryID = 0;

// The base (results) AJAX div that we'll append to
var ajaxResultsDiv = null;

// The base (details) AJAX div that we'll append to
var ajaxDetailsDiv = null;

// The XHTML AJAX form
var ajaxForm = null;

// The div that surrounds the XHTML AJAX form {@link ajaxForm}
var ajaxFormDiv = null;

// The results found by the search
var results = new Array();

var attributes = new Array('casestudyID',
							'title',
							'metaDescription',
							'metaKeywords',
							'client',
							'requirement',
							'aboutTheClient',
							'solution',
							'impact',
							'whatTheClientSays',
							'file1',
							'file2',
							'file3',
							'file4',
							'file5',
							'file6',
							'dspListImg',
							'dspPrimaryImg',
							'dspSuppImg',
							'dspLogoImg');

// Initialise the case studies AJAX functionality
function initCaseStudies(previewFlag, previewParentIDVal, previewCategoryIDVal)
{
	// Are we previewing?
	preview 			= previewFlag;
	previewParentID 	= previewParentIDVal;
	previewCategoryID 	= previewCategoryIDVal;
	
	// Init surrounding div for the results list
	ajaxResultsDiv = document.getElementById('ajaxResultsDiv');

	// Init surrounding div for the details page
	ajaxDetailsDiv = document.getElementById('ajaxDetailsDiv');

	_buildAjaxForm();

}

function _getTopLevelCategories(type, action)
{
	var categories = new Array();

	// Get the top level categories (basic only)
	var xmlHttp = getXmlHttpRequest();
	var url = '/casestudies/ajax/gettoplevelcategories/searchType/' + type.toLowerCase();
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4){
			if (xmlHttp.status == 200){

				// Fetch the response tag
				var rspXML = xmlHttp.responseXML;

				// Now add the newly found options
				var category = rspXML.getElementsByTagName('category');
				for(var i = 0, j = category.length; i < j; i++){

					var categoryID 	= category[i].getElementsByTagName('categoryID')[0].firstChild.nodeValue;
					var title 		= category[i].getElementsByTagName('title')[0].firstChild.nodeValue;

					categories[i] = new Array(categoryID, title);
				}

				switch(action){
					default:
						_buildTopLevelCategoriesHTML(type, categories);
						break;
				}

			}else{
				// Non 200 HTTP status
			}
		}
	}
}

function _getChildCategories(parentID, action, element)
{
	var categories = new Array();

	// Get the top level categories (basic only)
	var xmlHttp = getXmlHttpRequest();
	var url = '/casestudies/ajax/getchildcategories/parentID/' + parentID;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4){
			if (xmlHttp.status == 200){

				// Fetch the response tag
				var rspXML = xmlHttp.responseXML;

				// Now add the newly found options
				var category = rspXML.getElementsByTagName('category');
				for(var i = 0, j = category.length; i < j; i++){

					var categoryID 	= category[i].getElementsByTagName('categoryID')[0].firstChild.nodeValue;
					var title 		= category[i].getElementsByTagName('title')[0].firstChild.nodeValue;

					categories[i] = new Array(categoryID, title);
				}

				switch(action){
					default:
						_buildChildCategoriesHTML(categories, parentID, element);
					break;
				}
				

			}else{
				// Non 200 HTTP status
			}
		}
	}
}

function _buildAjaxForm()
{
	// Create the XHTML AJAX form
	ajaxForm = document.createElement('form');

	// Create the div that surrounds the XHTML AJAX form
	ajaxFormDiv = document.createElement('div');
	ajaxFormDiv.className = 'csAjaxForm';

	// Get the top level categories
	_getTopLevelCategories('Basic');
	// We're not using the advanced options anymore
	//setTimeout('_getTopLevelCategories(\'Advanced\')', 500);

	// Add the form's div to the base div
	ajaxResultsDiv.appendChild(ajaxFormDiv);
}

function _buildTopLevelCategoriesHTML(type, categories)
{

	if(type == 'Advanced'){
		fieldsetClass = 'right';
	}else{
		fieldsetClass = 'left';
	}

	// Basic Search fieldset
	var fieldset = document.createElement('fieldset');
	fieldset.className = fieldsetClass;
	var legend = document.createElement('legend');

	// SH added to add a span tage within the legend
	var legendSpan = document.createElement('span');
	//legendSpan.innerHTML = type + ' Search';
	legendSpan.innerHTML = 'search options';
	legend.appendChild(legendSpan);


	fieldset.appendChild(legend);
	var clearAll = document.createElement('br');
	clearAll.className = 'legendClear';

	fieldset.appendChild(clearAll);


	// Now add the newly found options
	for(var i = 0, j = categories.length; i < j; i++){

		categoryID 	= categories[i][0];
		categoryTitle 	= categories[i][1];

		var ul = document.createElement('ul');

		// Set the class var to null
		var ulClass = null;

		// If the counter is 0
		// then set the class to top
		if(i == 0){
			ulClass = 'top';
		}

		// Else set the class to bottom
		if(i == 1){
			ulClass = 'bottom';
		}

		ul.className = ulClass;

		var li = document.createElement('li');

		var label = document.createElement('label');
		label.id = 'label' + categoryID;
		label.htmlFor = 'dd' + categoryID;
		label.innerHTML = categoryTitle;

		li.appendChild(label);

		_getChildCategories(categoryID, 'default', li);

		ul.appendChild(li);
		fieldset.appendChild(ul);

	}
	
	ajaxForm.appendChild(fieldset);
	
	// Add the form to the form's div
	ajaxFormDiv.appendChild(ajaxForm);

	// Add the form's div to the base div
	ajaxResultsDiv.appendChild(ajaxFormDiv);
	
	_startFilter();
	
}

function _startFilter()
{
	if (previewParentID == null ){
		setTimeout("_startFilter()", 200);
	}else{
		if (previewParentID != 0){
			selects = document.getElementsByTagName('select');
			if(selects.length){
				_performFilter('dd' + previewParentID);
			} else {
				setTimeout("_startFilter()", 200);
			}
		}
	}
}

function _buildChildCategoriesHTML(categories, parentID, element)
{
	var select = document.createElement('select');
	select.id = 'dd' + parentID;
	select.onchange	= _performFilter;

	// 'Please select' option
	var option = document.createElement('option');
	option.value = -1;
	option.innerHTML = '-- SELECT --';
	select.appendChild(option);
	
	// Now add the newly found options
	for(var i = 0, j = categories.length; i < j; i++){

		categoryID 		= categories[i][0];
		categoryTitle 	= categories[i][1];

		var option = document.createElement('option');
		option.value = categoryID;
		option.innerHTML = categoryTitle;
		if ( (previewParentID != 0) && (previewCategoryID != 0) ){
			if ( (previewParentID == parentID) && (previewCategoryID == categoryID) ){
				option.selected = true;
			}
		}
		select.appendChild(option);
	}
	
	element.appendChild(select);
}

function _performFilter(event)
{
	if(typeof(event) !== 'string'){
		// FF
		try {
			parentID 			= event.target.id;
			categoryIDSelected 	= event.target.options[event.target.selectedIndex].value;
		}
		catch(e){
			// IE
			parentID 			= this.id;
			categoryIDSelected 	= this.options[this.selectedIndex].value;
		}
	} else {
		parentID = event;
		selects = document.getElementsByTagName('select');
		for(var i=0,j=selects.length;i<j;i++){
			if(selects[i].id == parentID){
				categoryIDSelected = selects[i].options[selects[i].selectedIndex].value
				break;
			}
		}		
		
	}

	if(1 == preview){
		url = "?parentID=" + parentID.replace('dd','') + "&categoryID=" + categoryIDSelected;
		window.location = '/casestudies/' + url;
	} else {
	
		// Reset the current results
		//ajaxResultsDiv.innerHTML = '';
	
		// Find all the select elements from the form to get each value
		selects = document.getElementsByTagName('select');
	
		params = '';
		for(var i = 0, j = selects.length; i < j; i++){
			params += selects[i].id.replace('dd','') + '/' + selects[i].value + '/';
		}
	
		// Get results. Search by category
		var xmlHttp = getXmlHttpRequest();
		var url = '/casestudies/ajax/searchbycategory/' + params;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4){
				if (xmlHttp.status == 200){
	
					// Fetch the response tag
					var rspXML = xmlHttp.responseXML;
	
					// Fetch the error tag
					var errTag = rspXML.getElementsByTagName('error');
	
					// Fetch the no search tag
					var noSearchTag = rspXML.getElementsByTagName('nosearch');
	
					// Was there an error? I.e  no results found?
					if (errTag.length != 0){
	
						// Blank the results array in case it's been populated before
						results	= new Array();
						// Set a marker for 'no results'
						var noResults = true;
						// Remove existing results
						removeExistingResults();
	
					// Has the user performed a valid search
					}else if(noSearchTag.length != 0){
	
						// Blank the results array in case it's been populated before
						results	= new Array();
						// Set a marker for 'invalid search'
						var invalidSearch = true;
						// Remove existing results
						removeExistingResults();
	
					}else{
	
						// Blank the results ready for new population
						results	= new Array();
	
						// Now add the newly found options
						var caseStudy = rspXML.getElementsByTagName('caseStudy');
	
						var u = 0;
						var next = 0;
						var classname = '';
	
						for(var i = 0, j = caseStudy.length; i < j; i++){
							if (next == u) {
								className = 'second';
							}else{
								if((u % 3) == 0){
									className = 'first';
									next = (u + 1);
								}else{
									className = null;
									next = null;
								}
							}
							variables = new Array('casestudyID','categoryID','title','metaDescription','metaKeywords','client',
													'requirement',
													'aboutTheClient',
													'solution',
													'impact','whatTheClientSays','file1','file2','file3','file4','file5','file6');
							for(var ii = 0,jj = variables.length; ii<jj ; ii++){
								try {
									eval(variables[ii] + ' = caseStudy[i].getElementsByTagName(variables[ii])[0].firstChild.nodeValue;');
								} catch(e) {
									eval(variables[ii] + '= "";');
								}
							}
							/*
							casestudyID 		= caseStudy[i].getElementsByTagName('casestudyID')[0].firstChild.nodeValue;
							categoryID 			= caseStudy[i].getElementsByTagName('categoryID')[0].firstChild.nodeValue;
							title 				= caseStudy[i].getElementsByTagName('title')[0].firstChild.nodeValue;
							metaDescription 	= caseStudy[i].getElementsByTagName('metaDescription')[0].firstChild.nodeValue;
							metaKeywords 		= caseStudy[i].getElementsByTagName('metaKeywords')[0].firstChild.nodeValue;
							client 				= caseStudy[i].getElementsByTagName('client')[0].firstChild.nodeValue;
							requirement 		= caseStudy[i].getElementsByTagName('requirement')[0].firstChild.nodeValue;
							aboutTheClient 		= caseStudy[i].getElementsByTagName('aboutTheClient')[0].firstChild.nodeValue;
							solution 			= caseStudy[i].getElementsByTagName('solution')[0].firstChild.nodeValue;
	
							try{
								impact 				= caseStudy[i].getElementsByTagName('impact')[0].firstChild.nodeValue;
							} catch(e) {
								impact = '';
							}
							whatTheClientSays 	= caseStudy[i].getElementsByTagName('whatTheClientSays')[0].firstChild.nodeValue;
							file1 				= caseStudy[i].getElementsByTagName('file1')[0].firstChild.nodeValue;
							file2 				= caseStudy[i].getElementsByTagName('file2')[0].firstChild.nodeValue;
							file3 				= caseStudy[i].getElementsByTagName('file3')[0].firstChild.nodeValue;
							file4 				= caseStudy[i].getElementsByTagName('file4')[0].firstChild.nodeValue;
							file5 				= caseStudy[i].getElementsByTagName('file5')[0].firstChild.nodeValue;
							file6 				= caseStudy[i].getElementsByTagName('file6')[0].firstChild.nodeValue;
							*/
							link 				= document.createElement('a');
	
							//link.href = '#';
							link.id = 'casestudy' + casestudyID;
							link.href = 'javascript:_displayCaseStudy(' + casestudyID + ');';
							link.onmouseover = link.style.cursor = 'pointer';
							link.innerHTML = subStringText(title, 0, 35);
	
							// Append to the results div
							results[i] 		= new Array();
							results[i][0] 	= casestudyID;
							results[i][1] 	= categoryID;
							results[i][2] 	= title;
							results[i][3] 	= metaDescription;
							results[i][4] 	= metaKeywords;
							results[i][5] 	= client;
							results[i][6] 	= requirement;
							results[i][7] 	= aboutTheClient;
							results[i][8] 	= solution;
							results[i][9] 	= impact;
							results[i][10] 	= whatTheClientSays;
							results[i][11] 	= file1;
							results[i][12] 	= file2;
							results[i][13] 	= file3;
							results[i][14] 	= file4;
							results[i][15] 	= file5;
							results[i][16] 	= file6;
							results[i][17] 	= className;
							results[i][18] 	= link;
	
							u++;
						}
	
					}
	
					switch(results.length){
						case 0:
							if(noResults){
	
								// Remove existing results
								removeExistingResults();
	
								var div = document.createElement('div');
								div.id = 'caseStudyResults';
	
								var h2 = document.createElement('h2');
								h2.innerHTML = 'No Case Studies Found';
								div.appendChild(h2);
	
								var p = document.createElement('p');
								p.innerHTML = 'Please add some more parameters to your search!';
								div.appendChild(p);
	
								ajaxResultsDiv.appendChild(div);
							}
							break;
						default:
	
							// Remove existing results
							removeExistingResults();
	
							// Create the results div
							var div = document.createElement('div');
							div.id = 'caseStudyResults';
	
							// Create the h2
							var h2 = document.createElement('h2');
							//h2.innerHTML = caseStudy.length + ' Case Studies Found';
							h2.innerHTML = 'Your Results (' + caseStudy.length + ')';
							div.appendChild(h2);
	
							// Create the ul
							var ul = document.createElement('ul');
	
							// Set the counter
							var counter = 1;
	
							// Loop through results
							for(var x = 0, y = results.length; x < y; x++){
	
								// Create the image holder div
								var imageHolder = document.createElement('div');
								imageHolder.className = 'image';
	
								// Create the client name holder div
								var clientName = document.createElement('div');
								clientName.className = 'client';
	
								// Create the client image
								var clientImage = document.createElement('img');
	
								// Work out the class we need to
								// apply to the li
								if(1 == counter){
									liClass = 'first';
								}
								if(2 == counter){
									liClass = 'second';
								}
								if(3 == counter){
									liClass = 'third';
								}
								if(4 == counter){
									liClass = 'fourth';
								}
	
								// Create the li
								var li = document.createElement('li');
	
								// Client list image
								clientImage.src='/public/backend/uploads/casestudies/item/primary/' + results[x][0] + '/file1/' + results[x][11] + '';
	
								// Create the image link
								imageLink 				= document.createElement('a');
								imageLink.id 			= results[x][0];
								//imageLink.onclick 		= _displayCaseStudy;
								imageLink.href 			= 'javascript:_displayCaseStudy(' + results[x][0] + ');';
								imageLink.onmouseover 	= link.style.cursor = 'pointer';
	
								// Append the image to the link
								imageLink.appendChild(clientImage);
	
								// Client name
								clientName.appendChild(results[x][18])
	
								// Append the client image to the holder
								imageHolder.appendChild(imageLink);
	
								// Append the image and the name to the li
								li.appendChild(imageHolder);
								li.appendChild(clientName);
	
								// Append the li to the ul
								ul.appendChild(li);
	
								// Apply the li class
								li.className = liClass;
	
								if(4 == counter){
									counter = 1;
								}else{
									// Add one to the counter
									counter ++;
								}
	
							}// End of for
	
							clearMe = document.createElement('div');
							clearMe.className = 'clearMe';
	
							ul.appendChild(clearMe);
							div.appendChild(ul);
	
							// cant-find-the-case-study
							a2 = document.createElement('a');
							a2.innerHTML = 'click here to contact us';
							a2.href = '/contact';
	
							div2 = document.createElement('div');
							div2.className = 'cant-find-the-case-study';
							div2.innerHTML = 'If you cant find the case study you\'re looking for then ';
							div2.appendChild(a2);
							div.appendChild(div2);
	
							// Create the footer div
							var resultsFooter = document.createElement('div');
							resultsFooter.className = 'resultsFooter';
							resultsFooter.id = 'resultsFooter';
	
							ajaxResultsDiv.appendChild(div);
							ajaxResultsDiv.appendChild(resultsFooter);
	
							break;
					}
	
				}else{
					// Non 200 HTTP status
				}
			}
		}
	}
}

function _displayCaseStudy(caseStudyID)
{
	_getCaseStudy(caseStudyID, 'default');
}

function _getCaseStudy(caseStudyID, action)
{
	var caseStudyDetails = new Array();

	// Get the top level caseStudy (basic only)
	var xmlHttp = getXmlHttpRequest();
	var url = '/casestudies/ajax/getcasestudy/caseStudyID/' + caseStudyID;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4){
			if (xmlHttp.status == 200){

				// Fetch the response tag
				var rspXML = xmlHttp.responseXML;

				// Now add the newly found options
				var caseStudy = rspXML.getElementsByTagName('caseStudy');
				for(var i = 0, j = caseStudy.length; i < j; i++){
					for(var ii = 0, jj = attributes.length; ii < jj; ii++){
						try{
							caseStudyDetails[ii] = caseStudy[i].getElementsByTagName(attributes[ii])[0].firstChild.nodeValue;
						} catch (e) {
							caseStudyDetails[ii] = '';
						}
					}
				}

				switch(action){
					default:
						_buildCaseStudyHTML(caseStudyDetails);
						break;
				}

			}else{
				// Non 200 HTTP status
			}
		}
	}
}

function _buildCaseStudyHTML(caseStudy)
{
	// Hide the results div
	hideDiv(ajaxResultsDiv.id);
	
	// Hide side bar stuff
	hideDiv('page-referrals');
	hideDiv('page-notices');

	// Show the details div
	showDiv(ajaxDetailsDiv.id);

	var contentDiv = document.createElement('div');
	contentDiv.className = 'content';

	// Extract elements from the array
	for(var i = 0, j = attributes.length; i < j; i++){
		eval(attributes[i] + ' = caseStudy[' + i + '];');
	}

	// Primary image
	if (1 == dspPrimaryImg){
		
		// Show the main image div
		showDiv('cs-main-image');
		
		var mainImageDiv = document.getElementById('cs-main-image');
		
		var a = document.createElement('a');
		a.href = '/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file2/' + file2;
		a.rel = 'lightbox';

		var img = document.createElement('img');
		img.alt = title;
		img.className = 'caseStudyImgPrim';
		img.src = '/public/backend/uploads/resizeImage.php?image=/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file2/' + file2 + '&max_width=225&max_height=2000';

		//a.appendChild(img);
		mainImageDiv.appendChild(img);
	}

	// Client
	h1 = document.createElement('h1');
	h1.innerHTML = title;
	contentDiv.appendChild(h1);
	
	// Logo
	if (1 == dspLogoImg){
		var img = document.createElement('img');
		img.alt = client + ' logo';
		img.className = 'caseStudyImgLogo';
		img.src = '/public/backend/uploads/resizeImage.php?image=/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file6/' + file6 + '&max_width=185&max_height=185';
		contentDiv.appendChild(img);
	}

	// About The Client
	if (aboutTheClient.length > 0){
		h2 = document.createElement('h2');
		h2.innerHTML = 'About The Client';
		contentDiv.appendChild(h2);

		div = document.createElement('div');
		div.innerHTML = aboutTheClient;
		contentDiv.appendChild(div);
		
		// Spacer
		spacerTwo = document.createElement('div');
		spacerTwo.className = 'spacerTwo';
		contentDiv.appendChild(spacerTwo);
	}

	// Requirement
	if (requirement.length > 0){
		h2 = document.createElement('h2');
		h2.innerHTML = 'Requirement';
		//ajaxDetailsDiv.appendChild(h2);
		contentDiv.appendChild(h2);

		div = document.createElement('div');
		div.innerHTML = requirement;
		contentDiv.appendChild(div);
		
		// Spacer
		spacerTwo = document.createElement('div');
		spacerTwo.className = 'spacerTwo';
		contentDiv.appendChild(spacerTwo);
	}

	// Solution
	if (solution.length > 0){
		h2 = document.createElement('h2');
		h2.innerHTML = 'Solution';
		contentDiv.appendChild(h2);

		div = document.createElement('div');
		div.innerHTML = solution;
		contentDiv.appendChild(div);
		
		// Spacer
		spacerTwo = document.createElement('div');
		spacerTwo.className = 'spacerTwo';
		contentDiv.appendChild(spacerTwo);
	}

	// Impact
	if (impact.length > 0){
		h2 = document.createElement('h2');
		h2.innerHTML = 'Impact';
		contentDiv.appendChild(h2);

		div = document.createElement('div');
		div.innerHTML = impact;
		contentDiv.appendChild(div);
		
		// Spacer
		spacerTwo = document.createElement('div');
		spacerTwo.className = 'spacerTwo';
		contentDiv.appendChild(spacerTwo);
	}

	// What The Client Says
	if (whatTheClientSays.length > 0){
		
		// Show the "What they said" div
		showDiv('cs-what-they-said');
		
		var whatTheySaid = document.getElementById('cs-what-they-said');
		
		var divWhiteBox = document.createElement('div');
		divWhiteBox.className = 'left-box white';
		
		var divLeftBoxHeader = document.createElement('div');
		divLeftBoxHeader.className = 'left-box-header';
		divLeftBoxHeader.innerHTML = 'What they said';
		
		var divLeftBoxContent = document.createElement('div');
		divLeftBoxContent.className = 'left-box-content3';
		
		var divLeftBoxContentQuoteWrapper = document.createElement('div');
		divLeftBoxContentQuoteWrapper.className = 'quote-wrapper';
		
		var img = document.createElement('img');
		img.alt = 'Quote mark';
		img.className = 'right-quote';
		img.src = '/public/frontend/i/quote-right.gif';
		
		divLeftBoxContentQuoteWrapper.innerHTML = whatTheClientSays;
		divLeftBoxContentQuoteWrapper.appendChild(img);
		
		divLeftBoxContent.appendChild(divLeftBoxContentQuoteWrapper);
		divWhiteBox.appendChild(divLeftBoxHeader);
		divWhiteBox.appendChild(divLeftBoxContent);
		whatTheySaid.appendChild(divWhiteBox);
		
	}

	ajaxDetailsDiv.appendChild(contentDiv);

	// Supporting images
	if (1 == dspSuppImg){

		var div = document.createElement('div');
		div.id = 'caseStudyImgsSupp';

		var imagesH2 = document.createElement('h2');
		imagesH2.innerHTML = 'Take a look';

		div.appendChild(imagesH2);

		if(file3.length > 0){
			var innerDiv = document.createElement('div');
			innerDiv.className = 'one';

			var a = document.createElement('a');
			a.href = '/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file3/' + file3;
			a.rel = 'lightbox';

			var img = document.createElement('img');
			img.alt = title;
			img.src = '/public/backend/uploads/resizeImage.php?image=/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file3/' + file3 + '&max_width=150&max_height=150';
			a.appendChild(img);

			innerDiv.appendChild(a);
			div.appendChild(innerDiv);
		}
		if(file4.length > 0){
			var innerDiv = document.createElement('div');
			innerDiv.className = 'two';

			var a = document.createElement('a');
			a.href = '/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file4/' + file4;
			a.rel = 'lightbox';

			var img = document.createElement('img');
			img.alt = title;
			img.src = '/public/backend/uploads/resizeImage.php?image=/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file4/' + file4 + '&max_width=150&max_height=150';
			a.appendChild(img);

			innerDiv.appendChild(a);
			div.appendChild(innerDiv);
		}
		if(file5.length > 0){
			var innerDiv = document.createElement('div');
			innerDiv.className = 'three';

			var a = document.createElement('a');
			a.href = '/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file5/' + file5;
			a.rel = 'lightbox';

			var img = document.createElement('img');
			img.alt = title;
			img.src = '/public/backend/uploads/resizeImage.php?image=/public/backend/uploads/casestudies/item/primary/' + casestudyID + '/file5/' + file5 + '&max_width=150&max_height=150';
			a.appendChild(img);

			innerDiv.appendChild(a);
			div.appendChild(innerDiv);
		}

		ajaxDetailsDiv.appendChild(div);
	}

	var div = document.createElement('div');
	div.className = 'clearMe';
	ajaxDetailsDiv.appendChild(div);

	// Require more information?
	showDiv('cs-more-info');
	var moreInfoDiv = document.getElementById('cs-more-info');
	
	var divWhiteBox = document.createElement('div');
	divWhiteBox.className = 'left-box white';
	
	var divLeftBoxHeader = document.createElement('div');
	divLeftBoxHeader.className = 'left-box-header';
	divLeftBoxHeader.innerHTML = 'Require more information?';
	
	var divLeftBoxContent = document.createElement('div');
	divLeftBoxContent.className = 'left-box-content';
	
	var div = document.createElement('div');
	div.innerHTML = 'For more information on this case study <a href="/contact">get in touch</a> with us, alternatively <a onmouseover="this.style.cursor=\'pointer\';" onclick="_viewResults();">go back to the case studies</a>.';
	divLeftBoxContent.appendChild(div);
	
	divWhiteBox.appendChild(divLeftBoxHeader);
	divWhiteBox.appendChild(divLeftBoxContent);
	moreInfoDiv.appendChild(divWhiteBox);
	

	// Back link
	p = document.createElement('p');
	var link = document.createElement('a');
	link.id = casestudyID;
	link.onclick = _viewResults;
	link.style.cursor = 'pointer';
	link.innerHTML = '&lt;&lt; back to list page';
	p.appendChild(link);
	contentDiv.appendChild(p);

	// Footer for photos
	var photosFooterDiv = document.createElement('div');
	photosFooterDiv.className = 'photos-footer';

	
	ajaxDetailsDiv.appendChild(photosFooterDiv);
}

function _viewResults()
{
	// Remove the details div
	ajaxDetailsDiv.innerHTML = '';
	hideDiv(ajaxDetailsDiv.id);
	
	// Hide the main image div
	document.getElementById('cs-main-image').innerHTML = '';
	hideDiv('cs-main-image');
	
	// Hide "What they said" div
	document.getElementById('cs-what-they-said').innerHTML = '';
	hideDiv('cs-what-they-said');
	
	// Hide the Require more information? div
	document.getElementById('cs-more-info').innerHTML = '';
	hideDiv('cs-more-info');
	
	// Show side bar stuff
	showDiv('page-referrals');
	showDiv('page-notices');
	
	// Show the results div
	showDiv(ajaxResultsDiv.id);
}

function getXmlHttpRequest()
{
	var xmlHttp = null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		// Internet Explorer
		// Switch depending on JavaScript technology installed in Internet Explorer.
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function subStringText(subject,
						subStrStart,
						subStrLimit)
{
	if(subject.length > subStrLimit){
		precis = subject.substring(subStrStart, subStrLimit);
		if(last_space = strrpos(precis, ' ')){
			precis = precis.substring(subStrStart, last_space);
			precis += "&hellip;";
		}
	} else {
		precis = subject;
	}

	return precis;
}

function strrpos( haystack, needle, offset){
    var i = haystack.lastIndexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

function hideDiv(elementID)
{
	if (document.getElementById) { // DOM3 = IE5, NS6
		//document.getElementById(elementID).style.position = 'absolute';
		//document.getElementById(elementID).style.left = '-99999px';
		document.getElementById(elementID).style.display = 'none';
	}
}

function showDiv(elementID)
{
	if (document.getElementById) { // DOM3 = IE5, NS6
		//document.getElementById(elementID).style.position = 'relative';
		//document.getElementById(elementID).style.left = null;
		document.getElementById(elementID).style.display = 'block';
	}
}

function removeDiv(parentElementID, elementToRemoveID)
{
	if (document.getElementById) { // DOM3 = IE5, NS6
		parentElement = document.getElementById(parentElementID);
		if(parentElement){
			elementToRemove = document.getElementById(elementToRemoveID);
			if (elementToRemove){
				parentElement.removeChild(elementToRemove);
			}
		}
	}
}
function addDiv(parentElementID, elementID)
{
	if (document.getElementById) { // DOM3 = IE5, NS6
		parentElement = document.getElementById(parentElementID);
		if(parentElement){
			element = document.getElementById(elementID);
			if (element){

			}else{
				div = document.createElement('div');
				div.id = elementID;
				parentElement.appendChild(div);

				return div;
			}
		}
	}
}

function removeExistingResults()
{
	existingResults = document.getElementById('caseStudyResults');
	if(existingResults){
		ajaxResultsDiv.removeChild(existingResults);
	}

	resultsFooter = document.getElementById('resultsFooter');
	if(resultsFooter){
		ajaxResultsDiv.removeChild(resultsFooter);
	}
}