﻿/*------- XML HTTP REQUEST ------*/

// xmlHttpRequest 
var XmlHttpRequestObject;

function GetXmlHttpRequestObject() {
    // Firefox, Opera 8.0, Safari
    try {
        return new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            // Internet Explorer (5.0 ve öncesi sürümler)
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Tarayıcınız XmlHttpRequest nesnesini desteklemiyor!");
            }
        }
    }
    return null;
}

function SendRequest(requestUrl) {
    if (!XmlHttpRequestObject)
        XmlHttpRequestObject = GetXmlHttpRequestObject();

    // onReadyStateChange özelliği sunucudan gelecek yanıtı işleyecek metot bilgisini tutar (tanımlı bir metot veya geçici bir metot atanabilir)
    // geçici metot
    // XmlHttpRequestObject.onReadySateChange = function(){yapilacak işlemler};

    // tanımlı bir metot
    XmlHttpRequestObject.onreadystatechange = Process;

    // xmlHttpRequest nesnesi üzerinden bir istek başlatmak için 'open' ve 'send' metotları kullanılır

    // open("yapılacak isteğin yöntemi 'GET' yada 'POST'","işlem yapılacak sayfa","işlem asenkron mu olacak? (true, false)")
    XmlHttpRequestObject.open("get", requestUrl, true);

    // send(null) metodu ile istek sunucuya iletilir
    XmlHttpRequestObject.send();
}

function Process() {

    // readyState özelliği sunucu yanıtının durumunu tutar
    switch (XmlHttpRequestObject.readyState) {
        // istek başlatılmadı 
        case 0:
            break;

        // istek ayarlandı 
        case 1:
            break;

        // istek gönderildi 
        case 2:
            break;

        // istek işlemde 
        case 3:
            break;

        // istek tamamlandı 
        case 4:
            // XmlHttpRequestObject.responseText özelliği sunucudan gelen veriyi tutar

            var responseText = XmlHttpRequestObject.responseText;

            if (typeof (SetOnlineDestekIsOnline) != "undefined" && IsContains(responseText))
                SetOnlineDestekIsOnline(responseText);
            else if (typeof (SetHizliSiparisToplam) != "undefined")
                SetHizliSiparisToplam(responseText);
            else
                return responseText;
            break;
    }
}

function IsContains(stringMetin) {
    stringMetin = stringMetin.toLowerCase();
    var index1 = stringMetin.lastIndexOf(".");
    var index2 = stringMetin.lastIndexOf("g");
    var index3 = stringMetin.lastIndexOf("i");
    var index4 = stringMetin.lastIndexOf("f");

    if ((index1 + 1) == index2 && (index2 + 1) == index3 && (index1 + 3) == index4) {
        return true;
    }

    return false;
}

/*------- XML HTTP REQUEST ------*/



/*-------- MODAL --------*/

function OpenModal() {
    var modal = document.getElementById('Modal');
    modal.style.display = 'block';
    modal.style.height = document.documentElement.scrollHeight;

    window.scrollTo(0, 0);
    //addEvent(window, 'scroll', ScrollYapma);
}

function CloseModal() {
    var modal = document.getElementById('Modal');
    modal.style.display = 'none';
    //removeEvent('scroll', ScrollYapma);
}

/*-------- MODAL --------*/



/*------- POPUP WINDOW --------*/

function OpenPopupWindow(url, width, height, scrollbars) {
    window.open(url, "Popup", "location=0, toolbar=0, status=0, scrollbars=" + scrollbars + ", resizable=0, menubar=0, width=" + width + ", height=" + height, true);
}

/*------- POPUP WINDOW --------*/



/*-------- ACCEPT BUTTON ---------*/

function KeyDownHandler(button) {
    if (event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
        document.getElementById(button).click();
        //__doPostBack(button,'');

    }
}

/*-------- ACCEPT BUTTON ---------*/



/*-------- EVENT --------*/

function addEvent(obj, type, fn) {
    if (obj.attachEvent)
        obj.attachEvent('on' + type, fn);
    else
        obj.addEventListener(type, fn, false);
}

function removeEvent(eventName, fn) {
    window.detachEvent('on' + eventName, fn);
}

/*-------- EVENT --------*/



/*------ ARRAY ------*/

// Add contains method to Array
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] == obj)
            return true;
    }

    return false;
}

// Add indexOf method to Array
Array.prototype.indexOf = function(obj) {
    var i = 0;
    while (i < this.length) {
        if (this[i] == obj)
            return i;

        i++;
    }

    return -1;
}

// Add remove method to Array
Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
}

/*------ ARRAY ------*/