/****************************************************
 Wunderman Ajax Code Kit(WACK)  v1.0
 Author: Charlie Seitz
 Date: 12-15-2006
 Description: A very simple and complete Ajax toolkit
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
****************************************************/

var browserWidth;
var browserHeight;

if((document.documentElement) && (document.documentElement.scrollHeight)) {
	/** IE 6 & IE 7 **/
	browserWidth = document.documentElement.scrollWidth;
	browserHeight = document.documentElement.scrollHeight;
} else if (document.body) {
	/** IE 5 & Firefox 1 **/
	browserWidth = document.body.scrollWidth;
	browserHeight = document.body.scrollHeight;
} else {
	/** Netscape **/
	browserWidth = window.innerWidth;
	browserHeight = window.innerHeight;
}

function WAsyncXMLHttpRequest() {
    /*********************************************  
    Date: 12-15-2006
    Description: Creates a new XMLHTTP object.
    **********************************************/
    if(window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
        var msxmls = new Array(
            'Msxml2.XMLHTTP.5.0',
            'Msxml2.XMLHTTP.4.0',
            'Msxml2.XMLHTTP.3.0',
            'Msxml2.XMLHTTP',
            'Microsoft.XMLHTTP');
        for (var i = 0; i < msxmls.length; i++) {
            try {
                return new ActiveXObject(msxmls[i]);
            } catch (e) { /** browser not equiped to handle XMLHTTP **/ }
        }
    }
    throw new Error("Could not instantiate XMLHTTP object.");
}

function Wack() {
    /*********************************************  
    Date: 12-15-2006
    Description: Initializes a new Async object.
    **********************************************/
    this._xmlhttp = new WAsyncXMLHttpRequest();
    this.username = null;
    this.password = null;
}

function WAsync_call(action, url, data) {
    /*********************************************  
    Date: 12-15-2006
    Description: Initiates the ajax action.
    **********************************************/
    var instance = this;
    
    this._xmlhttp.open(action, WAsync_noCache(url), true, this.username, this.password);
    this.openCallback(this._xmlhttp);
    this._xmlhttp.onreadystatechange = function() {
        switch(instance._xmlhttp.readyState) {
        case 1:
            instance.loading();
            break;
        case 2:
            instance.loaded();
            break;
        case 3:
            instance.interactive();
            break;
        case 4:
			 try {
            	instance.complete(instance._xmlhttp.status, instance._xmlhttp.statusText, instance._xmlhttp.responseText, instance._xmlhttp.responseXML);
			 } catch (e) { }
            break;
        }
    }
	try {
    	this._xmlhttp.send(data);
	} catch(e) { }
}

function WAsync_get(url) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "GET" action.
    **********************************************/
    this.call("GET", url, null);
}

function WAsync_put(url, mimetype, datalength, data) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "PUT" action.
    **********************************************/
    this.openCallback = function(xmlhttp) {
        xmlhttp.setRequestHeader("Content-type", mimetype);
        xmlhttp.setRequestHeader("Content-Length", datalength);
    }
    this.call("PUT", url, data);
}

function WAsync_delete(url) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "DELETE" action.
    **********************************************/
    this.call("DELETE", url, null);
}

function WAsync_post(url, datalength, data) {
   /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "POST" action.
    **********************************************/
	var thisReference = this;
	this.userOpenCallback = this.openCallback;
	this.openCallback = function(xmlhttp) {
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-Length", datalength);
		thisReference.userOpenCallback(xmlhttp);
		thisReference.openCallback = thisReference.userOpenCallback;
	}
	this.call("POST", url, data);
}

function  WAsync_noCache(url) {
    /*********************************************  
    Date: 12-15-2006
    Description: Prevents caching of querystring
    **********************************************/
	var qs = new Array();
	var arr = url.split('?');
	var scr = arr[0];
	if(arr[1]) qs = arr[1].split('&');
	qs[qs.length]='nocache='+new Date().getTime();
	return scr+'?'+qs.join('&');
}

function WAsync_loading() {
    /*********************************************  
    Date: 12-15-2006
    Description: Show "loading..." screen
    **********************************************/
	try {
		/**
		var WAsyncLoader = document.getElementById("wack_loading");
		var loadingLeft = parseInt(browserWidth - WAsyncLoader.offsetWidth) / 2;
		var loadingTop = parseInt(browserHeight - WAsyncLoader.offsetHeight) / 2;
		WAsyncLoader.style.visibility = "visible";
		WAsyncLoader.style.left = loadingLeft + "px";
		WAsyncLoader.style.top =  loadingTop + "px";
		**/
		document.getElementById("wack_loading").style.visibility = "visible";
	} catch(e) { /** browser not equiped to handle runtime style settings **/ }
}

function WAsync_loaded() {
    /*********************************************  
    Date: 12-15-2006
    Description: Hide "loading..." screen
    **********************************************/
	try {
		document.getElementById("wack_loading").style.visibility = "hidden";
	} catch(e) { /** browser not equiped to handle runtime style settings **/ }
}

function WAsync_interactive() { 
    /*********************************************  
    Date: 12-15-2006
    Description: XmlHttp ReadyState "interactive"
    **********************************************/
}

function WAsync_complete(status, statusText, responseText, responseHTML) {
   /**********************************************  
    Date: 12-15-2006
    Description: XmlHttp ReadyState "complete"
    **********************************************/

}

function WAsync_openCallback(obj) { 
    /*********************************************  
    Date: 12-15-2006
    Description: Explictly opens the XMLHTTP object
    **********************************************/
}

/*************************************************
 Class definitions / functions
**************************************************/
Wack.prototype.openCallback = WAsync_openCallback;
Wack.prototype.loading = WAsync_loading;
Wack.prototype.loaded = WAsync_loaded;
Wack.prototype.interactive = WAsync_interactive;
Wack.prototype.complete = WAsync_complete;
Wack.prototype.get = WAsync_get;
Wack.prototype.put = WAsync_put;
Wack.prototype.del = WAsync_delete;
Wack.prototype.post = WAsync_post;
Wack.prototype.call = WAsync_call;


