// Global variables
var records = 0;
var showrecord = 0;
var DisableMouseOver=0;
var timer1=0;
var timer2=0;
var timer3=0;

// hide search div
function hideSearchDiv()
{
	document.getElementById("ajaxsearchdiv").style.display="none";
}

// submit search form
function submitForm()
{
	document.searchform.submit();
}

//on mouse over ajax search div
function mouseOverDiv(object)
{
	object.style.cursor='pointer';
	clearTimeout(timer1);
	clearTimeout(timer2);
	clearTimeout(timer3);
	timer1=0;
	timer2=0;
	timer3=0;
}

//Get search words
function getSearchWords(key, url)
{	 
	// Send get request
	if (key  != 40 && key != 38 && key != 13 && key!=27) 
	{ 
		if(document.searchform.search.value.length >1)
		{
			setTimeout("sendGetRequest('" + url + "', onRequestComplete);" , 300);
			showrecord = 0;  
		}
		else
		{
			hideSearchDiv();
		}		
	}
} 	


//On request complete
function onRequestComplete()
{
	if(xmlHttp.readyState == 4)
	{	
		if(xmlHttp.status == 200)
		{
			responseText = xmlHttp.responseText;
			responseXml = xmlHttp.responseXML;										
		}		
	
		// Set data
		if(responseText!='')
		{
			document.getElementById("ajaxsearchdiv").style.display="block";
			document.getElementById("ajaxsearchdiv").innerHTML = responseText;
			records = document.getElementById("ajaxsearchdiv").childNodes.length;
		}
		else
		{
			//set dispaly property
			document.getElementById("ajaxsearchdiv").style.display="none";
			//set value
			document.getElementById("ajaxsearchdiv").innerHTML = "";
		}	                
	}
}	

// Mouse over event
function MouseOver(value)
{
	if(DisableMouseOver!=1)
	{
		// Loop records to mark selected
		for (i = 1; i <= records; i++)
		{
			if(i==value)
			{
				showrecord=i;
			}
			document.getElementById(i).style.backgroundColor = '#ffffff';
		}
		document.getElementById(value).style.backgroundColor='#e8e8e8';

		// Get browser type,to get a value
		if(document.all)
		{
			var post = document.getElementById(value).innerText;
		} 
		else
		{
			var post = document.getElementById(value).textContent;
		}
		// Set value
		document.getElementById("nodeid").value=document.getElementById(value).value;
	}
	DisableMouseOver=0;
}
    
           
// Mouse out event
function MouseOut(value)
{
	// Set color
	document.getElementById(value).style.backgroundColor='#ffffff';
}
     
// key event
function keyEventProcessing(key)
{

	// Record counter 
	var i;
	
	// Handle keys
	switch(key)
	{

		case 8:
			//mouse over spremenljivka
			DisableMouseOver=1;
			break; 
			
		case 9:
			var post="";
			showrecord +=1;

			// Low water
			if (showrecord < 1) 
			{ 
				showrecord = 1; 
			}

			// High water
			if (showrecord > records) 
			{ 
				showrecord = records; 
			}
			// Loop records to mark selected
			for (i = 1; i <= records; i++)
			{
				if (i == showrecord) 
				{ 
					var tmp=0;
					tmp=showrecord-1;
					//Set value
					post = document.getElementById(tmp).innerText;

					document.getElementById("nodeid").value=document.getElementById(tmp).value;
					hideSearchDiv();

					//Exit
					return; 
				}
			}
			break;

		case 38: // up arrow key
			// Decrease counter
			showrecord -=1;

			// Low water
			if (showrecord < 1) 
			{ 
				showrecord = 1; 
			}

			// High water
			if (showrecord > records) 
			{ 
				showrecord = records; 
			}

			// Loop records to mark selected
			for (i = 1; i <= records; i++)
			{
				if (i == showrecord) 
				{ 
					document.getElementById(i).style.backgroundColor = '#e8e8e8'; 
				} 
				else 
				{ 
					document.getElementById(i).style.backgroundColor = '#ffffff'; 
				}
			}

			//get browser type,to get a value                  
			if(document.all)
			{
				var post = document.getElementById(showrecord).innerText;
			} 
			else
			{
				post = document.getElementById(showrecord).textContent;
			}
			
			document.getElementById("nodeid").value=document.getElementById(showrecord).value;

			// Exit
			break;

		
		case 40: // down arrow key
			// Decrease counter
			showrecord +=1;

			// Low water
			if (showrecord < 1) 
			{ 
				showrecord = 1; 
			}

			// High water
			if (showrecord > records) 
			{ 
				showrecord = records; 
			}

			// Loop records to mark selected
			for (i = 1; i <= records; i++)
			{
				if (i == showrecord)
				{
					document.getElementById(i).style.backgroundColor = '#e8e8e8' ;
				}
				else
				{
					document.getElementById(i).style.backgroundColor = '#ffffff';
				}                   
			}
			
			//get browser type,to get a value
			if(document.all)
			{
				var post = document.getElementById(showrecord).innerText;
			} 
			else
			{ 
				//set value
				post = document.getElementById(showrecord).textContent;
			}
			document.getElementById("nodeid").value=document.getElementById(showrecord).value;

			//Exit                
			break;

		case 13: // enter key 
			hideSearchDiv();
			//Exit   
			break;
			
		case 27: //escape
			hideSearchDiv();
			break;
		
	}
}  
    
//catch mouse move and close search div
document.onmousemove = catchMouseMove;
function catchMouseMove(e)
{
	iScreenWidth = screen.width - 300;
	iScreenHeight = screen.height/2-50;
	try 
	{
		i_nullpunkt_x = screen.width/3;
		i_nullpunkt_y = screen.height/3;
	}
	catch (ex) 
	{
	}

	var objMouseEvent = 0;


	if (!e)
	{
		objMouseEventX = event.screenX;
		objMouseEventY = event.screenY;
	}
	else 
	{
		objMouseEventX = e.pageX;
		objMouseEventY = e.pageY;
	}

	if (objMouseEventX < i_nullpunkt_x) 
	{
		try 
		{
			if(timer1==0)
			{
				timer1=setTimeout("hideSearchDiv();",2000);
			}
		}
		catch (ex) 
		{
		}
	}

	if (objMouseEventX > iScreenWidth) 
	{
		try 
		{
			if(timer2==0)
			{
				timer2=setTimeout("hideSearchDiv();",2000);
			}
		}
		catch (ex) 
		{
		}
	}

	if (objMouseEventY > iScreenHeight) 
	{
		try 
		{
			if(timer3==0)
			{
				timer3=setTimeout("hideSearchDiv();",2000);
			}
		}
		catch (ex) 
		{
		}
	}
}
