﻿/*
* AJAXConnection.js
* Written by Angelo Munoz (angelo250@gmail.com)
* This javascript library will serve as a cross browser AJAX client side library.
* Date: Sept 2008
* Edited: July 2009 (Added support for POST HTTP method type)

* Properties:
    handler      : Function pointer. connection.handler = function () { }
    async        : Boolean, get|set to perform asynchronously. Default: true
    method       : String, get|set, The HTTP method to use. Default: GET
    data         : String, get|set, string data to send. Example: connection.data='id=4&name=unforgiving conquerer';
    isBusy       : Boolean, get, notify whether transaction in process
    disableCache : Boolean, set, allow request to be unique each time
* Methods:
    open(httpMethod,uri,async)       : Initialize the connection
    abort()       : Abort current operation
    send(data)    : Start the operation sending the corresponding data
    
* Example:

var conn = new Connection();
conn.init("GET","Example.aspx",true);
conn.handler = function() 
{
  if (this.readyState==4) //request complete.
    {
        alert(this.responseText);
        conn.isBusy = false;
    }    
}
conn.send("id=47&name=unforgiving Conquerer");

*/

function Connection()
{
    var cn = null;
    var br = navigator.appName.toLowerCase().indexOf("microsoft")>-1?"IE":"FF";
    this.init = function( httpMethod, uri, async ) 
    {
        this.isBusy = false;
        var conn = null;
        try { conn = new XMLHttpRequest(); } //MODERN Browsers
        catch(e) 
        { 
            try { conn = new ActiveXObject("Microsoft.XMLHTTP"); } //IE5/6
            catch(ex) 
            {   //not supported
                conn=null;                
            }
        }
        if (conn) 
        {
            //get url from browser.
            if (httpMethod) this.method = httpMethod;
            if (!this.method) this.method = "GET";
            if (async) this.async = async;
            if (!this.async) this.async = true; 
            this.uri = uri;           
        }
        cn = conn;
        return (cn) ? true : false;
    }
    this.reinit = function(method, uri, async)
    {
        this.abort();
        this.init(method,uri,async);
    }
    this.abort = function() 
    {
        try { cn.abort(); }
        catch(e) {}   
        this.isBusy = false;
    }
    this.send = function(data)
    {
        if (!cn) return false;
        this.isBusy = true;        
        if (!this.method) method = "GET";
        if (!this.async) this.async = true;
        if (data) this.data = data;
        switch(this.method.toUpperCase())
        {
          default:
          case "GET":
          {
            if (this.data) this.uri += '?' + this.data; //append data to uri
            if (this.disableCache) (this.uri.indexOf('?')>-1) ? (this.uri += '&rand=' & Math.floor(Math.random()*10000)) : (this.uri += '?rand=' + Math.floor(Math.random()*10000));
            cn.open(this.method,this.uri,this.async);
            cn.onreadystatechange = this.handler;
            if (br=="IE") cn.timeout = this.timeout;
            cn.send(null);    
            break;
          }
          case "POST":
          {
            cn.open(this.method,this.uri,this.async);
            cn.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            cn.onreadystatechange = this.handler;
            if (this.disableCache) this.data += '&rand=' + Math.floor(Math.random()*10000);
            if (br=="IE") cn.timeout = this.timeout;
            cn.send(this.data);    
            break;
          }
        }            
    }
    //TODO: Can addHeader be called before cn.open() ?
    this.addHeader = function(type,value) 
    {
        if (!cn) return;
        try { cn.setRequestHeader(type,value); }
        catch(e) { }
    }
    this.getURL = function() 
    {
        //return the tail end of the url... with the file name removed.
        //if has dot then return up to the previous slash.
        //if not dot, strip slash and return.
        var u = window.location.href;
        if (u==null || u.length==0) return null; 
        return u.substr(0,u.lastIndexOf("/"));                
    }
    this.timeout = 3000; //default of 1s response time
    this.handler = function() {} //added by calling app
    this.async = true; //default async = true
    this.method = "GET"; //default get
    this.data = null; //default data=null
    this.isBusy = false; //busy with transaction.   
    this.disableCache = true;  
    this.uri = ''; //page to execute, relative
     
}