ObjectSearch = function()
{
    this.regionsList = null;
    this.placeList = null;
    this.locationsListBox = null;

    this.regionsValue = null;
}

ObjectSearch.init = function()
{

    window.objectSearchInstance = new ObjectSearch();
    window.objectSearchInstance.assignFormHandlers();
}

ObjectSearch.prototype.assignFormHandlers = function()
{
    this.regionsList = document.getElementById('search-region');
    this.placeList = document.getElementById('search-place');
    this.locationsListBox = document.getElementById('searchPlaceWrapper');
    this.order_by_Box = document.getElementById('order_by');
    
    if (
        (!this.regionsList)
        ||
        (!this.locationsListBox)
    )
    {
        return null;
    }

    if (this.order_by_Box){
        this.order_by_Box.onchange = function (){
            this.order_by_form = document.getElementById('order_by_form');
            if (this.order_by_form){
                this.order_by_form.submit();
            }
        }
    }


    var self = this;
    this.regionsList.onchange = this.regionsList.onfocus =
    this.regionsList.onkeyup = this.regionsList.onclick =
    function ()
    {
        window.title = Math.random();
        self.regionChanged();
    }

    this.regionChanged ( );
    street_AutoCompleter();
    
    
    this.regionsList.onchange =
    function ()
    {
        street_AutoCompleter();
    }

    this.objectType = document.getElementById('object_type');
    if (this.objectType){
        this.objectType.onchange = this.objectType.onfocus =
        this.objectType.onkeyup = this.objectType.onclick =
        function ()
        {
            self.objectTypeChanged();
        }
    }
}

ObjectSearch.prototype.objectTypeChanged = function(  )
{
    var tikai_jaunie_box = document.getElementById('tikai_jaunie_box');
    var el = document.getElementById('object_type');
    if (tikai_jaunie_box && el)
    {
        if (el.value == 'DZ' || el.value == 'SM')
    	{
    		document.getElementById('tikai_jaunie_box').style.display = 'block';
    	}
    	else
    	{
    		document.getElementById('tikai_jaunie_box').style.display = 'none';
    	}
    }
}


ObjectSearch.prototype.regionChanged = function(  )
{

    var siteUrl = document.getElementById('siteUrl');

    if (!siteUrl)
    {
        return;
    }
    siteUrl = siteUrl.value;
    if (!siteUrl)
    {
        return;
    }


    // first call onlaod
    // alert (siteUrl);

    if (this.regionsValue === null)
    {
        this.regionsValue = this.regionsList.value;
    }

    

    if (this.regionsValue == this.regionsList.value)
    {
        return;
    }
    
    this.regionsValue = this.regionsList.value;

    // build url
    var requestUrl = siteUrl.concat('?module=object_db&do=place_list&district=', this.regionsValue);
    loadXmlHttp(requestUrl, ObjectSearch.handleResponse, this, null, null, true);
}

ObjectSearch.handleResponse = function(xmlhttp, main)
{
    // alert ('response');
    // alert (xmlhttp.responseText);
    // alert (main.locationsList.tagName);

    main.locationsListBox.innerHTML = xmlhttp.responseText;
}

var js_search_old_onload = window.onload;
window.onload = function()
{
	if (typeof js_search_old_onload == 'function')
	{
		 js_search_old_onload();
	}
	ObjectSearch.init();
//	set_tikai_jaunie();
}

function set_tikai_jaunie(){
	var el = document.getElementById('object_type');
	if(!el)
	{
		return;
	}
	if (el.value == 'DZ' || el.value == 'SM')
	{
		document.getElementById('tikai_jaunie_box').style.display = 'block';
	}
	else
	{
		document.getElementById('tikai_jaunie_box').style.display = 'none';
	}
} 

function check_tikai_jaunie(){
	if (document.getElementById('tikai_jaunie_box').style.display == 'block' &&
	   	document.getElementById('tikai_jaunie').value == 'on')
	{
		var form = document.getElementById('objSearcher');
		var select = document.createElement('select');
		select.setAttribute('multiple', 'multiple');
		select.setAttribute('name', 'DZ_PROJEKTS_LK[]');
		select.setAttribute('id', 'DZ_PROJEKTS_LK');
		//form.appendChild(select);
		//alert(document.getElementById('tikai_jaunie').value);
	}
} 

function street_AutoCompleter(){
    var siteUrl = document.getElementById('siteUrl');
    var region = document.getElementById('search-region');
    var place = document.getElementById('search-place');
    var placeValue=place.value;

    if (placeValue==''){
        placeValue='N';
    }

    var myAutocompleter=false;
	var inputWord = $('street');
	var myAutocompleter = new Autocompleter.Request.JSON(inputWord, siteUrl.value+'?module=object_db&do=street_suggest&region='+region.value+'&place='+placeValue, {});
}
