/* ******************  Case Studies  ************** */

// 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();

function changeText(theElement,fprice){
	//set the default price as it is in the HTML
	//alert(theElement);
	var s = document.getElementById(theElement);
	var selText = s.options[s.selectedIndex].text;
	// find the position of the last 
	var tstart = selText.indexOf("£")+1;
	
	if (!tstart) {
		//return(false);
		fprice = parseFloat(fprice);
	}
	
	var tend = selText.indexOf(")");
	var tprice = selText.substring(tstart,tend);
	if (selText.indexOf("(+") != -1) {
		fprice = parseFloat(tprice) + parseFloat(fprice);
	} 
	else if (selText.indexOf("(-") != -1){
		fprice = parseFloat(fprice) - parseFloat(tprice);
	}
	else {
		//alert("THE PRICE = "+tprice);
		//fprice = parseFloat(tprice);
		fprice = parseFloat(fprice);
	}	
	//convert it to float (2 decimal places)
	fprice = fprice.toFixed(2);
	document.getElementById('priceDisplay').innerHTML = fprice;
}

// Initialise the case studies AJAX functionality
function initCaseStudies()
{
	// 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');
	setTimeout('_getTopLevelCategories(\'Advanced\')', 500);

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

function _buildTopLevelCategoriesHTML(type, categories)
{
	// Basic Search fieldset
	var fieldset = document.createElement('fieldset');
	var legend = document.createElement('legend');
	legend.innerHTML = type + ' Search';
	fieldset.appendChild(legend);

	// 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');
		var li = document.createElement('li');

		var label = document.createElement('label');
		label.id = categoryID;
		label.htmlFor = 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);
}

function _buildChildCategoriesHTML(categories, parentID, element)
{
	var select = document.createElement('select');
	select.id = 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;
		select.appendChild(option);
	}

	element.appendChild(select);
}

function _performFilter(event)
{
	// 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;
	}

	// 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 + '/' + 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 = '';

					//alert(caseStudy.length);

					for(var i = 0, j = caseStudy.length; i < j; i++){
						//alert(i + ' = ' + j);
						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 {
								//alert(variables[ii] + ' = caseStudy[i].getElementsByTagName(\''+variables[ii]+'\')[0].firstChild.nodeValue;');
								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 = '';
						}
						alert("post 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');

						//alert('worked');
						//link.href = '#';
						link.id = casestudyID;
						link.onclick = _displayCaseStudy;
						link.onmouseover = link.style.cursor = 'pointer';
						link.innerHTML = subStringText(title, 0, 35);

						//alert('pre array def');

						// 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;

						//alert('post array def');

						u++;
					}
					//alert('after for each');

				}

				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 you search!';
							div.appendChild(p);

							ajaxResultsDiv.appendChild(div);
						}
						break;
					default:

						// Remove existing results
						removeExistingResults();

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

						var h2 = document.createElement('h2');
						h2.innerHTML = caseStudy.length + ' Case Studies Found';
						div.appendChild(h2);

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

						// Loop through results
						for(var x = 0, y = results.length; x < y; x++){
							var li = document.createElement('li');
							li.className = results[x][17];
							if (results[x][11].length > 0){
								li.style.background = 'url(\'/public/backend/uploads/casestudies/item/primary/' + results[x][0] + '/file1/' + results[x][11] + '\')';
							}else{
								li.style.background = '#e7e7df';
							}
							li.appendChild(results[x][18]);
							ul.appendChild(li);
						}

						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);

						ajaxResultsDiv.appendChild(div);

						break;
				}

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

function _displayCaseStudy(event)
{
	// FF
	try {
		caseStudyID = event.target.id;
	}
	catch(e){
		// IE
		caseStudyID = this.id;
	}

	_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++){

					caseStudyDetails[0] 	= caseStudy[i].getElementsByTagName('casestudyID')[0].firstChild.nodeValue;
					caseStudyDetails[2] 	= caseStudy[i].getElementsByTagName('title')[0].firstChild.nodeValue;
					caseStudyDetails[3] 	= caseStudy[i].getElementsByTagName('metaDescription')[0].firstChild.nodeValue;
					caseStudyDetails[4] 	= caseStudy[i].getElementsByTagName('metaKeywords')[0].firstChild.nodeValue;
					caseStudyDetails[5] 	= caseStudy[i].getElementsByTagName('client')[0].firstChild.nodeValue;
					caseStudyDetails[6] 	= caseStudy[i].getElementsByTagName('requirement')[0].firstChild.nodeValue;
					caseStudyDetails[7] 	= caseStudy[i].getElementsByTagName('aboutTheClient')[0].firstChild.nodeValue;
					caseStudyDetails[8] 	= caseStudy[i].getElementsByTagName('solution')[0].firstChild.nodeValue;
					caseStudyDetails[9] 	= caseStudy[i].getElementsByTagName('impact')[0].firstChild.nodeValue;
					caseStudyDetails[10] 	= caseStudy[i].getElementsByTagName('whatTheClientSays')[0].firstChild.nodeValue;
					caseStudyDetails[11] 	= caseStudy[i].getElementsByTagName('file1')[0].firstChild.nodeValue;
					caseStudyDetails[12] 	= caseStudy[i].getElementsByTagName('file2')[0].firstChild.nodeValue;
					caseStudyDetails[13] 	= caseStudy[i].getElementsByTagName('file3')[0].firstChild.nodeValue;
					caseStudyDetails[14] 	= caseStudy[i].getElementsByTagName('file4')[0].firstChild.nodeValue;
					caseStudyDetails[15] 	= caseStudy[i].getElementsByTagName('file5')[0].firstChild.nodeValue;
					caseStudyDetails[16] 	= caseStudy[i].getElementsByTagName('file6')[0].firstChild.nodeValue;
					caseStudyDetails[17] 	= caseStudy[i].getElementsByTagName('dspListImg')[0].firstChild.nodeValue;
					caseStudyDetails[18] 	= caseStudy[i].getElementsByTagName('dspPrimaryImg')[0].firstChild.nodeValue;
					caseStudyDetails[19] 	= caseStudy[i].getElementsByTagName('dspSuppImg')[0].firstChild.nodeValue;
					caseStudyDetails[20] 	= caseStudy[i].getElementsByTagName('dspLogoImg')[0].firstChild.nodeValue;
				}

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

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

function _buildCaseStudyHTML(caseStudy)
{
	// Hide the results div
	hideDiv(ajaxResultsDiv.id);

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

	// Extract elements from the array
	casestudyID 		= caseStudy[0];
	title 				= caseStudy[2];
	metaDescription 	= caseStudy[3];
	metaKeywords 		= caseStudy[4];
	client 				= caseStudy[5];
	requirement	 		= caseStudy[6];
	aboutTheClient 		= caseStudy[7];
	solution 			= caseStudy[8];
	impact 				= caseStudy[9];
	whatTheClientSays 	= caseStudy[10];
	file1 				= caseStudy[11];
	file2 				= caseStudy[12];
	file3 				= caseStudy[13];
	file4 				= caseStudy[14];
	file5 				= caseStudy[15];
	file6 				= caseStudy[16];
	dspListImg 			= caseStudy[17];
	dspPrimaryImg 		= caseStudy[18];
	dspSuppImg 			= caseStudy[19];
	dspLogoImg 			= caseStudy[20];

	h1 = document.createElement('h1');
	h1.innerHTML = caseStudy[2];
	ajaxDetailsDiv.appendChild(h1);

	// Primary image
	if (1 == dspPrimaryImg){
		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=250&max_height=250';

		a.appendChild(img);
		ajaxDetailsDiv.appendChild(a);
	}

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

	// Client
	h2 = document.createElement('h2');
	h2.innerHTML = 'Client';
	ajaxDetailsDiv.appendChild(h2);
	p = document.createElement('p');
	p.innerHTML = client;
	ajaxDetailsDiv.appendChild(p);

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

		div = document.createElement('div');
		div.innerHTML = aboutTheClient;
		ajaxDetailsDiv.appendChild(div);
	}

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

		div = document.createElement('div');
		div.innerHTML = requirement;
		ajaxDetailsDiv.appendChild(div);
	}

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

		div = document.createElement('div');
		div.innerHTML = solution;
		ajaxDetailsDiv.appendChild(div);
	}

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

		div = document.createElement('div');
		div.innerHTML = impact;
		ajaxDetailsDiv.appendChild(div);
	}

	// What The Client Says
	if (whatTheClientSays.length > 0){
		h2 = document.createElement('h2');
		h2.innerHTML = 'What The Client Says';
		ajaxDetailsDiv.appendChild(h2);

		div = document.createElement('div');
		div.innerHTML = whatTheClientSays;
		ajaxDetailsDiv.appendChild(div);
	}

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

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

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

			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');

			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');

			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);

	// 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);
	ajaxDetailsDiv.appendChild(div2);

	// Back link
	p = document.createElement('p');
	var link = document.createElement('a');
	link.id = casestudyID;
	link.onclick = _viewResults;
	link.style.cursor = 'pointer';
	link.innerHTML = 'go back';
	p.appendChild(link);
	ajaxDetailsDiv.appendChild(p);
}

function _viewResults()
{
	// Remove the details div
	ajaxDetailsDiv.innerHTML = '';
	hideDiv(ajaxDetailsDiv.id);

	// 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){
		//existingResults.innerHTML = '';
		//alert('run');
		ajaxResultsDiv.removeChild(existingResults);
	}
}

function rot13init()
{
  var map = new Array();
  var s   = "abcdefghijklmnopqrstuvwxyz";

  for (i=0; i<s.length; i++)
    map[s.charAt((i+13)%26)]				= s.charAt(i);
  for (i=0; i<s.length; i++)
    map[s.charAt((i+13)%26).toUpperCase()]	= s.charAt(i).toUpperCase();
  return map;
}

function rot13(a)
{
  if (!rot13map)
    rot13map=rot13init();
  s = "";
  for (i=0; i<a.length; i++)
    {
      var b = a.charAt(i);

      s	+= (b>='A' && b<='Z' || b>='a' && b<='z' ? rot13map[b] : b);
    }
  return s;
}

function MM_swapImgRestore() {
    var i, x, a = document.MM_sr;
    for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) {
        x.src = x.oSrc;
    }
}
function MM_swapImage() {
    var i, j = 0, x, a = MM_swapImage.arguments;
    document.MM_sr = new Array;
    for (i = 0; i < a.length - 2; i += 3) {
        if ((x = MM_findObj(a[i])) != null) {
            document.MM_sr[j++] = x;
            if (!x.oSrc) {
                x.oSrc = x.src;
            }
            x.src = a[i + 2];
        }
    }
}
function MM_goToURL() {
    var i, args = MM_goToURL.arguments;
    document.MM_returnValue = false;
    for (i = 0; i < args.length - 1; i += 2) {
        eval(args[i] + ".location='" + args[i + 1] + "'");
    }
}
function MM_findObj(n, d) {
    var p, i, x;
    if (!d) {
        d = document;
    }
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document;
        n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) {
        x = d.all[n];
    }
    for (i = 0; !x && i < d.forms.length; i++) {
        x = d.forms[i][n];
    }
    for (i = 0; !x && d.layers && i < d.layers.length; i++) {
        x = MM_findObj(n, d.layers[i].document);
    }
    if (!x && d.getElementById) {
        x = d.getElementById(n);
    }
    return x;
}


rot13map=rot13init();