﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("dk.advokatnoglen");

dk.advokatnoglen.Search = function(fields,tabs,buttons,areas,selSubArea,municipalities,selMunicipality,tabIndex,country) {
    this._fields = fields;
    this._nameField = fields[0];
    this._companyField = fields[1];
    this._areaSelect = fields[2];
    this._subAreaSelect = fields[3];
    this._freetextField = fields[4];
    this._courtSelect = fields[6];
    this._zipFrom = fields[7];
    this._zipTo = fields[8];
    this._municipalitySelect = fields[9];
        
    this._tabs = tabs;
    this._tabIndex = (tabIndex.length!==0?parseInt(tabIndex):0);
    
    this._resetButton = buttons[0];
    this._searchButton = buttons[1];
    
    this._areas = areas;
    this._selectedSubArea = selSubArea;
    
    this._municipalities = municipalities;
    this._selectedMunicipality = selMunicipality;    
    
    this._baseUrl = "/sog.aspx";
    this._country = country;
}

dk.advokatnoglen.Search.prototype = {
    initialize : function() {
        $addHandler(window, "unload", Function.createDelegate(this, this.dispose));
    
        // tab events
        Array.forEach(this._tabs, function(item) {
            $addHandler(item.button, "click", Function.createDelegate(this, this._onTabClick));                
        }, this);        
    
        // button events
        $addHandler(this._searchButton, "click", Function.createDelegate(this, this._onSearchClick));
        $addHandler(this._resetButton, "click", Function.createDelegate(this, this._onResetClick));
        
        // common field events
        Array.forEach(this._fields, function(field) {
            //$addHandler(field, "keyup", Function.createDelegate(this, this._onFieldKeyPress));
            $addHandler(field, "keydown", Function.createDelegate(this, this._onFieldKeyPress));
        }, this);
        
        // specific field events
        $addHandler(this._areaSelect, "change", Function.createDelegate(this, this._onAreaChange));
        $addHandler(this._courtSelect, "change", Function.createDelegate(this, this._onCourtChange));
        
        // initialize area selection
        this._onAreaChange();
        if(this._selectedSubArea)
            this._subAreaSelect.value = this._selectedSubArea;
        
        this._onCourtChange();
        if(this._selectedMunicipality)
            this._municipalitySelect.value = this._selectedMunicipality;
        
        this._updateTabSelection();
    },
           
    selectCourt : function(retskredsNr) {
        this._courtSelect.value = retskredsNr;
        this._onCourtChange();
    },
    
    _onCourtChange : function() {
        var selectedCourt = parseInt(this._courtSelect.value);
        var munis = this._municipalities[selectedCourt];
        if(munis!=null) {       
            this._municipalitySelect.innerHTML = "";
            this._addOption(this._municipalitySelect, "", "Vælg evt. kommune");
            for(var key in munis) {
                this._addOption(this._municipalitySelect, key, munis[key]);
            }            
            this._municipalitySelect.style.visibility = "visible";
        } else {
            this._municipalitySelect.style.visibility = "hidden";
        }        
    },
    
    _onResetClick : function() {
        this.reset();
    },
    
    reset : function() {
        if(confirm("Er du sikker på at du ønsker at nulstille alle felter?"))
            location.href = "/?r=1";    
    },
    
    _addOption : function(selectObj, key, text) {
	    var opt = document.createElement("option");
		opt.appendChild(document.createTextNode(text));
		opt.setAttribute("value", key);		
		if(selectObj==key)
		    opt.setAttribute("selected", "selected");		
		selectObj.appendChild(opt);  
    },
    
    _onAreaChange : function() {
        var selectedArea = parseInt(this._areaSelect.value);
        var subAreas = this._areas[selectedArea];
        if(subAreas!=null) {       
            this._subAreaSelect.innerHTML = "";
            this._addOption(this._subAreaSelect, "", "Uddyb evt. arbejdsområde");
            for(var key in subAreas) {
                this._addOption(this._subAreaSelect, key, subAreas[key]);
            }            
            this._subAreaSelect.style.visibility = "visible";
        } else {
            this._subAreaSelect.style.visibility = "hidden";
        }
    },
    
    _onTabClick : function(e) {
        this._tabIndex = parseInt(e.target.id.substring(2), 10);
        this._updateTabSelection();
    },
    
    _updateTabSelection : function() {
        Array.forEach(this._tabs, function(item) {
            item.tab.style.display = "none";
            item.button.className = "tab";
            item.tab.blur();
        });
        
        this._tabs[this._tabIndex].tab.style.display = "block";
        this._tabs[this._tabIndex].button.className = "tab selected";    
    },
    
    _onSearchClick : function() {
        this._search();
    },
    
    _onFieldKeyPress : function(e) {
        if(e.keyCode===13) {
            e.preventDefault();
            e.stopPropagation();
            this._search();
        }
    },
    
    listForeign : function(countryCode) {
        location.href = this._baseUrl + "?s=1&fn=" + countryCode;
    },
    
    back : function() {
        this._search();
    },
    
    page : function(index) {
        this._search(index);
    },
    
    _search : function(pageIndex) {
        var sb = new Sys.StringBuilder();
        sb.append("s=1&t="+encodeURIComponent(this._tabIndex));
        var baseLength = sb.toString().length;
        
        if(this._nameField.value.length!==0)
            sb.append("&name="+encodeURIComponent(this._nameField.value));                
        
        if(this._companyField.value.length!==0)
            sb.append("&firm="+encodeURIComponent(this._companyField.value));

        if(this._areaSelect.value.length!==0) {
            sb.append("&a="+encodeURIComponent(this._areaSelect.value));

            if(this._subAreaSelect.value.length!==0)            
                sb.append("&sa="+encodeURIComponent(this._subAreaSelect.value));            
        }            

        if(this._freetextField.value.length!==0)
            sb.append("&ft="+encodeURIComponent(this._freetextField.value));        

        // tabs
        if(this._tabIndex===0) {
            if(this._courtSelect.value.length!==0) {
                sb.append("&c="+encodeURIComponent(this._courtSelect.value));        
                
                if(this._municipalitySelect.value.length!==0)            
                    sb.append("&sm="+encodeURIComponent(this._municipalitySelect.value));
            }            
        } else {        
            if(this._zipFrom.value.length!==0)
                sb.append("&zf="+encodeURIComponent(this._zipFrom.value));
            if(this._zipTo.value.length!==0)
                sb.append("&zt="+encodeURIComponent(this._zipTo.value));
        }
        
        // country
        if(sb.toString().length===baseLength && this._country.length!==0)
            sb.append("&fn=" + encodeURIComponent(this._country));
        
        // paging
        if(typeof(pageIndex)==="number")
            sb.append("&p=" + pageIndex);
        
        var criteria = sb.toString();
        if(criteria.length===baseLength) {
            alert("Vælg en eller flere kriterier for at søge.");
            return;
        }

        location.href = this._baseUrl + "?" + criteria;
    },
    
    dispose : function() {    
        // todo: clear handlers
    }
};

dk.advokatnoglen.Search.registerClass("dk.advokatnoglen.Search");