var pos=0;
var num_rows;

function changePosition(id)
{
	if (this.pos > 0 && this.pos <= num_rows)
	{
		div_id = "s"+this.pos;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("industry_name").value = '';
	}
		
	this.pos = id;
	div_id = "s"+this.pos;
	document.getElementById(div_id).style.background = '#ffffff';
}

function updateSearch(id)
{
	if (this.pos > 0 && this.pos <= num_rows)
	{
		div_id = "s"+this.pos;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("industry_name").value = '';
	}
		
	this.pos = id;
	div_id = "s"+this.pos;
	document.getElementById(div_id).style.background = '#ffffff';
	document.getElementById("industry_name").value = document.getElementById(div_id +"_value").value.replace(/&amp;/gi, '&');
	
	document.getElementById("suggestBox").style.visibility = 'hidden';
	document.getElementById("industry_name").focus();
	document.getElementById("industry_name").value = document.getElementById("industry_name").value;
	this.pos = 0;
}

function suggest(e)
{
	var intKey = 0;
	e = (window.event)? event : e;
	intKey = (e.keyCode)? e.keyCode: e.charCode;

	if (intKey == 40) //down
	{
		
		//SWITCH THEM OFF
		if (this.pos>0 && this.pos<=num_rows)
		{
			
			div_id = "s"+this.pos;
		
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("industry_name").value = '';
		}
		
		//SWITCH THEM ON
		if (this.pos+1<=num_rows)
		{			
			this.pos = this.pos+1;
			div_id = "s"+this.pos;
			document.getElementById(div_id).style.background = '#ffffff';
			document.getElementById("industry_name").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if(this.pos+1 == num_rows+1)
		{
			this.pos = this.pos +1;			
		}
	}
	else if (intKey == 38) //up
	{
		//SWITCH THEM OFF
		if (this.pos>0 && this.pos<=num_rows)
		{
			div_id = "s"+this.pos;
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("industry_name").value = '';
		}
		
		//SWITCH THEM ON
		if (this.pos-1>0)
		{
			this.pos = this.pos-1;
			div_id = "s"+this.pos;
			document.getElementById(div_id).style.background = '#ffffff';
			input_text = 
			document.getElementById("industry_name").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if (this.pos-1==0)
		{
			this.pos = this.pos-1;
		}
	}
	else 
	{
		this.pos=0;
		getSuggestions();
	}
}

function getSuggestions()
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}


	var url="/suggest.php?str="+document.getElementById("industry_name").value + "&city_name=" + document.getElementById('city_name').value +"&sid="+Math.random();
	
	xmlHttp.onreadystatechange=displaySuggestions;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function displaySuggestions() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		trimmed = xmlHttp.responseText.replace(/^\s+|\s+$/g, '');
		
		delim = trimmed.indexOf(":==:");
		
		num_rows = parseInt(trimmed.substr(0,delim));
		
		remaining = trimmed.substr(trimmed.indexOf(":==:")+4);
		
		trimmed = remaining;
		
		if (remaining!='')
		{
			document.getElementById("suggestBox").style.visibility = 'visible';
		
			document.getElementById("suggestBox").innerHTML=remaining;
		}
		else
		{
			document.getElementById("suggestBox").style.visibility = 'hidden';
			this.pos = 0;
		}
	} 	
}

function GetXmlHttpObject()
{
	var xmlHttp;
	try
	{    // Firefox, Opera 8.0+, Safari
	    xmlHttp=new XMLHttpRequest();    
	}
	catch (e)
	{    // Internet Explorer    
		try
		{      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
		catch (e)
		{     
			try
			{       
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			}
			catch (e)
			{        
				alert("Your browser does not support AJAX!");        
				return false;        
			}      
		}    
	}
	return xmlHttp;
}

var pocs=0;
var num_rowsc;

function changePosition_c(id)
{
	if (this.posc > 0 && this.posc <= num_rowsc)
	{
		div_id = "sc"+this.posc;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("city_name").value = '';
	}
		
	this.posc = id;
	div_id = "sc"+this.posc;
	document.getElementById(div_id).style.background = '#ffffff';
}

function updateSearch_c(id)
{
	if (this.posc > 0 && this.posc <= num_rowsc)
	{
		div_id = "sc"+this.posc;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("city_name").value = '';
	}
		
	this.posc = id;
	div_id = "sc"+this.posc;
	document.getElementById(div_id).style.background = '#ffffff';
	document.getElementById("city_name").value = document.getElementById(div_id +"_value").value.replace(/&amp;/gi, '&');
	
	document.getElementById("suggestBoxc").style.visibility = 'hidden';
	document.getElementById("city_name").focus();
	document.getElementById("city_name").value = document.getElementById("city_name").value;
	this.pos = 0;
}

function suggest_c(e)
{
	var intKey = 0;
	e = (window.event)? event : e;
	intKey = (e.keyCode)? e.keyCode: e.charCode;
	
	if (intKey == 40) //down
	{
		
		
		//SWITCH THEM OFF
		if (this.posc>0 && this.posc<=num_rowsc)
		{
			
			div_id = "sc"+this.posc;
		
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("city_name").value = '';
		}
		
		//SWITCH THEM ON
		if (this.posc+1<=num_rowsc)
		{			
			//alert('city_down on');
			this.posc = this.posc+1;
			div_id = "sc"+this.posc;
			document.getElementById(div_id).style.background = '#ffffff';
			document.getElementById("city_name").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if(this.posc+1 == num_rowsc+1)
		{
			this.posc = this.posc +1;			
		}
	}
	else if (intKey == 38) //up
	{
		//SWITCH THEM OFF
		if (this.posc>0 && this.posc<=num_rows)
		{
			div_id = "sc"+this.pos;
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("city_name").value = '';
		}
		
		//SWITCH THEM ON
		if (this.posc-1>0)
		{
			this.posc = this.posc-1;
			div_id = "sc"+this.posc;
			document.getElementById(div_id).style.background = '#ffffff';
			input_text = 
			document.getElementById("city_name").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if (this.posc-1==0)
		{
			this.posc = this.posc-1;
		}
	}
	else 
	{
		this.posc=0;
		getSuggestions_c();
	}
}

function getSuggestions_c()
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="/suggest_city.php?str="+document.getElementById("city_name").value+"&sid="+Math.random();
	xmlHttp.onreadystatechange=displaySuggestions_c;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function displaySuggestions_c() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		trimmed = xmlHttp.responseText.replace(/^\s+|\s+$/g, '');
		
		delim = trimmed.indexOf(":==:");
		
		num_rowsc = parseInt(trimmed.substr(0,delim));
		//alert(num_rowsc);
		remaining = trimmed.substr(trimmed.indexOf(":==:")+4);
		
		trimmed = remaining;
		
		if (remaining!='')
		{
			document.getElementById("suggestBoxc").style.visibility = 'visible';
		
			document.getElementById("suggestBoxc").innerHTML=remaining;
		}
		else
		{
			document.getElementById("suggestBoxc").style.visibility = 'hidden';
			this.posc = 0;
		}
	} 	
}


var posb=0;
var num_rowsb;

function changePosition_b(id)
{
	if (this.posb > 0 && this.posb <= num_rowsb)
	{
		div_id = "sb"+this.posb;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("search").value = '';
	}
		
	this.posb = id;
	div_id = "sb"+this.posb;
	document.getElementById(div_id).style.background = '#ffffff';
}

function updateSearch_b(id)
{
	if (this.posb > 0 && this.posb <= num_rowsb)
	{
		div_id = "sb"+this.posb;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("search").value = '';
	}
		
	this.posb = id;
	div_id = "sb"+this.posb;
	document.getElementById(div_id).style.background = '#ffffff';
	document.getElementById("search").value = document.getElementById(div_id +"_value").value.replace(/&amp;/gi, '&');
	
	document.getElementById("suggestBoxb").style.visibility = 'hidden';
	document.getElementById("search").focus();
	document.getElementById("search").value = document.getElementById("search").value;
	this.posb = 0;
}

function suggest_b(e)
{
	var intKey = 0;
	e = (window.event)? event : e;
	intKey = (e.keyCode)? e.keyCode: e.charCode;
	
	if (intKey == 40) //down
	{
		
		
		//SWITCH THEM OFF
		if (this.posb>0 && this.posb<=num_rowsb)
		{
			
			div_id = "sb"+this.posc;
		
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("search").value = '';
		}
		
		//SWITCH THEM ON
		if (this.posb+1<=num_rowsb)
		{			
			//alert('city_down on');
			this.posb = this.posb+1;
			div_id = "sb"+this.posb;
			document.getElementById(div_id).style.background = '#ffffff';
			document.getElementById("search").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if(this.posb+1 == num_rowsb+1)
		{
			this.posb = this.posb +1;			
		}
	}
	else if (intKey == 38) //up
	{
		//SWITCH THEM OFF
		if (this.posb>0 && this.posb<=num_rows)
		{
			div_id = "sb"+this.posb;
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("search").value = '';
		}
		
		//SWITCH THEM ON
		if (this.posb-1>0)
		{
			this.posb = this.posb-1;
			div_id = "sb"+this.posb;
			document.getElementById(div_id).style.background = '#ffffff';
			input_text = 
			document.getElementById("search").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if (this.posb-1==0)
		{
			this.posb = this.posb-1;
		}
	}
	else 
	{
		this.posb=0;
		getSuggestions_b();
	}
}

function getSuggestions_b()
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}


	var url="/suggest_bus.php?str="+document.getElementById("search").value+ "&city_name=" + document.getElementById('city_name').value +"&sid="+Math.random();
	xmlHttp.onreadystatechange=displaySuggestions_b;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

 
function displaySuggestions_b() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		trimmed = xmlHttp.responseText.replace(/^\s+|\s+$/g, '');
		
		delim = trimmed.indexOf(":==:");
		
		num_rowsb = parseInt(trimmed.substr(0,delim));
		//alert(num_rowsc);
		remaining = trimmed.substr(trimmed.indexOf(":==:")+4);
		
		trimmed = remaining;
		
		if (remaining!='')
		{
			document.getElementById("suggestBoxb").style.visibility = 'visible';
		
			document.getElementById("suggestBoxb").innerHTML=remaining;
		}
		else
		{
			document.getElementById("suggestBoxb").style.visibility = 'hidden';
			this.posb = 0;
		}
	} 	
}


var posp=0;
var num_rowsp;

function changePosition_p(id)
{
	if (this.posp > 0 && this.posp <= num_rowsp)
	{
		div_id = "sp"+this.posp;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("search_postcode").value = '';
	}
		
	this.posp = id;
	div_id = "sp"+this.posp;
	document.getElementById(div_id).style.background = '#ffffff';
}

function updateSearch_p(id)
{
	if (this.posp > 0 && this.posp <= num_rowsp)
	{
		div_id = "sp"+this.posp;
		document.getElementById(div_id).style.background = '#F1F8FF';
		document.getElementById("search_postcode").value = '';
	}
		
	this.posp = id;
	div_id = "sp"+this.posp;
	document.getElementById(div_id).style.background = '#ffffff';
	document.getElementById("search_postcode").value = document.getElementById(div_id +"_value").value.replace(/&amp;/gi, '&');
	
	document.getElementById("suggestBoxp").style.visibility = 'hidden';
	document.getElementById("search_postcode").focus();
	document.getElementById("search_postcode").value = document.getElementById("search_postcode").value;
	this.posp = 0;
}

function suggest_p(e)
{
	var intKey = 0;
	e = (window.event)? event : e;
	intKey = (e.keyCode)? e.keyCode: e.charCode;
	
	if (intKey == 40) //down
	{
		
		
		//SWITCH THEM OFF
		if (this.posb>0 && this.posb<=num_rowsb)
		{
			
			div_id = "sp"+this.posc;
		
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("search_postcode").value = '';
		}
		
		//SWITCH THEM ON
		if (this.posp+1<=num_rowsp)
		{			
			//alert('city_down on');
			this.posp = this.posp+1;
			div_id = "sp"+this.posp;
			document.getElementById(div_id).style.background = '#ffffff';
			document.getElementById("search_postcode").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if(this.popb+1 == num_rowsp+1)
		{
			this.posp = this.posp +1;			
		}
	}
	else if (intKey == 38) //up
	{
		//SWITCH THEM OFF
		if (this.posp>0 && this.posp<=num_rows)
		{
			div_id = "sp"+this.posp;
			document.getElementById(div_id).style.background = '#F1F8FF';
			document.getElementById("search_postcode").value = '';
		}
		
		//SWITCH THEM ON
		if (this.posp-1>0)
		{
			this.posp = this.posp-1;
			div_id = "sp"+this.posp;
			document.getElementById(div_id).style.background = '#ffffff';
			input_text = 
			document.getElementById("search_postcode").value = document.getElementById(div_id + "_value").value.replace(/&amp;/gi, '&');
		}
		else if (this.posp-1==0)
		{
			this.posp = this.posp-1;
		}
	}
	else 
	{
		this.posp=0;
		getSuggestions_p();
	}
}

function getSuggestions_p()
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	
	var url="/suggest_postcode.php?str="+document.getElementById("search_postcode").value+"&sid="+Math.random();
	xmlHttp.onreadystatechange=displaySuggestions_p;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

 
function displaySuggestions_p() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	
		trimmed = xmlHttp.responseText.replace(/^\s+|\s+$/g, '');
		
		delim = trimmed.indexOf(":==:");
		
		num_rowsp = parseInt(trimmed.substr(0,delim));
		//alert(num_rowsc);
		remaining = trimmed.substr(trimmed.indexOf(":==:")+4);
		
		trimmed = remaining;
		
		if (remaining!='')
		{
			
			document.getElementById("suggestBoxp").style.visibility = 'visible';
		
			document.getElementById("suggestBoxp").innerHTML=remaining;
		}
		else
		{
			document.getElementById("suggestBoxp").style.visibility = 'hidden';
			this.posp = 0;
		}
	} 	
}

function clickedon(id)
{

	//alert(document.getElementById('industry_name').value);
	
	if(document.getElementById('industry_name').value=='')
	{
		document.getElementById('industry_name').value='Search by Category';
	}else if(id=='industry_name' && document.getElementById('industry_name').value=='Search by Category')
	{
		document.getElementById('industry_name').value='';
	}
	
	if(document.getElementById('search').value=='')
	{
		document.getElementById('search').value='Search by Business Name';
	}else if(id=='search' && document.getElementById('search').value=='Search by Business Name')
	{
		document.getElementById('search').value='';
	}
		
	
	
	document.getElementById('suggestBox').style.visibility='hidden';
	document.getElementById('suggestBoxb').style.visibility='hidden';

}
