// ajax quick searchBox_content

var pos_count=0;

var match_count=0;

var saved_wtf;

var scroll_count=0;



//										DEVELOPER'S INTERFACE

//-----------------------------------------------------------------------------------------------------------------------



var show_empty_box='no';    //show empty Box iF did not find any entries



function do_EnterKey_action()   // DEVELOPER'S CODE HERE  

{

	clear_table();

}

function do_Import_action(type,id)  // DEVELOPER'S CODE HERE

{
																																							

//	if (type=='company_categories') new Ajax.Updater('secondary_category','jax/load_secondary_company_category.jax.php',{evalScripts:true,method:'post',postBody:'id='+id});

}



//-----------------------------------------------------------------------------------------------------------------------





function select_line(way)
{
		//alert(match_count);
		if (pos_count>match_count)
			{	
				pos_count=1;way='up-down';
				if (document.getElementById('line'+match_count)) {document.getElementById('line'+match_count).className ='searchBox'} 
				scroll_count=0;
				$('searchBox_content').scrollTop = scroll_count;
			}
		else	
		if (pos_count<=0) 
			{	
				pos_count=match_count;way='up-down';
				if (document.getElementById('line1')) {document.getElementById('line1').className ='searchBox'}
				scroll_count=match_count*row_height;
				$('searchBox_content').scrollTop = scroll_count;
			}
		if (way=='down') if (document.getElementById('line'+(pos_count-1)))
			{
				document.getElementById('line'+(pos_count-1)).className ='searchBox';
				if ($('searchBox_content').scrollTop+searchBox_height < (pos_count*row_height)) 
					{
						scroll_count+=row_height;
						$('searchBox_content').scrollTop = scroll_count; 
					}
			} 
		if (way=='up') if (document.getElementById('line'+(pos_count+1))) 
			{
				document.getElementById('line'+(pos_count+1)).className ='searchBox';
				if ($('searchBox_content').scrollTop >= (pos_count*row_height)) 
					{
						scroll_count=$('searchBox_content').scrollTop;
						scroll_count-=row_height;
						$('searchBox_content').scrollTop = scroll_count; 
					}
			} 

		if (document.getElementById('line'+pos_count)) document.getElementById('line'+pos_count).className ='selected_searchBox'; 
}

function clear_table()
{
	pos_count=0;	
	for (i=1;i<=match_count;i++) if (document.getElementById('line'+i)) document.getElementById('line'+i).innerHTML='';
	document.getElementById('searchBox_content').style.visibility = 'hidden';
}

function import_value(line_id,value,id)
{
	if (id=='') id=saved_wtf;
	document.getElementById(id).value=value;
	if (document.getElementById(id+'_id')) document.getElementById(id+'_id').value=$('line'+line_id).axis;
	clear_table();	
	do_Import_action(id,$('line'+line_id).axis);
}

function showBox(field_object,e,otherParams)
{		
	document.onclick=clear_table;
	var myKeyCode=0;
	var flag=0;
	field_obj_name = field_object.name;
	field_obj_id = field_object.id;
	saved_wtf=field_obj_name;
	
	if (document.getElementById(field_obj_name+'_id')) document.getElementById(field_obj_name+'_id').value='';
	
	if (navigator.appName=='Microsoft Internet Explorer')
		myKeyCode=e.keyCode;
	else
		myKeyCode=e.which;
	
	if (myKeyCode==13  && pos_count<=0) { do_EnterKey_action();}
	else
	{
			if (myKeyCode==13 && pos_count>0 && document.getElementById('searchBox_content').style.visibility == 'visible') import_value(pos_count,document.getElementById('line'+pos_count).innerHTML,field_obj_id);
			else
			{
				if  (myKeyCode!=40 &&  myKeyCode!=38)
				{
					var pos = Position2.get(field_object);
					var thaDiv = document.getElementById('searchBox_content');	
					pos.top += 19;					
					pos.left-=12;	
					Position2.set(thaDiv, pos);
					savedTf = field_object;
					//alert(otherParams);
					//thaDiv.style.visibility = 'visible';	//DEBUG HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!		
					field_obj_value = field_object.value;
					new Ajax.Updater('searchBox_content', 'jax/searchBox.jax.php', {evalScripts:true, method:'post', postBody:'field_object='+field_object+'&field_obj_value='+field_obj_value+'&field_obj_name='+field_obj_name+'&pos_count='+pos_count+'&city_id='+otherParams});
				}
				if (myKeyCode==40) {pos_count++;select_line('down');}
				if (myKeyCode==38) {pos_count--;select_line('up');}
			}
	}
}


function alternativeShowBox(field_object,ev,otherParams)
{
	document.onclick=clear_table;
	field_obj_name = field_object.name;
	field_obj_id = field_object.id;
	saved_wtf=field_obj_name;
	var pos = Position2.get(field_object);
	var thaDiv = document.getElementById('searchBox_content');	
	pos.top += 19;					
	Position2.set(thaDiv, pos);
	savedTf = field_object;
	//thaDiv.style.visibility = 'visible';	//DEBUG HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!			
	field_obj_value = field_object.value;
	if (thaDiv.style.visibility != 'visible')
		new Ajax.Updater('searchBox_content', 'modules/'+module_dir+'/jax/searchBox.jax.php', {evalScripts:true, method:'post', postBody:'field_object='+field_object+'&field_obj_value='+field_obj_value+'&field_obj_name='+field_obj_name+'&pos_count='+pos_count+'&type=alternative'+otherParams});

}



















// the Position class



/*===================================================================

 Author: Matt Kruse

 

 View documentation, examples, and source code at:

     http://www.JavascriptToolbox.com/



 NOTICE: You may use this code for any purpose, commercial or

 private, without any further permission from the author. You may

 remove this notice from your final code if you wish, however it is

 appreciated by the author if at least the web site address is kept.



 This code may NOT be distributed for download from script sites, 

 open source CDs or sites, or any other distribution method. If you

 wish you share this code with others, please direct them to the 

 web site above.

 

 Pleae do not link directly to the .js files on the server above. Copy

 the files to your own server for use with your site or webapp.

 ===================================================================*/

var Position2 = (function() {

	// Resolve a string identifier to an object

	// ========================================

	function resolveObject(s) {

		if (document.getElementById && document.getElementById(s)!=null) {

			return document.getElementById(s);

		}

		else if (document.all && document.all[s]!=null) {

			return document.all[s];

		}

		else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x) {

			for (var i=0; i<document.anchors.length; i++) {

				if (document.anchors[i].name==s) { 

					return document.anchors[i]

				}

			}

		}

	}

	

	var pos = {};

	pos.$VERSION = 1.0;

	

	// Set the position of an object

	// =============================

	pos.set = function(o,left,top) {

		if (typeof(o)=="string") {

			o = resolveObject(o);

		}

		if (o==null || !o.style) {

			return false;

		}

		

		// If the second parameter is an object, it is assumed to be the result of getPosition()

		if (typeof(left)=="object") {

			var pos = left;

			left = pos.left;

			top = pos.top;

		}

		

		o.style.left = left + "px";

		o.style.top = top + "px";

		return true;

	};

	

	// Retrieve the position and size of an object

	// ===========================================

	pos.get = function(o) {

		var fixBrowserQuirks = true;

			// If a string is passed in instead of an object ref, resolve it

		if (typeof(o)=="string") {

			o = resolveObject(o);

		}

		

		if (o==null) {

			return null;

		}

		

		var left = 0;

		var top = 0;

		var width = 0;

		var height = 0;

		var parentNode = null;

		var offsetParent = null;

	

		

		offsetParent = o.offsetParent;

		var originalObject = o;

		var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references

		while (el.parentNode!=null) {

			el = el.parentNode;

			if (el.offsetParent==null) {

			}

			else {

				var considerScroll = true;

				/*

				In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already 

				take its scroll position into account. If elements further up the chain are scrollable, their 

				scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value

				which must be ignored.

				*/

				if (fixBrowserQuirks && window.opera) {

					if (el==originalObject.parentNode || el.nodeName=="TR") {

						considerScroll = false;

					}

				}

				if (considerScroll) {

					if (el.scrollTop && el.scrollTop>0) {

						top -= el.scrollTop;

					}

					if (el.scrollLeft && el.scrollLeft>0) {


						left -= el.scrollLeft;

					}

				}

			}

			// If this node is also the offsetParent, add on the offsets and reset to the new offsetParent

			if (el == offsetParent) {

				left += o.offsetLeft;

				if (el.clientLeft && el.nodeName!="TABLE") { 

					left += el.clientLeft;

				}

				top += o.offsetTop;

				if (el.clientTop && el.nodeName!="TABLE") {

					top += el.clientTop;

				}

				o = el;

				if (o.offsetParent==null) {

					if (o.offsetLeft) {

						left += o.offsetLeft;

					}

					if (o.offsetTop) {

						top += o.offsetTop;

					}

				}

				offsetParent = o.offsetParent;

			}

		}

		

	

		if (originalObject.offsetWidth) {

			width = originalObject.offsetWidth;

		}

		if (originalObject.offsetHeight) {

			height = originalObject.offsetHeight;

		}

		

		

		return {'left':left, 'top':top, 'width':width, 'height':height

				};

	};

	

	// Retrieve the position of an object's center point

	// =================================================

	pos.getCenter = function(o) {

		var c = this.get(o);

		if (c==null) { return null; }

		c.left = c.left + (c.width/2);

		c.top = c.top + (c.height/2);

		return c;

	};

	

	return pos;

})();


