﻿var NPComboSelected = -1;
function InitNPComboBox(table) {
    for (var i = 0; i < table.rows.length; i++) {
        table.rows[i].onmouseover = function() {
            var tt = this.parentNode;
            if (NPComboSelected == -1) {
                this.getElementsByTagName("td")[0].className = 'NPComboBoxItemHover';
                NPComboSelected = this.rowIndex;
            }
            else {
                tt.rows[NPComboSelected].getElementsByTagName("td")[0].className = 'NPComboBoxItem';
                this.getElementsByTagName("td")[0].className = 'NPComboBoxItemHover';
                NPComboSelected = this.rowIndex;
            }
        }
        table.rows[i].onclick = function() {
            this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.rows[0].getElementsByTagName('input')[0].value = GetInnerText(this.getElementsByTagName('TD')[0]);
            this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.rows[0].getElementsByTagName('input')[1].value = this.getElementsByTagName('TD')[0].attributes["ComboValue"].value;
            this.parentNode.parentNode.parentNode.style.display = 'none';
            try {
                this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.rows[0].getElementsByTagName('input')[1].Change();
            } catch (err) { };
        }
        table.rows[i].getElementsByTagName("td")[0].className = 'NPComboBoxItem';
    }
    table.parentNode.parentNode.parentNode.parentNode.rows[0].getElementsByTagName('input')[0].onkeydown = NPComboBox_KeyDown;
    table.parentNode.parentNode.parentNode.parentNode.rows[0].getElementsByTagName('input')[0].onkeyup = NPComboBox_Click;
}

document.onmousedown = function() {
    var objs = document.getElementsByTagName('div');
    for (var i = 0; i < objs.length; i++)
        if (objs[i].id.indexOf('divNPComboBox_') != -1 && objs[i].IsOver == '0') {
        objs[i].style.display = 'none';
    }
}

function GetInnerText(obj) {
    var res = "";
    if (navigator.appVersion.indexOf('MSIE') != -1) {
        return ReplaceKY(obj.innerText);
    }
    return ReplaceKY(obj.textContent);
}

function NPComboBox_KeyDown(evt) {
    var e = evt ? evt : window.event;
    if (!e) return;
    var key = 0;
    if (e.keyCode) { key = e.keyCode; }
    else if (typeof (e.which) != 'undefined') { key = e.which; }
    var tt = this.parentNode.parentNode.parentNode.rows[1].getElementsByTagName('table')[0];
    var objDiv = tt.parentNode;
    var j = -1;
    var CV = new Array();
    if (key == 40 || key == 38) {
        var c = 0;
        for (var i = 0; i < tt.rows.length; i++) {
            if (tt.rows[i].style.display != 'none') {
                CV[c] = i;
                if (tt.rows[i].getElementsByTagName("td")[0].className == 'NPComboBoxItemHover')
                    j = c;
                c++;
            }
        }
    }
    if (key == 40) {
        j++;
        if (j == CV.length)
            j--;

        if (NPComboSelected == -1) {
            tt.rows[CV[j]].getElementsByTagName("td")[0].className = 'NPComboBoxItemHover';
            NPComboSelected = tt.rows[CV[j]].rowIndex;
        }
        else {
            tt.rows[NPComboSelected].getElementsByTagName("td")[0].className = 'NPComboBoxItem';
            tt.rows[CV[j]].getElementsByTagName("td")[0].className = 'NPComboBoxItemHover';
            NPComboSelected = tt.rows[CV[j]].rowIndex;
        }

        var jj = objDiv.scrollTop / (objDiv.scrollHeight / (CV.length));
        if ((j - 8) > jj)
            objDiv.scrollTop = objDiv.scrollHeight / (CV.length) * (j - 8);
    }
    else if (key == 38) {
        j--;
        if (j == -1)
            j = 0;
        if (NPComboSelected == -1) {
            tt.rows[CV[j]].getElementsByTagName("td")[0].className = 'NPComboBoxItemHover';
            NPComboSelected = tt.rows[CV[j]].rowIndex;
        }
        else {
            tt.rows[NPComboSelected].getElementsByTagName("td")[0].className = 'NPComboBoxItem';
            tt.rows[CV[j]].getElementsByTagName("td")[0].className = 'NPComboBoxItemHover';
            NPComboSelected = tt.rows[CV[j]].rowIndex;
        }
        var jj = objDiv.scrollTop / (objDiv.scrollHeight / (CV.length));
        if (j < jj)
            objDiv.scrollTop = objDiv.scrollHeight / (CV.length) * j;
    }
    else if (key == 13) {
        var j = -1;
        for (var i = 0; i < tt.rows.length; i++)
            if (tt.rows[i].getElementsByTagName("td")[0].className == 'NPComboBoxItemHover') {
            j = i;
            break;
        }
        if (j != -1) {
            this.value = GetInnerText(tt.rows[j].getElementsByTagName('td')[0])
            this.parentNode.getElementsByTagName("input")[1].value = tt.rows[j].getElementsByTagName('td')[0].attributes["ComboValue"].value;
        }
        else
            this.parentNode.getElementsByTagName("input")[1].value = "-1";
        tt.parentNode.style.display = 'none';
        try {
            this.parentNode.getElementsByTagName("input")[1].Change();
        } catch (err) { };
        return false;
    }
    if (this.IsDigit == "1") {
        if (e.shiftKey == true || (((key < 48 || key > 57) && (key < 96 || key > 105)) && key != 8 && key != 37 && key != 38 && key != 40 && key != 13 && e.ctrlKey == false && key != 39 && key != 46 && key != 9))
            return false;
    }
}

function NPComboBox_Focus(e) {
    e.onkeyup();
}
function NPComboBox_Click(evt) {
    var e = evt ? evt : window.event;
    if (e != null) {
        var key = 0;
        if (e.keyCode) { key = e.keyCode; }
        else if (typeof (e.which) != 'undefined') { key = e.which; }
        if (key == 13 || key == 40 || key == 38)
            return false;
    }
    var divObj = this.parentNode.parentNode.parentNode.rows[1].getElementsByTagName('div')[0];
    var table = this.parentNode.parentNode.parentNode.rows[1].getElementsByTagName('Table')[0];
    divObj.style.display = 'block';
    table.style.borderTop = '1px solid #D6D9DE';
    var o = 0;
    var swVal = false;
    for (var i = 0; i < table.rows.length; i++) {
        if (GetInnerText(table.rows[i].getElementsByTagName('td')[0]).indexOf(ReplaceKY(this.value)) != -1) {
            table.rows[i].getElementsByTagName('td')[0].innerHTML = GetInnerText(table.rows[i].getElementsByTagName('td')[0]).replace(ReplaceKY(this.value), "<span style='color:red;'>" + ReplaceKY(this.value) + "</span>");
            table.rows[i].style.display = 'block';
            o++;
        }
        else {
            if (i == 0)
                table.style.borderTop = '1px solid #FFFFFF';
            table.rows[i].style.display = 'none';
        }
        if (GetInnerText(table.rows[i].getElementsByTagName('td')[0]) == ReplaceKY(this.value)) {
            swVal = true;
            this.parentNode.getElementsByTagName("input")[1].value = table.rows[i].getElementsByTagName('td')[0].attributes["ComboValue"].value;
        }
        table.rows[i].getElementsByTagName("td")[0].className = 'NPComboBoxItem';
    }
    NPComboSelected = -1;
    if (!swVal)
        this.parentNode.getElementsByTagName("input")[1].value = "-1";

    if (o == 0) {
        divObj.style.display = 'none';
    }
    else if (o > 9) {
        divObj.style.height = divObj.attributes["ComboDivHeight"].value;
        table.style.width = parseInt(divObj.style.width.toString().replace('px', ''), 10) - 19;
        divObj.style.overflow = 'auto';
    }
    else {
        divObj.style.height = "";
        table.style.width = parseInt(divObj.style.width.toString().replace('px', ''), 10);
        divObj.style.overflow = 'hidden';
    }
    if (navigator.appVersion.indexOf('MSIE') == -1)
        table.style.width = '100%';
    try {
        this.parentNode.getElementsByTagName("input")[1].Change();
    } catch (err) { };
}
function TextBoxChange(e) {
    var t = '';
    for (var i = 0; i < e.value.length; i++) {
        if (e.value.charCodeAt(i) >= 48 && e.value.charCodeAt(i) <= 57) {
            t += e.value.charAt(i).toString();
        }
    }
}
function ReplaceKY(str) {
    var re;
    re = /(ی)/g;
    str = str.replace(re, "ي");
    re = /(ک)/g;
    return str.replace(re, "ك");
}
