﻿function refSearchAll(){
    var btnReferentieZoeken = $("#btnReferentieZoeken");
    //var modal_reference = document.getElementById("modal_reference");
    var txtSearch = txtReferentie;
    $(btnReferentieZoeken).click(function() {
    if ((txtSearch.value != 'Zoek op referentie') && (txtSearch.value.length > 1)) {
        Ajax_Search.SearchAllReferences(txtSearch.value, btnReferentieZoeken.callBack);
    }
    });
    
    btnReferentieZoeken.callBack = function(result) 
    {
        try {
            var modal_reference = document.getElementById("modal_reference");
            if (modal_reference.hasChildNodes()) {
                while (modal_reference.childNodes.length >= 1) 
                {modal_reference.removeChild(modal_reference.firstChild);}
            }
            
            var div = document.createElement("div");
            div.setAttribute("height", "20px");
            div.innerHTML = "&nbsp;";
            modal_reference.appendChild(div);
            
            if (result.length == 0)
            {
                return false;
            }
            if (result.length == 1)
            {
                window.location = "pand_detail.aspx?pandid=" + result[0].Key;
                return false;
            }
               
            for (var i = 0; result.length > i; i++) 
            {
                var child = document.createElement("div");
                child.style.paddingLeft= "10px";
                var a = document.createElement("a");
                a.innerHTML = result[i].Value;
                
                if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                {   
                a.setAttribute('className', 'hypReference');
                }
                else
                {
                a.setAttribute("class", "hypReference");
                }
                a.href = "pand_detail.aspx?pandid=" + result[i].Key;
                child.appendChild(a);
                modal_reference.appendChild(child);
            }
            //modal_reference.style.display = "block";
            //searchBox.style.display = "none";
            $('#modal_reference').dialog('open');
        } 
        catch (ex) 
        { }
    }
};



var $ = jQuery.noConflict();
$(document).ready(function() {
    $("#modal_reference").dialog
        ({
            modal: true,
            autoOpen: false,
            resizable: false,
            width: 620,
            minHeight: 300,
            maxHeight: 500,
            open: function(type, data) {
                $(this).parents(".ui-dialog").wrap(
                  '<div class="modal_class"></div>'
                );
                $("div.scrollable").scrollable({
                    vertical: true,
                    size: 10
                });
            },
            close: function(type, data) {
                var wrapper = $(this).parents(".modal_class");
                wrapper.replaceWith(wrapper.children());
            },
            buttons:
            {
                "Cancelar": function() {
                    $(this).dialog("close");
                }
            }
        });
        refSearchAll();
});
