﻿var Agora_AutoComplete_Collection = new Array();
DataFloat.Agora.Util.AutoComplete = function(FieldId, FieldKey, RecordType, ServiceAutoComplete, MinChar, Width, Where, ClickFnc){
    $AgoraLog.WriteLog('[DataFloat.Agora.Util.AutoComplete] Field: ' + FieldId + ' - Initializing');
    ServiceAutoComplete = (DataFloat.Agora.Util.IsEmptyOrNull(ServiceAutoComplete)) ? DataFloat.Agora.Services.ServiceAutoComplete : ServiceAutoComplete ;
    MinChar = DataFloat.Agora.Util.IsEmptyOrNull(MinChar) ? 2 : MinChar;
    Width = DataFloat.Agora.Util.IsEmptyOrNull(Width) ? 0 : Width;
    //This function is ready to extend
    
    
    eval("var Params = {'RecordType' : '" + RecordType + "', 'Where' : '" + ((Where == undefined)? '' : Where) + "', 'FieldKey' : '" + FieldKey + "', 'Agora__ItemPerPage' : '1000'}");

    if ($("#" + FieldId).autocomplete != undefined){
        Agora_AutoComplete_Collection['AutoComplete_' + RecordType + '_' + FieldKey] = $("#" + FieldId).autocomplete(
		    ServiceAutoComplete,
		    {
			    delay:10,
			    minChars:MinChar,
			    matchSubset:1,
			    matchContains:1,
			    cacheLength:10,
			    autoFill:false,
			    maxItemsToShow: 10,
			    formatItem:DataFloat.Agora.Util.AutoComplete.FormatItem,
			    onItemSelect:ClickFnc,
			    queryStringField: 'Value',
			    extraParams : Params,
			    width : Width,
			    multiple : true
		    }
	    );
	    
	    if ($("#" + FieldId).result != null){
	        $("#" + FieldId).result(ClickFnc);
	    }

	    
	    $AgoraLog.WriteLog('[DataFloat.Agora.Util.AutoComplete] Field: ' + FieldId + ' - Completed');
	}else{
	    $AgoraLog.WriteLog('[DataFloat.Agora.Util.AutoComplete] Field: ' + FieldId + ' - Error to setup the AutoComplete');
	}
	
	

};


DataFloat.Agora.Util.AutoComplete.FormatItem = function(row) {
	return row[0];
}

DataFloat.Agora.Util.AutoComplete.ClickItem = function(row) {
    //alert(row.selectValue);
	return row[0];
}



