﻿//Constants for Ajax Objects
KuchiNet.STATE_UNINITIALIZED = 0;
KuchiNet.STATE_LOADING = 1;
KuchiNet.STATE_LOADED = 2;
KuchiNet.STATE_INTERACTIVE = 3;
KuchiNet.STATE_COMPLETE = 4;
KuchiNet.HTTP_METHOD_POST = "POST";
KuchiNet.HTTP_METHOD_GET = "GET";

//AjaxLoader component
//Constructor
KuchiNet.AjaxLoader = function(url, OnLoad, OnError, method, params, contentType)
{
    this.xmlHttpRequest = null;
    this.url = url;
    this.Message = "";
    this.OnLoad = OnLoad;
    this.OnError = (OnError) ? OnError : this.getDefaultError;
    this.InitializeXmlHttp();
    this.loadContent(url, method, params, contentType);
    
}

KuchiNet.AjaxLoader.prototype =
{
    InitializeXmlHttp : function()
    {
        if(window.XMLHttpRequest)
        {
            this.xmlHttpRequest = new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
            this.xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }     
    },

    loadContent : function(url, method, params, contentType)
    {
        if(!method)
        {
            method = KuchiNet.HTTP_METHOD_GET;
        }
        if(!contentType && method == KuchiNet.HTTP_METHOD_POST)
        {
            contentType = "application/x-www-form-urlencoded";
        }
        if(this.xmlHttpRequest)
        {
            try
            {
                var loader = this;
                this.xmlHttpRequest.onreadystatechange = function()
                {
                    loader.OnReady.call(loader);
                }
                this.xmlHttpRequest.open(method, url, true);
                if(contentType)
                    this.xmlHttpRequest.setRequestHeader("Content-Type", contentType);
                this.xmlHttpRequest.send(params);
            }
            catch(err)
            {
                this.OnError.call(this);
            }
        }
    },
    
    OnReady : function()
    {
        var xmlHttpRequest = this.xmlHttpRequest;
        if(xmlHttpRequest.readyState == KuchiNet.STATE_COMPLETE)
        {
            if(xmlHttpRequest.status == 200 || xmlHttpRequest.status == 0)
            {
                this.OnLoad.call(this, xmlHttpRequest);
            }
            else
            {
                this.OnError.call(this);
            }
        }
    },
    
    getDefaultError : function()
    {
        this.Message = "State : " + this.xmlHttpRequest.readyState 
                                  + "\n Status : " + this.xmlHttpRequest.status
                                  + "\n Headers : " + this.xmlHttpRequest.getAllResponseHeaders();            
                                  
        alert(this.Message);
    }
}

//XSLT Transformer 
//Constructor
KuchiNet.XSLTTransformer = function(xmlUrl, xslUrl)
{
    this.XmlUrl = xmlUrl;
    this.XslUrl = xslUrl; 
}

KuchiNet.XSLTTransformer.IsXSLTSupported = function()
{
    return ( window.XMLHttpRequest && window.XSLTProcessor) 
                                   || KuchiNet.XSLTTransformer.IsIEXmlSupported();
}

KuchiNet.XSLTTransformer.IsIEXmlSupported = function()
{
    if(!window.ActiveXObject)
        return false;
    try
    {
        new ActiveXObject("Microsoft.XMLDOM");
        return true;
    }
    catch(err)
    {
        return false;
    }
}

KuchiNet.XSLTTransformer.prototype = 
{
    viewLoader : function(container)
    {
        if(!KuchiNet.XSLTTransformer.IsXSLTSupported())
            return;
        this.XmlDocument = null;
        this.XslDocument = null;
        this.container = document.getElementById(container);
        new KuchiNet.AjaxLoader(this.XmlUrl, this.InitializeXmlDocument, "", "GET", "", "");
        new KuchiNet.AjaxLoader(this.XslUrl, this.InitializeXslDocument, "", "GET", "", "");  
    },
    
    InitializeXmlDocument : function(requestHttp)
    {
        this.XmlDocument = requestHttp.responseXML;
        //alert("Xml: " + this.XmlDocument.xml);
        this.updateCurrentView();
    },
    
    InitializeXslDocument : function(requestHttp)
    {
        this.XslDocument = requestHttp.responseXML;
        //alert("Xsl: " + this.XslDocument.xml);
        this.updateCurrentView();
        this.IEUpdater();
    },
    
    updateCurrentView : function()
    {
        if(!KuchiNet.XSLTTransformer.IsXSLTSupported())
            return;
        if(window.XMLHttpRequest && window.XSLTProcessor)
            this.MozzilaUpdater();
        else if (window.ActiveXObject)
            this.IEUpdater();
    },
    
    MozzilaUpdater : function()
    {
        var xslt = new XSLTProcessor();
        xslt.improtStylesheet(this.XslDocument);
        var fragment = xslt.TransformToFragment(this.XmlDocument, document);
        this.container.innerHTML = "";
        this.container.appendChild(fragment);
    },
    
    IEUpdater : function()
    {
        var result = this.XmlDocument.transformNode(this.XslDocument);
        this.container.innerHTML = result;       
    }
}