function wstawZnak(text) { 
	var obj = document.getElementById('szukaj');
	
	if(document.selection) { 
		obj.focus(); 
		/*
		var orig = obj.value.replace(/\r\n/g, "\n"); 
		var range = document.body.createTextRange();
				
		if(range.parentElement() != obj) { 
			return false; 
		}
		
		range.text = text; 
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n"); 
		
		var actual = tmp = orig + text; 
		for(var diff = 0; diff < orig.length; diff++) { 
			if(orig.charAt(diff) != actual.charAt(diff)) break; 
		} 
		for(var index = 0, start = 0;  
			            tmp.match(text)  
		                && (tmp = tmp.replace(text, ""))  
		                && index <= diff;  
			            index = start + text.length 
		) { 
			start = actual.indexOf(text, index); 
		}		
		*/
		
		start = obj.value.length;
		obj.value += text;
		 
	} else if(obj.selectionStart) { 
		var start = obj.selectionStart; 
		var end   = obj.selectionEnd; 
			 
		obj.value = obj.value.substr(0, start) + text + obj.value.substr(end, obj.value.length); 
	} 
	
	if(start != null) { 
		setCaretTo(obj, start + text.length); 
	} else { 
	    obj.value += text;	    
	}
} 

function setCaretTo(obj, pos) { 
	if(obj.createTextRange) { 
		var range = obj.createTextRange(); 
		range.move('character', pos); 
		range.select(); 
	} else if(obj.selectionStart) { 
		obj.focus(); 
		obj.setSelectionRange(pos, pos); 
	} 
} 

function rysujZnaki(lang, reload){
	if (reload){	
		var url = 'http://slowniki.wp.pl/';
		switch (lang){
			case 'enpl':
			case 'plen':
				url += 'angielski/slowa.html?lt=3&lt2=3d';
				break;
			case 'depl':
			case 'plde':
				url += 'niemiecki/slowa.html?lt=A&lt2=Aa';
				break;
			case 'frpl':
			case 'plfr':
				url += 'francuski/slowa.html?lt=A&lt2=Aa';
				break;
			case 'espl':
			case 'ples':
				url += 'hiszpanski/slowa.html?lt=A&lt2=Ab';
				break;
			case 'itpl':
			case 'plit':
				url += 'wloski/slowa.html?lt=A&lt2=Ab';
				break;
			case 'rupl':
			case 'plru':
				url += 'rosyjski/slowa.html?lt=%D0%90&lt2=%D0%90%D0%B1';
				break;
		}
		
		window.location.href = url;
	}

	var znaki = document.getElementById('znaki');
	var szukaj = document.getElementById('szukaj');
	var znakiTbl;
	var html = '';
	
	if (znaki){
		if ('depl' == lang){
			znakiTbl = new Array('&#228;', '&#246;', '&#252;', '&#223;');
		} else if ('frpl' == lang){
			znakiTbl = new Array('&#224;', '&#226;', '&#230;', '&#339;', '&#231;', '&#232;', '&#233;', '&#234;', '&#235;', '&#238;', '&#239;', '&#244;', '&#249;', '&#251;', '&#252;', '&#255;');
		} else if ('espl' == lang){
			znakiTbl = new Array('&#227;', '&#241;');
		} else if ('rupl' == lang){
			znakiTbl = new Array('&#1072;', '&#1073;', '&#1074;', '&#1075;', '&#1076;', '&#1077;', '&#1105;', '&#1078;', '&#1079;', '&#1080;', '&#1081;', '&#1082;', '&#1083;', '&#1084;', '&#1085;', '&#1086;', '&#1087;', '&#1088;', '&#1089;', '&#1090;', '&#1091;', '&#1092;', '&#1093;', '&#1094;', '&#1095;', '&#1096;', '&#1097;', '&#1098;', '&#1099;', '&#1100;', '&#1101;', '&#1102;', '&#1103;');
		} else if ('enpl' == lang || 'itpl' == lang){
			znakiTbl = null;
			html = '';
		} else {
			znakiTbl = new Array('&#261;', '&#263;', '&#281;', '&#322;','&#324;', '&#347;','&#378;', '&#380;', '&#243;');
		}

		if (znakiTbl){
			html = '	<ul class="letter-list cb" style="margin: 0px;">';
			for (var idx in znakiTbl){
				if (idx > 0 && 0 == idx % 17){
					html += '<br clear="left" />';
				}
				html += '		<li><span onclick="wstawZnak(\'' + znakiTbl[idx] + '\');">' + znakiTbl[idx] + '</span></li>';				
			}
			html += '	</ul>';
		}
		
		znaki.innerHTML = html;			
	}
	
	if (!reload && szukaj){
		szukaj.focus(); 
	}
}

function zamienJezyki(reload){
	var szukaj      = document.getElementById('szukaj');
	var lang_from   = document.getElementById('lang_from');
	var lang_to     = document.getElementById('lang_to');
	var czyPL       = document.search_form1.czyPL.value;
	var lang_select = document.getElementById('lang_select');
	var sIndex      = 0;
	var lang        = 'plen';
	var innerHTML   = '';

	if (lang_select){
		sIndex = lang_select.selectedIndex;		
	}

	if (1 == czyPL){
		innerHTML = '<select id="lang_select" name="lang" style="margin-left:0px; margin-right:0px; width:110px; margin-bottom:3px;" onchange="rysujZnaki(this.options[this.selectedIndex].value, ' + ((!reload) ? 'false' : 'true') + ');"><option value="enpl"';
		innerHTML += (0 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'angielski</option><option value="depl"';
		innerHTML += (1 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'niemiecki</option><option value="frpl"';
		innerHTML += (2 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'francuski</option><option value="itpl"';
		innerHTML += (3 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'włoski</option><option value="espl"';
		innerHTML += (4 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'hiszpański</option><option value="rupl"';
		innerHTML += (5 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'rosyjski</option></select>';
		lang_from.innerHTML = innerHTML;
		lang_to.innerHTML   = 'polski';
		document.search_form1.czyPL.value = 0;

		switch (sIndex){
			case 0:
				lang = 'enpl';
				break
			case 1:
				lang = 'depl';
				break;
			case 2:
				lang = 'frpl';
				break;
			case 3: 
				lang = 'itpl';
				break;
			case 4:
				lang = 'espl';
				break;
			case 5:
				lang = 'rupl';
				break;
			default:
				lang = 'enpl';
		}
	} else {
		lang_from.innerHTML = 'polski';
		innerHTML   = '<select id="lang_select" name="lang" style="margin-left:0px; margin-right:0px; width:110px; margin-bottom:3px;" onchange="rysujZnaki(this.options[this.selectedIndex].value, ' + ((!reload) ? ' false' : 'true') + ');"><option value="plen"';
		innerHTML += (0 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'angielski</option><option value="plde"';
		innerHTML += (1 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'niemiecki</option><option value="plfr"';
		innerHTML += (2 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'francuski</option><option value="plit"';
		innerHTML += (3 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'włoski</option><option value="ples"';
		innerHTML += (4 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'hiszpański</option><option value="plru"';
		innerHTML += (5 == sIndex) ? ' selected="selected">' : '>';
		innerHTML += 'rosyjski</option></select>';
		lang_to.innerHTML = innerHTML;
		document.search_form1.czyPL.value = 1;		

		switch (sIndex){
			case 0:
				lang = 'plen';
				break
			case 1:
				lang = 'plde';
				break;
			case 2:
				lang = 'plfr';
				break;
			case 3: 
				lang = 'plit';
				break;
			case 4:
				lang = 'ples';
				break;
			case 5:
				lang = 'plru';
				break;
			default:
				lang = 'plen';
		}
	}	

	rysujZnaki(lang, false);

	if (szukaj){
		szukaj.select();
		document.search_form1.submit();
	}
}

