﻿/*
Author  :   Mihir V Sathe
9 July 2010
*/


//The main Object for AJAX. Returns an AJAXError object if any problem.

var tel = new Object();

//self identifiable flags for this class

tel.XmlHttpBuilder = function () {
    this.XmlHttpFlag = true;
    this.ActiveXFlag = false;
    this.XmlHttpObj = null;
    this.ActiveOldFlag = false;
    this.ActiveNewFlag = false;
}

tel.XmlHttpBuilder.prototype = {
    GetAJAX: function () {
        if (this.XmlHttpFlag) {
            this.XmlHttpObj = new XMLHttpRequest();
            return this.XmlHttpObj;
        }
        else if (this.ActiveXFlag) {
            try {
                xmlhttp.ajax = new ActiveXObject("Micrsoft.XMLHTTP");
                this.ActiveOldFlag = true;
            }
            catch (exception) {
                this.XmlHttpObj = new ActiveXObject("MSXML2.XMLHTTP");
                this.ActiveNewFlag = true;
            }
            return this.XmlHttpObj;
        }
        else {
            var ErrorHandler = new AJAXErrorHandler(1);
            throw ErrorHandler;
        }
    }
}
/**
*@class AJAXError represents a system to handle and document the errors that occured in current operation. This acts as a separate system from rest of the 
*layered system because this can be used by ane layer.
*@constructor A new object of AJAXError.
*/
tel.AJAXError = function (ErrorCode) {
    this.AJAXErrorCode = ErrorCode;
}

tel.AJAXError.prototype = {
    /**
    *Gets a detailed message for the occured error. 
    */
    Message: function () {
        switch (this.AJAXErrorCode) {
            case 0:
                return "Unknown Error";
                break;
            case 1:
                return "AJAX not enabled";
                break;
            case 2:
                return "Sending async request failed";
                break;
            case 3:
                return "Sending sync request failed";
                break;
            case 4:
                return "This method is not supported";
                break;
            default:
                return "Unknown Error";
                break;
        }
    }
}

/**
*@class to send the final request and get a response. No need to call any method if using the library in prescribed way.
*@constructor creates a new object.
*/
tel.AJAXResp = function (onload , vars) {
    this.AJAXObject = new tel.XmlHttpBuilder().GetAJAX();
    this.Vars = vars;
    this.Method = this.Vars.HTTP_METHOD;
    this.Mode = this.Vars.AJAX_SEND_MODE;
    this.Url = this.Vars.URL;
    this.Send = null;
    this.onload = onload;
    this.TRANSACTION_STATUS = false;
    this.Condition = this.Vars.READY_STATE_COMPLETE;
    x = new tel.RequestBuilder(this.Url);
    if (this.Method == "POST") {
        this.Send = x.GetQueryString();
        this.Url = x.GetURI();
    }
}

tel.AJAXResp.prototype = {
    /**
    *Method to start an Asynchronous operation. set a function for this object's onload property.
    */
    SendRequestAsync: function () {
        var ajax = this.AJAXObject;
        if (ajax != null) {
            ajax.open(this.Method, this.Url);
            if (this.Method == "POST") {
                ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                ajax.setRequestHeader("Content-length", this.Send.length);
            }
            var loader = this;
            ajax.onreadystatechange = function () {
                loader.ReadyStateChange.call(loader);
            }
            ajax.send(this.Send);
        }
    },
    /**
    *Only for implicit calls.
    */
    ReadyStateChange: function () {
        var ajax = this.AJAXObject;
        if (ajax.readyState == 4 && ajax.status == 200) {
            this.onload.call(this);
        }
    }
}

/**
*@class to build final request and get a response. No need to call any method if using the library in prescribed way.
*@constructor creates a new object.
*/
tel.RequestBuilder = function (query) {
    this.Query = query;
    this.URI = null;
    this.QueryString = null;
    this.SetURI();
}

tel.RequestBuilder.prototype = {
    SetURI: function () {
        var last = this.Query;
        var index = last.indexOf("?", 0);
        var link = last.substr(0, index);
        this.URI = link;
        var qs = last.substr(index + 1, last.length);
        this.QueryString = qs;
    },

    GetURI: function () {
        return this.URI;
    },

    GetQueryString: function () {
        return this.QueryString;
    }

}

var VariablesObj = null;

tel.Variables = function () {
    //Variables forms the uppermost layer

    this.READY_STATE_UNINTIALISED = 0;
    this.READY_STATE_LOADING = 1;
    this.READY_STATE_LOADED = 2;
    this.READY_STATE_INTERACTIVE = 3;
    this.READY_STATE_COMPLETE = 4;

    this.HTTP_METHOD = "GET";
    this.AJAX_SEND_MODE = true; //true for async
    this.DOM_DEST = "none";
    this.RESPONSE = null;

    this.URL = null;
}

tel.Variables.prototype = {
    fire: function (RespRedirect , v) {
        var req = new tel.AJAXResp(RespRedirect , v);
        req.SendRequestAsync();
    }
}

tel.Variables.GetVar = function () {

    if (VariablesObj == null) {
        VariablesObj = new tel.Variables();
        return VariablesObj;
    }
    else {
        return VariablesObj;
    }
}


