﻿//----------------------------------  HCAjax functions   ------------------------------------------
function HCAjax()
{
    this.loading_message_ctl_id = null;
    this.showLoadingProgress = true;
}

HCAjax.prototype.call = function(Url, params, beforeCallFunction, afterCallFunction, asynchronous) {

    if (this.showLoadingProgress) {

        if (typeof beforeCallFunction == "function")
            beforeCallFunction();
        else {
            var f = new Function(beforeCallFunction);
            f.call();
        }
    }

    this.XmlHttpRequestCall(Url, params, afterCallFunction, asynchronous);

}


HCAjax.prototype.XmlHttpRequestCall = function(Url, params, afterCallFunction, asynchronous) {

    this.startLoadingMessage();

    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();
        asynchronous = true;
    }
    else asynchronous = false;
    
    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('<link rel="stylesheet" href="/cms/css/Panel.css">');
                        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);
            }

            hcAjax.stopLoadingMessage();


            if (hcAjax.showLoadingProgress) {
                var f = new Function(afterCallFunction);
                f.call();
            }

        }
    }

    xmlhttp.send(params);
}



HCAjax.prototype.startLoadingMessage = function()
{
    if(this.loading_message_ctl_id)
        document.getElementById(this.loading_message_ctl_id).style.display = 'block';
}

HCAjax.prototype.stopLoadingMessage = function()
{
    if(this.loading_message_ctl_id)
        document.getElementById(this.loading_message_ctl_id).style.display = 'none';
}

HCAjax.prototype.LoadLoginScreen = function()
{        
    var myObject = window;
    
    while (myObject.parent != null & myObject.hccms == null)
    {
        myObject = myObject.parent;
    }

    myObject.hccms.LoadLogin();
}

//using ICallbackEventHandler
HCAjax.prototype.onResponse = function(result, context) {
    var xmldoc = this.stringToXml(result);

    try {

        if (!this.onResponseResult(xmldoc)) {
            //Load the login screen
            if (result.indexOf("login") > -1)
                this.LoadLoginScreen();
        }
    }
    catch (err) {
        if (xmldoc.documentElement.firstChild.text.toLowerCase().indexOf("login") > -1) {
            this.LoadLoginScreen();
        }
        else {
            if (confirm(err + " - " + err.name + ", " + err.message + "\nClick OK to view more details about this error.")) {
                alert(xmldoc.xml);
            }
        }
    }
    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);
                    //document.body.appendChild(element);
                    //document.body.appendChild(escape(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();

//----------------------------------  HCAjax functions   ------------------------------------------

function FadeOutControl(controlid, opacity, minopacity)
{   
    
    if(opacity > minopacity)
    {
        opacity = opacity - 10;
        if(document.getElementById(controlid))
        {
            document.getElementById(controlid).style.cssText = "filter:alpha(opacity="+opacity+"); opacity: "+opacity/100+"; -moz-opacity:"+opacity/100+";";
            setTimeout("FadeOutControl('"+controlid+"',"+opacity+","+minopacity+");",10);
        }
        
    }
    else return;
}


    
function stringToXml(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;
    
}

function PopupMessage(message)
{
    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(message);
      generator.document.write('</body></html>');
      generator.document.close();
    }
    else alert('popup was blocked.');
  
}

