

var http = false;

if(navigator.appName == "Microsoft Internet Explorer") 
{
  http = new ActiveXObject("Microsoft.XMLHTTP");
} 
else 
{
  http = new XMLHttpRequest();
} 


//
// Delay the ajax call until user has stopped typing
//
last_value = "-";
refine_delay = "";
current_index = 0;
function refine(e) 
{
	if (e==undefined || !e) 
		var e = window.event;
		
	// 13 == enter
	// 38 == up
	// 40 == down
	if(e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 && last_value!=document.getElementById('search').value)  
	{
		if(refine_delay!="")
		{
			clearTimeout(refine_delay);
		}
		refine_delay = setTimeout("refine_sub();",300);
	}
	else
	{
		var dropsize = document.getElementById('ajaxmatches').getElementsByTagName('div').length;
		//alert(dropsize);
		
		var previous = current_index;
		if(e.keyCode==38 && current_index>0) current_index -= 1;
		else if(e.keyCode==40 && current_index<(dropsize-2)) current_index += 1;
		else if(e.keyCode==13)
		{
			document.getElementById('place'+current_index).onclick();
		}
				
		if(previous!=current_index)
		{	
			document.getElementById('place'+previous).className = 
				document.getElementById('place'+previous).className.replace(" select","") ;
			
			document.getElementById('place'+current_index).className = 
				document.getElementById('place'+current_index).className+" select" ;
		}
		
		if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } 
	}
}

function change_select(index)
{
	var previous = current_index;
	current_index = index;
	if(previous!=current_index)
	{	
		document.getElementById('place'+previous).className = 
				document.getElementById('place'+previous).className.replace(" select", "") ;
			
		document.getElementById('place'+current_index).className = 
				document.getElementById('place'+current_index).className+" select" ;
	}	
}


function refine_sub()	
{
	last_value = document.getElementById('search').value;
	
	refine_delay = "";
	document.getElementById('ajaxmatches').innerHTML = "loading, please wait...";
	
	current_index = 0;
	
  target = "ajaxsearch.php?search="+urlencode(document.getElementById('search').value);
  
  http.open("GET", target, true);
  http.onreadystatechange=function() 
  {
    if(http.readyState == 4) 
	{
      document.getElementById('ajaxmatches').innerHTML = http.responseText;
    }
    
    dropdown = document.getElementById('ajaxmatchesdropdown');
    
    
    pos = findpos(document.getElementById('search'));
    dropdown.style.left = (pos[0]-6) + "px";
    dropdown.style.top = (pos[1]+20) + "px";    
    dropdown.style.display = "block";
    
    
    if(document.getElementById('ajaxmatches').innerHTML!="")
    {
    	dropsize = document.getElementById('ajaxmatches').getElementsByTagName('div').length;
    	document.getElementById('ajaxmatches').state = "filled";
    	if( dropsize<8 )
    	{
    		dropdown.style.height = (dropsize*20+20)+"px"; //alert(dropsize);
    	}else dropdown.style.height = "180px";
    }
    else
    {
    	document.getElementById('ajaxmatches').innerHTML = "<div class=\"option\"><b>Tip:</b><br/>Enter the first few letters of the place, then use your mouse or your keyboard's up, down and enter keys to make your selection</div>";
    	document.getElementById('ajaxmatches').state = "empty";
    	dropdown.style.height = "69px";
    }
  }
  http.send(null);
}




function refine2(e) 
{
	if (e==undefined || !e) 
		var e = window.event;
		
	// 13 == enter
	// 38 == up
	// 40 == down
	if(e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 && last_value!=document.getElementById('search').value)  
	{
		if(refine_delay!="")
		{
			clearTimeout(refine_delay);
		}
		refine_delay = setTimeout("refine_sub2();",300);
	}
	else
	{
		var dropsize = document.getElementById('ajaxmatches').getElementsByTagName('div').length;
		//alert(dropsize);
		
		var previous = current_index;
		if(e.keyCode==38 && current_index>0) current_index -= 1;
		else if(e.keyCode==40 && current_index<(dropsize-2)) current_index += 1;
		else if(e.keyCode==13)
		{
			document.getElementById('place'+current_index).onclick();
		}
				
		if(previous!=current_index)
		{	
			document.getElementById('place'+previous).className = 
				document.getElementById('place'+previous).className.replace(" select","") ;
			
			document.getElementById('place'+current_index).className = 
				document.getElementById('place'+current_index).className+" select" ;
		}
		
		if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } 
	}
}


function refine_sub2() 
{
  target = "ajaxsearch2.php?search="+urlencode(document.getElementById('search').value);
  
  http.open("GET", target, true);
  http.onreadystatechange=function() 
  {
    if(http.readyState == 4) 
	{
      document.getElementById('ajaxmatches').innerHTML = http.responseText;
    }
    
    dropdown = document.getElementById('ajaxmatchesdropdown');
    
    
    pos = findpos(document.getElementById('search'));
    dropdown.style.left = (pos[0]-6) + "px";
    dropdown.style.top = (pos[1]+26) + "px";    
    dropdown.style.display = "block";
    
    
    if(document.getElementById('ajaxmatches').innerHTML!="")
    {
    	dropsize = document.getElementById('ajaxmatches').getElementsByTagName('div').length;
    	document.getElementById('ajaxmatches').state = "filled";
    	if( dropsize<8 )
    	{
    		dropdown.style.height = (dropsize*20+20)+"px"; //alert(dropsize);
    	}else dropdown.style.height = "180px";
    }
    else
    {
    	document.getElementById('ajaxmatches').innerHTML = "<div class=\"option\"><b>Tip:</b><br/>Enter the first few letters of the place, then use your mouse or your keyboard's up, down and enter keys to make your selection</div>";
    	document.getElementById('ajaxmatches').state = "empty";
    	dropdown.style.height = "69px";
    }
  }
  http.send(null);
}


 
function blursearch()
{

	if(document.getElementById('ajaxmatches').state == "empty")
    {
		document.getElementById('ajaxmatchesdropdown').style.display = 'none'	
	}
	
}

function blurnow()
{

	document.getElementById('ajaxmatchesdropdown').style.display = 'none'	
	
}



function subregion(level) 
{
	if(level==1)
	{
		target = "ajaxregion.php?level="+level+"&search="+urlencode(document.getElementById('subregion'+level).value);
	}
	else
  	{
	  	target = "ajaxregion.php?level="+level+
		  			"&parent="+document.getElementById('subregion'+(level-1)+"code").value+
				  	"&search="+urlencode(document.getElementById('subregion'+level).value);
	}
  
	http.open("GET", target, true);
	http.onreadystatechange=function() 
	{
	if(http.readyState == 4) 
	{
	  	document.getElementById('ajaxmatches').innerHTML = http.responseText;
	}
	
	if(document.getElementById('ajaxmatches').innerHTML!="")
	{
		
		pos = findpos(document.getElementById('subregion'+level));
		dropdown = document.getElementById('ajaxmatchesdropdown');
		
		dropdown.style.display = "block";
		dropdown.style.left = (pos[0]-6) + "px";
		dropdown.style.top = (pos[1]+20) + "px";
		
		dropsize = document.getElementById('ajaxmatches').getElementsByTagName('div').length;
		
		if(dropsize==0)
		{
			document.getElementById('ajaxmatchesdropdown').style.display = "none";	
		}
		else if( dropsize<8 )
		{
			dropdown.style.height = (dropsize*20+20)+"px"; //alert(dropsize);
		}else dropdown.style.height = "180px";
	}
	else
	{
		document.getElementById('ajaxmatchesdropdown').style.display = "none";	
	}
	}
	http.send(null);
}



function urlencode (clearString) 
{
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') 
	{
    	output += match[1];
      x += match[1].length;
    } 
	else 
	{
      if (clearString[x] == ' ')
        output += '+';
      else 
	  {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function findpos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}



function confirm_op( $type )
{

	if( confirm("Are you sure you wish to "+$type+"?") )
	{
		return true;
	}
	return false;
}


function GetPageSize()
{
	var scnWid,scnHei;
	if (self.innerHeight) // all except Explorer
	{
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}
	return [scnWid,scnHei];
}