// JavaScript Document

function SetAutoComplete(IObj, SubDiv, DefaultText, AjaxFunction, CustomDraw, CustomExecute, ItemHeight, DropCount)
{  
   if (IObj) {
   IObj.BoxFocused=false;
    IObj.BoxArray=new Array();
    IObj.BoxH=0;
	IObj.BoxTimeOut=0;
	IObj.BoxIndex=-1;
	IObj.BoxDiv=getObj(SubDiv);
	IObj.setAttribute('autocomplete', 'off');
	IObj.ItemHeight=ItemHeight;
	IObj.DropCount=DropCount;
    // Focus
    IObj.onfocus=function()
				{ this.BoxFocused=true;
				  if (this.value==DefaultText) this.value=''; }
	// Blur
	IObj.onblur=function()
				{  this.BoxFocused=false; 
				   if ((IObj.BoxDiv.Focused)&&(IObj.BoxDiv.Sel.Focused)==false)
				   {this.BoxH=setTimeout('getObj("'+this.id+'")'+'.BoxHide();',100);}
				   clearTimeout(this.BoxTimeOut); 
				   if (this.value=='') this.value=DefaultText; }		   
    // OnKeydown
	IObj.onkeydown=function(e)
					{  clearTimeout(this.BoxTimeOut);
					   var keynum;
					   if(window.event) {keynum = e.keyCode} else if(e.which) {keynum = e.which}	
								switch (keynum) { 
								  case 13:
								       this.BoxExecute();
								  	   break;
								  case 38:
									   if (this.BoxArray.length>0) { if (this.BoxIndex>0)  {this.BoxIndex--; this.BoxDraw(); }} 
									   break;
								  case 40:
									   if (this.BoxArray.length>0) {
										   if (this.BoxIndex<this.BoxArray.length-1)  {this.BoxIndex++; this.BoxDraw(); }}
									   break;	
								  default:	   
									clearTimeout(this.BoxTimeOut);
									//this.BoxTimeout=setTimeout('getObj("'+this.id+'")'+'.BoxAjax();',1000);
									if (AjaxFunction)
									{this.BoxTimeout=setTimeout(AjaxFunction+'();',1000)};
									break;
							} 
					}

	//BoxLoad
	IObj.BoxLoad=function (Content) {if (this.BoxFocused) { this.BoxArray=Content.split('<->');
											  this.BoxIndex=0;
											  this.BoxDraw();}}
					
							  
	//BoxHide
	IObj.BoxHide= function() { this.BoxDiv.style.display='none'; 
							   clearTimeout(this.BoxH); }
							   
	//BoxDraw
	IObj.BoxDraw=function() { if (CustomDraw) {CustomDraw(this);}}
	//BoxExecute
	IObj.BoxExecute=function (){ if (CustomExecute) CustomExecute(this); }
   }
} // end function

///********* Custom Function For SearchBox on page

   function SearchAjax(){
	  IObj=getObj('search_box'); 
	  if (IObj.BoxFocused) { var val=IObj.value; 
								  if (val!='') { IObj.BoxDiv.style.display='none'; 
												IObj.BoxH=setTimeout('getObj("search_box").BoxHide();',100);
												makePOSTRequest('/pretraga-ime/'+val,'',CatchSearchAjax);} 
												}}
												
   function CatchSearchAjax() { IObj=getObj('search_box');
								if (IObj.BoxFocused) { 
									if (http_request.readyState == 4) {  if (http_request.status == 200){	 
										IObj.BoxLoad(http_request.responseText); }else{}}else{} // http technical errors.
								}}
   function SearchDraw(IObj)
   { var mout='';
				var n,l;
				var mtext='';
				
				if (IObj.BoxArray.length>0) {
					
					if (IObj.BoxDiv.Sel) { IObj.BoxDiv.removeChild(IObj.BoxDiv.Sel); }
					var Sel=document.createElement('select');
					Sel.setAttribute('size',IObj.DropCount);
					Sel.onfocus=function() {  this.Focused=true; }
					Sel.onblur=function() {  this.Focused=false; if (this.Obj.BoxFocused==false) {
						this.Obj.BoxH=setTimeout('getObj("'+this.Obj.id+'")'+'.BoxHide();',100);
				        clearTimeout(this.Obj.BoxTimeOut);  }}
					IObj.BoxDiv.Sel=Sel;
					IObj.BoxDiv.appendChild(Sel);
					Sel.Obj=IObj;
									   
					Sel.onchange=function () { this.Obj.BoxIndex=this.selectedIndex;}
					Sel.onclick=function() { this.Obj.BoxIndex=this.selectedIndex;  SearchExecute(this.Obj); this.Obj.BoxH=setTimeout('getObj("'+this.Obj.id+'")'+'.BoxHide();',100); }
											for (r=0;r<=IObj.BoxArray.length-1;r++) {
											
												n=IObj.BoxArray[r].split('|')[0];
												l=IObj.BoxArray[r].split('|')[1]
												theOption=document.createElement("OPTION");	
											    theOption.innerHTML=n;
												IObj.BoxDiv.Sel.appendChild(theOption);
												if (r==IObj.BoxIndex) { theOption.setAttribute('selected','selected'); }
											}
											if (IObj.BoxFocused) { IObj.BoxDiv.style.display='block'; }
										}}
										
		function SearchExecute(IObj) {
			 if (IObj.BoxArray.length>0)
					{ var l;
					
					l=IObj.BoxArray[IObj.BoxIndex].split('|')[1]; 
					setTimeout('getObj("'+this.IObj.id+'")'+'.BoxHide();',100);
					location.href=l; }
			}								

//// * kfrom * ///////////////

   function KfromAjax(){
	  IObj=getObj('kfrom'); 
	  if (IObj.BoxFocused) { var val=IObj.value; 
								  if (val!='') { IObj.BoxDiv.style.display='none'; 
												IObj.BoxH=setTimeout('getObj("'+IObj.id+'").BoxHide();',100);
												makePOSTRequest('/pretraga-aerodrom/'+val,'',CatchKfromAjax);} 
												}}
												
   function CatchKfromAjax() { IObj=getObj('kfrom');
								if (IObj.BoxFocused) { 
									if (http_request.readyState == 4) {  if (http_request.status == 200){	 
										IObj.BoxLoad(http_request.responseText); }else{}}else{} // http technical errors.
								}}
   function KfromDraw(IObj)
   { var mout='';
				var n,l;
				var mtext='';
				
				if (IObj.BoxArray.length>0) {
					
					if (IObj.BoxDiv.Sel) { IObj.BoxDiv.removeChild(IObj.BoxDiv.Sel); }
					var Sel=document.createElement('select');
					Sel.setAttribute('size',IObj.DropCount);
					Sel.onfocus=function() {  this.Focused=true; }
					Sel.onblur=function() {  this.Focused=false; if (this.Obj.BoxFocused==false) {
						this.Obj.BoxH=setTimeout('getObj("'+this.Obj.id+'")'+'.BoxHide();',100);
				        clearTimeout(this.Obj.BoxTimeOut);  }}
					IObj.BoxDiv.Sel=Sel;
					IObj.BoxDiv.appendChild(Sel);
					Sel.Obj=IObj;
					Sel.onchange=function () { this.Obj.BoxIndex=this.selectedIndex;}
					
					Sel.onclick=function() { this.Obj.BoxIndex=this.selectedIndex; KfromExecute(this.Obj); this.Obj.BoxH=setTimeout('getObj("'+this.Obj.id+'")'+'.BoxHide();',100);}
					
											for (r=0;r<=IObj.BoxArray.length-1;r++) {
											
												n=IObj.BoxArray[r].split('|')[0];
												l=IObj.BoxArray[r].split('|')[1]
												theOption=document.createElement("OPTION");	
											    theOption.innerHTML=n;
												IObj.BoxDiv.Sel.appendChild(theOption);
												if (r==IObj.BoxIndex) { theOption.setAttribute('selected','selected'); }
											}
											if (IObj.BoxFocused) { IObj.BoxDiv.style.display='block'; }
										}}
										
		function KfromExecute(IObj) {
			 if (IObj.BoxArray.length>0)
					{ var l;
				  
					l=IObj.BoxArray[IObj.BoxIndex].split('|'); 
					getObj('kfromid').value=l[1];
					getObj('kfrom').value=l[0];
					setTimeout('getObj("'+this.IObj.id+'")'+'.BoxHide();',100);
					
					}
			}
			
//// * Kto * ///////////////

   function KtoAjax(){
	  IObj=getObj('kto'); 
	  if (IObj.BoxFocused) { var val=IObj.value; 
								  if (val!='') { IObj.BoxDiv.style.display='none'; 
												IObj.BoxH=setTimeout('getObj("'+IObj.id+'").BoxHide();',100);
												makePOSTRequest('/pretraga-aerodrom/'+val,'',CatchKtoAjax);} 
												}}
												
   function CatchKtoAjax() { IObj=getObj('kto');
								if (IObj.BoxFocused) { 
									if (http_request.readyState == 4) {  if (http_request.status == 200){	 
										IObj.BoxLoad(http_request.responseText); }else{}}else{} // http technical errors.
								}}
   function KtoDraw(IObj)
   { var mout='';
				var n,l;
				var mtext='';
				
				if (IObj.BoxArray.length>0) {
					
					if (IObj.BoxDiv.Sel) { IObj.BoxDiv.removeChild(IObj.BoxDiv.Sel); }
					var Sel=document.createElement('select');
					Sel.setAttribute('size',IObj.DropCount);
					Sel.onfocus=function() {  this.Focused=true; }
					Sel.onblur=function() {  this.Focused=false; if (this.Obj.BoxFocused==false) {
						this.Obj.BoxH=setTimeout('getObj("'+this.Obj.id+'")'+'.BoxHide();',100);
				        clearTimeout(this.Obj.BoxTimeOut);  }}
					IObj.BoxDiv.Sel=Sel;
					IObj.BoxDiv.appendChild(Sel);
					Sel.Obj=IObj;
					Sel.onchange=function () { this.Obj.BoxIndex=this.selectedIndex;}
					
					Sel.onclick=function() { this.Obj.BoxIndex=this.selectedIndex; KtoExecute(this.Obj); this.Obj.BoxH=setTimeout('getObj("'+this.Obj.id+'")'+'.BoxHide();',100);}
					
											for (r=0;r<=IObj.BoxArray.length-1;r++) {
											
												n=IObj.BoxArray[r].split('|')[0];
												l=IObj.BoxArray[r].split('|')[1]
												theOption=document.createElement("OPTION");	
											    theOption.innerHTML=n;
												IObj.BoxDiv.Sel.appendChild(theOption);
												if (r==IObj.BoxIndex) { theOption.setAttribute('selected','selected'); }
											}
											if (IObj.BoxFocused) { IObj.BoxDiv.style.display='block'; }
										}}
										
		function KtoExecute(IObj) {
			 if (IObj.BoxArray.length>0)
					{ var l;
				  
					l=IObj.BoxArray[IObj.BoxIndex].split('|'); 
					getObj('ktoid').value=l[1];
					getObj('kto').value=l[0];
					setTimeout('getObj("'+this.IObj.id+'")'+'.BoxHide();',100);
					
					}
			}			
