   String.prototype.ReplaceAll = function (stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}
function ReplaceAll (temp, stringToFind, stringToReplace) {
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

String.prototype.startsWith = function (str) {
    return (this.match("^" + str) == str)
}

function startsWith(inpt,str) {
    return (inpt.match("^" + str) == str)
}

String.prototype.endsWith = function (str) {
    return (this.match(str + "$") == str)
}

function endsWith(inpt, str) {
    return (inpt.match(str + "$") == str)
}

 
function Loaded() 
{
    this.Functions = new Array();    
    this.Add = function(NewFunc)
    {
        this.Functions[this.Functions.length] =  NewFunc;
    }
    
    this.Run = function() {
        for(var a in this.Functions){
            this.Functions[a]();
        }
    }
}
var GlobalAjaxLoaded = new Loaded();

function AjaxHandler() {
    this["LoadingImage"] = "Images/Loading.gif";
    this["LoadingText"] = "";
    this["LoadingImageObject"] = null;
    this["LoadingTextObject"] = null;
    this["LoadingTableObject"] = null;
    this["Place"] = null;
    this["Page"] = null;
    this.XMLHttp = null;
    this["Loaded"] = function () { };
    this["Append"] = false;
    this["Response"] = null;
    this["Method"] = "GET";
    this["Variables"] = "";
    this["Async"] = true;
    this["ContentType"] = "application/x-www-form-urlencoded"; 
    this.Initialize = function () {
        this.CreateXMLHttpObject();
    }
    this.LoadPage = function () {
        try {
            if (this["Page"] == null) {
                if (arguments[0] == null) {
                    alert("Need to set Page parameter!\nExample\n\tAjax.Page = 'something.html'\n\tAjax[\"Page\"]='something.html'");
                    return;
                }
            }
            if (this["Place"] != null) {


                this.CreateLoadTable();

                if (arguments.length > 0) {
                    alert("args" + arguments[0]);
                    this.SetDatas(arguments);
                } else {
                    if (this["Place"] == null)
                        this.CreatePlace();
                    if (typeof (this["Place"]) == "string") {
                        this["Place"] = FindObject(this["Place"]);
                    }
                }

                if (this["Append"] == false)
                    this["Place"].innerHTML = "";
                //alert(this["LoadingImageObject"].src);
                this["Place"].appendChild(this["LoadingTableObject"]);
                //this["Place"].innerHTML += this["LoadingText"];
                //alert(this["Page"]);
            }
            var ajax = this;
            var func = this.ReadyStateChange;
            this.XMLHttp.onreadystatechange = function (event) { func(event, ajax); };
            if (this.Method == "GET") {
                this.XMLHttp.open("GET", this["Page"].ReplaceAll("&amp;", "&"), this["Async"]);
                this.XMLHttp.send();
            } else {
                this.XMLHttp.open("POST", this["Page"], this["Async"]);             
                this.XMLHttp.setRequestHeader("Content-Type", this["ContentType"]);
                if(this["Variables"].toString().indexOf("+") > -1)
                    this["Variables"] = this["Variables"].ReplaceAll("+","%2B");
                this.XMLHttp.send(this["Variables"].toString());                                                          
            }
        } catch (ex) {
            alert(ex);
        }
    }
    this.Cancel = function () {
        if (this.XMLHttp != null)
            this.XMLHttp.abort();
    }
    this.SetDatas = function () {
        if (typeof (arguments[0]) == "string") {
            this["Page"] = arguments[0];
            this["Place"] = this.CreatePlace();
            this["Loaded"] = function () { };
            this["Append"] = false;
        }
        if (typeof (arguments[0]) == "object") {
            if (arguments[0]["Page"] != null)
                this["Page"] = arguments[0]["Page"];
            else
                this["Page"] = "http://google.com";
            if (arguments[0]["Place"] != null)
                this["Place"] = FindObject(arguments[0]["Place"]);
            else
                this["Place"] = this.CreatePlace();
            if (arguments[0]["Loaded"] != null)
                this["Loaded"] = arguments[0]["Loaded"];
            else
                this["AfterLoad"] = function () { };
            if (arguments[0]["Append"] != null)
                this["Append"] = arguments[0]["Append"];
            else
                this["Append"] = false;
            if (arguments[0]["LoadingImage"] != null)
                this["LoadingImage"] = arguments[0]["LoadingImage"];
        }
    }
    this.CreatePlace = function () {
        this["Place"] = document.createElement("div");
        this["Place"].style.position = "absolute";
        this["Place"].style.top = "0px";
        this["Place"].style.left = "0px";
        this["Place"].style.width = "100px";
        this["Place"].style.height = "100px";
        this["Place"].style.display = "none";
        document.body.appendChild(this["Place"]);
        return this["Place"];
    }
    this.CreateLoadImage = function () {
        this["LoadingImageObject"] = document.createElement("img");
        //alert(this["LoadingImage"]);
        this["LoadingImageObject"].src = this["LoadingImage"];
        return this["LoadingImageObject"];
    }
    this.CreateLoadText = function () {
        this["LoadingTextObject"] = document.createElement("font");
        //alert(this["LoadingImage"]);
        this["LoadingTextObject"].innerHTML = this["LoadingText"];
        return this["LoadingTextObject"];
    }

    this.CreateLoadTable = function () {
        this.CreateLoadImage();
        this.CreateLoadText();
        this["LoadingTableObject"] = document.createElement("table");
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        td.style.verticalAlign = "middle";
        td.appendChild(this["LoadingImageObject"]);
        tr.appendChild(td);
        td = document.createElement("td");
        td.innerHTML += this["LoadingText"];
        tr.appendChild(td);
        this["LoadingTableObject"].appendChild(tr);
        //alert(this["LoadingTableObject"].innerHTML);
    }
    this.CreateXMLHttpObject = function () {
        try {
            this.XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");    // Trying Internet Explorer 
        }
        catch (e)    // Failed 
        {
            this.XMLHttp = new XMLHttpRequest();    // Other browsers.
        }
    }
    this.ReadyStateChange = function (event, ajax) {
        switch (ajax.XMLHttp.readyState) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                ajax["Response"] = ajax.XMLHttp.responseText;
                if (ajax["Place"] != null) {
                    ajax["Place"].removeChild(ajax["LoadingTableObject"]);
                    if (ajax.Append) {
                        ajax["Place"].innerHTML = "";
                        ajax["Place"].innerHTML += ajax["Response"];
                    } else {
                        ajax["Place"].innerHTML = ajax["Response"];
                    }
                    //alert(ajax.XMLHttp.responseText);
                }
                ajax["Loaded"]();   
                GlobalAjaxLoaded.Run();
                break;
            default:
                alert("Unknown state change!");
                break;
        }
    }
    this.Initialize();
}
AjaxHandler.prototype = new Object;

function AjaxObject() {
    this.Counter = 0;
    this.Ajaxs = new Array();

    this["LoadingImage"] = "Images/Loading.gif";
    this["Place"] = null;
    this["Page"] = null;
    this["Loaded"] = function () { };
    this["Append"] = false;

    this.LoadPage = function () {
        var currAjax = this.Init();
        currAjax.LoadPage();
    }
    this.Init = function () {
        this.Ajaxs[this.Counter] = new AjaxHandler();
        this.Ajaxs[this.Counter]["LoadingImage"] = this["LoadingImage"];
        this.Ajaxs[this.Counter]["Place"] = this["Place"];
        this.Ajaxs[this.Counter]["Page"] = this["Page"];
        this.Ajaxs[this.Counter]["Loaded"] = this["Loaded"];
        this.Ajaxs[this.Counter]["Append"] = this["Append"];
        this.Ajaxs[this.Counter]["LoadingText"] = this["LoadingText"];
        this.Counter++;
        this["LoadingImage"] = "Images/Loading.gif";
        this["Place"] = null;
        this["Page"] = null;
        this["Loaded"] = function () { };
        this["Append"] = false;
        return this.Ajaxs[this.Counter - 1];
    }
    this.ExitAll = function (AjaxObj) {
        for (var i = 0; i < AjaxObj.Ajaxs.length; i++)
            AjaxObj.Ajaxs[i].Cancel();
    }
    var exit = this.ExitAll;
    var AjaxObj = this;
    AttachEvent(window, "unload", function () { exit(AjaxObj); }, true);
}

AjaxObject.prototype = new Object;

function FindObject(obj) {
    if (typeof (obj) == "object")
        return obj;

    if (typeof (obj) == "string")
        return document.getElementById(obj);
}

function DeleteObject(obj) {
    try {
        obj = FindObject(obj);
        obj.parentNode.removeChild(obj);
    }   catch( ex){}
}

function IncludeScript(path) {
    Ajax.LoadPage(path, document.getElementsByTagName("head")[0], true);
}
