var contentHandler = new TagHandler(null); 
var objAjax = null;

window.dhtmlHistory.create({debugMode: false});

window.onload = function() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
};

function historyChange(newLocation, historyData) {

	var historyMsg = (typeof historyData == "object" && historyData != null
		? historyStorage.toJSON(historyData)
		: historyData
	);

	if (!newLocation==""){
		if (!newLocation.search(/sections/i)){
			var url="";
			var title="";
			if (newLocation.indexOf("|")==-1) {
				url=newLocation;
				title="";
			}else{
				var locationArray = newLocation.split("|");
				url=locationArray[0];
				title=locationArray[1];
			}
			Ajax('ajaxcontent',url+'&ajax=1');
			document.title='VFM UK > '+unescape(title);
		}
	}else{
		Ajax('ajaxcontent',location.href+'&ajax=1');
		document.title='VFM UK Ltd';
	}
};

ajax = function(historyURL) {

	var url="";
	var title="";
	if (historyURL.indexOf("|")==-1) {
		url=historyURL;
		title="";
	}else{
		var locationArray = historyURL.split("|");
		url=locationArray[0];
		title=locationArray[1];
	}

	Ajax('ajaxcontent',url+'&ajax=1');
	if (title!=""){ document.title='VFM UK > '+unescape(title); }

	dhtmlHistory.add(historyURL,'');
	if (typeof historyURL == "object") {
		historyURL = historyStorage.toJSON(historyURL);
	}
}

function Ajax(tagName,url) {
	//do {
	//} while (objAjax);

	objAjax = createXMLHttpRequest();
	objAjax.open('get', url);
	objAjax.onreadystatechange = responseHandler;
	contentHandler.tagName = tagName;
	setTimeout ('objAjax', 500);
	objAjax.send(null); 
}


function createXMLHttpRequest(){
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
	try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
	return null;

}

function TagHandler(tagName)
{
	var tagName;
	return tagName;
}

function responseHandler() {
	var objCurrent = document.getElementById(contentHandler.tagName)

	if(objAjax.readyState == 0) { objCurrent.innerHTML = 'Loading..'; }
	if(objAjax.readyState == 1) { objCurrent.innerHTML = 'Loading...'; }
	if(objAjax.readyState == 2) { objCurrent.innerHTML = 'Loading....'; }
	if(objAjax.readyState == 3) { objCurrent.innerHTML = 'Loading......'; }

	if(objAjax.readyState == 4)
	{
		if(objAjax.status == 200)
		{
			objCurrent.innerHTML = '';
			var updateContent = objAjax.responseText;
			if(updateContent) {
				objCurrent.innerHTML = updateContent;
			}
		}
		else if(objAjax.status == 404)
		{
			// Retrive error message or redirect to error page
			objCurrent.innerHTML = 'File not found';
		}
		else
		{
			// Give a catch all error message
			objCurrent.innerHTML = 'An error has occured, please refresh this page to try again';
		}

		//set a tabindex to - 1 to help screen readers focus on the changed content. Then set the focus to the changed content.
		objCurrent.tabIndex = -1;
		//objCurrent.focus();
	}
}

function redirectHash()
{
  var hash = location.hash;
  var currenturl = location.href;

  if (hash)
  {
	var url = hash.replace(/#/, '');
	if (url.length > 0){
		url = url.replace('|','&pageTitle=');
		location.href = url;
	}
  }else{
	if (currenturl.indexOf('|')!=-1){
		var url = currenturl.replace('|', '&pageTitle=');
		location.href = url;
	}
  }
}

redirectHash();