﻿    var mintSelectedId = 0;
    
    function mfSearchKeyUp(pobjEvent)
    {
      //Declarations
      var objEvent;
      var divSearchOptions = document.getElementById('divSearchOptions');
      var SearchTerm        = document.getElementById('SearchTerm');
      
      //get a refrence to the keyboard event (IE different to mozila as normal ;-)
      if(window.event) // IE
        objEvent = event;
      else if(pobjEvent.which) // Netscape/Firefox/Opera
        objEvent = pobjEvent;
            
      //If the user has pressed the KeyDown or KeyUp event call a function to move the highlighter
      if (pobjEvent.keyCode == 40 || pobjEvent.keyCode == 38) //40=down, 38=up
        mfMoveSearchHighlighter(pobjEvent.keyCode);      
      else if (pobjEvent.keyCode == 27) //Escape
      {
        mfCloseSearchOptions();
        return false;
      }
      else if (pobjEvent.keyCode == 13)  //Enter
      {
        mfSearchSelect();
        return false;
      }      
      else
        mfGetSearchOptions(SearchTerm.value);
    }
    
    function mfSearchKeyDown(pobjEvent)
    {
      //Declarations
      var objEvent;
      
      //get a refrence to the keyboard event (IE different to mozila as normal ;-)
      if(window.event) // IE
        objEvent = event;
      else if(pobjEvent.which) // Netscape/Firefox/Opera
        objEvent = pobjEvent;
      
      if (pobjEvent.keyCode == 27)
      {
        mfCloseSearchOptions();
        return false;
      }
    }


    function mfSelectRow(pdivRow)
    {
      //Declarations
      var intLoop = 1;

      //unSelect all options as we are possibly about to select a new one
      while (document.getElementById('divSearchRow' + intLoop) != null)
      {
        document.getElementById('divSearchRow' + intLoop).className = 'Search_unSelected';
        intLoop++;
      }
      
      pdivRow.className = 'Search_Selected';
      mintSelectedId = pdivRow.id.replace('divSearchRow', '');
      
    }
    
    function mfMoveSearchHighlighter(pintKeyCode)
    {
      var intNewRow = 0;
      
      if (pintKeyCode == 40 && mintSelectedId < 10)
      {
        intNewRow = parseInt(mintSelectedId) + parseInt(1);
        mfSelectRow(document.getElementById('divSearchRow' + intNewRow))
      }
      else if (pintKeyCode == 38 && mintSelectedId > 1)
      {
        intNewRow = parseInt(mintSelectedId) - parseInt(1);
        mfSelectRow(document.getElementById('divSearchRow' + intNewRow))
      }
      
    }
    
    function mfSetFocus(pobjTextBox)
    {
      //Sets the focus to a text box and then moves the cursor to the end of the box
      pobjTextBox.focus();
      if (pobjTextBox.createTextRange)
      {
        var FieldRange = pobjTextBox.createTextRange();
        FieldRange.moveStart('character', pobjTextBox.value.length);
        FieldRange.collapse();
        FieldRange.select();
      }
    }
    
    function mfCloseSearchOptions()
    {
      mintSelectedId = 0;
      document.getElementById('divSearchOptions').style.display='none'; 
      mfSetFocus(document.getElementById('SearchTerm'))
    }

    function mfSearchSelect()
    {
    
      if (mintSelectedId > 0)
      {
        document.getElementById('SearchTerm').value = document.getElementById('divSearchRow' + mintSelectedId).innerHTML;              
        mfCloseSearchOptions();
        document.forms[0].submit();
      }
      else
      {
        //alert(document.getElementById('SearchTerm').value)
        mfCloseSearchOptions();
        document.forms[0].submit();
      }
    }
    
    function mfGetSearchOptions(pstrSearchText)
    {
    
      //Declarations      
      var objHTTPRequest      = MoreNet_CreateHTTPRequestObject();  //Create an ajax request object
      var Today               = new Date();      
      var strParams           = '';
      var strManufacturerName = '';
      

      if (document.getElementById('CategoryId') != null)
        strManufacturerName = document.getElementById('CategoryId').options[document.getElementById('CategoryId').selectedIndex].text;

      //Make an Ajax call to get the new search options      
      strParams = 'SearchText='  + escape(document.getElementById('SearchTerm').value) + 
                  '&Manufacturer=' + escape(strManufacturerName);

      objHTTPRequest.open("GET", 'SuggestiveSearch.ajax.aspx?CS=' + Today.getSeconds() * Today.getMilliseconds() + '&' + strParams, true);
      objHTTPRequest.onreadystatechange = function()
                                          {
                                            if (objHTTPRequest.readyState==4) 
                                              mfGetSearchOptionsResponse(objHTTPRequest.responseText);
                                          };
      objHTTPRequest.send(null);  	        
    }
    
    function mfGetSearchOptionsResponse(pstrResponse)
    {    
      var divSearchOptions = document.getElementById('divSearchOptions');          

      //Populate the innerHTML of the search box with the options gained from the above Ajax call
      mintSelectedId = 0;
      
      divSearchOptions.innerHTML = pstrResponse;
      divSearchOptions.innerHTML += '<div style="text-align:right; padding:2px; font-size:12px; color:Blue; cursor:pointer;" onclick="mfCloseSearchOptions();">close</div>';

      //Show the search options     
      divSearchOptions.style.display = '';

    }
    
    //###############################
    //# HTTPRequest Object          #
    //###############################
    function MoreNet_CreateHTTPRequestObject()
    {
      var xmlhttp=false;

      /*@cc_on @*/
      /*@if (@_jscript_version >= 5)
      // JScript gives us Conditional compilation, we can cope with old IE versions.
      // and security blocked creation of the objects.
       try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
        try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
         xmlhttp = false;
        }
       }
      @end @*/
        
      if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	      try {
		      xmlhttp = new XMLHttpRequest();		 
		      //xmlhttp.overrideMimeType('text/xml');
	      } catch (e) {
		      xmlhttp=false;
	      }
      }
      
      if (!xmlhttp && window.createRequest) {
	      try {
		      xmlhttp = window.createRequest();
	      } catch (e) {
		      xmlhttp=false;
	      }
      }
      
      //if (location.hostname.toLowerCase() == 'inks4students.co.uk') alert(!xmlhttp && window.createRequest);    
      //If we still have not been able to create an xmlhttprequest object try using our IFrame workaround
      if (!xmlhttp)
      {    
        xmlhttp = new IFrameHttpRequest();    
      } 
      return xmlhttp;  
    }  

    var kXHR_instances=0;
    var kXHR_objs=[];

    IFrameHttpRequest=function()
    {
      var i=0;
      var url='';
      var responseText='';
      var iframe='';

      this.onreadystatechange=function(){return false;}

      this.open=function(method,url)
      {
        this.i=++kXHR_instances; // id number of this request
        this.url=url;    
        document.body.appendChild(document.createElement('<iframe id="kXHR_iframe_'+this.i+'" style="display:none; z-index:99999; font-size:12px" src="/"></iframe>'));
      }
      
      this.send=function(postdata)
      {
        //TODO: use the postdata    
        document.getElementById('kXHR_iframe_'+this.i).src=this.url;
        kXHR_objs[this.i]=this;
        setTimeout('IFrameHttpRequest_checkState('+this.i+',2)',2);
      }
      return true;
    }

    IFrameHttpRequest_checkState=function(inst,delay)
    {
      var el=document.getElementById('kXHR_iframe_'+inst);

      if(el.readyState=='complete')
      {  
        var responseText=document.frames['kXHR_iframe_'+inst].document.body.innerHTML;
        kXHR_objs[inst].responseText=responseText;
        kXHR_objs[inst].readyState=4;
        kXHR_objs[inst].status=200;
        kXHR_objs[inst].onreadystatechange();
        el.parentNode.removeChild(el);
      }
      else
      {
        delay*=1.5;
        setTimeout('IFrameHttpRequest_checkState('+inst+','+delay+')',delay);
      }
    }      