﻿var rowCount = -1;

var hdnValue;
var hdnValue1;
var hdnValue2;
var hdnValue3;
var hdnValue4;
var hdnValue5;
var hdnValue6;
var hdnValue7;
var hdnValue8;
var hdnValue9;

var hdnEmail1;
var hdnEmail2;
var hdnEmail3;
var keyValue = new Array();
function ajaxFunction() {
    var xmlHttp = null
    try { xmlHttp = new XMLHttpRequest(); }
    catch (e) {
        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
            try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e) {
                alert("Your browser does not support AJAX!")
                return false
            }
        }
    } return xmlHttp
}

//selecting whole text
function selecttxt(obj) {
    obj.select()
}

function noenter(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode == 13)
        return false
}

function chkkey(e, objtxtinput) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    if (unicode == 38 || unicode == 95 || unicode == 37 || unicode == 42 || unicode == 64 || unicode == 35 || unicode == 36 || unicode == 33 || unicode == 94 || unicode == 91 || unicode == 93) {
        return false;
    }
    else
        return true;
}

function FormatStringTel(searchStr) {
    while (searchStr.substring(0, 1) == ' ') {
        searchStr = searchStr.substring(1, searchStr.length)
    }
    while (searchStr.substring(searchStr.length - 1, searchStr.length) == ' ') {
        searchStr = searchStr.substring(0, searchStr.length - 1)
    }
    if (searchStr.indexOf("'") > -1)
        searchStr = searchStr.replace("'", "`")
    return searchStr
}

function hideDivSel(flag) {    
    divsel = 'divSel'+flag
    objdivsel = document.getElementById(divsel)
    objdivsel.style.display = 'none';
}
//set position of div
function SetPos(objtxtinput, flag, img) {
    divsel = 'divSel'+flag
    objdivsel = document.getElementById(divsel)
    var obj = objtxtinput
    var curleft = curtop = 0    
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
        curtop = curtop + 20
        //if (img != null && img != "undefined") {
            curleft = curleft - 10
        //}
    }
    var dfs = objdivsel.style
    dfs.left = curleft +"px"
    dfs.top = curtop + "px"
    if (document.getElementById('tblDropdown' + flag)) {
        mytable = document.getElementById('tblDropdown' + flag)
        dfs.height = mytable.height
        countRow = mytable.rows.length
        if (countRow > 6) {
            dfs.height = "150px"
            divHeight = 150
        }
        else {
            if (countRow == 1) {
                dfs.height = "40px"
            }
        }
    }
}
//No records found Display
function norecords(str, check, searchStr, flag, objtxtinput) {
    strTable = ""
    strRow = ""
    var arrstrData = str
    var arrstrRow = arrstrData
    strId = arrstrRow
    strRow = "<tr class='unselected'><td align='center' style='font-size:11px;font-weight:bold;'>" + strId + "</td></tr>"
    strTable = "<table class='acborder' style='width:260px;background:#f2f3f5;color:black;' cellspacing='0' cellpadding='4' onmouseout=\"return doSomething(event);\">" + strRow + "</table>"
    divHeight = 100

    if (flag == 'Field1') {
        document.getElementById(hdnValue).value = null;        
    }
    if (flag == 'Field2') {
        document.getElementById(hdnValue1).value = null;
    }
    if (flag == 'Field3') {
        document.getElementById(hdnValue2).value = null;
    }
    if (flag == 'Field4') {
        document.getElementById(hdnValue3).value = null;
    }
    return strTable
}

//Actual function to display data in div
function showDivSel(e, flag, page, objtxtinput, valobjtxtinput, mintxtlength, what, varbool, isNRDisplay,img) {
    var unicode = e.charCode ? e.charCode : e.keyCode
    hdnid = 'hdnId' + flag
    tbldropdown = 'tblDropdown' + flag
    divsel = 'divSel'+flag
    objdivsel = document.getElementById(divsel)
    var Counter = parseInt(rowCount)
    if ((((unicode >= 48 && unicode <= 57) || (unicode >= 65 && unicode <= 90) || (unicode >= 96 && unicode <= 105) || (unicode >= 106 && unicode <= 111) || (unicode >= 187 && unicode <= 192) || unicode == 8 || unicode == 32 || unicode == 46 || unicode == 220 || unicode == 222) && unicode != 13) || varbool) {
        document.getElementById(hdnid).value = ""
        searchStr = FormatStringTel(valobjtxtinput)
        strlength = searchStr.length
        rowCount = -1
        strItem = ""
        if (strlength >= mintxtlength) {
            objdivsel.innerHTML = ""
            kvFlag = false
            for (kvCount = 0; kvCount < keyValue.length; kvCount++) {
                var arraykv = keyValue[kvCount].split("~")
                if (arraykv[0].toUpperCase().indexOf(searchStr.toUpperCase()) == 0 && arraykv[0].length == searchStr.length && arraykv[2] == what) {
                    kvFlag = true
                    objdivsel.innerHTML = arraykv[1]
                    objdivsel.style.display = ""
                    SetPos(objtxtinput, flag, img)
                    objdivsel.scrollTop = 0
                }
            }
            if (!kvFlag) {
                requestUrl = page + "?inputstr=" + searchStr + "&strFlag=" + what + "&no=" + Math.random();
                var xmlHttp1 = new ajaxFunction()
                xmlHttp1.onreadystatechange = function() {
                    if (xmlHttp1.readyState == 4) {
                        var str = xmlHttp1.responseText
                        if (str == "") {
                            if (isNRDisplay) {
                                objdivsel.style.display = ""
                                strData = norecords("No Records Found", "No", "", flag, objtxtinput)
                                objdivsel.innerHTML = strData
                                objdivsel.scrollTop = 0
                                SetPos(objtxtinput, flag, img)
                            }
                        }
                        if (str != "") {
                            var strChk = str.split("#$$#");
                            if (strChk[1] == "0")
                                strData = zero(str, searchStr, flag, objtxtinput, img)
                            //                                else
                            //                                    strData = one(str, searchStr, flag, objtxtinput)
                            /// 0 for more and prev display........

                            if (strData != "") {
                                objdivsel.style.display = ""
                                replaceHtml(objdivsel, strData);
                                //objdivsel.innerHTML = strData
                                objdivsel.scrollTop = 0
                                SetPos(objtxtinput, flag, img)
                            }
                        }
                    }
                }
                xmlHttp1.open("GET", requestUrl, true)
                xmlHttp1.send(null)

            }
        }
        else {
            strblank = ""
            keyValue.length = 0
            objdivsel.innerHTML = strblank
            objdivsel.style.display = "none"
        }
    }
    else if (unicode == 13) {
        if (strItem != "") {
            additem(strItem, flag, objtxtinput.id,img);
        }
    }
    else if (unicode == 40) {
        if (document.getElementById(tbldropdown)) {
            mytable = document.getElementById(tbldropdown)
            countRow = mytable.rows.length
            if (Counter < countRow - 1) {
                Counter = Counter + 1
                rowCount = Counter
                for (i = 0; i < countRow; i++) {
                    if (i == Counter) {
                        myrow = mytable.getElementsByTagName("tr")[i]
                        myrow.className = "selected"
                        strItem = myrow.id
                        objdivsel.scrollTop = Counter * 19
                    }
                    else {
                        myrow = mytable.getElementsByTagName("tr")[i]
                        myrow.className = "unselected"
                    }
                }
            }
            else if (Counter >= countRow) {
                myrow = mytable.getElementsByTagName("tr")[countRow]
                myrow.className = "selected"
            }
        }
    }
    else if (unicode == 38) {
        if (document.getElementById(tbldropdown)) {
            mytable = document.getElementById(tbldropdown)
            countRow = mytable.rows.length
            if (Counter > 0) {
                Counter = Counter - 1
                rowCount = Counter
                for (i = 0; i < countRow; i++) {
                    if (i == Counter) {
                        myrow = mytable.getElementsByTagName("tr")[i]
                        myrow.className = "selected"
                        strItem = myrow.id
                        objdivsel.scrollTop = Counter * 19
                    }
                    else {
                        myrow = mytable.getElementsByTagName("tr")[i]
                        myrow.className = "unselected"
                    }
                }
            }
            else if (Counter <= 0) {
                myrow = mytable.getElementsByTagName("tr")[0]
                myrow.className = "selected"
            }
        }
    }
    else {        
        objdivsel.innerHTML = ""
        objdivsel.style.display = "none"
        if (unicode == 27) {
            objtxtinput.value = ""
            document.getElementById(hdnid).value = ""
        }
    }
}

function replaceHtml(el, html) {
    var oldEl = typeof el === "string" ? document.getElementById(el) : el;
    /* @cc_on // Pure innerHTML is slightly faster in IE
    oldEl.innerHTML = html;
    return oldEl;
    @*/
    var newEl = oldEl.cloneNode(false);
    newEl.innerHTML = html;
    oldEl.parentNode.replaceChild(newEl, oldEl);
    /* Since we just removed the old element from the DOM, return a reference
    to the new element, which can be used to restore variable references. */
    return newEl;
};

function one(str, searchStr, flag, objtxtinput) {
    strTable = ""
    strRow = ""
    i = 0
    arrstrData = str.split("#R#");
    while (i < arrstrData.length) {
        var arrstrRow = arrstrData[i].split("#C#")
        strhiddenId = arrstrRow[0];
        strName = arrstrRow[1].toUpperCase().replace(searchStr.toUpperCase(), "<b>" + searchStr.toUpperCase() + "</b>")
        strId = arrstrRow[0] + ":" + arrstrRow[1] + ":" + arrstrRow[2]
        strRow = strRow + "<tr class='unselected' id='" + strId + "' style='cursor:pointer' OnClick=\"javascript:additem('" + strId + "','" + flag + "','" + objtxtinput.id + "');\" OnMouseOver=\"javascript:applyClass('" + strId + "','" + flag + "');\"><td class='datacol2'>" + strName + "</td></tr>"
        i++
    }
    strRow = strRow + "<tr class='unselected'><td colspan='3' align='left' style='color:blue;text-decoration:underline;height:25px;padding-left:5px;font-size:11px;'><span style='cursor:pointer' OnClick=\"javascript:showDivSel(event,'" + flag + "','../AutoCompleteCode/BackPage.aspx',document.getElementById('" + objtxtinput.id + "'),'" + searchStr + "',3,'0',true,true);\">previous</span></td></tr>"
    strTable = "<table id='tblDropdown" + flag + "' class='acborder' cellspacing='0' cellpadding='0' onmouseout=\"return doSomething(event);\">" + strRow + "</table>"
    if (arrstrData.length == 1) {
        var arrstrRow = arrstrData[0].split(":")
        document.getElementById(hdnid).value = arrstrRow[0] + "|" + arrstrRow[1]
    }
    //    else {
    //        document.getElementById(hdnid).value = ""
    //        keyValue[keyValue.length] = searchStr + "~" + strTable + "~1"
    //    }
    return strTable
}

function zero(str, searchStr, flag, objtxtinput, img) {    
    strTable = ""
    strRow = ""
    i = 0
    var arrstrData = str.split("#R#")
    while (i < arrstrData.length) {
        var arrstrRow = arrstrData[i].split("#C#")
        strhiddenId = arrstrRow[0];
        strName = arrstrRow[1].toUpperCase().replace(searchStr.toUpperCase(), "<b>" + searchStr.toUpperCase() + "</b>")
        strId = arrstrRow[0] + ":" + arrstrRow[1] + ":" + arrstrRow[2]
        strRow = strRow + "<tr class='unselected' id='" + strId + "' style='cursor:pointer' OnClick=\"javascript:additem('" + strId + "','" + flag + "','" + objtxtinput.id + "','" + img + "');\" OnMouseOver=\"javascript:applyClass('" + strId + "','" + flag + "');\"><td class='datacol2' style='border-bottom:0px;'>" + strName + "</td></tr>"
        i++
    }
    //strRow = strRow + "<tr class='unselected'><td colspan='3' align='left' style='color:blue;text-decoration:underline;height:25px;padding-left:5px;font-size:11px;'><span style='cursor:pointer' OnClick=\"javascript:showDivSel(event,'" + flag + "','../AutoCompleteCode/BackPage.aspx',document.getElementById('" + objtxtinput.id + "'),'" + searchStr + "',3,'1',true);\">more</span></td></tr>"
    strTable = "<table id='tblDropdown" + flag + "' class='acborder' cellspacing='0' cellpadding='0' onmouseout=\"return doSomething(event);\">" + strRow + "</table>"
    if (arrstrData.length == 1) {
        var arrstrRow = arrstrData[0].split(":")
        document.getElementById(hdnid).value = arrstrRow[0] + "|" + arrstrRow[1]
    }
    return strTable
}
function additem(name, flag, txtinput,img) {    
    hdnid = 'hdnId' + flag
    divsel = 'divSel'+flag
    objdivsel = document.getElementById(divsel)
    var arrayItem = name.split(":")
    document.getElementById(txtinput).value = arrayItem[1]
    document.getElementById(hdnid).value = arrayItem[0] + "|" + arrayItem[1]
    if (flag == 'Field1') {
        document.getElementById(hdnValue).value = arrayItem[0];
        if (hdnEmail1 != null) {
            document.getElementById(hdnEmail1).value = arrayItem[2];
        }
    }
    else if (flag == 'Field2') {
    document.getElementById(hdnValue1).value = arrayItem[0];
    if (img != null && img != "undefined") {
        document.getElementById(img).click();
    }
    
    
    if (hdnEmail2 != null) {
        document.getElementById(hdnEmail2).value = arrayItem[2];
    }
    }
    else if (flag == 'Field3') {
    document.getElementById(hdnValue2).value = arrayItem[0];
    if (hdnEmail3 != null) {
        document.getElementById(hdnEmail3).value = arrayItem[2];
    }
    }
    else if (flag == 'Field4') {
        document.getElementById(hdnValue3).value = arrayItem[0];
    }

    else if (flag == 'Field5') {
        document.getElementById(hdnValue4).value = arrayItem[0];
    }
    else if (flag == 'Field6') {
        document.getElementById(hdnValue5).value = arrayItem[0];
    }
    else if (flag == 'Field7') {
        document.getElementById(hdnValue6).value = arrayItem[0];
    }
    else if (flag == 'Field8') {
        document.getElementById(hdnValue7).value = arrayItem[0];
    }
    else if (flag == 'Field9') {
        document.getElementById(hdnValue8).value = arrayItem[0];
    }
    else if (flag == 'Field10') {
        document.getElementById(hdnValue9).value = arrayItem[0];
    }
    strItem = ""
    objdivsel.style.display = "none"
    rowCount = -1
    document.getElementById(txtinput).focus()
    //document.getElementById(txtinput).select()    
    //    if (txtinput == "txtscrip")
    //        DispStockReach("hdnIdAsset")
}


function applyClass(id, flag) {
    tbldropdown = 'tblDropdown' + flag
    mytable = document.getElementById(tbldropdown)
    countRow = mytable.rows.length
    for (i = 0; i < countRow; i++) {
        myrow = mytable.getElementsByTagName("tr")[i]
        if (document.getElementById(id).id == myrow.id)
            rowCount = i
        if (myrow.className == "selected")
            myrow.className = "unselected"
    }
    document.getElementById(id).className = "selected"
}


function doSomething(e) {
    try {        
        divshow = 'divshow'
        objdivsel = document.getElementById('divSelAsset')
        objdivsel1 = document.getElementById('divSelWLAsset')
        if (!e)
            var e = window.event
        if (e.type == 'mouseout') {
            document.getElementById(divshow).value = "hide"
        }
        else if (e.type != 'click') {
            objdivsel.style.display = ''
            objdivsel1.style.display = ''
            document.getElementById(divshow).value = "show"
        }
        if (e.type == 'click') {
            if (document.getElementById(divshow).value == "hide" || document.getElementById(divshow).value == "") {
                objdivsel.style.display = 'none'
                objdivsel1.style.display = 'none'
            }
        }
    }
    catch (err) { }
}
