//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  XSLHelper - See description toward the bottom of this file
//  @Author - Eric Chan
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var XSLHelper = new Object();

//================================================================
//  Sarissa
//================================================================

var TrueWebRoot = 'http://' + window.location.hostname + '/';

 var XSLProcessor = new Object(); 
     XSLProcessor.CommunityUnitListings=null;
     XSLProcessor.CommunityUnitListingsPagination=null;  
     XSLProcessor.loaded =               false;
     XSLProcessor.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();
        //alert(TrueWebRoot + XSLPath);
        xmlhttp.open('GET', TrueWebRoot + XSLPath, false);
	    
	    xmlhttp.send('');
	    xslDoc = xmlhttp.responseXML;
                    
	    processor.importStylesheet(xslDoc);
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    //alert(new XMLSerializer().serializeToString(xslDoc));
	    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	    return processor; 
    };
    
    XSLProcessor.loadAll = function()
    {   
         
    XSLProcessor.CommunityUnitListings             = XSLProcessor.load("includes/xsl/listings/community_unit_listings.xsl");   
	XSLProcessor.CommunityUnitListingPagination    = XSLProcessor.load("includes/xsl/listings/community_unit_listings_pagination2.xsl");	
	
	XSLProcessor.loaded = true;
};

function RunCommunityQuery(Query)
{ 
    var QGen = new UrlGen(Query);
     
     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     //  Load XSL into memory
     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
	if (XSLProcessor.loaded == false)
	{	        
	    // Sarissa gives unknown exception, skip it completely.
		//XSLProcessor.loadAll();
	}


    var xmlDocPath = "";
        xmlDocPath = "community_xml.aspx?" + Query;
            
	var XMLDoc = Sarissa.getDomDocument();	
	    XMLDoc.async = false;
	var xmlhttp = new XMLHttpRequest();

	    xmlhttp.open("GET", TrueWebRoot + xmlDocPath, false);	   	 
	    xmlhttp.send('');
	    XMLDoc = xmlhttp.responseXML;

	    /*====================================================================*/
        /* Orginal Code
        /*=====================================================================*/
	    //Sarissa.updateContentFromNode(XMLDoc, gE("paging_units_listings"), XSLProcessor.CommunityUnitListingPagination);
	    //UnescapeCommonInnerHTML(gE("paging_units_listings"));

	    //Sarissa.updateContentFromNode(XMLDoc, gE("results_units_listings"), XSLProcessor.CommunityUnitListings);
	    //UnescapeCommonInnerHTML(gE("results_units_listings"));
	    /*=====================================================================*/
	    
	    
	    //XSLHelper.UpdateSingleNodeContent(XMLDoc, "results_units_listings", XSLProcessor.CommunityUnitListings, Query);
	    //XSLHelper.UpdateSingleNodeContent(XMLDoc, "paging_units_listings", XSLProcessor.CommunityUnitListingPagination, Query);

	    XSLHelper.UpdateSingleNodeContent(XMLDoc, "results_units_listings", null, Query);
	    XSLHelper.UpdateSingleNodeContent(XMLDoc, "paging_units_listings", null, Query);	                 
}


/*================================================================================
* XSLHelper - Eric C.
*
* The XSLHelper routines are used to update node contents on the document since
* Sarissa is found to be error prone. 
*
* In the case Sarissa.updateContentFromNode() throws exception, the function
* UpdateSingleNodeContent() will make a trip to call (results_xsl_transform.aspx) to
* perform XSLT transformation using .NET routine.
*
* If the (results_xsl_transform.aspx) returns a message that contains the literal
* "ERROR", the content of the node is not updated rather than updating the node
* with the ERROR message.
*================================================================================*/

/*-----------------------------------------------------------------------
* This function returns an actual ID on the listings page by the alias
*-----------------------------------------------------------------------*/
XSLHelper.GetElementID = function(nodeAlias)
{
    var nodeID = "";
    switch (nodeAlias)
    {
        case "results_units_listings": nodeID = "results_units_listings"; break;
        case "paging_units_listings": nodeID = "paging_units_listings"; break;
        

        default: nodeID = "NULL"; break;
    }
    return nodeID;
}

/*----------------------------------------------------------------------------
* GetXslDocumentPath() - This function simply returns the xsl document path.
*----------------------------------------------------------------------------*/
XSLHelper.GetXslDocumentPath = function(nodeAliasOrId)
{
    var xslPath = "";
    switch (nodeAliasOrId)
    {
        case "paging_units_listings":
            xslPath = "includes/xsl/listings/community_unit_listings_pagination.xsl";
            break;

        case "results_units_listings":
            xslPath = "includes/xsl/listings/community_unit_listings.xsl";
            break;        

        default: throw "Node is not found or not supported"; break;
    }
    return xslPath;
};

/*--------------------------------------------------------------------------------------
* UpdateSingleNodeContent() - attempts to transform usign Sarissa, if operation fails
* this function calls AjaxGetTransformedContent() which makes a trip back to server
* to perform XSL transformation operation.
*--------------------------------------------------------------------------------------*/
XSLHelper.UpdateSingleNodeContent = function(XMLDocObj, NodeId, XsltProcessorObj, Query)
{
    if (gE(NodeId) === null)
    {
        return;
    }
    else
    {
        try
        {
            if (XsltProcessorObj == null)
                throw "XsltProcessorObj is null, skip using Sarissa";
            switch (NodeId)
            {
                case "paging_units_listings":
                case "results_units_listings":
                    {
                        throw "Skip using Sarissa";
                    }
                default:
                    Sarissa.updateContentFromNode(XMLDocObj, gE(NodeId), XsltProcessorObj);
            }
        }
        catch (ex)
        {
            var responseTransformedData = XSLHelper.AjaxGetTransformedContent(Query, NodeId);
            if (responseTransformedData.indexOf("ERROR") > -1)
            {
                return;
            }
            else
            {
                gE(NodeId).innerHTML = responseTransformedData;
                UnescapeCommonInnerHTML(gE(NodeId));
            }
        }
    }
};

/*-------------------------------------------------------------------------------
* AjaxGetTransformedContent() - makes a trip back to the server via AJAX call
* to transform the content of XML.
*-------------------------------------------------------------------------------*/
XSLHelper.AjaxGetTransformedContent = function(QG, NodeDomId)
{
    // Do not change the value of "QGSeparator", if changes are necessary, all of the
    // following files must be changed.
    //
    // results_xsl_transform.aspx.cs [ParseQueryString() method]
    // Lib.Sarissa.js                [XSLHelper.AjaxGetTransformedContent() method]
    // agent.js                      [XSLAgentHelper.AjaxGetTransformedContent() method]
    var QGSeparator = "$!?";

    var xslhttp = new XMLHttpRequest();
    var xslTransformerPath = "results_xsl_community_transform.aspx";

    xslTransformerPath = xslTransformerPath + "?QTYPE=COMMUNITYUNITS" + QGSeparator + "Query=" + QG + QGSeparator + "XSLPath=" + XSLHelper.GetXslDocumentPath(NodeDomId);
    xslhttp.open("GET", TrueWebRoot + xslTransformerPath, false);
    //xslhttp.setRequestHeader("Authorization", XKp);
    xslhttp.send('');

    var responseTransformedData = xslhttp.responseText;
    return (responseTransformedData);
};

/*-------------------------------------------------------------------------------------
* The following 2 functions IsNode() and IsElement() give ActiveX warnings in IE,
* intended to use in XSLHelper.UpdateSingleNodeContent() to check whether the parameter
* (nodeObj) is actually an element in DOM.
*------------------------------------------------------------------------------------*/

//Returns true if it is a DOM node
XSLHelper.IsNode = function(obj)
{
    return (typeof Node === "object" ? obj instanceof Node : typeof obj === "object" && typeof obj.nodeType === "number" && typeof obj.nodeName === "string");
};

//Returns true if it is a DOM element    
XSLHelper.IsElement = function(obj)
{
    return (typeof HTMLElement === "object" ? obj instanceof HTMLElement : //DoM2
        typeof obj === "object" && obj.nodeType === 1 && typeof obj.nodeName === "string");
};
