//Ajax Manager - Send a command to the givin url. The command can be
//post, get, ... Data is the data to be sent to the server. Callback
//is the function that you want to perform when the read state
//changes. It should take two argument this will be the responseText and a closure.
function ajax_manager(browser, url, command, data, callback, unique, closure) {
   if(browser.xmlHttp) {
       if (unique){
           myurl = uniqueUrl(url);
       }
       else{
           myurl = url;
       }
      browser.xmlHttp.open(command, myurl, true);
      browser.xmlHttp.onreadystatechange =
         function (){
            if (browser.xmlHttp.readyState == 4 && browser.xmlHttp.status == 200){
                callback(browser.xmlHttp.responseText, closure);
            }
         }
      browser.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      browser.xmlHttp.send(data);
   }
}

function BrowserInfo(){
    if (/Firefox[\/\s]*(\d+\.\d+)/.test(navigator.userAgent)){
        this.appName = 'firefox';
        this.appVersion = new Number(RegExp.$1);
    }
    else if (/MSIE *(\d+\.\d+);/.test(navigator.userAgent)){
        this.appName = 'msie';
        this.appVersion = new Number(RegExp.$1);
    }
    else{
        this.appName = 'other';
        this.appVersion = 0;
    }
}

function MozBrowser(){
    this.type = 'moz';
    this.xmlHttp = new XMLHttpRequest ();
    browserInfo = new BrowserInfo;
    this.appName = browserInfo.appName;
    this.appVersion = browserInfo.appVersion;
}

MozBrowser.prototype.ajaxRequest = function (url, command, data, callback, unique, closure){
    ajax_manager(this,url,command,data,callback, unique, closure);
}


MozBrowser.prototype.readXML = function(xmlData) {
    var parser = new DOMParser();
    return parser.parseFromString(xmlData, "text/xml");
}

MozBrowser.prototype.getKey = function(e) {
    return e.which;
}

function IeBrowser(){
    this.type = 'ie';
    try {
        this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            this.xmlHttp = false;
        }
    }
    browserInfo = new BrowserInfo();
    this.appName = browserInfo.appName;
    this.appVersion = browserInfo.appVersion;
}

IeBrowser.prototype.readXML = function(xmlData) {
    var doc = new ActiveXObject("Microsoft.XMLDOM");
    doc.async = false;
    doc.loadXML(xmlData);
    return doc;
}

    IeBrowser.prototype.ajaxRequest = function (url, command, data, callback, unique, closure){
        ajax_manager(this,url,command,data,callback,unique, closure);
}

IeBrowser.prototype.getKey = function(e) {
    return window.event.keyCode;
}

function getBrowser(){
    if (window.ActiveXObject){
        return new IeBrowser;
    }
    else {
        return new MozBrowser;
    }
}
