﻿var isIE = (document.all) ? true : false;

function mouseXY(e) {
    var X = (isIE ? e.clientX : e.pageX);
    var Y = (isIE ? e.clientY : e.pageY);
    return { Left: X, Top: Y };
}

function elementXY(e) {
    var left = 0;
    var top = 0;
    var width = e.offsetWidth;
    var height = e.offsetHeight;
    while (e.offsetParent) {
        left += e.offsetLeft;
        top += e.offsetTop;
        e = e.offsetParent;
    }
    return { Left: left, Top: top, Right: left + width, Bottom: top + height };
}

var oldObj = null;
function tabSelected(obj, validationGroup) {
    var validationResult = true;
    if (typeof (Page_ClientValidate) == 'function' && validationGroup) {
        validationResult = Page_ClientValidate(validationGroup);
    }

    if (validationResult) {
        if (oldObj) oldObj.className = "";
        oldObj = obj;
        obj.className = "selected";
    }
}
function nextTabSelected(validationGroup) {
    tabSelected(oldObj.nextSibling.nextSibling, validationGroup);
}

function isUnderElement(_this, e) {
    e = (e) ? e : window.Event;
    var mXY = mouseXY(e);
    var onKlik = false;

    var obj = _this.childNodes;
    var objlen = obj.length;
    for (var i = 0; i < objlen; i++) {

        if (obj[i].onclick || obj[i].type || obj[i].href) {
            var nXY = elementXY(obj[i]);
            if (mXY.Left > nXY.Left && mXY.Left < nXY.Right && mXY.Top > nXY.Top && mXY.Top < nXY.Bottom)
                return true;
            else if (isUnderElement(obj[i], e))
                return true;
        }
        else if (isUnderElement(obj[i], e))
            return true;
    }
    return false;
}

function Expand(_this) {
    return Expand(_this, -1, null);
}

function Expand(_this, _id, obj) {
    var tr = _this.parentNode.parentNode.parentNode;
    var trs = document.getElementsByTagName("tr");
    var trslen = trs.length;
    var gir = false;
    var expand = (_this.src.indexOf("arrow_state_blue_right") > -1);
    var tml = parseInt(_this.style.marginLeft) + 14;
    for (var i = 0; i < trslen; i++) {
        if (trs[i] == tr) gir = true;
        if (gir && trs[i] != tr) {
            var _ok;
            if (trs[i].childNodes[1].childNodes[1])
                _ok = trs[i].childNodes[1].childNodes[1].childNodes[0];
            else
                _ok = trs[i].childNodes[0].childNodes[0].childNodes[0];
            var ml = parseInt(_ok.style.marginLeft);
            if (ml == tml || (ml > tml && !expand)) {
                trs[i].style.display = (expand) ? "" : "none";
                _this.src = (expand) ? "/Visual/Image/Icon_Set_16x16/arrow_state_blue_expanded.png" : "/Visual/Image/Icon_Set_16x16/arrow_state_blue_right.png";
                if (!expand) _ok.src = "/Visual/Image/Icon_Set_16x16/arrow_state_blue_right.png";
            } else if (ml < tml)
                break;
        }
    }

    if (obj != null && _id > -1) {
        if (!expand)
            obj.value = obj.value.replace(_id.toString() + ";", "");
        else
            obj.value += _id.toString() + ";";
    }

    return false;
}

function ShowPopup(url, width, height) {
	var _window = $get('PopupWindow');
	var _background = $get('PopupBackGround');
    var _frame = $get('PopupFrame');

    _background.style.display = '';
	_window.style.width = width + 'px';
    _window.style.height = height + 'px';
    _window.style.marginLeft = '-' + (width / 2) + 'px';
    _window.style.marginTop = '-' + (height / 2) + 'px';
    _window.style.display = '';
    _frame.style.display = '';
    _frame.src = url;

    return false;
}

function HidePopup() {
    var _window = $get('PopupWindow');
    var _background = $get('PopupBackGround');
    var _frame = $get('PopupFrame');
    var _message = $get('Message');

    if (frameShowing && window.frames["PopupFrame"].document.body.innerHTML != "") {
        frameShowing = false;
        _window.style.width = oldWidth + 'px';
        _window.style.height = oldHeight + 'px';
        _window.style.marginLeft = '-' + (oldWidth / 2) + 'px';
        _window.style.marginTop = '-' + (oldHeight / 2) + 'px';
        _message.style.display = 'none';
        _frame.style.display = '';
    }
    else {
        _background.style.display = 'none';
        _window.style.display = 'none';
        _message.style.display = 'none';
        _frame.style.display = 'none';
        _frame.src = '';
    }

    return false;
}

var frameShowing = false;
var oldHeight = 0;
var oldWidth = 0;
function ShowErrorMessage(message, width, kod) {
    var _window = $get('PopupWindow');
    var _background = $get('PopupBackGround');
    var _frame = $get('PopupFrame');
    var _message = $get('Message');

    if (_window.style.display != 'none') {
        frameShowing = true;
        oldHeight = parseInt(_window.style.height);
        oldWidth = parseInt(_window.style.width);
    }

    $get('Message').innerHTML = message + "<br /><br /><center><a href=\"#\" class=\"button\" onclick=\"return HidePopup();\">Tamam</a></center>";
    _frame.style.display = 'none';
    _message.style.display = '';
    _background.style.display = '';
    _window.style.height = 'auto';
    _window.style.width = width;
    var ofWidth = _window.offsetWidth;
    _window.style.marginLeft = '-' + (ofWidth / 2) + 'px';
    _window.style.marginTop = '-' + (_window.offsetHeight / 2) + 'px';
    _window.style.display = '';

    return false;
}

function pageFullHeight() {
    var obj = $get('menu');
    var posMenu = elementXY(obj);

    var maxHeight;
    if (typeof (window.innerWidth) == 'number')
        maxHeight = window.innerHeight - 45; //Non-IE
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        maxHeight = document.documentElement.clientHeight - 45; //IE 6+ in 'standards compliant mode'
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        maxHeight = document.body.clientHeight - 45; //IE 4 compatible

    document.body.style.height = maxHeight - 10 + "px";
    //document.getElementsByTagName("form")[0].style.height = maxHeight - 10 + "px";
}

function trim(str) {
    var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    var strlen = str.length;
    for (var i = 0; i < strlen; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    for (i = strlen - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function isDate(date) {
    var mydate, myresult;
    var mydate = new Date(date);
    isNaN(mydate) ? myresult = false : myresult = true;
    return myresult;
}

function isNumber(sayi) {
    var myresult;
    isNaN(Number(sayi)) ? myresult = false : myresult = true;
    return myresult;
}

function isString(input) {
    return typeof (input) == 'string' && isNaN(input);
}

function getInnerText(elt) {
    var _innerText = elt.innerText;
    if (_innerText == undefined) {
        _innerText = elt.innerHTML.replace(/<[^>]+>/g, "");
    }
    return _innerText;
}

function columnFilter(funnel, parent, dataset_id, column) {
    var dataset = document.getElementById(dataset_id);
    if (!dataset) return false; //dataset yok ise çık.
    if (document.getElementById('Filter')) parent.removeChild(document.getElementById('Filter'));
    var Filter = Filter_onInitialize(funnel, parent);
    var tumu = addFilterItem(Filter.FilterList, "(Tümünü Seç)"); // Tümü

    var tr = dataset.getElementsByTagName('tr');
    var c = true;
    var sortItem = new Array();
    var trlen = tr.length;
    var len = 0;
    for (var i = 0; i < trlen; i++) {
        var td = tr[i].getElementsByTagName('td');
        var tdlen = td.length;
        if (tdlen - 1 < column) return; //column sayısı yok ise çık.
        c = true;
        if (len > 0) {
            for (var j = 0; j < len; j++) {
                if (sortItem[j].Text.toLowerCase() == getInnerText(td[column]).toLowerCase()) { c = false; break; } // aynı nesneden var ise çık.
            }
        }
        if (c) {
            for (var k = 0; k < tdlen; k++) {
                if (k != column && td[k].style.display == 'none') {
                    c = false;
                    break;
                }
            }
            if (c) {
                sortItem.push({ Text: getInnerText(td[column]), Display: tr[i].style.display });
                len++;
            }
        }
    }
    sortItem.sort(function(a, b) {
        var x = a.Text.toLowerCase();
        var y = b.Text.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
    c = true;
    len = sortItem.length;
    for (var i = 0; i < len; i++) {
        var cb = addFilterItem(Filter.FilterList, sortItem[i].Text);
        if (cb) {
            cb.checked = (sortItem[i].Display != 'none')
            c = c & cb.checked;
        }
    }
    tumu.checked = c;
    var FindText = function() {
        var _type = Filter.FilterType[Filter.FilterType.selectedIndex].value;
        if (_type == '') return;
        var cb = Filter.FilterList.getElementsByTagName('input');
        var tt = function(t) {
            if (isDate(Filter.FilterText.value))
                return new Date(t);
            else if (isNumber(Filter.FilterText.value))
                return Number(t);
            else
                return t.toString().toLowerCase();
        };
        var result;
        var findText = tt(Filter.FilterText.value);
        var findLen = Filter.FilterText.value.length;
        for (var i = 0; i < len; i++) {
            result = false;
            if (_type == '<<') result = (sortItem[i].Text.toLowerCase().indexOf(findText.toLowerCase()) == 0);
            else if (_type == '><') result = (sortItem[i].Text.toLowerCase().indexOf(findText.toLowerCase()) > -1);
            else if (_type == '>>') result = (sortItem[i].Text.toLowerCase().indexOf(findText.toLowerCase()) == sortItem[i].Text.length - findLen);
            else if (_type == '=') result = (tt(sortItem[i].Text) == findText);
            else if (_type == '!=') result = (tt(sortItem[i].Text) != findText);
            else if (_type == '>') result = (tt(sortItem[i].Text) > findText);
            else if (_type == '>=') result = (tt(sortItem[i].Text) >= findText);
            else if (_type == '<=') result = (tt(sortItem[i].Text) <= findText);
            cb[i + 1].checked = result;
        }
        cb[1].onclick();
    };
    Filter.OkButton.onclick = function() {
        if (trim(Filter.FilterText.value) != '') FindText();
        //var tr = dataset.getElementsByTagName('tr');
        var cb = Filter.FilterList.getElementsByTagName('input');
        var cblen = cb.length;
        var c = true;
        for (var i = 0; i < trlen; i++) {
            var td = tr[i].getElementsByTagName('td');
            tdlen = td.length;
            c = true;
            for (var j = 1; j < cblen; j++) {
                if (cb[j].value == getInnerText(td[column])) {
                    if (cb[j].checked) {
                        c = true;
                        for (var k = 0; k < tdlen; k++) {
                            if (k != column && td[k].style.display == 'none') {
                                td[column].style.display = '';
                                c = false;
                                break;
                            }
                        }
                        if (c) {
                            tr[i].style.display = '';
                            td[column].style.display = '';
                        }
                    } else {
                        tr[i].style.display = 'none';
                        td[column].style.display = 'none';
                    }
                    c = false;
                    break;
                }
            }
            if (c) {
                for (var k = 0; k < tdlen; k++)
                    td[column].style.display = 'none';
            }
        }
        funnel.style.backgroundImage = "url('/Visual/Image/Icon_Set_16x16/" + ((cb[0].checked) ? "funnel.png" : "funnel-plus.png") + "')";
        parent.removeChild(Filter.Filter);
        return false;
    };
    Filter.Filter.focus();
    document.body.onclick = function() {
    var e = document.activeElement;
        if (!e || e == funnel ) return;
        var a = false;
        while (e.parentNode) {
            if (e == Filter.Filter) {
                a = true;
                break;
            }
            e = e.parentNode;
        }
        if (!a) {
            if (Filter.Filter) {
                try {
                    parent.removeChild(Filter.Filter);
                }
                catch (err) {
                    document.removeChild(document.getElementById('Filter'));
                }
            }
            document.body.onclick = null;
        }
    };
    return false;
}

function addFilterItem(parent, text) {
    var cb = parent.getElementsByTagName('input')
    var cblen = cb.length;
    var item = document.createElement('input');
    item.id = 'cb' + cblen;
    item.type = 'checkbox';
    item.value = text;
    item.onclick = function() {
        var c = true;
        var cblen = cb.length;
        for (var i = 1; i < cblen; i++) {
            c = c && cb[i].checked;
            if (item == cb[0]) cb[i].checked = item.checked;
        }
        if (item != cb[0]) cb[0].checked = c;
    };
    parent.appendChild(item);
    var itemLbl = document.createElement('label');
    itemLbl.innerHTML = (trim(text) == '') ? '(Boş Olanlar)' : text;
    itemLbl.htmlFor = item.id;
    itemLbl.style.width = 'auto';
    parent.appendChild(itemLbl);
    parent.appendChild(document.createElement('br'));
    return item;
}

function Filter_onInitialize(funnel,parent) {
    var _Filter = document.createElement('div');
    _Filter.id = 'Filter';
    _Filter.style.position = 'absolute';
    _Filter.className = 'popup';
    _Filter.style.zIndex = '10012';
    _Filter.style.padding = '8px';
    _Filter.style.width = '150px';
    _Filter.style.height = '390px';
    var ms = elementXY(funnel);
    _Filter.style.left = ((ms.Left - 150 < 0) ? 0 : ms.Left - 150) + 'px';
    var maxHeight;
    if (typeof (window.innerHeight) == 'number')
        maxHeight = window.innerHeight - 45; //Non-IE
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        maxHeight = document.documentElement.clientHeight - 45; //IE 6+ in 'standards compliant mode'
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
        maxHeight = document.body.clientHeight - 45; //IE 4 compatible
    _Filter.style.top = ((ms.Top + 390 > maxHeight) ? maxHeight - 390 : ms.Top + funnel.offsetHeight) + 'px';
    parent.appendChild(_Filter);
    var _FilterType = document.createElement('select');
    _FilterType.id = 'FilterType';
    _FilterType.style.width = '100%';
    _Filter.appendChild(_FilterType);
    var _op = document.createElement('option');
    _op.value = '';
    _op.innerHTML = 'Seçmeli Filtreleme';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '';
    _op.innerHTML = '------------------------------';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '=';
    _op.innerHTML = 'Eşittir';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '!=';
    _op.innerHTML = 'Eşit Değildir';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '>';
    _op.innerHTML = 'Büyüktür';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '>=';
    _op.innerHTML = 'Büyük Eşittir';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '<';
    _op.innerHTML = 'Küçütür';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '<=';
    _op.innerHTML = 'Küçük Eşittir';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '<<';
    _op.innerHTML = 'ile Başlar';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '><';
    _op.innerHTML = 'İçerir';
    _FilterType.appendChild(_op);
    _op = document.createElement('option');
    _op.value = '>>';
    _op.innerHTML = 'ile Biter';
    _FilterType.appendChild(_op);
    var _FilterText = document.createElement('input');
    _FilterText.id = 'FilterText';
    _FilterText.type = 'text';
    _FilterText.style.width = '144px';
    _FilterText.style.display = 'none';
    _Filter.appendChild(_FilterText);
    var _FilterList = document.createElement('div');
    _FilterList.id = 'FilterList';
    _FilterList.style.backgroundColor = 'White';
    _FilterList.style.border = 'solid 1px #aaaaaa';
    _FilterList.style.height = '338px';
    _FilterList.style.fontSize = '8pt';
    _FilterList.style.overflow = 'auto';
    _Filter.appendChild(_FilterList);
    var _OkButton = document.createElement('a');
    _OkButton.className = 'button';
    _OkButton.style.width = '45px';
    _OkButton.style.margin = '5px';
    _OkButton.innerHTML = 'Uygula';
    _OkButton.href = '#';
    _Filter.appendChild(_OkButton);
    var _CancelButton = document.createElement('a');
    _CancelButton.className = 'button';
    _CancelButton.style.width = '45px';
    _CancelButton.style.margin = '5px';
    _CancelButton.innerHTML = 'İptal';
    _CancelButton.href = '#';
    _CancelButton.onclick = function() { parent.removeChild(_Filter); return false; };
    _Filter.appendChild(_CancelButton);

    _FilterType.onchange = function() {
        if (_FilterType[_FilterType.selectedIndex].value == '') {
            _FilterText.style.display = 'none';
            _FilterList.style.height = '338px';
            _FilterList.style.backgoundColor = 'White';
            _FilterList.disabled = false;
        }
        else {
            _FilterText.style.display = '';
            _FilterList.style.height = '315px';
            _FilterList.style.backgoundColor = '#BBBBBB';
            _FilterList.disabled = true;
            _FilterText.focus();
        }
    }

    return { Filter: _Filter, FilterList: _FilterList, OkButton: _OkButton, CancelButton: _CancelButton, FilterType: _FilterType, FilterText: _FilterText };
}
