
var webServiceURL = '/xNet/MainSite/WebServices/Address/Geolookup.asmx'
var citySuggestDiv = 'citySuggest';

//First, if we can't get a reference to these elements don't bother with all the code below
var scriptsCoreFolder = "/xNet/MainSite/Scripts/Core/";

//Check for dependancies!
//Order IS important here! 
if(!LNJS.Trace)
    document.write('<sc'+'ript language="javascript">LNJS.Trace=({__className:"Trace",debug:function(){},source:function(){},info:function(){},warn:function(){},error:function(){},fatal:function(){},system:function(){},write:function(){}});function jsTrace(){}</sc'+'ript>');
if(!LNJS.__xml)
    document.write('<sc'+'ript src="'+ scriptsCoreFolder +'xml.js"></sc'+'ript>');
if(!LNJS.__effects)
    document.write('<sc'+'ript src="'+ scriptsCoreFolder +'effects.js"></sc'+'ript>');
if(!LNJS.__ajax)
    document.write('<sc'+'ript src="'+ scriptsCoreFolder +'ajax.js"></sc'+'ript>');
if(!LNJS.__suggester)
    document.write('<sc'+'ript src="'+ scriptsCoreFolder +'Widgets/suggest.js"></sc'+'ript>');    


//Source js for Suggest Widget for debugging
/*
document.write('<sc'+'ript src="'+ scriptsCoreFolder +'Source/Widgets/suggest.js/base.js"></sc'+'ript>');    
document.write('<sc'+'ript src="'+ scriptsCoreFolder +'Source/Widgets/suggest.js/ajax.js"></sc'+'ript>');    
document.write('<sc'+'ript src="'+ scriptsCoreFolder +'Source/Widgets/suggest.js/local.js"></sc'+'ript>'); 
*/
var GeoSuggest = {
    currentText: "",
    lastText: "",
    init: function(editField, updateElement, url) {
        //check if we have an input
        if(!$(editField))return;
    
        //set up location suggestion
        this.suggestIndex = -1;
        this.suggestList = null;        
        this.Suggest = new LNJS.Ajax.Suggester(editField, updateElement, url, this.options);
        this.lastText = this.Suggest.element.value;
        this.currentText = this.Suggest.element.value;
        this.searchToken = null;
    },
    getSuggestion: function() {
        //Grab selection from suggestion dropdown
        var index = window.GeoSuggest.Suggest.getCurrentEntry();
        var pasteString = window.GeoSuggest.Suggest.getUpdateValue(index);
        //Begin ugly hack/strip
        //1. When pasting either us zip or ca zip do not paste in paranthesis portion
        pasteString = pasteString.replace(/\s\(.*\)/, '');
        return pasteString;
    },
    options: {
        queueLength: 1,
        groupName: 'RemUnivSuggest',
        defaultindex: -1,
        useLocalCache: true,
        frequency: .01,
        turnOffFading: true,
        minChars: 3,
        ServiceSourceCode: 10,
        clonePosition: false,
        SOAPMethod: 'GetQuickSearchSuggestions',
        SOAPNamespace: 'http://www.loopnet.com/WebServices',
        SOAPRequestAttributes: 'UsSearchOnly="true" SortByPopulation="true"', //Need to rewrite this to use key/value system
        processResults: function(req) {
            if (LNJS.isNull(req)) return;

            var nList;
            var searchToken = window.GeoSuggest.Suggest.getToken();
            window.GeoSuggest.searchToken = searchToken;
            if (!(req.documentElement)) {
                //Received from true ajax response obj
                var pfResponseInfo = req.responseXML.documentElement.selectSingleNode("//PFResponseInfo");
                if (pfResponseInfo.attributes[0].nodeValue == -1) return;
                nList = req.responseXML.documentElement.selectSingleNode("//AddressSuggestionList");

            } else {
                //Received from local cache
                nList = req.documentElement.selectSingleNode("//AddressSuggestionList");
            }

            if (nList != null) {
                //if a different request came back then what we were expecting just return
                if (searchToken != nList.getAttribute("GeoString")) return;
            
                var oSuggestList = nList.selectNodes("AddressSuggestion");
                //parse results into list
                //format results in so that the suggest widget can use them			
                var sHtml = new Array();
                sHtml.push("<ul>");
                var len = oSuggestList.length;
                for (i = 0; i < len; i++) {
                                        var currentSuggestion = oSuggestList[i];                    var suggString = currentSuggestion.selectSingleNode("SuggestionText").firstChild.nodeValue;
                    var isExactMatch =  ( currentSuggestion.getAttribute("ExactMatch") == "True" );
                    
                    //1. Boldify the prefix searched on
                    var splitPoint = 0;
                    while( searchToken.charAt(splitPoint).toLowerCase() === suggString.charAt(splitPoint).toLowerCase() && splitPoint < searchToken.length)
	                    splitPoint++;                    var prefix = suggString.substring(0, splitPoint);
                    var suffix = suggString.substring(splitPoint, suggString.length);
                    var resultSuggestion = "";
                    
                    if(isExactMatch)//full bold on exact match
                        resultSuggestion = "<b>" + suggString + "</b>";
                    else if( currentSuggestion.getAttribute("Address1") == null )//partial prefix bold if it's not an address
                        resultSuggestion = "<b>" + prefix + "</b>" + suffix;
                    else   
                        resultSuggestion = prefix + suffix; //no prefix highlighting at all.
                     
                    sHtml.push("<li><span title=\"" + suggString + "\">" + resultSuggestion + "</span></li>");
                }
                sHtml.push("</ul>");
                //call update on suggest widget
                window.GeoSuggest.Suggest.updateChoices(sHtml.join(""));
            }
            else 
            {	//no results hide it
                window.GeoSuggest.Suggest.hide();
            }
        },
        filterToken: function(token) {
            //This is an opportunity to filter the input before it gets sent to server.    
            var re = new RegExp(/[^'\w\s-,]/);
            token = token.replace(re, '');
            return token;
        },
        onKeyUp: function(ev) {
                        if (window.GeoSuggest.Suggest.index > -1 && window.GeoSuggest.Suggest.index < window.GeoSuggest.Suggest.entryCount) {
                window.GeoSuggest.Suggest.element.value = window.GeoSuggest.getSuggestion();
            } else {
                window.GeoSuggest.Suggest.element.value = window.GeoSuggest.searchToken;
            }
        },
        onKeyDn: function(ev) {
            if (window.GeoSuggest.Suggest.index > -1 && window.GeoSuggest.Suggest.index < window.GeoSuggest.Suggest.entryCount) {
                window.GeoSuggest.Suggest.element.value = window.GeoSuggest.getSuggestion();
            } else {
                window.GeoSuggest.Suggest.element.value = window.GeoSuggest.searchToken;
            }
        },
        //Keep this around for mouseclick to use our handler and not default
        updateElement: function(index) {
            if (window.GeoSuggest.Suggest.visible && window.GeoSuggest.Suggest.index > -1 && window.GeoSuggest.Suggest.index < window.GeoSuggest.Suggest.entryCount) {
                window.GeoSuggest.Suggest.element.value = window.GeoSuggest.getSuggestion();
                window.GeoSuggest.Suggest.element.focus();
            }
        },
        //this method fired when enter (return) is pressed in the textbox
        onReturn: function(ev) {
            
            if (window.GeoSuggest.Suggest.visible && window.GeoSuggest.Suggest.index > -1 && window.GeoSuggest.Suggest.index < window.GeoSuggest.Suggest.entryCount) {
                window.GeoSuggest.Suggest.element.value = window.GeoSuggest.getSuggestion();
                window.GeoSuggest.Suggest.element.focus();
            }
            window.QuickSearch.createAndSendFormHandler(null);
        },
        onChange: function() {
            //nothing        },
        onComplete: function() {
            //nothing        }
    }
};

LNJS.addObserver({onPageLoad:function()
{	
    
    var locationField = null;
    
    if( LNJS.QuickSearchLocationField )
        locationField = LNJS.QuickSearchLocationField;  //All other the field name is decided based on the ClientID control hierarchy
    else 
        locationField = 'UniversalSearch1_Location'; //SEO static page uses this field (pages that are generated by LNIndexPageCreator)
        
    GeoSuggest.init(locationField,citySuggestDiv,webServiceURL);
}});

function GetCatName(QS_CategoryID,QS_SearchType)
{
    var CatName = "All Property Types";
    if(QS_SearchType == "FS")
    {
        switch(QS_CategoryID)
        {
           case '70':CatName = 'Multifamily';break;
           case '80':CatName = 'Office';break;
           case '40':CatName = 'Industrial';break;
           case '90':CatName = 'Retail';break;
           case '110':CatName = 'Shopping Center';break;
           case '50':CatName = 'Land';break;
           case '10':CatName = 'Agricultural';break;
           case '60':CatName = 'Hotel &amp; Motel';break;
           case '100':CatName = 'Senior Housing';break;
           case '30':CatName = 'Health Care';break;
           case '130':CatName = 'Sport &amp; Entertainment';break;
           case '120':CatName = 'Special Purpose';break;
           case '140':CatName = 'Residential Income';break;
           default: break;     
        }
   }
   else  if(QS_SearchType == "FL")
   {
        switch(QS_CategoryID)
        {
            case '40': CatName = 'Industrial';break;
            case '80': CatName = 'Office';break;
            case '90': CatName = 'Retail';break;
            case '110': CatName = 'Shopping Center';break;
            case '50': CatName = 'Land';break;
            case '120': CatName = 'Special Purpose';break;            
            default: break;
        }
   }
  return CatName;
};