var position = -1;
var suggestions = 0;
var minVisiblePosition = 0;
var maxVisiblePosition = 8;
var countrylist = new Array();
var countries =  new Array();
var check = 0;
var LiveTimer;
var wordTimer;

var TimesKeyIsPressed = 0;
//var cnt = 0;
//var cnt = 0;

function fillLookupField(tags)
{
	document.getElementById("tag_box").innerHTML = tags;
}


var cnt = 1;
function get_words(keypressed)
{
	if(cnt >= 2)
	{
		//alert(cnt);
		clearTimeout(wordTimer);
		wordTimer = setTimeout("lookupFill(document.getElementById('search').value)",350);
		cnt = 1;
	}
	else
	{
	 cnt++;	
	}
	//lookupFill(document.getElementById('search').value);
	//var wordTimer = setTimeout("get_words("+keypressed+")",2000);

}

function lookupFill(search)
{
	x_LoadTagBox(search,fillLookupField);
}


//for usability issue

function fillLookupFieldFake(tags)
{
	document.getElementById("tag_box").innerHTML = tags;
}


function get_wordsFake(keypressed)
{
	lookupFillFake(document.getElementById("search").value);
	TimesKeyIsPressed++;
}

function lookupFillFake(search)
{
	x_LoadTagBoxFake(search,fillLookupFieldFake);
}

// searched for value in an array
function in_array(aInputArray,checkValue)
{
	for(i=0;i<aInputArray.length;i++)
	{
		if(checkValue == aInputArray[i])
		{
			return 1;
			break;
		}
	}
	return 0;
}

//returns the index of the searched key
function array_search(aInputArray,searchedValue)
{
	for(i=0;i<aInputArray.length;i++)
	{
		if(searchedValue == aInputArray[i])
		{
			return i;
			break;
		}
	}
}

//deletes an element from an indexed array and re-indexes the array
function delete_from_array(aInputArray,indexToDelete)
{
	var newLength = aInputArray.length-1;
	var newArray = new Array();

	if(newLength == 0)
	{
		newArray.length = 0;
	}
	else
	{
		for(i=0;i<aInputArray.length;i++)
		{
			if(i != indexToDelete)
			{
				newArray.push(aInputArray[i]);
			}
		}
	}

	return newArray;
}


var tempTagArray = new Array();

function addToQuickSearch(tagElement)
{
	var tempSearch;
	var elementExists;

	elementExists = in_array(tempTagArray,tagElement);

	if(elementExists == 0 && tempTagArray.length < 3)
	{
		tempTagArray.push(tagElement);
		document.getElementById("temp_search_box").innerHTML = "";
		for(z=0;z<tempTagArray.length;z++)
		{
			var clear = tempTagArray[z].replace(/\_/g,"&nbsp;");
			document.getElementById("temp_search_box").innerHTML += "<a href='javascript:void(0);' class=\"tagsPlus\" onclick=\"removeFromQuickSearch('"+tempTagArray[z]+"')\">"+clear+"</a>+&nbsp;";
		}

		postQuickSearch();

		document.getElementById("search").value = "";
		lookupFill(document.getElementById("search").value);
		document.getElementById("h_row").style.visibility = "visible";
	}
}

function removeFromQuickSearch(tagElement)
{
	var tempSearch2;
	searchedElementPosition = array_search(tempTagArray,tagElement);
	returnedArray = delete_from_array(tempTagArray,searchedElementPosition);
	tempTagArray = returnedArray;
	//alert(tempTagArray.toString());

	document.getElementById("temp_search_box").innerHTML = "";

	if(tempTagArray.length > 0)
	{
		for(z=0;z<tempTagArray.length;z++)
		{
			var clear = tempTagArray[z].replace(/\_/g,"&nbsp;");
			document.getElementById("temp_search_box").innerHTML += "<a href='javascript:void(0);' class=\"tagsPlus\" onclick=\"removeFromQuickSearch('"+tempTagArray[z]+"')\">"+clear+"</a>+";
		}
	}
	else
	{
		document.getElementById("temp_search_box").innerHTML = "";
	}

	postQuickSearch();
}

function postQuickSearch()
{
	var hiddenSearch = "";
	var separator;
	var z=1;

	for(i=0;i<tempTagArray.length;i++)
	{
		if(z < tempTagArray.length)
		{
			separator = "-";
		}
		else
		{
			separator = "";
		}

		if(tempTagArray[i] != "undefined")
		{
			hiddenSearch += tempTagArray[i]+separator;
		}
		z++;
	}

	document.getElementById("hidden_search").value = hiddenSearch;
}