/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function AjaxLoadJs(file)
{
    if(AjaxLoadJs.hasOwnProperty("files") && AjaxLoadJs.files.indexOf(file) != -1 )
        return;
    if (file.indexOf(".js")!=-1)
    {
        fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", file);
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(fileref);
        AjaxLoadJs.files += file+" ";
    }
}

function AjaxLoadCss(file)
{
    if(AjaxLoadCss.hasOwnProperty("files") && AjaxLoadCss.files.indexOf(file) != -1 )
        return;
    if (file.indexOf(".css")!=-1)
    {
        fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", file);
        document.getElementsByTagName("head").item(0).appendChild(fileref);
        AjaxLoadCss.files += file+" ";
    }
}

function Ajax()
{
    if(Ajax.hasOwnProperty("Http"))
        return Ajax.Object;
    
    this.CreateRequest = function()
    {
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer")
        {
            try {
                HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){}
            }
        }
        else
        {
            HttpRequest = new XMLHttpRequest();
            if (HttpRequest.overrideMimeType)
                HttpRequest.overrideMimeType('text/html');
        }
        return HttpRequest;
    }

    this.Get = function(Url,ObjectId)
    {
        Http = false;
        Http = CreateRequest();
        Http.open('get', Url);
        Http.onreadystatechange = Loader;
        Loader.Http = Http;
        Loader.ObjectId = ObjectId;
        Http.send(null);
    }
    this.Post = function (Url,Parameters,ObjectId)
    {
       Http = false;
       Http = CreateRequest();
       Http.onreadystatechange = Loader;
       Http.open('POST', Url, true);
       Loader.Http = Http;
       Loader.ObjectId = ObjectId;
       Http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       Http.setRequestHeader("Content-length", Parameters.length);
       Http.setRequestHeader("Connection", "close");
       Http.send(Parameters);

    }
    this.Loader = function()
    {
        if(Loader.Http.readyState == 4)
        {
            var responseText = Loader.Http.responseText;
            // Load script an css files
            {
                var reScript = /<(?:script|link)\s+(type|src|language|rel|href)\s*=\s*(?:"|')?([^"^']*)(?:"|')?\s+(type|src|language|rel|href)\s*=\s*(?:"|')?([^"^']*)(?:"|')?(?:\s+(type|src|language|rel|href)\s*=\s*(?:"|')?([^"^']*)(?:"|')?)?\s*>/i;
                var match = reScript.exec(responseText);
                if(match)
                {
                    var type,src;
                    for(i=1;i<match.length;i++)
                    {
                        if(!match[i])
                            continue;
                        if(match[i].toLowerCase() == "type" || match[i].toLowerCase() == "language")
                            type = match[i+1].toLowerCase();
                        if(match[i].toLowerCase() == "src")
                            src = match[i+1].toLowerCase();
                        if(match[i].toLowerCase() == "href")
                            src = match[i+1].toLowerCase();
                    }

                    if(type == "text/javascript" || type == "javascript")
                        AjaxLoadJs(src);
                    else if(type == "text/css")
                        AjaxLoadCss(src);
                }
            }
            document.getElementById(Loader.ObjectId).innerHTML = responseText;
        }
    }

    Ajax.Object = this;
    return Ajax.Object;
}

function AjaxLoad()
{
    if(AjaxLoad.http.readyState == 4)
    {
        var responseText = AjaxLoad.http.responseText;
        // Load script an css files
        {
            var reScript = /<(?:script|link)\s+(type|src|language|rel|href)\s*=\s*(?:"|')?([^"^']*)(?:"|')?\s+(type|src|language|rel|href)\s*=\s*(?:"|')?([^"^']*)(?:"|')?(?:\s+(type|src|language|rel|href)\s*=\s*(?:"|')?([^"^']*)(?:"|')?)?\s*>/i;
            var match = reScript.exec(responseText);
            if(match)
            {
                var type,src;
                for(i=1;i<match.length;i++)
                {
                    if(!match[i])
                        continue;
                    if(match[i].toLowerCase() == "type" || match[i].toLowerCase() == "language")
                        type = match[i+1].toLowerCase();
                    if(match[i].toLowerCase() == "src")
                        src = match[i+1].toLowerCase();
                }

                if(type == "text/javascript" || type == "javascript")
                    AjaxLoadJs(src);
                else if(type == "text/css")
                    AjaxLoadCss(src);
            }
        }
        document.getElementById(AjaxLoad.Id).innerHTML = responseText;
    }
}

function AjaxPostRequest(Form,serverFileName,htmlObjectId)
{
    if(!AjaxLoad.hasOwnProperty("http"))
    {
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer")
        {
            try {
                AjaxLoad.http = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    AjaxLoad.http = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){}
            }
        }
        else
        {
            AjaxLoad.http = new XMLHttpRequest();
            if (AjaxLoad.http.overrideMimeType)
                AjaxLoad.http.overrideMimeType('text/html');
        }
    }

    http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);

}

function AjaxGetRequest(serverFileName,htmlObjectId)
{
    if(!AjaxLoad.hasOwnProperty("http"))
    {
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer")
            AjaxLoad.http = new ActiveXObject("Microsoft.XMLHTTP");
        else
            AjaxLoad.http = new XMLHttpRequest();
    }

    AjaxLoad.Id = htmlObjectId;
    AjaxLoad.http.open('get', serverFileName);
    AjaxLoad.http.onreadystatechange = AjaxLoad;
    AjaxLoad.http.send(null);
}

function AjaxTable(tableID)
{
    this.Object = function()
    {
        return document.getElementById(tableID);
    }
    this.RowNumberByAttribute = function(AttrName,AttrValue)
    {
        var table = document.getElementById(tableID);
        if(table)
        {
            for(i=0;i<table.rows.length;i++)
            {
                if(table.rows[i].getAttribute(AttrName) == AttrValue)
                    return i;
            }
        }
        return -1;
    }
    this.AddRow = function(Cells)
    {
        var table = document.getElementById(tableID);
        if(table)
        {
            var tr = table.insertRow(table.rows.length);
            var tdCount = table.rows.length?table.rows[0].cells.length:Cells.length;
            for (var n=0; n < Cells.length; n++)
            {
                var td = tr.insertCell(n);
                if(n == (Cells.length-1) && tdCount != Cells.length)
                {
                    td.colSpan = (tdCount - Cells.length) + 1;
                }
                td.innerHTML = Cells[n];
            }
            return tr;
        }
    }
    this.AddSeparator = function(colSpan,innerHTML)
    {
        var table = document.getElementById(tableID);
        if(table)
        {
            var tr = table.insertRow(table.rows.length);
            var td = tr.insertCell(0);
            td.colSpan = colSpan;
            td.innerHTML = innerHTML;
        }
    }
    this.DeleteRow = function(Num)
    {
        if(Num == -1)
            return;
        var table = document.getElementById(tableID);
        if(table)
        {
            table.deleteRow(i);
        }
    }
    return this;
}
