/************************************
 *
 * Core Component
 * vajax.js
 * AJAX Library
 * Provides basic AJAX Services
 *

VLog 2.x - Multi-user ASP powered weblogging/content management system
Copyright (C) 2006-2008  Robert Vesse
rvesse@vdesign-studios.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 * Code is self executing to force the Namespace to be created and the Object made available
 ************************************/

(function() {

    if (!window.VLog) window['VLog'] = {};

    window.VLog.AJAXEngine = function(callback, extraInfo) {

        this.Get = makeGetRequest;
        this.Post = makePostRequest;
        this.FormPost = makeFormPostRequest;
        this.GetWithXml = makeGetRequestWithXml;
        this.PostWithXml = makePostRequestWithXml;
        this.AJAXSupported = supportsAJAX;

        function getHTTPObject() {
            var xhr = false;
            if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                try {
                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        xhr = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {
                        xhr = false;
                    }
                }
            }
            return xhr;
        }

        function callbackWrapper(request, callback) {
            if (request.readyState == 4) {
                if (request.status == 200 || request.status == 304) {
                    callback(request, extraInfo);
                } else {
                    alert('Error: Unable to complete AJAX Request successfully!');
                    alert(request.responseText);
                }
            }
        }

        function makeGetRequest(url) {
            makeGetRequestWithXml(url, null);
        }

        function makePostRequest(url) {
            makePostRequestWithXml(url, null);
        }

        function makeFormPostRequest(url, data) {
            var request = getHTTPObject();
            if (request) {
                request.open('POST', url, true);
                request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                request.setRequestHeader('Content-length', data.length);
                request.onreadystatechange = function() {
                    callbackWrapper(request, callback);
                }
                request.send(data);
            }
        }

        function makeGetRequestWithXml(url, xml) {
            var request = getHTTPObject();
            if (request) {
                request.onreadystatechange = function() {
                    callbackWrapper(request, callback);
                }
                request.open('GET', url, true);
                request.send(xml);
            }
        }

        function makePostRequestWithXml(url, xml) {
            var request = getHTTPObject();
            if (request) {
                request.onreadystatechange = function() {
                    callbackWrapper(request, callback);
                }
                request.open('POST', url, true);
                request.send(xml);
            }
        }

        function supportsAJAX() {
            var request = getHTTPObject();
            if (!request) {
                return false;
            } else {
                return true;
            }
        }
    } //End of AJAXEngine Object
})();