﻿    var _placesOriginalArray
    var _global;
   	var _placesArrayPrevious = new Array()
	var _places = new Array()
	var _placesArray = new Array()
	var _height = 0
	var _hover = -1
	var _isHover = false
	var _previousInput = ''
	var _checking = false	
    var _cancelEnterSubmit = false
    var _enabled = true
	var _focused = true
	var _xmlHTTP
	var _typedValue = ''
	var _browserOk = window.ActiveXObject || window.XMLHttpRequest
	var _search = '/modules/qpp/callback.ashx?action=autoComplete&plaats=';
	var _show = false;
	var _input;
	var _lasturl = '';	
	var IE = ( navigator.appVersion.indexOf("MSIE") != -1 )
	var IE50 = ( navigator.appVersion.indexOf("MSIE 5.0") != -1 )

    function OnFocus(elementId, defaultText)
    { 
       if (document.getElementById(elementId).value == defaultText)
       {
          document.getElementById(elementId).className = "normal";
          document.getElementById(elementId).value = "";
       }
    }

    function OnBlur(elementId, defaultText)
    {
       var textValue = document.getElementById(elementId).value;

       if (textValue == defaultText || textValue.length == 0)
       {
          document.getElementById(elementId).className = "watermark";
          document.getElementById(elementId).value = defaultText;
       }
       else
          document.getElementById(elementId).className = "normal";
    }

    function GetSearchUrl(eventSelect,sPrefix,pageKey,searchType,backPageKey, watermarkText)
    {
        var minPrice;
        var maxPrice;
        var sfRange;
        var sfInput;

        minPrice = sPrefix + "Select" + "MinPrice";
        maxPrice = sPrefix + "Select" + "MaxPrice";
        sfRange = sPrefix + "Select" + "Range";
        sfInput = sPrefix + "Input" + "PlaceZip";                                               
                                
        var oMinControl  = document.getElementById(minPrice);
        var oMaxControl = document.getElementById(maxPrice);
        var oRangeControl = document.getElementById(sfRange);
        var oInputControl = document.getElementById(sfInput);        
        var iMinValue = parseInt(oMinControl.value, 0);
        var iMaxValue = parseInt(oMaxControl.value, 0);
        
        //if value of input field has contains watermarktext; set the value to empty string.
        if (oInputControl.value == UrlDecode(watermarkText))
        {
            oInputControl.value = '';
        }
        
        if(iMaxValue == 0) iMaxValue = 1000000000;
        
        var iRangeValue = parseInt(oRangeControl.value, 0);                        
        var sInputValue = UrlEncode(oInputControl.value);
        
        
        var sPostalCode = sInputValue;
        if(sInputValue.length > 4)
        {
            sPostalCode = sInputValue.substr(0, 4);
        }
       
        if(IsNumeric(sPostalCode) == true )
        {
            document.location.href = "/default.aspx?pageKey="+ pageKey +"&minPrice=" + iMinValue + "&maxPrice=" + iMaxValue + "&placeOrZip=" + sInputValue.toUpperCase() +"&range=" + iRangeValue + "&type=" + searchType + "&SearchInputPage=" + backPageKey;
        }
        else
        {
           document.location.href = "/default.aspx?pageKey="+ pageKey +"&minPrice=" + iMinValue + "&maxPrice=" + iMaxValue + "&city=" + sInputValue.toUpperCase() +"&range=" + iRangeValue + "&type=" + searchType + "&SearchInputPage=" + backPageKey;
        }
    }
    
    function UrlEncode(text) 
    {		
		    text=ReplaceAll(text, "?", "%3F");
		    text=ReplaceAll(text, "=", "%3D");
		    text=ReplaceAll(text, "&", "%26");
		    text=ReplaceAll(text, " ", "+");
		    text=ReplaceAll(text, ",", "%2c");	
		    text=ReplaceAll(text, "'", "%27");	                						
		    return text;
    }
    
    function UrlDecode(text) 
    {		
		    text=ReplaceAll(text, "%3F", "?" );
		    text=ReplaceAll(text, "%3D", "=" );
		    text=ReplaceAll(text, "%26", "&" );
		    text=ReplaceAll(text, "+"  , " " );
		    text=ReplaceAll(text, "%2c", "," );	
		    text=ReplaceAll(text, "%27", "'" );	                						
		    return text;
    }
       
    function ReplaceAll(varb, replaceThis, replaceBy)
    {	
		    newvarbarray=varb.split(replaceThis);
		    newvarb=newvarbarray.join(replaceBy);	
		    return newvarb;
    }    
    function checkPrices(eventSelect,sPrefix)
    {
        var minPrice;
        var maxPrice;
        minPrice = sPrefix + "MinPrice";
        maxPrice = sPrefix + "MaxPrice";
        var otherSelect = (eventSelect.name == minPrice) ? eventSelect.form.elements[maxPrice] : eventSelect.form.elements[minPrice];
        var thisValue = parseInt(eventSelect.options[eventSelect.selectedIndex].value, 10);
        var otherValue = parseInt(otherSelect.options[otherSelect.selectedIndex].value, 10);
        if (eventSelect.name == minPrice)
        {
	      if (thisValue >= otherValue)
	      {
		        for (var i = 0; i < otherSelect.options.length; i++)
    		    {
		    	    if (otherSelect.options[i].value > thisValue)
    		      {
        		        otherSelect.selectedIndex = i;
    				    break;
			      }
            }
	      }
        } else
        {
	      if (thisValue <= otherValue)
	      {
		        for (var i = (otherSelect.options.length - 1); i >= 0 ; i--)
		        {
    			      if (parseInt(otherSelect.options[i].value, 10) < thisValue)
		    	      {
				        otherSelect.selectedIndex = i;
    		            break;
		            }
    		    }
	      }
        }
    }


	function Check(eventSelect,event,sPrefix,searchType,nvmNumbers,searchControlId,filter,pageKey,ControlCode)
	{
        var sControl = sPrefix + "PlaceZip";
        var oControl = (eventSelect.name == sControl) ? eventSelect.form.elements[sControl] : null;	
		if ( !_browserOk ) return
		if (oControl == null) return
		if ( _enabled == false || _checking != false || oControl.value == _previousInput ) return
		if ( event ) if ( event.keyCode == 13 ) 
		{
            eval(document.getElementById(searchControlId).href);  
		    return false;
		}
        _input = oControl;		
	
		if ( oControl.value.length == 0 ) 
		{
    		Hide();
		    return true;    		
		}
		
		_typedValue = oControl.value
		Letter = oControl.value.substring(0,1)
		_previousInput = oControl.value
		
		var nLeft = GetElementLeft(oControl);
		var nTop = GetElementTop(oControl) + GetElementHeight(oControl);
		var nWidth = GetElementWidth(oControl) - 2;
		
	    document.getElementById('Lijst').style.left = nLeft + 'px';
	    document.getElementById('Lijst').style.top = nTop + 'px';	
        document.getElementById('Lijst').style.width = nWidth + 'px';

	    document.getElementById('iFrame').style.left = nLeft + 'px';
	    document.getElementById('iFrame').style.top = nTop + 'px';
        document.getElementById('iFrame').style.width = nWidth + 'px';

	    document.getElementById('FrameContainer').style.left = nLeft + 'px';
	    document.getElementById('FrameContainer').style.top = nTop + 'px';
        document.getElementById('FrameContainer').style.width = nWidth + 'px';
		_checking = true
		_xmlHTTP = false
		if ( window.ActiveXObject ) _xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP")
		if ( window.XMLHttpRequest ) _xmlHTTP = new XMLHttpRequest()

		if ( !_xmlHTTP )
		{
			_placesArray = ''
		}
		else
		{
			_xmlHTTP.onreadystatechange = function()
			{
				if (_xmlHTTP.readyState == 4)
				{
					_placesArray = _xmlHTTP.responseText
					sHeader = '<?xml?><plaatsen>'
					sFooter = '</plaatsen>'
					_placesArray = _placesArray.substring(sHeader.length, _placesArray.length - sFooter.length)
					_placesArray = _placesArray.split(',')
					if ( _placesArray != _placesArrayPrevious )
					{
						_placesArrayPrevious = _placesArray
						_placesOriginalArray = CopyArray(_placesArray)
						SetList()
						FilterPlaatsen(oControl.value)
						document.body.onkeydown = KeyDown;
						document.body.onkeyup = KeyUp;	
						document.body.onkeypress = CheckKeypress;
						document.body.onclick = DocumentClick;
					}
				}
			}
			
            var searchAndNvm = "&type=" + searchType + "&nvmNumbers=" + nvmNumbers + "&filter=" + filter;
            if (_lasturl != _search + Letter + searchAndNvm )
            {
                _lasturl = _search + Letter + searchAndNvm;
                _xmlHTTP.open("GET", _search + Letter + searchAndNvm , true)
			    _xmlHTTP.send(null)
			}
			else
			{
				FilterPlaatsen(oControl.value)
			}
			
		}
	}

	function CopyArray(aArray)
	{
		var newArray = new Array()
		for(i=0;i<aArray.length;i++)
		{
			newArray[i] = aArray[i]
		}
		return newArray
	}

	function SetList()
	{
		document.getElementById('Lijst').innerHTML = ''
		itemCount = _placesArray.length > 5 ? 5 : _placesArray.length
		n = 2 + 16 * itemCount
		if ( n > 120 ) n = 120
		_height = 73 - n
		if ( navigator.userAgent.indexOf("Firefox")>0 ) _height += 16
		
		_height = _height < 0 ? 0 : _height
		if ( _placesArray[0] != '' )
		{
			_hover = -1
			s = '<table cellpadding=0 cellspacing=0 style="width:' + '100%' + ';">'
			for(i=0;i<itemCount;i++)
			{
				placesList = _placesArray[i]
				sAantal = placesList.substring(placesList.indexOf('|')+1, placesList.length)
				placesList = placesList.substring(0, placesList.indexOf('|'))
				placeSlash = placesList.replace('\'', '\\\'')
				_places[i] = placesList
				s += '<tr class="gewoon" id="Link' + i + '" onmouseover="MouseOverLink(' + i + ')" onclick="Click(\'' + placeSlash + '\')">' 
				s += '<td class="plaats" title="' + placesList + ', ' + sAantal + ' woningen"><span class="aantal" style="float:right;">' + sAantal + '</span>' + placesList + '</td>'
				s += '</tr>'
			}
			s += '</table>'
			document.getElementById('Lijst').innerHTML += s
			
			_show = true
			document.getElementById('Lijst').style.display = 'block'
			document.getElementById('Lijst').style.position = 'absolute'			
			if ( IE && !IE50 ) document.getElementById('FrameContainer').style.display = 'block'
		}
		else
		{
			Hide()
		}
		if ( IE && !IE50 ) document.getElementById('iFrame').style.height = (document.getElementById('Lijst').clientHeight+2) + 'px'
		if ( itemCount == 0 ) Hide()
		_checking = false
		MouseOverLink(0)
	}		
	
	function MouseOverLink(i)
	{   
		if ( _hover > -1 )
		{
			if ( document.getElementById('Link' + _hover) ) document.getElementById('Link' + _hover).className = 'gewoon'			
		}
		_hover = i
		if ( i != -1 )
		{
			if ( document.getElementById('Link' + i) ) document.getElementById('Link' + i).className = 'LinkHover'				
		}							
	}
	
	function Hide()
	{
		document.body.onkeydown = null;
		document.body.onkeyup = null;	
		document.body.onkeypress = null;
		document.body.onclick = null;	
        _previousInput	= '';
		_show = false
		document.getElementById('Lijst').style.display = 'none'
		if ( IE && !IE50 ) document.getElementById('FrameContainer').style.display = 'none'
	}

	function FilterPlaatsen(sString)
	{
		sString = sString.toLowerCase()
		if ( _placesOriginalArray ) _placesArray = CopyArray(_placesOriginalArray)
		for ( i=_placesArray.length-1; i>-1; i-- )
		{
			if ( _placesArray[i].substring(0, sString.length).toLowerCase() != sString ) Sloop(_placesArray, i)
		}
		SetList()
	}
	
	function Sloop(_placesArray, i)
	{
		for(n=i; n<_placesArray.length-1; n++)
		{
			_placesArray[n] = _placesArray[n+1]
		}
		_placesArray.length --
	}	
	
    function GetElementLeft(m_elem)
    {
	    var x = 0;
	    var elem;		
	    if(typeof(m_elem) == "object")
		    elem = m_elem;
	    else
		    elem = document.getElementById(m_elem);
    		
	    while (elem != null)
	    {
		    x += elem.offsetLeft;
		    elem = elem.offsetParent;
	    }
	    return parseInt(x);
    }
	
    function GetElementTop(m_elem)
    {
	    var y = 0;
	    var elem;
	    if(typeof(m_elem) == "object")
	    {
		    elem = m_elem;
	    }
	    else
	    {
		    elem = document.getElementById(m_elem);
	    }
	    while (elem != null)
	    {
		    y+= elem.offsetTop;
		    elem = elem.offsetParent;
	    }
	    return parseInt(y);
    }

    function GetElementWidth(m_elem)
    {
	    var elem;
	    if(typeof(m_elem) == "object")
	    {
		    elem = m_elem;
	    }
	    else
	    {
		    elem = document.getElementById(m_elem);
	    }
	    return parseInt(elem.offsetWidth);
    }

    function GetElementHeight(m_elem)
    {
	    var elem;
	    if(typeof(m_elem) == "object")
	    {
		    elem = m_elem;
	    }
	    else
	    {
		    elem = document.getElementById(m_elem);
	    }
	    return parseInt(elem.offsetHeight);
    }	

	function KeyDownLink(i)
	{
		if ( parseInt(document.getElementById('Lijst').scrollTop) < -36 + i*16 ) document.getElementById('Lijst').scrollTop = -36 + i*16
		if ( parseInt(document.getElementById('Lijst').scrollTop) > i*16 ) document.getElementById('Lijst').scrollTop = i*16
		MouseOverLink(i)
		_global=i;
		return false;
	}

	function DocumentClick()
	{
		if ( !_browserOk ) return
		if ( _isHover == false )
		{
			Hide()
		}
	}
	
	function CheckKeypress(e)
	{
        var key = window.event ? window.event.keyCode : e.which;

		if ( !_browserOk ) return
		if ( _show == true )
		{
		    if ( key == 40 ) return false
		    if ( key == 38 ) return false
		}
		return true
	}
	
	function KeyDown(e)
	{
	    var key = window.event ? window.event.keyCode : e.which;

		if ( !_enabled ) return
		if ( key == 13 )
		{
			_cancelEnterSubmit = _show
		}
		else
		{
			_cancelEnterSubmit = false
		}
		if ( key == 34 ) GoDown(4)
		if ( key == 33 ) GoUp(4)
		if ( key == 40 ) GoDown(1)
		if ( key == 38 ) GoUp(1)
	}
	
	function KeyUp(e)
	{
	  	var key = window.event ? window.event.keyCode : e.which;

		if ( !_enabled ) return
		if ( key == 13 ) Enter()
		if ( key == 27 ) Enter()
		if ( key == 9 ) Enter()
	}
	
	function Enter(bEnter)
	{
	   	if ( !_browserOk ) return
		if ( _show == true )
		{
            CheckAutocompleteSubmit();
			Hide()
			_input.focus()
		}
	}
	
	function GoDown(n)
	{
		if ( !_browserOk ) return
		if ( _focused == false ) return
		if ( _show == false ) Check()
		n = _hover + n
		if ( !window.itemCount ) return
		if ( n > itemCount-1 )
		{
			n = 0
		}
		KeyDownLink(n)
	}

	function GoUp(n)
	{
		if ( !_browserOk ) return
		if ( _focused == false ) return
		if ( _show == false ) Check()
		n = _hover - n
		if ( n < 0 )
		{
			n = itemCount-1
		}
		KeyDownLink(n)
	}
	
	function CheckAutocompleteSubmit()
	{
		if (( _focused == true && _show == true ) || _cancelEnterSubmit == true )
		{
			Hide()
			_input.value = _places[_hover]
			_cancelEnterSubmit = false
			return false
		}
		
		return CheckInput()
     }
	
	function Click(s)
	{
		_input.value = s
		_show = false
		document.getElementById('Lijst').style.display = 'none'
		if ( IE && !IE50 ) document.getElementById('FrameContainer').style.display = 'none'
		_input.focus()
	}
	
	function IsNumeric(sText)
    {
       var ValidChars = "0123456789";
       var IsNumber = true;
       var Char;
       for (i = 0; i < sText.length && IsNumber == true; i++) 
       { 
         Char = sText.charAt(i); 
         if (ValidChars.indexOf(Char) == -1) 
         {
             IsNumber = false;
         }
       }
    return IsNumber;
   }


