(function($){
    $.fn.checkAvailabilityUg = function(opts) {
        opts = $.extend({
            target: '#responseUg',
			trigger: '#btnCheck',
			ajaxSource: 'moduli/contractCheck.php',
			fireOnKeypress: true
        }, opts || {});
		
		var $this = $(this);
		
		if (opts.fireOnKeypress) {
			$this.keyup(function() {
				checkContract();
			});
			
			$this.keypress(function(event) {
				if (event.keyCode == 13) {
					event.preventDefault();
					return false;
				}
			});
		};
		
		$(opts.trigger).click(function() {
			checkContract();
        });
		
		function checkContract() {
				$(opts.target).html('<img src="slike/loading.gif"><span class="mali_crveni_tekst">checking availability...</span>');
				contractLookup(); 	
		};
		
		function contractLookup() { 
			var val = $this.val();
			$.ajax({ 
					url: opts.ajaxSource, 
					data: {fn:val,s:Math.random()},
					success: function(html){
						$(opts.target).html(html);
					},
					error:function (){
						$(opts.target).html('<span class="mali_crveni_tekst">Check script not found</span>');
					}
			}); 
		};
	};
})(jQuery);
