﻿function _AcOpenPopupSelect(div, evt) {
    var inpText = div.getElementsByTagName('input')[0];
    var popup = getNextSibling(div);
    if (popup.style.display == 'block') {
        _AcShowHidePopup(popup, false);
        if (evt) {
            evt.cancelBubble = true;
            evt.returnValue = false;
        }
    } else {
        _AcShowHidePopup(popup, true);
        if (inpText.onfocus)
            inpText.onfocus();
        inpText.select();
        inpText.onkeyup();
    }
}

function _AcOpenPopup(div, evt, show) {
    var popup = getNextSibling(div);
    if (typeof (show) == 'undefined')
        show = (popup.style.display == 'none');

    _AcShowHidePopup(popup, show);
    if (evt) {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
}

var z_ix = 150000;

function _AcShowHidePopup(popup, show) {
    var parent = popup.parentNode;
    if (window.currentPopup
    && popup != window.currentPopup)
        window.currentPopup.style.display = 'none';

    popup.style.zIndex = (show ? ++z_ix : popup._zIndex);
    // IE 7- Hack
    if (typeof (popup.block) == 'undefined') {
        for (var p = parent, i = 0; i < 5, null != p; i++, p = p.parentNode) {
            if (/(\s|^)block(\s|$)/.test(p.className)) {
                popup.block = p;
                popup.block._zIndex = popup.block.style.zIndex;
                popup.block.style.zIndex = z_ix;
                break;
            }
        }
    } else {
        popup.block.style.zIndex = (show ? z_ix : popup.block._zIndex);
    }
    popup.style.display = (show ? 'block' : 'none');
    if (typeof (popup._zIndex) == 'undefined')
        popup._zIndex = parent.style.zIndex;
    if (show) {
        parent.removeChild(popup);
        parent.appendChild(popup)
    }
    window.currentPopup = (show ? popup : null);
}

function _AcPopupSelectRow(a, evt) {
    var popup = a.parentNode.parentNode;
    popup.style.display = 'none';
    var div = getPreviousSibling(popup);
    var inpText = div.getElementsByTagName('input')[0];
    var inpValue = div.getElementsByTagName('input')[1];
    inpText.value = a.innerText;
    inpValue.value = a.getAttribute('value');
    div.value = inpValue.value;
    var div = popup.parentNode.parentNode;
    if (typeof (div.onchange) != 'function') {
        var fn = 'fn_' + a.id + ' = function (evt){'
                + 'if (typeof(event) == "undefined") event = evt;'
                + div.getAttribute('onchange')
            + '}';
        eval('function aaa(){}')
        div.onchange = eval(fn);
    }
    div.onchange(evt);
    inpText.onchange(evt);
    inpText.setAttribute('selectedValue', inpText.value);
    if (evt) {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
}

var _AcId = 0;

var bb = false;
function _AcAddRow(ac, text, value) {
    var itemsBox = document.getElementById(ac.getAttribute('itemsBox'));
    var item = document.createElement('a');
    item.setAttribute('id', ++_AcId);
    //item.setAttribute('onclick', 'javascript:_AcPopupSelectRow(this, event)');
    item.onmousedown = function(evt) { _AcPopupSelectRow(this, evt); };
    //item.setAttribute('href', '#');
    item.setAttribute('value', value);
    item.innerHTML = text;
    itemsBox.appendChild(item);
}

function _AcClearRows(ac) {
    var staticOptionsCount = ac.getAttribute('staticOptionsCount');
    var itemsBox = document.getElementById(ac.getAttribute('itemsBox'));
    var items = itemsBox.childNodes;

    for (var i = items.length - 1; i > staticOptionsCount; i--) {
        itemsBox.removeChild(items[i]);
    }
}

function _AcHintTextFocus(inp, evt) {
    if (inp.getAttribute('hintText')) {
        if (inp.getAttribute('hintText') == inp.value) {
            inp.value = '';
            inp.style.color = '';
        }
    }
}

function _AcHintTextBlur(inp, evt) {
    if (inp.getAttribute('hintText')) {
        if (!inp.value) {
            inp.value = inp.getAttribute('hintText');
            inp.style.color = '#CECECE';
        } else {
            inp.style.color = '';
        }
    }
}

function _AcHintTextChange(inp, evt) {
    var hint = inp.getAttribute('hintText');
    if (hint) {
        if (inp.getAttribute('hintText') == inp.value) {
            return;
        }
    }
    inp.style.color = '';
}
