﻿var myRecipient=null;
var browseContext=null;
var myContext=null;
var myType=null;
function ajaxPostOffice(type, context, recipient)
{
    
    searchTerms = document.getElementById('searchTerms').value;
    //alert('requesting suggestion');
    myRecipient=recipient;
    myContext = context;
    myType = type;
    AjaxGetData('/handlers/SuggestionServer.ashx?q='+searchTerms+'&t='+myType+'&c='+myContext,sendToRecipient);
}

function ajaxPostSearchQuery(searchTerms, type, context)
{
    //alert('requesting suggestion');
    //myRecipient=recipient;
    //alert('search site');
    browseContext = context;
    //alert(context);
    AjaxGetData('/handlers/SearchHandler.ashx?q='+searchTerms+'&r='+type+'&s='+context,acknowledgePost);//,type, context);    
}
function acknowledgePost()
{
    //alert('acknowledged'+getAjaxResponse());
    //getAjaxResponse();
    alert('debug');
    document.getElementById('suggestions').innerHTML = getAjaxResponse();
    if(browseContext==0)//all
    {
        window.location="/search-results.aspx";
    }
    if(browseContext==1)//browse christian only
    {
        window.location="/Christian/search-results.aspx";
    }
}
function sendToRecipient()
{
    //alert('sending to recipient');
    if(myRecipient==null)
    {
        //alert('myRecipient is null');
        return;        
    }
    myResponse = getAjaxResponse();
    if(myResponse!=null)
    {
        if(myResponse !='')
        {
            document.getElementById(myRecipient).innerHTML = myResponse;
            
            //if there's no suggestion to show empty the suggestions div
            var x = document.getElementById('SuggestionsBox').innerHTML;
            
            if(x==null || x=='' || x==' ')
            {
                try{
                (document.getElementById('suggestions')).innerHtml='';//.style.display="none";
                }
                catch(ex){}
                finally{}               
            }
        }
    }
    else
        document.getElementById(myRecipient).innerHTML = "<img alt='Loading' src='/images/loading.gif' />";
    //alert(myRecipient);    
}


function AjaxGetData(server, onAjaxResponse) {
    //alert("inside AjaxGetData");
     // ShowHide('divLoading');
     req=null;
      //document.getElementById('divLoading').style.display = "block";
     if (window.XMLHttpRequest)      { 
               //alert("new req object created");
               req = new XMLHttpRequest();     
      }    
      else if (window.ActiveXObject)  
      {        
      //alert("new req object created");
       req = new ActiveXObject("Microsoft.XMLHTTP");     
      }          
      if(req)     
      {         
        req.onreadystatechange = onAjaxResponse;  
        req.open('GET', server, true);        
        req.setRequestHeader("content-type","application/text");          
        req.send('');     
    }      
    else     
    {     
           alert('Your browser does not seem to support XMLHttpRequest.');    
    }  
}  


function HelloAjax()
{
    //alert("Hello world!");
    //response = getAjaxResponse();
    document.getElementById('AjaxOutput').innerHTML = getAjaxResponse();  
        
}
       
function getAjaxResponse() 
{ 
    ajaxResponse="";
  try 
    { 
        if (req.readyState == 4 ||  
            req.readyState == 'complete') 
        { 
            //document.getElementById('divLoading').style.display = "none"
            ajaxResponse = req.responseText;                            
            return ajaxResponse; 
        }
        else
        {
          //document.getElementById('divLoading').style.display = "block"
        }
    } 
     catch(e) 
     {         
        return 'There was some in getting respone'; 
     }     
}            