﻿
/*
* Search part
*/
Parts.SingleSearch = function (id){
    Parts.SingleSearch.initializeBase(this, [id]);

    this.controls = ""; //controls and their corresponding parameter names
    this.page =1; //paging of search results
//    this.txtAll = "";
//    this.aFillOnAll;
//    this.aDllFilter;
//    this.aQuick;
//    this.txtQuick;
    this.MIN_SCALE_ZOOM = 50000;
    this.searchString = "";
}

/*
* Init
*/
Parts.SingleSearch.prototype.init= function(){
    
    //List of application controls to hide when presenting serach results
    //IE6 fix as select's is on top of div's
    this.searchResHideControlList =  Global.DataController().getData("Search.searchResHideControlList");
    
    //disable controls
    for(var i=1; i<this.aDllFilter.length; i++){
        var ddl = this.elm(this.aDllFilter[i])
        ddl.disabled=true;
        ddl.options[ddl.options.length] = new Option(this.txtAll, "-1");
    }
}


/*
* Zoom to location in map based on value of ddl with format: id|llx|lly|urx|ury
*/
Parts.SingleSearch.prototype.zoom=function(ddlSource, zoomScale){
    var zoom = this.MIN_SCALE_ZOOM;
    if(zoomScale){
        zoom = zoomScale;
    }


    var aVal = Utils.getSelectedValue(this.elm(ddlSource)).split("|");
   
    if(aVal.length==5){
        MapApi.zoomToWorldRect(aVal[1], aVal[2], aVal[3], aVal[4], zoom);
    } 
}

/*
* Request server for data based on paging and value of controls
*/
Parts.SingleSearch.prototype.onSearch = function(page){
    this.elm("divSearchResult").style.display = "block";
    this.elm("divSearchLoading").style.display = "block";
    this.elm("spanSearchResult").innerHTML = "";
    var aParams = this.getParams();
    //if(aParams == null){return;}
    //new Array();
    
    //page of search result
    this.page = page; 
    if(this.page<1) this.page=1;

    aParams[aParams.length] = "Page";
    aParams[aParams.length] = this.page;
    
    Global.DataController().getServerData(
        "Search", 
        aParams, 
        this._id, 
        Function.createDelegate(this, this.onSearchReturned) , 
        Function.createDelegate(this, this.onSearchError), 
        ""
    );
}


//return parameters for search
Parts.SingleSearch.prototype.getParams = function(){

    var aWords = new Array();
    var streetOrTown = "";
    var number = "";
    var town = "";
    var postcode = "";
    var type = "";
    

    var inputString = this.elm("txtSearch").value.replace(/^\s+|\s+$/g, ''); //regexp strips whitespace
    this.seachString = inputString;
    //if(inputString.length == 0){return null;}
    
    aWords = inputString.split(' ');
    
    //1. String with only words, no digits
    if(!inputString.match(/\d+/)){
        streetOrTown = aWords.join('%') + '%';
        type = "streetOrTown";
    }
    
    //2. Postcode and maybe some words
    else if(inputString.match(/\d{4}/)){
        var i = 0;
        for(i=0; i<aWords.length ; i++){ 
            if(aWords[i].match(/\d{4}/) && aWords[i].length == 4){
                postcode = aWords[i];
            }else{
                streetOrTown += aWords[i] + '%';
            }
        }
        
        if(postcode.length == 0){
            type = "streetOrTown"; 
        }
        else{ type = "postcode"; }
    }
    
    //3. Address
    else if(inputString.match(/\d+/)){
        //3a. Address only (name and number)
        var i = 0;
        for(i=0; i<aWords.length; i++){
            if(aWords[i].match(/\d+/)){
                number = aWords[i];
                break;
            }else{
                streetOrTown += aWords[i] + '%';
            }
        }
        type = "adress";

        //3b. Address and town (name, number, name)
        if( i < aWords.length-1){
            i++; //increment so we pass over number
            for(i; i<aWords.length; i++){
                town += aWords[i] + '%';
            }
            type = "adressAndTown";
        }    
    }
   
    //alert("Gate/by: " + streetOrTown);
    //alert(number);
    //alert(town);
    //alert(postcode);
    //alert(type);
    //alert("|"+inputString+"|");
    
    var aParams = new Array();
    
    aParams[aParams.length] = "Type";
    aParams[aParams.length] = type;
    aParams[aParams.length] = "streetOrTown";
    aParams[aParams.length] = streetOrTown;
    aParams[aParams.length] = "postcode";
    aParams[aParams.length] = postcode;
    aParams[aParams.length] = "number";
    aParams[aParams.length] = number;
    aParams[aParams.length] = "town";
    aParams[aParams.length] = town;
    return aParams;
}

/*
* Fill search div with search results.
* data[0]: returned html
* data[1]: indicator for more pages
*/
Parts.SingleSearch.prototype.onSearchReturned=function(data){
    //First we hide window controls (not windowless) which is located under the div
    Utils.setControlVisibility(this.searchResHideControlList, 'hidden')
    
    $get('divSearchResult').style.display="block";
    if(this.elm("divSearchLoading")){this.elm("divSearchLoading").style.display = "none";}
    
    //if more pages
    if(data[1] == true){
        $get('btnSearchNextPage').style.display='inline';
    }
    else{
        $get('btnSearchNextPage').style.display='none'; 
    }
    
    //if first page
    if(this.page==1){
        $get('btnSearchPrevPage').style.display='none';
    }
    else{
        $get('btnSearchPrevPage').style.display='inline'; 
    }
    
    
    //if data returned
    if(data[0]!=null){
        $get('spanSearchResult').innerHTML=data[0];
    }
    else{
        $get('spanSearchResult').innerHTML= Global.DataController().getData(this.getId() + ".NoHits");
    }
    
    // Telenor statistics script snippet
    if(s_gi){
        var s=s_gi('telenorprod');
        //var s = s_gi('telenordev');
        s.linkTrackVars = 'prop3,prop7';
        s.prop3 = (escape(this.seachString)).replace(/\//g, '|'); //correct & not a comment. ignore the syntax hilite 
        s.prop7 = data[1] == false ? String(data[2]) : '>' + data[2];
        void(s.tl(true,'o','P-Dekningskart Søk'));
    } 
    
    //alert("hits (s.prop7): " + s.prop7 + " morepages? " + data[1]);  
}

/*
* Error in search part
*/
Parts.SingleSearch.prototype.onSearchError=function(e){
   alert("onSearchError" + e.get_message());
   this.onSearchReturned(null);
}

/*
* Reset search results
*/
Parts.SingleSearch.prototype.onReset=function(){
    // unhide hidden controls
    Utils.setControlVisibility(this.searchResHideControlList, 'visible')
     
    this.elm('divSearchResult').style.display='none';
    //this.elm('txtSearch').value="";
        
    return false;
}


Parts.SingleSearch.prototype.onKeyUp = function(e){ //e is event object passed from function invocation
    var characterCode; //literal character code will be stored in this variable
    if(e && e.which){ //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    }
    else{
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }
    //alert(characterCode);
    if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
        this.onSearch(1); //if enter, do search.
        return true; 
    }else{
        return false;
    }
}

/*
* Get next page of data
*/
Parts.SingleSearch.prototype.nextPage=function(increment){
    this.onSearch(increment+this.page);
}

/*
* Get next page of data
*/
Parts.SingleSearch.prototype.sufficentLetters=function(increment){
    var numLetters= Global.DataController().getData("Search.NumLetters");
    if(numLetters>=$get('txtSearch').value.length){
        return false;
    }
    else{
        return true;
    }
}

/*
*  Inherit from part base class.
*/
Parts.SingleSearch.registerClass('Parts.SingleSearch', Parts.Part, Sys.IDisposable);

