var currentlyOpenListboxes = []; 
 
function ShowDropDownList(listItems,txtDropDown)
{
		toggleListboxVisibility(listItems);
		var listBox = document.getElementById(listItems);
		var txtResult = document.getElementById(txtDropDown);
		txtResult.width = listBox.width;
		var posArray = findPos(txtResult);
		listBox.style.left =posArray[0];
		listBox.style.top = posArray[1] + 20;
		listBox.onclick = function ReadListItemValue()
		{
			if(listBox.selectedIndex >=0)
			{
				txtResult.value = listBox.options[listBox.selectedIndex].text;
				listBox.style.display='none';
			}
		}
}

function findPos(obj)
{
	var posX = obj.offsetLeft;
	var posY = obj.offsetTop;
	while(obj.offsetParent)
	{
		if(obj==document.getElementsByTagName('body')[0])
			break
		else
		{
			posX=posX+obj.offsetParent.offsetLeft;
			posY=posY+obj.offsetParent.offsetTop;
			obj=obj.offsetParent;
		}
	}
	var posArray=[posX,posY];
	return posArray;
}

function toggleListboxVisibility(id)
{
	var listBox = document.getElementById(id);
	if(listBox.style.display=='block') 
		hideListbox(listBox);
	else 
		showListbox(listBox)
}

function hideListbox(listBox)
{
	listBox.style.display = 'none';
	currentlyOpenListboxes[listBox.arrayIndex, 1];
}

function showListbox(listBox)
{
	listBox.style.display = 'block';
	currentlyOpenListboxes[currentlyOpenListboxes.length] = listBox;
	listBox.arrayIndex = currentlyOpenListboxes.length - 1;
}



/* This is used to validate the search in the home page */
function SearchValidation(craftsId, brandsId, projectsId, txtBrand, txtCraft, txtProject)
{
	var craftIndex = document.getElementById(craftsId).selectedIndex;
	var brandsIndex = document.getElementById(brandsId).selectedIndex;
	var projectindex = document.getElementById(projectsId).selectedIndex;
	if(craftIndex <= 0)
	{
		if(alert("Please select a craft type"))
		{
			return true;
		}
		else
		{
		  ShowDropDownList(craftsId,txtCraft);
		  return false;
		}
	}
	else if(brandsIndex <= 0)
	{
		if(alert("Please select a brand type"))
		{
		  return true;
		}
		else
		{
		  ShowDropDownList(brandsId,txtBrand);
		  return false;
		}
	}
	else if(projectindex <= 0)
	{
		if(alert("Please select a project type"))
		{
			return true;
		}
		else
		{
			ShowDropDownList(projectsId,txtProject);
			return false;
		}
	}
	else
		 return true;
}

function SelectDefault(listboxId, textboxId)
{	
	var listbox = document.getElementById(listboxId);	
	
	for(var i=0; i<=listbox.options.length; i++)
	{
		if(listbox.options[i] != null)
		{
			if(listbox.options[i].text == "How To Knit A Stitch")
			{
				document.getElementById(textboxId).value = "How To Knit A Stitch";				
			}
		}
	}
}
