﻿var Ajax = {

    Type: 1,
    Codes: "",
    Page: "Ajax/ParseCode.aspx",
    MyhttpObject: null,
    UsePost: false,
    Method: '',
    Parameters: '',
    Counter: 0,
    ready: true,
    CodeList: new Array(),
    StartAjax: function(params, method) {
        this.ready = false;
        document.getElementById('AjaxTemp').innerHTML += ('<input onclick=" return ' + method + '()" type="hidden" id="AjaxHidden' + this.Counter + '" value="' + this.Type + ',' + method + ',' + params + '">');
        var hdn = document.getElementById('AjaxHidden' + this.Counter);
        this.Counter++;

        return hdn;
    },

    StopAjax: function(id) {

        var hdn = document.getElementById(id);
        document.getElementById('AjaxTemp').removeChild(hdn);
        this.ready = true;
    },
    AddCode: function(code) {
        this.Codes += code;
    },
    SendCode: function() {
        if (this.ready == false) {
            setTimeout("Ajax.SendCode()", 100);
            return;
        }
        this.Parameters = Date.prototype.getHours() + Date.prototype.getMinutes() + Date.prototype.getSeconds() + Date.prototype.getMilliseconds();
        this.Page = "../../Pages/Ajax/ParseCode.aspx?code=" + this.Codes + "&counter=" + this.Counter;
        this.Type = 1;
        this.doWork();
        this.Codes = '';
    },
    GetXml: function(xmlFile, method, parameters, nocache) {

        if (this.ready == false) {

            setTimeout("Ajax.GetXml('" + xmlFile + "','" + method + "','" + parameters + "','" + nocache + "')", 100);
            return;
        }
        if (nocache != 'true' && nocache != true) {
            for (var i = 0; i < this.CodeList.length; i++) {
                if (this.CodeList[i] == parameters) {
                    return;
                }
            }
        }

        this.CodeList.push(parameters);
        this.Type = 2;
        this.Page = xmlFile;
        this.Method = method;
        this.Parameters = parameters;
        this.doWork();
    },

    GetPage: function(page, method, parameters, nocache) {
        if (this.ready == false) {
            setTimeout("Ajax.GetPage('" + page + "','" + method + "','" + parameters + "')", 100);
            return;
        }
        if (nocache != 'true' && nocache != true) {
            for (var i = 0; i < this.CodeList.length; i++) {
                if (this.CodeList[i] == parameters) {
                    return;
                }
            }
        }


        page += "&dateTime=" + this.Counter;




        this.CodeList.push(parameters);

        this.Type = 3;
        this.Page = page;
        this.Method = method;
        this.Parameters = parameters;
        this.doWork();
    },
    //  
    //    ShowLoading: function(loader) {
    //        loader.style.visibility = 'visible';
    //        loader.style.display = '';
    //    },

    //    HideLoading: function(loader) {
    //        loader.style.visibility = 'hidden';
    //        loader.style.display = 'none';
    //    },


    getMyHTTPObject: function() {
        var xmlHttp = null;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            // Internet Explorer
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    },
    setMyOutput: function() {
        if (this.readyState == 4) {
            var text = this.responseText;
            text = String.Replace(text, "ÄŸ", "ğ");
            text = String.Replace(text, "Ã¶", "ö");
            text = String.Replace(text, "ÅŸ", "ş");
            text = String.Replace(text, "Ã§", "ç");
            text = String.Replace(text, "Ã¼", "ü");
            text = String.Replace(text, "Ä±", "ı");
            text = String.Replace(text, "Ä", "Ğ");
            text = String.Replace(text, "Ãœ", "Ü");
            text = String.Replace(text, "Å", "Ş");
            text = String.Replace(text, "Ä°", "İ");
            text = String.Replace(text, "Ã‡", "Ç");
            text = String.Replace(text, "Ã", "Ö");
            text = String.Replace(text, "ï»¿", "");

            text = String.Replace(text, "yy9BA83FF5-83AE-45E3-B606-5DC32DFA23FC", "ğ");
            text = String.Replace(text, "yyE356C51B-3032-4617-8E0B-49D08BC5C9D8", "ö");
            text = String.Replace(text, "yy372750EC-C6D6-4990-89E1-ECD14EF14F49", "ş");
            text = String.Replace(text, "yyBC2BF0F2-B853-443A-9EFB-DD725130FB5B", "ç");
            text = String.Replace(text, "yy31BFBB23-61C7-435D-B1FC-EAAC0FC48D8C", "ü");
            text = String.Replace(text, "yy8696522A-BCEC-44FD-BE3D-B563F0C2DED1", "ı");
            text = String.Replace(text, "yy97737D08-D440-450D-B831-01F59E00BE65", "Ğ");
            text = String.Replace(text, "yy1061F823-5C8D-459D-808A-FFD121E976B4", "Ü");
            text = String.Replace(text, "yy50C4D8BA-9EDC-41FE-8F98-0B01577D4BF7", "Ş");
            text = String.Replace(text, "yy276884E7-2B19-4C45-A48F-BB1FCC3EC806", "İ");
            text = String.Replace(text, "yyD9A8A4EE-7266-4C05-B72B-B4B48D676246", "Ç");
            text = String.Replace(text, "yy812D3E0C-84EE-4F57-9842-90DE183267B5", "Ö");


            text = String.Replace(text, "yy952D1E0C-13AE-4F57-9842-90DE183367B5", "'");
            text = String.Replace(text, "yy042D2E0C-14DE-4F57-9842-90DE183477B5", '"');
            text = String.Replace(text, "yy202D3E0C-15VE-4F57-9842-90DE183787B5", "?");
            text = String.Replace(text, "yy312D4E0C-16BE-4F57-9842-90DE183907B5", "/");
            text = String.Replace(text, "yy422D5E0C-17NE-4F57-9842-90DE183787B5", "\\");
            text = String.Replace(text, "yy532D6E0C-21ME-4F57-9842-90DE183907B5", "|");
            text = String.Replace(text, "yy642D7E0C-22QE-4F57-9842-90DE183247B5", "#");
            text = String.Replace(text, "yy732D8E0C-23WE-4F57-9842-90DE183637B5", "$");
            text = String.Replace(text, "yy822D9E0C-24EE-4F57-9842-90DE183527B5", "&");
            text = String.Replace(text, "yy992D0E0C-25RE-4F57-9842-90DE183807B5", ".");
            text = String.Replace(text, "yy982D9E0C-26TE-4F57-9842-90DE183797B5", ":");
            text = String.Replace(text, "yy872D8E0C-27YE-4F57-9842-90DE183487B5", "~");
            text = String.Replace(text, "yy762D7E0C-28UE-4F57-9842-90DE183377B5", ";");
            text = String.Replace(text, "yy692D6E0C-29IE-4F57-9842-90DE183557B5", "!");
            text = String.Replace(text, "yy552D5E0C-31EO-4F57-9842-90DE183266B5", "%");
            text = String.Replace(text, "yy642D4E0C-32EA-4F57-9842-90DE183277B5", "=");
            text = String.Replace(text, "yy732D3E0C-33ES-4F57-9842-90DE183288B5", "http");
            text = String.Replace(text, "yy822D2E0C-34ED-4F57-9842-90DE183299B5", "www");
            text = String.Replace(text, "yy902D1E0C-36EF-4F57-9842-90DE183200B5", "´");
            text = String.Replace(text, "yy952D1E0C-13AE-4F58-9872-41DD183367B5", "<");
            text = String.Replace(text, "yy042D2E0C-14DE-4F50-9862-32DF183477B5", ">");

            var type = Ajax.getType(this.obj.value);

            var params = Ajax.getParams(this.obj.value);

            var method = Ajax.getMethod(this.obj.value);

            if (type == 1) {

                CodeHelper.ParseCodeList(text);
            }
            if (type == 2) {
                CodeHelper.RunMethod(method, text, params);
            }
            if (type == 3) {
                CodeHelper.RunMethod(method, text, params);
            }
            Ajax.StopAjax(this.obj.id);
        }
    },
    getType: function(val) {
        var vals = val.split(',');
        return vals[0];
    },
    getParams: function(val) {
        var vals = val.split(',');
        return vals[2];
    },

    getMethod: function(val) {
        var vals = val.split(',');
        return vals[1];
    },
    doWork: function() {
        this.MyhttpObject = this.getMyHTTPObject();
        if (this.MyhttpObject != null) {
            this.Page = String.Replace(this.Page, "ğ", "yy9BA83FF5-83AE-45E3-B606-5DC32DFA23FC");
            this.Page = String.Replace(this.Page, "ö", "yyE356C51B-3032-4617-8E0B-49D08BC5C9D8");
            this.Page = String.Replace(this.Page, "ş", "yy372750EC-C6D6-4990-89E1-ECD14EF14F49");
            this.Page = String.Replace(this.Page, "ç", "yyBC2BF0F2-B853-443A-9EFB-DD725130FB5B");
            this.Page = String.Replace(this.Page, "ü", "yy31BFBB23-61C7-435D-B1FC-EAAC0FC48D8C");
            this.Page = String.Replace(this.Page, "ı", "yy8696522A-BCEC-44FD-BE3D-B563F0C2DED1");
            this.Page = String.Replace(this.Page, "Ğ", "yy97737D08-D440-450D-B831-01F59E00BE65");
            this.Page = String.Replace(this.Page, "Ü", "yy1061F823-5C8D-459D-808A-FFD121E976B4");
            this.Page = String.Replace(this.Page, "Ş", "yy50C4D8BA-9EDC-41FE-8F98-0B01577D4BF7");
            this.Page = String.Replace(this.Page, "İ", "yy276884E7-2B19-4C45-A48F-BB1FCC3EC806");
            this.Page = String.Replace(this.Page, "Ç", "yyD9A8A4EE-7266-4C05-B72B-B4B48D676246");
            this.Page = String.Replace(this.Page, "Ö", "yy812D3E0C-84EE-4F57-9842-90DE183267B5");

            this.Page = String.Replace(this.Page, "<", "yy952D1E0C-13AE-4F58-9872-41DD183367B5");
            this.Page = String.Replace(this.Page, ">", "yy042D2E0C-14DE-4F50-9862-32DF183477B5");
            this.Page = String.Replace(this.Page, "'", "yy952D1E0C-13AE-4F57-9842-90DE183367B5");
            this.Page = String.Replace(this.Page, '"', "yy042D2E0C-14DE-4F57-9842-90DE183477B5");
            //            this.Page = String.Replace(this.Page, "?", "yy202D3E0C-15VE-4F57-9842-90DE183787B5");
            //            this.Page = String.Replace(this.Page, "/", "yy312D4E0C-16BE-4F57-9842-90DE183907B5");
            // this.Page = String.Replace(this.Page, "\\", "yy422D5E0C-17NE-4F57-9842-90DE183787B5");
            this.Page = String.Replace(this.Page, "|", "yy532D6E0C-21ME-4F57-9842-90DE183907B5");
            this.Page = String.Replace(this.Page, "#", "yy642D7E0C-22QE-4F57-9842-90DE183247B5");
            this.Page = String.Replace(this.Page, "$", "yy732D8E0C-23WE-4F57-9842-90DE183637B5");
            //   this.Page = String.Replace(this.Page, "&", "yy822D9E0C-24EE-4F57-9842-90DE183527B5");
            //  this.Page = String.Replace(this.Page, ".", "yy992D0E0C-25RE-4F57-9842-90DE183807B5");
            //   this.Page = String.Replace(this.Page, ":", "yy982D9E0C-26TE-4F57-9842-90DE183797B5");
            this.Page = String.Replace(this.Page, "~", "yy872D8E0C-27YE-4F57-9842-90DE183487B5");
            //    this.Page = String.Replace(this.Page, ";", "yy762D7E0C-28UE-4F57-9842-90DE183377B5");
            this.Page = String.Replace(this.Page, "!", "yy692D6E0C-29IE-4F57-9842-90DE183557B5");
            //     this.Page = String.Replace(this.Page, "%", "yy552D5E0C-31EO-4F57-9842-90DE183266B5");
            //            this.Page = String.Replace(this.Page, "=", "yy642D4E0C-32EA-4F57-9842-90DE183277B5");
            //            this.Page = String.Replace(this.Page, "http", "yy732D3E0C-33ES-4F57-9842-90DE183288B5");
            //            this.Page = String.Replace(this.Page, "www", "yy822D2E0C-34ED-4F57-9842-90DE183299B5");
            this.Page = String.Replace(this.Page, "´", "yy902D1E0C-36EF-4F57-9842-90DE183200B5");
            if (this.UsePost) {
                try {
                    this.MyhttpObject.open('POST', this.Page, true);
                    this.MyhttpObject.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
                    this.MyhttpObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                    this.MyhttpObject.setRequestHeader('Content-length', 0);
                    this.MyhttpObject.setRequestHeader('Connection', 'close');
                    this.MyhttpObject.onreadystatechange = Ajax.setMyOutput;
                    this.MyhttpObject.send(null);
                }
                catch (ex)
                { }
            }

            else { try {


                this.MyhttpObject.open("GET", this.Page, true);
                this.MyhttpObject.obj = this.StartAjax(this.Parameters, this.Method.toString());
                this.MyhttpObject.send(null);
                this.MyhttpObject.onreadystatechange = this.setMyOutput;
            }
            catch (ex)
                { }
            }
        }
    }
}
