﻿        function XmlHttp( ){
            this.CreateXmlHttpObject = CreateXmlHttpObject;
            this.GetUrlContent = GetUrlContent;
            this.GetResponseText = GetResponseText;
            this.GetReadyState = GetReadyState;           
            this.HttpMethod = 'GET'; // default
            this.objXmlHttp = this.CreateXmlHttpObject();
        }

        // Initialize XMLHttpObject
        function CreateXmlHttpObject(){
            var xmlhttp=false;
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
   

        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
            xmlhttp = new XMLHttpRequest();
        }
            return xmlhttp;
        }
        var objXMLHttp =  new XmlHttp();
               
        function GetReadyState( ){
            return this.objXmlHttp.readyState;
        }

        function GetResponseText( ){
            return this.objXmlHttp.responseText;
        }


        // Function performs Get request to absolute url(strUrl)
        // using XmlHttp object (asynchroni)
        // Response returned into objResult element using innerHTML.
        // When state of XmlHttp object is changed - objOnReadyStateChangeFunction called
        function GetUrlContent( strUrl, objOnReadyStateChangeFunction ){
            this.objXmlHttp.open(this.HttpMethod, strUrl, true);
            this.objXmlHttp.setRequestHeader('Content-Type', 'text/xml; charset=UTF-8');
            if(objOnReadyStateChangeFunction){
                this.objXmlHttp.onreadystatechange=function(){
                    objOnReadyStateChangeFunction();
                }
            }
            this.objXmlHttp.send(null)   
        }

        //This function is called when we get the data back from the server.       
        function GetResponse2( ){   
            if (objXMLHttp.GetReadyState()==4) {                       
                // save response in inner html of result object   
                var objMyDiv = document.getElementById( 'content' );
                objMyDiv.innerHTML = objXMLHttp.GetResponseText( );
                var objMyDiv2 = document.getElementById( 'loader' );
                objMyDiv2.innerHTML = '';      
            }
        } 
   
        function UpdateDiv2( FieldValue ){
            if( FieldValue !='' ){
                var objMyDiv = document.getElementById( 'content' );
				var objMyDiv2 = document.getElementById( 'loader' );
                objMyDiv.innerHTML = 'Letar...';
				objMyDiv2.innerHTML = '<img src="/files/img/uploaded/ajax-loader.gif" />';
                objXMLHttp.GetUrlContent( '/files/fnc/simplesearch.asp?what=' + encodeURIComponent(FieldValue) , GetResponse2 );       
            }
            return;
         }   
		function apply_add_ajax_search() {
		
				// Find the search box in the DOM
				var theElement = document.getElementById('search');
		
				// Create a function for when the user presses a key
				theElement.onkeyup = function(){ search_delay(this); };
		}
		
		function search_delay(element) {
		
				// Create a function to get the search results
				var func = function() { UpdateDiv2( element.value ); };
		
				// Check to see if there is already a timeout and if so...
				// ...cancel it and create a new one
				if ( element.zid ) {
					clearTimeout(element.zid);
				}
				element.zid = setTimeout(func,500);
		
		}