﻿function setPDocumentOptions(){
    $("#"+txtParentDocument.id).autocomplete("GetData.aspx",
        {
            delay: 30,
            minChars: 3,
            onItemSelect: selectPDocItem,
            formatItem: formatPDocItem,
            autoFill: true,
            mustMatch:true,
            multipleSeparator:"| ",
            extraParams:{
                  0:"d",
                  1: function() { return (ddlLawsArea.selectedIndex>=0?ddlLawsArea.options[ddlLawsArea.selectedIndex].value:0); },
                  2: function() { return (ddlParentType.selectedIndex>=0?ddlParentType.options[ddlParentType.selectedIndex].value:0) },
                  3: function() { return (ddlPSection.selectedIndex>=0?ddlPSection.options[ddlPSection.selectedIndex].value:0) }
            },
            maxItemsToShow: 20
        }
    );
    
    $("#"+txtParentDocument.id).blur(function(){
         //$("#"+txtParentDocument.id).search();
         var docObj = $("#"+txtParentDocument.id);
         var docIDObj =$("#"+txtParentDocumentID.id);
         if(docObj.val()=="") docIDObj.val("");
         if(docIDObj.val() =="") docObj.val("");
         if(typeof txtDocumentdDate != "undefined") if(docIDObj.val() =="") $("#"+txtDocumentdDate.id).val("");
    });
    
    $("#"+txtParentDocument.id).result(function(e, item) {
        if(item){ 
            $("#"+txtParentDocumentID.id).val(item[1]);  
            $("#"+txtParentDocumentNo.id).val(item[2]);
            if(typeof txtDocumentdDate != "undefined") if($("#"+txtDocumentdDate.id).length >0) $("#"+txtDocumentdDate.id).val(item[3]);
        }else { $("#"+txtParentDocumentID.id).val(""); }
    });
}

function findValue(li) {
    if (li == null) return alert("No match!");
    if (!!li.extra) var sValue = li.extra[0];
    else var sValue = li.selectValue;
    return sValue; 
}

function selectPDocItem(li) {
    $("#"+txtParentDocumentID.id).val(findValue(li));
}

function formatPDocItem(row) {
    return row[0] + " (No: " + row[2] + ")";
}

function selectionChange(){
    $("#"+txtParentDocument.id).flushCache();
    $("#"+txtParentDocument.id).val("");
    $("#"+txtParentDocumentID.id).val("");
}

function isDefined(obj){
    return (obj===undefined);
}