var rsSuburbs = new Array();
var rsSubsRef = new Array();

var cPage	= null;
var cTab	= null;

var isBusy	= false;
var isReady = false;

var isSelected		= false;
var isFirstMatch	= false;

var reCheck = false;
var errCNT	= 0;

var isNextArray = false;
var isListening = false;

if (!document.all) { document.captureEvents(Event.MOUSEMOVE); }
document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
	if (document.all) {
		if (document.body && event) {
			// grab the x-y pos.s if browser is IE
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
			
			window.status = tempX +' | '+ tempY;
		}
	}
	else {
		// grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}

	if (tempX < 0) { tempX = 0; }
	if (tempY < 0) { tempY = 0; }

	return true;
}

function onKeyNav(e) {
	if (document.all) e = event;
	
	var elem = e.target || e.srcElement;
	

	if (elem.id.indexOf('QS') > 0) {
		var sMatches = document.getElementById('sMatchesQS')
		var isOther = 'QS';
	}

	else {
		var sMatches = document.getElementById('sMatches')
		var isOther = '';
	}
		
	if (sMatches && sMatches.className == 'hideMe') {
		if (e.keyCode == 13) {
			//return true;
		}
		else {
			return;
		}
	}
	else {
		if (e.keyCode == 38) { // Up arrow
			if (!isSelected) return;
			if (isSelected && !isSelected.parentNode.previousSibling) return;
			var prev = isSelected.parentNode.previousSibling.firstChild;

			if (!prev) return;

			selectMe(prev, true, isOther);
		}

		if (e.keyCode == 40) { // Down arrow
			if (!isSelected) {
				if (!isFirstMatch) return;
				selectMe(isFirstMatch, true, isOther);
			}
			else {
				if (isSelected && !isSelected.parentNode.nextSibling) return;
				var next = isSelected.parentNode.nextSibling.firstChild;

				if (!next) return;
				selectMe(next, true, isOther);
			}
		}

		if (e.keyCode == 13 || e.keyCode == 9) { // Enter key or tab key
			if (isSelected && isSelected.className && isSelected.className == 'isSelected') {
				chooseMe(isSelected);
				return false;
			}
			else return false;
		}

		if (e.keyCode == 27) { // Escape key
			clearSubs(isOther);
		}
	}
}

function keyListen(e, isOther) {
	if (navigator.userAgent.toLowerCase().indexOf("safari") >= 0 || navigator.userAgent.toLowerCase().indexOf("msie") >= 0) {
		document.body.onkeydown = onKeyNav;
	}
	else {
		document.body.onkeypress = onKeyNav;
	}

	isListening = true;
}

function showVid() {
	if (document.getElementById('vaVid')) {
		document.getElementById('embdVid').src = "http://www.youtube.com/embed/LdOS8Pd_E7M?hl=en&fs=1";
		document.getElementById('vaVid').className = 'showMe';

		var wHeight = getViewportHeight();
		var vHeight = parseInt(wHeight - getY('vaVid'));

		if (parseInt(document.getElementById('sBox').offsetHeight) < vHeight) {
			vHeight = parseInt(document.getElementById('vaVid').offsetHeight);
		}

		document.getElementById('vaVidContent').style.left = (parseInt(document.getElementById('vaVid').offsetWidth)/2)-(parseInt(document.getElementById('vaVidContent').offsetWidth)/2) +'px';
		document.getElementById('vaVidContent').style.top = (vHeight/2)-(parseInt(document.getElementById('vaVidContent').offsetHeight)/2) +'px';

		hideSelectBoxes();
	}
	
	return false;
}

function hideVid() {
	if (document.getElementById('vaVid') && document.getElementById('vaVid').className == 'showMe') {
		document.getElementById('embdVid').src = "#";
		document.getElementById('vaVid').className = 'hideMe';
	}
}


function showSuper() {
	if (document.getElementById('sBox')) {
		document.getElementById('sBox').className = 'showMe';
		if (document.getElementById('sName')) document.getElementById('sName').focus();

		keyListen();
		moveSuper();
		
		addEvent(window, "resize", moveSuper);
		
		hideSelectBoxes();
		//urchinTracker('/search/vacancy-super-search/');
	}
	
	return false;
}

function hideSuper() {
	if (document.getElementById('sBox') && document.getElementById('sBox').className == 'showMe') {
		document.getElementById('sBox').className = 'hideMe';
	}
}

function moveSuper() {
	if (document.getElementById('sBox').className == 'showMe') {
		var wHeight = getViewportHeight();
		var vHeight = parseInt(wHeight - getY('sBox'));

		if (parseInt(document.getElementById('sBox').offsetHeight) < vHeight) {
			vHeight = parseInt(document.getElementById('sBox').offsetHeight);
		}

		document.getElementById('sBoxContent').style.left = (parseInt(document.getElementById('sBox').offsetWidth)/2)-(parseInt(document.getElementById('sBoxContent').offsetWidth)/2) +'px';
		document.getElementById('sBoxContent').style.top = (vHeight/2)-(parseInt(document.getElementById('sBoxContent').offsetHeight)/2) +'px';
	}
}

function setSubs(strSearch, xml) {
	if (xml.readyState == 4 || xml.readyState == "complete") {
		if (xml.responseText.length > 0) {
			var rText = xml.responseText;

			rText = rText.replace(/[|]/g, '"],["');
			rText = rText.replace(/[*]/g, '","');
			rText = '['+ rText +']';
					
			addSub(strSearch, eval(rText));
		}
	}
}

function getSubs(strSearch, isNext, isOther) {
	if (strSearch.length > 0) {
		strSearch = strSearch.trim();

		if (strSearch.length > 0) {
			isBusy = true;
			isReady = false;

			showLoad(isOther);

			if (window.addEventListener) {
				var xml = GetXmlHttpObject(function() {
					xml.onreadystatechange = setSubs(strSearch, this);
				});
			}
			else {
				var xml = GetXmlHttpObject(function() {
					xml.onreadystatechange = function () {
						if (xml.readyState == 4 || xml.readyState == "complete") {
							if (xml.responseText.length > 0) {
								var rText = xml.responseText;

								rText = rText.replace(/[|]/g, '"],["');
								rText = rText.replace(/[*]/g, '","');
								rText = '['+ rText +']';
														
								addSub(strSearch, eval(rText));
							}
						}
					}
				});
			}

			//var sVars = "str="+ strSearch;
			var sVars = null;
			
			xml.open ("GET", "/_ssi/ajax/_getSuburbs.asp?str="+ strSearch , true);
			xml.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
			xml.send (sVars);
		}
		else {
			clearSubs(isOther);
		}
	}
	else {
		clearSubs(isOther);
	}
}

function addSub(strSearch, rsSubs) {
	var nxtSlot = null;
	
	if (rsSubsRef.length == 0) {
		nxtSlot = 0;	
	}
	else {
		for (i=0;i<rsSubsRef.length;i++) {
			if (strSearch.isNumeric()) {
				if (rsSubsRef[i] == strSearch.substring(0,2)) {
					nxtSlot = i;
					break;
				}
			}
			else {
				if (rsSubsRef[i] == strSearch.substring(0,1).toLowerCase()) {
					nxtSlot = i;
					break;
				}
			}
		}
		
		if (nxtSlot == null) {
			nxtSlot = rsSubsRef.length;
		}
	}
	
	if (strSearch.isNumeric()) {
		rsSubsRef[nxtSlot] = strSearch.substring(0,2).toLowerCase();
	}

	else {
		rsSubsRef[nxtSlot] = strSearch.substring(0,1).toLowerCase();
	}
	rsSuburbs[nxtSlot] = rsSubs;

	isBusy = false;
}

function loadList(strSearch, isNext, isOther) {
	if (isNextArray == isNext) {
		var nxtSlot = null;
		if (strSearch != null && strSearch != '') {
			for (i=0;i<rsSubsRef.length;i++) {
				if (strSearch.isNumeric()) {
					if (rsSubsRef[i] == strSearch.substring(0,2)) {
						nxtSlot = i;
						break;
					}
				}
				else {
					if (rsSubsRef[i] == strSearch.substring(0,1).toLowerCase()) {
						nxtSlot = i;
						break;
					}
				}
			}
		}
		
		if (nxtSlot != null) {
			var cntMatched = buildList(strSearch, nxtSlot, null, null, isOther);
			if (cntMatched == 0) { clearSubs(isOther); }
		}
	}
	else {
		isReady = true;
	}
}

function buildList(strSearch, nxtSlot, fromIndex, toIndex, isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	var sDiv = document.getElementById('sMatches'+ isOther);
	
	if (document.getElementById('matches'+ isOther)) {
		var sDivUL = document.getElementById('matches'+ isOther);
	}
	else {
		var sDivUL = document.createElement('UL');
		sDivUL.id = 'matches'+ isOther;
	}
	
	if (fromIndex == null) { fromIndex = 0; }

	if (fromIndex < 0) { fromIndex = 0; }
	
	if (toIndex == null) { toIndex = rsSuburbs[nxtSlot].length; }
	if (toIndex > rsSuburbs[nxtSlot].length) { toIndex = rsSuburbs[nxtSlot].length; }

	var sDivLI = null;
	var sDivA = null;

	var sName = new String;
	var sPostCode = new String;
	var sState = new String;

	var cVal = null;
	var sVal = null;
	
	var cntMatched = 0;

	for (i=fromIndex;i<toIndex;i++) {
		sName = rsSuburbs[nxtSlot][i][0];
		sState = rsSuburbs[nxtSlot][i][1];
		sPostCode = rsSuburbs[nxtSlot][i][2];
		
		cVal = sName.toLowerCase();
		sVal = strSearch.toLowerCase();

		cValAlt = sName.toLowerCase() +' ('+ sState.toLowerCase() +')';

		if (strSearch.isNumeric()) {
			if (sPostCode.indexOf(sVal) == 0) {
				getIT = true;
			}
			else {
				getIT = false;
			}
		}
		else {
			if (cVal.indexOf(sVal) == 0 || cValAlt.indexOf(sVal) == 0) {
				getIT = true;
			}
			else {
				getIT = false;
			}
		}
		
		if (getIT) {		
			sDivLI = document.createElement('LI');

			sDivA = document.createElement('A');
			if (sState.trim() != '') {
				sDivA.id = doCapitalize(sName) +' ('+ sState +')';
			}
			else {
				sDivA.id = doCapitalize(sName);
			}
			sDivA.isOther = isOther;
			sDivA.query = doCapitalize(sName);
			sDivA.pcode = sPostCode;
			sDivA.onclick = chooseMe;
			sDivA.onmouseover = selectMe;

			sDivA.href = 'javascript:void(0)';

			//if (document.all) { sDivA.style.width = '273px'; }

			if (sState.trim() != '') {
				sDivA.innerHTML = sName.replace(strSearch, '<strong>'+ strSearch +'</strong>') +' <i>('+ sState +' - '+ sPostCode +')</i>';
			}
			else {
				sDivA.innerHTML = sName.replace(strSearch, '<strong>'+ strSearch +'</strong>');
			}

			sDivLI.appendChild(sDivA);
			sDivUL.appendChild(sDivLI);

			if (cntMatched == 0) {
				isFirstMatch = sDivA;
			}

			cntMatched++;
		}
	}

	if (cntMatched > 0) {
		if (!document.getElementById('matches'+ isOther)) {
			sDiv.appendChild(sDivUL);
			isSelected = false;
			sDiv.scrollTop = 0;		
		}
		
		showSubs(isOther);
		
		if (navigator.userAgent.toLowerCase().indexOf("opera") >= 0 || navigator.userAgent.toLowerCase().indexOf("msie")>=0) {
			//sDiv.style.width = '275px';
					
			if (sDiv.offsetHeight > 125 || sDiv.scrollHeight > 125) {
				sDiv.style.height = '125px';
			}

			if (sDiv.scrollHeight < 125) {
				sDiv.style.height = parseInt(sDiv.scrollHeight) +'px';
				if (navigator.userAgent.toLowerCase().indexOf("opera") >= 0) {
					sDiv.style.overflow = 'hidden';
				}
				else {
					sDiv.style.overflowY = 'hidden';
				}
			}
			else {
				sDiv.style.height = '125px';
				if (navigator.userAgent.toLowerCase().indexOf("opera") >= 0) {
					sDiv.style.overflow = 'auto';
				}
				else {
					sDiv.style.overflowY = 'auto';
				}
			}
		}
	}
	
	isReady = true;
	return cntMatched;
}

function loadSubs(strSearch, e, isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	document.getElementById('sName'+ isOther).isfocused = true;
	
	var sDiv = document.getElementById('sMatches'+ isOther);
	var cNode = document.getElementById('matches'+ isOther);

	if (e) {
		keyPressed = e.keyCode;

		if (keyPressed) {
			if (keyPressed == 40 && isSelected && sDiv.className && sDiv.className == 'hideMe') {
				//isSelected = null; showSubs(isOther); sDiv.scrollTop = 0; selectMe(isFirstMatch); return;
			}
			else {
				if (keyPressed == 13 || keyPressed == 9 || keyPressed == 37 || keyPressed == 38 || keyPressed == 39 || keyPressed == 40 || keyPressed == 27) return;
			}
		}
	}

	if (cNode) {
		sDiv.removeChild(cNode);
	}

	var doIT = false;
	
	if (strSearch.isAlphaNumeric()) {
		if (strSearch.isNumeric()) {
			doIT = true;
		}
		else {
			if (strSearch.length >= 3) {
				doIT = true;
			}
		}
	}

	if (doIT) {
		if (document.getElementById('sMsg'+ isOther)) document.getElementById('sMsg'+ isOther).style.color = '#1c7fc0';
		document.getElementById('sName'+ isOther).style.backgroundColor = '';

		if (strSearch.isNumeric()) {
			document.getElementById('frmQuery'+ isOther).value = '';
			document.getElementById('frmPostCode'+ isOther).value = strSearch;
		}
		else {	
			document.getElementById('frmQuery'+ isOther).value = strSearch;
			document.getElementById('frmPostCode'+ isOther).value = '';
		}
		
		isReady = false;
		showLoad(isOther);

		var nxtSlot = null;
		for (i=0;i<rsSubsRef.length;i++) {
			if (strSearch.isNumeric()) {
				if (rsSubsRef[i].isNumeric()) {
					if (rsSubsRef[i] == strSearch.substring(0,2)) {
						nxtSlot = i;
						break;
					}
				}
			}
			else {
				if (rsSubsRef[i] == strSearch.substring(0,1).toLowerCase()) {
					nxtSlot = i;
					break;
				}
			}
		}
			
		if (nxtSlot == null) {
			var isNext = new Date();
			isNext = isNext.getTime();

			while (isNext == isNextArray) {
				isNext = (isNext/2)+23165;
			}

			isNextArray = isNext;

			if (!isBusy) {
				if (strSearch.isNumeric()) {
					if (parseInt(strSearch) > 10 && parseInt(strSearch) < 100) {
						getSubs(strSearch, isNextArray, isOther);
					}
					else {
						isReady = true;
					}
				}
				else {
					if (!strSearch.substring(0,1).isNumeric()) {
						getSubs(strSearch.substring(0,1), isNextArray, isOther);
					}
					else {
						isReady = true;
					}
				}
				
				reCheck = window.setInterval(function() {
					if (!isBusy) {
						loadList(strSearch, isNextArray, isOther);
						window.clearInterval(reCheck);
					}
				}, 1);
			}
			else {				
				if (reCheck) {
					window.clearInterval(reCheck);
				}

				if (strSearch.length == 1) {
					getSubs(strSearch.substring(0,1), isNextArray, isOther);
				}

				reCheck = window.setInterval(function() {
					if (!isBusy) {
						loadList(strSearch, isNextArray, isOther);
						window.clearInterval(reCheck);
					}
				}, 1);
			}
		}
		else {		
			if (rsSuburbs[nxtSlot] && rsSuburbs[nxtSlot].length > 0) {
				var cntTotal = parseInt(rsSuburbs[nxtSlot].length);		
				var cntMatched = buildList(strSearch, nxtSlot, null, null, isOther);
				if (cntMatched == 0) { clearSubs(isOther); }
				
				/*
				var cnt = 0;
				while (cnt < cntTotal) {			
					cntMatched += buildList(strSearch, nxtSlot, cnt, (cnt+100), isOther);
					if (cntMatched > 0) { showSubs(isOther); }

					cnt += 100;
				}
				*/
			}
			else {
				clearSubs(isOther);
			}
		}
	}
	else {
		if (reCheck) {
			window.clearInterval(reCheck);
		}

		document.getElementById('frmQuery'+ isOther).value = '';
		document.getElementById('frmPostCode'+ isOther).value = '';

		isNextArray = false;
		isReady = true;
		
		hideSubs(isOther);
	}
	
	if (!isListening) {
		keyListen();
	}
}

function vSearch(f, isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	hideSubs(isOther);
	document.getElementById('sName'+ isOther).blur();
	
	if (document.getElementById('sName'+ isOther).value == '') {
		if (errCNT > 0) {
			alert('Please enter a Suburb or Postcode in the field provided');
		}
		if (document.getElementById('sMsg'+ isOther)) document.getElementById('sMsg'+ isOther).style.color = '#990000';
		document.getElementById('sName'+ isOther).style.backgroundColor = '#bfddff';

		errCNT++;

		document.getElementById('sName'+ isOther).focus();
		return false;
	}
	
	if (f.query.value == '' || f.postcode.value == '') {
		alert('Please select one of the suggested suburbs');
		document.getElementById('sName'+ isOther).isfocused = true;
		document.getElementById('sName'+ isOther).focus();
		
		return false;
	}
	
	document.getElementById('sName'+ isOther).value = '';

	return true;
}

function showLoad(isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	document.getElementById('sLoad'+ isOther).className ='load showMe';

	var timer = window.setInterval(function() {
		if (isReady) {
			hideLoad(isOther);
			window.clearInterval(timer);
		}
	}, 1);
}

function hideLoad(isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	document.getElementById('sLoad'+ isOther).className = 'hideMe';
}

function showSubs(isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	var sDiv = document.getElementById('sMatches'+ isOther);
	if (sDiv) sDiv.className = 'pgSubMatches sbarsNew';
}

function hideSubs(isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	if (document.getElementById('sName'+ isOther).isfocused == true) {
		//leave it open
	}
	else {
		var sDiv = document.getElementById('sMatches'+ isOther);
		if (sDiv) sDiv.className = 'hideMe';
	}
}

function clearSubs(isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	var sDiv = document.getElementById('sMatches'+ isOther);
	var cNode = document.getElementById('ulMatches'+ isOther);

	if (cNode) {
		sDiv.removeChild(cNode);
	}

	hideSubs(isOther);

	isSelected	= false;
	isFirstMatch	= false;
}

function blurMe(isOther) {
	if (!isOther) {
		isOther = '';
	}
	
	var timer = window.setInterval(function() {
		var xPos = parseInt(tempX,10);
		var yPos = parseInt(tempY,10);
		
		if (xPos >= getX('sMatches'+ isOther) && xPos <= (getX('sMatches'+ isOther) + getW('sMatches'+ isOther)) && yPos >= getY('sMatches'+ isOther) && yPos <= (getY('sMatches'+ isOther) + getH('sMatches'+ isOther))) {
			//leave it open
		}
		else {
			document.getElementById('sName'+ isOther).isfocused = false;
			
			window.setTimeout("hideSubs('"+ isOther +"')", 500);
			window.clearInterval(timer);
		}
	}, 1);
}

function chooseMe(obj) {
	if (this.id) {
		obj = this;
	}

	isSelected = obj;

	document.getElementById('sName'+ isSelected.isOther).value = isSelected.id;
	document.getElementById('frmQuery'+ isSelected.isOther).value = isSelected.query;
	document.getElementById('frmPostCode'+ isSelected.isOther).value = isSelected.pcode;

	var sDiv = document.getElementById('sMatches'+ isSelected.isOther);
	if (sDiv) sDiv.className = 'hideMe';
}

function selectMe(obj, fromKey, isOther) {
	if (this.id) {
		obj = this;
		
		if (obj.id.indexOf('QS') > 0) {
			isOther = 'QS';
		}
	}

	if (!isOther) {
		isOther = '';
	}

	if (isSelected) {
		if (isSelected.className == 'isSelected') {
			if (document.all) {
				isSelected.className = '';
			}
			else {
				isSelected.removeAttribute('class');
			}
		}
	}

	isSelected = obj;

	if (fromKey) {
		var sDiv = document.getElementById('sMatches'+ isOther);

		if (isSelected.offsetTop > sDiv.offsetHeight) {
		    sDiv.scrollTop = isSelected.offsetTop - sDiv.offsetHeight + isSelected.offsetHeight + 2;
		}

		if (isSelected.offsetTop < sDiv.scrollTop) {
		    sDiv.scrollTop = 0;
		}
	}

	isSelected.className = 'isSelected';
}
