
(function($){
    $.fn.extend(
    {
        simpleAutoComplete: function( page, options, callback )
        {
            if(typeof(page) == "undefined" )
            {
                alert("simpleAutoComplete: Error.");
            }
			
            var classAC = 'autocomplete';
            var selClass = 'sel';
            var attrCB = 'rel';
            var thisElement = $(this);
            var insertEl = thisElement;
            var minLength = 3;
            var extraParam = '';
            if( typeof( options.extraParamFromInput ) != "undefined" ) {
                extraParam = options.extraParamFromInput.split(',');
            }

            $(":not(div." + classAC + ")").click(function(){
                $("div." + classAC).remove();
            });
			
            thisElement.attr("autocomplete","off");
			
            thisElement.keyup(function( ev )
            {
                var getOptions = {
                    query: thisElement.val()
                }
				
                if( typeof(options) == "object" )
                {
                    classAC = typeof( options.autoCompleteClassName ) != "undefined" ? options.autoCompleteClassName : classAC;
                    selClass = typeof( options.selectedClassName ) != "undefined" ? options.selectedClassName : selClass;
                    minLength = typeof( options.minLength ) != "undefined" ? options.minLength : minLength;
                    insertEl = typeof( options.insert ) != "undefined" ? $('#' + options.insert) : thisElement;
					
                    attrCB = typeof( options.attrCallBack ) != "undefined" ? options.attrCallBack : attrCB;
                    if( typeof( options.identifier ) == "string" )
                        getOptions.identifier = options.identifier;

                    if( extraParam != '' ) {
                        $.each(extraParam, function() {
                            val = $('#' + this).val();
                            getOptions[this] = val;
                        })
                        
                    }
                }

                kc = ( ( typeof( ev.charCode ) == 'undefined' || ev.charCode === 0 ) ? ev.keyCode : ev.charCode );
                key = String.fromCharCode(kc);

                //console.log(kc, key, ev );
                if (kc) {
                    $('#location_message').css({
                        'display': 'none'
                    });
                }

                if (kc == 27)
                {
                    $('div.' + classAC).remove();
                }
                if (kc == 13)
                {
                    $('div.' + classAC + ' li.' + selClass).trigger('click');
                }
                if (key.match(/[a-zA-Z0-9_\- ]/) || kc == 8 || kc == 46)
                {
                    if (thisElement.attr('value').length >= minLength) {
                        $.get(page, getOptions, function(r)
                        {
                            $('div.' + classAC).remove();
                            autoCompleteList = $('<div>').addClass(classAC).html(r);
                            if (r != '')
                            {
                                autoCompleteList.insertAfter(insertEl);
							
                                var position = thisElement.position();
                                var height = thisElement.height();
                                var width = thisElement.width();

                                $('div.' + classAC).css({
                                    'top': ( height + position.top + 6 ) + 'px',
                                    'left': ( position.left )+'px',
                                    'margin': '0px'
                                });
							
                                $('div.' + classAC + ' ul').css({
                                    'margin-left': '0px'
                                });
							
                                $('div.' + classAC + ' li').each(function( n, el )
                                {
                                    el = $(el);
                                    el.mouseenter(function(){
                                        $('div.' + classAC + ' li.' + selClass).removeClass(selClass);
                                        $(this).addClass(selClass);
                                    });
                                    el.click(function()
                                    {
                                        thisElement.attr('value', el.text());

                                        if( typeof( callback ) == "function" )
                                            callback( el.attr(attrCB).split('_') );
									
                                        $('div.' + classAC).remove();
                                        thisElement.focus();
                                    });
                                });
                            } else {
                                $('#location_message').css({
                                    'display': 'block'
                                });
                            }
                        });
                    }
                }
                if (kc == 38 || kc == 40){
                    if ($('div.' + classAC + ' li.' + selClass).length == 0)
                    {
                        if (kc == 38)
                        {
                            $($('div.' + classAC + ' li')[$('div.' + classAC + ' li').length - 1]).addClass(selClass);
                        } else {
                            $($('div.' + classAC + ' li')[0]).addClass(selClass);
                        }
                    }
                    else
                    {
                        sel = false;
                        $('div.' + classAC + ' li').each(function(n, el)
                        {
                            el = $(el);
                            if ( !sel && el.hasClass(selClass) )
                            {
                                el.removeClass(selClass);
                                $($('div.' + classAC + ' li')[(kc == 38 ? (n - 1) : (n + 1))]).addClass(selClass);
                                sel = true;
                            }
                        });
                    }
                }
                if (thisElement.val() == '')
                {
                    $('div.' + classAC).remove();
                }
            });
        }
    });
})(jQuery);



function viewAutoComplete() {
    jQuery('#origin_autocomplete').focus();
    jQuery('#origin_autocomplete').simpleAutoComplete(__basePath + 'ajax/autoCompleteOrigin/'+jQuery("#cboCountry").val()+'/',{
        autoCompleteClassName: 'autocomplete',
        selectedClassName: 'sel',
        attrCallBack: 'rel',
        identifier: 'originName',
        minLength: 3
    }, estadoCallback);

}

function estadoCallback( par ) {
    enableCmdGoFind();
    jQuery("#uf1").val( par[0] );
}


function viewAutoCompleteOriginProductInquery() {
    jQuery('#origin').focus();
    jQuery('#origin').simpleAutoComplete(__basePath + 'ajax/autoCompleteOrigin/'+jQuery("#cboCountry").val()+'/',{
        autoCompleteClassName: 'autocomplete',
        selectedClassName: 'sel',
        attrCallBack: 'rel',
        identifier: 'originName',
        minLength: 3
    }, originCallback);

}

function originCallback( par ) {
    jQuery("#originid").val( par[0] );
}


function viewAutoCompleteDestinationProductInquery() {

    jQuery('#txtDestinationName').simpleAutoComplete(__basePath + 'ajax/autoCompleteOrigin/'+jQuery("#cboCountry").val()+'/',{
        autoCompleteClassName: 'autocomplete',
        selectedClassName: 'sel',
        attrCallBack: 'rel',
        identifier: 'originName'
    },destinationCallBack);

}

function destinationCallBack( par ) {

    jQuery("#txtDestinationID").val( par[0] );
}

