﻿//----------------------------------  HCAjax functions   ------------------------------------------
function HCAjax()
{
        
}

HCAjax.prototype.call = function(Url, params, beforeCallFunction, afterCallFunction, asynchronous)
{   
    var f = new Function(beforeCallFunction);
    f.call();
    this.XmlHttpRequestCall(Url, params, afterCallFunction, asynchronous);     
}


HCAjax.prototype.XmlHttpRequestCall = function(Url, params, afterCallFunction, asynchronous) {


    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') {
        xmlhttp = new XMLHttpRequest();        
    }

    xmlhttp.open("POST", Url, asynchronous);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {

            if (xmlhttp.status == "500") {
                if (confirm("A server error has occured. Click ok to see the error.")) {

                    var generator = window.open('ErrorWindow', 'name', 'height=300,width=500,status=1,scrollbars=1,resizable=1');
                    if (generator) {
                        generator.document.write('<html><head><title>Error Message 500</title>');
                        generator.document.write('</head><body>');
                        generator.document.write(xmlhttp.responseText);
                        generator.document.write('</body></html>');
                        generator.document.close();
                    }
                    else alert('popup was blocked.');
                }

            }
            else if (xmlhttp.status == "404") {
                alert("Script not found returned from server: " + Url);
            }
            else {
                hcAjax.onResponse(xmlhttp.responseText);
            }
            
            var f = new Function(afterCallFunction);
            f.call();

        }
    }

    xmlhttp.send(params);
}

//using ICallbackEventHandler
HCAjax.prototype.onResponse = function(result, context) {
    var xmldoc = this.stringToXml(result);

    try {
        this.onResponseResult(xmldoc);
    }
    catch (err) {
        alert(err + " - " + err.name + ", " + err.message + "");
    }
    return false;

}

HCAjax.prototype.parseHtml = function(element)
{
    
    if (element != null)
    {
    
        if (element.tagName != null && element.tagName.toLowerCase() == "script")
        {
            eval(element.innerHTML);
            try{
                
            }
            catch(err)
            {   
                if(confirm("Error parsing script on dynamic data (" + err +"). Click ok to see the source."))
                {
                    alert(element.innerHTML);
                }                
            }
        }
                        
        for (var i = 0; i < element.childNodes.length; i++)
        {
            
            var currentElement = element.childNodes[i];
            
            if (currentElement.tagName != null)
            {
                if (currentElement.tagName.toLowerCase() == "script")
                {
                    eval(currentElement.innerHTML);
                }
                else if(currentElement.childNodes.length > 0)
                {
                    for(var j = 0; j < currentElement.childNodes.length; j++)
                    {   
                        if(currentElement.childNodes[j].tagName != null && currentElement.childNodes[j].tagName.toLowerCase() == "panel")
                        {
                            var t = 0;
                        }
                        
                        this.parseHtml(currentElement.childNodes[j]);
                    }
                }
            }
        }
    }
}


    
HCAjax.prototype.onResponseResult = function (xmldoc)
{
    ///<response>
    ///   <item action="prefixdata" controlid="MyPanelID">some <b>html</b> content to prefix</item>
    ///   <item action="suffixdata" controlid="MyPanelID">some <b>html</b> content to suffix</item>
    ///   <item action="setdata" controlid="MyPanelID">some <b>html</b> content to display</item>
    ///   <item action="functioncall">Myfunction("param1","param2")</item>
    ///</response>

    //Result is n XML string
    
    if( xmldoc != null && 
        xmldoc.documentElement != null && 
        xmldoc.documentElement.childNodes.length > 0)
    {

        var response = null;
        var x = null;
                
        //First check the immediate children of the returned document
        x = xmldoc.getElementsByTagName("item");
            
        //If there are no child items
        if(x == null || x.length == 0)
        {
            //Normalize the document element
            xmldoc.documentElement.normalize();
            
            var xmldoc_responsexmldoc = this.stringToXml(xmldoc.documentElement.childNodes[0].data);                            
            response = xmldoc_responsexmldoc.documentElement;
            x = response.getElementsByTagName("item");
        }
        
        //If results have been found
        if(x != null && x.length > 0)
        {          
            for (var aci = 0; aci < x.length; aci++)
            {                
                var action = x[aci].getAttribute("action");
                 
                 if(action != null && x[aci].firstChild != null && x[aci].firstChild.data != null)
                 {
                     switch(action)
                     {
                        case "setdata":
                            
                            var controlid = x[aci].getAttribute("controlid");
                            if(controlid != null)
                            {
                                //Normalize the node
                                x[aci].normalize();
          
//                                document.getElementById(controlid).innerHTML = "";
//                                
//                                var newElement = document.createElement("div");
//                                newElement.innerHTML =  x[aci].firstChild.data;
//                                
//                                for(var j=0; j < newElement.childNodes.length; j++ )
//                                {
//                                    document.getElementById(controlid).appendChild(newElement.childNodes[j]);
//                                }                               
                                
                                document.getElementById(controlid).innerHTML = x[aci].firstChild.data;
                                
                                this.parseHtml(document.getElementById(controlid));
                            }
                                
                        break;
                        
                        case "prefixdata":
                            
                            var controlid = x[aci].getAttribute("controlid");
                            if(controlid != null)
                            {
                                var savedHTML = document.getElementById(controlid).innerHTML;
                                
                                //Normalize the node
                                x[aci].normalize();
                                
                                document.getElementById(controlid).innerHTML = x[aci].childNodes[0].data + savedHTML;
                                
                                this.parseHtml(document.getElementById(controlid));
                            }
                                
                        break;
                        
                        case "suffixdata":
                        
                            var controlid = x[aci].getAttribute("controlid");
                            if(controlid != null)
                            {                                                                
                                //Normalize the node
                                x[aci].normalize();
                                
                                document.getElementById(controlid).innerHTML += x[aci].childNodes[0].data;
                                this.parseHtml(document.getElementById(controlid));
                            }
                        
                        break;
                            
                        case "functioncall":
                                
                            //Normalize the node
                            x[aci].normalize();
                                
                            var f = new Function(x[aci].childNodes[0].data);
                            f.call();    
                                                
                        break;
                        
                     }
                 }
            }
        }
        
    }
    else {
        return false;
    }       
  
  return true;
}
    
    
HCAjax.prototype.stringToXml = function(xmlstr)
{
    var xmldoc;
    
    // code for IE
    if (window.ActiveXObject)
    {
        var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
        xmldoc.async="false";
        xmldoc.loadXML(xmlstr);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else
    {
        var parser = new DOMParser();
        var xmldoc = parser.parseFromString(xmlstr,"text/xml");
    }
    
    return xmldoc;
    
}


var hcAjax = new HCAjax();
