﻿/* Variables required for FES_REALNEXT_QSRCHCP Module */
var FES_REALNEXT_QSRCHCP_placesOriginalArray
var FES_REALNEXT_QSRCHCP_global;
var FES_REALNEXT_QSRCHCP_placesArrayPrevious = new Array()
var FES_REALNEXT_QSRCHCP_places = new Array()
var FES_REALNEXT_QSRCHCP_placesArray = new Array()
var FES_REALNEXT_QSRCHCP_height = 0
var FES_REALNEXT_QSRCHCP_hover = -1
var FES_REALNEXT_QSRCHCP_isHover = false
var FES_REALNEXT_QSRCHCP_previousInput = ''
var FES_REALNEXT_QSRCHCP_checking = false	
var FES_REALNEXT_QSRCHCP_cancelEnterSubmit = false
var FES_REALNEXT_QSRCHCP_enabled = true
var FES_REALNEXT_QSRCHCP_focused = true
var FES_REALNEXT_QSRCHCP_xmlHTTP
var FES_REALNEXT_QSRCHCP_typedValue = ''
var FES_REALNEXT_QSRCHCP_browserOk = window.ActiveXObject || window.XMLHttpRequest
var FES_REALNEXT_QSRCHCP_search = '/modules/FES_REALNEXT_QSRCHCP/CallBack.ashx?Action=AutoCompleteRealNext&plaats=';
var FES_REALNEXT_QSRCHCP_show = false;
var FES_REALNEXT_QSRCHCP_input;

//Creating a JS object 'FES_REALNEXT_QSRCHCP_Constants' to store constants specific to this module.
//Note: See also CONSTANTS in FES_REALNEXT_ENGINE. For readiblity and user's ease, the Constant names are kept same in both files.
var FES_REALNEXT_QSRCHCP_Constants =
{
	ACTUAL_PAGENAME: "/default.aspx",
	QS_PARAMETER_PAGEKEY : "pageKey",
	QS_PARAMETER_RNE_PERFORMSEARCH : "rne_performSearch",
	QS_PARAMETER_RNE_PERFORMSEARCH_TRUEVALUE : "true",
	QS_PARAMETER_RNE_USAGE : "rne_usage",
	QS_PARAMETER_RNE_METRAGE : "rne_metrage",
	QS_PARAMETER_RNE_CITY : "rne_location",
	QS_PARAMETER_RNE_RANGE : "rne_range",
	FRAME_CONTAINER_ID : "FrameContainer",
	LIST_ID : "Lijst",
	IFRAME_ID : "iFrame"
	
	/*http://search.propertynl.com/search.dll?guid=9CAF6C7AD8C14CC28236BF9FA52A43F7&
	uri=/2000401-69fb/bedrijfs-onroerend-goed-zoeken.aspx&
	callback=/Modules/FES_REALNEXT_ENGINE/RealNextCallback.aspx&
	browser=msie&pagekey=2000401&call=results&usage=1&metrage=0-0&
	location=deventer&range=0&performSearch=true&no-cache=1318861374834*/
};

var IE = ( navigator.appVersion.indexOf("MSIE") != -1 )
var IE50 = ( navigator.appVersion.indexOf("MSIE 5.0") != -1 )

//Sumbits gathered information (search criteria) to FES_REALNEXT_ENGINE module, in speficided page (pageKey).
function FES_REALNEXT_QSRCHCP_SearchRealNext(usageElementId, metrageElementId, placeElementId, rangeElementId, pageKey, destinationSite)
{
    var sPace;
    sPace = document.getElementById(placeElementId).value;
    
    if (sPace == '')
    {
      alert('De invoer van een plaatsnaam of een postcode is noodzakelijk.\r\nVoer a.u.b. alsnog een plaatsnaam of een postcode in.');
    }
    else
    {
	    var realnextEngineModuleQS = destinationSite;
	    realnextEngineModuleQS += FES_REALNEXT_QSRCHCP_Constants.ACTUAL_PAGENAME + "?" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_PAGEKEY + "=" + pageKey;
	    realnextEngineModuleQS += "&" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_RNE_USAGE + "=" + document.getElementById(usageElementId).value;
	    realnextEngineModuleQS += "&" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_RNE_METRAGE + "=" + document.getElementById(metrageElementId).value;
	    realnextEngineModuleQS += "&" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_RNE_CITY + "=" + document.getElementById(placeElementId).value;
	    // Range value must be forwarded as kilometers, but metres are used as value for selectbox.
	    realnextEngineModuleQS += "&" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_RNE_RANGE + "=" + (document.getElementById(rangeElementId).value/1000);
	    realnextEngineModuleQS += "&" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_RNE_PERFORMSEARCH + "=" + FES_REALNEXT_QSRCHCP_Constants.QS_PARAMETER_RNE_PERFORMSEARCH_TRUEVALUE;
	    realnextEngineModuleQS += "&" + "call=results";
    	
	    document.location.href = realnextEngineModuleQS;
	}
}

function FES_REALNEXT_QSRCHCP_ReplaceAll(varb, replaceThis, replaceBy)
{	
	    newvarbarray=varb.split(replaceThis);
	    newvarb=newvarbarray.join(replaceBy);	
	    return newvarb;
}

function FES_REALNEXT_QSRCHCP_UrlEncode(text) 
{		
	    text=FES_REALNEXT_QSRCHCP_ReplaceAll(text, "?", "%3F");
	    text=FES_REALNEXT_QSRCHCP_ReplaceAll(text, "=", "%3D");
	    text=FES_REALNEXT_QSRCHCP_ReplaceAll(text, "&", "%26");
	    text=FES_REALNEXT_QSRCHCP_ReplaceAll(text, " ", "+");
	    text=FES_REALNEXT_QSRCHCP_ReplaceAll(text, ",", "%2c");	
	    text=FES_REALNEXT_QSRCHCP_ReplaceAll(text, "'", "%27");	                						
	    return text;
}
   
function FES_REALNEXT_QSRCHCP_Check(eventSelect,event,sControl,searchControlId)
{
    //var sControl = sPrefix;
    var oControl = document.getElementById(sControl);
	if ( !FES_REALNEXT_QSRCHCP_browserOk ) return
	if (oControl == null) return
	if ( FES_REALNEXT_QSRCHCP_enabled == false || FES_REALNEXT_QSRCHCP_checking != false || oControl.value == FES_REALNEXT_QSRCHCP_previousInput ) return
	if ( event ) if ( event.keyCode == 13 ) 
	{
		eval(document.getElementById(searchControlId).href);  
	    return false;
	}
    FES_REALNEXT_QSRCHCP_input = oControl;		

	if ( oControl.value.length == 0 ) 
	{
		FES_REALNEXT_QSRCHCP_Hide();
	    return true;    		
	}
	
	FES_REALNEXT_QSRCHCP_typedValue = oControl.value
	//Letter = oControl.value.substring(0,1)
	FES_REALNEXT_QSRCHCP_previousInput = oControl.value
	
	var nLeft = FES_REALNEXT_QSRCHCP_GetElementLeft(oControl);
	var nTop = FES_REALNEXT_QSRCHCP_GetElementTop(oControl) + FES_REALNEXT_QSRCHCP_GetElementHeight(oControl);
	var nWidth = FES_REALNEXT_QSRCHCP_GetElementWidth(oControl) - 2;
	
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.left = nLeft + 'px';
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.top = nTop + 'px';	
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.width = nWidth + 'px';

    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.IFRAME_ID).style.left = nLeft + 'px';
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.IFRAME_ID).style.top = nTop + 'px';
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.IFRAME_ID).style.width = nWidth + 'px';

    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.FRAME_CONTAINER_ID).style.left = nLeft + 'px';
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.FRAME_CONTAINER_ID).style.top = nTop + 'px';
    document.getElementById(FES_REALNEXT_QSRCHCP_Constants.FRAME_CONTAINER_ID).style.width = nWidth + 'px';
	FES_REALNEXT_QSRCHCP_checking = true
	FES_REALNEXT_QSRCHCP_xmlHTTP = false
	if ( window.ActiveXObject ) FES_REALNEXT_QSRCHCP_xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP")
	if ( window.XMLHttpRequest ) FES_REALNEXT_QSRCHCP_xmlHTTP = new XMLHttpRequest()

	if ( !FES_REALNEXT_QSRCHCP_xmlHTTP )
	{
		FES_REALNEXT_QSRCHCP_placesArray = ''
	}
	else
	{
		FES_REALNEXT_QSRCHCP_xmlHTTP.onreadystatechange = function()
		{
			if (FES_REALNEXT_QSRCHCP_xmlHTTP.readyState == 4)
			{
				FES_REALNEXT_QSRCHCP_placesArray = FES_REALNEXT_QSRCHCP_xmlHTTP.responseText
				sHeader = '<?xml?><plaatsen>'
				sFooter = '</plaatsen>'
				FES_REALNEXT_QSRCHCP_placesArray = FES_REALNEXT_QSRCHCP_placesArray.substring(sHeader.length, FES_REALNEXT_QSRCHCP_placesArray.length - sFooter.length)
				FES_REALNEXT_QSRCHCP_placesArray = FES_REALNEXT_QSRCHCP_placesArray.split(',')
				if ( FES_REALNEXT_QSRCHCP_placesArray != FES_REALNEXT_QSRCHCP_placesArrayPrevious )
				{
					FES_REALNEXT_QSRCHCP_placesArrayPrevious = FES_REALNEXT_QSRCHCP_placesArray
					FES_REALNEXT_QSRCHCP_placesOriginalArray = FES_REALNEXT_QSRCHCP_CopyArray(FES_REALNEXT_QSRCHCP_placesArray)
					FES_REALNEXT_QSRCHCP_SetList()
					FES_REALNEXT_QSRCHCP_FilterPlaatsen(oControl.value)
					document.body.onkeydown = FES_REALNEXT_QSRCHCP_KeyDown;
					document.body.onkeyup = FES_REALNEXT_QSRCHCP_KeyUp;	
					document.body.onkeypress = FES_REALNEXT_QSRCHCP_CheckKeypress;
					document.body.onclick = FES_REALNEXT_QSRCHCP_DocumentClick;
				}
			}
		}
		
		FES_REALNEXT_QSRCHCP_xmlHTTP.open("GET", FES_REALNEXT_QSRCHCP_search + FES_REALNEXT_QSRCHCP_typedValue, true)
		FES_REALNEXT_QSRCHCP_xmlHTTP.send(null)
	}
}

function FES_REALNEXT_QSRCHCP_CopyArray(aArray)
{
	var newArray = new Array()
	for(i=0;i<aArray.length;i++)
	{
		newArray[i] = aArray[i]
	}
	return newArray
}

function FES_REALNEXT_QSRCHCP_SetList()
{
	document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).innerHTML = ''
	itemCount = FES_REALNEXT_QSRCHCP_placesArray.length > 5 ? 5 : FES_REALNEXT_QSRCHCP_placesArray.length
	n = 2 + 16 * itemCount
	if ( n > 120 ) n = 120
	FES_REALNEXT_QSRCHCP_height = 73 - n
	if ( navigator.userAgent.indexOf("Firefox")>0 ) FES_REALNEXT_QSRCHCP_height += 16
	
	FES_REALNEXT_QSRCHCP_height = FES_REALNEXT_QSRCHCP_height < 0 ? 0 : FES_REALNEXT_QSRCHCP_height
	if ( FES_REALNEXT_QSRCHCP_placesArray[0] != '' )
	{
		FES_REALNEXT_QSRCHCP_hover = -1
		s = '<table cellpadding=0 cellspacing=0 style="width:' + '100%' + ';">'
		for(i=0;i<itemCount;i++)
		{
			placesList = FES_REALNEXT_QSRCHCP_placesArray[i]
			sAantal = placesList.substring(placesList.indexOf('|')+1, placesList.length)
			placesList = placesList.substring(0, placesList.indexOf('|'))
			placeSlash = placesList.replace('\'', '\\\'')
			FES_REALNEXT_QSRCHCP_places[i] = placesList
			s += '<tr class="gewoon" id="Link' + i + '" onmouseover="FES_REALNEXT_QSRCHCP_MouseOverLink(' + i + ')" onclick="FES_REALNEXT_QSRCHCP_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(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).innerHTML += s
		
		FES_REALNEXT_QSRCHCP_show = true
		document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.display = 'block'
		document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.position = 'absolute'			
		if ( IE && !IE50 ) document.getElementById(FES_REALNEXT_QSRCHCP_Constants.FRAME_CONTAINER_ID).style.display = 'block'
	}
	else
	{
		FES_REALNEXT_QSRCHCP_Hide()
	}
	if ( IE && !IE50 ) document.getElementById(FES_REALNEXT_QSRCHCP_Constants.IFRAME_ID).style.height = (document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).clientHeight+2) + 'px'
	if ( itemCount == 0 ) FES_REALNEXT_QSRCHCP_Hide()
	FES_REALNEXT_QSRCHCP_checking = false
	FES_REALNEXT_QSRCHCP_MouseOverLink(0)
}		

function FES_REALNEXT_QSRCHCP_MouseOverLink(i)
{   
	if ( FES_REALNEXT_QSRCHCP_hover > -1 )
	{
		if ( document.getElementById('Link' + FES_REALNEXT_QSRCHCP_hover) ) document.getElementById('Link' + FES_REALNEXT_QSRCHCP_hover).className = 'gewoon'			
	}
	FES_REALNEXT_QSRCHCP_hover = i
	if ( i != -1 )
	{
		if ( document.getElementById('Link' + i) ) document.getElementById('Link' + i).className = 'LinkHover'	
		
	}							
}

function FES_REALNEXT_QSRCHCP_Hide()
{
	document.body.onkeydown = null;
	document.body.onkeyup = null;	
	document.body.onkeypress = null;
	document.body.onclick = null;	
    FES_REALNEXT_QSRCHCP_previousInput	= '';
	FES_REALNEXT_QSRCHCP_show = false
	document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.display = 'none'
	if ( IE && !IE50 ) document.getElementById(FES_REALNEXT_QSRCHCP_Constants.FRAME_CONTAINER_ID).style.display = 'none'
}

function FES_REALNEXT_QSRCHCP_FilterPlaatsen(sString)
{
	sString = sString.toLowerCase()
	if ( FES_REALNEXT_QSRCHCP_placesOriginalArray ) FES_REALNEXT_QSRCHCP_placesArray = FES_REALNEXT_QSRCHCP_CopyArray(FES_REALNEXT_QSRCHCP_placesOriginalArray)
	for ( i=FES_REALNEXT_QSRCHCP_placesArray.length-1; i>-1; i-- )
	{
		if ( FES_REALNEXT_QSRCHCP_placesArray[i].substring(0, sString.length).toLowerCase() != sString ) FES_REALNEXT_QSRCHCP_Sloop(FES_REALNEXT_QSRCHCP_placesArray, i)
	}
	FES_REALNEXT_QSRCHCP_SetList()
}

function FES_REALNEXT_QSRCHCP_Sloop(FES_REALNEXT_QSRCHCP_placesArray, i)
{
	for(n=i; n<FES_REALNEXT_QSRCHCP_placesArray.length-1; n++)
	{
		FES_REALNEXT_QSRCHCP_placesArray[n] = FES_REALNEXT_QSRCHCP_placesArray[n+1]
	}
	FES_REALNEXT_QSRCHCP_placesArray.length --
}	

function FES_REALNEXT_QSRCHCP_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 FES_REALNEXT_QSRCHCP_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 FES_REALNEXT_QSRCHCP_GetElementWidth(m_elem)
{
    var elem;
    if(typeof(m_elem) == "object")
    {
	    elem = m_elem;
    }
    else
    {
	    elem = document.getElementById(m_elem);
    }
    return parseInt(elem.offsetWidth);
}

function FES_REALNEXT_QSRCHCP_GetElementHeight(m_elem)
{
    var elem;
    if(typeof(m_elem) == "object")
    {
	    elem = m_elem;
    }
    else
    {
	    elem = document.getElementById(m_elem);
    }
    return parseInt(elem.offsetHeight);
}	

function FES_REALNEXT_QSRCHCP_KeyDownLink(i)
{
	if ( parseInt(document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).scrollTop) < -36 + i*16 ) document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).scrollTop = -36 + i*16
	if ( parseInt(document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).scrollTop) > i*16 ) document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).scrollTop = i*16
	FES_REALNEXT_QSRCHCP_MouseOverLink(i)
	FES_REALNEXT_QSRCHCP_global=i;
	return false;
}

function FES_REALNEXT_QSRCHCP_DocumentClick()
{
	if ( !FES_REALNEXT_QSRCHCP_browserOk ) return
	if ( FES_REALNEXT_QSRCHCP_isHover == false )
	{
		FES_REALNEXT_QSRCHCP_Hide()
	}
}

function FES_REALNEXT_QSRCHCP_CheckKeypress(e)
{
    var key = window.event ? window.event.keyCode : e.which;

	if ( !FES_REALNEXT_QSRCHCP_browserOk ) return
	if ( FES_REALNEXT_QSRCHCP_show == true )
	{
	    if ( key == 40 ) return false
	    if ( key == 38 ) return false
	}
	return true
}

function FES_REALNEXT_QSRCHCP_KeyDown(e)
{
    var key = window.event ? window.event.keyCode : e.which;

	if ( !FES_REALNEXT_QSRCHCP_enabled ) return
	if ( key == 13 )
	{
		FES_REALNEXT_QSRCHCP_cancelEnterSubmit = FES_REALNEXT_QSRCHCP_show
	}
	else
	{
		FES_REALNEXT_QSRCHCP_cancelEnterSubmit = false
	}
	if ( key == 34 ) FES_REALNEXT_QSRCHCP_GoDown(4)
	if ( key == 33 ) FES_REALNEXT_QSRCHCP_GoUp(4)
	if ( key == 40 ) FES_REALNEXT_QSRCHCP_GoDown(1)
	if ( key == 38 ) FES_REALNEXT_QSRCHCP_GoUp(1)
}

function FES_REALNEXT_QSRCHCP_KeyUp(e)
{
  	var key = window.event ? window.event.keyCode : e.which;

	if ( !FES_REALNEXT_QSRCHCP_enabled ) return
	if ( key == 13 ) FES_REALNEXT_QSRCHCP_Enter()
	if ( key == 27 ) FES_REALNEXT_QSRCHCP_Enter()
	if ( key == 9 ) FES_REALNEXT_QSRCHCP_Enter()
}

function FES_REALNEXT_QSRCHCP_Enter(bEnter)
{
   	if ( !FES_REALNEXT_QSRCHCP_browserOk ) return
	if ( FES_REALNEXT_QSRCHCP_show == true )
	{
        FES_REALNEXT_QSRCHCP_CheckAutocompleteSubmit();
		FES_REALNEXT_QSRCHCP_Hide()
		FES_REALNEXT_QSRCHCP_input.focus()
	}
}

function FES_REALNEXT_QSRCHCP_GoDown(n)
{
	if ( !FES_REALNEXT_QSRCHCP_browserOk ) return
	if ( FES_REALNEXT_QSRCHCP_focused == false ) return
	if ( FES_REALNEXT_QSRCHCP_show == false ) FES_REALNEXT_QSRCHCP_Check()
	n = FES_REALNEXT_QSRCHCP_hover + n
	if ( !window.itemCount ) return
	if ( n > itemCount-1 )
	{
		n = 0
	}
	FES_REALNEXT_QSRCHCP_KeyDownLink(n)
}

function FES_REALNEXT_QSRCHCP_GoUp(n)
{
	if ( !FES_REALNEXT_QSRCHCP_browserOk ) return
	if ( FES_REALNEXT_QSRCHCP_focused == false ) return
	if ( FES_REALNEXT_QSRCHCP_show == false ) FES_REALNEXT_QSRCHCP_Check()
	n = FES_REALNEXT_QSRCHCP_hover - n
	if ( n < 0 )
	{
		n = itemCount-1
	}
	FES_REALNEXT_QSRCHCP_KeyDownLink(n)
}

function FES_REALNEXT_QSRCHCP_CheckAutocompleteSubmit()
{
	if (( FES_REALNEXT_QSRCHCP_focused == true && FES_REALNEXT_QSRCHCP_show == true ) || FES_REALNEXT_QSRCHCP_cancelEnterSubmit == true )
	{
		FES_REALNEXT_QSRCHCP_Hide()
		FES_REALNEXT_QSRCHCP_input.value = FES_REALNEXT_QSRCHCP_places[FES_REALNEXT_QSRCHCP_hover]
		FES_REALNEXT_QSRCHCP_cancelEnterSubmit = false
		return false
	}
	
	return CheckInput()
 }

function FES_REALNEXT_QSRCHCP_Click(s)
{
	FES_REALNEXT_QSRCHCP_input.value = s
	FES_REALNEXT_QSRCHCP_show = false
	document.getElementById(FES_REALNEXT_QSRCHCP_Constants.LIST_ID).style.display = 'none'
	if ( IE && !IE50 ) document.getElementById(FES_REALNEXT_QSRCHCP_Constants.FRAME_CONTAINER_ID).style.display = 'none'
	FES_REALNEXT_QSRCHCP_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;
}

