var searchpopup;
var sppos = new Array;
function toptotal(ele) {
	if (ele.offsetParent) {
		return ele.offsetTop + toptotal(ele.offsetParent);
	}
	return ele.offsetTop;
}
function lefttotal(ele) {
	if (ele.offsetParent) {
		return ele.offsetLeft + lefttotal(ele.offsetParent);
	}
	return ele.offsetLeft;
}
function httpreq() {
	var req = false;
	if(window.XMLHttpRequest) { // 'standards'
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	} else if(window.ActiveXObject) { // ie
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	return req;
}

function inithover() {
	/*if (document.getElementById) {*/
		function flipit(ele, img) {
			var preload;
			preload = new Image();
			preload.src = img;
			return function() {
				ele.src = preload.src;
			}
		}
		var imgarray = new Array();
		var a = document.getElementsByTagName('a');
		var x;
		for (x = 0; x < a.length; x ++) {
			var img, hover;
			img = a[x].getElementsByTagName('img');
			if (img.length) {
				var y;
				for (y = 0; y < img.length; y++) {
					if (hover = img[y].getAttribute('title')) {
						a[x].onmouseover = flipit(img[y], hover);
						a[x].onmouseout = flipit(img[y], img[y].src);
						img[y].removeAttribute('title');
					}
				}
			}
		}
	//}
}
function initevents() {
	/*if (document.getElementById) {*/
		function hi() {
			var td = this.getElementsByTagName('td');
			var x;
			for (x = 0; x < td.length; x ++) {
				if (td[x].className=='g') {
					td[x].style.background='#dddddd';
				} else {
					td[x].style.background='#eeeeee';
				}
			}
		}
		function lo() {
			var td = this.getElementsByTagName('td');
			var x;
			for (x = 0; x < td.length; x ++) {
				if (td[x].className=='g') {
					td[x].style.background='#eeeeee';
				} else {
					td[x].style.background='#ffffff';
				}
			}
		}
		function click(e) {
			if (document.all) e = window.event;
			var target = e.target?e.target:e.srcElement;
			if (target.nodeName != 'A') {
				var a = this.getElementsByTagName('a');
				document.location.href=a[0].getAttribute('href');
				
			}
		}
		var tab = document.getElementById('ssaaevents');
		if (tab) {
			var tr = tab.getElementsByTagName('tr');
			var x;
			for (x = 0; x < tr.length; x ++) {
				var a = tr[x].getElementsByTagName('a');
				if (a.length) {
					a[0].parentNode.parentNode.onmouseover = hi;
					a[0].parentNode.parentNode.onmouseout = lo;
					a[0].parentNode.parentNode.onclick = click;
				}
			}
		}
	//}
}
function hidepopup(e) {
	if (document.all) e = window.event;
	var x = e.pageX?e.pageX:e.clientX + document.body.parentNode.scrollLeft;
	var y = e.pageY?e.pageY:e.clientY + document.body.scrollTop;
	if ((x < sppos[3]) || (y < sppos[0]) || (x > sppos[1]) || (y > sppos[2])) {
		document.body.onmousemove = null;
		searchpopup.style.visibility='hidden';
	}
}
function searchpopupele(id, x, y) {
	var ct;
	if(navigator.userAgent.indexOf("MSIE")>0)
	y=y+document.body.scrollTop;
	for (ct = searchpopup.childNodes.length - 1; ct >= 0; ct --) {
		searchpopup.removeChild(searchpopup.childNodes[ct]);
	}
	var d = document.createElement('div');
	d.innerHTML = 'Select a region...';
	searchpopup.appendChild(d);
	for (ct = 0; ct < rsm[id].length; ct += 2) {
		var a = document.createElement('a');
		a.setAttribute('href', 'http://www.selfstorage.com.au/scripts/cgiip.exe/WService=SSAA/ccms.r?'+rsm[id][ct]);
		a.innerHTML = rsm[id][ct+1];
		searchpopup.appendChild(a);
	}
	var h = searchpopup.offsetHeight / 2;
	var w = searchpopup.offsetWidth / 2;
	sppos[0] = y - h;
	sppos[1] = x + w;
	sppos[2] = y + h;
	sppos[3] = x - w;
	searchpopup.style.top = (y - h) + 'px';
	searchpopup.style.left = (x - w) + 'px';
	searchpopup.style.visibility='visible';
}
function initsearchmap(mapimg) {
	if (mapimg) {
		function click(e) {
			if (document.all) e = window.event;
			if (this.blur) {
				this.blur();
			}
			if(rsm[this.id.substr(1)].length == 2) {
				return true;
			} else {
				var x = e.pageX?e.pageX:e.clientX + document.body.parentNode.scrollLeft;
				var y = e.pageY?e.pageY:e.clientY + document.body.parentNode.scrollTop;
				searchpopupele(this.id.substr(1), x, y);
			}
			document.body.onmousemove = hidepopup;
			return false;
		}
		var areas = mapimg.getElementsByTagName('area');
		var x;
		for (x = 0; x < areas.length; x ++) {
			if (areas[x].id.substr(0,1) == 'r') {
				areas[x].onclick=click;
			}
		}
	}
}
function initsearchpopup() {
	/*if (document.getElementById) {*/
		
		searchpopup = document.createElement('div');
		searchpopup.style.position = 'absolute';
		searchpopup.style.top = '0px';
		searchpopup.style.left = '0px';
		searchpopup.style.background = '#ffcc33';
		searchpopup.style.border = 'solid black';
		searchpopup.style.borderWidth = '1px 2px 2px 1px';
		searchpopup.style.width = '120px';
		searchpopup.style.visibility='hidden';
		searchpopup.className = 'searchpopup';
		var mapimg = document.getElementById('ausmap');
		
		if(mapimg==null)
		{
			mapimg = document.getElementById('nzmap');
			if(mapimg==null)return;
			
		}
		

		document.body.appendChild(searchpopup);

		initsearchmap(mapimg);
		mapimg = document.getElementById('nzmap');
		if(mapimg==null)return;
		initsearchmap(mapimg);
	//}
}
function initsearch() {
		initsearchpopup();
		initinput();
		/*
	if (document.getElementById) {
		initsearchpopup();
		initinput();
	}*/
}

function initinput() {
	/*if (document.getElementById) {*/
		function updateinput(e) {
			if (document.all) {
				setTimeout("document.getElementById('pcode').updatepopup()", 100);
			} else {
				if (e.charCode) {
					setTimeout("document.getElementById('pcode').updatepopup()", 100);
				}
			}
		}

		function scrollpopup(e) {
			if (document.all) e = window.event;

			if (this.popup.style.visibility == 'visible') {
				if (e.keyCode == 38) {
					this.scrollindex -= 1;
					if (this.scrollindex < 1) {
						this.scrollindex = this.scrollcount;
					}
				} else if (e.keyCode == 40) {
					this.scrollindex += 1;
					if (this.scrollindex > (this.scrollcount)) {
						this.scrollindex = 1;
					}
				} else if (e.keyCode == 9) {
					this.popup.style.visibility = 'hidden';
				} else if ((e.keyCode == 37) || (e.keyCode == 39)) {
					return;
				} else {
					setTimeout("document.getElementById('pcode').updatepopup()", 100);
					return;
				}
				this.updatehighlight();
			}
		}

		function checkclear(e) {
			if (document.all) e = window.event;
			
			if (this.popup.style.visibility == 'visible') {
				if ((e.keyCode != 38) && (e.keyCode != 40)) {
					setTimeout("document.getElementById('pcode').updatepopup()", 100);
				}
			}
		}

		function hidesearchpopup(e) {
			this.popup.style.visibility = 'hidden';
		}

		function checkmouseupdate(e) {
			if (document.all) e = window.event;
			var y = e.pageY?e.pageY:e.clientY + document.body.parentNode.scrollTop;
			var newtest = Math.floor((y - this.offsetTop - 2) / 18) + 1;
			if ((newtest > this.input.scrollcount) || (newtest < 1)) {
				return;
			}
			if (newtest != this.input.scrollindex) {
				this.input.scrollindex = newtest;
				this.input.updatehighlight();
			}
		}
		
		var input = document.getElementById('pcode');
		if (input) {
			input.onkeypress=updateinput;
			input.onkeydown=scrollpopup;
			input.onkeyup=checkclear;
			input.onblur=hidesearchpopup;
			input.oldtext = '';
			input.show = false;

			input.updatehighlight = function() {
				var divs = this.popup.getElementsByTagName('div');
				if (this.oldhighlight != null) {
					this.oldhighlight.style.background = 'buttonface';
					this.oldhighlight.style.color = 'buttontext';
				}
				divs[this.scrollindex - 1].style.background='highlight';
				divs[this.scrollindex - 1].style.color='highlighttext';
				this.oldhighlight = divs[this.scrollindex - 1];
				this.value = divs[this.scrollindex - 1].innerHTML;
			}
			input.updatepopup = function() {
				if (this.value.length >= 2) {
					if (this.value == this.oldtext) {
						if (input.show) {
							this.popup.style.visibility = 'visible';
						}
					} else {
						this.oldtext = this.value;
						var req = httpreq();
						var input = this;

						if (req) {
							req.onreadystatechange = function() {
								if (req.readyState == 4) {
									var ret = req.responseText;
									if (ret == 'false') {
										input.popup.style.visibility = 'hidden';
										input.show = false;
									} else {
										input.show = true;
										var arr = eval(ret);
										var x;
										var div;
										input.scrollindex = 0;
										input.scrollcount = arr.length;
										input.popup.innerHTML='';

										for (x = 0; x < arr.length; x ++) {
											div = document.createElement('div');
											div.innerHTML = arr[x];
											input.popup.appendChild(div);
											if (div.offsetWidth > input.offsetWidth) {
												div.setAttribute("title", arr[x]);
											}
										}

										input.lasthighlight = null;
										input.popup.style.top = (toptotal(input) - input.popup.offsetHeight) + 'px';
										input.popup.style.visibility = 'visible';
									}
								}
							}
							req.open("GET", "databind.php?op=suburb&s="+this.value, true);
							req.send(null);
						}
					}
				} else {
					this.popup.style.visibility = 'hidden';
				}
			}

			input.popup = document.createElement('div');
			input.popup.style.position = 'absolute';
			input.popup.style.border = '2px outset';
			input.popup.style.background = 'buttonface';
			input.popup.style.width = (input.offsetWidth - 4) + 'px';
			input.popup.style.top = '0px';
			input.popup.style.left = lefttotal(input) + 'px';
			input.popup.style.visibility = 'hidden';
			input.popup.style.overflow = 'hidden';
			input.popup.className = 'inputpopup';
			input.popup.innerHTML = '';
			input.popup.input = input;
			input.popup.onmousemove = checkmouseupdate;
			document.body.appendChild(input.popup);
		}
	//}
}
