var str = "";

function start()
{
	document.getElementById("search").focus();
}

function myslnik(wartosc)
{
	var vStr = "";
	for (var i=0; i<wartosc.length; i++) 
		if(wartosc.charAt(i) != '-') vStr += wartosc.charAt(i);
	wartosc = vStr;
	if(wartosc.length >= 2) 
		wartosc = wartosc.substring(0, 2) + '-' + wartosc.substring(2, wartosc.length);
	return wartosc;
}

function sprawdzpole()
{
	var kod = document.getElementById("search").value;

	if (kod.match('[0-9]+')) 
	{
		document.getElementById("search").maxLength = 6;
		if (kod.length >= 0 && kod.length <= 6) 
		{
			if(kod.match('^[0-9]{2}-[0-9]{3}$'))
			{
				// ok
			}
			else
			{
				var temp = kod;
				if (kod.length > str.length && str != null && str != "") temp = myslnik(kod);
				else str = kod;
				document.getElementById("search").value = temp;
				str = temp;
				document.getElementById("blad").innerHTML = '';
			}
		}
		else
			//document.getElementById("blad").innerHTML = 'Poprawny format kodu: np. <b>71-210</b>';
			document.getElementById("blad").innerHTML = '';
	}
	else
		document.getElementById("search").maxLength = 50;
}



Ext.onReady(function(){

	Ext.BLANK_IMAGE_URL = './ext-2.0.2/resources/images/default/s.gif';

    var ds = new Ext.data.Store({

				proxy: new Ext.data.HttpProxy({
					url: 'index.php?id=91&action=dane',
					method : 'POST'
			}),

			reader: new Ext.data.JsonReader({
					root: 'topics',
					fields: [
			            {name: 'miejsc'},
            			{name: 'nazwa'}
					]
			})
    });

    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="search-item">',
            '<h6>{miejsc}</h6> <span>woj. {nazwa}</span>',
        '</div></tpl>'
    );
    
    var search = new Ext.form.ComboBox({
			store: ds,
			displayField:'miejsc',
			loadingText: 'szukam...',
			width: 233,
			height:35,
			minHeight: 90,
			maxHeight: 300,
			minChars: 2,
			selectOnFocus: true,
			hideTrigger:true,
			tpl: resultTpl,
			applyTo: 'search',
			itemSelector: 'div.search-item'
    });
});


