//--------------------------------------------------------------------------------------------------------
// RealNext Search Engine Client Script
// Copyright (c) 2006 Solutior B.V.
//--------------------------------------------------------------------------------------------------------

var RealNextSearch = new Object();

RealNextSearch.ScopeTimer = 0;
RealNextSearch.ScopeURL = "";
RealNextSearch.ScopeURI = 0;
RealNextSearch.ScopeXML = null;
RealNextSearch.ScopeMSXML = false;
RealNextSearch.ScopeState = 0;
RealNextSearch.ScopeObject = "";
RealNextSearch.ScopeObjects = "";
RealNextSearch.URI = "";
RealNextSearch.Posting = false;
RealNextSearch.Data = null;

RealNextSearch.onValidateNumber = function(e) {
	if (!e)
		e = window.event;
	
	if (e && e.type == "keypress") {
		var c = e.keyCode ? e.keyCode : e.which ? e.which : 0;

		if (!(c >= 48 && c <= 57) && !(c >= 35 && c <= 39) && c != 8 && c != 9 && c != 46) {
			e.cancelBubble = true;
			e.returnValue = false;
			
			if (e.stopPropagation)
				e.stopPropagation();

			if (e.preventDefault)
				e.preventDefault();
		}
	}
}

RealNextSearch.onChangeNumber = function(e) {
	var val = this.value;
	var f_val = "";
	var f_null = 1;

	for(var i = 0;i < val.length;i ++)
		if (val.charCodeAt(i) >= 48 && val.charCodeAt(i) <= 57 && (!f_null || val.charCodeAt(i) != 48)) {
			f_val += val.charAt(i);
			f_null = 0;
		}

	val = "";

	if (f_val != "")
		f_val = String(parseInt(f_val));

	for(var i = f_val.length-1,j=0;i >= 0;i --) {
		val = f_val.charAt(i)+(j == 3 ? "." : "")+val;

		if (j == 3)
			j = 0;

		j ++;
	}

	if (val == "")
		val = "-";

	if (!this.orgLength)
		this.orgLength = this.maxLength;

	this.maxLength = 20;
	this.hasFocus = 0;

	if (this.value != val)
		this.value = val;
}

RealNextSearch.onActivateNumber = function(e) {
	if (!this.hasFocus) {
		var val = this.value;
		var f_val = "";

		for(var i = 0;i < val.length;i ++)
			if (val.charCodeAt(i) >= 48 && val.charCodeAt(i) <= 57)
				f_val += val.charAt(i);

		this.value = f_val;
		this.maxLength = this.orgLength;
		this.select();
		this.hasFocus = 1;
	}
}

RealNextSearch.onSelect = function(e) {
	if (!this.hasFocus) {
		this.select();
		this.hasFocus = 1;
	}
}

RealNextSearch.onDeselect = function(e) {
	this.hasFocus = 0;
}

RealNextSearch.onSortingChange = function(e) {
	var pSortingDesc = document.getElementById("RealNextSearchSortingDesc");
	
	if (pSortingDesc) {
		pSortingDesc.disabled = this.selectedIndex ? false : true;
	
		if (!this.selectedIndex)
			pSortingDesc.selectedIndex = 0;
	}
}

RealNextSearch.onLocationChange = function(e) {
	var pRange = document.getElementById("RealNextSearchRange");
	
	if (pRange)
		pRange.disabled = this.value != "" ? false : true;
		
	RealNextSearch.postponeScope();
}

RealNextSearch.setPageSize = function(sURL) {
	var pPageSize = document.getElementById("RealNextSearchPageSize");
	
	if (pPageSize && sURL != "") {
		var pSorting = document.getElementById("RealNextSearchSorting");
		var pSortingDesc = document.getElementById("RealNextSearchSortingDesc");
	
		if (pSorting)
			pSorting.disabled = 1;
			
		if (pSortingDesc)
			pSortingDesc.disabled = 1;
			
		pPageSize.disabled = 1;

		window.location = sURL+"&pagesize="+pPageSize.options[pPageSize.selectedIndex].value;
	}
}

RealNextSearch.setSorting = function(sURL) {
	var pSorting = document.getElementById("RealNextSearchSorting");

	if (pSorting && sURL != "") {
		var pSortingDesc = document.getElementById("RealNextSearchSortingDesc");
		var pPageSize = document.getElementById("RealNextSearchPageSize");
		
		if (pSorting)
			pSorting.disabled = 1;
			
		if (pSortingDesc)
			pSortingDesc.disabled = 1;
		
		if (pPageSize)
			pPageSize.disabled = 1;

		window.location = sURL+"&sorting="+pSorting.options[pSorting.selectedIndex].value+"&sortingdesc="+(pSortingDesc ? pSortingDesc.options[pSortingDesc.selectedIndex].value : 0);
	}
}

RealNextSearch.applyNumber = function(e) {
	e.onkeypress = RealNextSearch.onValidateNumber;
	e.onfocus = RealNextSearch.onActivateNumber;
	e.onblur = RealNextSearch.onChangeNumber;
		
	if (e.captureEvents) {
		e.captureEvents(Event.KeyPress);
		e.captureEvents(Event.Focus);
		e.captureEvents(Event.Blur);
	}
	
	e.onblur();
}

RealNextSearch.applySelection = function(e) {
	e.onfocus = RealNextSearch.onSelect;
	e.onblur = RealNextSearch.onDeselect;
		
	if (e.captureEvents) {
		e.captureEvents(Event.Focus);
		e.captureEvents(Event.Blur);
	}
}

RealNextSearch.postponeScope = function() {
	if (document.getElementById("RealNextSearchScope")) {
		if (RealNextSearch.ScopeTimer) {
			window.clearTimeout(RealNextSearch.ScopeTimer);
			RealNextSearch.ScopeTimer = 0;
		}
	
		RealNextSearch.ScopeTimer = window.setTimeout("RealNextSearch.updateScope();", 750);
	}
}

RealNextSearch.updateScope = function() {
	var p = document.getElementById("RealNextSearchScope");
	
	if (p && !RealNextSearch.Posting && RealNextSearch.Data) {
		var f = RealNextSearch.Data;
		var u = RealNextSearch.ScopeURL;

		for(var i = 0;i < f.length;i ++)
			if (f[i].type != "submit" && f[i].type != "button" && f[i].name != "guid" && f[i].name != "call" && f[i].name != "session" && f[i].name != "area")
				u += "&"+f[i].name+"="+escape(f[i].value.replace("&nbsp;"," "));

		RealNextSearch.ScopeTimer = 0;

		if (RealNextSearch.ScopeURI != u) {
			if (!RealNextSearch.ScopeState) {
				if (!RealNextSearch.ScopeXML) {
					if (typeof(XMLHttpRequest) == "undefined") {
						var pVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
						
						for(var nVersion = 0;nVersion < pVersions.length && !RealNextSearch.ScopeXML;nVersion ++) {
							try {
								RealNextSearch.ScopeXML = new ActiveXObject(pVersions[nVersion]);
							} catch(e) {
								RealNextSearch.ScopeXML = null;
							}
						}
						RealNextSearch.ScopeMSXML = true;
					} else RealNextSearch.ScopeXML = new XMLHttpRequest();
				}

				if (RealNextSearch.ScopeXML) {
					try {
						RealNextSearch.ScopeURI = u;
						RealNextSearch.ScopeState = 1;

						RealNextSearch.ScopeXML.open("GET", u+"&"+(new Date()).getTime(), 1);

						RealNextSearch.ScopeXML.onreadystatechange = function() {
							if (RealNextSearch.ScopeXML.readyState == 4) {
								var p = document.getElementById("RealNextSearchScope");
									
								if (p && RealNextSearch.ScopeXML.status == 200) {
									var nScope = parseInt(RealNextSearch.ScopeXML.responseText);
									
									if (isNaN(nScope) || typeof(nScope) != "number")
										nScope = 0;
										
									if (p.Scope != nScope) {
										var sScope = String(nScope);
										var nDots = sScope.length-3;
										
										while(nDots > 0) {
											sScope = sScope.slice(0,nDots)+"."+sScope.slice(nDots);
											nDots -= 3;
										}
										
										if (p.filters && p.filters.length)
											p.filters[0].apply();

										p.childNodes[0].innerHTML = "<B>"+sScope+"&nbsp;"+(nScope == 1 ? RealNextSearch.ScopeObject : RealNextSearch.ScopeObjects)+"</B>";
										p.Scope = nScope;

										if (p.filters && p.filters.length)
											p.filters[0].play();
									}
								}

								RealNextSearch.ScopeState = 0;
							}
						};

						RealNextSearch.ScopeXML.send(null);
					} catch(e) {}
				}
			} else RealNextSearch.ScopeTimer = window.setTimeout("RealNextSearch.updateScope();", 100);
		}
	}
}

RealNextSearch.setState = function() {
	if (RealNextSearch.Data) {
		var f = RealNextSearch.Data;

		for(var i = 0;i < f.length;i ++)
			if (f[i].type == "hidden") {
				if (f[i].name.indexOf("usage") == 0 && document.getElementById(f[i].name+"check"))
					document.getElementById(f[i].name+"check").className = "realnext_search_checkbox"+(f[i].value == "1" ? "_checked" : "");
				else if (f[i].name.indexOf("type") == 0 && document.getElementById(f[i].name+"check"))
					document.getElementById(f[i].name+"check").className = "realnext_search_checkbox"+(f[i].value == "1" ? "_checked" : "");
			}
	}
}

RealNextSearch.getURL = function(s, b) {
	s = String(s);
	
	if (s.length) {
		while(s.indexOf("&amp;") != -1)
			s = s.replace("&amp;", "###amp###");
	
		if (!b)
			while(s.indexOf("&") != -1)
				s = s.replace("&", escape("&"));

		while(s.indexOf("###amp###") != -1)
			s = s.replace("###amp###", "&");
	}
	
	return s;
}

RealNextSearch.Highlight = function(p) {
	p.className = "realnext_search_results";
	
	for(var i = 1;i < p.cells.length;i++)
		p.cells[i].className += " realnext_search_results_hover";
}

RealNextSearch.Unlight = function(p) {
	p.className = "";
	
	for(var i = 1;i < p.cells.length;i++)
		p.cells[i].className = p.cells[i].className.replace(" realnext_search_results_hover", "");
}

RealNextSearch.View = function(id) {
	var p = document.getElementById("RealNextSearch"+id);
	
	if (p) {
		var doc = document.getElementById("RealNextSearchDocument");
		
		if (doc && doc.RealNextSearchSelected != id) {
			if (doc.filters && doc.filters.length == 1)
				doc.filters[0].apply();
		
			for(var i = 0;i < doc.childNodes.length;i++)
				if (doc.childNodes[i]) {
					doc.childNodes[i].style.display = "none";
				
					if (doc.childNodes[i].id != "" && document.getElementById(doc.childNodes[i].id+"Button"))
						document.getElementById(doc.childNodes[i].id+"Button").className = "realnext_search_link";
				}
					
					
			if (doc.scrollTop)
				doc.scrollTop = 0;
				
			p.style.display = "block";
			
			if (document.getElementById("RealNextSearch"+id+"Button")) {
				document.getElementById("RealNextSearch"+id+"Button").className = "realnext_search_link_focus";
				doc.RealNextSearchSelected = id;
			}

			if (doc.filters && doc.filters.length == 1)
				doc.filters[0].play();
		}
	}
}

RealNextSearch.Print = function(s) {
	window.open(s, "", "width=600,height=400,scrollbars=yes,location=no,resizable=yes,status=no,toolbar=no,menubar=no");
}

RealNextSearch.Map = function(url) {
	window.open(RealNextSearch.getURL(url, 1), "", "width=600,height=400");
}

RealNextSearch.Cyclomedia = function(url, data, type) {
	window.open(url+"cyclomedia/cyclomedia.asp?"+RealNextSearch.getURL(data, 1), "", type ? "width=530,height=480,scrollbars=no,location=no,resizable=yes,status=no,toolbar=no,menubar=no" : "width=450,height=450");
}

RealNextSearch.ExtendWindow = function(url, guid, id, view) {
	window.open(url+"search.aspx?guid="+guid+"&id="+id+"&view="+view, "", "width=1000,height=705,scrollbars=no,location=no,resizable=no,status=no,toolbar=no,menubar=no");
}

RealNextSearch.Initialize = function(sURI, sURL, sObject, sObjects) {
	try {
		RealNextSearch.URI = sURI != "" ? sURI : "search.dll";
		
		if (!document.RealNextSearchForm) {
			var pInputs = document.getElementsByTagName("input");
			var pSelects = document.getElementsByTagName("select");
			
			RealNextSearch.Data = new Array();
			
			if (pInputs)
				for(var i = 0;i < pInputs.length;i ++)
					if (pInputs[i].id.indexOf("RealNextSearch") == 0)
						RealNextSearch.Data[RealNextSearch.Data.length] = pInputs[i];

			if (pSelects)
				for(var i = 0;i < pSelects.length;i ++)
					if (pSelects[i].id.indexOf("RealNextSearch") == 0)
						RealNextSearch.Data[RealNextSearch.Data.length] = pSelects[i];
		} else RealNextSearch.Data = document.RealNextSearchForm;
		
		if (RealNextSearch.Data && document.getElementById("RealNextSearchLocation") || document.getElementById("RealNextSearchArea")) {
			var pLocation = document.getElementById("RealNextSearchLocation");
			var pSorting = document.getElementById("RealNextSearchSorting");
			
			if (pLocation) {
				RealNextSearch.applySelection(pLocation);

				pLocation.onchange = RealNextSearch.onLocationChange;
				pLocation.onpropertychange = RealNextSearch.onLocationChange;

				pLocation.onchange();

				if (pLocation.captureEvents)
					pLocation.captureEvents(Event.Change);
			}

			if (document.getElementById("RealNextSearchStreet"))
				RealNextSearch.applySelection(document.getElementById("RealNextSearchStreet"));

			if (document.getElementById("RealNextSearchBuilding"))
				RealNextSearch.applySelection(document.getElementById("RealNextSearchBuilding"));

			if (document.getElementById("RealNextSearchParkingFrom"))
				RealNextSearch.applyNumber(document.getElementById("RealNextSearchParkingFrom"));

			if (document.getElementById("RealNextSearchPriceFrom"))
				RealNextSearch.applyNumber(document.getElementById("RealNextSearchPriceFrom"));

			if (document.getElementById("RealNextSearchPriceTo"))
				RealNextSearch.applyNumber(document.getElementById("RealNextSearchPriceTo"));

			if (document.getElementById("RealNextSearchMetrageFrom"))
				RealNextSearch.applyNumber(document.getElementById("RealNextSearchMetrageFrom"));

			if (document.getElementById("RealNextSearchMetrageTo"))
				RealNextSearch.applyNumber(document.getElementById("RealNextSearchMetrageTo"));

			if (pSorting) {
				pSorting.onchange = RealNextSearch.onSortingChange;

				if (pSorting.captureEvents)
					pSorting.captureEvents(Event.Change);
			}
			
			RealNextSearch.ScopeURL = RealNextSearch.getURL(sURL);
			RealNextSearch.ScopeObject = sObject;
			RealNextSearch.ScopeObjects = sObjects;

			if (document.getElementById("RealNextSearchScope")) {
				var f = RealNextSearch.Data;

				for(var i = 0;i < f.length;i ++)
					if (f[i].type != "submit" && f[i].name != "guid" && f[i].name != "call")
						if (!f[i].onpropertychange)
							f[i].onpropertychange = RealNextSearch.postponeScope;
			}

			window.setTimeout("RealNextSearch.setState();", 10);
			window.setInterval("RealNextSearch.updateScope();", 1500);
		}
		
		if (document.getElementById("RealNextSearchTitle"))
			window.setTimeout("var p=document.getElementById('RealNextSearchTitle');if(p){if(p.filters&&p.filters.length)p.filters[0].apply();p.style.visibility='visible';if(p.filters&&p.filters.length)p.filters[0].play();}",100);

		if (document.getElementById("RealNextSearchDetails") && document.getElementById("RealNextSearchDocument"))
			document.getElementById("RealNextSearchDocument").RealNextSearchSelected = "Details";
		
		if (document.getElementById("RealNextSearchDescription")) {
			try {
				this.Clean(document.getElementById("RealNextSearchDescription"));
			} catch(e) {}
		
			document.getElementById("RealNextSearchDescription").style.visibility = "visible";
		}
		
		if (typeof(RealNextSearchMapInitialize) == "function")
			eval("RealNextSearchMapInitialize();");
	} catch(e) {}
}

RealNextSearch.Search = function(sURL, pAreas) {
	for(var i = 0;i < pAreas.length;i ++) {
		var pValue = document.getElementById("RealNextSearchArea"+pAreas[i]+"Value");
		
		if (parseInt(pValue.value) == 1)
			sURL += "&area"+pAreas[i]+"=1";
	}

	window.location = sURL+"&areaview=1";
}

RealNextSearch.Select = function(nParent, pAreas) {
	if (pAreas) {
		var bSelected = false;
	
		var pValueAll = document.getElementById("RealNextSearchArea");
		var pElementAll = document.getElementById("areacheck");

		if (!nParent) {
			for(var i = 0;i < pAreas.length;i ++) {
				var pValue = document.getElementById("RealNextSearchArea"+pAreas[i]+"Value");
				var pElement = document.getElementById("area"+pAreas[i]+"check");
				
				if (pValue)
					pValue.value = 0;
					
				if (pElement)
					pElement.className = "realnext_search_checkbox";
			}
		}
			
		for(var i = 0;i < pAreas.length && !bSelected;i ++) {
			var pValue = document.getElementById("RealNextSearchArea"+pAreas[i]+"Value");
			
			if (parseInt(pValue.value) == 1)
				bSelected = true;
		}

		if (pValueAll)
			pValueAll.value = bSelected ? 0 : 1;
			
		if (pElementAll)
			pElementAll.className = "realnext_search_checkbox"+(bSelected ? "" : "_checked");
	}
}

RealNextSearch.QuickSearch = function(sURL) {
	window.location = sURL;
}

RealNextSearch.Submit = function(sCall) {
	try {
	    if (!RealNextSearch.Posting) {
		    RealNextSearch.Posting = true;
    		
		    if (typeof(sCall) == "string" && sCall != "" && document.getElementById("RealNextSearchCall"))
			    document.getElementById("RealNextSearchCall").value = sCall;
    		
		    if (!RealNextSearch.Data.submit) {
			    var sURL = RealNextSearch.URI;
    			
			    for(var i = 0;i < RealNextSearch.Data.length;i ++)
				    if (RealNextSearch.Data[i].type != "submit" && RealNextSearch.Data[i].type != "button")
					    sURL += (sURL.indexOf("?") == -1 ? "?" : "&")+RealNextSearch.Data[i].name+"="+escape(RealNextSearch.Data[i].value);

			    if (sURL != "")
				    window.location = sURL;
		    } else RealNextSearch.Data.submit();
	    }
    } catch(e) {}
}

RealNextSearch.Clean = function(pElement) {
	if (pElement && typeof pElement.className === "string" && typeof pElement.childNodes === "object") {
		var sTagName = String(pElement.tagName).toLowerCase();

		if (pElement.childNodes && pElement.childNodes.length > 0)
			for(var nChild = 0;nChild < pElement.childNodes.length;nChild ++)
				RealNextSearch.Clean(pElement.childNodes[nChild]);
		
		if (sTagName == "div" || sTagName == "table" || sTagName == "tr" || sTagName == "td" || sTagName == "th" || sTagName == "span" || sTagName == "a") {
			pElement.style.borderColor = "";
			pElement.style.backgroundColor = "";
			pElement.style.fontFamily = "";
			pElement.style.fontSize = "";
			pElement.className = (pElement.className == "" ? "" : pElement.className+" ")+"realnext_search_default";
		}
	}
}