// JavaScript Document

var XMLObject;

function LoadFeed(URL, ElementID, LoadingHTML, LoadCode) {
	var XMLObject;

	XMLObject = false;

	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	
		try {
			XMLObject = new XMLHttpRequest();
		} catch(e) {
			XMLObject = false;
		}

	} else if(window.ActiveXObject) {
		
		try {
			XMLObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				XMLObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				XMLObject = false;
	       		}
		}

	}

	if(XMLObject) {
		if (ElementID != '') {
			document.getElementById(ElementID).innerHTML = LoadingHTML;
		}
			
		if (URL.indexOf('?') == -1) {
			XMLObject.open("GET", URL + '?Rndd=' + Math.floor(Math.random()*99999999999999999), true);
		} else {
			XMLObject.open("GET", URL + '&Rndd=' + Math.floor(Math.random()*99999999999999999), true);
		}
			
		XMLObject.onreadystatechange = function() {					
							if (XMLObject.readyState == 4 && XMLObject.status == 200) {
								if (ElementID != '') {
									document.getElementById(ElementID).innerHTML = XMLObject.responseText
								}
								XMLObject = null;
								eval(LoadCode);
							}					
						}
		XMLObject.send("");
	} else {
		document.getElementById(ElementID).innerHTML = '<div class="errordiv">No LDAP detected</div>'	
	}
		
}

//use if select box is showing through an element thats above it in ie6
function selectDisplay(TempDiv) {
	try {
		var temp = document.getElementById(TempDiv);

		if(temp.style.visibility == 'hidden') {
			temp.style.visibility = 'visible'
		} else {
			temp.style.visibility = 'hidden'
		}
	} catch(e) {

	}
}
//end ie6 select fix

function EmailTo(url) {
	window.location = url;
}

function GotoArea(Menu) {
	if (Menu.value !== '' && Menu.value !== 'Type City or Town') {
		if (searchurl.length > 0) {
			document.location = searchurl;
		} else {
        		document.location = '/SearchOffices.aspx?ST=FreeText&Location=' + Menu.value + '&ComPage=0';
		}
	} else {
		try {
			if (searchurl != '') {
				document.location = searchurl;
			} else {
            			Menu = document.getElementById('home-txt')
				if (Menu.value !== ''  && Menu.value !== 'Type City or Town') {		
					document.location = '/SearchOffices.aspx?ST=FreeText&Location=' + Menu.value + '&ComPage=0';
				}
			}
		} catch(e) {

		}
	}
}

function GotoAreaDrp(Menu) {
	if (Menu.value !== 'na') {
		document.location = '/' + Menu.value + '_Office+Space+to+Rent_1.html?ST=DropDown';
	}
}

function checkFormError(element, html) {
	var splitElements = element.split(":");

	if(html == undefined) {
		html = false
	}

	for(i = 0; i < splitElements.length; i++){
		var temp = document.getElementById(splitElements[i]).value
		if(html == true) {
			temp = temp.replace(/</g, "[LT]");
		}
		else
		{
			temp = temp.replace(/</g, "&lt;");
		}
		document.getElementById(splitElements[i]).value = temp
		
	}
	return true;
}

function globalSearch(MyTextValue) {
	if (MyTextValue != ''  && MyTextValue !== 'Type UK location') {		
		document.location = '/SearchOffices.aspx?ST=FreeText&Location=' + MyTextValue + '&ComPage=0';
	}		
}


function globalCheckEnter(e,MyTextValue) { //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	} else {
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		/*if (document.location != '') {
				document.location = searchurl;
		} else {
			globalSearch(MyTextValue) //submit the form
		} */
		
		globalSearch(MyTextValue)
		return false
	} else {
		return true
	}

}

function checkEnter(e,Menu) { //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	} else {
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		if (document.location != '') {
				document.location = searchurl;
		} else {
			GotoArea(Menu) //submit the form
		}
		return false
	} else {
		return true
	}
}

function clearsearch(MyBox) {
	var searchbox = MyBox;

	if (searchbox.value == '') {
		searchbox.value = 'Type City or Town';
		searchbox.style.color = '#CCCCCC';
	} else if (searchbox.value == 'Type City or Town') {
		searchbox.value = '';
		searchbox.style.color = '#000000';
	}
}

var oldlength = 0;
var helperlink = 0;
var FoundResults = 0;
var searchurl = '';
var tempurl = '';

var searchurl = '';
var VisableHelper = 'Head';

function UpdateSearchType(MyType, MyTextBox) {
	SearchType = MyType;
	TextBoxType =  MyTextBox;
}

function HideHelper() {
	if (DontHide == 0) {
		document.getElementById(SearchType + 'SearchHelper').style.display = 'none'

		tempurl = searchurl
		searchurl = '';
	}
}

function showhelper(searchbox) {
	if (document.getElementById(SearchType + 'SearchHelper').innerHTML == '[POST CODE SEARCH]') {
		return false;
	}

	if (VisableHelper != SearchType) {
		document.getElementById(VisableHelper + 'SearchHelper').style.display = 'none'
		tempurl ='';
		searchurl = '';
	}

	if  ((FoundResults == 1) && (searchbox.value.length > 3) && (searchbox.value != 'Type UK location' && searchbox.value != 'Type US location' && searchbox.value != 'Type City or Town')) {
		document.getElementById(SearchType + 'SearchHelper').style.display = 'block'

		VisableHelper = SearchType;
	}
}

function changeGlobal(MyBox,Typing,SearchType) {
	var searchbox = MyBox;	
		
	if (searchbox.value == '' && Typing == false) {
		searchbox.value = 'Type UK location';
	} else if (Typing == true && searchbox.value == 'Type UK location') {
		searchbox.value = '';
		searchbox.style.color = '#000';
		searchbox.style.fontStyle = 'normal';
	}
}

function FocusHelp(e, forceme) {
	var characterCode //literal character code will be stored in this variable

	if(e.which){ //if which property of event object is supported (NN4)

		characterCode = e.which //character code is contained in NN4's which property
	} else {
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if ((characterCode == 40) || (forceme)){

		document.getElementById(SearchType.toLowerCase() + '-' + TextBoxType).blur()
		document.getElementById(SearchType + 'SearchHelper').focus()

		if (forceme == false) {
			changehelperlink(0);
			scrolldirection = 1;
			scrollnow();
		}
	}
}

function scrollnow() {
	if (scrolldirection)  {
		if (helperlink + 1 <= parseInt(document.getElementById(SearchType + 'totallinks').innerHTML)) {
			changehelperlink(helperlink + 1)
		} else {
			scrollme = 0
		}
	} else {
		if (helperlink - 1 >= 0) {
			changehelperlink(helperlink - 1)
		} else {
			scrollme = 0
		}
	}
	
	if (scrollme) {
		setTimeout('scrollnow',500)
	}
}

function changehelperlink(newlink) {
	if ((document.getElementById(SearchType + 'SearchHelper').innerHTML == '[POST CODE SEARCH]') || (document.getElementById(SearchType + 'SearchHelper').innerHTML == '')) {
		DontHide = 0;
		HideHelper();
		return false;
	}

	if (document.getElementById(SearchType + 'SearchHelper').style.display != 'block') {
		DontHide = 1;
		document.getElementById(SearchType + 'SearchHelper').innerHTML = document.getElementById(SearchType + 'SearchHelper').innerHTML;
		document.getElementById(SearchType + 'SearchHelper').style.display = 'block'
	}

	var templink = helperlink

	try {
		if (SearchType == 'Head') {
			document.getElementById(SearchType + 'HelperLink_' + helperlink).style.backgroundColor = '#FFFFFF';
		} else {
			document.getElementById(SearchType + 'HelperLink_' + helperlink).style.backgroundColor = '#FFFFFF'
		}
		helperlink = newlink;
		document.getElementById(SearchType + 'HelperLink_' + newlink).style.backgroundColor = '#D9E0FF';
		searchurl = document.getElementById(SearchType + 'HelperLink_l' + newlink).href;
	} catch(e) {
		helperlink = templink;
		scrollme = 0;
		document.getElementById(SearchType + 'HelperLink_' + helperlink).style.backgroundColor = '#D9E0FF';
		searchurl = document.getElementById(SearchType + 'HelperLink_l' + newlink).href;
	}
}

function findplaces(searchbox) {
	if ((searchbox.value.length > 3) && (document.getElementById(SearchType + 'SearchHelper').innerHTML != 'Loading...') && (searchbox.value.length != oldlength)) {
		LoadFeed('/includes/PredictSearch.aspx?Placename=' + searchbox.value + '&Helper=' + SearchType, SearchType + 'SearchHelper', 'Loading...', 'changehelperlink(0)');
		FoundResults = 1;
		searchurl = tempurl;
	} else if (searchbox.value.length <= 3) {
		document.getElementById(SearchType + 'SearchHelper').style.display = 'none'
		tempurl = searchurl
		searchurl = '';
	}

	oldlength = searchbox.value.length
}

function startscroll(e) {
	var characterCode //literal character code will be stored in this variable

	if(e.which){ //if which property of event object is supported (NN4)	
		characterCode = e.which //character code is contained in NN4's which property
	} else {
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if (characterCode == 38){ //if generated character code is equal to ascii 13 (if enter key)
		scrolldirection = 0;
		scrollnow();
	} else if (characterCode == 40) {
		scrolldirection = 1;
		scrollnow();
	} else if (characterCode == 13) {
		document.location = searchurl;
	}
}

function stopscroll() {
	scrollme = 0;
}

function hide() {
	DontHide = 1;
}

function setSearchHelperEvents(searchHelper) {
	var temp = document.getElementById(searchHelper);
	temp.tabIndex="1";

	if (document.addEventListener) {
		temp.addEventListener('focus', hide, false);
	} else if (document.attachEvent) {
		temp.attachEvent("onfocus", hide);
	}
}