// AJAX
function createQCObject()
{ 
   var http; 
   if(window.XMLHttpRequest)
   { 
      // Is not IE
      http = new XMLHttpRequest(); 
   }
   else if(window.ActiveXObject)
   { 
      // Is  IE 5+
      http = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else
   { 
      alert('Problem creating the XMLHttpRequest object'); 
   } 
   return http; 
} 


// Load ALL restaurants from db as XML
function requestRestaurantsFromDbAsXML()
{
	requestEventsFromDbAsXML();
}

// Load ALL events from db as XML
function requestEventsFromDbAsXML()
{
	httpreq = createQCObject(); 	
	httpreq.onreadystatechange = loadEventsFromDbAsXML;
	httpreq.open("POST", "ajax_tapahtumat.php", true);
	
	// Set HTTP-header
	httpreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpreq.send(null);
}

// Load ALL restaurants from db as XML
function requestTownsFromDbAsXML()
{
	httpreq2 = createQCObject(); 	
	httpreq2.onreadystatechange = loadTownsFromDbAsXML;
	httpreq2.open("POST", "ajax_paikkakunnat.php", true);
	httpreq2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var town_query;
	
	httpreq2.send('replyType=ajax');
}

function loadTownsFromDbAsXML()
{
	if(httpreq2.readyState == 4)
	{
		// Check if request is completed:
		if(httpreq2.status == 200)
		{
			all_towns_xml = httpreq2.responseXML;
			requestRestaurantsFromDbAsXML();
		}	
	}
}

function loadEventsFromDbAsXML()
{
	if(httpreq.readyState == 4)
	{
		if(httpreq.status == 200)
		{
			//all_events_xml = httpreq.responseXML;
			
			// All XML loaded, now update all lists
			updateLists(0, 0, 0, 0);
		}	
	}
}

function loadRestaurantsFromDbAsXML()
{
	var i = 0;
	var ravintolat = "";
	var kaupungit = [];

	i = 0;
			
	// Check if request is ready
	if(http.readyState == 4)
	{
		// Check if request is completed
		if(http.status == 200)
		{
			// Save response xml to global variable
			//all_restaurants_xml = http.responseXML;
			
			// Call next ajax-request
			requestEventsFromDbAsXML();
	
		}	
	}
}

function ajax_search(e, str)
{
	
	// get the event 
	e = (!e) ? window.event : e;
	 
  	// get the character code of the pressed button 
  	code = (e.charCode) ? e.charCode : 
       ((e.keyCode) ? e.keyCode : 
       ((e.which) ? e.which : 0)); 
  
  	// check to see if the event was keyup 
  	if (e.type == "keyup")  
  	{     
  	  	isKeyUpDownPressed =false;  
  	  	// check to see we if are interested in the current character 
		if ((code < 13 && code != 8) ||  
  	      (code >=14 && code < 32) ||  
  	      (code >= 33 && code <= 46 && code != 38 && code != 40) ||  
  	      (code >= 112 && code <= 123) || (code == 13))  
  	  	{ 
  	  	 	// just ignore non-interesting charss 
		} 
    	else 
    	{
    		search = str;
	   		requestRestaurantsList(); 	
    		
    		if (getQueryPart('sivu') != 'ravintolat')
				window.location = 'index2.php?sivu=ravintolat';
    	}	
	} 
}

function ajax_search2(str)
{
	if (str.length == 0)
	{ 
		document.getElementById("hakukentta").innerHTML = "";
		search = '';
		input_text = '';	
	}
	else
	{
		search = str;
		input_text = str;
	}
	requestRestaurantsList();
} 
