﻿
//raise the par on the Ajax support if need be
if(typeof(XMLHttpRequest) == "undefined") 
{
    XMLHttpRequest = function() 
    {
        try 
        { 
            return new ActiveXObject("Msxml2.XMLHTTP.6.0"); 
        }catch(ex){}
        
        try 
        { 
            return new ActiveXObject("Msxml2.XMLHTTP.3.0"); 
        }catch(ex){}
        
        try 
        { 
            return new ActiveXObject("Msxml2.XMLHTTP"); 
        }catch(ex){}
        
        try 
        { 
            return new ActiveXObject("Microsoft.XMLHTTP"); 
        }catch(ex){}
        
        
        throw new Error("This browser does not support XMLHttpRequest.");
    };
}

function AjaxChart()
{
    this.__chartBaseUrl = "http://localhost:2764/";
    
    this.ChartID = "";
    this.ChartTitle = "";
    this.ChartType = "Bar";
    this.ChartWidth = "640";
    this.ChartHeight = "320";
    
    //handle getting an ajax object
    this.GetHttp = function()
    {
        var xmlHttp = null;

        try 
        {
            xmlHttp = new XMLHttpRequest();
        }
        catch(ex) 
        {
            try 
            {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(ex) 
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        
        return xmlHttp;
    };//end GetHttp

    //called by the caller in their Create callback
    this.ProcessCreateCallback = function()
    {
        if (this.__createHttp.readyState == 4) 
        {
            // only if "OK"
            if (this.__createHttp.status == 200) 
            {
                this.ChartID = this.__createHttp.responseText;
                this.__createHttp = null;//try to release memory
            }
        }
    };//end AjaxChartProcCreate

    //initializes the chart
    this.Create = function(callback,title,type,width,height,skin)
    {        
        this.ChartTitle = title;
        this.ChartType = type;
        this.ChartWidth = width;
        this.ChartHeight = height;
        
        this.__createHttp = null;
        this.__createHttp = this.GetHttp();
        this.__createHttp.onreadystatechange  = callback;
        
        var query = "r=" + escape(Math.random() * 99999) + "&title=" + escape(title) + "&type=" + escape(type) + "&width=" + escape(width) + "&height=" + escape(height) + "&sk=" + escape(skin);

        this.__createHttp.open("GET", this.__chartBaseUrl + "AjaxChartCreate.aspx?" + query, true);
        this.__createHttp.send(null);
       
    };//end Create
    
    this.ImageTag = function()
    {
        return '<img src="' + this.__chartBaseUrl + 'AjaxChartRender.aspx?id=' + escape(this.ChartID) + '" alt="" />';
    };//end ImageTag

    //used to add a series of information to a chart
    this.AddSeries = function(name, values, labels, names)
    {
        //this ajax call needs to be self contained
        if(void(0) == this.ChartID || null == this.ChartID)
        {
            throw("Chart has not been created.");
        }
        
        var http = null;
        http = this.GetHttp();    
                
        var query = "r=" + escape(Math.random() * 99999) +"&id=" + escape(this.ChartID) + "&s=" + escape(name) + "&v=" + escape(values) + "&l=" + escape(labels) + "&n=" + escape(names);    
        http.open("GET", this.__chartBaseUrl + "AjaxChartAddSeries.aspx?" + query, false);
        http.send(null);
    };//end AddSeries
    
    //sets the values of the x axis labels
    this.SetXAxisLabels = function(labels)
    {
        //this ajax call needs to be self contained
        if(void(0) == this.ChartID || null == this.ChartID)
        {
            throw("Chart has not been created.");
        }
        
        var http = null;
        http = this.GetHttp();    
                
        var query = "r=" + escape(Math.random() * 99999) + "&id=" + escape(this.ChartID) + "&l=" + escape(labels);
        http.open("GET", this.__chartBaseUrl + "AjaxChartSetXLabels.aspx?" + query, false);
        http.send(null);
    };//end SetXAxisLabels
    
    //gets the html to render the chart from the server
    this.GetChartHtml = function()
    {
        //this ajax call needs to be self contained
        if(void(0) == this.ChartID || null == this.ChartID)
        {
            throw("Chart has not been created.");
        }
        
        var http = null;
        http = this.GetHttp();    
                
        var query = "r=" + escape(Math.random() * 99999) +"&id=" + escape(this.ChartID);
        http.open("GET", this.__chartBaseUrl + "AjaxChartRenderHtml.aspx?" + query, false);
        http.send(null);
        
        var ret = http.responseText.replace("/RadControls/Chart/",this.__chartBaseUrl + "RadControls/Chart/");
        return ret;
        
    };//end GetHtml
}
