﻿addNamespace("Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls")
Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls.SimpleSearchControl = function()
{

    this.strElementSearchBoxId = 'tbxSearchTerm'
    this.strElementSearchButtonId = 'btnSearch'
    this.strElementRadioSelectionId = 'rdoSearchSelection'
    this.strElementRadioAllId = 'rdoSearchAll'
    
    this.tbxSearchBox = null
    this.boolDefaultSearchAll = true
    this.btnSearch = null
    this.rdoSearchAll = null
    this.rdoSearchSelection = null  
    
    this.strDefaultPhrase = null  
    this.strDefaultPhraseClass = null
    this.strSearchFilterKey = '__SEARCH__'
    this.strCurrentPhrase = ''
   
    this.strSearchUrlAll = ''
    this.strSearchUrlSelection = ''
    this.strSearchUrlClear = ''
}

Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls.SimpleSearchControl.prototype.Initialize = function()
{
    this.tbxSearchBox = document.getElementById(this.strElementSearchBoxId)
    this.btnSearch = document.getElementById(this.strElementSearchButtonId)
    this.rdoSearchAll = document.getElementById(this.strElementRadioAllId)
    this.rdoSearchSelection = document.getElementById(this.strElementRadioSelectionId)
    
    if(this.tbxSearchBox == null)
    {
        alert('Could not find searchbox for simple search')
        return
    }
    Aspacts.Idios3.I_CAP.Scripting.Core.ImplementEvent(this.tbxSearchBox)
    this.tbxSearchBox.refCallBack = this
    this.tbxSearchBox.addEventListener('keydown', function(evt){this.refCallBack.HandleKeyPress(evt||window.event)}, true)
    this.tbxSearchBox.value = this.strCurrentPhrase
   
   if(this.rdoSearchAll && this.rdoSearchSelection)
   {
       this.rdoSearchAll.checked = this.boolDefaultSearchAll
       this.rdoSearchSelection.checked = !this.boolDefaultSearchAll
   }
    
    if (this.strDefaultPhrase != null)
    {
        
        this.tbxSearchBox.addEventListener('click', function(evt){this.refCallBack.HandleFocus()}, true)
        this.tbxSearchBox.addEventListener('blur', function(evt){this.refCallBack.HandleBlur()}, true)
        this.HandleBlur()
    }
    
    if(this.btnSearch != null)
    {
        Aspacts.Idios3.I_CAP.Scripting.Core.ImplementEvent(this.btnSearch)
        this.btnSearch.refCallBack = this
        this.btnSearch.addEventListener('click', function(evt){this.refCallBack.ExecuteSearch(evt)}, true)
    }
	
    
}

Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls.SimpleSearchControl.prototype.HandleFocus = function()
{
    if (this.tbxSearchBox.value == this.strDefaultPhrase || trim(this.tbxSearchBox.value)=='')
    {
        this.tbxSearchBox.value = ""  
        if (this.strDefaultPhraseClass != null)
        {
            this.tbxSearchBox.className = this.tbxSearchBox.className.replace(this.strDefaultPhraseClass, '')
        }
    }
}

Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls.SimpleSearchControl.prototype.HandleBlur = function()
{
    if (this.tbxSearchBox.value == this.strDefaultPhrase || trim(this.tbxSearchBox.value)=='')
    {
        this.tbxSearchBox.value = this.strDefaultPhrase
        if (this.strDefaultPhraseClass != null)
        {
             this.tbxSearchBox.className += " " + this.strDefaultPhraseClass
        }
    }
}


Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls.SimpleSearchControl.prototype.ExecuteSearch = function(evt)
{
    var boolSearchAll =  this.boolDefaultSearchAll
    if (this.rdoSearchAll && this.rdoSearchAll.checked)
    {
        boolSearchAll = true
    }
    if (this.rdoSearchSelection && this.rdoSearchSelection.checked)
    {
        boolSearchAll = false
    }

    var strBaseUrl = this.strSearchUrlSelection
    if (boolSearchAll)
    {
        strBaseUrl = this.strSearchUrlAll
    }
      
    var strSearchPrase = trim(this.tbxSearchBox.value.replace(/[<>"'=\/\\]/g, ' '))
    if (strSearchPrase == '' || strSearchPrase == this.strDefaultPhrase)
    { 
        location.href=this.strSearchUrlClear
        Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt);
		return false;
    }
   	
	strBaseUrl = strBaseUrl.replace(encodeBase64(this.strSearchFilterKey), encodeBase64(strSearchPrase))
	location.href = strBaseUrl
	
    Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt);
	return false;
}

Aspacts.Products.FacetBaseJaarverslag.FacetBase.Templating.Controls.SimpleSearchControl.prototype.HandleKeyPress = function(evt)
{
    if (evt.keyCode ==13)
	{
		this.ExecuteSearch()
		Aspacts.Idios3.I_CAP.Scripting.Core.CancelEvent(evt);
		return false;
	}
}
