
function getCoordinates ( node ){
    var coord = {
        x           : node.offsetLeft,
        y           : node.offsetTop
    }

    if ( node.offsetParent ){
        var p_coord = getCoordinates( node.offsetParent );
        coord.x += p_coord.x;
        coord.y += p_coord.y;
    }

    return coord;
}



var acLocTimeout;

function setAcLocVisibleInterval() 
{
	acLocTimeout = setTimeout('closeAcLoc()', 3000);
}


function closeAcLoc(itemId)
{
	if (document.getElementById(itemId)) document.getElementById(itemId).style.display = "none";
	
	clearTimeout(acLocTimeout);
}



function cancelCloseAcLoc()
{
	clearTimeout(acLocTimeout);
}



var itemIndex = -1;
var itemOldIndex = 0;
var originalInputText = "";


function getLocations(action, fieldId, containerId) 
{
	if (TrimString(document.getElementById(fieldId).value).length > 2) 
	{
		cancelCloseAcLoc();
		
		if (document.getElementById(containerId).style.display != "block") 
		{
			if (TrimString(document.getElementById(fieldId).value).length == 3)
			{
				var coords = getCoordinates( document.getElementById(fieldId) );
				var Ycoords = coords.y + 18;
			
				document.getElementById(containerId).style.left = coords.x + "px";
				document.getElementById(containerId).style.top = Ycoords + "px";
				document.getElementById(containerId).style.width = "300px";
				document.getElementById(containerId).style.display = "block";

				// load the auto-complete box:
				document.getElementById(containerId).src = "back/ac_location.aspx?act=" + action + "&str=" + document.getElementById(fieldId).value + "&fieldid=" + fieldId;

				itemIndex = -1;
				itemOldIndex = 0;
				
			//	alert(itemIndex);
			}

			
		}
		else 
		{
			var iframe = window.frames[containerId];
			var doc;
			if (iframe.documentElement) doc = iframe.documentElement;
			else if (iframe.contentDocument) doc = iframe.contentDocument;
			else if (iframe.contentWindow) doc = iframe.contentWindow;
			else doc = iframe.document;

			var arrAC = new Array();
			var m = 0;

			if (doc.getElementById("resultLine")) 
			{
				for (i = 0; i < doc.getElementById("resultLine").childNodes.length; i++) 
				{
					var node = doc.getElementById("resultLine").childNodes.item(i);
					if (node.nodeType == 1) 
					{
						var valueTxt = new String();
						if (document.all) valueTxt = node.innerText.toLowerCase();
						else valueTxt = node.text.toLowerCase();

						// filtering the list:
						if (valueTxt.indexOf(document.getElementById(fieldId).value.toLowerCase()) > -1) 
						{
							if (document.all) node.style.display = "block";
							else node.setAttribute('style', 'display:block;');

							arrAC[m] = new Array(3);
							arrAC[m][0] = node.attributes["id"].value;
							if (document.all) arrAC[m][1] = node.innerText;
							else arrAC[m][1] = node.text;
							arrAC[m][2] = i;
							m++;
						}
						else 
						{
							if (document.all) node.style.display = "none";
							else node.setAttribute('style', 'display:none;');
						}
					}
				} // end "for"

			}
			
			var containerHeight = m*19;
			iframe.height = containerHeight + "px";
			
			
			
			if ((pressedKey == 40 || pressedKey == 38) && m > 0) 
			{
				itemOldIndex = itemIndex;
				
				if (pressedKey == 40 && itemIndex < arrAC.length-1) itemIndex++; // 40 - down (++)
				
				if (pressedKey == 38 && itemIndex > 0) itemIndex--; // 38 - up (--)

				// clear the old marked:
				if (itemOldIndex > -1) 
					doc.getElementById('resultLine').childNodes.item(arrAC[itemOldIndex][2]).style.backgroundColor = "#FFFFFF";
				
				// hilite:
				doc.getElementById('resultLine').childNodes.item(arrAC[itemIndex][2]).style.backgroundColor = "#EEEEEE";
			}


			if (pressedKey == 13 /*enter*/ || pressedKey == 27/*escape*/ || pressedKey == 9/*tab*/)
			{
				// fill in the values:
				try {
					var atest = arrAC[itemIndex][1];
				}
				catch (e) {
					itemIndex = 0;
					itemOldIndex = 1;
				}
			//	document.getElementById(fieldId).value = arrAC[itemIndex][1];
			//	document.getElementById(fieldId+"Id").value = arrAC[itemIndex][0];

			//	if (action == "pickup") 
			//	{
					// set the same return location:
			//		document.getElementById('dropofflocation').value = arrAC[itemIndex][1];
			//		document.getElementById('dropofflocationId').value = arrAC[itemIndex][0];
			//	}
			
				doc.getElementById(arrAC[itemIndex][0]).click();

				// closing the location section:
				document.getElementById(containerId).style.display = "none";
				closeAcLoc(containerId);

				// reset the values:
				itemIndex = -1;
				itemOldIndex = 0;
				
				
			//	if (fieldId == "dropofflocationBooking")
			//	{
			//		// load the offices for return location in the booking details page:
			//		if (oldDropOffLocationId != document.getElementById('dropofflocationBookingId').value)
			//		{
			//			getOfficeDetail("dropoff", 0, '____');
			//			oldDropOffLocationId = document.getElementById('dropofflocationBookingId').value;
			//			alert(document.getElementById('dropofflocationBookingId').value);
			//		}
			//	}
			}
		}
		
	}
	
	else 
	{
		document.getElementById(containerId).style.display = "none";
		closeAcLoc(containerId);
	}
	
}

