function intval (mixed_var, base) {
    // Get the integer value of a variable using the optional base for the conversion  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/intval
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30
    var tmp;

    var type = typeof( mixed_var );

    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}
(function($) {
		  
	$.fn.select_autocomplete = function(options) {
		
		// make sure we have an options object
		options = options || {};
		
		// setup our defaults
		var defaults = { 
			  minChars: 1
			, width: 310
			, matchContains: false
			, autoFill: false
			, formatItem: function(row, i, max) {
				return row.name;
			}
			, formatMatch: function(row, i, max) {
				return row.name;
			}
			, formatResult: function(row) {
				return row.name;
			}
		};
		
		options = $.extend(defaults, options);
		
		return this.each(function() {
		
			//stick each of it's options in to an items array of objects with name and value attributes 
			var $this = $(this),
				data = [],
				$input = $('<input type="text" id="my_gorod"/>');
			
			if (this.tagName.toLowerCase() != 'select') { return; }
				
			
			$this.children('option').each(function() {
		
				var $option = $(this);
				
				if ($option.val() != '') { //ignore empty value options

					data.push({
						  name: $option.html()
						, value:$option.val()
					});
				}
			});
			
			// insert the input after the select
			$this.after($input);
			
			// add it our data
			options.data = data;
		
			//make the input box into an autocomplete for the select items
			$input.autocomplete(data, options);
		
			//make the result handler set the selected item in the select list
			$input.result(function(event, selected_item, formatted) { 
				$($this.find('option[value=' + selected_item.value + ']')[0]).attr('selected', true);
				var groupID = $($this.find('option[value=' + selected_item.value + ']')[0]).attr('groupID');
				//alert(intval(groupID));
				switch (intval(groupID)) {
				    case 1: 
				      document.getElementById('mkad').style.display='none';
				      document.getElementById('moskva').style.display='none';
				      document.getElementById('fulladdress').style.display='none';
				      document.getElementById('rossia').style.display='block';
				    break;
				    case 2: 
				      document.getElementById('moskva').style.display='none';
				      document.getElementById('rossia').style.display='none';
				      document.getElementById('mkad').style.display='block';
				      document.getElementById('fulladdress').style.display='block';
				    break;
				    case 3: 
				      document.getElementById('mkad').style.display='none';
				      document.getElementById('rossia').style.display='none';
				      document.getElementById('moskva').style.display='block';
				      document.getElementById('fulladdress').style.display='block';
				    break;
				}
				//cityGroup(intval(document.getElementById("cityname").options[document.getElementById("cityname").selectedIndex].getAttribute(&#39;groupID&#39;)));
			});

      $input.blur(function(){
          // autocomplete has removed blank options
          // ensure that if value is removed we select the blank option if available
          if(this.value == ""){
            $($this.find('option[value=]')[0]).attr('selected', true);
          }

          /*   failsafe to ensure text box always represents the value being used in the select
           *   there are edge cases where if you leave the field part way through the word, or clear the value when no blank option is available
           *   that force a mismatch between the 2 elements
           */
          if(this.value != $this[0].options[$this[0].selectedIndex].text){
            $input.val($this[0].options[$this[0].selectedIndex].text);
          }  
      });
		
			//set the initial text value of the autocomplete input box to the text node of the selected item in the select control
			$input.val($this[0].options[$this[0].selectedIndex].text);
		
			//normally, you'd hide the select list but we won't for this demo
			$this.hide();
		});
	};		  
  
})(jQuery);
