//========================================================
//  AgentPagingQuery
//  temporary...will use sarissa
//========================================================
 function AgentPagingQuery(Query)
 {    
    window.location.href =  window.location.href.split("?")[0] + "?" + Query;    
 }
 
 
function Agent(AgentForeignId)
{
    var UGen = new UrlGen(QSCache.QueryString);
        UGen.RemoveParam("AID");
        UGen.AddParam("AID",AgentForeignId);        
        var strURL = WebRoot + "controls/ajaxcalls/DetailAgentPath.aspx?" + UGen.ToString();
       
        $AJAX.GetForDelegate(function(AjaxResponse)
        {
            //alert(AjaxResponse);
            window.location.href = AjaxResponse;
            
        }, strURL);        

}


function AgentPage(No, Position)
{
    QSCache.Position = Position; // position of current listing after query.
    var UGen = new UrlGen(QSCache.QueryString);
    //alert(QSCache.QueryString + "::Mamta");
    UGen.RemoveParam("No");
    UGen.AddParam("No",No);
    QSCache.QueryString = UGen.ToString(); 
   // alert(QSCache.QueryString);
    $AJAX.GetForDelegate(AgentPageHandler, WebRoot + "Controls/AjaxCalls/DetailAgentIds.aspx?" + QSCache.QueryString);
}

    function AgentPageHandler(AJAXResponse)
    {
        if (AJAXResponse.length > 0)
        {
            var arrAgentIds = AJAXResponse.split(',');
            var AgentForeignId = arrAgentIds[QSCache.Position];
            var UGen = new UrlGen(QSCache.QueryString);
            UGen.RemoveParams(["AgentIds","AID"]);
            UGen.AddParam("AgentIds",AJAXResponse.replace(/,/g,'--'));
            UGen.AddParam("AID",AgentForeignId);
            
            var strURL = WebRoot + "controls/ajaxcalls/DetailAgentPath.aspx?" + UGen.ToString();
            //alert(strURL);
            $AJAX.GetForDelegate(function(AjaxResponse)
            {
                //alert(AjaxResponse);
                window.location.href = AjaxResponse;
                
            }, strURL);

        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  Get_AgentDetail(AgentForeignId)
//  Jump to the Agent Detail page
//  We need to carry several hidden form parameters with us
//  for Paging and Return to Results
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Get_AgentDetail(AgentForeignId)
{
    var UGen = new UrlGen(PageCache.QueryString);
        UGen.RemoveParams(["AID","AgentIds","T"]);
        UGen.AddParam("T",PageCache.TotalRecords);
        UGen.AddParam("AID",AgentForeignId);
        UGen.AddParam("AgentIds",PageCache.AgentForeignIds.replace(/,/g,'--'));
        //alert(AgentForeignId);
        // alert(PageCache.AgentForeignIds);

        var SEOSiteID = window.location.href.substring(window.location.href.indexOf("/") + 2, window.location.href.length);
        var strSEOSiteID = SEOSiteID.split("/");       

        var strURL = WebRoot + "controls/ajaxcalls/DetailAgentPath.aspx?" + UGen.ToString() + "&SEOSiteID=" + strSEOSiteID[1];
      // alert(UGen.ToString());
        $AJAX.GetForDelegate(function(AjaxResponse)
        {
            //alert(AjaxResponse);
            window.location.href = AjaxResponse;
            
        }, strURL);
        

}

//================================================================
//  Sarissa
//================================================================

var XSLProcessors = new Object();
    XSLProcessors.agents = null;
    XSLProcessors.paging = null;
    XSLProcessors.status = null;
    XSLProcessors.sortoptions = null;   
    XSLProcessors.featuredagents = null;    
    XSLProcessors.results = null; 
    XSLProcessors.searchlocation =null;   
    XSLProcessors.AgentListings =null ;
    XSLProcessors.AgentListingPagination=null;
    XSLProcessors.loaded = false;
    XSLProcessors.load = function(XSLPath)
    {
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // create an instance of XSLTProcessor   
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    var processor = new XSLTProcessor(); 
             
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    // create a DOM Document containing an XSLT stylesheet   
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    var xslDoc = Sarissa.getDomDocument(); 
	    xslDoc.async = false;
	    var xmlhttp = new XMLHttpRequest();
	    xmlhttp.open('GET', AbsoluteWebRoot + XSLPath, false);
	    xmlhttp.send('');
	    xslDoc = xmlhttp.responseXML;
                    
	    processor.importStylesheet(xslDoc);
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    //  Debug: alert(new XMLSerializer().serializeToString(xslDoc));
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    return processor; 
    };

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  Load all XML into client side cache on initial page load.
//  Each XSL is pulled from Cache after that for speed.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XSLProcessors.loadAll = function()
{
    XSLProcessors.agents               = XSLProcessors.load("includes/xsl/agents/agents.xsl");
	XSLProcessors.paging                = XSLProcessors.load("includes/xsl/agents/agents_pagination.xsl");
	XSLProcessors.status                = XSLProcessors.load("includes/xsl/agents/agents_results_status.xsl");	
    XSLProcessors.sortoptions           = XSLProcessors.load("includes/xsl/agents/agents_sort_options.xsl");	
	XSLProcessors.results               = XSLProcessors.load("includes/xsl/agents/agents_results.xsl");	
	XSLProcessors.featuredagents        = XSLProcessors.load("includes/xsl/agents/featured_agents.xsl");
	XSLProcessors.searchlocation        = XSLProcessors.load("includes/xsl/agents/agents_search_location.xsl");	
	XSLProcessors.loaded = true;
};



function RunAgentQuery(Query)
{   
    var QGen = new UrlGen(Query);
     
       var isZipSearch = false;
        if (QGen.GetParam("Ntk").toLowerCase() == "addrzip")
        {
            isZipSearch = true;
        }
     
       //----------------------------------------------------------------
        //  For Non-Sarissa Compliant Browsers
        //----------------------------------------------------------------
        if ((!window.XSLTProcessor) && (!window.ActiveXObject))
        {
           var QGen = new UrlGen(Query);
               QGen.RemoveParams(["sb", "ms"]);
               QGen.AddParam("sb", sb);           
           window.location.href = window.location.href.substr(0,indexOf('?')) + QGen.ToString();
           return;
        }
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
        //  Load XSL into memory
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
	    if (XSLProcessors.loaded == false)
	    {
		    XSLProcessors.loadAll();
	    }
     
    var xmlDocPath = "";
        xmlDocPath = "agents_xml.aspx?" + Query;    
	var XMLDoc = Sarissa.getDomDocument();
	    XMLDoc.async = false;
	var xmlhttp = new XMLHttpRequest();
	    xmlhttp.open("GET", AbsoluteWebRoot + xmlDocPath, false);	 
	    xmlhttp.send('');
	    XMLDoc = xmlhttp.responseXML;
	    PageCache.XML = XMLDoc; 
	    
    	
	     PageCache.CenterOnRegions = eval(GetSingleNodeContent(XMLDoc, "/results/centeronregions"));
    	
	      var SEOPath = GetSingleNodeContent(XMLDoc, '/results/seopath');
    	  
    	  
	        window.location.href = "#" + SEOPath;
    	    
	        if (gE("testingQuery") != null)
            {
                gE("testingQuery").value = Query;
                
            }
    	    
	        Sarissa.updateContentFromNode(XMLDoc, gE("results_results"), XSLProcessors.results);
            UnescapeCommonInnerHTML(gE("results_results"));
            
            PageCache.TotalRecords    = parseInt(GetSingleNodeContent(XMLDoc, '/results/pagination/pages/totalrecords'),10);
            if (isNaN(PageCache.TotalRecords))
            {
                PageCache.TotalRecords = 0;
            }
                
                 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            //  Use Sarissa to update page contents and PageCache
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	        Sarissa.updateContentFromNode(XMLDoc, gE("paging_top"), XSLProcessors.paging);
	        Sarissa.updateContentFromNode(XMLDoc, gE("paging_bottom"), XSLProcessors.paging);
	        Sarissa.updateContentFromNode(XMLDoc, gE("sortoptions"), XSLProcessors.sortoptions);	   
	        Sarissa.updateContentFromNode(XMLDoc, gE("status"), XSLProcessors.status);
	        Sarissa.updateContentFromNode(XMLDoc, gE("results_agents"), XSLProcessors.agents);	       
	        Sarissa.updateContentFromNode(XMLDoc, gE("results_search_location"), XSLProcessors.searchlocation);
	        Sarissa.updateContentFromNode(XMLDoc, gE("featured_agents_wrap"), XSLProcessors.featuredagents);
	         PageCache.AgentForeignIds=GetSingleNodeContent(XMLDoc, "/results/agentforeignids");	    
    	    
    	    
	          PageCache.QueryString = Query;
    	      
	          if (PageCache.TotalRecords > 0)
            {
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                //  Cache the QueryString for a return
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                PageCache.LastQueryString = Query; // Cache querystring for return
            }    
     
        window.scrollTo(0,0); 
}




