/*

po zmene proved minifikaci

*/


/*
Adam Haken, 2006
tento kod je chranen autorskym zakonem, jakekoli nepovolene pouziti je trestne
*/

var pom_cansubmit=true;

function pomocnik(id_ex,id_q,typ,pocet,autosubmit) {

/* globalni promenne */

	this.id_typ=null;
	this.id_value=null;
	this.t_fc=null;
	this.id_q=null;
	this.id_ex=null;
	this.id_pomocnik=null;
	this.id_helper=null;
	this.p_vstup=0;
	this.i_act=0;
	this.i_pocet=0;
	this.i_max=12;
	this.a_vals=new Array();
	this.a_valid=new Array();
	this.a_valid2=new Array();
	this.a_valid3=new Array();
	this.i_lastq=null;
	this.i_submit=0;
	this.i_last=0;
	this.i_wlast=0;
	this.i_keytime=200;
	this.i_wtime=6000;
	this.autosubmit=autosubmit;


		this.id_ex=id_ex;

		this.id_typ=typ;
		this.id_value=document.getElementById(this.id_ex);

		this.id_q=document.getElementById(id_q);
		this.t_fc='xpom[\''+this.id_typ+'\']';

		setAttr(this.id_q,'autocomplete','off');

		if (IE) {
			setAttr(this.id_q,'onkeypress',this.t_fc+'.check()');
			}
		else {
			setAttr(this.id_q,'onkeyup',this.t_fc+'.check()');
			}

		setAttr(this.id_q,'onfocus',this.t_fc+'.focus()');
		setAttr(this.id_q,'onblur',this.t_fc+'.blur()');

		this.id_pomocnik=document.getElementById('p-'+this.id_ex);
		var tmp1=document.createElement('div');
		tmp1.id='p-'+this.id_ex;
		tmp1.className='pomocnik';
		tmp1.innerHTML='';

		this.id_q.parentNode.insertBefore(tmp1,this.id_q);
//		this.id_q.parentNode.appendChild(tmp1);

		this.id_pomocnik=document.getElementById('p-'+this.id_ex);
		this.id_helper=document.getElementById('helper');
		this.i_max=pocet;

		if (IE) {
			setAttr(this.id_q,'onkeydown',this.t_fc+'.checkkey(event)');
			}
		else {
			setAttr(this.id_q,'onkeypress',this.t_fc+'.checkkey(event)');
			}


/*
		if (document.all) this.id_q.onkeydown=this.checkkey;
		else this.id_q.onkeypress=this.checkkey;

		this.id_q.form.onsubmit=this.cansubmit;
*/


	this.update=function(pole) {

		var tmp;

		this.i_act=0;
		this.p_vstup=pole.length;
		if (this.p_vstup==0) {

			this.wclosenow();
			return 0;
			}

		if (this.p_vstup>this.i_max) this.i_pocet=this.i_max;
		else this.i_pocet=this.p_vstup;

		tmp='<div class="oflow" id="'+this.t_fc+'-pomocnik-oflow">';
		tmp+='<ul>';
		for(var i=0;i<this.i_pocet;i++) {
		
			this.a_vals[i+1]=pole[i][0];
			this.a_valid[i+1]=pole[i][1];
			this.a_valid2[i+1]=pole[i][3];
			this.a_valid3[i+1]=pole[i][4];

			switch (this.id_typ) {
				case "search":
//<span id="'+this.t_fc+'-spomocnik-'+(i+1)+'">'+pole[i][2]+' </span>
					tmp+='<li id="'+this.t_fc+'-pomocnik-'+(i+1)+'" onmouseover="'+this.t_fc+'.itemover('+(i+1)+');" onmouseout="'+this.t_fc+'.itemout('+(i+1)+');" onclick="'+this.t_fc+'.itemclick('+(i+1)+');">'+pole[i][1]+'</li>';

				break;
				case "mesto":

					tmp+='<li id="'+this.t_fc+'-pomocnik-'+(i+1)+'" onmouseover="'+this.t_fc+'.itemover('+(i+1)+');" onmouseout="'+this.t_fc+'.itemout('+(i+1)+');" onclick="'+this.t_fc+'.itemclick('+(i+1)+');"><span id="'+this.t_fc+'-spomocnik-'+(i+1)+'">'+pole[i][3]+' </span><span id="'+this.t_fc+'-spomocnik-'+(i+1)+'">'+pole[i][2]+' </span>'+pole[i][1]+'</li>';

				break;
				default:

					tmp+='<li id="'+this.t_fc+'-pomocnik-'+(i+1)+'" onmouseover="'+this.t_fc+'.itemover('+(i+1)+');" onmouseout="'+this.t_fc+'.itemout('+(i+1)+');" onclick="'+this.t_fc+'.itemclick('+(i+1)+');"><span id="'+this.t_fc+'-spomocnik-'+(i+1)+'">'+pole[i][1]+' </span>'+pole[i][0]+'</li>';
				break;
				}
			}
		tmp+='</ul>';
		tmp+='</div>';

		this.id_pomocnik.innerHTML=tmp;
		this.id_pomocnik.style.display='block';
		}

	this.check=function() {

		if (this.i_lastq!=this.id_q.value) {

			this.i_last++;
			window.setTimeout(this.t_fc+'.loaddata('+this.i_last+')',this.i_keytime);
			}
		}

	this.loaddata=function(last) {

		if (last==this.i_last) {

			this.i_lastq=this.id_q.value;
			var head=document.getElementsByTagName('head').item(0);
			var old=document.getElementById('last_id');
			if (old)head.removeChild(old);
			script=document.createElement('script');
			script.src='/_data/clscripts/pomocnik.php?ex='+this.id_ex+'&typ='+this.id_typ+'&q='+this.id_q.value;
			script.type='text/javascript';
			script.defer=true;
			script.id='last_id'
			void(head.appendChild(script));
			}
		}

	this.wclosenow=function(wlast) {

		this.i_pocet=0;
		this.i_act=0;
		this.a_vals=Array();
		this.id_pomocnik.innerHTML='';
		this.id_pomocnik.style.display='none';
		}

	this.wclose=function(wlast) {

		if (wlast==this.i_wlast) {

			this.wclosenow();
			}
		}

	this.focus=function() {

		switch (this.id_typ) {
			case "zakazka-idklient":

				this.id_q.select();
			
			break;
			}

		pom_cansubmit=false;
		}

	this.blur=function() {

		window.setTimeout(this.t_fc+'.wclosenow()',150);

		window.setTimeout('pom_cansubmit=true',200);
		}

	this.rewrite=function(doscroll) {

		for(var i=0;i<this.i_pocet;i++) {

			if (i+1==this.i_act) {

				try { 
					document.getElementById(''+this.t_fc+'-pomocnik-'+(i+1)).className='hover';
					}
				catch (e) {
					}

				try { 
					document.getElementById(''+this.t_fc+'-spomocnik-'+(i+1)).className='hover';
					}
				catch (e) {
					}
				}
			else {

				try { 
					document.getElementById(''+this.t_fc+'-pomocnik-'+(i+1)).className='';
					}
				catch (e) {
					}
				try { 
					document.getElementById(''+this.t_fc+'-spomocnik-'+(i+1)).className='';
					}
				catch (e) {
					}
				}
			}

		if (doscroll) {

			try {
	
				tmp=document.getElementById(this.t_fc+'-pomocnik-'+this.i_act).offsetTop-80;
				if (tmp<0) tmp=0;
				document.getElementById(''+this.t_fc+'-pomocnik-oflow').scrollTop=tmp;
				}
			catch(e) {
	
				}
			}
	
		this.i_wlast++;
		window.setTimeout(this.t_fc+'.wclose('+this.i_wlast+')',this.i_wtime);
		}

	this.itemover=function(item) {

		this.i_act=item;

		this.rewrite(false);
		}

	this.itemout=function(item) {

/*		this.i_act=0;*/

		this.rewrite(false);
		}

	this.itemclick=function(item) {

		switch (this.id_typ) {

			case "search":

			this.id_value.value=this.a_valid[this.i_act];

			if (this.autosubmit==1)
				this.id_value.form.submit();

			break;
			case "mesto":

			document.getElementById('item-idmesto').value=this.a_vals[this.i_act];
			document.getElementById('item-mesto').value=this.a_valid[this.i_act];
			document.getElementById('item-psc').value=this.a_valid2[this.i_act];
			document.getElementById('item-okres').value=this.a_valid3[this.i_act];

			document.getElementById('item-email').focus();

			break;
			default:

			this.id_q.value=this.a_vals[this.i_act];
			this.id_value.value=this.a_valid[this.i_act];
		
			break;
			}



/*
		this.i_submit=1;
		this.id_q.form.submit();
*/
		}

	this.cansubmit=function() {

		return false;

		if (i_act==0) return true;
		if (i_submit==0) return false;
		}

	this.checkkey=function(e) {

		if(!e) if (window.event) e=window.event;

		var key;
		if (typeof(e.which)=='number') key=e.which;
		else if (typeof(e.keyCode)=='number') key=e.keyCode;
		else if (typeof(e.charCode)=='number') key=e.charCode;
   	else return;

		key=e.keyCode;

		switch(key) {
			case 13:

				if (this.i_act>0) {
/*
					this.id_q.value=this.a_vals[this.i_act];
*/

					switch (this.id_typ) {
			
						case "search":
			
						this.id_value.value=this.a_valid[this.i_act];
			
						if (this.autosubmit==1)
							this.id_value.form.submit();
			
						break;
						case "mesto":
						
						document.getElementById('item-idmesto').value=this.a_vals[this.i_act];
						document.getElementById('item-mesto').value=this.a_valid[this.i_act];
						document.getElementById('item-psc').value=this.a_valid2[this.i_act];
						document.getElementById('item-okres').value=this.a_valid3[this.i_act];
						
						document.getElementById('item-email').focus();

						break;
						default:
			
						this.id_q.value=this.a_vals[this.i_act];
			
					
						break;
						}
		
					try {

						if (pomocnikdotab)
							xxopt_dotab(this.id_q);
						else
							this.id_q.blur();
						}
					catch(e) {

						}
					}
				else {

					this.id_q.blur();
					document.getElementById('item-okres').focus();
					}

/*
				this.i_submit=1;
				this.id_q.form.submit();
*/
			break
			case 38:

				if (this.i_act>1) this.i_act--;
				this.rewrite(true);

			break
			case 40:
		
				if (this.i_act<this.i_pocet) this.i_act++;
				this.rewrite(true);

			break
			}

		}

	}

var xpom=new Array();

